Whamcloud - gitweb
LU-11359 mdt: fix mdt_dom_discard_data() timeouts
[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_60h() {
6706         [ $MDS1_VERSION -le $(version_code 2.12.52) ] ||
6707                 skip "Need MDS version at least 2.12.52"
6708         [ $MDSCOUNT -le 2 ] || skip "Need >= 2 MDTs"
6709
6710         local f
6711
6712         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
6713         #define OBD_FAIL_MDS_STRIPE_FID          0x189
6714         for fail_loc in 0x80000188 0x80000189; do
6715                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
6716                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
6717                         error "mkdir $dir-$fail_loc failed"
6718                 for i in {0..10}; do
6719                         # create may fail on missing stripe
6720                         echo $i > $DIR/$tdir-$fail_loc/$i
6721                 done
6722                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6723                         error "getdirstripe $tdir-$fail_loc failed"
6724                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
6725                         error "migrate $tdir-$fail_loc failed"
6726                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6727                         error "getdirstripe $tdir-$fail_loc failed"
6728                 pushd $DIR/$tdir-$fail_loc
6729                 for f in *; do
6730                         echo $f | cmp $f - || error "$f data mismatch"
6731                 done
6732                 popd
6733                 rm -rf $DIR/$tdir-$fail_loc
6734         done
6735 }
6736 run_test 60h "striped directory with missing stripes can be accessed"
6737
6738 test_61a() {
6739         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6740
6741         f="$DIR/f61"
6742         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
6743         cancel_lru_locks osc
6744         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6745         sync
6746 }
6747 run_test 61a "mmap() writes don't make sync hang ================"
6748
6749 test_61b() {
6750         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
6751 }
6752 run_test 61b "mmap() of unstriped file is successful"
6753
6754 # bug 2330 - insufficient obd_match error checking causes LBUG
6755 test_62() {
6756         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6757
6758         f="$DIR/f62"
6759         echo foo > $f
6760         cancel_lru_locks osc
6761         lctl set_param fail_loc=0x405
6762         cat $f && error "cat succeeded, expect -EIO"
6763         lctl set_param fail_loc=0
6764 }
6765 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6766 # match every page all of the time.
6767 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6768
6769 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6770 # Though this test is irrelevant anymore, it helped to reveal some
6771 # other grant bugs (LU-4482), let's keep it.
6772 test_63a() {   # was test_63
6773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6774
6775         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6776
6777         for i in `seq 10` ; do
6778                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6779                 sleep 5
6780                 kill $!
6781                 sleep 1
6782         done
6783
6784         rm -f $DIR/f63 || true
6785 }
6786 run_test 63a "Verify oig_wait interruption does not crash ======="
6787
6788 # bug 2248 - async write errors didn't return to application on sync
6789 # bug 3677 - async write errors left page locked
6790 test_63b() {
6791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6792
6793         debugsave
6794         lctl set_param debug=-1
6795
6796         # ensure we have a grant to do async writes
6797         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6798         rm $DIR/$tfile
6799
6800         sync    # sync lest earlier test intercept the fail_loc
6801
6802         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6803         lctl set_param fail_loc=0x80000406
6804         $MULTIOP $DIR/$tfile Owy && \
6805                 error "sync didn't return ENOMEM"
6806         sync; sleep 2; sync     # do a real sync this time to flush page
6807         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6808                 error "locked page left in cache after async error" || true
6809         debugrestore
6810 }
6811 run_test 63b "async write errors should be returned to fsync ==="
6812
6813 test_64a () {
6814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6815
6816         df $DIR
6817         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6818 }
6819 run_test 64a "verify filter grant calculations (in kernel) ====="
6820
6821 test_64b () {
6822         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6823
6824         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6825 }
6826 run_test 64b "check out-of-space detection on client"
6827
6828 test_64c() {
6829         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6830 }
6831 run_test 64c "verify grant shrink"
6832
6833 # this does exactly what osc_request.c:osc_announce_cached() does in
6834 # order to calculate max amount of grants to ask from server
6835 want_grant() {
6836         local tgt=$1
6837
6838         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6839         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6840
6841         ((rpc_in_flight ++));
6842         nrpages=$((nrpages * rpc_in_flight))
6843
6844         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6845
6846         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
6847
6848         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6849         local undirty=$((nrpages * PAGE_SIZE))
6850
6851         local max_extent_pages
6852         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6853             grep grant_max_extent_size | awk '{print $2}')
6854         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
6855         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6856         local grant_extent_tax
6857         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6858             grep grant_extent_tax | awk '{print $2}')
6859
6860         undirty=$((undirty + nrextents * grant_extent_tax))
6861
6862         echo $undirty
6863 }
6864
6865 # this is size of unit for grant allocation. It should be equal to
6866 # what tgt_grant.c:tgt_grant_chunk() calculates
6867 grant_chunk() {
6868         local tgt=$1
6869         local max_brw_size
6870         local grant_extent_tax
6871
6872         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6873             grep max_brw_size | awk '{print $2}')
6874
6875         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6876             grep grant_extent_tax | awk '{print $2}')
6877
6878         echo $(((max_brw_size + grant_extent_tax) * 2))
6879 }
6880
6881 test_64d() {
6882         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
6883                 skip "OST < 2.10.55 doesn't limit grants enough"
6884
6885         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6886         local file=$DIR/$tfile
6887
6888         [[ $($LCTL get_param osc.${tgt}.import |
6889              grep "connect_flags:.*grant_param") ]] ||
6890                 skip "no grant_param connect flag"
6891
6892         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6893
6894         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6895
6896         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6897         stack_trap "rm -f $file" EXIT
6898
6899         $LFS setstripe $file -i 0 -c 1
6900         dd if=/dev/zero of=$file bs=1M count=1000 &
6901         ddpid=$!
6902
6903         while true
6904         do
6905                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6906                 if [[ $cur_grant -gt $max_cur_granted ]]
6907                 then
6908                         kill $ddpid
6909                         error "cur_grant $cur_grant > $max_cur_granted"
6910                 fi
6911                 kill -0 $ddpid
6912                 [[ $? -ne 0 ]] && break;
6913                 sleep 2
6914         done
6915
6916         rm -f $DIR/$tfile
6917         wait_delete_completed
6918         $LCTL set_param debug="$olddebug" 2> /dev/null || true
6919 }
6920 run_test 64d "check grant limit exceed"
6921
6922 # bug 1414 - set/get directories' stripe info
6923 test_65a() {
6924         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6925
6926         test_mkdir $DIR/$tdir
6927         touch $DIR/$tdir/f1
6928         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
6929 }
6930 run_test 65a "directory with no stripe info"
6931
6932 test_65b() {
6933         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6934
6935         test_mkdir $DIR/$tdir
6936         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
6937
6938         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6939                                                 error "setstripe"
6940         touch $DIR/$tdir/f2
6941         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
6942 }
6943 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
6944
6945 test_65c() {
6946         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6947         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
6948
6949         test_mkdir $DIR/$tdir
6950         local stripesize=$($LFS getstripe -S $DIR/$tdir)
6951
6952         $LFS setstripe -S $((stripesize * 4)) -i 1 \
6953                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
6954         touch $DIR/$tdir/f3
6955         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
6956 }
6957 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
6958
6959 test_65d() {
6960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6961
6962         test_mkdir $DIR/$tdir
6963         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
6964         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
6965
6966         if [[ $STRIPECOUNT -le 0 ]]; then
6967                 sc=1
6968         elif [[ $STRIPECOUNT -gt 2000 ]]; then
6969 #LOV_MAX_STRIPE_COUNT is 2000
6970                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
6971         else
6972                 sc=$(($STRIPECOUNT - 1))
6973         fi
6974         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
6975         touch $DIR/$tdir/f4 $DIR/$tdir/f5
6976         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
6977                 error "lverify failed"
6978 }
6979 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
6980
6981 test_65e() {
6982         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6983
6984         test_mkdir $DIR/$tdir
6985
6986         $LFS setstripe $DIR/$tdir || error "setstripe"
6987         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6988                                         error "no stripe info failed"
6989         touch $DIR/$tdir/f6
6990         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
6991 }
6992 run_test 65e "directory setstripe defaults"
6993
6994 test_65f() {
6995         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6996
6997         test_mkdir $DIR/${tdir}f
6998         $RUNAS $LFS setstripe $DIR/${tdir}f &&
6999                 error "setstripe succeeded" || true
7000 }
7001 run_test 65f "dir setstripe permission (should return error) ==="
7002
7003 test_65g() {
7004         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7005
7006         test_mkdir $DIR/$tdir
7007         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7008
7009         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7010                 error "setstripe -S failed"
7011         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7012         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7013                 error "delete default stripe failed"
7014 }
7015 run_test 65g "directory setstripe -d"
7016
7017 test_65h() {
7018         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7019
7020         test_mkdir $DIR/$tdir
7021         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7022
7023         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7024                 error "setstripe -S failed"
7025         test_mkdir $DIR/$tdir/dd1
7026         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
7027                 error "stripe info inherit failed"
7028 }
7029 run_test 65h "directory stripe info inherit ===================="
7030
7031 test_65i() {
7032         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7033
7034         save_layout_restore_at_exit $MOUNT
7035
7036         # bug6367: set non-default striping on root directory
7037         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7038
7039         # bug12836: getstripe on -1 default directory striping
7040         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7041
7042         # bug12836: getstripe -v on -1 default directory striping
7043         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7044
7045         # bug12836: new find on -1 default directory striping
7046         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7047 }
7048 run_test 65i "various tests to set root directory striping"
7049
7050 test_65j() { # bug6367
7051         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7052
7053         sync; sleep 1
7054
7055         # if we aren't already remounting for each test, do so for this test
7056         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
7057                 cleanup || error "failed to unmount"
7058                 setup
7059         fi
7060
7061         save_layout_restore_at_exit $MOUNT
7062
7063         $LFS setstripe -d $MOUNT || error "setstripe failed"
7064 }
7065 run_test 65j "set default striping on root directory (bug 6367)="
7066
7067 cleanup_65k() {
7068         rm -rf $DIR/$tdir
7069         wait_delete_completed
7070         do_facet $SINGLEMDS "lctl set_param -n \
7071                 osp.$ost*MDT0000.max_create_count=$max_count"
7072         do_facet $SINGLEMDS "lctl set_param -n \
7073                 osp.$ost*MDT0000.create_count=$count"
7074         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7075         echo $INACTIVE_OSC "is Activate"
7076
7077         wait_osc_import_state mds ost$ostnum FULL
7078 }
7079
7080 test_65k() { # bug11679
7081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7082         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7083         remote_mds_nodsh && skip "remote MDS with nodsh"
7084
7085         local disable_precreate=true
7086         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7087                 disable_precreate=false
7088
7089         echo "Check OST status: "
7090         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7091                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7092
7093         for OSC in $MDS_OSCS; do
7094                 echo $OSC "is active"
7095                 do_facet $SINGLEMDS lctl --device %$OSC activate
7096         done
7097
7098         for INACTIVE_OSC in $MDS_OSCS; do
7099                 local ost=$(osc_to_ost $INACTIVE_OSC)
7100                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7101                                lov.*md*.target_obd |
7102                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7103
7104                 mkdir -p $DIR/$tdir
7105                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
7106                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7107
7108                 echo "Deactivate: " $INACTIVE_OSC
7109                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7110
7111                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7112                               osp.$ost*MDT0000.create_count")
7113                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7114                                   osp.$ost*MDT0000.max_create_count")
7115                 $disable_precreate &&
7116                         do_facet $SINGLEMDS "lctl set_param -n \
7117                                 osp.$ost*MDT0000.max_create_count=0"
7118
7119                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7120                         [ -f $DIR/$tdir/$idx ] && continue
7121                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
7122                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
7123                                 { cleanup_65k;
7124                                   error "setstripe $idx should succeed"; }
7125                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7126                 done
7127                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7128                 rmdir $DIR/$tdir
7129
7130                 do_facet $SINGLEMDS "lctl set_param -n \
7131                         osp.$ost*MDT0000.max_create_count=$max_count"
7132                 do_facet $SINGLEMDS "lctl set_param -n \
7133                         osp.$ost*MDT0000.create_count=$count"
7134                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7135                 echo $INACTIVE_OSC "is Activate"
7136
7137                 wait_osc_import_state mds ost$ostnum FULL
7138         done
7139 }
7140 run_test 65k "validate manual striping works properly with deactivated OSCs"
7141
7142 test_65l() { # bug 12836
7143         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7144
7145         test_mkdir -p $DIR/$tdir/test_dir
7146         $LFS setstripe -c -1 $DIR/$tdir/test_dir
7147         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7148 }
7149 run_test 65l "lfs find on -1 stripe dir ========================"
7150
7151 test_65m() {
7152         local layout=$(save_layout $MOUNT)
7153         $RUNAS $LFS setstripe -c 2 $MOUNT && {
7154                 restore_layout $MOUNT $layout
7155                 error "setstripe should fail by non-root users"
7156         }
7157         true
7158 }
7159 run_test 65m "normal user can't set filesystem default stripe"
7160
7161 test_65n() {
7162         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
7163         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.50) ]] ||
7164                 skip "Need MDS version at least 2.12.50"
7165         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7166
7167         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7168         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7169         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7170
7171         local root_layout=$(save_layout $MOUNT)
7172         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7173
7174         # new subdirectory under root directory should not inherit
7175         # the default layout from root
7176         local dir1=$MOUNT/$tdir-1
7177         mkdir $dir1 || error "mkdir $dir1 failed"
7178         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7179                 error "$dir1 shouldn't have LOV EA"
7180
7181         # delete the default layout on root directory
7182         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7183
7184         local dir2=$MOUNT/$tdir-2
7185         mkdir $dir2 || error "mkdir $dir2 failed"
7186         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7187                 error "$dir2 shouldn't have LOV EA"
7188
7189         # set a new striping pattern on root directory
7190         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7191         local new_def_stripe_size=$((def_stripe_size * 2))
7192         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7193                 error "set stripe size on $MOUNT failed"
7194
7195         # new file created in $dir2 should inherit the new stripe size from
7196         # the filesystem default
7197         local file2=$dir2/$tfile-2
7198         touch $file2 || error "touch $file2 failed"
7199
7200         local file2_stripe_size=$($LFS getstripe -S $file2)
7201         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7202                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7203
7204         local dir3=$MOUNT/$tdir-3
7205         mkdir $dir3 || error "mkdir $dir3 failed"
7206         ! getfattr -n trusted.lov $dir3 &> /dev/null ||
7207                 error "$dir3 shouldn't have LOV EA"
7208
7209         # set OST pool on root directory
7210         local pool=$TESTNAME
7211         pool_add $pool || error "add $pool failed"
7212         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7213                 error "add targets to $pool failed"
7214
7215         $LFS setstripe -p $pool $MOUNT ||
7216                 error "set OST pool on $MOUNT failed"
7217
7218         # new file created in $dir3 should inherit the pool from
7219         # the filesystem default
7220         local file3=$dir3/$tfile-3
7221         touch $file3 || error "touch $file3 failed"
7222
7223         local file3_pool=$($LFS getstripe -p $file3)
7224         [[ "$file3_pool" = "$pool" ]] ||
7225                 error "$file3 didn't inherit OST pool $pool"
7226
7227         local dir4=$MOUNT/$tdir-4
7228         mkdir $dir4 || error "mkdir $dir4 failed"
7229         ! getfattr -n trusted.lov $dir4 &> /dev/null ||
7230                 error "$dir4 shouldn't have LOV EA"
7231
7232         # new file created in $dir4 should inherit the pool from
7233         # the filesystem default
7234         local file4=$dir4/$tfile-4
7235         touch $file4 || error "touch $file4 failed"
7236
7237         local file4_pool=$($LFS getstripe -p $file4)
7238         [[ "$file4_pool" = "$pool" ]] ||
7239                 error "$file4 didn't inherit OST pool $pool"
7240
7241         # new subdirectory under non-root directory should inherit
7242         # the default layout from its parent directory
7243         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7244                 error "set directory layout on $dir4 failed"
7245
7246         local dir5=$dir4/$tdir-5
7247         mkdir $dir5 || error "mkdir $dir5 failed"
7248
7249         local dir4_layout=$(get_layout_param $dir4)
7250         local dir5_layout=$(get_layout_param $dir5)
7251         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7252                 error "$dir5 should inherit the default layout from $dir4"
7253 }
7254 run_test 65n "don't inherit default layout from root for new subdirectories"
7255
7256 # bug 2543 - update blocks count on client
7257 test_66() {
7258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7259
7260         COUNT=${COUNT:-8}
7261         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7262         sync; sync_all_data; sync; sync_all_data
7263         cancel_lru_locks osc
7264         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7265         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7266 }
7267 run_test 66 "update inode blocks count on client ==============="
7268
7269 meminfo() {
7270         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7271 }
7272
7273 swap_used() {
7274         swapon -s | awk '($1 == "'$1'") { print $4 }'
7275 }
7276
7277 # bug5265, obdfilter oa2dentry return -ENOENT
7278 # #define OBD_FAIL_SRV_ENOENT 0x217
7279 test_69() {
7280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7281         remote_ost_nodsh && skip "remote OST with nodsh"
7282
7283         f="$DIR/$tfile"
7284         $LFS setstripe -c 1 -i 0 $f
7285
7286         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7287
7288         do_facet ost1 lctl set_param fail_loc=0x217
7289         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7290         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7291
7292         do_facet ost1 lctl set_param fail_loc=0
7293         $DIRECTIO write $f 0 2 || error "write error"
7294
7295         cancel_lru_locks osc
7296         $DIRECTIO read $f 0 1 || error "read error"
7297
7298         do_facet ost1 lctl set_param fail_loc=0x217
7299         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7300
7301         do_facet ost1 lctl set_param fail_loc=0
7302         rm -f $f
7303 }
7304 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
7305
7306 test_71() {
7307         test_mkdir $DIR/$tdir
7308         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
7309         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
7310 }
7311 run_test 71 "Running dbench on lustre (don't segment fault) ===="
7312
7313 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
7314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7315         [ "$RUNAS_ID" = "$UID" ] &&
7316                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7317         # Check that testing environment is properly set up. Skip if not
7318         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
7319                 skip_env "User $RUNAS_ID does not exist - skipping"
7320
7321         touch $DIR/$tfile
7322         chmod 777 $DIR/$tfile
7323         chmod ug+s $DIR/$tfile
7324         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
7325                 error "$RUNAS dd $DIR/$tfile failed"
7326         # See if we are still setuid/sgid
7327         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
7328                 error "S/gid is not dropped on write"
7329         # Now test that MDS is updated too
7330         cancel_lru_locks mdc
7331         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
7332                 error "S/gid is not dropped on MDS"
7333         rm -f $DIR/$tfile
7334 }
7335 run_test 72a "Test that remove suid works properly (bug5695) ===="
7336
7337 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7338         local perm
7339
7340         [ "$RUNAS_ID" = "$UID" ] &&
7341                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7342         [ "$RUNAS_ID" -eq 0 ] &&
7343                 skip_env "RUNAS_ID = 0 -- skipping"
7344         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7345         # Check that testing environment is properly set up. Skip if not
7346         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7347                 skip_env "User $RUNAS_ID does not exist - skipping"
7348
7349         touch $DIR/${tfile}-f{g,u}
7350         test_mkdir $DIR/${tfile}-dg
7351         test_mkdir $DIR/${tfile}-du
7352         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7353         chmod g+s $DIR/${tfile}-{f,d}g
7354         chmod u+s $DIR/${tfile}-{f,d}u
7355         for perm in 777 2777 4777; do
7356                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7357                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7358                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7359                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7360         done
7361         true
7362 }
7363 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7364
7365 # bug 3462 - multiple simultaneous MDC requests
7366 test_73() {
7367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7368
7369         test_mkdir $DIR/d73-1
7370         test_mkdir $DIR/d73-2
7371         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7372         pid1=$!
7373
7374         lctl set_param fail_loc=0x80000129
7375         $MULTIOP $DIR/d73-1/f73-2 Oc &
7376         sleep 1
7377         lctl set_param fail_loc=0
7378
7379         $MULTIOP $DIR/d73-2/f73-3 Oc &
7380         pid3=$!
7381
7382         kill -USR1 $pid1
7383         wait $pid1 || return 1
7384
7385         sleep 25
7386
7387         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7388         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7389         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7390
7391         rm -rf $DIR/d73-*
7392 }
7393 run_test 73 "multiple MDC requests (should not deadlock)"
7394
7395 test_74a() { # bug 6149, 6184
7396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7397
7398         touch $DIR/f74a
7399         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7400         #
7401         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7402         # will spin in a tight reconnection loop
7403         $LCTL set_param fail_loc=0x8000030e
7404         # get any lock that won't be difficult - lookup works.
7405         ls $DIR/f74a
7406         $LCTL set_param fail_loc=0
7407         rm -f $DIR/f74a
7408         true
7409 }
7410 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
7411
7412 test_74b() { # bug 13310
7413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7414
7415         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7416         #
7417         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7418         # will spin in a tight reconnection loop
7419         $LCTL set_param fail_loc=0x8000030e
7420         # get a "difficult" lock
7421         touch $DIR/f74b
7422         $LCTL set_param fail_loc=0
7423         rm -f $DIR/f74b
7424         true
7425 }
7426 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
7427
7428 test_74c() {
7429         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7430
7431         #define OBD_FAIL_LDLM_NEW_LOCK
7432         $LCTL set_param fail_loc=0x319
7433         touch $DIR/$tfile && error "touch successful"
7434         $LCTL set_param fail_loc=0
7435         true
7436 }
7437 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
7438
7439 num_inodes() {
7440         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
7441 }
7442
7443 test_76() { # Now for bug 20433, added originally in bug 1443
7444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7445
7446         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
7447
7448         cancel_lru_locks osc
7449         BEFORE_INODES=$(num_inodes)
7450         echo "before inodes: $BEFORE_INODES"
7451         local COUNT=1000
7452         [ "$SLOW" = "no" ] && COUNT=100
7453         for i in $(seq $COUNT); do
7454                 touch $DIR/$tfile
7455                 rm -f $DIR/$tfile
7456         done
7457         cancel_lru_locks osc
7458         AFTER_INODES=$(num_inodes)
7459         echo "after inodes: $AFTER_INODES"
7460         local wait=0
7461         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
7462                 sleep 2
7463                 AFTER_INODES=$(num_inodes)
7464                 wait=$((wait+2))
7465                 echo "wait $wait seconds inodes: $AFTER_INODES"
7466                 if [ $wait -gt 30 ]; then
7467                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
7468                 fi
7469         done
7470 }
7471 run_test 76 "confirm clients recycle inodes properly ===="
7472
7473
7474 export ORIG_CSUM=""
7475 set_checksums()
7476 {
7477         # Note: in sptlrpc modes which enable its own bulk checksum, the
7478         # original crc32_le bulk checksum will be automatically disabled,
7479         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
7480         # will be checked by sptlrpc code against sptlrpc bulk checksum.
7481         # In this case set_checksums() will not be no-op, because sptlrpc
7482         # bulk checksum will be enabled all through the test.
7483
7484         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
7485         lctl set_param -n osc.*.checksums $1
7486         return 0
7487 }
7488
7489 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7490                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
7491 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7492                              tr -d [] | head -n1)}
7493 set_checksum_type()
7494 {
7495         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
7496         log "set checksum type to $1"
7497         return 0
7498 }
7499 F77_TMP=$TMP/f77-temp
7500 F77SZ=8
7501 setup_f77() {
7502         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
7503                 error "error writing to $F77_TMP"
7504 }
7505
7506 test_77a() { # bug 10889
7507         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7508         $GSS && skip_env "could not run with gss"
7509
7510         [ ! -f $F77_TMP ] && setup_f77
7511         set_checksums 1
7512         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
7513         set_checksums 0
7514         rm -f $DIR/$tfile
7515 }
7516 run_test 77a "normal checksum read/write operation"
7517
7518 test_77b() { # bug 10889
7519         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7520         $GSS && skip_env "could not run with gss"
7521
7522         [ ! -f $F77_TMP ] && setup_f77
7523         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7524         $LCTL set_param fail_loc=0x80000409
7525         set_checksums 1
7526
7527         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7528                 error "dd error: $?"
7529         $LCTL set_param fail_loc=0
7530
7531         for algo in $CKSUM_TYPES; do
7532                 cancel_lru_locks osc
7533                 set_checksum_type $algo
7534                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7535                 $LCTL set_param fail_loc=0x80000408
7536                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7537                 $LCTL set_param fail_loc=0
7538         done
7539         set_checksums 0
7540         set_checksum_type $ORIG_CSUM_TYPE
7541         rm -f $DIR/$tfile
7542 }
7543 run_test 77b "checksum error on client write, read"
7544
7545 cleanup_77c() {
7546         trap 0
7547         set_checksums 0
7548         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7549         $check_ost &&
7550                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7551         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7552         $check_ost && [ -n "$ost_file_prefix" ] &&
7553                 do_facet ost1 rm -f ${ost_file_prefix}\*
7554 }
7555
7556 test_77c() {
7557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7558         $GSS && skip_env "could not run with gss"
7559         remote_ost_nodsh && skip "remote OST with nodsh"
7560
7561         local bad1
7562         local osc_file_prefix
7563         local osc_file
7564         local check_ost=false
7565         local ost_file_prefix
7566         local ost_file
7567         local orig_cksum
7568         local dump_cksum
7569         local fid
7570
7571         # ensure corruption will occur on first OSS/OST
7572         $LFS setstripe -i 0 $DIR/$tfile
7573
7574         [ ! -f $F77_TMP ] && setup_f77
7575         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7576                 error "dd write error: $?"
7577         fid=$($LFS path2fid $DIR/$tfile)
7578
7579         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
7580         then
7581                 check_ost=true
7582                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7583                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7584         else
7585                 echo "OSS do not support bulk pages dump upon error"
7586         fi
7587
7588         osc_file_prefix=$($LCTL get_param -n debug_path)
7589         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7590
7591         trap cleanup_77c EXIT
7592
7593         set_checksums 1
7594         # enable bulk pages dump upon error on Client
7595         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7596         # enable bulk pages dump upon error on OSS
7597         $check_ost &&
7598                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7599
7600         # flush Client cache to allow next read to reach OSS
7601         cancel_lru_locks osc
7602
7603         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7604         $LCTL set_param fail_loc=0x80000408
7605         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7606         $LCTL set_param fail_loc=0
7607
7608         rm -f $DIR/$tfile
7609
7610         # check cksum dump on Client
7611         osc_file=$(ls ${osc_file_prefix}*)
7612         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7613         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7614         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7615         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7616         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7617                      cksum)
7618         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7619         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7620                 error "dump content does not match on Client"
7621
7622         $check_ost || skip "No need to check cksum dump on OSS"
7623
7624         # check cksum dump on OSS
7625         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7626         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7627         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7628         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7629         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7630                 error "dump content does not match on OSS"
7631
7632         cleanup_77c
7633 }
7634 run_test 77c "checksum error on client read with debug"
7635
7636 test_77d() { # bug 10889
7637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7638         $GSS && skip_env "could not run with gss"
7639
7640         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7641         $LCTL set_param fail_loc=0x80000409
7642         set_checksums 1
7643         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7644                 error "direct write: rc=$?"
7645         $LCTL set_param fail_loc=0
7646         set_checksums 0
7647
7648         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7649         $LCTL set_param fail_loc=0x80000408
7650         set_checksums 1
7651         cancel_lru_locks osc
7652         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7653                 error "direct read: rc=$?"
7654         $LCTL set_param fail_loc=0
7655         set_checksums 0
7656 }
7657 run_test 77d "checksum error on OST direct write, read"
7658
7659 test_77f() { # bug 10889
7660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7661         $GSS && skip_env "could not run with gss"
7662
7663         set_checksums 1
7664         for algo in $CKSUM_TYPES; do
7665                 cancel_lru_locks osc
7666                 set_checksum_type $algo
7667                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7668                 $LCTL set_param fail_loc=0x409
7669                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
7670                         error "direct write succeeded"
7671                 $LCTL set_param fail_loc=0
7672         done
7673         set_checksum_type $ORIG_CSUM_TYPE
7674         set_checksums 0
7675 }
7676 run_test 77f "repeat checksum error on write (expect error)"
7677
7678 test_77g() { # bug 10889
7679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7680         $GSS && skip_env "could not run with gss"
7681         remote_ost_nodsh && skip "remote OST with nodsh"
7682
7683         [ ! -f $F77_TMP ] && setup_f77
7684
7685         local file=$DIR/$tfile
7686         stack_trap "rm -f $file" EXIT
7687
7688         $LFS setstripe -c 1 -i 0 $file
7689         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
7690         do_facet ost1 lctl set_param fail_loc=0x8000021a
7691         set_checksums 1
7692         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
7693                 error "write error: rc=$?"
7694         do_facet ost1 lctl set_param fail_loc=0
7695         set_checksums 0
7696
7697         cancel_lru_locks osc
7698         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
7699         do_facet ost1 lctl set_param fail_loc=0x8000021b
7700         set_checksums 1
7701         cmp $F77_TMP $file || error "file compare failed"
7702         do_facet ost1 lctl set_param fail_loc=0
7703         set_checksums 0
7704 }
7705 run_test 77g "checksum error on OST write, read"
7706
7707 test_77k() { # LU-10906
7708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7709         $GSS && skip_env "could not run with gss"
7710
7711         local cksum_param="osc.$FSNAME*.checksums"
7712         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
7713         local checksum
7714         local i
7715
7716         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
7717         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
7718         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
7719                 EXIT
7720
7721         for i in 0 1; do
7722                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
7723                         error "failed to set checksum=$i on MGS"
7724                 wait_update $HOSTNAME "$get_checksum" $i
7725                 #remount
7726                 echo "remount client, checksum should be $i"
7727                 remount_client $MOUNT || "failed to remount client"
7728                 checksum=$(eval $get_checksum)
7729                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7730         done
7731         # remove persistent param to avoid races with checksum mountopt below
7732         do_facet mgs $LCTL set_param -P -d $cksum_param ||
7733                 error "failed to delete checksum on MGS"
7734
7735         for opt in "checksum" "nochecksum"; do
7736                 #remount with mount option
7737                 echo "remount client with option $opt, checksum should be $i"
7738                 umount_client $MOUNT || "failed to umount client"
7739                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
7740                         "failed to mount client with option '$opt'"
7741                 checksum=$(eval $get_checksum)
7742                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7743                 i=$((i - 1))
7744         done
7745
7746         remount_client $MOUNT || "failed to remount client"
7747 }
7748 run_test 77k "enable/disable checksum correctly"
7749
7750 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
7751 rm -f $F77_TMP
7752 unset F77_TMP
7753
7754 cleanup_test_78() {
7755         trap 0
7756         rm -f $DIR/$tfile
7757 }
7758
7759 test_78() { # bug 10901
7760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7761         remote_ost || skip_env "local OST"
7762
7763         NSEQ=5
7764         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
7765         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
7766         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
7767         echo "MemTotal: $MEMTOTAL"
7768
7769         # reserve 256MB of memory for the kernel and other running processes,
7770         # and then take 1/2 of the remaining memory for the read/write buffers.
7771         if [ $MEMTOTAL -gt 512 ] ;then
7772                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
7773         else
7774                 # for those poor memory-starved high-end clusters...
7775                 MEMTOTAL=$((MEMTOTAL / 2))
7776         fi
7777         echo "Mem to use for directio: $MEMTOTAL"
7778
7779         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
7780         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
7781         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
7782         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
7783                 head -n1)
7784         echo "Smallest OST: $SMALLESTOST"
7785         [[ $SMALLESTOST -lt 10240 ]] &&
7786                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
7787
7788         trap cleanup_test_78 EXIT
7789
7790         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
7791                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
7792
7793         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
7794         echo "File size: $F78SIZE"
7795         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
7796         for i in $(seq 1 $NSEQ); do
7797                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
7798                 echo directIO rdwr round $i of $NSEQ
7799                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
7800         done
7801
7802         cleanup_test_78
7803 }
7804 run_test 78 "handle large O_DIRECT writes correctly ============"
7805
7806 test_79() { # bug 12743
7807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7808
7809         wait_delete_completed
7810
7811         BKTOTAL=$(calc_osc_kbytes kbytestotal)
7812         BKFREE=$(calc_osc_kbytes kbytesfree)
7813         BKAVAIL=$(calc_osc_kbytes kbytesavail)
7814
7815         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
7816         DFTOTAL=`echo $STRING | cut -d, -f1`
7817         DFUSED=`echo $STRING  | cut -d, -f2`
7818         DFAVAIL=`echo $STRING | cut -d, -f3`
7819         DFFREE=$(($DFTOTAL - $DFUSED))
7820
7821         ALLOWANCE=$((64 * $OSTCOUNT))
7822
7823         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
7824            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
7825                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
7826         fi
7827         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
7828            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
7829                 error "df free($DFFREE) mismatch OST free($BKFREE)"
7830         fi
7831         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
7832            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
7833                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
7834         fi
7835 }
7836 run_test 79 "df report consistency check ======================="
7837
7838 test_80() { # bug 10718
7839         remote_ost_nodsh && skip "remote OST with nodsh"
7840         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7841
7842         # relax strong synchronous semantics for slow backends like ZFS
7843         local soc="obdfilter.*.sync_on_lock_cancel"
7844         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
7845         local hosts=
7846         if [ "$soc_old" != "never" ] &&
7847                 [ "$ost1_FSTYPE" != "ldiskfs" ]; then
7848                         hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
7849                                 facet_active_host $host; done | sort -u)
7850                         do_nodes $hosts lctl set_param $soc=never
7851         fi
7852
7853         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
7854         sync; sleep 1; sync
7855         local BEFORE=`date +%s`
7856         cancel_lru_locks osc
7857         local AFTER=`date +%s`
7858         local DIFF=$((AFTER-BEFORE))
7859         if [ $DIFF -gt 1 ] ; then
7860                 error "elapsed for 1M@1T = $DIFF"
7861         fi
7862
7863         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
7864
7865         rm -f $DIR/$tfile
7866 }
7867 run_test 80 "Page eviction is equally fast at high offsets too  ===="
7868
7869 test_81a() { # LU-456
7870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7871         remote_ost_nodsh && skip "remote OST with nodsh"
7872
7873         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7874         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
7875         do_facet ost1 lctl set_param fail_loc=0x80000228
7876
7877         # write should trigger a retry and success
7878         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7879         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7880         RC=$?
7881         if [ $RC -ne 0 ] ; then
7882                 error "write should success, but failed for $RC"
7883         fi
7884 }
7885 run_test 81a "OST should retry write when get -ENOSPC ==============="
7886
7887 test_81b() { # LU-456
7888         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7889         remote_ost_nodsh && skip "remote OST with nodsh"
7890
7891         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7892         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
7893         do_facet ost1 lctl set_param fail_loc=0x228
7894
7895         # write should retry several times and return -ENOSPC finally
7896         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7897         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7898         RC=$?
7899         ENOSPC=28
7900         if [ $RC -ne $ENOSPC ] ; then
7901                 error "dd should fail for -ENOSPC, but succeed."
7902         fi
7903 }
7904 run_test 81b "OST should return -ENOSPC when retry still fails ======="
7905
7906 test_82() { # LU-1031
7907         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
7908         local gid1=14091995
7909         local gid2=16022000
7910
7911         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
7912         local MULTIPID1=$!
7913         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
7914         local MULTIPID2=$!
7915         kill -USR1 $MULTIPID2
7916         sleep 2
7917         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
7918                 error "First grouplock does not block second one"
7919         else
7920                 echo "Second grouplock blocks first one"
7921         fi
7922         kill -USR1 $MULTIPID1
7923         wait $MULTIPID1
7924         wait $MULTIPID2
7925 }
7926 run_test 82 "Basic grouplock test"
7927
7928 test_99() {
7929         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
7930
7931         test_mkdir $DIR/$tdir.cvsroot
7932         chown $RUNAS_ID $DIR/$tdir.cvsroot
7933
7934         cd $TMP
7935         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
7936
7937         cd /etc/init.d
7938         # some versions of cvs import exit(1) when asked to import links or
7939         # files they can't read.  ignore those files.
7940         local toignore=$(find . -type l -printf '-I %f\n' -o \
7941                          ! -perm /4 -printf '-I %f\n')
7942         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
7943                 $tdir.reposname vtag rtag
7944
7945         cd $DIR
7946         test_mkdir $DIR/$tdir.reposname
7947         chown $RUNAS_ID $DIR/$tdir.reposname
7948         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
7949
7950         cd $DIR/$tdir.reposname
7951         $RUNAS touch foo99
7952         $RUNAS cvs add -m 'addmsg' foo99
7953         $RUNAS cvs update
7954         $RUNAS cvs commit -m 'nomsg' foo99
7955         rm -fr $DIR/$tdir.cvsroot
7956 }
7957 run_test 99 "cvs strange file/directory operations"
7958
7959 test_100() {
7960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7961         [[ "$NETTYPE" =~ tcp ]] ||
7962                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
7963         remote_ost_nodsh && skip "remote OST with nodsh"
7964         remote_mds_nodsh && skip "remote MDS with nodsh"
7965         remote_servers ||
7966                 skip "useless for local single node setup"
7967
7968         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
7969                 [ "$PROT" != "tcp" ] && continue
7970                 RPORT=$(echo $REMOTE | cut -d: -f2)
7971                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
7972
7973                 rc=0
7974                 LPORT=`echo $LOCAL | cut -d: -f2`
7975                 if [ $LPORT -ge 1024 ]; then
7976                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
7977                         netstat -tna
7978                         error_exit "local: $LPORT > 1024, remote: $RPORT"
7979                 fi
7980         done
7981         [ "$rc" = 0 ] || error_exit "privileged port not found" )
7982 }
7983 run_test 100 "check local port using privileged port ==========="
7984
7985 function get_named_value()
7986 {
7987     local tag
7988
7989     tag=$1
7990     while read ;do
7991         line=$REPLY
7992         case $line in
7993         $tag*)
7994             echo $line | sed "s/^$tag[ ]*//"
7995             break
7996             ;;
7997         esac
7998     done
7999 }
8000
8001 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8002                    awk '/^max_cached_mb/ { print $2 }')
8003
8004 cleanup_101a() {
8005         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8006         trap 0
8007 }
8008
8009 test_101a() {
8010         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8011         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
8012
8013         local s
8014         local discard
8015         local nreads=10000
8016         local cache_limit=32
8017
8018         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8019         trap cleanup_101a EXIT
8020         $LCTL set_param -n llite.*.read_ahead_stats 0
8021         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8022
8023         #
8024         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8025         #
8026         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8027         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8028
8029         discard=0
8030         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8031                 get_named_value 'read but discarded' | cut -d" " -f1); do
8032                         discard=$(($discard + $s))
8033         done
8034         cleanup_101a
8035
8036         if [[ $(($discard * 10)) -gt $nreads ]]; then
8037                 $LCTL get_param osc.*-osc*.rpc_stats
8038                 $LCTL get_param llite.*.read_ahead_stats
8039                 error "too many ($discard) discarded pages"
8040         fi
8041         rm -f $DIR/$tfile || true
8042 }
8043 run_test 101a "check read-ahead for random reads"
8044
8045 setup_test101bc() {
8046         test_mkdir $DIR/$tdir
8047         local ssize=$1
8048         local FILE_LENGTH=$2
8049         STRIPE_OFFSET=0
8050
8051         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
8052
8053         local list=$(comma_list $(osts_nodes))
8054         set_osd_param $list '' read_cache_enable 0
8055         set_osd_param $list '' writethrough_cache_enable 0
8056
8057         trap cleanup_test101bc EXIT
8058         # prepare the read-ahead file
8059         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8060
8061         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
8062                                 count=$FILE_SIZE_MB 2> /dev/null
8063
8064 }
8065
8066 cleanup_test101bc() {
8067         trap 0
8068         rm -rf $DIR/$tdir
8069         rm -f $DIR/$tfile
8070
8071         local list=$(comma_list $(osts_nodes))
8072         set_osd_param $list '' read_cache_enable 1
8073         set_osd_param $list '' writethrough_cache_enable 1
8074 }
8075
8076 calc_total() {
8077         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8078 }
8079
8080 ra_check_101() {
8081         local READ_SIZE=$1
8082         local STRIPE_SIZE=$2
8083         local FILE_LENGTH=$3
8084         local RA_INC=1048576
8085         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8086         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8087                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8088         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8089                         get_named_value 'read but discarded' |
8090                         cut -d" " -f1 | calc_total)
8091         if [[ $DISCARD -gt $discard_limit ]]; then
8092                 $LCTL get_param llite.*.read_ahead_stats
8093                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8094         else
8095                 echo "Read-ahead success for size ${READ_SIZE}"
8096         fi
8097 }
8098
8099 test_101b() {
8100         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8101         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8102
8103         local STRIPE_SIZE=1048576
8104         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8105
8106         if [ $SLOW == "yes" ]; then
8107                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8108         else
8109                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8110         fi
8111
8112         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8113
8114         # prepare the read-ahead file
8115         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8116         cancel_lru_locks osc
8117         for BIDX in 2 4 8 16 32 64 128 256
8118         do
8119                 local BSIZE=$((BIDX*4096))
8120                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8121                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8122                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8123                 $LCTL set_param -n llite.*.read_ahead_stats 0
8124                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8125                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8126                 cancel_lru_locks osc
8127                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8128         done
8129         cleanup_test101bc
8130         true
8131 }
8132 run_test 101b "check stride-io mode read-ahead ================="
8133
8134 test_101c() {
8135         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8136
8137         local STRIPE_SIZE=1048576
8138         local FILE_LENGTH=$((STRIPE_SIZE*100))
8139         local nreads=10000
8140         local rsize=65536
8141         local osc_rpc_stats
8142
8143         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8144
8145         cancel_lru_locks osc
8146         $LCTL set_param osc.*.rpc_stats 0
8147         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8148         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8149                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8150                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8151                 local size
8152
8153                 if [ $lines -le 20 ]; then
8154                         continue
8155                 fi
8156                 for size in 1 2 4 8; do
8157                         local rpc=$(echo "$stats" |
8158                                     awk '($1 == "'$size':") {print $2; exit; }')
8159                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8160                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8161                 done
8162                 echo "$osc_rpc_stats check passed!"
8163         done
8164         cleanup_test101bc
8165         true
8166 }
8167 run_test 101c "check stripe_size aligned read-ahead ================="
8168
8169 set_read_ahead() {
8170         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8171         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8172 }
8173
8174 test_101d() {
8175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8176
8177         local file=$DIR/$tfile
8178         local sz_MB=${FILESIZE_101d:-500}
8179         local ra_MB=${READAHEAD_MB:-40}
8180
8181         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8182         [ $free_MB -lt $sz_MB ] &&
8183                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8184
8185         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8186         $LFS setstripe -c -1 $file || error "setstripe failed"
8187
8188         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8189         echo Cancel LRU locks on lustre client to flush the client cache
8190         cancel_lru_locks osc
8191
8192         echo Disable read-ahead
8193         local old_READAHEAD=$(set_read_ahead 0)
8194
8195         echo Reading the test file $file with read-ahead disabled
8196         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8197
8198         echo Cancel LRU locks on lustre client to flush the client cache
8199         cancel_lru_locks osc
8200         echo Enable read-ahead with ${ra_MB}MB
8201         set_read_ahead $ra_MB
8202
8203         echo Reading the test file $file with read-ahead enabled
8204         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8205
8206         echo "read-ahead disabled time read $raOFF"
8207         echo "read-ahead enabled  time read $raON"
8208
8209         set_read_ahead $old_READAHEAD
8210         rm -f $file
8211         wait_delete_completed
8212
8213         [ $raOFF -le 1 ] || [ $raON -lt $raOFF ] ||
8214                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8215 }
8216 run_test 101d "file read with and without read-ahead enabled"
8217
8218 test_101e() {
8219         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8220
8221         local file=$DIR/$tfile
8222         local size_KB=500  #KB
8223         local count=100
8224         local bsize=1024
8225
8226         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8227         local need_KB=$((count * size_KB))
8228         [[ $free_KB -le $need_KB ]] &&
8229                 skip_env "Need free space $need_KB, have $free_KB"
8230
8231         echo "Creating $count ${size_KB}K test files"
8232         for ((i = 0; i < $count; i++)); do
8233                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8234         done
8235
8236         echo "Cancel LRU locks on lustre client to flush the client cache"
8237         cancel_lru_locks $OSC
8238
8239         echo "Reset readahead stats"
8240         $LCTL set_param -n llite.*.read_ahead_stats 0
8241
8242         for ((i = 0; i < $count; i++)); do
8243                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8244         done
8245
8246         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8247                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8248
8249         for ((i = 0; i < $count; i++)); do
8250                 rm -rf $file.$i 2>/dev/null
8251         done
8252
8253         #10000 means 20% reads are missing in readahead
8254         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8255 }
8256 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
8257
8258 test_101f() {
8259         which iozone || skip_env "no iozone installed"
8260
8261         local old_debug=$($LCTL get_param debug)
8262         old_debug=${old_debug#*=}
8263         $LCTL set_param debug="reada mmap"
8264
8265         # create a test file
8266         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
8267
8268         echo Cancel LRU locks on lustre client to flush the client cache
8269         cancel_lru_locks osc
8270
8271         echo Reset readahead stats
8272         $LCTL set_param -n llite.*.read_ahead_stats 0
8273
8274         echo mmap read the file with small block size
8275         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
8276                 > /dev/null 2>&1
8277
8278         echo checking missing pages
8279         $LCTL get_param llite.*.read_ahead_stats
8280         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8281                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
8282
8283         $LCTL set_param debug="$old_debug"
8284         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
8285         rm -f $DIR/$tfile
8286 }
8287 run_test 101f "check mmap read performance"
8288
8289 test_101g_brw_size_test() {
8290         local mb=$1
8291         local pages=$((mb * 1048576 / PAGE_SIZE))
8292         local file=$DIR/$tfile
8293
8294         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
8295                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
8296         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
8297                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
8298                         return 2
8299         done
8300
8301         stack_trap "rm -f $file" EXIT
8302         $LCTL set_param -n osc.*.rpc_stats=0
8303
8304         # 10 RPCs should be enough for the test
8305         local count=10
8306         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
8307                 { error "dd write ${mb} MB blocks failed"; return 3; }
8308         cancel_lru_locks osc
8309         dd of=/dev/null if=$file bs=${mb}M count=$count ||
8310                 { error "dd write ${mb} MB blocks failed"; return 4; }
8311
8312         # calculate number of full-sized read and write RPCs
8313         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
8314                 sed -n '/pages per rpc/,/^$/p' |
8315                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
8316                 END { print reads,writes }'))
8317         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
8318                 return 5
8319         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
8320                 return 6
8321
8322         return 0
8323 }
8324
8325 test_101g() {
8326         remote_ost_nodsh && skip "remote OST with nodsh"
8327
8328         local rpcs
8329         local osts=$(get_facets OST)
8330         local list=$(comma_list $(osts_nodes))
8331         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
8332         local brw_size="obdfilter.*.brw_size"
8333
8334         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8335
8336         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
8337
8338         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
8339                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
8340                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
8341            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
8342                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
8343                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
8344
8345                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
8346                         suffix="M"
8347
8348                 if [[ $orig_mb -lt 16 ]]; then
8349                         save_lustre_params $osts "$brw_size" > $p
8350                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8351                                 error "set 16MB RPC size failed"
8352
8353                         echo "remount client to enable new RPC size"
8354                         remount_client $MOUNT || error "remount_client failed"
8355                 fi
8356
8357                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8358                 # should be able to set brw_size=12, but no rpc_stats for that
8359                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
8360         fi
8361
8362         test_101g_brw_size_test 4 || error "4MB RPC test failed"
8363
8364         if [[ $orig_mb -lt 16 ]]; then
8365                 restore_lustre_params < $p
8366                 remount_client $MOUNT || error "remount_client restore failed"
8367         fi
8368
8369         rm -f $p $DIR/$tfile
8370 }
8371 run_test 101g "Big bulk(4/16 MiB) readahead"
8372
8373 setup_test102() {
8374         test_mkdir $DIR/$tdir
8375         chown $RUNAS_ID $DIR/$tdir
8376         STRIPE_SIZE=65536
8377         STRIPE_OFFSET=1
8378         STRIPE_COUNT=$OSTCOUNT
8379         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
8380
8381         trap cleanup_test102 EXIT
8382         cd $DIR
8383         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
8384         cd $DIR/$tdir
8385         for num in 1 2 3 4; do
8386                 for count in $(seq 1 $STRIPE_COUNT); do
8387                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
8388                                 local size=`expr $STRIPE_SIZE \* $num`
8389                                 local file=file"$num-$idx-$count"
8390                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
8391                         done
8392                 done
8393         done
8394
8395         cd $DIR
8396         $1 tar cf $TMP/f102.tar $tdir --xattrs
8397 }
8398
8399 cleanup_test102() {
8400         trap 0
8401         rm -f $TMP/f102.tar
8402         rm -rf $DIR/d0.sanity/d102
8403 }
8404
8405 test_102a() {
8406         [ "$UID" != 0 ] && skip "must run as root"
8407         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
8408                 skip_env "must have user_xattr"
8409
8410         [ -z "$(which setfattr 2>/dev/null)" ] &&
8411                 skip_env "could not find setfattr"
8412
8413         local testfile=$DIR/$tfile
8414
8415         touch $testfile
8416         echo "set/get xattr..."
8417         setfattr -n trusted.name1 -v value1 $testfile ||
8418                 error "setfattr -n trusted.name1=value1 $testfile failed"
8419         getfattr -n trusted.name1 $testfile 2> /dev/null |
8420           grep "trusted.name1=.value1" ||
8421                 error "$testfile missing trusted.name1=value1"
8422
8423         setfattr -n user.author1 -v author1 $testfile ||
8424                 error "setfattr -n user.author1=author1 $testfile failed"
8425         getfattr -n user.author1 $testfile 2> /dev/null |
8426           grep "user.author1=.author1" ||
8427                 error "$testfile missing trusted.author1=author1"
8428
8429         echo "listxattr..."
8430         setfattr -n trusted.name2 -v value2 $testfile ||
8431                 error "$testfile unable to set trusted.name2"
8432         setfattr -n trusted.name3 -v value3 $testfile ||
8433                 error "$testfile unable to set trusted.name3"
8434         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
8435             grep "trusted.name" | wc -l) -eq 3 ] ||
8436                 error "$testfile missing 3 trusted.name xattrs"
8437
8438         setfattr -n user.author2 -v author2 $testfile ||
8439                 error "$testfile unable to set user.author2"
8440         setfattr -n user.author3 -v author3 $testfile ||
8441                 error "$testfile unable to set user.author3"
8442         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
8443             grep "user.author" | wc -l) -eq 3 ] ||
8444                 error "$testfile missing 3 user.author xattrs"
8445
8446         echo "remove xattr..."
8447         setfattr -x trusted.name1 $testfile ||
8448                 error "$testfile error deleting trusted.name1"
8449         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
8450                 error "$testfile did not delete trusted.name1 xattr"
8451
8452         setfattr -x user.author1 $testfile ||
8453                 error "$testfile error deleting user.author1"
8454         echo "set lustre special xattr ..."
8455         $LFS setstripe -c1 $testfile
8456         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
8457                 awk -F "=" '/trusted.lov/ { print $2 }' )
8458         setfattr -n "trusted.lov" -v $lovea $testfile ||
8459                 error "$testfile doesn't ignore setting trusted.lov again"
8460         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
8461                 error "$testfile allow setting invalid trusted.lov"
8462         rm -f $testfile
8463 }
8464 run_test 102a "user xattr test =================================="
8465
8466 test_102b() {
8467         [ -z "$(which setfattr 2>/dev/null)" ] &&
8468                 skip_env "could not find setfattr"
8469         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8470
8471         # b10930: get/set/list trusted.lov xattr
8472         echo "get/set/list trusted.lov xattr ..."
8473         local testfile=$DIR/$tfile
8474         $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8475                 error "setstripe failed"
8476         local STRIPECOUNT=$($LFS getstripe -c $testfile) ||
8477                 error "getstripe failed"
8478         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
8479                 error "can't get trusted.lov from $testfile"
8480
8481         local testfile2=${testfile}2
8482         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
8483                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
8484
8485         $MCREATE $testfile2
8486         setfattr -n trusted.lov -v $value $testfile2
8487         local stripe_size=$($LFS getstripe -S $testfile2)
8488         local stripe_count=$($LFS getstripe -c $testfile2)
8489         [[ $stripe_size -eq 65536 ]] ||
8490                 error "stripe size $stripe_size != 65536"
8491         [[ $stripe_count -eq $STRIPECOUNT ]] ||
8492                 error "stripe count $stripe_count != $STRIPECOUNT"
8493         rm -f $DIR/$tfile
8494 }
8495 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
8496
8497 test_102c() {
8498         [ -z "$(which setfattr 2>/dev/null)" ] &&
8499                 skip_env "could not find setfattr"
8500         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8501
8502         # b10930: get/set/list lustre.lov xattr
8503         echo "get/set/list lustre.lov xattr ..."
8504         test_mkdir $DIR/$tdir
8505         chown $RUNAS_ID $DIR/$tdir
8506         local testfile=$DIR/$tdir/$tfile
8507         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8508                 error "setstripe failed"
8509         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
8510                 error "getstripe failed"
8511         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
8512         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8513
8514         local testfile2=${testfile}2
8515         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8516                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8517
8518         $RUNAS $MCREATE $testfile2
8519         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8520         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
8521         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
8522         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8523         [ $stripe_count -eq $STRIPECOUNT ] ||
8524                 error "stripe count $stripe_count != $STRIPECOUNT"
8525 }
8526 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8527
8528 compare_stripe_info1() {
8529         local stripe_index_all_zero=true
8530
8531         for num in 1 2 3 4; do
8532                 for count in $(seq 1 $STRIPE_COUNT); do
8533                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8534                                 local size=$((STRIPE_SIZE * num))
8535                                 local file=file"$num-$offset-$count"
8536                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8537                                 [[ $stripe_size -ne $size ]] &&
8538                                     error "$file: size $stripe_size != $size"
8539                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8540                                 # allow fewer stripes to be created, ORI-601
8541                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8542                                     error "$file: count $stripe_count != $count"
8543                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8544                                 [[ $stripe_index -ne 0 ]] &&
8545                                         stripe_index_all_zero=false
8546                         done
8547                 done
8548         done
8549         $stripe_index_all_zero &&
8550                 error "all files are being extracted starting from OST index 0"
8551         return 0
8552 }
8553
8554 have_xattrs_include() {
8555         tar --help | grep -q xattrs-include &&
8556                 echo --xattrs-include="lustre.*"
8557 }
8558
8559 test_102d() {
8560         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8561         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8562
8563         XINC=$(have_xattrs_include)
8564         setup_test102
8565         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8566         cd $DIR/$tdir/$tdir
8567         compare_stripe_info1
8568 }
8569 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8570
8571 test_102f() {
8572         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8573         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8574
8575         XINC=$(have_xattrs_include)
8576         setup_test102
8577         test_mkdir $DIR/$tdir.restore
8578         cd $DIR
8579         tar cf - --xattrs $tdir | tar xf - \
8580                 -C $DIR/$tdir.restore --xattrs $XINC
8581         cd $DIR/$tdir.restore/$tdir
8582         compare_stripe_info1
8583 }
8584 run_test 102f "tar copy files, not keep osts"
8585
8586 grow_xattr() {
8587         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8588                 skip "must have user_xattr"
8589         [ -z "$(which setfattr 2>/dev/null)" ] &&
8590                 skip_env "could not find setfattr"
8591         [ -z "$(which getfattr 2>/dev/null)" ] &&
8592                 skip_env "could not find getfattr"
8593
8594         local xsize=${1:-1024}  # in bytes
8595         local file=$DIR/$tfile
8596         local value="$(generate_string $xsize)"
8597         local xbig=trusted.big
8598         local toobig=$2
8599
8600         touch $file
8601         log "save $xbig on $file"
8602         if [ -z "$toobig" ]
8603         then
8604                 setfattr -n $xbig -v $value $file ||
8605                         error "saving $xbig on $file failed"
8606         else
8607                 setfattr -n $xbig -v $value $file &&
8608                         error "saving $xbig on $file succeeded"
8609                 return 0
8610         fi
8611
8612         local orig=$(get_xattr_value $xbig $file)
8613         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8614
8615         local xsml=trusted.sml
8616         log "save $xsml on $file"
8617         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8618
8619         local new=$(get_xattr_value $xbig $file)
8620         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8621
8622         log "grow $xsml on $file"
8623         setfattr -n $xsml -v "$value" $file ||
8624                 error "growing $xsml on $file failed"
8625
8626         new=$(get_xattr_value $xbig $file)
8627         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8628         log "$xbig still valid after growing $xsml"
8629
8630         rm -f $file
8631 }
8632
8633 test_102h() { # bug 15777
8634         grow_xattr 1024
8635 }
8636 run_test 102h "grow xattr from inside inode to external block"
8637
8638 test_102ha() {
8639         large_xattr_enabled || skip_env "ea_inode feature disabled"
8640
8641         echo "setting xattr of max xattr size: $(max_xattr_size)"
8642         grow_xattr $(max_xattr_size)
8643
8644         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
8645         echo "This should fail:"
8646         grow_xattr $(($(max_xattr_size) + 10)) 1
8647 }
8648 run_test 102ha "grow xattr from inside inode to external inode"
8649
8650 test_102i() { # bug 17038
8651         [ -z "$(which getfattr 2>/dev/null)" ] &&
8652                 skip "could not find getfattr"
8653
8654         touch $DIR/$tfile
8655         ln -s $DIR/$tfile $DIR/${tfile}link
8656         getfattr -n trusted.lov $DIR/$tfile ||
8657                 error "lgetxattr on $DIR/$tfile failed"
8658         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
8659                 grep -i "no such attr" ||
8660                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
8661         rm -f $DIR/$tfile $DIR/${tfile}link
8662 }
8663 run_test 102i "lgetxattr test on symbolic link ============"
8664
8665 test_102j() {
8666         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8667         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8668
8669         XINC=$(have_xattrs_include)
8670         setup_test102 "$RUNAS"
8671         chown $RUNAS_ID $DIR/$tdir
8672         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8673         cd $DIR/$tdir/$tdir
8674         compare_stripe_info1 "$RUNAS"
8675 }
8676 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
8677
8678 test_102k() {
8679         [ -z "$(which setfattr 2>/dev/null)" ] &&
8680                 skip "could not find setfattr"
8681
8682         touch $DIR/$tfile
8683         # b22187 just check that does not crash for regular file.
8684         setfattr -n trusted.lov $DIR/$tfile
8685         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
8686         local test_kdir=$DIR/$tdir
8687         test_mkdir $test_kdir
8688         local default_size=$($LFS getstripe -S $test_kdir)
8689         local default_count=$($LFS getstripe -c $test_kdir)
8690         local default_offset=$($LFS getstripe -i $test_kdir)
8691         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
8692                 error 'dir setstripe failed'
8693         setfattr -n trusted.lov $test_kdir
8694         local stripe_size=$($LFS getstripe -S $test_kdir)
8695         local stripe_count=$($LFS getstripe -c $test_kdir)
8696         local stripe_offset=$($LFS getstripe -i $test_kdir)
8697         [ $stripe_size -eq $default_size ] ||
8698                 error "stripe size $stripe_size != $default_size"
8699         [ $stripe_count -eq $default_count ] ||
8700                 error "stripe count $stripe_count != $default_count"
8701         [ $stripe_offset -eq $default_offset ] ||
8702                 error "stripe offset $stripe_offset != $default_offset"
8703         rm -rf $DIR/$tfile $test_kdir
8704 }
8705 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
8706
8707 test_102l() {
8708         [ -z "$(which getfattr 2>/dev/null)" ] &&
8709                 skip "could not find getfattr"
8710
8711         # LU-532 trusted. xattr is invisible to non-root
8712         local testfile=$DIR/$tfile
8713
8714         touch $testfile
8715
8716         echo "listxattr as user..."
8717         chown $RUNAS_ID $testfile
8718         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
8719             grep -q "trusted" &&
8720                 error "$testfile trusted xattrs are user visible"
8721
8722         return 0;
8723 }
8724 run_test 102l "listxattr size test =================================="
8725
8726 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
8727         local path=$DIR/$tfile
8728         touch $path
8729
8730         listxattr_size_check $path || error "listattr_size_check $path failed"
8731 }
8732 run_test 102m "Ensure listxattr fails on small bufffer ========"
8733
8734 cleanup_test102
8735
8736 getxattr() { # getxattr path name
8737         # Return the base64 encoding of the value of xattr name on path.
8738         local path=$1
8739         local name=$2
8740
8741         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
8742         # file: $path
8743         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8744         #
8745         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8746
8747         getfattr --absolute-names --encoding=base64 --name=$name $path |
8748                 awk -F= -v name=$name '$1 == name {
8749                         print substr($0, index($0, "=") + 1);
8750         }'
8751 }
8752
8753 test_102n() { # LU-4101 mdt: protect internal xattrs
8754         [ -z "$(which setfattr 2>/dev/null)" ] &&
8755                 skip "could not find setfattr"
8756         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
8757         then
8758                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
8759         fi
8760
8761         local file0=$DIR/$tfile.0
8762         local file1=$DIR/$tfile.1
8763         local xattr0=$TMP/$tfile.0
8764         local xattr1=$TMP/$tfile.1
8765         local namelist="lov lma lmv link fid version som hsm"
8766         local name
8767         local value
8768
8769         rm -rf $file0 $file1 $xattr0 $xattr1
8770         touch $file0 $file1
8771
8772         # Get 'before' xattrs of $file1.
8773         getfattr --absolute-names --dump --match=- $file1 > $xattr0
8774
8775         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
8776                 namelist+=" lfsck_namespace"
8777         for name in $namelist; do
8778                 # Try to copy xattr from $file0 to $file1.
8779                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8780
8781                 setfattr --name=trusted.$name --value="$value" $file1 ||
8782                         error "setxattr 'trusted.$name' failed"
8783
8784                 # Try to set a garbage xattr.
8785                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8786
8787                 if [[ x$name == "xlov" ]]; then
8788                         setfattr --name=trusted.lov --value="$value" $file1 &&
8789                         error "setxattr invalid 'trusted.lov' success"
8790                 else
8791                         setfattr --name=trusted.$name --value="$value" $file1 ||
8792                                 error "setxattr invalid 'trusted.$name' failed"
8793                 fi
8794
8795                 # Try to remove the xattr from $file1. We don't care if this
8796                 # appears to succeed or fail, we just don't want there to be
8797                 # any changes or crashes.
8798                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8799         done
8800
8801         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
8802         then
8803                 name="lfsck_ns"
8804                 # Try to copy xattr from $file0 to $file1.
8805                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8806
8807                 setfattr --name=trusted.$name --value="$value" $file1 ||
8808                         error "setxattr 'trusted.$name' failed"
8809
8810                 # Try to set a garbage xattr.
8811                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8812
8813                 setfattr --name=trusted.$name --value="$value" $file1 ||
8814                         error "setxattr 'trusted.$name' failed"
8815
8816                 # Try to remove the xattr from $file1. We don't care if this
8817                 # appears to succeed or fail, we just don't want there to be
8818                 # any changes or crashes.
8819                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8820         fi
8821
8822         # Get 'after' xattrs of file1.
8823         getfattr --absolute-names --dump --match=- $file1 > $xattr1
8824
8825         if ! diff $xattr0 $xattr1; then
8826                 error "before and after xattrs of '$file1' differ"
8827         fi
8828
8829         rm -rf $file0 $file1 $xattr0 $xattr1
8830
8831         return 0
8832 }
8833 run_test 102n "silently ignore setxattr on internal trusted xattrs"
8834
8835 test_102p() { # LU-4703 setxattr did not check ownership
8836         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
8837                 skip "MDS needs to be at least 2.5.56"
8838
8839         local testfile=$DIR/$tfile
8840
8841         touch $testfile
8842
8843         echo "setfacl as user..."
8844         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
8845         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
8846
8847         echo "setfattr as user..."
8848         setfacl -m "u:$RUNAS_ID:---" $testfile
8849         $RUNAS setfattr -x system.posix_acl_access $testfile
8850         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
8851 }
8852 run_test 102p "check setxattr(2) correctly fails without permission"
8853
8854 test_102q() {
8855         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
8856                 skip "MDS needs to be at least 2.6.92"
8857
8858         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
8859 }
8860 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
8861
8862 test_102r() {
8863         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
8864                 skip "MDS needs to be at least 2.6.93"
8865
8866         touch $DIR/$tfile || error "touch"
8867         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
8868         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
8869         rm $DIR/$tfile || error "rm"
8870
8871         #normal directory
8872         mkdir -p $DIR/$tdir || error "mkdir"
8873         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8874         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8875         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8876                 error "$testfile error deleting user.author1"
8877         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8878                 grep "user.$(basename $tdir)" &&
8879                 error "$tdir did not delete user.$(basename $tdir)"
8880         rmdir $DIR/$tdir || error "rmdir"
8881
8882         #striped directory
8883         test_mkdir $DIR/$tdir
8884         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8885         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8886         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8887                 error "$testfile error deleting user.author1"
8888         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8889                 grep "user.$(basename $tdir)" &&
8890                 error "$tdir did not delete user.$(basename $tdir)"
8891         rmdir $DIR/$tdir || error "rm striped dir"
8892 }
8893 run_test 102r "set EAs with empty values"
8894
8895 test_102s() {
8896         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
8897                 skip "MDS needs to be at least 2.11.52"
8898
8899         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
8900
8901         save_lustre_params client "llite.*.xattr_cache" > $save
8902
8903         for cache in 0 1; do
8904                 lctl set_param llite.*.xattr_cache=$cache
8905
8906                 rm -f $DIR/$tfile
8907                 touch $DIR/$tfile || error "touch"
8908                 for prefix in lustre security system trusted user; do
8909                         # Note getxattr() may fail with 'Operation not
8910                         # supported' or 'No such attribute' depending
8911                         # on prefix and cache.
8912                         getfattr -n $prefix.n102s $DIR/$tfile &&
8913                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
8914                 done
8915         done
8916
8917         restore_lustre_params < $save
8918 }
8919 run_test 102s "getting nonexistent xattrs should fail"
8920
8921 test_102t() {
8922         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
8923                 skip "MDS needs to be at least 2.11.52"
8924
8925         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
8926
8927         save_lustre_params client "llite.*.xattr_cache" > $save
8928
8929         for cache in 0 1; do
8930                 lctl set_param llite.*.xattr_cache=$cache
8931
8932                 for buf_size in 0 256; do
8933                         rm -f $DIR/$tfile
8934                         touch $DIR/$tfile || error "touch"
8935                         setfattr -n user.multiop $DIR/$tfile
8936                         $MULTIOP $DIR/$tfile oa$buf_size ||
8937                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
8938                 done
8939         done
8940
8941         restore_lustre_params < $save
8942 }
8943 run_test 102t "zero length xattr values handled correctly"
8944
8945 run_acl_subtest()
8946 {
8947     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
8948     return $?
8949 }
8950
8951 test_103a() {
8952         [ "$UID" != 0 ] && skip "must run as root"
8953         $GSS && skip_env "could not run under gss"
8954         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
8955                 skip_env "must have acl enabled"
8956         [ -z "$(which setfacl 2>/dev/null)" ] &&
8957                 skip_env "could not find setfacl"
8958         remote_mds_nodsh && skip "remote MDS with nodsh"
8959
8960         gpasswd -a daemon bin                           # LU-5641
8961         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
8962
8963         declare -a identity_old
8964
8965         for num in $(seq $MDSCOUNT); do
8966                 switch_identity $num true || identity_old[$num]=$?
8967         done
8968
8969         SAVE_UMASK=$(umask)
8970         umask 0022
8971         mkdir -p $DIR/$tdir
8972         cd $DIR/$tdir
8973
8974         echo "performing cp ..."
8975         run_acl_subtest cp || error "run_acl_subtest cp failed"
8976         echo "performing getfacl-noacl..."
8977         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
8978         echo "performing misc..."
8979         run_acl_subtest misc || error  "misc test failed"
8980         echo "performing permissions..."
8981         run_acl_subtest permissions || error "permissions failed"
8982         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
8983         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
8984                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
8985                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
8986         then
8987                 echo "performing permissions xattr..."
8988                 run_acl_subtest permissions_xattr ||
8989                         error "permissions_xattr failed"
8990         fi
8991         echo "performing setfacl..."
8992         run_acl_subtest setfacl || error  "setfacl test failed"
8993
8994         # inheritance test got from HP
8995         echo "performing inheritance..."
8996         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
8997         chmod +x make-tree || error "chmod +x failed"
8998         run_acl_subtest inheritance || error "inheritance test failed"
8999         rm -f make-tree
9000
9001         echo "LU-974 ignore umask when acl is enabled..."
9002         run_acl_subtest 974 || error "LU-974 umask test failed"
9003         if [ $MDSCOUNT -ge 2 ]; then
9004                 run_acl_subtest 974_remote ||
9005                         error "LU-974 umask test failed under remote dir"
9006         fi
9007
9008         echo "LU-2561 newly created file is same size as directory..."
9009         if [ "$mds1_FSTYPE" != "zfs" ]; then
9010                 run_acl_subtest 2561 || error "LU-2561 test failed"
9011         else
9012                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9013         fi
9014
9015         run_acl_subtest 4924 || error "LU-4924 test failed"
9016
9017         cd $SAVE_PWD
9018         umask $SAVE_UMASK
9019
9020         for num in $(seq $MDSCOUNT); do
9021                 if [ "${identity_old[$num]}" = 1 ]; then
9022                         switch_identity $num false || identity_old[$num]=$?
9023                 fi
9024         done
9025 }
9026 run_test 103a "acl test"
9027
9028 test_103b() {
9029         declare -a pids
9030         local U
9031
9032         for U in {0..511}; do
9033                 {
9034                 local O=$(printf "%04o" $U)
9035
9036                 umask $(printf "%04o" $((511 ^ $O)))
9037                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9038                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9039
9040                 (( $S == ($O & 0666) )) ||
9041                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9042
9043                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9044                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9045                 (( $S == ($O & 0666) )) ||
9046                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9047
9048                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9049                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9050                 (( $S == ($O & 0666) )) ||
9051                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9052                 rm -f $DIR/$tfile.[smp]$0
9053                 } &
9054                 local pid=$!
9055
9056                 # limit the concurrently running threads to 64. LU-11878
9057                 local idx=$((U % 64))
9058                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9059                 pids[idx]=$pid
9060         done
9061         wait
9062 }
9063 run_test 103b "umask lfs setstripe"
9064
9065 test_103c() {
9066         mkdir -p $DIR/$tdir
9067         cp -rp $DIR/$tdir $DIR/$tdir.bak
9068
9069         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9070                 error "$DIR/$tdir shouldn't contain default ACL"
9071         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9072                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9073         true
9074 }
9075 run_test 103c "'cp -rp' won't set empty acl"
9076
9077 test_104a() {
9078         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9079
9080         touch $DIR/$tfile
9081         lfs df || error "lfs df failed"
9082         lfs df -ih || error "lfs df -ih failed"
9083         lfs df -h $DIR || error "lfs df -h $DIR failed"
9084         lfs df -i $DIR || error "lfs df -i $DIR failed"
9085         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9086         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9087
9088         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9089         lctl --device %$OSC deactivate
9090         lfs df || error "lfs df with deactivated OSC failed"
9091         lctl --device %$OSC activate
9092         # wait the osc back to normal
9093         wait_osc_import_ready client ost
9094
9095         lfs df || error "lfs df with reactivated OSC failed"
9096         rm -f $DIR/$tfile
9097 }
9098 run_test 104a "lfs df [-ih] [path] test ========================="
9099
9100 test_104b() {
9101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9102         [ $RUNAS_ID -eq $UID ] &&
9103                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9104
9105         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9106                         grep "Permission denied" | wc -l)))
9107         if [ $denied_cnt -ne 0 ]; then
9108                 error "lfs check servers test failed"
9109         fi
9110 }
9111 run_test 104b "$RUNAS lfs check servers test ===================="
9112
9113 test_105a() {
9114         # doesn't work on 2.4 kernels
9115         touch $DIR/$tfile
9116         if $(flock_is_enabled); then
9117                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9118         else
9119                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9120         fi
9121         rm -f $DIR/$tfile
9122 }
9123 run_test 105a "flock when mounted without -o flock test ========"
9124
9125 test_105b() {
9126         touch $DIR/$tfile
9127         if $(flock_is_enabled); then
9128                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9129         else
9130                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9131         fi
9132         rm -f $DIR/$tfile
9133 }
9134 run_test 105b "fcntl when mounted without -o flock test ========"
9135
9136 test_105c() {
9137         touch $DIR/$tfile
9138         if $(flock_is_enabled); then
9139                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9140         else
9141                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9142         fi
9143         rm -f $DIR/$tfile
9144 }
9145 run_test 105c "lockf when mounted without -o flock test"
9146
9147 test_105d() { # bug 15924
9148         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9149
9150         test_mkdir $DIR/$tdir
9151         flock_is_enabled || skip_env "mount w/o flock enabled"
9152         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9153         $LCTL set_param fail_loc=0x80000315
9154         flocks_test 2 $DIR/$tdir
9155 }
9156 run_test 105d "flock race (should not freeze) ========"
9157
9158 test_105e() { # bug 22660 && 22040
9159         flock_is_enabled || skip_env "mount w/o flock enabled"
9160
9161         touch $DIR/$tfile
9162         flocks_test 3 $DIR/$tfile
9163 }
9164 run_test 105e "Two conflicting flocks from same process"
9165
9166 test_106() { #bug 10921
9167         test_mkdir $DIR/$tdir
9168         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9169         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9170 }
9171 run_test 106 "attempt exec of dir followed by chown of that dir"
9172
9173 test_107() {
9174         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9175
9176         CDIR=`pwd`
9177         local file=core
9178
9179         cd $DIR
9180         rm -f $file
9181
9182         local save_pattern=$(sysctl -n kernel.core_pattern)
9183         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9184         sysctl -w kernel.core_pattern=$file
9185         sysctl -w kernel.core_uses_pid=0
9186
9187         ulimit -c unlimited
9188         sleep 60 &
9189         SLEEPPID=$!
9190
9191         sleep 1
9192
9193         kill -s 11 $SLEEPPID
9194         wait $SLEEPPID
9195         if [ -e $file ]; then
9196                 size=`stat -c%s $file`
9197                 [ $size -eq 0 ] && error "Fail to create core file $file"
9198         else
9199                 error "Fail to create core file $file"
9200         fi
9201         rm -f $file
9202         sysctl -w kernel.core_pattern=$save_pattern
9203         sysctl -w kernel.core_uses_pid=$save_uses_pid
9204         cd $CDIR
9205 }
9206 run_test 107 "Coredump on SIG"
9207
9208 test_110() {
9209         test_mkdir $DIR/$tdir
9210         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9211         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9212                 error "mkdir with 256 char should fail, but did not"
9213         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9214                 error "create with 255 char failed"
9215         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9216                 error "create with 256 char should fail, but did not"
9217
9218         ls -l $DIR/$tdir
9219         rm -rf $DIR/$tdir
9220 }
9221 run_test 110 "filename length checking"
9222
9223 #
9224 # Purpose: To verify dynamic thread (OSS) creation.
9225 #
9226 test_115() {
9227         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9228         remote_ost_nodsh && skip "remote OST with nodsh"
9229
9230         # Lustre does not stop service threads once they are started.
9231         # Reset number of running threads to default.
9232         stopall
9233         setupall
9234
9235         local OSTIO_pre
9236         local save_params="$TMP/sanity-$TESTNAME.parameters"
9237
9238         # Get ll_ost_io count before I/O
9239         OSTIO_pre=$(do_facet ost1 \
9240                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
9241         # Exit if lustre is not running (ll_ost_io not running).
9242         [ -z "$OSTIO_pre" ] && error "no OSS threads"
9243
9244         echo "Starting with $OSTIO_pre threads"
9245         local thread_max=$((OSTIO_pre * 2))
9246         local rpc_in_flight=$((thread_max * 2))
9247         # Number of I/O Process proposed to be started.
9248         local nfiles
9249         local facets=$(get_facets OST)
9250
9251         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
9252         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
9253
9254         # Set in_flight to $rpc_in_flight
9255         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
9256                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
9257         nfiles=${rpc_in_flight}
9258         # Set ost thread_max to $thread_max
9259         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
9260
9261         # 5 Minutes should be sufficient for max number of OSS
9262         # threads(thread_max) to be created.
9263         local timeout=300
9264
9265         # Start I/O.
9266         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
9267         test_mkdir $DIR/$tdir
9268         for i in $(seq $nfiles); do
9269                 local file=$DIR/$tdir/${tfile}-$i
9270                 $LFS setstripe -c -1 -i 0 $file
9271                 ($WTL $file $timeout)&
9272         done
9273
9274         # I/O Started - Wait for thread_started to reach thread_max or report
9275         # error if thread_started is more than thread_max.
9276         echo "Waiting for thread_started to reach thread_max"
9277         local thread_started=0
9278         local end_time=$((SECONDS + timeout))
9279
9280         while [ $SECONDS -le $end_time ] ; do
9281                 echo -n "."
9282                 # Get ost i/o thread_started count.
9283                 thread_started=$(do_facet ost1 \
9284                         "$LCTL get_param \
9285                         ost.OSS.ost_io.threads_started | cut -d= -f2")
9286                 # Break out if thread_started is equal/greater than thread_max
9287                 if [[ $thread_started -ge $thread_max ]]; then
9288                         echo ll_ost_io thread_started $thread_started, \
9289                                 equal/greater than thread_max $thread_max
9290                         break
9291                 fi
9292                 sleep 1
9293         done
9294
9295         # Cleanup - We have the numbers, Kill i/o jobs if running.
9296         jobcount=($(jobs -p))
9297         for i in $(seq 0 $((${#jobcount[@]}-1)))
9298         do
9299                 kill -9 ${jobcount[$i]}
9300                 if [ $? -ne 0 ] ; then
9301                         echo Warning: \
9302                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
9303                 fi
9304         done
9305
9306         # Cleanup files left by WTL binary.
9307         for i in $(seq $nfiles); do
9308                 local file=$DIR/$tdir/${tfile}-$i
9309                 rm -rf $file
9310                 if [ $? -ne 0 ] ; then
9311                         echo "Warning: Failed to delete file $file"
9312                 fi
9313         done
9314
9315         restore_lustre_params <$save_params
9316         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
9317
9318         # Error out if no new thread has started or Thread started is greater
9319         # than thread max.
9320         if [[ $thread_started -le $OSTIO_pre ||
9321                         $thread_started -gt $thread_max ]]; then
9322                 error "ll_ost_io: thread_started $thread_started" \
9323                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
9324                       "No new thread started or thread started greater " \
9325                       "than thread_max."
9326         fi
9327 }
9328 run_test 115 "verify dynamic thread creation===================="
9329
9330 free_min_max () {
9331         wait_delete_completed
9332         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
9333         echo "OST kbytes available: ${AVAIL[@]}"
9334         MAXV=${AVAIL[0]}
9335         MAXI=0
9336         MINV=${AVAIL[0]}
9337         MINI=0
9338         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
9339                 #echo OST $i: ${AVAIL[i]}kb
9340                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
9341                         MAXV=${AVAIL[i]}
9342                         MAXI=$i
9343                 fi
9344                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
9345                         MINV=${AVAIL[i]}
9346                         MINI=$i
9347                 fi
9348         done
9349         echo "Min free space: OST $MINI: $MINV"
9350         echo "Max free space: OST $MAXI: $MAXV"
9351 }
9352
9353 test_116a() { # was previously test_116()
9354         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9355         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9356         remote_mds_nodsh && skip "remote MDS with nodsh"
9357
9358         echo -n "Free space priority "
9359         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
9360                 head -n1
9361         declare -a AVAIL
9362         free_min_max
9363
9364         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
9365         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
9366         trap simple_cleanup_common EXIT
9367
9368         # Check if we need to generate uneven OSTs
9369         test_mkdir -p $DIR/$tdir/OST${MINI}
9370         local FILL=$((MINV / 4))
9371         local DIFF=$((MAXV - MINV))
9372         local DIFF2=$((DIFF * 100 / MINV))
9373
9374         local threshold=$(do_facet $SINGLEMDS \
9375                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
9376         threshold=${threshold%%%}
9377         echo -n "Check for uneven OSTs: "
9378         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
9379
9380         if [[ $DIFF2 -gt $threshold ]]; then
9381                 echo "ok"
9382                 echo "Don't need to fill OST$MINI"
9383         else
9384                 # generate uneven OSTs. Write 2% over the QOS threshold value
9385                 echo "no"
9386                 DIFF=$((threshold - DIFF2 + 2))
9387                 DIFF2=$((MINV * DIFF / 100))
9388                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
9389                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
9390                         error "setstripe failed"
9391                 DIFF=$((DIFF2 / 2048))
9392                 i=0
9393                 while [ $i -lt $DIFF ]; do
9394                         i=$((i + 1))
9395                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
9396                                 bs=2M count=1 2>/dev/null
9397                         echo -n .
9398                 done
9399                 echo .
9400                 sync
9401                 sleep_maxage
9402                 free_min_max
9403         fi
9404
9405         DIFF=$((MAXV - MINV))
9406         DIFF2=$((DIFF * 100 / MINV))
9407         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
9408         if [ $DIFF2 -gt $threshold ]; then
9409                 echo "ok"
9410         else
9411                 echo "failed - QOS mode won't be used"
9412                 simple_cleanup_common
9413                 skip "QOS imbalance criteria not met"
9414         fi
9415
9416         MINI1=$MINI
9417         MINV1=$MINV
9418         MAXI1=$MAXI
9419         MAXV1=$MAXV
9420
9421         # now fill using QOS
9422         $LFS setstripe -c 1 $DIR/$tdir
9423         FILL=$((FILL / 200))
9424         if [ $FILL -gt 600 ]; then
9425                 FILL=600
9426         fi
9427         echo "writing $FILL files to QOS-assigned OSTs"
9428         i=0
9429         while [ $i -lt $FILL ]; do
9430                 i=$((i + 1))
9431                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
9432                         count=1 2>/dev/null
9433                 echo -n .
9434         done
9435         echo "wrote $i 200k files"
9436         sync
9437         sleep_maxage
9438
9439         echo "Note: free space may not be updated, so measurements might be off"
9440         free_min_max
9441         DIFF2=$((MAXV - MINV))
9442         echo "free space delta: orig $DIFF final $DIFF2"
9443         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
9444         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
9445         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
9446         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
9447         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
9448         if [[ $DIFF -gt 0 ]]; then
9449                 FILL=$((DIFF2 * 100 / DIFF - 100))
9450                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
9451         fi
9452
9453         # Figure out which files were written where
9454         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9455                awk '/'$MINI1': / {print $2; exit}')
9456         echo $UUID
9457         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9458         echo "$MINC files created on smaller OST $MINI1"
9459         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9460                awk '/'$MAXI1': / {print $2; exit}')
9461         echo $UUID
9462         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9463         echo "$MAXC files created on larger OST $MAXI1"
9464         if [[ $MINC -gt 0 ]]; then
9465                 FILL=$((MAXC * 100 / MINC - 100))
9466                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
9467         fi
9468         [[ $MAXC -gt $MINC ]] ||
9469                 error_ignore LU-9 "stripe QOS didn't balance free space"
9470         simple_cleanup_common
9471 }
9472 run_test 116a "stripe QOS: free space balance ==================="
9473
9474 test_116b() { # LU-2093
9475         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9476         remote_mds_nodsh && skip "remote MDS with nodsh"
9477
9478 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
9479         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
9480                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
9481         [ -z "$old_rr" ] && skip "no QOS"
9482         do_facet $SINGLEMDS lctl set_param \
9483                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
9484         mkdir -p $DIR/$tdir
9485         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
9486         createmany -o $DIR/$tdir/f- 20 || error "can't create"
9487         do_facet $SINGLEMDS lctl set_param fail_loc=0
9488         rm -rf $DIR/$tdir
9489         do_facet $SINGLEMDS lctl set_param \
9490                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
9491 }
9492 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
9493
9494 test_117() # bug 10891
9495 {
9496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9497
9498         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
9499         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
9500         lctl set_param fail_loc=0x21e
9501         > $DIR/$tfile || error "truncate failed"
9502         lctl set_param fail_loc=0
9503         echo "Truncate succeeded."
9504         rm -f $DIR/$tfile
9505 }
9506 run_test 117 "verify osd extend =========="
9507
9508 NO_SLOW_RESENDCOUNT=4
9509 export OLD_RESENDCOUNT=""
9510 set_resend_count () {
9511         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
9512         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
9513         lctl set_param -n $PROC_RESENDCOUNT $1
9514         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
9515 }
9516
9517 # for reduce test_118* time (b=14842)
9518 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9519
9520 # Reset async IO behavior after error case
9521 reset_async() {
9522         FILE=$DIR/reset_async
9523
9524         # Ensure all OSCs are cleared
9525         $LFS setstripe -c -1 $FILE
9526         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
9527         sync
9528         rm $FILE
9529 }
9530
9531 test_118a() #bug 11710
9532 {
9533         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9534
9535         reset_async
9536
9537         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9538         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9539         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9540
9541         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9542                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9543                 return 1;
9544         fi
9545         rm -f $DIR/$tfile
9546 }
9547 run_test 118a "verify O_SYNC works =========="
9548
9549 test_118b()
9550 {
9551         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9552         remote_ost_nodsh && skip "remote OST with nodsh"
9553
9554         reset_async
9555
9556         #define OBD_FAIL_SRV_ENOENT 0x217
9557         set_nodes_failloc "$(osts_nodes)" 0x217
9558         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9559         RC=$?
9560         set_nodes_failloc "$(osts_nodes)" 0
9561         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9562         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9563                     grep -c writeback)
9564
9565         if [[ $RC -eq 0 ]]; then
9566                 error "Must return error due to dropped pages, rc=$RC"
9567                 return 1;
9568         fi
9569
9570         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9571                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9572                 return 1;
9573         fi
9574
9575         echo "Dirty pages not leaked on ENOENT"
9576
9577         # Due to the above error the OSC will issue all RPCs syncronously
9578         # until a subsequent RPC completes successfully without error.
9579         $MULTIOP $DIR/$tfile Ow4096yc
9580         rm -f $DIR/$tfile
9581
9582         return 0
9583 }
9584 run_test 118b "Reclaim dirty pages on fatal error =========="
9585
9586 test_118c()
9587 {
9588         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9589
9590         # for 118c, restore the original resend count, LU-1940
9591         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
9592                                 set_resend_count $OLD_RESENDCOUNT
9593         remote_ost_nodsh && skip "remote OST with nodsh"
9594
9595         reset_async
9596
9597         #define OBD_FAIL_OST_EROFS               0x216
9598         set_nodes_failloc "$(osts_nodes)" 0x216
9599
9600         # multiop should block due to fsync until pages are written
9601         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9602         MULTIPID=$!
9603         sleep 1
9604
9605         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9606                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9607         fi
9608
9609         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9610                     grep -c writeback)
9611         if [[ $WRITEBACK -eq 0 ]]; then
9612                 error "No page in writeback, writeback=$WRITEBACK"
9613         fi
9614
9615         set_nodes_failloc "$(osts_nodes)" 0
9616         wait $MULTIPID
9617         RC=$?
9618         if [[ $RC -ne 0 ]]; then
9619                 error "Multiop fsync failed, rc=$RC"
9620         fi
9621
9622         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9623         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9624                     grep -c writeback)
9625         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9626                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9627         fi
9628
9629         rm -f $DIR/$tfile
9630         echo "Dirty pages flushed via fsync on EROFS"
9631         return 0
9632 }
9633 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
9634
9635 # continue to use small resend count to reduce test_118* time (b=14842)
9636 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9637
9638 test_118d()
9639 {
9640         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9641         remote_ost_nodsh && skip "remote OST with nodsh"
9642
9643         reset_async
9644
9645         #define OBD_FAIL_OST_BRW_PAUSE_BULK
9646         set_nodes_failloc "$(osts_nodes)" 0x214
9647         # multiop should block due to fsync until pages are written
9648         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9649         MULTIPID=$!
9650         sleep 1
9651
9652         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9653                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9654         fi
9655
9656         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9657                     grep -c writeback)
9658         if [[ $WRITEBACK -eq 0 ]]; then
9659                 error "No page in writeback, writeback=$WRITEBACK"
9660         fi
9661
9662         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
9663         set_nodes_failloc "$(osts_nodes)" 0
9664
9665         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9666         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9667                     grep -c writeback)
9668         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9669                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9670         fi
9671
9672         rm -f $DIR/$tfile
9673         echo "Dirty pages gaurenteed flushed via fsync"
9674         return 0
9675 }
9676 run_test 118d "Fsync validation inject a delay of the bulk =========="
9677
9678 test_118f() {
9679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9680
9681         reset_async
9682
9683         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
9684         lctl set_param fail_loc=0x8000040a
9685
9686         # Should simulate EINVAL error which is fatal
9687         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9688         RC=$?
9689         if [[ $RC -eq 0 ]]; then
9690                 error "Must return error due to dropped pages, rc=$RC"
9691         fi
9692
9693         lctl set_param fail_loc=0x0
9694
9695         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9696         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9697         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9698                     grep -c writeback)
9699         if [[ $LOCKED -ne 0 ]]; then
9700                 error "Locked pages remain in cache, locked=$LOCKED"
9701         fi
9702
9703         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9704                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9705         fi
9706
9707         rm -f $DIR/$tfile
9708         echo "No pages locked after fsync"
9709
9710         reset_async
9711         return 0
9712 }
9713 run_test 118f "Simulate unrecoverable OSC side error =========="
9714
9715 test_118g() {
9716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9717
9718         reset_async
9719
9720         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
9721         lctl set_param fail_loc=0x406
9722
9723         # simulate local -ENOMEM
9724         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9725         RC=$?
9726
9727         lctl set_param fail_loc=0
9728         if [[ $RC -eq 0 ]]; then
9729                 error "Must return error due to dropped pages, rc=$RC"
9730         fi
9731
9732         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9733         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9734         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9735                         grep -c writeback)
9736         if [[ $LOCKED -ne 0 ]]; then
9737                 error "Locked pages remain in cache, locked=$LOCKED"
9738         fi
9739
9740         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9741                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9742         fi
9743
9744         rm -f $DIR/$tfile
9745         echo "No pages locked after fsync"
9746
9747         reset_async
9748         return 0
9749 }
9750 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
9751
9752 test_118h() {
9753         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9754         remote_ost_nodsh && skip "remote OST with nodsh"
9755
9756         reset_async
9757
9758         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9759         set_nodes_failloc "$(osts_nodes)" 0x20e
9760         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9761         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9762         RC=$?
9763
9764         set_nodes_failloc "$(osts_nodes)" 0
9765         if [[ $RC -eq 0 ]]; then
9766                 error "Must return error due to dropped pages, rc=$RC"
9767         fi
9768
9769         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9770         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9771         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9772                     grep -c writeback)
9773         if [[ $LOCKED -ne 0 ]]; then
9774                 error "Locked pages remain in cache, locked=$LOCKED"
9775         fi
9776
9777         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9778                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9779         fi
9780
9781         rm -f $DIR/$tfile
9782         echo "No pages locked after fsync"
9783
9784         return 0
9785 }
9786 run_test 118h "Verify timeout in handling recoverables errors  =========="
9787
9788 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9789
9790 test_118i() {
9791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9792         remote_ost_nodsh && skip "remote OST with nodsh"
9793
9794         reset_async
9795
9796         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9797         set_nodes_failloc "$(osts_nodes)" 0x20e
9798
9799         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9800         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9801         PID=$!
9802         sleep 5
9803         set_nodes_failloc "$(osts_nodes)" 0
9804
9805         wait $PID
9806         RC=$?
9807         if [[ $RC -ne 0 ]]; then
9808                 error "got error, but should be not, rc=$RC"
9809         fi
9810
9811         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9812         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9813         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9814         if [[ $LOCKED -ne 0 ]]; then
9815                 error "Locked pages remain in cache, locked=$LOCKED"
9816         fi
9817
9818         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9819                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9820         fi
9821
9822         rm -f $DIR/$tfile
9823         echo "No pages locked after fsync"
9824
9825         return 0
9826 }
9827 run_test 118i "Fix error before timeout in recoverable error  =========="
9828
9829 [ "$SLOW" = "no" ] && set_resend_count 4
9830
9831 test_118j() {
9832         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9833         remote_ost_nodsh && skip "remote OST with nodsh"
9834
9835         reset_async
9836
9837         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
9838         set_nodes_failloc "$(osts_nodes)" 0x220
9839
9840         # return -EIO from OST
9841         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9842         RC=$?
9843         set_nodes_failloc "$(osts_nodes)" 0x0
9844         if [[ $RC -eq 0 ]]; then
9845                 error "Must return error due to dropped pages, rc=$RC"
9846         fi
9847
9848         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9849         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9850         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9851         if [[ $LOCKED -ne 0 ]]; then
9852                 error "Locked pages remain in cache, locked=$LOCKED"
9853         fi
9854
9855         # in recoverable error on OST we want resend and stay until it finished
9856         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9857                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9858         fi
9859
9860         rm -f $DIR/$tfile
9861         echo "No pages locked after fsync"
9862
9863         return 0
9864 }
9865 run_test 118j "Simulate unrecoverable OST side error =========="
9866
9867 test_118k()
9868 {
9869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9870         remote_ost_nodsh && skip "remote OSTs with nodsh"
9871
9872         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9873         set_nodes_failloc "$(osts_nodes)" 0x20e
9874         test_mkdir $DIR/$tdir
9875
9876         for ((i=0;i<10;i++)); do
9877                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
9878                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
9879                 SLEEPPID=$!
9880                 sleep 0.500s
9881                 kill $SLEEPPID
9882                 wait $SLEEPPID
9883         done
9884
9885         set_nodes_failloc "$(osts_nodes)" 0
9886         rm -rf $DIR/$tdir
9887 }
9888 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
9889
9890 test_118l() # LU-646
9891 {
9892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9893
9894         test_mkdir $DIR/$tdir
9895         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
9896         rm -rf $DIR/$tdir
9897 }
9898 run_test 118l "fsync dir"
9899
9900 test_118m() # LU-3066
9901 {
9902         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9903
9904         test_mkdir $DIR/$tdir
9905         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
9906         rm -rf $DIR/$tdir
9907 }
9908 run_test 118m "fdatasync dir ========="
9909
9910 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9911
9912 test_118n()
9913 {
9914         local begin
9915         local end
9916
9917         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9918         remote_ost_nodsh && skip "remote OSTs with nodsh"
9919
9920         # Sleep to avoid a cached response.
9921         #define OBD_STATFS_CACHE_SECONDS 1
9922         sleep 2
9923
9924         # Inject a 10 second delay in the OST_STATFS handler.
9925         #define OBD_FAIL_OST_STATFS_DELAY 0x242
9926         set_nodes_failloc "$(osts_nodes)" 0x242
9927
9928         begin=$SECONDS
9929         stat --file-system $MOUNT > /dev/null
9930         end=$SECONDS
9931
9932         set_nodes_failloc "$(osts_nodes)" 0
9933
9934         if ((end - begin > 20)); then
9935             error "statfs took $((end - begin)) seconds, expected 10"
9936         fi
9937 }
9938 run_test 118n "statfs() sends OST_STATFS requests in parallel"
9939
9940 test_119a() # bug 11737
9941 {
9942         BSIZE=$((512 * 1024))
9943         directio write $DIR/$tfile 0 1 $BSIZE
9944         # We ask to read two blocks, which is more than a file size.
9945         # directio will indicate an error when requested and actual
9946         # sizes aren't equeal (a normal situation in this case) and
9947         # print actual read amount.
9948         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
9949         if [ "$NOB" != "$BSIZE" ]; then
9950                 error "read $NOB bytes instead of $BSIZE"
9951         fi
9952         rm -f $DIR/$tfile
9953 }
9954 run_test 119a "Short directIO read must return actual read amount"
9955
9956 test_119b() # bug 11737
9957 {
9958         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9959
9960         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
9961         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
9962         sync
9963         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
9964                 error "direct read failed"
9965         rm -f $DIR/$tfile
9966 }
9967 run_test 119b "Sparse directIO read must return actual read amount"
9968
9969 test_119c() # bug 13099
9970 {
9971         BSIZE=1048576
9972         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
9973         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
9974         rm -f $DIR/$tfile
9975 }
9976 run_test 119c "Testing for direct read hitting hole"
9977
9978 test_119d() # bug 15950
9979 {
9980         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9981
9982         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
9983         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
9984         BSIZE=1048576
9985         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
9986         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
9987         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
9988         lctl set_param fail_loc=0x40d
9989         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
9990         pid_dio=$!
9991         sleep 1
9992         cat $DIR/$tfile > /dev/null &
9993         lctl set_param fail_loc=0
9994         pid_reads=$!
9995         wait $pid_dio
9996         log "the DIO writes have completed, now wait for the reads (should not block very long)"
9997         sleep 2
9998         [ -n "`ps h -p $pid_reads -o comm`" ] && \
9999         error "the read rpcs have not completed in 2s"
10000         rm -f $DIR/$tfile
10001         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10002 }
10003 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10004
10005 test_120a() {
10006         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10007         remote_mds_nodsh && skip "remote MDS with nodsh"
10008         test_mkdir -i0 -c1 $DIR/$tdir
10009         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10010                 skip_env "no early lock cancel on server"
10011
10012         lru_resize_disable mdc
10013         lru_resize_disable osc
10014         cancel_lru_locks mdc
10015         # asynchronous object destroy at MDT could cause bl ast to client
10016         cancel_lru_locks osc
10017
10018         stat $DIR/$tdir > /dev/null
10019         can1=$(do_facet mds1 \
10020                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10021                awk '/ldlm_cancel/ {print $2}')
10022         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10023                awk '/ldlm_bl_callback/ {print $2}')
10024         test_mkdir -i0 -c1 $DIR/$tdir/d1
10025         can2=$(do_facet mds1 \
10026                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10027                awk '/ldlm_cancel/ {print $2}')
10028         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10029                awk '/ldlm_bl_callback/ {print $2}')
10030         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10031         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10032         lru_resize_enable mdc
10033         lru_resize_enable osc
10034 }
10035 run_test 120a "Early Lock Cancel: mkdir test"
10036
10037 test_120b() {
10038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10039         remote_mds_nodsh && skip "remote MDS with nodsh"
10040         test_mkdir $DIR/$tdir
10041         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10042                 skip_env "no early lock cancel on server"
10043
10044         lru_resize_disable mdc
10045         lru_resize_disable osc
10046         cancel_lru_locks mdc
10047         stat $DIR/$tdir > /dev/null
10048         can1=$(do_facet $SINGLEMDS \
10049                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10050                awk '/ldlm_cancel/ {print $2}')
10051         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10052                awk '/ldlm_bl_callback/ {print $2}')
10053         touch $DIR/$tdir/f1
10054         can2=$(do_facet $SINGLEMDS \
10055                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10056                awk '/ldlm_cancel/ {print $2}')
10057         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10058                awk '/ldlm_bl_callback/ {print $2}')
10059         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10060         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10061         lru_resize_enable mdc
10062         lru_resize_enable osc
10063 }
10064 run_test 120b "Early Lock Cancel: create test"
10065
10066 test_120c() {
10067         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10068         remote_mds_nodsh && skip "remote MDS with nodsh"
10069         test_mkdir -i0 -c1 $DIR/$tdir
10070         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10071                 skip "no early lock cancel on server"
10072
10073         lru_resize_disable mdc
10074         lru_resize_disable osc
10075         test_mkdir -i0 -c1 $DIR/$tdir/d1
10076         test_mkdir -i0 -c1 $DIR/$tdir/d2
10077         touch $DIR/$tdir/d1/f1
10078         cancel_lru_locks mdc
10079         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10080         can1=$(do_facet mds1 \
10081                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10082                awk '/ldlm_cancel/ {print $2}')
10083         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10084                awk '/ldlm_bl_callback/ {print $2}')
10085         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10086         can2=$(do_facet mds1 \
10087                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10088                awk '/ldlm_cancel/ {print $2}')
10089         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10090                awk '/ldlm_bl_callback/ {print $2}')
10091         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10092         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10093         lru_resize_enable mdc
10094         lru_resize_enable osc
10095 }
10096 run_test 120c "Early Lock Cancel: link test"
10097
10098 test_120d() {
10099         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10100         remote_mds_nodsh && skip "remote MDS with nodsh"
10101         test_mkdir -i0 -c1 $DIR/$tdir
10102         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10103                 skip_env "no early lock cancel on server"
10104
10105         lru_resize_disable mdc
10106         lru_resize_disable osc
10107         touch $DIR/$tdir
10108         cancel_lru_locks mdc
10109         stat $DIR/$tdir > /dev/null
10110         can1=$(do_facet mds1 \
10111                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10112                awk '/ldlm_cancel/ {print $2}')
10113         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10114                awk '/ldlm_bl_callback/ {print $2}')
10115         chmod a+x $DIR/$tdir
10116         can2=$(do_facet mds1 \
10117                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10118                awk '/ldlm_cancel/ {print $2}')
10119         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10120                awk '/ldlm_bl_callback/ {print $2}')
10121         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10122         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10123         lru_resize_enable mdc
10124         lru_resize_enable osc
10125 }
10126 run_test 120d "Early Lock Cancel: setattr test"
10127
10128 test_120e() {
10129         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10130         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10131                 skip_env "no early lock cancel on server"
10132         remote_mds_nodsh && skip "remote MDS with nodsh"
10133
10134         local dlmtrace_set=false
10135
10136         test_mkdir -i0 -c1 $DIR/$tdir
10137         lru_resize_disable mdc
10138         lru_resize_disable osc
10139         ! $LCTL get_param debug | grep -q dlmtrace &&
10140                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10141         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10142         cancel_lru_locks mdc
10143         cancel_lru_locks osc
10144         dd if=$DIR/$tdir/f1 of=/dev/null
10145         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10146         # XXX client can not do early lock cancel of OST lock
10147         # during unlink (LU-4206), so cancel osc lock now.
10148         sleep 2
10149         cancel_lru_locks osc
10150         can1=$(do_facet mds1 \
10151                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10152                awk '/ldlm_cancel/ {print $2}')
10153         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10154                awk '/ldlm_bl_callback/ {print $2}')
10155         unlink $DIR/$tdir/f1
10156         sleep 5
10157         can2=$(do_facet mds1 \
10158                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10159                awk '/ldlm_cancel/ {print $2}')
10160         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10161                awk '/ldlm_bl_callback/ {print $2}')
10162         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10163                 $LCTL dk $TMP/cancel.debug.txt
10164         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10165                 $LCTL dk $TMP/blocking.debug.txt
10166         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10167         lru_resize_enable mdc
10168         lru_resize_enable osc
10169 }
10170 run_test 120e "Early Lock Cancel: unlink test"
10171
10172 test_120f() {
10173         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10174         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10175                 skip_env "no early lock cancel on server"
10176         remote_mds_nodsh && skip "remote MDS with nodsh"
10177
10178         test_mkdir -i0 -c1 $DIR/$tdir
10179         lru_resize_disable mdc
10180         lru_resize_disable osc
10181         test_mkdir -i0 -c1 $DIR/$tdir/d1
10182         test_mkdir -i0 -c1 $DIR/$tdir/d2
10183         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10184         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10185         cancel_lru_locks mdc
10186         cancel_lru_locks osc
10187         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10188         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10189         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10190         # XXX client can not do early lock cancel of OST lock
10191         # during rename (LU-4206), so cancel osc lock now.
10192         sleep 2
10193         cancel_lru_locks osc
10194         can1=$(do_facet mds1 \
10195                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10196                awk '/ldlm_cancel/ {print $2}')
10197         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10198                awk '/ldlm_bl_callback/ {print $2}')
10199         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10200         sleep 5
10201         can2=$(do_facet mds1 \
10202                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10203                awk '/ldlm_cancel/ {print $2}')
10204         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10205                awk '/ldlm_bl_callback/ {print $2}')
10206         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10207         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10208         lru_resize_enable mdc
10209         lru_resize_enable osc
10210 }
10211 run_test 120f "Early Lock Cancel: rename test"
10212
10213 test_120g() {
10214         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10215         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10216                 skip_env "no early lock cancel on server"
10217         remote_mds_nodsh && skip "remote MDS with nodsh"
10218
10219         lru_resize_disable mdc
10220         lru_resize_disable osc
10221         count=10000
10222         echo create $count files
10223         test_mkdir $DIR/$tdir
10224         cancel_lru_locks mdc
10225         cancel_lru_locks osc
10226         t0=$(date +%s)
10227
10228         can0=$(do_facet $SINGLEMDS \
10229                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10230                awk '/ldlm_cancel/ {print $2}')
10231         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10232                awk '/ldlm_bl_callback/ {print $2}')
10233         createmany -o $DIR/$tdir/f $count
10234         sync
10235         can1=$(do_facet $SINGLEMDS \
10236                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10237                awk '/ldlm_cancel/ {print $2}')
10238         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10239                awk '/ldlm_bl_callback/ {print $2}')
10240         t1=$(date +%s)
10241         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
10242         echo rm $count files
10243         rm -r $DIR/$tdir
10244         sync
10245         can2=$(do_facet $SINGLEMDS \
10246                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10247                awk '/ldlm_cancel/ {print $2}')
10248         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10249                awk '/ldlm_bl_callback/ {print $2}')
10250         t2=$(date +%s)
10251         echo total: $count removes in $((t2-t1))
10252         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
10253         sleep 2
10254         # wait for commitment of removal
10255         lru_resize_enable mdc
10256         lru_resize_enable osc
10257 }
10258 run_test 120g "Early Lock Cancel: performance test"
10259
10260 test_121() { #bug #10589
10261         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10262
10263         rm -rf $DIR/$tfile
10264         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
10265 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
10266         lctl set_param fail_loc=0x310
10267         cancel_lru_locks osc > /dev/null
10268         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
10269         lctl set_param fail_loc=0
10270         [[ $reads -eq $writes ]] ||
10271                 error "read $reads blocks, must be $writes blocks"
10272 }
10273 run_test 121 "read cancel race ========="
10274
10275 test_123a() { # was test 123, statahead(bug 11401)
10276         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10277
10278         SLOWOK=0
10279         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
10280                 log "testing UP system. Performance may be lower than expected."
10281                 SLOWOK=1
10282         fi
10283
10284         rm -rf $DIR/$tdir
10285         test_mkdir $DIR/$tdir
10286         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
10287         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
10288         MULT=10
10289         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
10290                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
10291
10292                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10293                 lctl set_param -n llite.*.statahead_max 0
10294                 lctl get_param llite.*.statahead_max
10295                 cancel_lru_locks mdc
10296                 cancel_lru_locks osc
10297                 stime=`date +%s`
10298                 time ls -l $DIR/$tdir | wc -l
10299                 etime=`date +%s`
10300                 delta=$((etime - stime))
10301                 log "ls $i files without statahead: $delta sec"
10302                 lctl set_param llite.*.statahead_max=$max
10303
10304                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10305                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
10306                 cancel_lru_locks mdc
10307                 cancel_lru_locks osc
10308                 stime=`date +%s`
10309                 time ls -l $DIR/$tdir | wc -l
10310                 etime=`date +%s`
10311                 delta_sa=$((etime - stime))
10312                 log "ls $i files with statahead: $delta_sa sec"
10313                 lctl get_param -n llite.*.statahead_stats
10314                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10315
10316                 [[ $swrong -lt $ewrong ]] &&
10317                         log "statahead was stopped, maybe too many locks held!"
10318                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
10319
10320                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10321                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10322                     lctl set_param -n llite.*.statahead_max 0
10323                     lctl get_param llite.*.statahead_max
10324                     cancel_lru_locks mdc
10325                     cancel_lru_locks osc
10326                     stime=`date +%s`
10327                     time ls -l $DIR/$tdir | wc -l
10328                     etime=`date +%s`
10329                     delta=$((etime - stime))
10330                     log "ls $i files again without statahead: $delta sec"
10331                     lctl set_param llite.*.statahead_max=$max
10332                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10333                         if [  $SLOWOK -eq 0 ]; then
10334                                 error "ls $i files is slower with statahead!"
10335                         else
10336                                 log "ls $i files is slower with statahead!"
10337                         fi
10338                         break
10339                     fi
10340                 fi
10341
10342                 [ $delta -gt 20 ] && break
10343                 [ $delta -gt 8 ] && MULT=$((50 / delta))
10344                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
10345         done
10346         log "ls done"
10347
10348         stime=`date +%s`
10349         rm -r $DIR/$tdir
10350         sync
10351         etime=`date +%s`
10352         delta=$((etime - stime))
10353         log "rm -r $DIR/$tdir/: $delta seconds"
10354         log "rm done"
10355         lctl get_param -n llite.*.statahead_stats
10356 }
10357 run_test 123a "verify statahead work"
10358
10359 test_123b () { # statahead(bug 15027)
10360         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10361
10362         test_mkdir $DIR/$tdir
10363         createmany -o $DIR/$tdir/$tfile-%d 1000
10364
10365         cancel_lru_locks mdc
10366         cancel_lru_locks osc
10367
10368 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
10369         lctl set_param fail_loc=0x80000803
10370         ls -lR $DIR/$tdir > /dev/null
10371         log "ls done"
10372         lctl set_param fail_loc=0x0
10373         lctl get_param -n llite.*.statahead_stats
10374         rm -r $DIR/$tdir
10375         sync
10376
10377 }
10378 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
10379
10380 test_124a() {
10381         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10382         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10383                 skip_env "no lru resize on server"
10384
10385         local NR=2000
10386
10387         test_mkdir $DIR/$tdir
10388
10389         log "create $NR files at $DIR/$tdir"
10390         createmany -o $DIR/$tdir/f $NR ||
10391                 error "failed to create $NR files in $DIR/$tdir"
10392
10393         cancel_lru_locks mdc
10394         ls -l $DIR/$tdir > /dev/null
10395
10396         local NSDIR=""
10397         local LRU_SIZE=0
10398         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
10399                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
10400                 LRU_SIZE=$($LCTL get_param -n $PARAM)
10401                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
10402                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
10403                         log "NSDIR=$NSDIR"
10404                         log "NS=$(basename $NSDIR)"
10405                         break
10406                 fi
10407         done
10408
10409         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
10410                 skip "Not enough cached locks created!"
10411         fi
10412         log "LRU=$LRU_SIZE"
10413
10414         local SLEEP=30
10415
10416         # We know that lru resize allows one client to hold $LIMIT locks
10417         # for 10h. After that locks begin to be killed by client.
10418         local MAX_HRS=10
10419         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
10420         log "LIMIT=$LIMIT"
10421         if [ $LIMIT -lt $LRU_SIZE ]; then
10422                 skip "Limit is too small $LIMIT"
10423         fi
10424
10425         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
10426         # killing locks. Some time was spent for creating locks. This means
10427         # that up to the moment of sleep finish we must have killed some of
10428         # them (10-100 locks). This depends on how fast ther were created.
10429         # Many of them were touched in almost the same moment and thus will
10430         # be killed in groups.
10431         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
10432
10433         # Use $LRU_SIZE_B here to take into account real number of locks
10434         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
10435         local LRU_SIZE_B=$LRU_SIZE
10436         log "LVF=$LVF"
10437         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
10438         log "OLD_LVF=$OLD_LVF"
10439         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
10440
10441         # Let's make sure that we really have some margin. Client checks
10442         # cached locks every 10 sec.
10443         SLEEP=$((SLEEP+20))
10444         log "Sleep ${SLEEP} sec"
10445         local SEC=0
10446         while ((SEC<$SLEEP)); do
10447                 echo -n "..."
10448                 sleep 5
10449                 SEC=$((SEC+5))
10450                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
10451                 echo -n "$LRU_SIZE"
10452         done
10453         echo ""
10454         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
10455         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
10456
10457         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
10458                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
10459                 unlinkmany $DIR/$tdir/f $NR
10460                 return
10461         }
10462
10463         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
10464         log "unlink $NR files at $DIR/$tdir"
10465         unlinkmany $DIR/$tdir/f $NR
10466 }
10467 run_test 124a "lru resize ======================================="
10468
10469 get_max_pool_limit()
10470 {
10471         local limit=$($LCTL get_param \
10472                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
10473         local max=0
10474         for l in $limit; do
10475                 if [[ $l -gt $max ]]; then
10476                         max=$l
10477                 fi
10478         done
10479         echo $max
10480 }
10481
10482 test_124b() {
10483         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10484         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10485                 skip_env "no lru resize on server"
10486
10487         LIMIT=$(get_max_pool_limit)
10488
10489         NR=$(($(default_lru_size)*20))
10490         if [[ $NR -gt $LIMIT ]]; then
10491                 log "Limit lock number by $LIMIT locks"
10492                 NR=$LIMIT
10493         fi
10494
10495         IFree=$(mdsrate_inodes_available)
10496         if [ $IFree -lt $NR ]; then
10497                 log "Limit lock number by $IFree inodes"
10498                 NR=$IFree
10499         fi
10500
10501         lru_resize_disable mdc
10502         test_mkdir -p $DIR/$tdir/disable_lru_resize
10503
10504         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
10505         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
10506         cancel_lru_locks mdc
10507         stime=`date +%s`
10508         PID=""
10509         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10510         PID="$PID $!"
10511         sleep 2
10512         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10513         PID="$PID $!"
10514         sleep 2
10515         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10516         PID="$PID $!"
10517         wait $PID
10518         etime=`date +%s`
10519         nolruresize_delta=$((etime-stime))
10520         log "ls -la time: $nolruresize_delta seconds"
10521         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10522         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
10523
10524         lru_resize_enable mdc
10525         test_mkdir -p $DIR/$tdir/enable_lru_resize
10526
10527         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
10528         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
10529         cancel_lru_locks mdc
10530         stime=`date +%s`
10531         PID=""
10532         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10533         PID="$PID $!"
10534         sleep 2
10535         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10536         PID="$PID $!"
10537         sleep 2
10538         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10539         PID="$PID $!"
10540         wait $PID
10541         etime=`date +%s`
10542         lruresize_delta=$((etime-stime))
10543         log "ls -la time: $lruresize_delta seconds"
10544         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10545
10546         if [ $lruresize_delta -gt $nolruresize_delta ]; then
10547                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
10548         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
10549                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
10550         else
10551                 log "lru resize performs the same with no lru resize"
10552         fi
10553         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
10554 }
10555 run_test 124b "lru resize (performance test) ======================="
10556
10557 test_124c() {
10558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10559         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10560                 skip_env "no lru resize on server"
10561
10562         # cache ununsed locks on client
10563         local nr=100
10564         cancel_lru_locks mdc
10565         test_mkdir $DIR/$tdir
10566         createmany -o $DIR/$tdir/f $nr ||
10567                 error "failed to create $nr files in $DIR/$tdir"
10568         ls -l $DIR/$tdir > /dev/null
10569
10570         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10571         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10572         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
10573         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
10574         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
10575
10576         # set lru_max_age to 1 sec
10577         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
10578         echo "sleep $((recalc_p * 2)) seconds..."
10579         sleep $((recalc_p * 2))
10580
10581         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
10582         # restore lru_max_age
10583         $LCTL set_param -n $nsdir.lru_max_age $max_age
10584         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
10585         unlinkmany $DIR/$tdir/f $nr
10586 }
10587 run_test 124c "LRUR cancel very aged locks"
10588
10589 test_125() { # 13358
10590         $LCTL get_param -n llite.*.client_type | grep -q local ||
10591                 skip "must run as local client"
10592         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
10593                 skip_env "must have acl enabled"
10594         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
10595
10596         test_mkdir $DIR/$tdir
10597         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
10598         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
10599         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
10600 }
10601 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
10602
10603 test_126() { # bug 12829/13455
10604         $GSS && skip_env "must run as gss disabled"
10605         $LCTL get_param -n llite.*.client_type | grep -q local ||
10606                 skip "must run as local client"
10607         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
10608
10609         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
10610         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
10611         rm -f $DIR/$tfile
10612         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
10613 }
10614 run_test 126 "check that the fsgid provided by the client is taken into account"
10615
10616 test_127a() { # bug 15521
10617         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10618
10619         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
10620         $LCTL set_param osc.*.stats=0
10621         FSIZE=$((2048 * 1024))
10622         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10623         cancel_lru_locks osc
10624         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
10625
10626         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
10627         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10628                 echo "got $COUNT $NAME"
10629                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
10630                 eval $NAME=$COUNT || error "Wrong proc format"
10631
10632                 case $NAME in
10633                         read_bytes|write_bytes)
10634                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
10635                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
10636                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
10637                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
10638                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
10639                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
10640                                 error "sumsquare is too small: $SUMSQ"
10641                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
10642                                 error "sumsquare is too big: $SUMSQ"
10643                         ;;
10644                         *) ;;
10645                 esac
10646         done < $DIR/${tfile}.tmp
10647
10648         #check that we actually got some stats
10649         [ "$read_bytes" ] || error "Missing read_bytes stats"
10650         [ "$write_bytes" ] || error "Missing write_bytes stats"
10651         [ "$read_bytes" != 0 ] || error "no read done"
10652         [ "$write_bytes" != 0 ] || error "no write done"
10653 }
10654 run_test 127a "verify the client stats are sane"
10655
10656 test_127b() { # bug LU-333
10657         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10658         local name count samp unit min max sum sumsq
10659
10660         $LCTL set_param llite.*.stats=0
10661
10662         # perform 2 reads and writes so MAX is different from SUM.
10663         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10664         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10665         cancel_lru_locks osc
10666         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10667         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10668
10669         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
10670         while read name count samp unit min max sum sumsq; do
10671                 echo "got $count $name"
10672                 eval $name=$count || error "Wrong proc format"
10673
10674                 case $name in
10675                 read_bytes)
10676                         [ $count -ne 2 ] && error "count is not 2: $count"
10677                         [ $min -ne $PAGE_SIZE ] &&
10678                                 error "min is not $PAGE_SIZE: $min"
10679                         [ $max -ne $PAGE_SIZE ] &&
10680                                 error "max is incorrect: $max"
10681                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10682                                 error "sum is wrong: $sum"
10683                         ;;
10684                 write_bytes)
10685                         [ $count -ne 2 ] && error "count is not 2: $count"
10686                         [ $min -ne $PAGE_SIZE ] &&
10687                                 error "min is not $PAGE_SIZE: $min"
10688                         [ $max -ne $PAGE_SIZE ] &&
10689                                 error "max is incorrect: $max"
10690                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10691                                 error "sum is wrong: $sum"
10692                         ;;
10693                 *) ;;
10694                 esac
10695         done < $TMP/$tfile.tmp
10696
10697         #check that we actually got some stats
10698         [ "$read_bytes" ] || error "Missing read_bytes stats"
10699         [ "$write_bytes" ] || error "Missing write_bytes stats"
10700         [ "$read_bytes" != 0 ] || error "no read done"
10701         [ "$write_bytes" != 0 ] || error "no write done"
10702
10703         rm -f $TMP/${tfile}.tmp
10704 }
10705 run_test 127b "verify the llite client stats are sane"
10706
10707 test_128() { # bug 15212
10708         touch $DIR/$tfile
10709         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
10710                 find $DIR/$tfile
10711                 find $DIR/$tfile
10712         EOF
10713
10714         result=$(grep error $TMP/$tfile.log)
10715         rm -f $DIR/$tfile $TMP/$tfile.log
10716         [ -z "$result" ] ||
10717                 error "consecutive find's under interactive lfs failed"
10718 }
10719 run_test 128 "interactive lfs for 2 consecutive find's"
10720
10721 set_dir_limits () {
10722         local mntdev
10723         local canondev
10724         local node
10725
10726         local ldproc=/proc/fs/ldiskfs
10727         local facets=$(get_facets MDS)
10728
10729         for facet in ${facets//,/ }; do
10730                 canondev=$(ldiskfs_canon \
10731                            *.$(convert_facet2label $facet).mntdev $facet)
10732                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
10733                         ldproc=/sys/fs/ldiskfs
10734                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
10735                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
10736         done
10737 }
10738
10739 check_mds_dmesg() {
10740         local facets=$(get_facets MDS)
10741         for facet in ${facets//,/ }; do
10742                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
10743         done
10744         return 1
10745 }
10746
10747 test_129() {
10748         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10749         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
10750                 skip "Need MDS version with at least 2.5.56"
10751         if [ "$mds1_FSTYPE" != ldiskfs ]; then
10752                 skip_env "ldiskfs only test"
10753         fi
10754         remote_mds_nodsh && skip "remote MDS with nodsh"
10755
10756         local ENOSPC=28
10757         local EFBIG=27
10758         local has_warning=false
10759
10760         rm -rf $DIR/$tdir
10761         mkdir -p $DIR/$tdir
10762
10763         # block size of mds1
10764         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
10765         set_dir_limits $maxsize $maxsize
10766         local dirsize=$(stat -c%s "$DIR/$tdir")
10767         local nfiles=0
10768         while [[ $dirsize -le $maxsize ]]; do
10769                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
10770                 rc=$?
10771                 if ! $has_warning; then
10772                         check_mds_dmesg '"is approaching"' && has_warning=true
10773                 fi
10774                 # check two errors:
10775                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
10776                 # EFBIG for previous versions included in ldiskfs series
10777                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
10778                         set_dir_limits 0 0
10779                         echo "return code $rc received as expected"
10780
10781                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
10782                                 error_exit "create failed w/o dir size limit"
10783
10784                         check_mds_dmesg '"has reached"' ||
10785                                 error_exit "reached message should be output"
10786
10787                         [ $has_warning = "false" ] &&
10788                                 error_exit "warning message should be output"
10789
10790                         dirsize=$(stat -c%s "$DIR/$tdir")
10791
10792                         [[ $dirsize -ge $maxsize ]] && return 0
10793                         error_exit "current dir size $dirsize, " \
10794                                    "previous limit $maxsize"
10795                 elif [ $rc -ne 0 ]; then
10796                         set_dir_limits 0 0
10797                         error_exit "return $rc received instead of expected " \
10798                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
10799                 fi
10800                 nfiles=$((nfiles + 1))
10801                 dirsize=$(stat -c%s "$DIR/$tdir")
10802         done
10803
10804         set_dir_limits 0 0
10805         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
10806 }
10807 run_test 129 "test directory size limit ========================"
10808
10809 OLDIFS="$IFS"
10810 cleanup_130() {
10811         trap 0
10812         IFS="$OLDIFS"
10813 }
10814
10815 test_130a() {
10816         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10817         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10818
10819         trap cleanup_130 EXIT RETURN
10820
10821         local fm_file=$DIR/$tfile
10822         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
10823         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
10824                 error "dd failed for $fm_file"
10825
10826         # LU-1795: test filefrag/FIEMAP once, even if unsupported
10827         filefrag -ves $fm_file
10828         RC=$?
10829         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
10830                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10831         [ $RC != 0 ] && error "filefrag $fm_file failed"
10832
10833         filefrag_op=$(filefrag -ve -k $fm_file |
10834                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10835         lun=$($LFS getstripe -i $fm_file)
10836
10837         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
10838         IFS=$'\n'
10839         tot_len=0
10840         for line in $filefrag_op
10841         do
10842                 frag_lun=`echo $line | cut -d: -f5`
10843                 ext_len=`echo $line | cut -d: -f4`
10844                 if (( $frag_lun != $lun )); then
10845                         cleanup_130
10846                         error "FIEMAP on 1-stripe file($fm_file) failed"
10847                         return
10848                 fi
10849                 (( tot_len += ext_len ))
10850         done
10851
10852         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
10853                 cleanup_130
10854                 error "FIEMAP on 1-stripe file($fm_file) failed;"
10855                 return
10856         fi
10857
10858         cleanup_130
10859
10860         echo "FIEMAP on single striped file succeeded"
10861 }
10862 run_test 130a "FIEMAP (1-stripe file)"
10863
10864 test_130b() {
10865         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
10866
10867         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10868         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10869
10870         trap cleanup_130 EXIT RETURN
10871
10872         local fm_file=$DIR/$tfile
10873         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
10874                         error "setstripe on $fm_file"
10875         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
10876                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10877
10878         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
10879                 error "dd failed on $fm_file"
10880
10881         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10882         filefrag_op=$(filefrag -ve -k $fm_file |
10883                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10884
10885         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10886                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10887
10888         IFS=$'\n'
10889         tot_len=0
10890         num_luns=1
10891         for line in $filefrag_op
10892         do
10893                 frag_lun=$(echo $line | cut -d: -f5 |
10894                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10895                 ext_len=$(echo $line | cut -d: -f4)
10896                 if (( $frag_lun != $last_lun )); then
10897                         if (( tot_len != 1024 )); then
10898                                 cleanup_130
10899                                 error "FIEMAP on $fm_file failed; returned " \
10900                                 "len $tot_len for OST $last_lun instead of 1024"
10901                                 return
10902                         else
10903                                 (( num_luns += 1 ))
10904                                 tot_len=0
10905                         fi
10906                 fi
10907                 (( tot_len += ext_len ))
10908                 last_lun=$frag_lun
10909         done
10910         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
10911                 cleanup_130
10912                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10913                         "luns or wrong len for OST $last_lun"
10914                 return
10915         fi
10916
10917         cleanup_130
10918
10919         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
10920 }
10921 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
10922
10923 test_130c() {
10924         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10925
10926         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10927         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10928
10929         trap cleanup_130 EXIT RETURN
10930
10931         local fm_file=$DIR/$tfile
10932         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
10933         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
10934                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10935
10936         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
10937                         error "dd failed on $fm_file"
10938
10939         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10940         filefrag_op=$(filefrag -ve -k $fm_file |
10941                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10942
10943         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10944                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10945
10946         IFS=$'\n'
10947         tot_len=0
10948         num_luns=1
10949         for line in $filefrag_op
10950         do
10951                 frag_lun=$(echo $line | cut -d: -f5 |
10952                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10953                 ext_len=$(echo $line | cut -d: -f4)
10954                 if (( $frag_lun != $last_lun )); then
10955                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
10956                         if (( logical != 512 )); then
10957                                 cleanup_130
10958                                 error "FIEMAP on $fm_file failed; returned " \
10959                                 "logical start for lun $logical instead of 512"
10960                                 return
10961                         fi
10962                         if (( tot_len != 512 )); then
10963                                 cleanup_130
10964                                 error "FIEMAP on $fm_file failed; returned " \
10965                                 "len $tot_len for OST $last_lun instead of 1024"
10966                                 return
10967                         else
10968                                 (( num_luns += 1 ))
10969                                 tot_len=0
10970                         fi
10971                 fi
10972                 (( tot_len += ext_len ))
10973                 last_lun=$frag_lun
10974         done
10975         if (( num_luns != 2 || tot_len != 512 )); then
10976                 cleanup_130
10977                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10978                         "luns or wrong len for OST $last_lun"
10979                 return
10980         fi
10981
10982         cleanup_130
10983
10984         echo "FIEMAP on 2-stripe file with hole succeeded"
10985 }
10986 run_test 130c "FIEMAP (2-stripe file with hole)"
10987
10988 test_130d() {
10989         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
10990
10991         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10992         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10993
10994         trap cleanup_130 EXIT RETURN
10995
10996         local fm_file=$DIR/$tfile
10997         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
10998                         error "setstripe on $fm_file"
10999         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11000                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11001
11002         local actual_stripe_count=$($LFS getstripe -c $fm_file)
11003         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11004                 error "dd failed on $fm_file"
11005
11006         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11007         filefrag_op=$(filefrag -ve -k $fm_file |
11008                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11009
11010         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11011                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11012
11013         IFS=$'\n'
11014         tot_len=0
11015         num_luns=1
11016         for line in $filefrag_op
11017         do
11018                 frag_lun=$(echo $line | cut -d: -f5 |
11019                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11020                 ext_len=$(echo $line | cut -d: -f4)
11021                 if (( $frag_lun != $last_lun )); then
11022                         if (( tot_len != 1024 )); then
11023                                 cleanup_130
11024                                 error "FIEMAP on $fm_file failed; returned " \
11025                                 "len $tot_len for OST $last_lun instead of 1024"
11026                                 return
11027                         else
11028                                 (( num_luns += 1 ))
11029                                 tot_len=0
11030                         fi
11031                 fi
11032                 (( tot_len += ext_len ))
11033                 last_lun=$frag_lun
11034         done
11035         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11036                 cleanup_130
11037                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11038                         "luns or wrong len for OST $last_lun"
11039                 return
11040         fi
11041
11042         cleanup_130
11043
11044         echo "FIEMAP on N-stripe file succeeded"
11045 }
11046 run_test 130d "FIEMAP (N-stripe file)"
11047
11048 test_130e() {
11049         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11050
11051         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11052         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11053
11054         trap cleanup_130 EXIT RETURN
11055
11056         local fm_file=$DIR/$tfile
11057         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11058         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
11059                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11060
11061         NUM_BLKS=512
11062         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11063         for ((i = 0; i < $NUM_BLKS; i++))
11064         do
11065                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11066         done
11067
11068         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11069         filefrag_op=$(filefrag -ve -k $fm_file |
11070                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11071
11072         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11073                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11074
11075         IFS=$'\n'
11076         tot_len=0
11077         num_luns=1
11078         for line in $filefrag_op
11079         do
11080                 frag_lun=$(echo $line | cut -d: -f5 |
11081                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11082                 ext_len=$(echo $line | cut -d: -f4)
11083                 if (( $frag_lun != $last_lun )); then
11084                         if (( tot_len != $EXPECTED_LEN )); then
11085                                 cleanup_130
11086                                 error "FIEMAP on $fm_file failed; returned " \
11087                                 "len $tot_len for OST $last_lun instead " \
11088                                 "of $EXPECTED_LEN"
11089                                 return
11090                         else
11091                                 (( num_luns += 1 ))
11092                                 tot_len=0
11093                         fi
11094                 fi
11095                 (( tot_len += ext_len ))
11096                 last_lun=$frag_lun
11097         done
11098         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11099                 cleanup_130
11100                 error "FIEMAP on $fm_file failed; returned wrong number " \
11101                         "of luns or wrong len for OST $last_lun"
11102                 return
11103         fi
11104
11105         cleanup_130
11106
11107         echo "FIEMAP with continuation calls succeeded"
11108 }
11109 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11110
11111 test_130f() {
11112         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11113         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11114
11115         local fm_file=$DIR/$tfile
11116         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
11117                 error "multiop create with lov_delay_create on $fm_file"
11118
11119         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11120         filefrag_extents=$(filefrag -vek $fm_file |
11121                            awk '/extents? found/ { print $2 }')
11122         if [[ "$filefrag_extents" != "0" ]]; then
11123                 error "FIEMAP on $fm_file failed; " \
11124                       "returned $filefrag_extents expected 0"
11125         fi
11126
11127         rm -f $fm_file
11128 }
11129 run_test 130f "FIEMAP (unstriped file)"
11130
11131 # Test for writev/readv
11132 test_131a() {
11133         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
11134                 error "writev test failed"
11135         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
11136                 error "readv failed"
11137         rm -f $DIR/$tfile
11138 }
11139 run_test 131a "test iov's crossing stripe boundary for writev/readv"
11140
11141 test_131b() {
11142         local fsize=$((524288 + 1048576 + 1572864))
11143         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
11144                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11145                         error "append writev test failed"
11146
11147         ((fsize += 1572864 + 1048576))
11148         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
11149                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11150                         error "append writev test failed"
11151         rm -f $DIR/$tfile
11152 }
11153 run_test 131b "test append writev"
11154
11155 test_131c() {
11156         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
11157         error "NOT PASS"
11158 }
11159 run_test 131c "test read/write on file w/o objects"
11160
11161 test_131d() {
11162         rwv -f $DIR/$tfile -w -n 1 1572864
11163         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
11164         if [ "$NOB" != 1572864 ]; then
11165                 error "Short read filed: read $NOB bytes instead of 1572864"
11166         fi
11167         rm -f $DIR/$tfile
11168 }
11169 run_test 131d "test short read"
11170
11171 test_131e() {
11172         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
11173         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
11174         error "read hitting hole failed"
11175         rm -f $DIR/$tfile
11176 }
11177 run_test 131e "test read hitting hole"
11178
11179 check_stats() {
11180         local facet=$1
11181         local op=$2
11182         local want=${3:-0}
11183         local res
11184
11185         case $facet in
11186         mds*) res=$(do_facet $facet \
11187                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
11188                  ;;
11189         ost*) res=$(do_facet $facet \
11190                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
11191                  ;;
11192         *) error "Wrong facet '$facet'" ;;
11193         esac
11194         [ "$res" ] || error "The counter for $op on $facet was not incremented"
11195         # if the argument $3 is zero, it means any stat increment is ok.
11196         if [[ $want -gt 0 ]]; then
11197                 local count=$(echo $res | awk '{ print $2 }')
11198                 [[ $count -ne $want ]] &&
11199                         error "The $op counter on $facet is $count, not $want"
11200         fi
11201 }
11202
11203 test_133a() {
11204         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11205         remote_ost_nodsh && skip "remote OST with nodsh"
11206         remote_mds_nodsh && skip "remote MDS with nodsh"
11207         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11208                 skip_env "MDS doesn't support rename stats"
11209
11210         local testdir=$DIR/${tdir}/stats_testdir
11211
11212         mkdir -p $DIR/${tdir}
11213
11214         # clear stats.
11215         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11216         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11217
11218         # verify mdt stats first.
11219         mkdir ${testdir} || error "mkdir failed"
11220         check_stats $SINGLEMDS "mkdir" 1
11221         touch ${testdir}/${tfile} || error "touch failed"
11222         check_stats $SINGLEMDS "open" 1
11223         check_stats $SINGLEMDS "close" 1
11224         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
11225                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
11226                 check_stats $SINGLEMDS "mknod" 2
11227         }
11228         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
11229         check_stats $SINGLEMDS "unlink" 1
11230         rm -f ${testdir}/${tfile} || error "file remove failed"
11231         check_stats $SINGLEMDS "unlink" 2
11232
11233         # remove working dir and check mdt stats again.
11234         rmdir ${testdir} || error "rmdir failed"
11235         check_stats $SINGLEMDS "rmdir" 1
11236
11237         local testdir1=$DIR/${tdir}/stats_testdir1
11238         mkdir -p ${testdir}
11239         mkdir -p ${testdir1}
11240         touch ${testdir1}/test1
11241         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
11242         check_stats $SINGLEMDS "crossdir_rename" 1
11243
11244         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
11245         check_stats $SINGLEMDS "samedir_rename" 1
11246
11247         rm -rf $DIR/${tdir}
11248 }
11249 run_test 133a "Verifying MDT stats ========================================"
11250
11251 test_133b() {
11252         local res
11253
11254         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11255         remote_ost_nodsh && skip "remote OST with nodsh"
11256         remote_mds_nodsh && skip "remote MDS with nodsh"
11257
11258         local testdir=$DIR/${tdir}/stats_testdir
11259
11260         mkdir -p ${testdir} || error "mkdir failed"
11261         touch ${testdir}/${tfile} || error "touch failed"
11262         cancel_lru_locks mdc
11263
11264         # clear stats.
11265         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11266         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11267
11268         # extra mdt stats verification.
11269         chmod 444 ${testdir}/${tfile} || error "chmod failed"
11270         check_stats $SINGLEMDS "setattr" 1
11271         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11272         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
11273         then            # LU-1740
11274                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
11275                 check_stats $SINGLEMDS "getattr" 1
11276         fi
11277         rm -rf $DIR/${tdir}
11278
11279         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
11280         # so the check below is not reliable
11281         [ $MDSCOUNT -eq 1 ] || return 0
11282
11283         # Sleep to avoid a cached response.
11284         #define OBD_STATFS_CACHE_SECONDS 1
11285         sleep 2
11286         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11287         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11288         $LFS df || error "lfs failed"
11289         check_stats $SINGLEMDS "statfs" 1
11290
11291         # check aggregated statfs (LU-10018)
11292         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
11293                 return 0
11294         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
11295                 return 0
11296         sleep 2
11297         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11298         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11299         df $DIR
11300         check_stats $SINGLEMDS "statfs" 1
11301
11302         # We want to check that the client didn't send OST_STATFS to
11303         # ost1 but the MDT also uses OST_STATFS for precreate. So some
11304         # extra care is needed here.
11305         if remote_mds; then
11306                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
11307                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
11308
11309                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
11310                 [ "$res" ] && error "OST got STATFS"
11311         fi
11312
11313         return 0
11314 }
11315 run_test 133b "Verifying extra MDT stats =================================="
11316
11317 test_133c() {
11318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11319         remote_ost_nodsh && skip "remote OST with nodsh"
11320         remote_mds_nodsh && skip "remote MDS with nodsh"
11321
11322         local testdir=$DIR/$tdir/stats_testdir
11323
11324         test_mkdir -p $testdir
11325
11326         # verify obdfilter stats.
11327         $LFS setstripe -c 1 -i 0 $testdir/$tfile
11328         sync
11329         cancel_lru_locks osc
11330         wait_delete_completed
11331
11332         # clear stats.
11333         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11334         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11335
11336         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
11337                 error "dd failed"
11338         sync
11339         cancel_lru_locks osc
11340         check_stats ost1 "write" 1
11341
11342         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
11343         check_stats ost1 "read" 1
11344
11345         > $testdir/$tfile || error "truncate failed"
11346         check_stats ost1 "punch" 1
11347
11348         rm -f $testdir/$tfile || error "file remove failed"
11349         wait_delete_completed
11350         check_stats ost1 "destroy" 1
11351
11352         rm -rf $DIR/$tdir
11353 }
11354 run_test 133c "Verifying OST stats ========================================"
11355
11356 order_2() {
11357         local value=$1
11358         local orig=$value
11359         local order=1
11360
11361         while [ $value -ge 2 ]; do
11362                 order=$((order*2))
11363                 value=$((value/2))
11364         done
11365
11366         if [ $orig -gt $order ]; then
11367                 order=$((order*2))
11368         fi
11369         echo $order
11370 }
11371
11372 size_in_KMGT() {
11373     local value=$1
11374     local size=('K' 'M' 'G' 'T');
11375     local i=0
11376     local size_string=$value
11377
11378     while [ $value -ge 1024 ]; do
11379         if [ $i -gt 3 ]; then
11380             #T is the biggest unit we get here, if that is bigger,
11381             #just return XXXT
11382             size_string=${value}T
11383             break
11384         fi
11385         value=$((value >> 10))
11386         if [ $value -lt 1024 ]; then
11387             size_string=${value}${size[$i]}
11388             break
11389         fi
11390         i=$((i + 1))
11391     done
11392
11393     echo $size_string
11394 }
11395
11396 get_rename_size() {
11397         local size=$1
11398         local context=${2:-.}
11399         local sample=$(do_facet $SINGLEMDS $LCTL \
11400                 get_param mdt.$FSNAME-MDT0000.rename_stats |
11401                 grep -A1 $context |
11402                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
11403         echo $sample
11404 }
11405
11406 test_133d() {
11407         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11408         remote_ost_nodsh && skip "remote OST with nodsh"
11409         remote_mds_nodsh && skip "remote MDS with nodsh"
11410         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11411                 skip_env "MDS doesn't support rename stats"
11412
11413         local testdir1=$DIR/${tdir}/stats_testdir1
11414         local testdir2=$DIR/${tdir}/stats_testdir2
11415         mkdir -p $DIR/${tdir}
11416
11417         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11418
11419         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
11420         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
11421
11422         createmany -o $testdir1/test 512 || error "createmany failed"
11423
11424         # check samedir rename size
11425         mv ${testdir1}/test0 ${testdir1}/test_0
11426
11427         local testdir1_size=$(ls -l $DIR/${tdir} |
11428                 awk '/stats_testdir1/ {print $5}')
11429         local testdir2_size=$(ls -l $DIR/${tdir} |
11430                 awk '/stats_testdir2/ {print $5}')
11431
11432         testdir1_size=$(order_2 $testdir1_size)
11433         testdir2_size=$(order_2 $testdir2_size)
11434
11435         testdir1_size=$(size_in_KMGT $testdir1_size)
11436         testdir2_size=$(size_in_KMGT $testdir2_size)
11437
11438         echo "source rename dir size: ${testdir1_size}"
11439         echo "target rename dir size: ${testdir2_size}"
11440
11441         local cmd="do_facet $SINGLEMDS $LCTL "
11442         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
11443
11444         eval $cmd || error "$cmd failed"
11445         local samedir=$($cmd | grep 'same_dir')
11446         local same_sample=$(get_rename_size $testdir1_size)
11447         [ -z "$samedir" ] && error "samedir_rename_size count error"
11448         [[ $same_sample -eq 1 ]] ||
11449                 error "samedir_rename_size error $same_sample"
11450         echo "Check same dir rename stats success"
11451
11452         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11453
11454         # check crossdir rename size
11455         mv ${testdir1}/test_0 ${testdir2}/test_0
11456
11457         testdir1_size=$(ls -l $DIR/${tdir} |
11458                 awk '/stats_testdir1/ {print $5}')
11459         testdir2_size=$(ls -l $DIR/${tdir} |
11460                 awk '/stats_testdir2/ {print $5}')
11461
11462         testdir1_size=$(order_2 $testdir1_size)
11463         testdir2_size=$(order_2 $testdir2_size)
11464
11465         testdir1_size=$(size_in_KMGT $testdir1_size)
11466         testdir2_size=$(size_in_KMGT $testdir2_size)
11467
11468         echo "source rename dir size: ${testdir1_size}"
11469         echo "target rename dir size: ${testdir2_size}"
11470
11471         eval $cmd || error "$cmd failed"
11472         local crossdir=$($cmd | grep 'crossdir')
11473         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
11474         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
11475         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
11476         [[ $src_sample -eq 1 ]] ||
11477                 error "crossdir_rename_size error $src_sample"
11478         [[ $tgt_sample -eq 1 ]] ||
11479                 error "crossdir_rename_size error $tgt_sample"
11480         echo "Check cross dir rename stats success"
11481         rm -rf $DIR/${tdir}
11482 }
11483 run_test 133d "Verifying rename_stats ========================================"
11484
11485 test_133e() {
11486         remote_mds_nodsh && skip "remote MDS with nodsh"
11487         remote_ost_nodsh && skip "remote OST with nodsh"
11488         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11489
11490         local testdir=$DIR/${tdir}/stats_testdir
11491         local ctr f0 f1 bs=32768 count=42 sum
11492
11493         mkdir -p ${testdir} || error "mkdir failed"
11494
11495         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
11496
11497         for ctr in {write,read}_bytes; do
11498                 sync
11499                 cancel_lru_locks osc
11500
11501                 do_facet ost1 $LCTL set_param -n \
11502                         "obdfilter.*.exports.clear=clear"
11503
11504                 if [ $ctr = write_bytes ]; then
11505                         f0=/dev/zero
11506                         f1=${testdir}/${tfile}
11507                 else
11508                         f0=${testdir}/${tfile}
11509                         f1=/dev/null
11510                 fi
11511
11512                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
11513                         error "dd failed"
11514                 sync
11515                 cancel_lru_locks osc
11516
11517                 sum=$(do_facet ost1 $LCTL get_param \
11518                         "obdfilter.*.exports.*.stats" |
11519                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
11520                                 $1 == ctr { sum += $7 }
11521                                 END { printf("%0.0f", sum) }')
11522
11523                 if ((sum != bs * count)); then
11524                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
11525                 fi
11526         done
11527
11528         rm -rf $DIR/${tdir}
11529 }
11530 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
11531
11532 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
11533
11534 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
11535 # not honor the -ignore_readdir_race option correctly. So we call
11536 # error_ignore() rather than error() in these cases. See LU-11152.
11537 error_133() {
11538         if (find --version; do_facet mds1 find --version) |
11539                 grep -q '\b4\.5\.1[1-4]\b'; then
11540                 error_ignore LU-11152 "$@"
11541         else
11542                 error "$@"
11543         fi
11544 }
11545
11546 test_133f() {
11547         # First without trusting modes.
11548         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
11549         echo "proc_dirs='$proc_dirs'"
11550         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
11551         find $proc_dirs -exec cat '{}' \; &> /dev/null
11552
11553         # Second verifying readability.
11554         $LCTL get_param -R '*' &> /dev/null
11555
11556         # Verifing writability with badarea_io.
11557         find $proc_dirs \
11558                 -ignore_readdir_race \
11559                 -type f \
11560                 -not -name force_lbug \
11561                 -not -name changelog_mask \
11562                 -exec badarea_io '{}' \; ||
11563                         error_133 "find $proc_dirs failed"
11564 }
11565 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
11566
11567 test_133g() {
11568         remote_mds_nodsh && skip "remote MDS with nodsh"
11569         remote_ost_nodsh && skip "remote OST with nodsh"
11570
11571         # eventually, this can also be replaced with "lctl get_param -R",
11572         # but not until that option is always available on the server
11573         local facet
11574         for facet in mds1 ost1; do
11575                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
11576                         skip_noexit "Too old lustre on $facet"
11577                 local facet_proc_dirs=$(do_facet $facet \
11578                                         \\\ls -d $proc_regexp 2>/dev/null)
11579                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11580                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11581                 do_facet $facet find $facet_proc_dirs \
11582                         ! -name req_history \
11583                         -exec cat '{}' \\\; &> /dev/null
11584
11585                 do_facet $facet find $facet_proc_dirs \
11586                         ! -name req_history \
11587                         -type f \
11588                         -exec cat '{}' \\\; &> /dev/null ||
11589                                 error "proc file read failed"
11590
11591                 do_facet $facet find $facet_proc_dirs \
11592                         -ignore_readdir_race \
11593                         -type f \
11594                         -not -name force_lbug \
11595                         -not -name changelog_mask \
11596                         -exec badarea_io '{}' \\\; ||
11597                                 error_133 "$facet find $facet_proc_dirs failed"
11598         done
11599
11600         # remount the FS in case writes/reads /proc break the FS
11601         cleanup || error "failed to unmount"
11602         setup || error "failed to setup"
11603         true
11604 }
11605 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
11606
11607 test_133h() {
11608         remote_mds_nodsh && skip "remote MDS with nodsh"
11609         remote_ost_nodsh && skip "remote OST with nodsh"
11610         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
11611                 skip "Need MDS version at least 2.9.54"
11612
11613         local facet
11614
11615         for facet in client mds1 ost1; do
11616                 local facet_proc_dirs=$(do_facet $facet \
11617                                         \\\ls -d $proc_regexp 2> /dev/null)
11618                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11619                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11620                 # Get the list of files that are missing the terminating newline
11621                 local missing=($(do_facet $facet \
11622                         find ${facet_proc_dirs} -type f \|              \
11623                                 while read F\; do                       \
11624                                         awk -v FS='\v' -v RS='\v\v'     \
11625                                         "'END { if(NR>0 &&              \
11626                                         \\\$NF !~ /.*\\\n\$/)           \
11627                                                 print FILENAME}'"       \
11628                                         '\$F'\;                         \
11629                                 done 2>/dev/null))
11630                 [ ${#missing[*]} -eq 0 ] ||
11631                         error "files do not end with newline: ${missing[*]}"
11632         done
11633 }
11634 run_test 133h "Proc files should end with newlines"
11635
11636 test_134a() {
11637         remote_mds_nodsh && skip "remote MDS with nodsh"
11638         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11639                 skip "Need MDS version at least 2.7.54"
11640
11641         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11642         cancel_lru_locks mdc
11643
11644         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11645         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11646         [ $unused -eq 0 ] || error "$unused locks are not cleared"
11647
11648         local nr=1000
11649         createmany -o $DIR/$tdir/f $nr ||
11650                 error "failed to create $nr files in $DIR/$tdir"
11651         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11652
11653         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
11654         do_facet mds1 $LCTL set_param fail_loc=0x327
11655         do_facet mds1 $LCTL set_param fail_val=500
11656         touch $DIR/$tdir/m
11657
11658         echo "sleep 10 seconds ..."
11659         sleep 10
11660         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
11661
11662         do_facet mds1 $LCTL set_param fail_loc=0
11663         do_facet mds1 $LCTL set_param fail_val=0
11664         [ $lck_cnt -lt $unused ] ||
11665                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
11666
11667         rm $DIR/$tdir/m
11668         unlinkmany $DIR/$tdir/f $nr
11669 }
11670 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
11671
11672 test_134b() {
11673         remote_mds_nodsh && skip "remote MDS with nodsh"
11674         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11675                 skip "Need MDS version at least 2.7.54"
11676
11677         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11678         cancel_lru_locks mdc
11679
11680         local low_wm=$(do_facet mds1 $LCTL get_param -n \
11681                         ldlm.lock_reclaim_threshold_mb)
11682         # disable reclaim temporarily
11683         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
11684
11685         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
11686         do_facet mds1 $LCTL set_param fail_loc=0x328
11687         do_facet mds1 $LCTL set_param fail_val=500
11688
11689         $LCTL set_param debug=+trace
11690
11691         local nr=600
11692         createmany -o $DIR/$tdir/f $nr &
11693         local create_pid=$!
11694
11695         echo "Sleep $TIMEOUT seconds ..."
11696         sleep $TIMEOUT
11697         if ! ps -p $create_pid  > /dev/null 2>&1; then
11698                 do_facet mds1 $LCTL set_param fail_loc=0
11699                 do_facet mds1 $LCTL set_param fail_val=0
11700                 do_facet mds1 $LCTL set_param \
11701                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
11702                 error "createmany finished incorrectly!"
11703         fi
11704         do_facet mds1 $LCTL set_param fail_loc=0
11705         do_facet mds1 $LCTL set_param fail_val=0
11706         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
11707         wait $create_pid || return 1
11708
11709         unlinkmany $DIR/$tdir/f $nr
11710 }
11711 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
11712
11713 test_140() { #bug-17379
11714         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11715
11716         test_mkdir $DIR/$tdir
11717         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
11718         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
11719
11720         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
11721         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
11722         local i=0
11723         while i=$((i + 1)); do
11724                 test_mkdir $i
11725                 cd $i || error "Changing to $i"
11726                 ln -s ../stat stat || error "Creating stat symlink"
11727                 # Read the symlink until ELOOP present,
11728                 # not LBUGing the system is considered success,
11729                 # we didn't overrun the stack.
11730                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
11731                 if [ $ret -ne 0 ]; then
11732                         if [ $ret -eq 40 ]; then
11733                                 break  # -ELOOP
11734                         else
11735                                 error "Open stat symlink"
11736                                         return
11737                         fi
11738                 fi
11739         done
11740         i=$((i - 1))
11741         echo "The symlink depth = $i"
11742         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
11743                 error "Invalid symlink depth"
11744
11745         # Test recursive symlink
11746         ln -s symlink_self symlink_self
11747         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
11748         echo "open symlink_self returns $ret"
11749         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
11750 }
11751 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
11752
11753 test_150() {
11754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11755
11756         local TF="$TMP/$tfile"
11757
11758         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11759         cp $TF $DIR/$tfile
11760         cancel_lru_locks $OSC
11761         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
11762         remount_client $MOUNT
11763         df -P $MOUNT
11764         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
11765
11766         $TRUNCATE $TF 6000
11767         $TRUNCATE $DIR/$tfile 6000
11768         cancel_lru_locks $OSC
11769         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
11770
11771         echo "12345" >>$TF
11772         echo "12345" >>$DIR/$tfile
11773         cancel_lru_locks $OSC
11774         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
11775
11776         echo "12345" >>$TF
11777         echo "12345" >>$DIR/$tfile
11778         cancel_lru_locks $OSC
11779         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
11780
11781         rm -f $TF
11782         true
11783 }
11784 run_test 150 "truncate/append tests"
11785
11786 #LU-2902 roc_hit was not able to read all values from lproc
11787 function roc_hit_init() {
11788         local list=$(comma_list $(osts_nodes))
11789         local dir=$DIR/$tdir-check
11790         local file=$dir/$tfile
11791         local BEFORE
11792         local AFTER
11793         local idx
11794
11795         test_mkdir $dir
11796         #use setstripe to do a write to every ost
11797         for i in $(seq 0 $((OSTCOUNT-1))); do
11798                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
11799                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
11800                 idx=$(printf %04x $i)
11801                 BEFORE=$(get_osd_param $list *OST*$idx stats |
11802                         awk '$1 == "cache_access" {sum += $7}
11803                                 END { printf("%0.0f", sum) }')
11804
11805                 cancel_lru_locks osc
11806                 cat $file >/dev/null
11807
11808                 AFTER=$(get_osd_param $list *OST*$idx stats |
11809                         awk '$1 == "cache_access" {sum += $7}
11810                                 END { printf("%0.0f", sum) }')
11811
11812                 echo BEFORE:$BEFORE AFTER:$AFTER
11813                 if ! let "AFTER - BEFORE == 4"; then
11814                         rm -rf $dir
11815                         error "roc_hit is not safe to use"
11816                 fi
11817                 rm $file
11818         done
11819
11820         rm -rf $dir
11821 }
11822
11823 function roc_hit() {
11824         local list=$(comma_list $(osts_nodes))
11825         echo $(get_osd_param $list '' stats |
11826                 awk '$1 == "cache_hit" {sum += $7}
11827                         END { printf("%0.0f", sum) }')
11828 }
11829
11830 function set_cache() {
11831         local on=1
11832
11833         if [ "$2" == "off" ]; then
11834                 on=0;
11835         fi
11836         local list=$(comma_list $(osts_nodes))
11837         set_osd_param $list '' $1_cache_enable $on
11838
11839         cancel_lru_locks osc
11840 }
11841
11842 test_151() {
11843         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11844         remote_ost_nodsh && skip "remote OST with nodsh"
11845
11846         local CPAGES=3
11847         local list=$(comma_list $(osts_nodes))
11848
11849         # check whether obdfilter is cache capable at all
11850         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
11851                 skip "not cache-capable obdfilter"
11852         fi
11853
11854         # check cache is enabled on all obdfilters
11855         if get_osd_param $list '' read_cache_enable | grep 0; then
11856                 skip "oss cache is disabled"
11857         fi
11858
11859         set_osd_param $list '' writethrough_cache_enable 1
11860
11861         # check write cache is enabled on all obdfilters
11862         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
11863                 skip "oss write cache is NOT enabled"
11864         fi
11865
11866         roc_hit_init
11867
11868         #define OBD_FAIL_OBD_NO_LRU  0x609
11869         do_nodes $list $LCTL set_param fail_loc=0x609
11870
11871         # pages should be in the case right after write
11872         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
11873                 error "dd failed"
11874
11875         local BEFORE=$(roc_hit)
11876         cancel_lru_locks osc
11877         cat $DIR/$tfile >/dev/null
11878         local AFTER=$(roc_hit)
11879
11880         do_nodes $list $LCTL set_param fail_loc=0
11881
11882         if ! let "AFTER - BEFORE == CPAGES"; then
11883                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11884         fi
11885
11886         # the following read invalidates the cache
11887         cancel_lru_locks osc
11888         set_osd_param $list '' read_cache_enable 0
11889         cat $DIR/$tfile >/dev/null
11890
11891         # now data shouldn't be found in the cache
11892         BEFORE=$(roc_hit)
11893         cancel_lru_locks osc
11894         cat $DIR/$tfile >/dev/null
11895         AFTER=$(roc_hit)
11896         if let "AFTER - BEFORE != 0"; then
11897                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11898         fi
11899
11900         set_osd_param $list '' read_cache_enable 1
11901         rm -f $DIR/$tfile
11902 }
11903 run_test 151 "test cache on oss and controls ==============================="
11904
11905 test_152() {
11906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11907
11908         local TF="$TMP/$tfile"
11909
11910         # simulate ENOMEM during write
11911 #define OBD_FAIL_OST_NOMEM      0x226
11912         lctl set_param fail_loc=0x80000226
11913         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11914         cp $TF $DIR/$tfile
11915         sync || error "sync failed"
11916         lctl set_param fail_loc=0
11917
11918         # discard client's cache
11919         cancel_lru_locks osc
11920
11921         # simulate ENOMEM during read
11922         lctl set_param fail_loc=0x80000226
11923         cmp $TF $DIR/$tfile || error "cmp failed"
11924         lctl set_param fail_loc=0
11925
11926         rm -f $TF
11927 }
11928 run_test 152 "test read/write with enomem ============================"
11929
11930 test_153() {
11931         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
11932 }
11933 run_test 153 "test if fdatasync does not crash ======================="
11934
11935 dot_lustre_fid_permission_check() {
11936         local fid=$1
11937         local ffid=$MOUNT/.lustre/fid/$fid
11938         local test_dir=$2
11939
11940         echo "stat fid $fid"
11941         stat $ffid > /dev/null || error "stat $ffid failed."
11942         echo "touch fid $fid"
11943         touch $ffid || error "touch $ffid failed."
11944         echo "write to fid $fid"
11945         cat /etc/hosts > $ffid || error "write $ffid failed."
11946         echo "read fid $fid"
11947         diff /etc/hosts $ffid || error "read $ffid failed."
11948         echo "append write to fid $fid"
11949         cat /etc/hosts >> $ffid || error "append write $ffid failed."
11950         echo "rename fid $fid"
11951         mv $ffid $test_dir/$tfile.1 &&
11952                 error "rename $ffid to $tfile.1 should fail."
11953         touch $test_dir/$tfile.1
11954         mv $test_dir/$tfile.1 $ffid &&
11955                 error "rename $tfile.1 to $ffid should fail."
11956         rm -f $test_dir/$tfile.1
11957         echo "truncate fid $fid"
11958         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
11959         echo "link fid $fid"
11960         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
11961         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
11962                 echo "setfacl fid $fid"
11963                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
11964                 echo "getfacl fid $fid"
11965                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
11966         fi
11967         echo "unlink fid $fid"
11968         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
11969         echo "mknod fid $fid"
11970         mknod $ffid c 1 3 && error "mknod $ffid should fail."
11971
11972         fid=[0xf00000400:0x1:0x0]
11973         ffid=$MOUNT/.lustre/fid/$fid
11974
11975         echo "stat non-exist fid $fid"
11976         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
11977         echo "write to non-exist fid $fid"
11978         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
11979         echo "link new fid $fid"
11980         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
11981
11982         mkdir -p $test_dir/$tdir
11983         touch $test_dir/$tdir/$tfile
11984         fid=$($LFS path2fid $test_dir/$tdir)
11985         rc=$?
11986         [ $rc -ne 0 ] &&
11987                 error "error: could not get fid for $test_dir/$dir/$tfile."
11988
11989         ffid=$MOUNT/.lustre/fid/$fid
11990
11991         echo "ls $fid"
11992         ls $ffid > /dev/null || error "ls $ffid failed."
11993         echo "touch $fid/$tfile.1"
11994         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
11995
11996         echo "touch $MOUNT/.lustre/fid/$tfile"
11997         touch $MOUNT/.lustre/fid/$tfile && \
11998                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
11999
12000         echo "setxattr to $MOUNT/.lustre/fid"
12001         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12002
12003         echo "listxattr for $MOUNT/.lustre/fid"
12004         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12005
12006         echo "delxattr from $MOUNT/.lustre/fid"
12007         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12008
12009         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12010         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12011                 error "touch invalid fid should fail."
12012
12013         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12014         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12015                 error "touch non-normal fid should fail."
12016
12017         echo "rename $tdir to $MOUNT/.lustre/fid"
12018         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12019                 error "rename to $MOUNT/.lustre/fid should fail."
12020
12021         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
12022         then            # LU-3547
12023                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
12024                 local new_obf_mode=777
12025
12026                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
12027                 chmod $new_obf_mode $DIR/.lustre/fid ||
12028                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
12029
12030                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
12031                 [ $obf_mode -eq $new_obf_mode ] ||
12032                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12033
12034                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12035                 chmod $old_obf_mode $DIR/.lustre/fid ||
12036                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12037         fi
12038
12039         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12040         fid=$($LFS path2fid $test_dir/$tfile-2)
12041
12042         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12043         then # LU-5424
12044                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12045                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12046                         error "create lov data thru .lustre failed"
12047         fi
12048         echo "cp /etc/passwd $test_dir/$tfile-2"
12049         cp /etc/passwd $test_dir/$tfile-2 ||
12050                 error "copy to $test_dir/$tfile-2 failed."
12051         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12052         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12053                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12054
12055         rm -rf $test_dir/tfile.lnk
12056         rm -rf $test_dir/$tfile-2
12057 }
12058
12059 test_154A() {
12060         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12061                 skip "Need MDS version at least 2.4.1"
12062
12063         local tf=$DIR/$tfile
12064         touch $tf
12065
12066         local fid=$($LFS path2fid $tf)
12067         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12068
12069         # check that we get the same pathname back
12070         local found=$($LFS fid2path $MOUNT "$fid")
12071         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12072         [ "$found" == "$tf" ] ||
12073                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12074 }
12075 run_test 154A "lfs path2fid and fid2path basic checks"
12076
12077 test_154B() {
12078         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12079                 skip "Need MDS version at least 2.4.1"
12080
12081         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12082         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12083         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12084         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12085
12086         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12087         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12088
12089         # check that we get the same pathname
12090         echo "PFID: $PFID, name: $name"
12091         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12092         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12093         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12094                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12095
12096         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12097 }
12098 run_test 154B "verify the ll_decode_linkea tool"
12099
12100 test_154a() {
12101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12102         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12103         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12104                 skip "Need MDS version at least 2.2.51"
12105         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12106
12107         cp /etc/hosts $DIR/$tfile
12108
12109         fid=$($LFS path2fid $DIR/$tfile)
12110         rc=$?
12111         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12112
12113         dot_lustre_fid_permission_check "$fid" $DIR ||
12114                 error "dot lustre permission check $fid failed"
12115
12116         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
12117
12118         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
12119
12120         touch $MOUNT/.lustre/file &&
12121                 error "creation is not allowed under .lustre"
12122
12123         mkdir $MOUNT/.lustre/dir &&
12124                 error "mkdir is not allowed under .lustre"
12125
12126         rm -rf $DIR/$tfile
12127 }
12128 run_test 154a "Open-by-FID"
12129
12130 test_154b() {
12131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12132         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12133         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12134         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12135                 skip "Need MDS version at least 2.2.51"
12136
12137         local remote_dir=$DIR/$tdir/remote_dir
12138         local MDTIDX=1
12139         local rc=0
12140
12141         mkdir -p $DIR/$tdir
12142         $LFS mkdir -i $MDTIDX $remote_dir ||
12143                 error "create remote directory failed"
12144
12145         cp /etc/hosts $remote_dir/$tfile
12146
12147         fid=$($LFS path2fid $remote_dir/$tfile)
12148         rc=$?
12149         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
12150
12151         dot_lustre_fid_permission_check "$fid" $remote_dir ||
12152                 error "dot lustre permission check $fid failed"
12153         rm -rf $DIR/$tdir
12154 }
12155 run_test 154b "Open-by-FID for remote directory"
12156
12157 test_154c() {
12158         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12159                 skip "Need MDS version at least 2.4.1"
12160
12161         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
12162         local FID1=$($LFS path2fid $DIR/$tfile.1)
12163         local FID2=$($LFS path2fid $DIR/$tfile.2)
12164         local FID3=$($LFS path2fid $DIR/$tfile.3)
12165
12166         local N=1
12167         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
12168                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
12169                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
12170                 local want=FID$N
12171                 [ "$FID" = "${!want}" ] ||
12172                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
12173                 N=$((N + 1))
12174         done
12175
12176         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
12177         do
12178                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
12179                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
12180                 N=$((N + 1))
12181         done
12182 }
12183 run_test 154c "lfs path2fid and fid2path multiple arguments"
12184
12185 test_154d() {
12186         remote_mds_nodsh && skip "remote MDS with nodsh"
12187         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
12188                 skip "Need MDS version at least 2.5.53"
12189
12190         if remote_mds; then
12191                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
12192         else
12193                 nid="0@lo"
12194         fi
12195         local proc_ofile="mdt.*.exports.'$nid'.open_files"
12196         local fd
12197         local cmd
12198
12199         rm -f $DIR/$tfile
12200         touch $DIR/$tfile
12201
12202         local fid=$($LFS path2fid $DIR/$tfile)
12203         # Open the file
12204         fd=$(free_fd)
12205         cmd="exec $fd<$DIR/$tfile"
12206         eval $cmd
12207         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
12208         echo "$fid_list" | grep "$fid"
12209         rc=$?
12210
12211         cmd="exec $fd>/dev/null"
12212         eval $cmd
12213         if [ $rc -ne 0 ]; then
12214                 error "FID $fid not found in open files list $fid_list"
12215         fi
12216 }
12217 run_test 154d "Verify open file fid"
12218
12219 test_154e()
12220 {
12221         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
12222                 skip "Need MDS version at least 2.6.50"
12223
12224         if ls -a $MOUNT | grep -q '^\.lustre$'; then
12225                 error ".lustre returned by readdir"
12226         fi
12227 }
12228 run_test 154e ".lustre is not returned by readdir"
12229
12230 test_154f() {
12231         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12232
12233         # create parent directory on a single MDT to avoid cross-MDT hardlinks
12234         test_mkdir -p -c1 $DIR/$tdir/d
12235         # test dirs inherit from its stripe
12236         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
12237         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
12238         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
12239         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
12240         touch $DIR/f
12241
12242         # get fid of parents
12243         local FID0=$($LFS path2fid $DIR/$tdir/d)
12244         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
12245         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
12246         local FID3=$($LFS path2fid $DIR)
12247
12248         # check that path2fid --parents returns expected <parent_fid>/name
12249         # 1) test for a directory (single parent)
12250         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
12251         [ "$parent" == "$FID0/foo1" ] ||
12252                 error "expected parent: $FID0/foo1, got: $parent"
12253
12254         # 2) test for a file with nlink > 1 (multiple parents)
12255         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
12256         echo "$parent" | grep -F "$FID1/$tfile" ||
12257                 error "$FID1/$tfile not returned in parent list"
12258         echo "$parent" | grep -F "$FID2/link" ||
12259                 error "$FID2/link not returned in parent list"
12260
12261         # 3) get parent by fid
12262         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
12263         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12264         echo "$parent" | grep -F "$FID1/$tfile" ||
12265                 error "$FID1/$tfile not returned in parent list (by fid)"
12266         echo "$parent" | grep -F "$FID2/link" ||
12267                 error "$FID2/link not returned in parent list (by fid)"
12268
12269         # 4) test for entry in root directory
12270         parent=$($LFS path2fid --parents $DIR/f)
12271         echo "$parent" | grep -F "$FID3/f" ||
12272                 error "$FID3/f not returned in parent list"
12273
12274         # 5) test it on root directory
12275         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
12276                 error "$MOUNT should not have parents"
12277
12278         # enable xattr caching and check that linkea is correctly updated
12279         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12280         save_lustre_params client "llite.*.xattr_cache" > $save
12281         lctl set_param llite.*.xattr_cache 1
12282
12283         # 6.1) linkea update on rename
12284         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
12285
12286         # get parents by fid
12287         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12288         # foo1 should no longer be returned in parent list
12289         echo "$parent" | grep -F "$FID1" &&
12290                 error "$FID1 should no longer be in parent list"
12291         # the new path should appear
12292         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
12293                 error "$FID2/$tfile.moved is not in parent list"
12294
12295         # 6.2) linkea update on unlink
12296         rm -f $DIR/$tdir/d/foo2/link
12297         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12298         # foo2/link should no longer be returned in parent list
12299         echo "$parent" | grep -F "$FID2/link" &&
12300                 error "$FID2/link should no longer be in parent list"
12301         true
12302
12303         rm -f $DIR/f
12304         restore_lustre_params < $save
12305         rm -f $save
12306 }
12307 run_test 154f "get parent fids by reading link ea"
12308
12309 test_154g()
12310 {
12311         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12312         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
12313            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
12314                 skip "Need MDS version at least 2.6.92"
12315
12316         mkdir -p $DIR/$tdir
12317         llapi_fid_test -d $DIR/$tdir
12318 }
12319 run_test 154g "various llapi FID tests"
12320
12321 test_155_small_load() {
12322     local temp=$TMP/$tfile
12323     local file=$DIR/$tfile
12324
12325     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
12326         error "dd of=$temp bs=6096 count=1 failed"
12327     cp $temp $file
12328     cancel_lru_locks $OSC
12329     cmp $temp $file || error "$temp $file differ"
12330
12331     $TRUNCATE $temp 6000
12332     $TRUNCATE $file 6000
12333     cmp $temp $file || error "$temp $file differ (truncate1)"
12334
12335     echo "12345" >>$temp
12336     echo "12345" >>$file
12337     cmp $temp $file || error "$temp $file differ (append1)"
12338
12339     echo "12345" >>$temp
12340     echo "12345" >>$file
12341     cmp $temp $file || error "$temp $file differ (append2)"
12342
12343     rm -f $temp $file
12344     true
12345 }
12346
12347 test_155_big_load() {
12348         remote_ost_nodsh && skip "remote OST with nodsh"
12349
12350         local temp=$TMP/$tfile
12351         local file=$DIR/$tfile
12352
12353         free_min_max
12354         local cache_size=$(do_facet ost$((MAXI+1)) \
12355                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
12356         local large_file_size=$((cache_size * 2))
12357
12358         echo "OSS cache size: $cache_size KB"
12359         echo "Large file size: $large_file_size KB"
12360
12361         [ $MAXV -le $large_file_size ] &&
12362                 skip_env "max available OST size needs > $large_file_size KB"
12363
12364         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
12365
12366         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
12367                 error "dd of=$temp bs=$large_file_size count=1k failed"
12368         cp $temp $file
12369         ls -lh $temp $file
12370         cancel_lru_locks osc
12371         cmp $temp $file || error "$temp $file differ"
12372
12373         rm -f $temp $file
12374         true
12375 }
12376
12377 save_writethrough() {
12378         local facets=$(get_facets OST)
12379
12380         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
12381 }
12382
12383 test_155a() {
12384         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12385
12386         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12387
12388         save_writethrough $p
12389
12390         set_cache read on
12391         set_cache writethrough on
12392         test_155_small_load
12393         restore_lustre_params < $p
12394         rm -f $p
12395 }
12396 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
12397
12398 test_155b() {
12399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12400
12401         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12402
12403         save_writethrough $p
12404
12405         set_cache read on
12406         set_cache writethrough off
12407         test_155_small_load
12408         restore_lustre_params < $p
12409         rm -f $p
12410 }
12411 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
12412
12413 test_155c() {
12414         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12415
12416         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12417
12418         save_writethrough $p
12419
12420         set_cache read off
12421         set_cache writethrough on
12422         test_155_small_load
12423         restore_lustre_params < $p
12424         rm -f $p
12425 }
12426 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
12427
12428 test_155d() {
12429         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12430
12431         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12432
12433         save_writethrough $p
12434
12435         set_cache read off
12436         set_cache writethrough off
12437         test_155_small_load
12438         restore_lustre_params < $p
12439         rm -f $p
12440 }
12441 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
12442
12443 test_155e() {
12444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12445
12446         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12447
12448         save_writethrough $p
12449
12450         set_cache read on
12451         set_cache writethrough on
12452         test_155_big_load
12453         restore_lustre_params < $p
12454         rm -f $p
12455 }
12456 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
12457
12458 test_155f() {
12459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12460
12461         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12462
12463         save_writethrough $p
12464
12465         set_cache read on
12466         set_cache writethrough off
12467         test_155_big_load
12468         restore_lustre_params < $p
12469         rm -f $p
12470 }
12471 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
12472
12473 test_155g() {
12474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12475
12476         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12477
12478         save_writethrough $p
12479
12480         set_cache read off
12481         set_cache writethrough on
12482         test_155_big_load
12483         restore_lustre_params < $p
12484         rm -f $p
12485 }
12486 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
12487
12488 test_155h() {
12489         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12490
12491         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12492
12493         save_writethrough $p
12494
12495         set_cache read off
12496         set_cache writethrough off
12497         test_155_big_load
12498         restore_lustre_params < $p
12499         rm -f $p
12500 }
12501 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
12502
12503 test_156() {
12504         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12505         remote_ost_nodsh && skip "remote OST with nodsh"
12506         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
12507                 skip "stats not implemented on old servers"
12508         [ "$ost1_FSTYPE" = "zfs" ] &&
12509                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
12510
12511         local CPAGES=3
12512         local BEFORE
12513         local AFTER
12514         local file="$DIR/$tfile"
12515         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12516
12517         save_writethrough $p
12518         roc_hit_init
12519
12520         log "Turn on read and write cache"
12521         set_cache read on
12522         set_cache writethrough on
12523
12524         log "Write data and read it back."
12525         log "Read should be satisfied from the cache."
12526         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12527         BEFORE=$(roc_hit)
12528         cancel_lru_locks osc
12529         cat $file >/dev/null
12530         AFTER=$(roc_hit)
12531         if ! let "AFTER - BEFORE == CPAGES"; then
12532                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12533         else
12534                 log "cache hits:: before: $BEFORE, after: $AFTER"
12535         fi
12536
12537         log "Read again; it should be satisfied from the cache."
12538         BEFORE=$AFTER
12539         cancel_lru_locks osc
12540         cat $file >/dev/null
12541         AFTER=$(roc_hit)
12542         if ! let "AFTER - BEFORE == CPAGES"; then
12543                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12544         else
12545                 log "cache hits:: before: $BEFORE, after: $AFTER"
12546         fi
12547
12548         log "Turn off the read cache and turn on the write cache"
12549         set_cache read off
12550         set_cache writethrough on
12551
12552         log "Read again; it should be satisfied from the cache."
12553         BEFORE=$(roc_hit)
12554         cancel_lru_locks osc
12555         cat $file >/dev/null
12556         AFTER=$(roc_hit)
12557         if ! let "AFTER - BEFORE == CPAGES"; then
12558                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12559         else
12560                 log "cache hits:: before: $BEFORE, after: $AFTER"
12561         fi
12562
12563         log "Read again; it should not be satisfied from the cache."
12564         BEFORE=$AFTER
12565         cancel_lru_locks osc
12566         cat $file >/dev/null
12567         AFTER=$(roc_hit)
12568         if ! let "AFTER - BEFORE == 0"; then
12569                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12570         else
12571                 log "cache hits:: before: $BEFORE, after: $AFTER"
12572         fi
12573
12574         log "Write data and read it back."
12575         log "Read should be satisfied from the cache."
12576         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12577         BEFORE=$(roc_hit)
12578         cancel_lru_locks osc
12579         cat $file >/dev/null
12580         AFTER=$(roc_hit)
12581         if ! let "AFTER - BEFORE == CPAGES"; then
12582                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12583         else
12584                 log "cache hits:: before: $BEFORE, after: $AFTER"
12585         fi
12586
12587         log "Read again; it should not be satisfied from the cache."
12588         BEFORE=$AFTER
12589         cancel_lru_locks osc
12590         cat $file >/dev/null
12591         AFTER=$(roc_hit)
12592         if ! let "AFTER - BEFORE == 0"; then
12593                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12594         else
12595                 log "cache hits:: before: $BEFORE, after: $AFTER"
12596         fi
12597
12598         log "Turn off read and write cache"
12599         set_cache read off
12600         set_cache writethrough off
12601
12602         log "Write data and read it back"
12603         log "It should not be satisfied from the cache."
12604         rm -f $file
12605         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12606         cancel_lru_locks osc
12607         BEFORE=$(roc_hit)
12608         cat $file >/dev/null
12609         AFTER=$(roc_hit)
12610         if ! let "AFTER - BEFORE == 0"; then
12611                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12612         else
12613                 log "cache hits:: before: $BEFORE, after: $AFTER"
12614         fi
12615
12616         log "Turn on the read cache and turn off the write cache"
12617         set_cache read on
12618         set_cache writethrough off
12619
12620         log "Write data and read it back"
12621         log "It should not be satisfied from the cache."
12622         rm -f $file
12623         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12624         BEFORE=$(roc_hit)
12625         cancel_lru_locks osc
12626         cat $file >/dev/null
12627         AFTER=$(roc_hit)
12628         if ! let "AFTER - BEFORE == 0"; then
12629                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12630         else
12631                 log "cache hits:: before: $BEFORE, after: $AFTER"
12632         fi
12633
12634         log "Read again; it should be satisfied from the cache."
12635         BEFORE=$(roc_hit)
12636         cancel_lru_locks osc
12637         cat $file >/dev/null
12638         AFTER=$(roc_hit)
12639         if ! let "AFTER - BEFORE == CPAGES"; then
12640                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12641         else
12642                 log "cache hits:: before: $BEFORE, after: $AFTER"
12643         fi
12644
12645         restore_lustre_params < $p
12646         rm -f $p $file
12647 }
12648 run_test 156 "Verification of tunables"
12649
12650 test_160a() {
12651         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12652         remote_mds_nodsh && skip "remote MDS with nodsh"
12653         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
12654                 skip "Need MDS version at least 2.2.0"
12655
12656         changelog_register || error "changelog_register failed"
12657         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12658         changelog_users $SINGLEMDS | grep -q $cl_user ||
12659                 error "User $cl_user not found in changelog_users"
12660
12661         # change something
12662         test_mkdir -p $DIR/$tdir/pics/2008/zachy
12663         changelog_clear 0 || error "changelog_clear failed"
12664         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
12665         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
12666         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
12667         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
12668         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
12669         rm $DIR/$tdir/pics/desktop.jpg
12670
12671         changelog_dump | tail -10
12672
12673         echo "verifying changelog mask"
12674         changelog_chmask "-MKDIR"
12675         changelog_chmask "-CLOSE"
12676
12677         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
12678         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
12679
12680         changelog_chmask "+MKDIR"
12681         changelog_chmask "+CLOSE"
12682
12683         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
12684         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
12685
12686         changelog_dump | tail -10
12687         MKDIRS=$(changelog_dump | grep -c "MKDIR")
12688         CLOSES=$(changelog_dump | grep -c "CLOSE")
12689         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
12690         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
12691
12692         # verify contents
12693         echo "verifying target fid"
12694         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
12695         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
12696         [ "$fidc" == "$fidf" ] ||
12697                 error "changelog '$tfile' fid $fidc != file fid $fidf"
12698         echo "verifying parent fid"
12699         # The FID returned from the Changelog may be the directory shard on
12700         # a different MDT, and not the FID returned by path2fid on the parent.
12701         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
12702         # since this is what will matter when recreating this file in the tree.
12703         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
12704         local pathp=$($LFS fid2path $MOUNT "$fidp")
12705         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
12706                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
12707
12708         echo "getting records for $cl_user"
12709         changelog_users $SINGLEMDS
12710         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
12711         local nclr=3
12712         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
12713                 error "changelog_clear failed"
12714         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
12715         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
12716         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
12717                 error "user index expect $user_rec1 + $nclr != $user_rec2"
12718
12719         local min0_rec=$(changelog_users $SINGLEMDS |
12720                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
12721         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
12722                           awk '{ print $1; exit; }')
12723
12724         changelog_dump | tail -n 5
12725         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
12726         [ $first_rec == $((min0_rec + 1)) ] ||
12727                 error "first index should be $min0_rec + 1 not $first_rec"
12728
12729         # LU-3446 changelog index reset on MDT restart
12730         local cur_rec1=$(changelog_users $SINGLEMDS |
12731                          awk '/^current.index:/ { print $NF }')
12732         changelog_clear 0 ||
12733                 error "clear all changelog records for $cl_user failed"
12734         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
12735         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
12736                 error "Fail to start $SINGLEMDS"
12737         local cur_rec2=$(changelog_users $SINGLEMDS |
12738                          awk '/^current.index:/ { print $NF }')
12739         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
12740         [ $cur_rec1 == $cur_rec2 ] ||
12741                 error "current index should be $cur_rec1 not $cur_rec2"
12742
12743         echo "verifying users from this test are deregistered"
12744         changelog_deregister || error "changelog_deregister failed"
12745         changelog_users $SINGLEMDS | grep -q $cl_user &&
12746                 error "User '$cl_user' still in changelog_users"
12747
12748         # lctl get_param -n mdd.*.changelog_users
12749         # current index: 144
12750         # ID    index (idle seconds)
12751         # cl3   144 (2)
12752         if ! changelog_users $SINGLEMDS | grep "^cl"; then
12753                 # this is the normal case where all users were deregistered
12754                 # make sure no new records are added when no users are present
12755                 local last_rec1=$(changelog_users $SINGLEMDS |
12756                                   awk '/^current.index:/ { print $NF }')
12757                 touch $DIR/$tdir/chloe
12758                 local last_rec2=$(changelog_users $SINGLEMDS |
12759                                   awk '/^current.index:/ { print $NF }')
12760                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
12761                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
12762         else
12763                 # any changelog users must be leftovers from a previous test
12764                 changelog_users $SINGLEMDS
12765                 echo "other changelog users; can't verify off"
12766         fi
12767 }
12768 run_test 160a "changelog sanity"
12769
12770 test_160b() { # LU-3587
12771         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12772         remote_mds_nodsh && skip "remote MDS with nodsh"
12773         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
12774                 skip "Need MDS version at least 2.2.0"
12775
12776         changelog_register || error "changelog_register failed"
12777         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12778         changelog_users $SINGLEMDS | grep -q $cl_user ||
12779                 error "User '$cl_user' not found in changelog_users"
12780
12781         local longname1=$(str_repeat a 255)
12782         local longname2=$(str_repeat b 255)
12783
12784         cd $DIR
12785         echo "creating very long named file"
12786         touch $longname1 || error "create of '$longname1' failed"
12787         echo "renaming very long named file"
12788         mv $longname1 $longname2
12789
12790         changelog_dump | grep RENME | tail -n 5
12791         rm -f $longname2
12792 }
12793 run_test 160b "Verify that very long rename doesn't crash in changelog"
12794
12795 test_160c() {
12796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12797         remote_mds_nodsh && skip "remote MDS with nodsh"
12798
12799         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
12800                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
12801                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
12802                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
12803
12804         local rc=0
12805
12806         # Registration step
12807         changelog_register || error "changelog_register failed"
12808
12809         rm -rf $DIR/$tdir
12810         mkdir -p $DIR/$tdir
12811         $MCREATE $DIR/$tdir/foo_160c
12812         changelog_chmask "-TRUNC"
12813         $TRUNCATE $DIR/$tdir/foo_160c 200
12814         changelog_chmask "+TRUNC"
12815         $TRUNCATE $DIR/$tdir/foo_160c 199
12816         changelog_dump | tail -n 5
12817         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
12818         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
12819 }
12820 run_test 160c "verify that changelog log catch the truncate event"
12821
12822 test_160d() {
12823         remote_mds_nodsh && skip "remote MDS with nodsh"
12824         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12825         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12826         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
12827                 skip "Need MDS version at least 2.7.60"
12828
12829         # Registration step
12830         changelog_register || error "changelog_register failed"
12831
12832         mkdir -p $DIR/$tdir/migrate_dir
12833         changelog_clear 0 || error "changelog_clear failed"
12834
12835         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
12836         changelog_dump | tail -n 5
12837         local migrates=$(changelog_dump | grep -c "MIGRT")
12838         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
12839 }
12840 run_test 160d "verify that changelog log catch the migrate event"
12841
12842 test_160e() {
12843         remote_mds_nodsh && skip "remote MDS with nodsh"
12844
12845         # Create a user
12846         changelog_register || error "changelog_register failed"
12847
12848         # Delete a future user (expect fail)
12849         local MDT0=$(facet_svc $SINGLEMDS)
12850         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
12851         local rc=$?
12852
12853         if [ $rc -eq 0 ]; then
12854                 error "Deleted non-existant user cl77"
12855         elif [ $rc -ne 2 ]; then
12856                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
12857         fi
12858
12859         # Clear to a bad index (1 billion should be safe)
12860         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
12861         rc=$?
12862
12863         if [ $rc -eq 0 ]; then
12864                 error "Successfully cleared to invalid CL index"
12865         elif [ $rc -ne 22 ]; then
12866                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
12867         fi
12868 }
12869 run_test 160e "changelog negative testing (should return errors)"
12870
12871 test_160f() {
12872         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12873         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
12874                 skip "Need MDS version at least 2.10.56"
12875
12876         local mdts=$(comma_list $(mdts_nodes))
12877
12878         # Create a user
12879         changelog_register || error "first changelog_register failed"
12880         changelog_register || error "second changelog_register failed"
12881         local cl_users
12882         declare -A cl_user1
12883         declare -A cl_user2
12884         local user_rec1
12885         local user_rec2
12886         local i
12887
12888         # generate some changelog records to accumulate on each MDT
12889         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12890         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12891                 error "create $DIR/$tdir/$tfile failed"
12892
12893         # check changelogs have been generated
12894         local nbcl=$(changelog_dump | wc -l)
12895         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12896
12897         for param in "changelog_max_idle_time=10" \
12898                      "changelog_gc=1" \
12899                      "changelog_min_gc_interval=2" \
12900                      "changelog_min_free_cat_entries=3"; do
12901                 local MDT0=$(facet_svc $SINGLEMDS)
12902                 local var="${param%=*}"
12903                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12904
12905                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12906                 do_nodes $mdts $LCTL set_param mdd.*.$param
12907         done
12908
12909         # force cl_user2 to be idle (1st part)
12910         sleep 9
12911
12912         # simulate changelog catalog almost full
12913         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12914         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12915
12916         for i in $(seq $MDSCOUNT); do
12917                 cl_users=(${CL_USERS[mds$i]})
12918                 cl_user1[mds$i]="${cl_users[0]}"
12919                 cl_user2[mds$i]="${cl_users[1]}"
12920
12921                 [ -n "${cl_user1[mds$i]}" ] ||
12922                         error "mds$i: no user registered"
12923                 [ -n "${cl_user2[mds$i]}" ] ||
12924                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12925
12926                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12927                 [ -n "$user_rec1" ] ||
12928                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12929                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12930                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12931                 [ -n "$user_rec2" ] ||
12932                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12933                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12934                      "$user_rec1 + 2 == $user_rec2"
12935                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12936                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12937                               "$user_rec1 + 2, but is $user_rec2"
12938                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12939                 [ -n "$user_rec2" ] ||
12940                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12941                 [ $user_rec1 == $user_rec2 ] ||
12942                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12943                               "$user_rec1, but is $user_rec2"
12944         done
12945
12946         # force cl_user2 to be idle (2nd part) and to reach
12947         # changelog_max_idle_time
12948         sleep 2
12949
12950         # generate one more changelog to trigger fail_loc
12951         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12952                 error "create $DIR/$tdir/${tfile}bis failed"
12953
12954         # ensure gc thread is done
12955         for i in $(mdts_nodes); do
12956                 wait_update $i \
12957                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
12958                         error "$i: GC-thread not done"
12959         done
12960
12961         local first_rec
12962         for i in $(seq $MDSCOUNT); do
12963                 # check cl_user1 still registered
12964                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12965                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12966                 # check cl_user2 unregistered
12967                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12968                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12969
12970                 # check changelogs are present and starting at $user_rec1 + 1
12971                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12972                 [ -n "$user_rec1" ] ||
12973                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12974                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12975                             awk '{ print $1; exit; }')
12976
12977                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12978                 [ $((user_rec1 + 1)) == $first_rec ] ||
12979                         error "mds$i: first index should be $user_rec1 + 1, " \
12980                               "but is $first_rec"
12981         done
12982 }
12983 run_test 160f "changelog garbage collect (timestamped users)"
12984
12985 test_160g() {
12986         remote_mds_nodsh && skip "remote MDS with nodsh"
12987         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
12988                 skip "Need MDS version at least 2.10.56"
12989
12990         local mdts=$(comma_list $(mdts_nodes))
12991
12992         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
12993         do_nodes $mdts $LCTL set_param fail_loc=0x1314
12994
12995         # Create a user
12996         changelog_register || error "first changelog_register failed"
12997         changelog_register || error "second changelog_register failed"
12998         local cl_users
12999         declare -A cl_user1
13000         declare -A cl_user2
13001         local user_rec1
13002         local user_rec2
13003         local i
13004
13005         # generate some changelog records to accumulate on each MDT
13006         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13007         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13008                 error "create $DIR/$tdir/$tfile failed"
13009
13010         # check changelogs have been generated
13011         local nbcl=$(changelog_dump | wc -l)
13012         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13013
13014         # reduce the max_idle_indexes value to make sure we exceed it
13015         max_ndx=$((nbcl / 2 - 1))
13016
13017         for param in "changelog_max_idle_indexes=$max_ndx" \
13018                      "changelog_gc=1" \
13019                      "changelog_min_gc_interval=2" \
13020                      "changelog_min_free_cat_entries=3"; do
13021                 local MDT0=$(facet_svc $SINGLEMDS)
13022                 local var="${param%=*}"
13023                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13024
13025                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13026                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
13027                         error "unable to set mdd.*.$param"
13028         done
13029
13030         # simulate changelog catalog almost full
13031         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13032         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13033
13034         for i in $(seq $MDSCOUNT); do
13035                 cl_users=(${CL_USERS[mds$i]})
13036                 cl_user1[mds$i]="${cl_users[0]}"
13037                 cl_user2[mds$i]="${cl_users[1]}"
13038
13039                 [ -n "${cl_user1[mds$i]}" ] ||
13040                         error "mds$i: no user registered"
13041                 [ -n "${cl_user2[mds$i]}" ] ||
13042                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13043
13044                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13045                 [ -n "$user_rec1" ] ||
13046                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13047                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13048                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13049                 [ -n "$user_rec2" ] ||
13050                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13051                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13052                      "$user_rec1 + 2 == $user_rec2"
13053                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13054                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13055                               "$user_rec1 + 2, but is $user_rec2"
13056                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13057                 [ -n "$user_rec2" ] ||
13058                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13059                 [ $user_rec1 == $user_rec2 ] ||
13060                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13061                               "$user_rec1, but is $user_rec2"
13062         done
13063
13064         # ensure we are past the previous changelog_min_gc_interval set above
13065         sleep 2
13066
13067         # generate one more changelog to trigger fail_loc
13068         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13069                 error "create $DIR/$tdir/${tfile}bis failed"
13070
13071         # ensure gc thread is done
13072         for i in $(mdts_nodes); do
13073                 wait_update $i \
13074                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13075                         error "$i: GC-thread not done"
13076         done
13077
13078         local first_rec
13079         for i in $(seq $MDSCOUNT); do
13080                 # check cl_user1 still registered
13081                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13082                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13083                 # check cl_user2 unregistered
13084                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13085                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13086
13087                 # check changelogs are present and starting at $user_rec1 + 1
13088                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13089                 [ -n "$user_rec1" ] ||
13090                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13091                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13092                             awk '{ print $1; exit; }')
13093
13094                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13095                 [ $((user_rec1 + 1)) == $first_rec ] ||
13096                         error "mds$i: first index should be $user_rec1 + 1, " \
13097                               "but is $first_rec"
13098         done
13099 }
13100 run_test 160g "changelog garbage collect (old users)"
13101
13102 test_160h() {
13103         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13104         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13105                 skip "Need MDS version at least 2.10.56"
13106
13107         local mdts=$(comma_list $(mdts_nodes))
13108
13109         # Create a user
13110         changelog_register || error "first changelog_register failed"
13111         changelog_register || error "second changelog_register failed"
13112         local cl_users
13113         declare -A cl_user1
13114         declare -A cl_user2
13115         local user_rec1
13116         local user_rec2
13117         local i
13118
13119         # generate some changelog records to accumulate on each MDT
13120         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13121         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13122                 error "create $DIR/$tdir/$tfile failed"
13123
13124         # check changelogs have been generated
13125         local nbcl=$(changelog_dump | wc -l)
13126         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13127
13128         for param in "changelog_max_idle_time=10" \
13129                      "changelog_gc=1" \
13130                      "changelog_min_gc_interval=2"; do
13131                 local MDT0=$(facet_svc $SINGLEMDS)
13132                 local var="${param%=*}"
13133                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13134
13135                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13136                 do_nodes $mdts $LCTL set_param mdd.*.$param
13137         done
13138
13139         # force cl_user2 to be idle (1st part)
13140         sleep 9
13141
13142         for i in $(seq $MDSCOUNT); do
13143                 cl_users=(${CL_USERS[mds$i]})
13144                 cl_user1[mds$i]="${cl_users[0]}"
13145                 cl_user2[mds$i]="${cl_users[1]}"
13146
13147                 [ -n "${cl_user1[mds$i]}" ] ||
13148                         error "mds$i: no user registered"
13149                 [ -n "${cl_user2[mds$i]}" ] ||
13150                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13151
13152                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13153                 [ -n "$user_rec1" ] ||
13154                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13155                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13156                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13157                 [ -n "$user_rec2" ] ||
13158                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13159                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13160                      "$user_rec1 + 2 == $user_rec2"
13161                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13162                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13163                               "$user_rec1 + 2, but is $user_rec2"
13164                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13165                 [ -n "$user_rec2" ] ||
13166                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13167                 [ $user_rec1 == $user_rec2 ] ||
13168                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13169                               "$user_rec1, but is $user_rec2"
13170         done
13171
13172         # force cl_user2 to be idle (2nd part) and to reach
13173         # changelog_max_idle_time
13174         sleep 2
13175
13176         # force each GC-thread start and block then
13177         # one per MDT/MDD, set fail_val accordingly
13178         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
13179         do_nodes $mdts $LCTL set_param fail_loc=0x1316
13180
13181         # generate more changelogs to trigger fail_loc
13182         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13183                 error "create $DIR/$tdir/${tfile}bis failed"
13184
13185         # stop MDT to stop GC-thread, should be done in back-ground as it will
13186         # block waiting for the thread to be released and exit
13187         declare -A stop_pids
13188         for i in $(seq $MDSCOUNT); do
13189                 stop mds$i &
13190                 stop_pids[mds$i]=$!
13191         done
13192
13193         for i in $(mdts_nodes); do
13194                 local facet
13195                 local nb=0
13196                 local facets=$(facets_up_on_host $i)
13197
13198                 for facet in ${facets//,/ }; do
13199                         if [[ $facet == mds* ]]; then
13200                                 nb=$((nb + 1))
13201                         fi
13202                 done
13203                 # ensure each MDS's gc threads are still present and all in "R"
13204                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
13205                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
13206                         error "$i: expected $nb GC-thread"
13207                 wait_update $i \
13208                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
13209                         "R" 20 ||
13210                         error "$i: GC-thread not found in R-state"
13211                 # check umounts of each MDT on MDS have reached kthread_stop()
13212                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
13213                         error "$i: expected $nb umount"
13214                 wait_update $i \
13215                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
13216                         error "$i: umount not found in D-state"
13217         done
13218
13219         # release all GC-threads
13220         do_nodes $mdts $LCTL set_param fail_loc=0
13221
13222         # wait for MDT stop to complete
13223         for i in $(seq $MDSCOUNT); do
13224                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
13225         done
13226
13227         # XXX
13228         # may try to check if any orphan changelog records are present
13229         # via ldiskfs/zfs and llog_reader...
13230
13231         # re-start/mount MDTs
13232         for i in $(seq $MDSCOUNT); do
13233                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
13234                         error "Fail to start mds$i"
13235         done
13236
13237         local first_rec
13238         for i in $(seq $MDSCOUNT); do
13239                 # check cl_user1 still registered
13240                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13241                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13242                 # check cl_user2 unregistered
13243                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13244                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13245
13246                 # check changelogs are present and starting at $user_rec1 + 1
13247                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13248                 [ -n "$user_rec1" ] ||
13249                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13250                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13251                             awk '{ print $1; exit; }')
13252
13253                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13254                 [ $((user_rec1 + 1)) == $first_rec ] ||
13255                         error "mds$i: first index should be $user_rec1 + 1, " \
13256                               "but is $first_rec"
13257         done
13258 }
13259 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
13260               "during mount"
13261
13262 test_160i() {
13263
13264         local mdts=$(comma_list $(mdts_nodes))
13265
13266         changelog_register || error "first changelog_register failed"
13267
13268         # generate some changelog records to accumulate on each MDT
13269         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13270         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13271                 error "create $DIR/$tdir/$tfile failed"
13272
13273         # check changelogs have been generated
13274         local nbcl=$(changelog_dump | wc -l)
13275         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13276
13277         # simulate race between register and unregister
13278         # XXX as fail_loc is set per-MDS, with DNE configs the race
13279         # simulation will only occur for one MDT per MDS and for the
13280         # others the normal race scenario will take place
13281         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
13282         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
13283         do_nodes $mdts $LCTL set_param fail_val=1
13284
13285         # unregister 1st user
13286         changelog_deregister &
13287         local pid1=$!
13288         # wait some time for deregister work to reach race rdv
13289         sleep 2
13290         # register 2nd user
13291         changelog_register || error "2nd user register failed"
13292
13293         wait $pid1 || error "1st user deregister failed"
13294
13295         local i
13296         local last_rec
13297         declare -A LAST_REC
13298         for i in $(seq $MDSCOUNT); do
13299                 if changelog_users mds$i | grep "^cl"; then
13300                         # make sure new records are added with one user present
13301                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
13302                                           awk '/^current.index:/ { print $NF }')
13303                 else
13304                         error "mds$i has no user registered"
13305                 fi
13306         done
13307
13308         # generate more changelog records to accumulate on each MDT
13309         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13310                 error "create $DIR/$tdir/${tfile}bis failed"
13311
13312         for i in $(seq $MDSCOUNT); do
13313                 last_rec=$(changelog_users $SINGLEMDS |
13314                            awk '/^current.index:/ { print $NF }')
13315                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
13316                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
13317                         error "changelogs are off on mds$i"
13318         done
13319 }
13320 run_test 160i "changelog user register/unregister race"
13321
13322 test_161a() {
13323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13324
13325         test_mkdir -c1 $DIR/$tdir
13326         cp /etc/hosts $DIR/$tdir/$tfile
13327         test_mkdir -c1 $DIR/$tdir/foo1
13328         test_mkdir -c1 $DIR/$tdir/foo2
13329         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
13330         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
13331         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
13332         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
13333         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
13334         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13335                 $LFS fid2path $DIR $FID
13336                 error "bad link ea"
13337         fi
13338         # middle
13339         rm $DIR/$tdir/foo2/zachary
13340         # last
13341         rm $DIR/$tdir/foo2/thor
13342         # first
13343         rm $DIR/$tdir/$tfile
13344         # rename
13345         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
13346         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
13347                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
13348         rm $DIR/$tdir/foo2/maggie
13349
13350         # overflow the EA
13351         local longname=$tfile.avg_len_is_thirty_two_
13352         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
13353                 error_noexit 'failed to unlink many hardlinks'" EXIT
13354         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
13355                 error "failed to hardlink many files"
13356         links=$($LFS fid2path $DIR $FID | wc -l)
13357         echo -n "${links}/1000 links in link EA"
13358         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
13359 }
13360 run_test 161a "link ea sanity"
13361
13362 test_161b() {
13363         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13364         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
13365
13366         local MDTIDX=1
13367         local remote_dir=$DIR/$tdir/remote_dir
13368
13369         mkdir -p $DIR/$tdir
13370         $LFS mkdir -i $MDTIDX $remote_dir ||
13371                 error "create remote directory failed"
13372
13373         cp /etc/hosts $remote_dir/$tfile
13374         mkdir -p $remote_dir/foo1
13375         mkdir -p $remote_dir/foo2
13376         ln $remote_dir/$tfile $remote_dir/foo1/sofia
13377         ln $remote_dir/$tfile $remote_dir/foo2/zachary
13378         ln $remote_dir/$tfile $remote_dir/foo1/luna
13379         ln $remote_dir/$tfile $remote_dir/foo2/thor
13380
13381         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
13382                      tr -d ']')
13383         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13384                 $LFS fid2path $DIR $FID
13385                 error "bad link ea"
13386         fi
13387         # middle
13388         rm $remote_dir/foo2/zachary
13389         # last
13390         rm $remote_dir/foo2/thor
13391         # first
13392         rm $remote_dir/$tfile
13393         # rename
13394         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
13395         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
13396         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
13397                 $LFS fid2path $DIR $FID
13398                 error "bad link rename"
13399         fi
13400         rm $remote_dir/foo2/maggie
13401
13402         # overflow the EA
13403         local longname=filename_avg_len_is_thirty_two_
13404         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
13405                 error "failed to hardlink many files"
13406         links=$($LFS fid2path $DIR $FID | wc -l)
13407         echo -n "${links}/1000 links in link EA"
13408         [[ ${links} -gt 60 ]] ||
13409                 error "expected at least 60 links in link EA"
13410         unlinkmany $remote_dir/foo2/$longname 1000 ||
13411         error "failed to unlink many hardlinks"
13412 }
13413 run_test 161b "link ea sanity under remote directory"
13414
13415 test_161c() {
13416         remote_mds_nodsh && skip "remote MDS with nodsh"
13417         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13418         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
13419                 skip "Need MDS version at least 2.1.5"
13420
13421         # define CLF_RENAME_LAST 0x0001
13422         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
13423         changelog_register || error "changelog_register failed"
13424
13425         rm -rf $DIR/$tdir
13426         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
13427         touch $DIR/$tdir/foo_161c
13428         touch $DIR/$tdir/bar_161c
13429         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13430         changelog_dump | grep RENME | tail -n 5
13431         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13432         changelog_clear 0 || error "changelog_clear failed"
13433         if [ x$flags != "x0x1" ]; then
13434                 error "flag $flags is not 0x1"
13435         fi
13436
13437         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
13438         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
13439         touch $DIR/$tdir/foo_161c
13440         touch $DIR/$tdir/bar_161c
13441         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13442         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13443         changelog_dump | grep RENME | tail -n 5
13444         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13445         changelog_clear 0 || error "changelog_clear failed"
13446         if [ x$flags != "x0x0" ]; then
13447                 error "flag $flags is not 0x0"
13448         fi
13449         echo "rename overwrite a target having nlink > 1," \
13450                 "changelog record has flags of $flags"
13451
13452         # rename doesn't overwrite a target (changelog flag 0x0)
13453         touch $DIR/$tdir/foo_161c
13454         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
13455         changelog_dump | grep RENME | tail -n 5
13456         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
13457         changelog_clear 0 || error "changelog_clear failed"
13458         if [ x$flags != "x0x0" ]; then
13459                 error "flag $flags is not 0x0"
13460         fi
13461         echo "rename doesn't overwrite a target," \
13462                 "changelog record has flags of $flags"
13463
13464         # define CLF_UNLINK_LAST 0x0001
13465         # unlink a file having nlink = 1 (changelog flag 0x1)
13466         rm -f $DIR/$tdir/foo2_161c
13467         changelog_dump | grep UNLNK | tail -n 5
13468         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13469         changelog_clear 0 || error "changelog_clear failed"
13470         if [ x$flags != "x0x1" ]; then
13471                 error "flag $flags is not 0x1"
13472         fi
13473         echo "unlink a file having nlink = 1," \
13474                 "changelog record has flags of $flags"
13475
13476         # unlink a file having nlink > 1 (changelog flag 0x0)
13477         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13478         rm -f $DIR/$tdir/foobar_161c
13479         changelog_dump | grep UNLNK | tail -n 5
13480         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13481         changelog_clear 0 || error "changelog_clear failed"
13482         if [ x$flags != "x0x0" ]; then
13483                 error "flag $flags is not 0x0"
13484         fi
13485         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
13486 }
13487 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
13488
13489 test_161d() {
13490         remote_mds_nodsh && skip "remote MDS with nodsh"
13491         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
13492
13493         local pid
13494         local fid
13495
13496         changelog_register || error "changelog_register failed"
13497
13498         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
13499         # interfer with $MOUNT/.lustre/fid/ access
13500         mkdir $DIR/$tdir
13501         [[ $? -eq 0 ]] || error "mkdir failed"
13502
13503         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
13504         $LCTL set_param fail_loc=0x8000140c
13505         # 5s pause
13506         $LCTL set_param fail_val=5
13507
13508         # create file
13509         echo foofoo > $DIR/$tdir/$tfile &
13510         pid=$!
13511
13512         # wait for create to be delayed
13513         sleep 2
13514
13515         ps -p $pid
13516         [[ $? -eq 0 ]] || error "create should be blocked"
13517
13518         local tempfile=$(mktemp)
13519         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
13520         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
13521         # some delay may occur during ChangeLog publishing and file read just
13522         # above, that could allow file write to happen finally
13523         [[ -s $tempfile ]] && echo "file should be empty"
13524
13525         $LCTL set_param fail_loc=0
13526
13527         wait $pid
13528         [[ $? -eq 0 ]] || error "create failed"
13529 }
13530 run_test 161d "create with concurrent .lustre/fid access"
13531
13532 check_path() {
13533         local expected="$1"
13534         shift
13535         local fid="$2"
13536
13537         local path
13538         path=$($LFS fid2path "$@")
13539         local rc=$?
13540
13541         if [ $rc -ne 0 ]; then
13542                 error "path looked up of '$expected' failed: rc=$rc"
13543         elif [ "$path" != "$expected" ]; then
13544                 error "path looked up '$path' instead of '$expected'"
13545         else
13546                 echo "FID '$fid' resolves to path '$path' as expected"
13547         fi
13548 }
13549
13550 test_162a() { # was test_162
13551         test_mkdir -p -c1 $DIR/$tdir/d2
13552         touch $DIR/$tdir/d2/$tfile
13553         touch $DIR/$tdir/d2/x1
13554         touch $DIR/$tdir/d2/x2
13555         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
13556         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
13557         # regular file
13558         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
13559         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
13560
13561         # softlink
13562         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
13563         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
13564         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
13565
13566         # softlink to wrong file
13567         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
13568         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
13569         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
13570
13571         # hardlink
13572         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
13573         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
13574         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
13575         # fid2path dir/fsname should both work
13576         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
13577         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
13578
13579         # hardlink count: check that there are 2 links
13580         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
13581         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
13582
13583         # hardlink indexing: remove the first link
13584         rm $DIR/$tdir/d2/p/q/r/hlink
13585         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
13586 }
13587 run_test 162a "path lookup sanity"
13588
13589 test_162b() {
13590         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13591         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13592
13593         mkdir $DIR/$tdir
13594         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
13595                                 error "create striped dir failed"
13596
13597         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
13598                                         tail -n 1 | awk '{print $2}')
13599         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
13600
13601         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
13602         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
13603
13604         # regular file
13605         for ((i=0;i<5;i++)); do
13606                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
13607                         error "get fid for f$i failed"
13608                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
13609
13610                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
13611                         error "get fid for d$i failed"
13612                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
13613         done
13614
13615         return 0
13616 }
13617 run_test 162b "striped directory path lookup sanity"
13618
13619 # LU-4239: Verify fid2path works with paths 100 or more directories deep
13620 test_162c() {
13621         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
13622                 skip "Need MDS version at least 2.7.51"
13623
13624         local lpath=$tdir.local
13625         local rpath=$tdir.remote
13626
13627         test_mkdir $DIR/$lpath
13628         test_mkdir $DIR/$rpath
13629
13630         for ((i = 0; i <= 101; i++)); do
13631                 lpath="$lpath/$i"
13632                 mkdir $DIR/$lpath
13633                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
13634                         error "get fid for local directory $DIR/$lpath failed"
13635                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
13636
13637                 rpath="$rpath/$i"
13638                 test_mkdir $DIR/$rpath
13639                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
13640                         error "get fid for remote directory $DIR/$rpath failed"
13641                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
13642         done
13643
13644         return 0
13645 }
13646 run_test 162c "fid2path works with paths 100 or more directories deep"
13647
13648 test_169() {
13649         # do directio so as not to populate the page cache
13650         log "creating a 10 Mb file"
13651         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13652         log "starting reads"
13653         dd if=$DIR/$tfile of=/dev/null bs=4096 &
13654         log "truncating the file"
13655         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13656         log "killing dd"
13657         kill %+ || true # reads might have finished
13658         echo "wait until dd is finished"
13659         wait
13660         log "removing the temporary file"
13661         rm -rf $DIR/$tfile || error "tmp file removal failed"
13662 }
13663 run_test 169 "parallel read and truncate should not deadlock"
13664
13665 test_170() {
13666         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13667
13668         $LCTL clear     # bug 18514
13669         $LCTL debug_daemon start $TMP/${tfile}_log_good
13670         touch $DIR/$tfile
13671         $LCTL debug_daemon stop
13672         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
13673                 error "sed failed to read log_good"
13674
13675         $LCTL debug_daemon start $TMP/${tfile}_log_good
13676         rm -rf $DIR/$tfile
13677         $LCTL debug_daemon stop
13678
13679         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
13680                error "lctl df log_bad failed"
13681
13682         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13683         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13684
13685         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
13686         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
13687
13688         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
13689                 error "bad_line good_line1 good_line2 are empty"
13690
13691         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13692         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
13693         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13694
13695         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
13696         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13697         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13698
13699         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
13700                 error "bad_line_new good_line_new are empty"
13701
13702         local expected_good=$((good_line1 + good_line2*2))
13703
13704         rm -f $TMP/${tfile}*
13705         # LU-231, short malformed line may not be counted into bad lines
13706         if [ $bad_line -ne $bad_line_new ] &&
13707                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
13708                 error "expected $bad_line bad lines, but got $bad_line_new"
13709                 return 1
13710         fi
13711
13712         if [ $expected_good -ne $good_line_new ]; then
13713                 error "expected $expected_good good lines, but got $good_line_new"
13714                 return 2
13715         fi
13716         true
13717 }
13718 run_test 170 "test lctl df to handle corrupted log ====================="
13719
13720 test_171() { # bug20592
13721         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13722
13723         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
13724         $LCTL set_param fail_loc=0x50e
13725         $LCTL set_param fail_val=3000
13726         multiop_bg_pause $DIR/$tfile O_s || true
13727         local MULTIPID=$!
13728         kill -USR1 $MULTIPID
13729         # cause log dump
13730         sleep 3
13731         wait $MULTIPID
13732         if dmesg | grep "recursive fault"; then
13733                 error "caught a recursive fault"
13734         fi
13735         $LCTL set_param fail_loc=0
13736         true
13737 }
13738 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
13739
13740 # it would be good to share it with obdfilter-survey/iokit-libecho code
13741 setup_obdecho_osc () {
13742         local rc=0
13743         local ost_nid=$1
13744         local obdfilter_name=$2
13745         echo "Creating new osc for $obdfilter_name on $ost_nid"
13746         # make sure we can find loopback nid
13747         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
13748
13749         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
13750                            ${obdfilter_name}_osc_UUID || rc=2; }
13751         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
13752                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
13753         return $rc
13754 }
13755
13756 cleanup_obdecho_osc () {
13757         local obdfilter_name=$1
13758         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
13759         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
13760         return 0
13761 }
13762
13763 obdecho_test() {
13764         local OBD=$1
13765         local node=$2
13766         local pages=${3:-64}
13767         local rc=0
13768         local id
13769
13770         local count=10
13771         local obd_size=$(get_obd_size $node $OBD)
13772         local page_size=$(get_page_size $node)
13773         if [[ -n "$obd_size" ]]; then
13774                 local new_count=$((obd_size / (pages * page_size / 1024)))
13775                 [[ $new_count -ge $count ]] || count=$new_count
13776         fi
13777
13778         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
13779         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
13780                            rc=2; }
13781         if [ $rc -eq 0 ]; then
13782             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
13783             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
13784         fi
13785         echo "New object id is $id"
13786         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
13787                            rc=4; }
13788         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
13789                            "test_brw $count w v $pages $id" || rc=4; }
13790         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
13791                            rc=4; }
13792         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
13793                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
13794         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
13795                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
13796         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
13797         return $rc
13798 }
13799
13800 test_180a() {
13801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13802
13803         if ! module_loaded obdecho; then
13804                 load_module obdecho/obdecho &&
13805                         stack_trap "rmmod obdecho" EXIT ||
13806                         error "unable to load obdecho on client"
13807         fi
13808
13809         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
13810         local host=$($LCTL get_param -n osc.$osc.import |
13811                      awk '/current_connection:/ { print $2 }' )
13812         local target=$($LCTL get_param -n osc.$osc.import |
13813                        awk '/target:/ { print $2 }' )
13814         target=${target%_UUID}
13815
13816         if [ -n "$target" ]; then
13817                 setup_obdecho_osc $host $target &&
13818                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
13819                         { error "obdecho setup failed with $?"; return; }
13820
13821                 obdecho_test ${target}_osc client ||
13822                         error "obdecho_test failed on ${target}_osc"
13823         else
13824                 $LCTL get_param osc.$osc.import
13825                 error "there is no osc.$osc.import target"
13826         fi
13827 }
13828 run_test 180a "test obdecho on osc"
13829
13830 test_180b() {
13831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13832         remote_ost_nodsh && skip "remote OST with nodsh"
13833
13834         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13835                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13836                 error "failed to load module obdecho"
13837
13838         local target=$(do_facet ost1 $LCTL dl |
13839                        awk '/obdfilter/ { print $4; exit; }')
13840
13841         if [ -n "$target" ]; then
13842                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
13843         else
13844                 do_facet ost1 $LCTL dl
13845                 error "there is no obdfilter target on ost1"
13846         fi
13847 }
13848 run_test 180b "test obdecho directly on obdfilter"
13849
13850 test_180c() { # LU-2598
13851         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13852         remote_ost_nodsh && skip "remote OST with nodsh"
13853         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
13854                 skip "Need MDS version at least 2.4.0"
13855
13856         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13857                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13858                 error "failed to load module obdecho"
13859
13860         local target=$(do_facet ost1 $LCTL dl |
13861                        awk '/obdfilter/ { print $4; exit; }')
13862
13863         if [ -n "$target" ]; then
13864                 local pages=16384 # 64MB bulk I/O RPC size
13865
13866                 obdecho_test "$target" ost1 "$pages" ||
13867                         error "obdecho_test with pages=$pages failed with $?"
13868         else
13869                 do_facet ost1 $LCTL dl
13870                 error "there is no obdfilter target on ost1"
13871         fi
13872 }
13873 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
13874
13875 test_181() { # bug 22177
13876         test_mkdir $DIR/$tdir
13877         # create enough files to index the directory
13878         createmany -o $DIR/$tdir/foobar 4000
13879         # print attributes for debug purpose
13880         lsattr -d .
13881         # open dir
13882         multiop_bg_pause $DIR/$tdir D_Sc || return 1
13883         MULTIPID=$!
13884         # remove the files & current working dir
13885         unlinkmany $DIR/$tdir/foobar 4000
13886         rmdir $DIR/$tdir
13887         kill -USR1 $MULTIPID
13888         wait $MULTIPID
13889         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
13890         return 0
13891 }
13892 run_test 181 "Test open-unlinked dir ========================"
13893
13894 test_182() {
13895         local fcount=1000
13896         local tcount=10
13897
13898         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13899
13900         $LCTL set_param mdc.*.rpc_stats=clear
13901
13902         for (( i = 0; i < $tcount; i++ )) ; do
13903                 mkdir $DIR/$tdir/$i
13904         done
13905
13906         for (( i = 0; i < $tcount; i++ )) ; do
13907                 createmany -o $DIR/$tdir/$i/f- $fcount &
13908         done
13909         wait
13910
13911         for (( i = 0; i < $tcount; i++ )) ; do
13912                 unlinkmany $DIR/$tdir/$i/f- $fcount &
13913         done
13914         wait
13915
13916         $LCTL get_param mdc.*.rpc_stats
13917
13918         rm -rf $DIR/$tdir
13919 }
13920 run_test 182 "Test parallel modify metadata operations ================"
13921
13922 test_183() { # LU-2275
13923         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13924         remote_mds_nodsh && skip "remote MDS with nodsh"
13925         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
13926                 skip "Need MDS version at least 2.3.56"
13927
13928         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13929         echo aaa > $DIR/$tdir/$tfile
13930
13931 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
13932         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
13933
13934         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
13935         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
13936
13937         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13938
13939         # Flush negative dentry cache
13940         touch $DIR/$tdir/$tfile
13941
13942         # We are not checking for any leaked references here, they'll
13943         # become evident next time we do cleanup with module unload.
13944         rm -rf $DIR/$tdir
13945 }
13946 run_test 183 "No crash or request leak in case of strange dispositions ========"
13947
13948 # test suite 184 is for LU-2016, LU-2017
13949 test_184a() {
13950         check_swap_layouts_support
13951
13952         dir0=$DIR/$tdir/$testnum
13953         test_mkdir -p -c1 $dir0
13954         ref1=/etc/passwd
13955         ref2=/etc/group
13956         file1=$dir0/f1
13957         file2=$dir0/f2
13958         $LFS setstripe -c1 $file1
13959         cp $ref1 $file1
13960         $LFS setstripe -c2 $file2
13961         cp $ref2 $file2
13962         gen1=$($LFS getstripe -g $file1)
13963         gen2=$($LFS getstripe -g $file2)
13964
13965         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
13966         gen=$($LFS getstripe -g $file1)
13967         [[ $gen1 != $gen ]] ||
13968                 "Layout generation on $file1 does not change"
13969         gen=$($LFS getstripe -g $file2)
13970         [[ $gen2 != $gen ]] ||
13971                 "Layout generation on $file2 does not change"
13972
13973         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
13974         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13975
13976         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
13977 }
13978 run_test 184a "Basic layout swap"
13979
13980 test_184b() {
13981         check_swap_layouts_support
13982
13983         dir0=$DIR/$tdir/$testnum
13984         mkdir -p $dir0 || error "creating dir $dir0"
13985         file1=$dir0/f1
13986         file2=$dir0/f2
13987         file3=$dir0/f3
13988         dir1=$dir0/d1
13989         dir2=$dir0/d2
13990         mkdir $dir1 $dir2
13991         $LFS setstripe -c1 $file1
13992         $LFS setstripe -c2 $file2
13993         $LFS setstripe -c1 $file3
13994         chown $RUNAS_ID $file3
13995         gen1=$($LFS getstripe -g $file1)
13996         gen2=$($LFS getstripe -g $file2)
13997
13998         $LFS swap_layouts $dir1 $dir2 &&
13999                 error "swap of directories layouts should fail"
14000         $LFS swap_layouts $dir1 $file1 &&
14001                 error "swap of directory and file layouts should fail"
14002         $RUNAS $LFS swap_layouts $file1 $file2 &&
14003                 error "swap of file we cannot write should fail"
14004         $LFS swap_layouts $file1 $file3 &&
14005                 error "swap of file with different owner should fail"
14006         /bin/true # to clear error code
14007 }
14008 run_test 184b "Forbidden layout swap (will generate errors)"
14009
14010 test_184c() {
14011         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
14012         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
14013         check_swap_layouts_support
14014
14015         local dir0=$DIR/$tdir/$testnum
14016         mkdir -p $dir0 || error "creating dir $dir0"
14017
14018         local ref1=$dir0/ref1
14019         local ref2=$dir0/ref2
14020         local file1=$dir0/file1
14021         local file2=$dir0/file2
14022         # create a file large enough for the concurrent test
14023         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
14024         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
14025         echo "ref file size: ref1($(stat -c %s $ref1))," \
14026              "ref2($(stat -c %s $ref2))"
14027
14028         cp $ref2 $file2
14029         dd if=$ref1 of=$file1 bs=16k &
14030         local DD_PID=$!
14031
14032         # Make sure dd starts to copy file
14033         while [ ! -f $file1 ]; do sleep 0.1; done
14034
14035         $LFS swap_layouts $file1 $file2
14036         local rc=$?
14037         wait $DD_PID
14038         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
14039         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
14040
14041         # how many bytes copied before swapping layout
14042         local copied=$(stat -c %s $file2)
14043         local remaining=$(stat -c %s $ref1)
14044         remaining=$((remaining - copied))
14045         echo "Copied $copied bytes before swapping layout..."
14046
14047         cmp -n $copied $file1 $ref2 | grep differ &&
14048                 error "Content mismatch [0, $copied) of ref2 and file1"
14049         cmp -n $copied $file2 $ref1 ||
14050                 error "Content mismatch [0, $copied) of ref1 and file2"
14051         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
14052                 error "Content mismatch [$copied, EOF) of ref1 and file1"
14053
14054         # clean up
14055         rm -f $ref1 $ref2 $file1 $file2
14056 }
14057 run_test 184c "Concurrent write and layout swap"
14058
14059 test_184d() {
14060         check_swap_layouts_support
14061         [ -z "$(which getfattr 2>/dev/null)" ] &&
14062                 skip_env "no getfattr command"
14063
14064         local file1=$DIR/$tdir/$tfile-1
14065         local file2=$DIR/$tdir/$tfile-2
14066         local file3=$DIR/$tdir/$tfile-3
14067         local lovea1
14068         local lovea2
14069
14070         mkdir -p $DIR/$tdir
14071         touch $file1 || error "create $file1 failed"
14072         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14073                 error "create $file2 failed"
14074         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14075                 error "create $file3 failed"
14076         lovea1=$(get_layout_param $file1)
14077
14078         $LFS swap_layouts $file2 $file3 ||
14079                 error "swap $file2 $file3 layouts failed"
14080         $LFS swap_layouts $file1 $file2 ||
14081                 error "swap $file1 $file2 layouts failed"
14082
14083         lovea2=$(get_layout_param $file2)
14084         echo "$lovea1"
14085         echo "$lovea2"
14086         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
14087
14088         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14089         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
14090 }
14091 run_test 184d "allow stripeless layouts swap"
14092
14093 test_184e() {
14094         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
14095                 skip "Need MDS version at least 2.6.94"
14096         check_swap_layouts_support
14097         [ -z "$(which getfattr 2>/dev/null)" ] &&
14098                 skip_env "no getfattr command"
14099
14100         local file1=$DIR/$tdir/$tfile-1
14101         local file2=$DIR/$tdir/$tfile-2
14102         local file3=$DIR/$tdir/$tfile-3
14103         local lovea
14104
14105         mkdir -p $DIR/$tdir
14106         touch $file1 || error "create $file1 failed"
14107         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14108                 error "create $file2 failed"
14109         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14110                 error "create $file3 failed"
14111
14112         $LFS swap_layouts $file1 $file2 ||
14113                 error "swap $file1 $file2 layouts failed"
14114
14115         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14116         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
14117
14118         echo 123 > $file1 || error "Should be able to write into $file1"
14119
14120         $LFS swap_layouts $file1 $file3 ||
14121                 error "swap $file1 $file3 layouts failed"
14122
14123         echo 123 > $file1 || error "Should be able to write into $file1"
14124
14125         rm -rf $file1 $file2 $file3
14126 }
14127 run_test 184e "Recreate layout after stripeless layout swaps"
14128
14129 test_184f() {
14130         # Create a file with name longer than sizeof(struct stat) ==
14131         # 144 to see if we can get chars from the file name to appear
14132         # in the returned striping. Note that 'f' == 0x66.
14133         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
14134
14135         mkdir -p $DIR/$tdir
14136         mcreate $DIR/$tdir/$file
14137         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
14138                 error "IOC_MDC_GETFILEINFO returned garbage striping"
14139         fi
14140 }
14141 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
14142
14143 test_185() { # LU-2441
14144         # LU-3553 - no volatile file support in old servers
14145         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
14146                 skip "Need MDS version at least 2.3.60"
14147
14148         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14149         touch $DIR/$tdir/spoo
14150         local mtime1=$(stat -c "%Y" $DIR/$tdir)
14151         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
14152                 error "cannot create/write a volatile file"
14153         [ "$FILESET" == "" ] &&
14154         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
14155                 error "FID is still valid after close"
14156
14157         multiop_bg_pause $DIR/$tdir vVw4096_c
14158         local multi_pid=$!
14159
14160         local OLD_IFS=$IFS
14161         IFS=":"
14162         local fidv=($fid)
14163         IFS=$OLD_IFS
14164         # assume that the next FID for this client is sequential, since stdout
14165         # is unfortunately eaten by multiop_bg_pause
14166         local n=$((${fidv[1]} + 1))
14167         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
14168         if [ "$FILESET" == "" ]; then
14169                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
14170                         error "FID is missing before close"
14171         fi
14172         kill -USR1 $multi_pid
14173         # 1 second delay, so if mtime change we will see it
14174         sleep 1
14175         local mtime2=$(stat -c "%Y" $DIR/$tdir)
14176         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
14177 }
14178 run_test 185 "Volatile file support"
14179
14180 test_187a() {
14181         remote_mds_nodsh && skip "remote MDS with nodsh"
14182         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14183                 skip "Need MDS version at least 2.3.0"
14184
14185         local dir0=$DIR/$tdir/$testnum
14186         mkdir -p $dir0 || error "creating dir $dir0"
14187
14188         local file=$dir0/file1
14189         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
14190         local dv1=$($LFS data_version $file)
14191         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
14192         local dv2=$($LFS data_version $file)
14193         [[ $dv1 != $dv2 ]] ||
14194                 error "data version did not change on write $dv1 == $dv2"
14195
14196         # clean up
14197         rm -f $file1
14198 }
14199 run_test 187a "Test data version change"
14200
14201 test_187b() {
14202         remote_mds_nodsh && skip "remote MDS with nodsh"
14203         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14204                 skip "Need MDS version at least 2.3.0"
14205
14206         local dir0=$DIR/$tdir/$testnum
14207         mkdir -p $dir0 || error "creating dir $dir0"
14208
14209         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
14210         [[ ${DV[0]} != ${DV[1]} ]] ||
14211                 error "data version did not change on write"\
14212                       " ${DV[0]} == ${DV[1]}"
14213
14214         # clean up
14215         rm -f $file1
14216 }
14217 run_test 187b "Test data version change on volatile file"
14218
14219 test_200() {
14220         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14221         remote_mgs_nodsh && skip "remote MGS with nodsh"
14222         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14223
14224         local POOL=${POOL:-cea1}
14225         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
14226         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
14227         # Pool OST targets
14228         local first_ost=0
14229         local last_ost=$(($OSTCOUNT - 1))
14230         local ost_step=2
14231         local ost_list=$(seq $first_ost $ost_step $last_ost)
14232         local ost_range="$first_ost $last_ost $ost_step"
14233         local test_path=$POOL_ROOT/$POOL_DIR_NAME
14234         local file_dir=$POOL_ROOT/file_tst
14235         local subdir=$test_path/subdir
14236         local rc=0
14237
14238         if ! combined_mgs_mds ; then
14239                 mount_mgs_client
14240         fi
14241
14242         while : ; do
14243                 # former test_200a test_200b
14244                 pool_add $POOL                          || { rc=$? ; break; }
14245                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
14246                 # former test_200c test_200d
14247                 mkdir -p $test_path
14248                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
14249                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
14250                 mkdir -p $subdir
14251                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
14252                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
14253                                                         || { rc=$? ; break; }
14254                 # former test_200e test_200f
14255                 local files=$((OSTCOUNT*3))
14256                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
14257                                                         || { rc=$? ; break; }
14258                 pool_create_files $POOL $file_dir $files "$ost_list" \
14259                                                         || { rc=$? ; break; }
14260                 # former test_200g test_200h
14261                 pool_lfs_df $POOL                       || { rc=$? ; break; }
14262                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
14263
14264                 # former test_201a test_201b test_201c
14265                 pool_remove_first_target $POOL          || { rc=$? ; break; }
14266
14267                 local f=$test_path/$tfile
14268                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
14269                 pool_remove $POOL $f                    || { rc=$? ; break; }
14270                 break
14271         done
14272
14273         destroy_test_pools
14274
14275         if ! combined_mgs_mds ; then
14276                 umount_mgs_client
14277         fi
14278         return $rc
14279 }
14280 run_test 200 "OST pools"
14281
14282 # usage: default_attr <count | size | offset>
14283 default_attr() {
14284         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
14285 }
14286
14287 # usage: check_default_stripe_attr
14288 check_default_stripe_attr() {
14289         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
14290         case $1 in
14291         --stripe-count|-c)
14292                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
14293         --stripe-size|-S)
14294                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
14295         --stripe-index|-i)
14296                 EXPECTED=-1;;
14297         *)
14298                 error "unknown getstripe attr '$1'"
14299         esac
14300
14301         [ $ACTUAL == $EXPECTED ] ||
14302                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
14303 }
14304
14305 test_204a() {
14306         test_mkdir $DIR/$tdir
14307         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
14308
14309         check_default_stripe_attr --stripe-count
14310         check_default_stripe_attr --stripe-size
14311         check_default_stripe_attr --stripe-index
14312 }
14313 run_test 204a "Print default stripe attributes"
14314
14315 test_204b() {
14316         test_mkdir $DIR/$tdir
14317         $LFS setstripe --stripe-count 1 $DIR/$tdir
14318
14319         check_default_stripe_attr --stripe-size
14320         check_default_stripe_attr --stripe-index
14321 }
14322 run_test 204b "Print default stripe size and offset"
14323
14324 test_204c() {
14325         test_mkdir $DIR/$tdir
14326         $LFS setstripe --stripe-size 65536 $DIR/$tdir
14327
14328         check_default_stripe_attr --stripe-count
14329         check_default_stripe_attr --stripe-index
14330 }
14331 run_test 204c "Print default stripe count and offset"
14332
14333 test_204d() {
14334         test_mkdir $DIR/$tdir
14335         $LFS setstripe --stripe-index 0 $DIR/$tdir
14336
14337         check_default_stripe_attr --stripe-count
14338         check_default_stripe_attr --stripe-size
14339 }
14340 run_test 204d "Print default stripe count and size"
14341
14342 test_204e() {
14343         test_mkdir $DIR/$tdir
14344         $LFS setstripe -d $DIR/$tdir
14345
14346         check_default_stripe_attr --stripe-count --raw
14347         check_default_stripe_attr --stripe-size --raw
14348         check_default_stripe_attr --stripe-index --raw
14349 }
14350 run_test 204e "Print raw stripe attributes"
14351
14352 test_204f() {
14353         test_mkdir $DIR/$tdir
14354         $LFS setstripe --stripe-count 1 $DIR/$tdir
14355
14356         check_default_stripe_attr --stripe-size --raw
14357         check_default_stripe_attr --stripe-index --raw
14358 }
14359 run_test 204f "Print raw stripe size and offset"
14360
14361 test_204g() {
14362         test_mkdir $DIR/$tdir
14363         $LFS setstripe --stripe-size 65536 $DIR/$tdir
14364
14365         check_default_stripe_attr --stripe-count --raw
14366         check_default_stripe_attr --stripe-index --raw
14367 }
14368 run_test 204g "Print raw stripe count and offset"
14369
14370 test_204h() {
14371         test_mkdir $DIR/$tdir
14372         $LFS setstripe --stripe-index 0 $DIR/$tdir
14373
14374         check_default_stripe_attr --stripe-count --raw
14375         check_default_stripe_attr --stripe-size --raw
14376 }
14377 run_test 204h "Print raw stripe count and size"
14378
14379 # Figure out which job scheduler is being used, if any,
14380 # or use a fake one
14381 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
14382         JOBENV=SLURM_JOB_ID
14383 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
14384         JOBENV=LSB_JOBID
14385 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
14386         JOBENV=PBS_JOBID
14387 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
14388         JOBENV=LOADL_STEP_ID
14389 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
14390         JOBENV=JOB_ID
14391 else
14392         $LCTL list_param jobid_name > /dev/null 2>&1
14393         if [ $? -eq 0 ]; then
14394                 JOBENV=nodelocal
14395         else
14396                 JOBENV=FAKE_JOBID
14397         fi
14398 fi
14399 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
14400
14401 verify_jobstats() {
14402         local cmd=($1)
14403         shift
14404         local facets="$@"
14405
14406 # we don't really need to clear the stats for this test to work, since each
14407 # command has a unique jobid, but it makes debugging easier if needed.
14408 #       for facet in $facets; do
14409 #               local dev=$(convert_facet2label $facet)
14410 #               # clear old jobstats
14411 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
14412 #       done
14413
14414         # use a new JobID for each test, or we might see an old one
14415         [ "$JOBENV" = "FAKE_JOBID" ] &&
14416                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
14417
14418         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
14419
14420         [ "$JOBENV" = "nodelocal" ] && {
14421                 FAKE_JOBID=id.$testnum.%e.$RANDOM
14422                 $LCTL set_param jobid_name=$FAKE_JOBID
14423                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
14424         }
14425
14426         log "Test: ${cmd[*]}"
14427         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
14428
14429         if [ $JOBENV = "FAKE_JOBID" ]; then
14430                 FAKE_JOBID=$JOBVAL ${cmd[*]}
14431         else
14432                 ${cmd[*]}
14433         fi
14434
14435         # all files are created on OST0000
14436         for facet in $facets; do
14437                 local stats="*.$(convert_facet2label $facet).job_stats"
14438
14439                 # strip out libtool wrappers for in-tree executables
14440                 if [ $(do_facet $facet lctl get_param $stats |
14441                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
14442                         do_facet $facet lctl get_param $stats
14443                         error "No jobstats for $JOBVAL found on $facet::$stats"
14444                 fi
14445         done
14446 }
14447
14448 jobstats_set() {
14449         local new_jobenv=$1
14450
14451         set_persistent_param_and_check client "jobid_var" \
14452                 "$FSNAME.sys.jobid_var" $new_jobenv
14453 }
14454
14455 test_205() { # Job stats
14456         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14457         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
14458                 skip "Need MDS version with at least 2.7.1"
14459         remote_mgs_nodsh && skip "remote MGS with nodsh"
14460         remote_mds_nodsh && skip "remote MDS with nodsh"
14461         remote_ost_nodsh && skip "remote OST with nodsh"
14462         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
14463                 skip "Server doesn't support jobstats"
14464         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
14465
14466         local old_jobenv=$($LCTL get_param -n jobid_var)
14467         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
14468
14469         if [[ $PERM_CMD == *"set_param -P"* ]]; then
14470                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
14471         else
14472                 stack_trap "do_facet mgs $PERM_CMD \
14473                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
14474         fi
14475         changelog_register
14476
14477         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
14478                                 mdt.*.job_cleanup_interval | head -n 1)
14479         local new_interval=5
14480         do_facet $SINGLEMDS \
14481                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
14482         stack_trap "do_facet $SINGLEMDS \
14483                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
14484         local start=$SECONDS
14485
14486         local cmd
14487         # mkdir
14488         cmd="mkdir $DIR/$tdir"
14489         verify_jobstats "$cmd" "$SINGLEMDS"
14490         # rmdir
14491         cmd="rmdir $DIR/$tdir"
14492         verify_jobstats "$cmd" "$SINGLEMDS"
14493         # mkdir on secondary MDT
14494         if [ $MDSCOUNT -gt 1 ]; then
14495                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
14496                 verify_jobstats "$cmd" "mds2"
14497         fi
14498         # mknod
14499         cmd="mknod $DIR/$tfile c 1 3"
14500         verify_jobstats "$cmd" "$SINGLEMDS"
14501         # unlink
14502         cmd="rm -f $DIR/$tfile"
14503         verify_jobstats "$cmd" "$SINGLEMDS"
14504         # create all files on OST0000 so verify_jobstats can find OST stats
14505         # open & close
14506         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
14507         verify_jobstats "$cmd" "$SINGLEMDS"
14508         # setattr
14509         cmd="touch $DIR/$tfile"
14510         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14511         # write
14512         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
14513         verify_jobstats "$cmd" "ost1"
14514         # read
14515         cancel_lru_locks osc
14516         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
14517         verify_jobstats "$cmd" "ost1"
14518         # truncate
14519         cmd="$TRUNCATE $DIR/$tfile 0"
14520         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14521         # rename
14522         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
14523         verify_jobstats "$cmd" "$SINGLEMDS"
14524         # jobstats expiry - sleep until old stats should be expired
14525         local left=$((new_interval + 5 - (SECONDS - start)))
14526         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
14527                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
14528                         "0" $left
14529         cmd="mkdir $DIR/$tdir.expire"
14530         verify_jobstats "$cmd" "$SINGLEMDS"
14531         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
14532             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
14533
14534         # Ensure that jobid are present in changelog (if supported by MDS)
14535         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
14536                 changelog_dump | tail -10
14537                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
14538                 [ $jobids -eq 9 ] ||
14539                         error "Wrong changelog jobid count $jobids != 9"
14540
14541                 # LU-5862
14542                 JOBENV="disable"
14543                 jobstats_set $JOBENV
14544                 touch $DIR/$tfile
14545                 changelog_dump | grep $tfile
14546                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
14547                 [ $jobids -eq 0 ] ||
14548                         error "Unexpected jobids when jobid_var=$JOBENV"
14549         fi
14550
14551         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
14552         JOBENV="JOBCOMPLEX"
14553         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
14554
14555         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
14556 }
14557 run_test 205 "Verify job stats"
14558
14559 # LU-1480, LU-1773 and LU-1657
14560 test_206() {
14561         mkdir -p $DIR/$tdir
14562         $LFS setstripe -c -1 $DIR/$tdir
14563 #define OBD_FAIL_LOV_INIT 0x1403
14564         $LCTL set_param fail_loc=0xa0001403
14565         $LCTL set_param fail_val=1
14566         touch $DIR/$tdir/$tfile || true
14567 }
14568 run_test 206 "fail lov_init_raid0() doesn't lbug"
14569
14570 test_207a() {
14571         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14572         local fsz=`stat -c %s $DIR/$tfile`
14573         cancel_lru_locks mdc
14574
14575         # do not return layout in getattr intent
14576 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
14577         $LCTL set_param fail_loc=0x170
14578         local sz=`stat -c %s $DIR/$tfile`
14579
14580         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
14581
14582         rm -rf $DIR/$tfile
14583 }
14584 run_test 207a "can refresh layout at glimpse"
14585
14586 test_207b() {
14587         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14588         local cksum=`md5sum $DIR/$tfile`
14589         local fsz=`stat -c %s $DIR/$tfile`
14590         cancel_lru_locks mdc
14591         cancel_lru_locks osc
14592
14593         # do not return layout in getattr intent
14594 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
14595         $LCTL set_param fail_loc=0x171
14596
14597         # it will refresh layout after the file is opened but before read issues
14598         echo checksum is "$cksum"
14599         echo "$cksum" |md5sum -c --quiet || error "file differs"
14600
14601         rm -rf $DIR/$tfile
14602 }
14603 run_test 207b "can refresh layout at open"
14604
14605 test_208() {
14606         # FIXME: in this test suite, only RD lease is used. This is okay
14607         # for now as only exclusive open is supported. After generic lease
14608         # is done, this test suite should be revised. - Jinshan
14609
14610         remote_mds_nodsh && skip "remote MDS with nodsh"
14611         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
14612                 skip "Need MDS version at least 2.4.52"
14613
14614         echo "==== test 1: verify get lease work"
14615         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
14616
14617         echo "==== test 2: verify lease can be broken by upcoming open"
14618         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14619         local PID=$!
14620         sleep 1
14621
14622         $MULTIOP $DIR/$tfile oO_RDONLY:c
14623         kill -USR1 $PID && wait $PID || error "break lease error"
14624
14625         echo "==== test 3: verify lease can't be granted if an open already exists"
14626         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
14627         local PID=$!
14628         sleep 1
14629
14630         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
14631         kill -USR1 $PID && wait $PID || error "open file error"
14632
14633         echo "==== test 4: lease can sustain over recovery"
14634         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
14635         PID=$!
14636         sleep 1
14637
14638         fail mds1
14639
14640         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
14641
14642         echo "==== test 5: lease broken can't be regained by replay"
14643         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14644         PID=$!
14645         sleep 1
14646
14647         # open file to break lease and then recovery
14648         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
14649         fail mds1
14650
14651         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
14652
14653         rm -f $DIR/$tfile
14654 }
14655 run_test 208 "Exclusive open"
14656
14657 test_209() {
14658         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
14659                 skip_env "must have disp_stripe"
14660
14661         touch $DIR/$tfile
14662         sync; sleep 5; sync;
14663
14664         echo 3 > /proc/sys/vm/drop_caches
14665         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14666
14667         # open/close 500 times
14668         for i in $(seq 500); do
14669                 cat $DIR/$tfile
14670         done
14671
14672         echo 3 > /proc/sys/vm/drop_caches
14673         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14674
14675         echo "before: $req_before, after: $req_after"
14676         [ $((req_after - req_before)) -ge 300 ] &&
14677                 error "open/close requests are not freed"
14678         return 0
14679 }
14680 run_test 209 "read-only open/close requests should be freed promptly"
14681
14682 test_212() {
14683         size=`date +%s`
14684         size=$((size % 8192 + 1))
14685         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
14686         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
14687         rm -f $DIR/f212 $DIR/f212.xyz
14688 }
14689 run_test 212 "Sendfile test ============================================"
14690
14691 test_213() {
14692         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
14693         cancel_lru_locks osc
14694         lctl set_param fail_loc=0x8000040f
14695         # generate a read lock
14696         cat $DIR/$tfile > /dev/null
14697         # write to the file, it will try to cancel the above read lock.
14698         cat /etc/hosts >> $DIR/$tfile
14699 }
14700 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
14701
14702 test_214() { # for bug 20133
14703         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
14704         for (( i=0; i < 340; i++ )) ; do
14705                 touch $DIR/$tdir/d214c/a$i
14706         done
14707
14708         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
14709         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
14710         ls $DIR/d214c || error "ls $DIR/d214c failed"
14711         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
14712         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
14713 }
14714 run_test 214 "hash-indexed directory test - bug 20133"
14715
14716 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
14717 create_lnet_proc_files() {
14718         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
14719 }
14720
14721 # counterpart of create_lnet_proc_files
14722 remove_lnet_proc_files() {
14723         rm -f $TMP/lnet_$1.sys
14724 }
14725
14726 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14727 # 3rd arg as regexp for body
14728 check_lnet_proc_stats() {
14729         local l=$(cat "$TMP/lnet_$1" |wc -l)
14730         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
14731
14732         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
14733 }
14734
14735 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14736 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
14737 # optional and can be regexp for 2nd line (lnet.routes case)
14738 check_lnet_proc_entry() {
14739         local blp=2          # blp stands for 'position of 1st line of body'
14740         [ -z "$5" ] || blp=3 # lnet.routes case
14741
14742         local l=$(cat "$TMP/lnet_$1" |wc -l)
14743         # subtracting one from $blp because the body can be empty
14744         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
14745
14746         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
14747                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
14748
14749         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
14750                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
14751
14752         # bail out if any unexpected line happened
14753         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
14754         [ "$?" != 0 ] || error "$2 misformatted"
14755 }
14756
14757 test_215() { # for bugs 18102, 21079, 21517
14758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14759
14760         local N='(0|[1-9][0-9]*)'       # non-negative numeric
14761         local P='[1-9][0-9]*'           # positive numeric
14762         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
14763         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
14764         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
14765         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
14766
14767         local L1 # regexp for 1st line
14768         local L2 # regexp for 2nd line (optional)
14769         local BR # regexp for the rest (body)
14770
14771         # lnet.stats should look as 11 space-separated non-negative numerics
14772         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
14773         create_lnet_proc_files "stats"
14774         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
14775         remove_lnet_proc_files "stats"
14776
14777         # lnet.routes should look like this:
14778         # Routing disabled/enabled
14779         # net hops priority state router
14780         # where net is a string like tcp0, hops > 0, priority >= 0,
14781         # state is up/down,
14782         # router is a string like 192.168.1.1@tcp2
14783         L1="^Routing (disabled|enabled)$"
14784         L2="^net +hops +priority +state +router$"
14785         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
14786         create_lnet_proc_files "routes"
14787         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
14788         remove_lnet_proc_files "routes"
14789
14790         # lnet.routers should look like this:
14791         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
14792         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
14793         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
14794         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
14795         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
14796         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
14797         create_lnet_proc_files "routers"
14798         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
14799         remove_lnet_proc_files "routers"
14800
14801         # lnet.peers should look like this:
14802         # nid refs state last max rtr min tx min queue
14803         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
14804         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
14805         # numeric (0 or >0 or <0), queue >= 0.
14806         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
14807         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
14808         create_lnet_proc_files "peers"
14809         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
14810         remove_lnet_proc_files "peers"
14811
14812         # lnet.buffers  should look like this:
14813         # pages count credits min
14814         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
14815         L1="^pages +count +credits +min$"
14816         BR="^ +$N +$N +$I +$I$"
14817         create_lnet_proc_files "buffers"
14818         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
14819         remove_lnet_proc_files "buffers"
14820
14821         # lnet.nis should look like this:
14822         # nid status alive refs peer rtr max tx min
14823         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
14824         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
14825         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
14826         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
14827         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
14828         create_lnet_proc_files "nis"
14829         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
14830         remove_lnet_proc_files "nis"
14831
14832         # can we successfully write to lnet.stats?
14833         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
14834 }
14835 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
14836
14837 test_216() { # bug 20317
14838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14839         remote_ost_nodsh && skip "remote OST with nodsh"
14840
14841         local node
14842         local facets=$(get_facets OST)
14843         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14844
14845         save_lustre_params client "osc.*.contention_seconds" > $p
14846         save_lustre_params $facets \
14847                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
14848         save_lustre_params $facets \
14849                 "ldlm.namespaces.filter-*.contended_locks" >> $p
14850         save_lustre_params $facets \
14851                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
14852         clear_stats osc.*.osc_stats
14853
14854         # agressive lockless i/o settings
14855         do_nodes $(comma_list $(osts_nodes)) \
14856                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
14857                         ldlm.namespaces.filter-*.contended_locks=0 \
14858                         ldlm.namespaces.filter-*.contention_seconds=60"
14859         lctl set_param -n osc.*.contention_seconds=60
14860
14861         $DIRECTIO write $DIR/$tfile 0 10 4096
14862         $CHECKSTAT -s 40960 $DIR/$tfile
14863
14864         # disable lockless i/o
14865         do_nodes $(comma_list $(osts_nodes)) \
14866                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
14867                         ldlm.namespaces.filter-*.contended_locks=32 \
14868                         ldlm.namespaces.filter-*.contention_seconds=0"
14869         lctl set_param -n osc.*.contention_seconds=0
14870         clear_stats osc.*.osc_stats
14871
14872         dd if=/dev/zero of=$DIR/$tfile count=0
14873         $CHECKSTAT -s 0 $DIR/$tfile
14874
14875         restore_lustre_params <$p
14876         rm -f $p
14877         rm $DIR/$tfile
14878 }
14879 run_test 216 "check lockless direct write updates file size and kms correctly"
14880
14881 test_217() { # bug 22430
14882         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14883
14884         local node
14885         local nid
14886
14887         for node in $(nodes_list); do
14888                 nid=$(host_nids_address $node $NETTYPE)
14889                 if [[ $nid = *-* ]] ; then
14890                         echo "lctl ping $(h2nettype $nid)"
14891                         lctl ping $(h2nettype $nid)
14892                 else
14893                         echo "skipping $node (no hyphen detected)"
14894                 fi
14895         done
14896 }
14897 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
14898
14899 test_218() {
14900        # do directio so as not to populate the page cache
14901        log "creating a 10 Mb file"
14902        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14903        log "starting reads"
14904        dd if=$DIR/$tfile of=/dev/null bs=4096 &
14905        log "truncating the file"
14906        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14907        log "killing dd"
14908        kill %+ || true # reads might have finished
14909        echo "wait until dd is finished"
14910        wait
14911        log "removing the temporary file"
14912        rm -rf $DIR/$tfile || error "tmp file removal failed"
14913 }
14914 run_test 218 "parallel read and truncate should not deadlock"
14915
14916 test_219() {
14917         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14918
14919         # write one partial page
14920         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
14921         # set no grant so vvp_io_commit_write will do sync write
14922         $LCTL set_param fail_loc=0x411
14923         # write a full page at the end of file
14924         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
14925
14926         $LCTL set_param fail_loc=0
14927         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
14928         $LCTL set_param fail_loc=0x411
14929         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
14930
14931         # LU-4201
14932         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
14933         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
14934 }
14935 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
14936
14937 test_220() { #LU-325
14938         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14939         remote_ost_nodsh && skip "remote OST with nodsh"
14940         remote_mds_nodsh && skip "remote MDS with nodsh"
14941         remote_mgs_nodsh && skip "remote MGS with nodsh"
14942
14943         local OSTIDX=0
14944
14945         # create on MDT0000 so the last_id and next_id are correct
14946         mkdir $DIR/$tdir
14947         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
14948         OST=${OST%_UUID}
14949
14950         # on the mdt's osc
14951         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
14952         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
14953                         osc.$mdtosc_proc1.prealloc_last_id)
14954         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
14955                         osc.$mdtosc_proc1.prealloc_next_id)
14956
14957         $LFS df -i
14958
14959         if ! combined_mgs_mds ; then
14960                 mount_mgs_client
14961         fi
14962
14963         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
14964         #define OBD_FAIL_OST_ENOINO              0x229
14965         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
14966         create_pool $FSNAME.$TESTNAME || return 1
14967         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
14968
14969         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
14970
14971         MDSOBJS=$((last_id - next_id))
14972         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
14973
14974         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
14975         echo "OST still has $count kbytes free"
14976
14977         echo "create $MDSOBJS files @next_id..."
14978         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
14979
14980         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14981                         osc.$mdtosc_proc1.prealloc_last_id)
14982         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14983                         osc.$mdtosc_proc1.prealloc_next_id)
14984
14985         echo "after creation, last_id=$last_id2, next_id=$next_id2"
14986         $LFS df -i
14987
14988         echo "cleanup..."
14989
14990         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
14991         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
14992
14993         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
14994                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
14995         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14996                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
14997         echo "unlink $MDSOBJS files @$next_id..."
14998         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
14999
15000         if ! combined_mgs_mds ; then
15001                 umount_mgs_client
15002         fi
15003 }
15004 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
15005
15006 test_221() {
15007         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15008
15009         dd if=`which date` of=$MOUNT/date oflag=sync
15010         chmod +x $MOUNT/date
15011
15012         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
15013         $LCTL set_param fail_loc=0x80001401
15014
15015         $MOUNT/date > /dev/null
15016         rm -f $MOUNT/date
15017 }
15018 run_test 221 "make sure fault and truncate race to not cause OOM"
15019
15020 test_222a () {
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_DELAY           0x31a
15030         $LCTL set_param fail_loc=0x31a
15031         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
15032         $LCTL set_param fail_loc=0
15033         rm -r $DIR/$tdir
15034 }
15035 run_test 222a "AGL for ls should not trigger CLIO lock failure"
15036
15037 test_222b () {
15038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15039
15040         rm -rf $DIR/$tdir
15041         test_mkdir $DIR/$tdir
15042         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15043         createmany -o $DIR/$tdir/$tfile 10
15044         cancel_lru_locks mdc
15045         cancel_lru_locks osc
15046         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15047         $LCTL set_param fail_loc=0x31a
15048         rm -r $DIR/$tdir || error "AGL for rmdir failed"
15049         $LCTL set_param fail_loc=0
15050 }
15051 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
15052
15053 test_223 () {
15054         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15055
15056         rm -rf $DIR/$tdir
15057         test_mkdir $DIR/$tdir
15058         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15059         createmany -o $DIR/$tdir/$tfile 10
15060         cancel_lru_locks mdc
15061         cancel_lru_locks osc
15062         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
15063         $LCTL set_param fail_loc=0x31b
15064         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
15065         $LCTL set_param fail_loc=0
15066         rm -r $DIR/$tdir
15067 }
15068 run_test 223 "osc reenqueue if without AGL lock granted ======================="
15069
15070 test_224a() { # LU-1039, MRP-303
15071         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15072
15073         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
15074         $LCTL set_param fail_loc=0x508
15075         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
15076         $LCTL set_param fail_loc=0
15077         df $DIR
15078 }
15079 run_test 224a "Don't panic on bulk IO failure"
15080
15081 test_224b() { # LU-1039, MRP-303
15082         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15083
15084         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
15085         cancel_lru_locks osc
15086         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
15087         $LCTL set_param fail_loc=0x515
15088         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
15089         $LCTL set_param fail_loc=0
15090         df $DIR
15091 }
15092 run_test 224b "Don't panic on bulk IO failure"
15093
15094 test_224c() { # LU-6441
15095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15096         remote_mds_nodsh && skip "remote MDS with nodsh"
15097
15098         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15099         save_writethrough $p
15100         set_cache writethrough on
15101
15102         local pages_per_rpc=$($LCTL get_param \
15103                                 osc.*.max_pages_per_rpc)
15104         local at_max=$($LCTL get_param -n at_max)
15105         local timeout=$($LCTL get_param -n timeout)
15106         local test_at="at_max"
15107         local param_at="$FSNAME.sys.at_max"
15108         local test_timeout="timeout"
15109         local param_timeout="$FSNAME.sys.timeout"
15110
15111         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
15112
15113         set_persistent_param_and_check client "$test_at" "$param_at" 0
15114         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
15115
15116         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
15117         do_facet ost1 "$LCTL set_param fail_loc=0x520"
15118         $LFS setstripe -c 1 -i 0 $DIR/$tfile
15119         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
15120         sync
15121         do_facet ost1 "$LCTL set_param fail_loc=0"
15122
15123         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
15124         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
15125                 $timeout
15126
15127         $LCTL set_param -n $pages_per_rpc
15128         restore_lustre_params < $p
15129         rm -f $p
15130 }
15131 run_test 224c "Don't hang if one of md lost during large bulk RPC"
15132
15133 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
15134 test_225a () {
15135         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15136         if [ -z ${MDSSURVEY} ]; then
15137                 skip_env "mds-survey not found"
15138         fi
15139         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15140                 skip "Need MDS version at least 2.2.51"
15141
15142         local mds=$(facet_host $SINGLEMDS)
15143         local target=$(do_nodes $mds 'lctl dl' |
15144                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15145
15146         local cmd1="file_count=1000 thrhi=4"
15147         local cmd2="dir_count=2 layer=mdd stripe_count=0"
15148         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15149         local cmd="$cmd1 $cmd2 $cmd3"
15150
15151         rm -f ${TMP}/mds_survey*
15152         echo + $cmd
15153         eval $cmd || error "mds-survey with zero-stripe failed"
15154         cat ${TMP}/mds_survey*
15155         rm -f ${TMP}/mds_survey*
15156 }
15157 run_test 225a "Metadata survey sanity with zero-stripe"
15158
15159 test_225b () {
15160         if [ -z ${MDSSURVEY} ]; then
15161                 skip_env "mds-survey not found"
15162         fi
15163         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15164                 skip "Need MDS version at least 2.2.51"
15165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15166         remote_mds_nodsh && skip "remote MDS with nodsh"
15167         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
15168                 skip_env "Need to mount OST to test"
15169         fi
15170
15171         local mds=$(facet_host $SINGLEMDS)
15172         local target=$(do_nodes $mds 'lctl dl' |
15173                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15174
15175         local cmd1="file_count=1000 thrhi=4"
15176         local cmd2="dir_count=2 layer=mdd stripe_count=1"
15177         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15178         local cmd="$cmd1 $cmd2 $cmd3"
15179
15180         rm -f ${TMP}/mds_survey*
15181         echo + $cmd
15182         eval $cmd || error "mds-survey with stripe_count failed"
15183         cat ${TMP}/mds_survey*
15184         rm -f ${TMP}/mds_survey*
15185 }
15186 run_test 225b "Metadata survey sanity with stripe_count = 1"
15187
15188 mcreate_path2fid () {
15189         local mode=$1
15190         local major=$2
15191         local minor=$3
15192         local name=$4
15193         local desc=$5
15194         local path=$DIR/$tdir/$name
15195         local fid
15196         local rc
15197         local fid_path
15198
15199         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
15200                 error "cannot create $desc"
15201
15202         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
15203         rc=$?
15204         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
15205
15206         fid_path=$($LFS fid2path $MOUNT $fid)
15207         rc=$?
15208         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
15209
15210         [ "$path" == "$fid_path" ] ||
15211                 error "fid2path returned $fid_path, expected $path"
15212
15213         echo "pass with $path and $fid"
15214 }
15215
15216 test_226a () {
15217         rm -rf $DIR/$tdir
15218         mkdir -p $DIR/$tdir
15219
15220         mcreate_path2fid 0010666 0 0 fifo "FIFO"
15221         mcreate_path2fid 0020666 1 3 null "character special file (null)"
15222         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
15223         mcreate_path2fid 0040666 0 0 dir "directory"
15224         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
15225         mcreate_path2fid 0100666 0 0 file "regular file"
15226         mcreate_path2fid 0120666 0 0 link "symbolic link"
15227         mcreate_path2fid 0140666 0 0 sock "socket"
15228 }
15229 run_test 226a "call path2fid and fid2path on files of all type"
15230
15231 test_226b () {
15232         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15233
15234         local MDTIDX=1
15235
15236         rm -rf $DIR/$tdir
15237         mkdir -p $DIR/$tdir
15238         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
15239                 error "create remote directory failed"
15240         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
15241         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
15242                                 "character special file (null)"
15243         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
15244                                 "character special file (no device)"
15245         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
15246         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
15247                                 "block special file (loop)"
15248         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
15249         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
15250         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
15251 }
15252 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
15253
15254 # LU-1299 Executing or running ldd on a truncated executable does not
15255 # cause an out-of-memory condition.
15256 test_227() {
15257         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15258         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
15259
15260         dd if=$(which date) of=$MOUNT/date bs=1k count=1
15261         chmod +x $MOUNT/date
15262
15263         $MOUNT/date > /dev/null
15264         ldd $MOUNT/date > /dev/null
15265         rm -f $MOUNT/date
15266 }
15267 run_test 227 "running truncated executable does not cause OOM"
15268
15269 # LU-1512 try to reuse idle OI blocks
15270 test_228a() {
15271         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15272         remote_mds_nodsh && skip "remote MDS with nodsh"
15273         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15274
15275         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15276         local myDIR=$DIR/$tdir
15277
15278         mkdir -p $myDIR
15279         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15280         $LCTL set_param fail_loc=0x80001002
15281         createmany -o $myDIR/t- 10000
15282         $LCTL set_param fail_loc=0
15283         # The guard is current the largest FID holder
15284         touch $myDIR/guard
15285         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15286                     tr -d '[')
15287         local IDX=$(($SEQ % 64))
15288
15289         do_facet $SINGLEMDS sync
15290         # Make sure journal flushed.
15291         sleep 6
15292         local blk1=$(do_facet $SINGLEMDS \
15293                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15294                      grep Blockcount | awk '{print $4}')
15295
15296         # Remove old files, some OI blocks will become idle.
15297         unlinkmany $myDIR/t- 10000
15298         # Create new files, idle OI blocks should be reused.
15299         createmany -o $myDIR/t- 2000
15300         do_facet $SINGLEMDS sync
15301         # Make sure journal flushed.
15302         sleep 6
15303         local blk2=$(do_facet $SINGLEMDS \
15304                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15305                      grep Blockcount | awk '{print $4}')
15306
15307         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15308 }
15309 run_test 228a "try to reuse idle OI blocks"
15310
15311 test_228b() {
15312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15313         remote_mds_nodsh && skip "remote MDS with nodsh"
15314         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15315
15316         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15317         local myDIR=$DIR/$tdir
15318
15319         mkdir -p $myDIR
15320         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15321         $LCTL set_param fail_loc=0x80001002
15322         createmany -o $myDIR/t- 10000
15323         $LCTL set_param fail_loc=0
15324         # The guard is current the largest FID holder
15325         touch $myDIR/guard
15326         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15327                     tr -d '[')
15328         local IDX=$(($SEQ % 64))
15329
15330         do_facet $SINGLEMDS sync
15331         # Make sure journal flushed.
15332         sleep 6
15333         local blk1=$(do_facet $SINGLEMDS \
15334                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15335                      grep Blockcount | awk '{print $4}')
15336
15337         # Remove old files, some OI blocks will become idle.
15338         unlinkmany $myDIR/t- 10000
15339
15340         # stop the MDT
15341         stop $SINGLEMDS || error "Fail to stop MDT."
15342         # remount the MDT
15343         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
15344
15345         df $MOUNT || error "Fail to df."
15346         # Create new files, idle OI blocks should be reused.
15347         createmany -o $myDIR/t- 2000
15348         do_facet $SINGLEMDS sync
15349         # Make sure journal flushed.
15350         sleep 6
15351         local blk2=$(do_facet $SINGLEMDS \
15352                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15353                      grep Blockcount | awk '{print $4}')
15354
15355         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15356 }
15357 run_test 228b "idle OI blocks can be reused after MDT restart"
15358
15359 #LU-1881
15360 test_228c() {
15361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15362         remote_mds_nodsh && skip "remote MDS with nodsh"
15363         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15364
15365         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15366         local myDIR=$DIR/$tdir
15367
15368         mkdir -p $myDIR
15369         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15370         $LCTL set_param fail_loc=0x80001002
15371         # 20000 files can guarantee there are index nodes in the OI file
15372         createmany -o $myDIR/t- 20000
15373         $LCTL set_param fail_loc=0
15374         # The guard is current the largest FID holder
15375         touch $myDIR/guard
15376         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15377                     tr -d '[')
15378         local IDX=$(($SEQ % 64))
15379
15380         do_facet $SINGLEMDS sync
15381         # Make sure journal flushed.
15382         sleep 6
15383         local blk1=$(do_facet $SINGLEMDS \
15384                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15385                      grep Blockcount | awk '{print $4}')
15386
15387         # Remove old files, some OI blocks will become idle.
15388         unlinkmany $myDIR/t- 20000
15389         rm -f $myDIR/guard
15390         # The OI file should become empty now
15391
15392         # Create new files, idle OI blocks should be reused.
15393         createmany -o $myDIR/t- 2000
15394         do_facet $SINGLEMDS sync
15395         # Make sure journal flushed.
15396         sleep 6
15397         local blk2=$(do_facet $SINGLEMDS \
15398                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15399                      grep Blockcount | awk '{print $4}')
15400
15401         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15402 }
15403 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
15404
15405 test_229() { # LU-2482, LU-3448
15406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15407         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
15408         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
15409                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
15410
15411         rm -f $DIR/$tfile
15412
15413         # Create a file with a released layout and stripe count 2.
15414         $MULTIOP $DIR/$tfile H2c ||
15415                 error "failed to create file with released layout"
15416
15417         $LFS getstripe -v $DIR/$tfile
15418
15419         local pattern=$($LFS getstripe -L $DIR/$tfile)
15420         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
15421
15422         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
15423                 error "getstripe"
15424         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
15425         stat $DIR/$tfile || error "failed to stat released file"
15426
15427         chown $RUNAS_ID $DIR/$tfile ||
15428                 error "chown $RUNAS_ID $DIR/$tfile failed"
15429
15430         chgrp $RUNAS_ID $DIR/$tfile ||
15431                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
15432
15433         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
15434         rm $DIR/$tfile || error "failed to remove released file"
15435 }
15436 run_test 229 "getstripe/stat/rm/attr changes work on released files"
15437
15438 test_230a() {
15439         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15440         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15441         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15442                 skip "Need MDS version at least 2.11.52"
15443
15444         local MDTIDX=1
15445
15446         test_mkdir $DIR/$tdir
15447         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
15448         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
15449         [ $mdt_idx -ne 0 ] &&
15450                 error "create local directory on wrong MDT $mdt_idx"
15451
15452         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
15453                         error "create remote directory failed"
15454         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
15455         [ $mdt_idx -ne $MDTIDX ] &&
15456                 error "create remote directory on wrong MDT $mdt_idx"
15457
15458         createmany -o $DIR/$tdir/test_230/t- 10 ||
15459                 error "create files on remote directory failed"
15460         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
15461         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
15462         rm -r $DIR/$tdir || error "unlink remote directory failed"
15463 }
15464 run_test 230a "Create remote directory and files under the remote directory"
15465
15466 test_230b() {
15467         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15468         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15469         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15470                 skip "Need MDS version at least 2.11.52"
15471
15472         local MDTIDX=1
15473         local mdt_index
15474         local i
15475         local file
15476         local pid
15477         local stripe_count
15478         local migrate_dir=$DIR/$tdir/migrate_dir
15479         local other_dir=$DIR/$tdir/other_dir
15480
15481         test_mkdir $DIR/$tdir
15482         test_mkdir -i0 -c1 $migrate_dir
15483         test_mkdir -i0 -c1 $other_dir
15484         for ((i=0; i<10; i++)); do
15485                 mkdir -p $migrate_dir/dir_${i}
15486                 createmany -o $migrate_dir/dir_${i}/f 10 ||
15487                         error "create files under remote dir failed $i"
15488         done
15489
15490         cp /etc/passwd $migrate_dir/$tfile
15491         cp /etc/passwd $other_dir/$tfile
15492         chattr +SAD $migrate_dir
15493         chattr +SAD $migrate_dir/$tfile
15494
15495         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15496         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15497         local old_dir_mode=$(stat -c%f $migrate_dir)
15498         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
15499
15500         mkdir -p $migrate_dir/dir_default_stripe2
15501         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
15502         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
15503
15504         mkdir -p $other_dir
15505         ln $migrate_dir/$tfile $other_dir/luna
15506         ln $migrate_dir/$tfile $migrate_dir/sofia
15507         ln $other_dir/$tfile $migrate_dir/david
15508         ln -s $migrate_dir/$tfile $other_dir/zachary
15509         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
15510         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
15511
15512         $LFS migrate -m $MDTIDX $migrate_dir ||
15513                 error "fails on migrating remote dir to MDT1"
15514
15515         echo "migratate to MDT1, then checking.."
15516         for ((i = 0; i < 10; i++)); do
15517                 for file in $(find $migrate_dir/dir_${i}); do
15518                         mdt_index=$($LFS getstripe -m $file)
15519                         [ $mdt_index == $MDTIDX ] ||
15520                                 error "$file is not on MDT${MDTIDX}"
15521                 done
15522         done
15523
15524         # the multiple link file should still in MDT0
15525         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
15526         [ $mdt_index == 0 ] ||
15527                 error "$file is not on MDT${MDTIDX}"
15528
15529         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15530         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15531                 error " expect $old_dir_flag get $new_dir_flag"
15532
15533         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15534         [ "$old_file_flag" = "$new_file_flag" ] ||
15535                 error " expect $old_file_flag get $new_file_flag"
15536
15537         local new_dir_mode=$(stat -c%f $migrate_dir)
15538         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15539                 error "expect mode $old_dir_mode get $new_dir_mode"
15540
15541         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15542         [ "$old_file_mode" = "$new_file_mode" ] ||
15543                 error "expect mode $old_file_mode get $new_file_mode"
15544
15545         diff /etc/passwd $migrate_dir/$tfile ||
15546                 error "$tfile different after migration"
15547
15548         diff /etc/passwd $other_dir/luna ||
15549                 error "luna different after migration"
15550
15551         diff /etc/passwd $migrate_dir/sofia ||
15552                 error "sofia different after migration"
15553
15554         diff /etc/passwd $migrate_dir/david ||
15555                 error "david different after migration"
15556
15557         diff /etc/passwd $other_dir/zachary ||
15558                 error "zachary different after migration"
15559
15560         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15561                 error "${tfile}_ln different after migration"
15562
15563         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15564                 error "${tfile}_ln_other different after migration"
15565
15566         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
15567         [ $stripe_count = 2 ] ||
15568                 error "dir strpe_count $d != 2 after migration."
15569
15570         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
15571         [ $stripe_count = 2 ] ||
15572                 error "file strpe_count $d != 2 after migration."
15573
15574         #migrate back to MDT0
15575         MDTIDX=0
15576
15577         $LFS migrate -m $MDTIDX $migrate_dir ||
15578                 error "fails on migrating remote dir to MDT0"
15579
15580         echo "migrate back to MDT0, checking.."
15581         for file in $(find $migrate_dir); do
15582                 mdt_index=$($LFS getstripe -m $file)
15583                 [ $mdt_index == $MDTIDX ] ||
15584                         error "$file is not on MDT${MDTIDX}"
15585         done
15586
15587         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15588         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15589                 error " expect $old_dir_flag get $new_dir_flag"
15590
15591         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15592         [ "$old_file_flag" = "$new_file_flag" ] ||
15593                 error " expect $old_file_flag get $new_file_flag"
15594
15595         local new_dir_mode=$(stat -c%f $migrate_dir)
15596         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15597                 error "expect mode $old_dir_mode get $new_dir_mode"
15598
15599         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15600         [ "$old_file_mode" = "$new_file_mode" ] ||
15601                 error "expect mode $old_file_mode get $new_file_mode"
15602
15603         diff /etc/passwd ${migrate_dir}/$tfile ||
15604                 error "$tfile different after migration"
15605
15606         diff /etc/passwd ${other_dir}/luna ||
15607                 error "luna different after migration"
15608
15609         diff /etc/passwd ${migrate_dir}/sofia ||
15610                 error "sofia different after migration"
15611
15612         diff /etc/passwd ${other_dir}/zachary ||
15613                 error "zachary different after migration"
15614
15615         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15616                 error "${tfile}_ln different after migration"
15617
15618         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15619                 error "${tfile}_ln_other different after migration"
15620
15621         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
15622         [ $stripe_count = 2 ] ||
15623                 error "dir strpe_count $d != 2 after migration."
15624
15625         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
15626         [ $stripe_count = 2 ] ||
15627                 error "file strpe_count $d != 2 after migration."
15628
15629         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15630 }
15631 run_test 230b "migrate directory"
15632
15633 test_230c() {
15634         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15635         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15636         remote_mds_nodsh && skip "remote MDS with nodsh"
15637         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15638                 skip "Need MDS version at least 2.11.52"
15639
15640         local MDTIDX=1
15641         local total=3
15642         local mdt_index
15643         local file
15644         local migrate_dir=$DIR/$tdir/migrate_dir
15645
15646         #If migrating directory fails in the middle, all entries of
15647         #the directory is still accessiable.
15648         test_mkdir $DIR/$tdir
15649         test_mkdir -i0 -c1 $migrate_dir
15650         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
15651         stat $migrate_dir
15652         createmany -o $migrate_dir/f $total ||
15653                 error "create files under ${migrate_dir} failed"
15654
15655         # fail after migrating top dir, and this will fail only once, so the
15656         # first sub file migration will fail (currently f3), others succeed.
15657         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
15658         do_facet mds1 lctl set_param fail_loc=0x1801
15659         local t=$(ls $migrate_dir | wc -l)
15660         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
15661                 error "migrate should fail"
15662         local u=$(ls $migrate_dir | wc -l)
15663         [ "$u" == "$t" ] || error "$u != $t during migration"
15664
15665         # add new dir/file should succeed
15666         mkdir $migrate_dir/dir ||
15667                 error "mkdir failed under migrating directory"
15668         touch $migrate_dir/file ||
15669                 error "create file failed under migrating directory"
15670
15671         # add file with existing name should fail
15672         for file in $migrate_dir/f*; do
15673                 stat $file > /dev/null || error "stat $file failed"
15674                 $OPENFILE -f O_CREAT:O_EXCL $file &&
15675                         error "open(O_CREAT|O_EXCL) $file should fail"
15676                 $MULTIOP $file m && error "create $file should fail"
15677                 touch $DIR/$tdir/remote_dir/$tfile ||
15678                         error "touch $tfile failed"
15679                 ln $DIR/$tdir/remote_dir/$tfile $file &&
15680                         error "link $file should fail"
15681                 mdt_index=$($LFS getstripe -m $file)
15682                 if [ $mdt_index == 0 ]; then
15683                         # file failed to migrate is not allowed to rename to
15684                         mv $DIR/$tdir/remote_dir/$tfile $file &&
15685                                 error "rename to $file should fail"
15686                 else
15687                         mv $DIR/$tdir/remote_dir/$tfile $file ||
15688                                 error "rename to $file failed"
15689                 fi
15690                 echo hello >> $file || error "write $file failed"
15691         done
15692
15693         # resume migration with different options should fail
15694         $LFS migrate -m 0 $migrate_dir &&
15695                 error "migrate -m 0 $migrate_dir should fail"
15696
15697         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
15698                 error "migrate -c 2 $migrate_dir should fail"
15699
15700         # resume migration should succeed
15701         $LFS migrate -m $MDTIDX $migrate_dir ||
15702                 error "migrate $migrate_dir failed"
15703
15704         echo "Finish migration, then checking.."
15705         for file in $(find $migrate_dir); do
15706                 mdt_index=$($LFS getstripe -m $file)
15707                 [ $mdt_index == $MDTIDX ] ||
15708                         error "$file is not on MDT${MDTIDX}"
15709         done
15710
15711         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15712 }
15713 run_test 230c "check directory accessiblity if migration failed"
15714
15715 test_230d() {
15716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15717         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15718         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15719                 skip "Need MDS version at least 2.11.52"
15720         # LU-11235
15721         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
15722
15723         local migrate_dir=$DIR/$tdir/migrate_dir
15724         local old_index
15725         local new_index
15726         local old_count
15727         local new_count
15728         local new_hash
15729         local mdt_index
15730         local i
15731         local j
15732
15733         old_index=$((RANDOM % MDSCOUNT))
15734         old_count=$((MDSCOUNT - old_index))
15735         new_index=$((RANDOM % MDSCOUNT))
15736         new_count=$((MDSCOUNT - new_index))
15737         new_hash="all_char"
15738
15739         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
15740         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
15741
15742         test_mkdir $DIR/$tdir
15743         test_mkdir -i $old_index -c $old_count $migrate_dir
15744
15745         for ((i=0; i<100; i++)); do
15746                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
15747                 createmany -o $migrate_dir/dir_${i}/f 100 ||
15748                         error "create files under remote dir failed $i"
15749         done
15750
15751         echo -n "Migrate from MDT$old_index "
15752         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
15753         echo -n "to MDT$new_index"
15754         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
15755         echo
15756
15757         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
15758         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
15759                 error "migrate remote dir error"
15760
15761         echo "Finish migration, then checking.."
15762         for file in $(find $migrate_dir); do
15763                 mdt_index=$($LFS getstripe -m $file)
15764                 if [ $mdt_index -lt $new_index ] ||
15765                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
15766                         error "$file is on MDT$mdt_index"
15767                 fi
15768         done
15769
15770         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15771 }
15772 run_test 230d "check migrate big directory"
15773
15774 test_230e() {
15775         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15776         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15777         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15778                 skip "Need MDS version at least 2.11.52"
15779
15780         local i
15781         local j
15782         local a_fid
15783         local b_fid
15784
15785         mkdir -p $DIR/$tdir
15786         mkdir $DIR/$tdir/migrate_dir
15787         mkdir $DIR/$tdir/other_dir
15788         touch $DIR/$tdir/migrate_dir/a
15789         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
15790         ls $DIR/$tdir/other_dir
15791
15792         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15793                 error "migrate dir fails"
15794
15795         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15796         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15797
15798         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15799         [ $mdt_index == 0 ] || error "a is not on MDT0"
15800
15801         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
15802                 error "migrate dir fails"
15803
15804         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
15805         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
15806
15807         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15808         [ $mdt_index == 1 ] || error "a is not on MDT1"
15809
15810         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
15811         [ $mdt_index == 1 ] || error "b is not on MDT1"
15812
15813         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15814         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
15815
15816         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
15817
15818         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15819 }
15820 run_test 230e "migrate mulitple local link files"
15821
15822 test_230f() {
15823         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15824         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15825         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15826                 skip "Need MDS version at least 2.11.52"
15827
15828         local a_fid
15829         local ln_fid
15830
15831         mkdir -p $DIR/$tdir
15832         mkdir $DIR/$tdir/migrate_dir
15833         $LFS mkdir -i1 $DIR/$tdir/other_dir
15834         touch $DIR/$tdir/migrate_dir/a
15835         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
15836         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
15837         ls $DIR/$tdir/other_dir
15838
15839         # a should be migrated to MDT1, since no other links on MDT0
15840         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15841                 error "#1 migrate dir fails"
15842         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15843         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15844         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15845         [ $mdt_index == 1 ] || error "a is not on MDT1"
15846
15847         # a should stay on MDT1, because it is a mulitple link file
15848         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15849                 error "#2 migrate dir fails"
15850         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15851         [ $mdt_index == 1 ] || error "a is not on MDT1"
15852
15853         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15854                 error "#3 migrate dir fails"
15855
15856         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15857         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
15858         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
15859
15860         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
15861         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
15862
15863         # a should be migrated to MDT0, since no other links on MDT1
15864         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15865                 error "#4 migrate dir fails"
15866         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15867         [ $mdt_index == 0 ] || error "a is not on MDT0"
15868
15869         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15870 }
15871 run_test 230f "migrate mulitple remote link files"
15872
15873 test_230g() {
15874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15875         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15876         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15877                 skip "Need MDS version at least 2.11.52"
15878
15879         mkdir -p $DIR/$tdir/migrate_dir
15880
15881         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
15882                 error "migrating dir to non-exist MDT succeeds"
15883         true
15884 }
15885 run_test 230g "migrate dir to non-exist MDT"
15886
15887 test_230h() {
15888         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15889         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15890         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15891                 skip "Need MDS version at least 2.11.52"
15892
15893         local mdt_index
15894
15895         mkdir -p $DIR/$tdir/migrate_dir
15896
15897         $LFS migrate -m1 $DIR &&
15898                 error "migrating mountpoint1 should fail"
15899
15900         $LFS migrate -m1 $DIR/$tdir/.. &&
15901                 error "migrating mountpoint2 should fail"
15902
15903         # same as mv
15904         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
15905                 error "migrating $tdir/migrate_dir/.. should fail"
15906
15907         true
15908 }
15909 run_test 230h "migrate .. and root"
15910
15911 test_230i() {
15912         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15913         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15914         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15915                 skip "Need MDS version at least 2.11.52"
15916
15917         mkdir -p $DIR/$tdir/migrate_dir
15918
15919         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
15920                 error "migration fails with a tailing slash"
15921
15922         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
15923                 error "migration fails with two tailing slashes"
15924 }
15925 run_test 230i "lfs migrate -m tolerates trailing slashes"
15926
15927 test_230j() {
15928         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
15929         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15930                 skip "Need MDS version at least 2.11.52"
15931
15932         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
15933         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
15934                 error "create $tfile failed"
15935         cat /etc/passwd > $DIR/$tdir/$tfile
15936
15937         $LFS migrate -m 1 $DIR/$tdir
15938
15939         cmp /etc/passwd $DIR/$tdir/$tfile ||
15940                 error "DoM file mismatch after migration"
15941 }
15942 run_test 230j "DoM file data not changed after dir migration"
15943
15944 test_230k() {
15945         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
15946         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
15947                 skip "Need MDS version at least 2.11.56"
15948
15949         local total=20
15950         local files_on_starting_mdt=0
15951
15952         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
15953         $LFS getdirstripe $DIR/$tdir
15954         for i in $(seq $total); do
15955                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
15956                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
15957                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15958         done
15959
15960         echo "$files_on_starting_mdt files on MDT0"
15961
15962         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
15963         $LFS getdirstripe $DIR/$tdir
15964
15965         files_on_starting_mdt=0
15966         for i in $(seq $total); do
15967                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
15968                         error "file $tfile.$i mismatch after migration"
15969                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
15970                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15971         done
15972
15973         echo "$files_on_starting_mdt files on MDT1 after migration"
15974         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
15975
15976         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
15977         $LFS getdirstripe $DIR/$tdir
15978
15979         files_on_starting_mdt=0
15980         for i in $(seq $total); do
15981                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
15982                         error "file $tfile.$i mismatch after 2nd migration"
15983                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
15984                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
15985         done
15986
15987         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
15988         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
15989
15990         true
15991 }
15992 run_test 230k "file data not changed after dir migration"
15993
15994 test_230l() {
15995         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
15996         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
15997                 skip "Need MDS version at least 2.11.56"
15998
15999         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
16000         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
16001                 error "create files under remote dir failed $i"
16002         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
16003 }
16004 run_test 230l "readdir between MDTs won't crash"
16005
16006 test_231a()
16007 {
16008         # For simplicity this test assumes that max_pages_per_rpc
16009         # is the same across all OSCs
16010         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
16011         local bulk_size=$((max_pages * PAGE_SIZE))
16012         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
16013                                        head -n 1)
16014
16015         mkdir -p $DIR/$tdir
16016         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
16017                 error "failed to set stripe with -S ${brw_size}M option"
16018
16019         # clear the OSC stats
16020         $LCTL set_param osc.*.stats=0 &>/dev/null
16021         stop_writeback
16022
16023         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
16024         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
16025                 oflag=direct &>/dev/null || error "dd failed"
16026
16027         sync; sleep 1; sync # just to be safe
16028         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
16029         if [ x$nrpcs != "x1" ]; then
16030                 $LCTL get_param osc.*.stats
16031                 error "found $nrpcs ost_write RPCs, not 1 as expected"
16032         fi
16033
16034         start_writeback
16035         # Drop the OSC cache, otherwise we will read from it
16036         cancel_lru_locks osc
16037
16038         # clear the OSC stats
16039         $LCTL set_param osc.*.stats=0 &>/dev/null
16040
16041         # Client reads $bulk_size.
16042         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
16043                 iflag=direct &>/dev/null || error "dd failed"
16044
16045         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
16046         if [ x$nrpcs != "x1" ]; then
16047                 $LCTL get_param osc.*.stats
16048                 error "found $nrpcs ost_read RPCs, not 1 as expected"
16049         fi
16050 }
16051 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
16052
16053 test_231b() {
16054         mkdir -p $DIR/$tdir
16055         local i
16056         for i in {0..1023}; do
16057                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
16058                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
16059                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
16060         done
16061         sync
16062 }
16063 run_test 231b "must not assert on fully utilized OST request buffer"
16064
16065 test_232a() {
16066         mkdir -p $DIR/$tdir
16067         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16068
16069         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16070         do_facet ost1 $LCTL set_param fail_loc=0x31c
16071
16072         # ignore dd failure
16073         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
16074
16075         do_facet ost1 $LCTL set_param fail_loc=0
16076         umount_client $MOUNT || error "umount failed"
16077         mount_client $MOUNT || error "mount failed"
16078         stop ost1 || error "cannot stop ost1"
16079         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16080 }
16081 run_test 232a "failed lock should not block umount"
16082
16083 test_232b() {
16084         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
16085                 skip "Need MDS version at least 2.10.58"
16086
16087         mkdir -p $DIR/$tdir
16088         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16089         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
16090         sync
16091         cancel_lru_locks osc
16092
16093         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16094         do_facet ost1 $LCTL set_param fail_loc=0x31c
16095
16096         # ignore failure
16097         $LFS data_version $DIR/$tdir/$tfile || true
16098
16099         do_facet ost1 $LCTL set_param fail_loc=0
16100         umount_client $MOUNT || error "umount failed"
16101         mount_client $MOUNT || error "mount failed"
16102         stop ost1 || error "cannot stop ost1"
16103         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16104 }
16105 run_test 232b "failed data version lock should not block umount"
16106
16107 test_233a() {
16108         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
16109                 skip "Need MDS version at least 2.3.64"
16110         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16111
16112         local fid=$($LFS path2fid $MOUNT)
16113
16114         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16115                 error "cannot access $MOUNT using its FID '$fid'"
16116 }
16117 run_test 233a "checking that OBF of the FS root succeeds"
16118
16119 test_233b() {
16120         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
16121                 skip "Need MDS version at least 2.5.90"
16122         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16123
16124         local fid=$($LFS path2fid $MOUNT/.lustre)
16125
16126         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16127                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
16128
16129         fid=$($LFS path2fid $MOUNT/.lustre/fid)
16130         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16131                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
16132 }
16133 run_test 233b "checking that OBF of the FS .lustre succeeds"
16134
16135 test_234() {
16136         local p="$TMP/sanityN-$TESTNAME.parameters"
16137         save_lustre_params client "llite.*.xattr_cache" > $p
16138         lctl set_param llite.*.xattr_cache 1 ||
16139                 skip_env "xattr cache is not supported"
16140
16141         mkdir -p $DIR/$tdir || error "mkdir failed"
16142         touch $DIR/$tdir/$tfile || error "touch failed"
16143         # OBD_FAIL_LLITE_XATTR_ENOMEM
16144         $LCTL set_param fail_loc=0x1405
16145         getfattr -n user.attr $DIR/$tdir/$tfile &&
16146                 error "getfattr should have failed with ENOMEM"
16147         $LCTL set_param fail_loc=0x0
16148         rm -rf $DIR/$tdir
16149
16150         restore_lustre_params < $p
16151         rm -f $p
16152 }
16153 run_test 234 "xattr cache should not crash on ENOMEM"
16154
16155 test_235() {
16156         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
16157                 skip "Need MDS version at least 2.4.52"
16158
16159         flock_deadlock $DIR/$tfile
16160         local RC=$?
16161         case $RC in
16162                 0)
16163                 ;;
16164                 124) error "process hangs on a deadlock"
16165                 ;;
16166                 *) error "error executing flock_deadlock $DIR/$tfile"
16167                 ;;
16168         esac
16169 }
16170 run_test 235 "LU-1715: flock deadlock detection does not work properly"
16171
16172 #LU-2935
16173 test_236() {
16174         check_swap_layouts_support
16175
16176         local ref1=/etc/passwd
16177         local ref2=/etc/group
16178         local file1=$DIR/$tdir/f1
16179         local file2=$DIR/$tdir/f2
16180
16181         test_mkdir -c1 $DIR/$tdir
16182         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
16183         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
16184         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
16185         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
16186         local fd=$(free_fd)
16187         local cmd="exec $fd<>$file2"
16188         eval $cmd
16189         rm $file2
16190         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
16191                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
16192         cmd="exec $fd>&-"
16193         eval $cmd
16194         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
16195
16196         #cleanup
16197         rm -rf $DIR/$tdir
16198 }
16199 run_test 236 "Layout swap on open unlinked file"
16200
16201 # LU-4659 linkea consistency
16202 test_238() {
16203         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
16204                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
16205                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
16206                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
16207
16208         touch $DIR/$tfile
16209         ln $DIR/$tfile $DIR/$tfile.lnk
16210         touch $DIR/$tfile.new
16211         mv $DIR/$tfile.new $DIR/$tfile
16212         local fid1=$($LFS path2fid $DIR/$tfile)
16213         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
16214         local path1=$($LFS fid2path $FSNAME "$fid1")
16215         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
16216         local path2=$($LFS fid2path $FSNAME "$fid2")
16217         [ $tfile.lnk == $path2 ] ||
16218                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
16219         rm -f $DIR/$tfile*
16220 }
16221 run_test 238 "Verify linkea consistency"
16222
16223 test_239A() { # was test_239
16224         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
16225                 skip "Need MDS version at least 2.5.60"
16226
16227         local list=$(comma_list $(mdts_nodes))
16228
16229         mkdir -p $DIR/$tdir
16230         createmany -o $DIR/$tdir/f- 5000
16231         unlinkmany $DIR/$tdir/f- 5000
16232         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
16233                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
16234         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
16235                         osp.*MDT*.sync_in_flight" | calc_sum)
16236         [ "$changes" -eq 0 ] || error "$changes not synced"
16237 }
16238 run_test 239A "osp_sync test"
16239
16240 test_239a() { #LU-5297
16241         remote_mds_nodsh && skip "remote MDS with nodsh"
16242
16243         touch $DIR/$tfile
16244         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
16245         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
16246         chgrp $RUNAS_GID $DIR/$tfile
16247         wait_delete_completed
16248 }
16249 run_test 239a "process invalid osp sync record correctly"
16250
16251 test_239b() { #LU-5297
16252         remote_mds_nodsh && skip "remote MDS with nodsh"
16253
16254         touch $DIR/$tfile1
16255         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
16256         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
16257         chgrp $RUNAS_GID $DIR/$tfile1
16258         wait_delete_completed
16259         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
16260         touch $DIR/$tfile2
16261         chgrp $RUNAS_GID $DIR/$tfile2
16262         wait_delete_completed
16263 }
16264 run_test 239b "process osp sync record with ENOMEM error correctly"
16265
16266 test_240() {
16267         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16268         remote_mds_nodsh && skip "remote MDS with nodsh"
16269
16270         mkdir -p $DIR/$tdir
16271
16272         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
16273                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
16274         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
16275                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
16276
16277         umount_client $MOUNT || error "umount failed"
16278         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
16279         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
16280         mount_client $MOUNT || error "failed to mount client"
16281
16282         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
16283         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
16284 }
16285 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
16286
16287 test_241_bio() {
16288         local count=$1
16289         local bsize=$2
16290
16291         for LOOP in $(seq $count); do
16292                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
16293                 cancel_lru_locks $OSC || true
16294         done
16295 }
16296
16297 test_241_dio() {
16298         local count=$1
16299         local bsize=$2
16300
16301         for LOOP in $(seq $1); do
16302                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
16303                         2>/dev/null
16304         done
16305 }
16306
16307 test_241a() { # was test_241
16308         local bsize=$PAGE_SIZE
16309
16310         (( bsize < 40960 )) && bsize=40960
16311         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
16312         ls -la $DIR/$tfile
16313         cancel_lru_locks $OSC
16314         test_241_bio 1000 $bsize &
16315         PID=$!
16316         test_241_dio 1000 $bsize
16317         wait $PID
16318 }
16319 run_test 241a "bio vs dio"
16320
16321 test_241b() {
16322         local bsize=$PAGE_SIZE
16323
16324         (( bsize < 40960 )) && bsize=40960
16325         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
16326         ls -la $DIR/$tfile
16327         test_241_dio 1000 $bsize &
16328         PID=$!
16329         test_241_dio 1000 $bsize
16330         wait $PID
16331 }
16332 run_test 241b "dio vs dio"
16333
16334 test_242() {
16335         remote_mds_nodsh && skip "remote MDS with nodsh"
16336
16337         mkdir -p $DIR/$tdir
16338         touch $DIR/$tdir/$tfile
16339
16340         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
16341         do_facet mds1 lctl set_param fail_loc=0x105
16342         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
16343
16344         do_facet mds1 lctl set_param fail_loc=0
16345         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
16346 }
16347 run_test 242 "mdt_readpage failure should not cause directory unreadable"
16348
16349 test_243()
16350 {
16351         test_mkdir $DIR/$tdir
16352         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
16353 }
16354 run_test 243 "various group lock tests"
16355
16356 test_244()
16357 {
16358         test_mkdir $DIR/$tdir
16359         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
16360         sendfile_grouplock $DIR/$tdir/$tfile || \
16361                 error "sendfile+grouplock failed"
16362         rm -rf $DIR/$tdir
16363 }
16364 run_test 244 "sendfile with group lock tests"
16365
16366 test_245() {
16367         local flagname="multi_mod_rpcs"
16368         local connect_data_name="max_mod_rpcs"
16369         local out
16370
16371         # check if multiple modify RPCs flag is set
16372         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
16373                 grep "connect_flags:")
16374         echo "$out"
16375
16376         echo "$out" | grep -qw $flagname
16377         if [ $? -ne 0 ]; then
16378                 echo "connect flag $flagname is not set"
16379                 return
16380         fi
16381
16382         # check if multiple modify RPCs data is set
16383         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
16384         echo "$out"
16385
16386         echo "$out" | grep -qw $connect_data_name ||
16387                 error "import should have connect data $connect_data_name"
16388 }
16389 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
16390
16391 test_246() { # LU-7371
16392         remote_ost_nodsh && skip "remote OST with nodsh"
16393         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
16394                 skip "Need OST version >= 2.7.62"
16395
16396         do_facet ost1 $LCTL set_param fail_val=4095
16397 #define OBD_FAIL_OST_READ_SIZE          0x234
16398         do_facet ost1 $LCTL set_param fail_loc=0x234
16399         $LFS setstripe $DIR/$tfile -i 0 -c 1
16400         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
16401         cancel_lru_locks $FSNAME-OST0000
16402         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
16403 }
16404 run_test 246 "Read file of size 4095 should return right length"
16405
16406 cleanup_247() {
16407         local submount=$1
16408
16409         trap 0
16410         umount_client $submount
16411         rmdir $submount
16412 }
16413
16414 test_247a() {
16415         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16416                 grep -q subtree ||
16417                 skip_env "Fileset feature is not supported"
16418
16419         local submount=${MOUNT}_$tdir
16420
16421         mkdir $MOUNT/$tdir
16422         mkdir -p $submount || error "mkdir $submount failed"
16423         FILESET="$FILESET/$tdir" mount_client $submount ||
16424                 error "mount $submount failed"
16425         trap "cleanup_247 $submount" EXIT
16426         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
16427         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
16428                 error "read $MOUNT/$tdir/$tfile failed"
16429         cleanup_247 $submount
16430 }
16431 run_test 247a "mount subdir as fileset"
16432
16433 test_247b() {
16434         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16435                 skip_env "Fileset feature is not supported"
16436
16437         local submount=${MOUNT}_$tdir
16438
16439         rm -rf $MOUNT/$tdir
16440         mkdir -p $submount || error "mkdir $submount failed"
16441         SKIP_FILESET=1
16442         FILESET="$FILESET/$tdir" mount_client $submount &&
16443                 error "mount $submount should fail"
16444         rmdir $submount
16445 }
16446 run_test 247b "mount subdir that dose not exist"
16447
16448 test_247c() {
16449         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16450                 skip_env "Fileset feature is not supported"
16451
16452         local submount=${MOUNT}_$tdir
16453
16454         mkdir -p $MOUNT/$tdir/dir1
16455         mkdir -p $submount || error "mkdir $submount failed"
16456         trap "cleanup_247 $submount" EXIT
16457         FILESET="$FILESET/$tdir" mount_client $submount ||
16458                 error "mount $submount failed"
16459         local fid=$($LFS path2fid $MOUNT/)
16460         $LFS fid2path $submount $fid && error "fid2path should fail"
16461         cleanup_247 $submount
16462 }
16463 run_test 247c "running fid2path outside root"
16464
16465 test_247d() {
16466         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16467                 skip "Fileset feature is not supported"
16468
16469         local submount=${MOUNT}_$tdir
16470
16471         mkdir -p $MOUNT/$tdir/dir1
16472         mkdir -p $submount || error "mkdir $submount failed"
16473         FILESET="$FILESET/$tdir" mount_client $submount ||
16474                 error "mount $submount failed"
16475         trap "cleanup_247 $submount" EXIT
16476         local fid=$($LFS path2fid $submount/dir1)
16477         $LFS fid2path $submount $fid || error "fid2path should succeed"
16478         cleanup_247 $submount
16479 }
16480 run_test 247d "running fid2path inside root"
16481
16482 # LU-8037
16483 test_247e() {
16484         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16485                 grep -q subtree ||
16486                 skip "Fileset feature is not supported"
16487
16488         local submount=${MOUNT}_$tdir
16489
16490         mkdir $MOUNT/$tdir
16491         mkdir -p $submount || error "mkdir $submount failed"
16492         FILESET="$FILESET/.." mount_client $submount &&
16493                 error "mount $submount should fail"
16494         rmdir $submount
16495 }
16496 run_test 247e "mount .. as fileset"
16497
16498 test_248() {
16499         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
16500         [ -z "$fast_read_sav" ] && skip "no fast read support"
16501
16502         # create a large file for fast read verification
16503         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
16504
16505         # make sure the file is created correctly
16506         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
16507                 { rm -f $DIR/$tfile; skip "file creation error"; }
16508
16509         echo "Test 1: verify that fast read is 4 times faster on cache read"
16510
16511         # small read with fast read enabled
16512         $LCTL set_param -n llite.*.fast_read=1
16513         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16514                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16515                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16516         # small read with fast read disabled
16517         $LCTL set_param -n llite.*.fast_read=0
16518         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16519                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16520                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16521
16522         # verify that fast read is 4 times faster for cache read
16523         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
16524                 error_not_in_vm "fast read was not 4 times faster: " \
16525                            "$t_fast vs $t_slow"
16526
16527         echo "Test 2: verify the performance between big and small read"
16528         $LCTL set_param -n llite.*.fast_read=1
16529
16530         # 1k non-cache read
16531         cancel_lru_locks osc
16532         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16533                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16534                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16535
16536         # 1M non-cache read
16537         cancel_lru_locks osc
16538         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16539                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16540                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16541
16542         # verify that big IO is not 4 times faster than small IO
16543         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
16544                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
16545
16546         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
16547         rm -f $DIR/$tfile
16548 }
16549 run_test 248 "fast read verification"
16550
16551 test_249() { # LU-7890
16552         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
16553                 skip "Need at least version 2.8.54"
16554
16555         rm -f $DIR/$tfile
16556         $LFS setstripe -c 1 $DIR/$tfile
16557         # Offset 2T == 4k * 512M
16558         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
16559                 error "dd to 2T offset failed"
16560 }
16561 run_test 249 "Write above 2T file size"
16562
16563 test_250() {
16564         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
16565          && skip "no 16TB file size limit on ZFS"
16566
16567         $LFS setstripe -c 1 $DIR/$tfile
16568         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
16569         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
16570         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
16571         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
16572                 conv=notrunc,fsync && error "append succeeded"
16573         return 0
16574 }
16575 run_test 250 "Write above 16T limit"
16576
16577 test_251() {
16578         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
16579
16580         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
16581         #Skip once - writing the first stripe will succeed
16582         $LCTL set_param fail_loc=0xa0001407 fail_val=1
16583         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
16584                 error "short write happened"
16585
16586         $LCTL set_param fail_loc=0xa0001407 fail_val=1
16587         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
16588                 error "short read happened"
16589
16590         rm -f $DIR/$tfile
16591 }
16592 run_test 251 "Handling short read and write correctly"
16593
16594 test_252() {
16595         remote_mds_nodsh && skip "remote MDS with nodsh"
16596         remote_ost_nodsh && skip "remote OST with nodsh"
16597         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
16598                 skip_env "ldiskfs only test"
16599         fi
16600
16601         local tgt
16602         local dev
16603         local out
16604         local uuid
16605         local num
16606         local gen
16607
16608         # check lr_reader on OST0000
16609         tgt=ost1
16610         dev=$(facet_device $tgt)
16611         out=$(do_facet $tgt $LR_READER $dev)
16612         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16613         echo "$out"
16614         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
16615         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
16616                 error "Invalid uuid returned by $LR_READER on target $tgt"
16617         echo -e "uuid returned by $LR_READER is '$uuid'\n"
16618
16619         # check lr_reader -c on MDT0000
16620         tgt=mds1
16621         dev=$(facet_device $tgt)
16622         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
16623                 skip "$LR_READER does not support additional options"
16624         fi
16625         out=$(do_facet $tgt $LR_READER -c $dev)
16626         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16627         echo "$out"
16628         num=$(echo "$out" | grep -c "mdtlov")
16629         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
16630                 error "Invalid number of mdtlov clients returned by $LR_READER"
16631         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
16632
16633         # check lr_reader -cr on MDT0000
16634         out=$(do_facet $tgt $LR_READER -cr $dev)
16635         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
16636         echo "$out"
16637         echo "$out" | grep -q "^reply_data:$" ||
16638                 error "$LR_READER should have returned 'reply_data' section"
16639         num=$(echo "$out" | grep -c "client_generation")
16640         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
16641 }
16642 run_test 252 "check lr_reader tool"
16643
16644 test_253_fill_ost() {
16645         local size_mb #how many MB should we write to pass watermark
16646         local lwm=$3  #low watermark
16647         local free_10mb #10% of free space
16648
16649         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
16650         size_mb=$((free_kb / 1024 - lwm))
16651         free_10mb=$((free_kb / 10240))
16652         #If 10% of free space cross low watermark use it
16653         if (( free_10mb > size_mb )); then
16654                 size_mb=$free_10mb
16655         else
16656                 #At least we need to store 1.1 of difference between
16657                 #free space and low watermark
16658                 size_mb=$((size_mb + size_mb / 10))
16659         fi
16660         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
16661                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
16662                          oflag=append conv=notrunc
16663         fi
16664
16665         sleep_maxage
16666
16667         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
16668         echo "OST still has $((free_kb / 1024)) mbytes free"
16669 }
16670
16671 test_253() {
16672         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16673         remote_mds_nodsh && skip "remote MDS with nodsh"
16674         remote_mgs_nodsh && skip "remote MGS with nodsh"
16675
16676         local ostidx=0
16677         local rc=0
16678
16679         local ost_name=$($LFS osts |
16680                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
16681         # on the mdt's osc
16682         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
16683         do_facet $SINGLEMDS $LCTL get_param -n \
16684                 osp.$mdtosc_proc1.reserved_mb_high ||
16685                 skip  "remote MDS does not support reserved_mb_high"
16686
16687         rm -rf $DIR/$tdir
16688         wait_mds_ost_sync
16689         wait_delete_completed
16690         mkdir $DIR/$tdir
16691
16692         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
16693                         osp.$mdtosc_proc1.reserved_mb_high)
16694         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
16695                         osp.$mdtosc_proc1.reserved_mb_low)
16696         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
16697
16698         if ! combined_mgs_mds ; then
16699                 mount_mgs_client
16700         fi
16701         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
16702         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
16703                 error "Adding $ost_name to pool failed"
16704
16705         # Wait for client to see a OST at pool
16706         wait_update $HOSTNAME "$LCTL get_param -n
16707                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
16708                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
16709                 error "Client can not see the pool"
16710         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
16711                 error "Setstripe failed"
16712
16713         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
16714         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
16715         echo "OST still has $((blocks/1024)) mbytes free"
16716
16717         local new_lwm=$((blocks/1024-10))
16718         do_facet $SINGLEMDS $LCTL set_param \
16719                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
16720         do_facet $SINGLEMDS $LCTL set_param \
16721                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
16722
16723         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
16724
16725         #First enospc could execute orphan deletion so repeat.
16726         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
16727
16728         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
16729                         osp.$mdtosc_proc1.prealloc_status)
16730         echo "prealloc_status $oa_status"
16731
16732         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
16733                 error "File creation should fail"
16734         #object allocation was stopped, but we still able to append files
16735         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
16736                 error "Append failed"
16737         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
16738
16739         wait_delete_completed
16740
16741         sleep_maxage
16742
16743         for i in $(seq 10 12); do
16744                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
16745                         error "File creation failed after rm";
16746         done
16747
16748         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
16749                         osp.$mdtosc_proc1.prealloc_status)
16750         echo "prealloc_status $oa_status"
16751
16752         if (( oa_status != 0 )); then
16753                 error "Object allocation still disable after rm"
16754         fi
16755         do_facet $SINGLEMDS $LCTL set_param \
16756                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
16757         do_facet $SINGLEMDS $LCTL set_param \
16758                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
16759
16760
16761         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
16762                 error "Remove $ost_name from pool failed"
16763         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16764                 error "Pool destroy fialed"
16765
16766         if ! combined_mgs_mds ; then
16767                 umount_mgs_client
16768         fi
16769 }
16770 run_test 253 "Check object allocation limit"
16771
16772 test_254() {
16773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16774         remote_mds_nodsh && skip "remote MDS with nodsh"
16775         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
16776                 skip "MDS does not support changelog_size"
16777
16778         local cl_user
16779         local MDT0=$(facet_svc $SINGLEMDS)
16780
16781         changelog_register || error "changelog_register failed"
16782
16783         changelog_clear 0 || error "changelog_clear failed"
16784
16785         local size1=$(do_facet $SINGLEMDS \
16786                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16787         echo "Changelog size $size1"
16788
16789         rm -rf $DIR/$tdir
16790         $LFS mkdir -i 0 $DIR/$tdir
16791         # change something
16792         mkdir -p $DIR/$tdir/pics/2008/zachy
16793         touch $DIR/$tdir/pics/2008/zachy/timestamp
16794         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
16795         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
16796         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
16797         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
16798         rm $DIR/$tdir/pics/desktop.jpg
16799
16800         local size2=$(do_facet $SINGLEMDS \
16801                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16802         echo "Changelog size after work $size2"
16803
16804         (( $size2 > $size1 )) ||
16805                 error "new Changelog size=$size2 less than old size=$size1"
16806 }
16807 run_test 254 "Check changelog size"
16808
16809 ladvise_no_type()
16810 {
16811         local type=$1
16812         local file=$2
16813
16814         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
16815                 awk -F: '{print $2}' | grep $type > /dev/null
16816         if [ $? -ne 0 ]; then
16817                 return 0
16818         fi
16819         return 1
16820 }
16821
16822 ladvise_no_ioctl()
16823 {
16824         local file=$1
16825
16826         lfs ladvise -a willread $file > /dev/null 2>&1
16827         if [ $? -eq 0 ]; then
16828                 return 1
16829         fi
16830
16831         lfs ladvise -a willread $file 2>&1 |
16832                 grep "Inappropriate ioctl for device" > /dev/null
16833         if [ $? -eq 0 ]; then
16834                 return 0
16835         fi
16836         return 1
16837 }
16838
16839 percent() {
16840         bc <<<"scale=2; ($1 - $2) * 100 / $2"
16841 }
16842
16843 # run a random read IO workload
16844 # usage: random_read_iops <filename> <filesize> <iosize>
16845 random_read_iops() {
16846         local file=$1
16847         local fsize=$2
16848         local iosize=${3:-4096}
16849
16850         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
16851                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
16852 }
16853
16854 drop_file_oss_cache() {
16855         local file="$1"
16856         local nodes="$2"
16857
16858         $LFS ladvise -a dontneed $file 2>/dev/null ||
16859                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
16860 }
16861
16862 ladvise_willread_performance()
16863 {
16864         local repeat=10
16865         local average_origin=0
16866         local average_cache=0
16867         local average_ladvise=0
16868
16869         for ((i = 1; i <= $repeat; i++)); do
16870                 echo "Iter $i/$repeat: reading without willread hint"
16871                 cancel_lru_locks osc
16872                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16873                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
16874                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
16875                 average_origin=$(bc <<<"$average_origin + $speed_origin")
16876
16877                 cancel_lru_locks osc
16878                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
16879                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
16880                 average_cache=$(bc <<<"$average_cache + $speed_cache")
16881
16882                 cancel_lru_locks osc
16883                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16884                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
16885                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
16886                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
16887                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
16888         done
16889         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
16890         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
16891         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
16892
16893         speedup_cache=$(percent $average_cache $average_origin)
16894         speedup_ladvise=$(percent $average_ladvise $average_origin)
16895
16896         echo "Average uncached read: $average_origin"
16897         echo "Average speedup with OSS cached read: " \
16898                 "$average_cache = +$speedup_cache%"
16899         echo "Average speedup with ladvise willread: " \
16900                 "$average_ladvise = +$speedup_ladvise%"
16901
16902         local lowest_speedup=20
16903         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
16904                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
16905                         "got $average_cache%. Skipping ladvise willread check."
16906                 return 0
16907         fi
16908
16909         # the test won't work on ZFS until it supports 'ladvise dontneed', but
16910         # it is still good to run until then to exercise 'ladvise willread'
16911         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16912                 [ "$ost1_FSTYPE" = "zfs" ] &&
16913                 echo "osd-zfs does not support dontneed or drop_caches" &&
16914                 return 0
16915
16916         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
16917         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
16918                 error_not_in_vm "Speedup with willread is less than " \
16919                         "$lowest_speedup%, got $average_ladvise%"
16920 }
16921
16922 test_255a() {
16923         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
16924                 skip "lustre < 2.8.54 does not support ladvise "
16925         remote_ost_nodsh && skip "remote OST with nodsh"
16926
16927         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
16928
16929         ladvise_no_type willread $DIR/$tfile &&
16930                 skip "willread ladvise is not supported"
16931
16932         ladvise_no_ioctl $DIR/$tfile &&
16933                 skip "ladvise ioctl is not supported"
16934
16935         local size_mb=100
16936         local size=$((size_mb * 1048576))
16937         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16938                 error "dd to $DIR/$tfile failed"
16939
16940         lfs ladvise -a willread $DIR/$tfile ||
16941                 error "Ladvise failed with no range argument"
16942
16943         lfs ladvise -a willread -s 0 $DIR/$tfile ||
16944                 error "Ladvise failed with no -l or -e argument"
16945
16946         lfs ladvise -a willread -e 1 $DIR/$tfile ||
16947                 error "Ladvise failed with only -e argument"
16948
16949         lfs ladvise -a willread -l 1 $DIR/$tfile ||
16950                 error "Ladvise failed with only -l argument"
16951
16952         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
16953                 error "End offset should not be smaller than start offset"
16954
16955         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
16956                 error "End offset should not be equal to start offset"
16957
16958         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
16959                 error "Ladvise failed with overflowing -s argument"
16960
16961         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
16962                 error "Ladvise failed with overflowing -e argument"
16963
16964         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
16965                 error "Ladvise failed with overflowing -l argument"
16966
16967         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
16968                 error "Ladvise succeeded with conflicting -l and -e arguments"
16969
16970         echo "Synchronous ladvise should wait"
16971         local delay=4
16972 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
16973         do_nodes $(comma_list $(osts_nodes)) \
16974                 $LCTL set_param fail_val=$delay fail_loc=0x237
16975
16976         local start_ts=$SECONDS
16977         lfs ladvise -a willread $DIR/$tfile ||
16978                 error "Ladvise failed with no range argument"
16979         local end_ts=$SECONDS
16980         local inteval_ts=$((end_ts - start_ts))
16981
16982         if [ $inteval_ts -lt $(($delay - 1)) ]; then
16983                 error "Synchronous advice didn't wait reply"
16984         fi
16985
16986         echo "Asynchronous ladvise shouldn't wait"
16987         local start_ts=$SECONDS
16988         lfs ladvise -a willread -b $DIR/$tfile ||
16989                 error "Ladvise failed with no range argument"
16990         local end_ts=$SECONDS
16991         local inteval_ts=$((end_ts - start_ts))
16992
16993         if [ $inteval_ts -gt $(($delay / 2)) ]; then
16994                 error "Asynchronous advice blocked"
16995         fi
16996
16997         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
16998         ladvise_willread_performance
16999 }
17000 run_test 255a "check 'lfs ladvise -a willread'"
17001
17002 facet_meminfo() {
17003         local facet=$1
17004         local info=$2
17005
17006         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
17007 }
17008
17009 test_255b() {
17010         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17011                 skip "lustre < 2.8.54 does not support ladvise "
17012         remote_ost_nodsh && skip "remote OST with nodsh"
17013
17014         lfs setstripe -c 1 -i 0 $DIR/$tfile
17015
17016         ladvise_no_type dontneed $DIR/$tfile &&
17017                 skip "dontneed ladvise is not supported"
17018
17019         ladvise_no_ioctl $DIR/$tfile &&
17020                 skip "ladvise ioctl is not supported"
17021
17022         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17023                 [ "$ost1_FSTYPE" = "zfs" ] &&
17024                 skip "zfs-osd does not support 'ladvise dontneed'"
17025
17026         local size_mb=100
17027         local size=$((size_mb * 1048576))
17028         # In order to prevent disturbance of other processes, only check 3/4
17029         # of the memory usage
17030         local kibibytes=$((size_mb * 1024 * 3 / 4))
17031
17032         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17033                 error "dd to $DIR/$tfile failed"
17034
17035         #force write to complete before dropping OST cache & checking memory
17036         sync
17037
17038         local total=$(facet_meminfo ost1 MemTotal)
17039         echo "Total memory: $total KiB"
17040
17041         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
17042         local before_read=$(facet_meminfo ost1 Cached)
17043         echo "Cache used before read: $before_read KiB"
17044
17045         lfs ladvise -a willread $DIR/$tfile ||
17046                 error "Ladvise willread failed"
17047         local after_read=$(facet_meminfo ost1 Cached)
17048         echo "Cache used after read: $after_read KiB"
17049
17050         lfs ladvise -a dontneed $DIR/$tfile ||
17051                 error "Ladvise dontneed again failed"
17052         local no_read=$(facet_meminfo ost1 Cached)
17053         echo "Cache used after dontneed ladvise: $no_read KiB"
17054
17055         if [ $total -lt $((before_read + kibibytes)) ]; then
17056                 echo "Memory is too small, abort checking"
17057                 return 0
17058         fi
17059
17060         if [ $((before_read + kibibytes)) -gt $after_read ]; then
17061                 error "Ladvise willread should use more memory" \
17062                         "than $kibibytes KiB"
17063         fi
17064
17065         if [ $((no_read + kibibytes)) -gt $after_read ]; then
17066                 error "Ladvise dontneed should release more memory" \
17067                         "than $kibibytes KiB"
17068         fi
17069 }
17070 run_test 255b "check 'lfs ladvise -a dontneed'"
17071
17072 test_255c() {
17073         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
17074                 skip "lustre < 2.10.53 does not support lockahead"
17075
17076         local count
17077         local new_count
17078         local difference
17079         local i
17080         local rc
17081
17082         test_mkdir -p $DIR/$tdir
17083         $LFS setstripe -i 0 -c 1 $DIR/$tdir
17084
17085         #test 10 returns only success/failure
17086         i=10
17087         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17088         rc=$?
17089         if [ $rc -eq 255 ]; then
17090                 error "Ladvise test${i} failed, ${rc}"
17091         fi
17092
17093         #test 11 counts lock enqueue requests, all others count new locks
17094         i=11
17095         count=$(do_facet ost1 \
17096                 $LCTL get_param -n ost.OSS.ost.stats)
17097         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
17098
17099         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17100         rc=$?
17101         if [ $rc -eq 255 ]; then
17102                 error "Ladvise test${i} failed, ${rc}"
17103         fi
17104
17105         new_count=$(do_facet ost1 \
17106                 $LCTL get_param -n ost.OSS.ost.stats)
17107         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
17108                    awk '{ print $2 }')
17109
17110         difference="$((new_count - count))"
17111         if [ $difference -ne $rc ]; then
17112                 error "Ladvise test${i}, bad enqueue count, returned " \
17113                       "${rc}, actual ${difference}"
17114         fi
17115
17116         for i in $(seq 12 21); do
17117                 # If we do not do this, we run the risk of having too many
17118                 # locks and starting lock cancellation while we are checking
17119                 # lock counts.
17120                 cancel_lru_locks osc
17121
17122                 count=$($LCTL get_param -n \
17123                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17124
17125                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
17126                 rc=$?
17127                 if [ $rc -eq 255 ]; then
17128                         error "Ladvise test ${i} failed, ${rc}"
17129                 fi
17130
17131                 new_count=$($LCTL get_param -n \
17132                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17133                 difference="$((new_count - count))"
17134
17135                 # Test 15 output is divided by 100 to map down to valid return
17136                 if [ $i -eq 15 ]; then
17137                         rc="$((rc * 100))"
17138                 fi
17139
17140                 if [ $difference -ne $rc ]; then
17141                         error "Ladvise test ${i}, bad lock count, returned " \
17142                               "${rc}, actual ${difference}"
17143                 fi
17144         done
17145
17146         #test 22 returns only success/failure
17147         i=22
17148         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17149         rc=$?
17150         if [ $rc -eq 255 ]; then
17151                 error "Ladvise test${i} failed, ${rc}"
17152         fi
17153 }
17154 run_test 255c "suite of ladvise lockahead tests"
17155
17156 test_256() {
17157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17158         remote_mds_nodsh && skip "remote MDS with nodsh"
17159         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
17160         changelog_users $SINGLEMDS | grep "^cl" &&
17161                 skip "active changelog user"
17162
17163         local cl_user
17164         local cat_sl
17165         local mdt_dev
17166
17167         mdt_dev=$(mdsdevname 1)
17168         echo $mdt_dev
17169
17170         changelog_register || error "changelog_register failed"
17171
17172         rm -rf $DIR/$tdir
17173         mkdir -p $DIR/$tdir
17174
17175         changelog_clear 0 || error "changelog_clear failed"
17176
17177         # change something
17178         touch $DIR/$tdir/{1..10}
17179
17180         # stop the MDT
17181         stop $SINGLEMDS || error "Fail to stop MDT"
17182
17183         # remount the MDT
17184
17185         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
17186
17187         #after mount new plainllog is used
17188         touch $DIR/$tdir/{11..19}
17189         local tmpfile=$(mktemp -u $tfile.XXXXXX)
17190         cat_sl=$(do_facet $SINGLEMDS "sync; \
17191                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
17192                  llog_reader $tmpfile | grep -c type=1064553b")
17193         do_facet $SINGLEMDS llog_reader $tmpfile
17194
17195         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
17196
17197         changelog_clear 0 || error "changelog_clear failed"
17198
17199         cat_sl=$(do_facet $SINGLEMDS "sync; \
17200                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
17201                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
17202
17203         if (( cat_sl == 2 )); then
17204                 error "Empty plain llog was not deleted from changelog catalog"
17205         elif (( cat_sl != 1 )); then
17206                 error "Active plain llog shouldn't be deleted from catalog"
17207         fi
17208 }
17209 run_test 256 "Check llog delete for empty and not full state"
17210
17211 test_257() {
17212         remote_mds_nodsh && skip "remote MDS with nodsh"
17213         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
17214                 skip "Need MDS version at least 2.8.55"
17215
17216         test_mkdir $DIR/$tdir
17217
17218         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
17219                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
17220         stat $DIR/$tdir
17221
17222 #define OBD_FAIL_MDS_XATTR_REP                  0x161
17223         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
17224         local facet=mds$((mdtidx + 1))
17225         set_nodes_failloc $(facet_active_host $facet) 0x80000161
17226         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
17227
17228         stop $facet || error "stop MDS failed"
17229         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
17230                 error "start MDS fail"
17231         wait_recovery_complete $facet
17232 }
17233 run_test 257 "xattr locks are not lost"
17234
17235 # Verify we take the i_mutex when security requires it
17236 test_258a() {
17237 #define OBD_FAIL_IMUTEX_SEC 0x141c
17238         $LCTL set_param fail_loc=0x141c
17239         touch $DIR/$tfile
17240         chmod u+s $DIR/$tfile
17241         chmod a+rwx $DIR/$tfile
17242         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
17243         RC=$?
17244         if [ $RC -ne 0 ]; then
17245                 error "error, failed to take i_mutex, rc=$?"
17246         fi
17247         rm -f $DIR/$tfile
17248 }
17249 run_test 258a "verify i_mutex security behavior when suid attributes is set"
17250
17251 # Verify we do NOT take the i_mutex in the normal case
17252 test_258b() {
17253 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
17254         $LCTL set_param fail_loc=0x141d
17255         touch $DIR/$tfile
17256         chmod a+rwx $DIR
17257         chmod a+rw $DIR/$tfile
17258         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
17259         RC=$?
17260         if [ $RC -ne 0 ]; then
17261                 error "error, took i_mutex unnecessarily, rc=$?"
17262         fi
17263         rm -f $DIR/$tfile
17264
17265 }
17266 run_test 258b "verify i_mutex security behavior"
17267
17268 test_259() {
17269         local file=$DIR/$tfile
17270         local before
17271         local after
17272
17273         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
17274
17275         stack_trap "rm -f $file" EXIT
17276
17277         wait_delete_completed
17278         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17279         echo "before: $before"
17280
17281         $LFS setstripe -i 0 -c 1 $file
17282         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
17283         sync_all_data
17284         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17285         echo "after write: $after"
17286
17287 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
17288         do_facet ost1 $LCTL set_param fail_loc=0x2301
17289         $TRUNCATE $file 0
17290         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17291         echo "after truncate: $after"
17292
17293         stop ost1
17294         do_facet ost1 $LCTL set_param fail_loc=0
17295         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17296         sleep 2
17297         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17298         echo "after restart: $after"
17299         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
17300                 error "missing truncate?"
17301
17302         return 0
17303 }
17304 run_test 259 "crash at delayed truncate"
17305
17306 test_260() {
17307 #define OBD_FAIL_MDC_CLOSE               0x806
17308         $LCTL set_param fail_loc=0x80000806
17309         touch $DIR/$tfile
17310
17311 }
17312 run_test 260 "Check mdc_close fail"
17313
17314 ### Data-on-MDT sanity tests ###
17315 test_270a() {
17316         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17317                 skip "Need MDS version at least 2.10.55 for DoM"
17318
17319         # create DoM file
17320         local dom=$DIR/$tdir/dom_file
17321         local tmp=$DIR/$tdir/tmp_file
17322
17323         mkdir -p $DIR/$tdir
17324
17325         # basic checks for DoM component creation
17326         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
17327                 error "Can set MDT layout to non-first entry"
17328
17329         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
17330                 error "Can define multiple entries as MDT layout"
17331
17332         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
17333
17334         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
17335         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
17336         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
17337
17338         local mdtidx=$($LFS getstripe -m $dom)
17339         local mdtname=MDT$(printf %04x $mdtidx)
17340         local facet=mds$((mdtidx + 1))
17341         local space_check=1
17342
17343         # Skip free space checks with ZFS
17344         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
17345
17346         # write
17347         sync
17348         local size_tmp=$((65536 * 3))
17349         local mdtfree1=$(do_facet $facet \
17350                          lctl get_param -n osd*.*$mdtname.kbytesfree)
17351
17352         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
17353         # check also direct IO along write
17354         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
17355         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
17356         sync
17357         cmp $tmp $dom || error "file data is different"
17358         [ $(stat -c%s $dom) == $size_tmp ] ||
17359                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
17360         if [ $space_check == 1 ]; then
17361                 local mdtfree2=$(do_facet $facet \
17362                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
17363
17364                 # increase in usage from by $size_tmp
17365                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17366                         error "MDT free space wrong after write: " \
17367                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17368         fi
17369
17370         # truncate
17371         local size_dom=10000
17372
17373         $TRUNCATE $dom $size_dom
17374         [ $(stat -c%s $dom) == $size_dom ] ||
17375                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
17376         if [ $space_check == 1 ]; then
17377                 mdtfree1=$(do_facet $facet \
17378                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17379                 # decrease in usage from $size_tmp to new $size_dom
17380                 [ $(($mdtfree1 - $mdtfree2)) -ge \
17381                   $(((size_tmp - size_dom) / 1024)) ] ||
17382                         error "MDT free space is wrong after truncate: " \
17383                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
17384         fi
17385
17386         # append
17387         cat $tmp >> $dom
17388         sync
17389         size_dom=$((size_dom + size_tmp))
17390         [ $(stat -c%s $dom) == $size_dom ] ||
17391                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
17392         if [ $space_check == 1 ]; then
17393                 mdtfree2=$(do_facet $facet \
17394                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17395                 # increase in usage by $size_tmp from previous
17396                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17397                         error "MDT free space is wrong after append: " \
17398                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17399         fi
17400
17401         # delete
17402         rm $dom
17403         if [ $space_check == 1 ]; then
17404                 mdtfree1=$(do_facet $facet \
17405                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17406                 # decrease in usage by $size_dom from previous
17407                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
17408                         error "MDT free space is wrong after removal: " \
17409                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
17410         fi
17411
17412         # combined striping
17413         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
17414                 error "Can't create DoM + OST striping"
17415
17416         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
17417         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
17418         # check also direct IO along write
17419         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
17420         sync
17421         cmp $tmp $dom || error "file data is different"
17422         [ $(stat -c%s $dom) == $size_tmp ] ||
17423                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
17424         rm $dom $tmp
17425
17426         return 0
17427 }
17428 run_test 270a "DoM: basic functionality tests"
17429
17430 test_270b() {
17431         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17432                 skip "Need MDS version at least 2.10.55"
17433
17434         local dom=$DIR/$tdir/dom_file
17435         local max_size=1048576
17436
17437         mkdir -p $DIR/$tdir
17438         $LFS setstripe -E $max_size -L mdt $dom
17439
17440         # truncate over the limit
17441         $TRUNCATE $dom $(($max_size + 1)) &&
17442                 error "successful truncate over the maximum size"
17443         # write over the limit
17444         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
17445                 error "successful write over the maximum size"
17446         # append over the limit
17447         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
17448         echo "12345" >> $dom && error "successful append over the maximum size"
17449         rm $dom
17450
17451         return 0
17452 }
17453 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
17454
17455 test_270c() {
17456         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17457                 skip "Need MDS version at least 2.10.55"
17458
17459         mkdir -p $DIR/$tdir
17460         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17461
17462         # check files inherit DoM EA
17463         touch $DIR/$tdir/first
17464         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
17465                 error "bad pattern"
17466         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
17467                 error "bad stripe count"
17468         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
17469                 error "bad stripe size"
17470
17471         # check directory inherits DoM EA and uses it as default
17472         mkdir $DIR/$tdir/subdir
17473         touch $DIR/$tdir/subdir/second
17474         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
17475                 error "bad pattern in sub-directory"
17476         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
17477                 error "bad stripe count in sub-directory"
17478         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
17479                 error "bad stripe size in sub-directory"
17480         return 0
17481 }
17482 run_test 270c "DoM: DoM EA inheritance tests"
17483
17484 test_270d() {
17485         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17486                 skip "Need MDS version at least 2.10.55"
17487
17488         mkdir -p $DIR/$tdir
17489         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17490
17491         # inherit default DoM striping
17492         mkdir $DIR/$tdir/subdir
17493         touch $DIR/$tdir/subdir/f1
17494
17495         # change default directory striping
17496         $LFS setstripe -c 1 $DIR/$tdir/subdir
17497         touch $DIR/$tdir/subdir/f2
17498         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
17499                 error "wrong default striping in file 2"
17500         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
17501                 error "bad pattern in file 2"
17502         return 0
17503 }
17504 run_test 270d "DoM: change striping from DoM to RAID0"
17505
17506 test_270e() {
17507         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17508                 skip "Need MDS version at least 2.10.55"
17509
17510         mkdir -p $DIR/$tdir/dom
17511         mkdir -p $DIR/$tdir/norm
17512         DOMFILES=20
17513         NORMFILES=10
17514         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
17515         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
17516
17517         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
17518         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
17519
17520         # find DoM files by layout
17521         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
17522         [ $NUM -eq  $DOMFILES ] ||
17523                 error "lfs find -L: found $NUM, expected $DOMFILES"
17524         echo "Test 1: lfs find 20 DOM files by layout: OK"
17525
17526         # there should be 1 dir with default DOM striping
17527         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
17528         [ $NUM -eq  1 ] ||
17529                 error "lfs find -L: found $NUM, expected 1 dir"
17530         echo "Test 2: lfs find 1 DOM dir by layout: OK"
17531
17532         # find DoM files by stripe size
17533         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
17534         [ $NUM -eq  $DOMFILES ] ||
17535                 error "lfs find -S: found $NUM, expected $DOMFILES"
17536         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
17537
17538         # find files by stripe offset except DoM files
17539         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
17540         [ $NUM -eq  $NORMFILES ] ||
17541                 error "lfs find -i: found $NUM, expected $NORMFILES"
17542         echo "Test 5: lfs find no DOM files by stripe index: OK"
17543         return 0
17544 }
17545 run_test 270e "DoM: lfs find with DoM files test"
17546
17547 test_270f() {
17548         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17549                 skip "Need MDS version at least 2.10.55"
17550
17551         local mdtname=${FSNAME}-MDT0000-mdtlov
17552         local dom=$DIR/$tdir/dom_file
17553         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
17554                                                 lod.$mdtname.dom_stripesize)
17555         local dom_limit=131072
17556
17557         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
17558         local dom_current=$(do_facet mds1 $LCTL get_param -n \
17559                                                 lod.$mdtname.dom_stripesize)
17560         [ ${dom_limit} -eq ${dom_current} ] ||
17561                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
17562
17563         $LFS mkdir -i 0 -c 1 $DIR/$tdir
17564         $LFS setstripe -d $DIR/$tdir
17565         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
17566                 error "Can't set directory default striping"
17567
17568         # exceed maximum stripe size
17569         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
17570                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
17571         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
17572                 error "Able to create DoM component size more than LOD limit"
17573
17574         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
17575         dom_current=$(do_facet mds1 $LCTL get_param -n \
17576                                                 lod.$mdtname.dom_stripesize)
17577         [ 0 -eq ${dom_current} ] ||
17578                 error "Can't set zero DoM stripe limit"
17579         rm $dom
17580
17581         # attempt to create DoM file on server with disabled DoM should
17582         # remove DoM entry from layout and be succeed
17583         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
17584                 error "Can't create DoM file (DoM is disabled)"
17585         [ $($LFS getstripe -L $dom) == "mdt" ] &&
17586                 error "File has DoM component while DoM is disabled"
17587         rm $dom
17588
17589         # attempt to create DoM file with only DoM stripe should return error
17590         $LFS setstripe -E $dom_limit -L mdt $dom &&
17591                 error "Able to create DoM-only file while DoM is disabled"
17592
17593         # too low values to be aligned with smallest stripe size 64K
17594         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
17595         dom_current=$(do_facet mds1 $LCTL get_param -n \
17596                                                 lod.$mdtname.dom_stripesize)
17597         [ 30000 -eq ${dom_current} ] &&
17598                 error "Can set too small DoM stripe limit"
17599
17600         # 64K is a minimal stripe size in Lustre, expect limit of that size
17601         [ 65536 -eq ${dom_current} ] ||
17602                 error "Limit is not set to 64K but ${dom_current}"
17603
17604         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
17605         dom_current=$(do_facet mds1 $LCTL get_param -n \
17606                                                 lod.$mdtname.dom_stripesize)
17607         echo $dom_current
17608         [ 2147483648 -eq ${dom_current} ] &&
17609                 error "Can set too large DoM stripe limit"
17610
17611         do_facet mds1 $LCTL set_param -n \
17612                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
17613         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
17614                 error "Can't create DoM component size after limit change"
17615         do_facet mds1 $LCTL set_param -n \
17616                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
17617         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
17618                 error "Can't create DoM file after limit decrease"
17619         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
17620                 error "Can create big DoM component after limit decrease"
17621         touch ${dom}_def ||
17622                 error "Can't create file with old default layout"
17623
17624         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
17625         return 0
17626 }
17627 run_test 270f "DoM: maximum DoM stripe size checks"
17628
17629 test_271a() {
17630         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17631                 skip "Need MDS version at least 2.10.55"
17632
17633         local dom=$DIR/$tdir/dom
17634
17635         mkdir -p $DIR/$tdir
17636
17637         $LFS setstripe -E 1024K -L mdt $dom
17638
17639         lctl set_param -n mdc.*.stats=clear
17640         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
17641         cat $dom > /dev/null
17642         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
17643         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
17644         ls $dom
17645         rm -f $dom
17646 }
17647 run_test 271a "DoM: data is cached for read after write"
17648
17649 test_271b() {
17650         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17651                 skip "Need MDS version at least 2.10.55"
17652
17653         local dom=$DIR/$tdir/dom
17654
17655         mkdir -p $DIR/$tdir
17656
17657         $LFS setstripe -E 1024K -L mdt -E EOF $dom
17658
17659         lctl set_param -n mdc.*.stats=clear
17660         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
17661         cancel_lru_locks mdc
17662         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
17663         # second stat to check size is cached on client
17664         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
17665         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
17666         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
17667         rm -f $dom
17668 }
17669 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
17670
17671 test_271ba() {
17672         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17673                 skip "Need MDS version at least 2.10.55"
17674
17675         local dom=$DIR/$tdir/dom
17676
17677         mkdir -p $DIR/$tdir
17678
17679         $LFS setstripe -E 1024K -L mdt -E EOF $dom
17680
17681         lctl set_param -n mdc.*.stats=clear
17682         lctl set_param -n osc.*.stats=clear
17683         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
17684         cancel_lru_locks mdc
17685         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
17686         # second stat to check size is cached on client
17687         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
17688         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
17689         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
17690         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
17691         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
17692         rm -f $dom
17693 }
17694 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
17695
17696
17697 get_mdc_stats() {
17698         local mdtidx=$1
17699         local param=$2
17700         local mdt=MDT$(printf %04x $mdtidx)
17701
17702         if [ -z $param ]; then
17703                 lctl get_param -n mdc.*$mdt*.stats
17704         else
17705                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
17706         fi
17707 }
17708
17709 test_271c() {
17710         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17711                 skip "Need MDS version at least 2.10.55"
17712
17713         local dom=$DIR/$tdir/dom
17714
17715         mkdir -p $DIR/$tdir
17716
17717         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17718
17719         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
17720         local facet=mds$((mdtidx + 1))
17721
17722         cancel_lru_locks mdc
17723         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
17724         createmany -o $dom 1000
17725         lctl set_param -n mdc.*.stats=clear
17726         smalliomany -w $dom 1000 200
17727         get_mdc_stats $mdtidx
17728         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
17729         # Each file has 1 open, 1 IO enqueues, total 2000
17730         # but now we have also +1 getxattr for security.capability, total 3000
17731         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
17732         unlinkmany $dom 1000
17733
17734         cancel_lru_locks mdc
17735         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
17736         createmany -o $dom 1000
17737         lctl set_param -n mdc.*.stats=clear
17738         smalliomany -w $dom 1000 200
17739         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
17740         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
17741         # for OPEN and IO lock.
17742         [ $((enq - enq_2)) -ge 1000 ] ||
17743                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
17744         unlinkmany $dom 1000
17745         return 0
17746 }
17747 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
17748
17749 cleanup_271def_tests() {
17750         trap 0
17751         rm -f $1
17752 }
17753
17754 test_271d() {
17755         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
17756                 skip "Need MDS version at least 2.10.57"
17757
17758         local dom=$DIR/$tdir/dom
17759         local tmp=$TMP/$tfile
17760         trap "cleanup_271def_tests $tmp" EXIT
17761
17762         mkdir -p $DIR/$tdir
17763
17764         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17765
17766         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
17767
17768         cancel_lru_locks mdc
17769         dd if=/dev/urandom of=$tmp bs=1000 count=1
17770         dd if=$tmp of=$dom bs=1000 count=1
17771         cancel_lru_locks mdc
17772
17773         cat /etc/hosts >> $tmp
17774         lctl set_param -n mdc.*.stats=clear
17775
17776         # append data to the same file it should update local page
17777         echo "Append to the same page"
17778         cat /etc/hosts >> $dom
17779         local num=$(get_mdc_stats $mdtidx ost_read)
17780         local ra=$(get_mdc_stats $mdtidx req_active)
17781         local rw=$(get_mdc_stats $mdtidx req_waittime)
17782
17783         [ -z $num ] || error "$num READ RPC occured"
17784         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17785         echo "... DONE"
17786
17787         # compare content
17788         cmp $tmp $dom || error "file miscompare"
17789
17790         cancel_lru_locks mdc
17791         lctl set_param -n mdc.*.stats=clear
17792
17793         echo "Open and read file"
17794         cat $dom > /dev/null
17795         local num=$(get_mdc_stats $mdtidx ost_read)
17796         local ra=$(get_mdc_stats $mdtidx req_active)
17797         local rw=$(get_mdc_stats $mdtidx req_waittime)
17798
17799         [ -z $num ] || error "$num READ RPC occured"
17800         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17801         echo "... DONE"
17802
17803         # compare content
17804         cmp $tmp $dom || error "file miscompare"
17805
17806         return 0
17807 }
17808 run_test 271d "DoM: read on open (1K file in reply buffer)"
17809
17810 test_271f() {
17811         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
17812                 skip "Need MDS version at least 2.10.57"
17813
17814         local dom=$DIR/$tdir/dom
17815         local tmp=$TMP/$tfile
17816         trap "cleanup_271def_tests $tmp" EXIT
17817
17818         mkdir -p $DIR/$tdir
17819
17820         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17821
17822         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
17823
17824         cancel_lru_locks mdc
17825         dd if=/dev/urandom of=$tmp bs=200000 count=1
17826         dd if=$tmp of=$dom bs=200000 count=1
17827         cancel_lru_locks mdc
17828         cat /etc/hosts >> $tmp
17829         lctl set_param -n mdc.*.stats=clear
17830
17831         echo "Append to the same page"
17832         cat /etc/hosts >> $dom
17833         local num=$(get_mdc_stats $mdtidx ost_read)
17834         local ra=$(get_mdc_stats $mdtidx req_active)
17835         local rw=$(get_mdc_stats $mdtidx req_waittime)
17836
17837         [ -z $num ] || error "$num READ RPC occured"
17838         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17839         echo "... DONE"
17840
17841         # compare content
17842         cmp $tmp $dom || error "file miscompare"
17843
17844         cancel_lru_locks mdc
17845         lctl set_param -n mdc.*.stats=clear
17846
17847         echo "Open and read file"
17848         cat $dom > /dev/null
17849         local num=$(get_mdc_stats $mdtidx ost_read)
17850         local ra=$(get_mdc_stats $mdtidx req_active)
17851         local rw=$(get_mdc_stats $mdtidx req_waittime)
17852
17853         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
17854         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
17855         echo "... DONE"
17856
17857         # compare content
17858         cmp $tmp $dom || error "file miscompare"
17859
17860         return 0
17861 }
17862 run_test 271f "DoM: read on open (200K file and read tail)"
17863
17864 test_271g() {
17865         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
17866                 skip "Skipping due to old client or server version"
17867
17868         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
17869         # to get layout
17870         $CHECKSTAT -t file $DIR1/$tfile
17871
17872         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
17873         MULTIOP_PID=$!
17874         sleep 1
17875         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
17876         $LCTL set_param fail_loc=0x80000314
17877         rm $DIR1/$tfile || error "Unlink fails"
17878         RC=$?
17879         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
17880         [ $RC -eq 0 ] || error "Failed write to stale object"
17881 }
17882 run_test 271g "Discard DoM data vs client flush race"
17883
17884 test_272a() {
17885         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17886                 skip "Need MDS version at least 2.11.50"
17887
17888         local dom=$DIR/$tdir/dom
17889         mkdir -p $DIR/$tdir
17890
17891         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
17892         dd if=/dev/urandom of=$dom bs=512K count=1 ||
17893                 error "failed to write data into $dom"
17894         local old_md5=$(md5sum $dom)
17895
17896         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
17897                 error "failed to migrate to the same DoM component"
17898
17899         local new_md5=$(md5sum $dom)
17900
17901         [ "$old_md5" == "$new_md5" ] ||
17902                 error "md5sum differ: $old_md5, $new_md5"
17903
17904         [ $($LFS getstripe -c $dom) -eq 2 ] ||
17905                 error "migrate stripe count bad: $(LFS getstripe -c $dom) != 2"
17906 }
17907 run_test 272a "DoM migration: new layout with the same DOM component"
17908
17909 test_272b() {
17910         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17911                 skip "Need MDS version at least 2.11.50"
17912
17913         local dom=$DIR/$tdir/dom
17914         mkdir -p $DIR/$tdir
17915         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
17916
17917         local mdtidx=$($LFS getstripe -m $dom)
17918         local mdtname=MDT$(printf %04x $mdtidx)
17919         local facet=mds$((mdtidx + 1))
17920
17921         local mdtfree1=$(do_facet $facet \
17922                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17923         dd if=/dev/urandom of=$dom bs=2M count=1 ||
17924                 error "failed to write data into $dom"
17925         local old_md5=$(md5sum $dom)
17926         cancel_lru_locks mdc
17927         local mdtfree1=$(do_facet $facet \
17928                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17929
17930         $LFS migrate -c2 $dom ||
17931                 error "failed to migrate to the new composite layout"
17932         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
17933                 error "MDT stripe was not removed"
17934
17935         cancel_lru_locks mdc
17936         local new_md5=$(md5sum $dom)
17937         [ "$old_md5" != "$new_md5" ] &&
17938                 error "$old_md5 != $new_md5"
17939
17940         # Skip free space checks with ZFS
17941         if [ "$(facet_fstype $facet)" != "zfs" ]; then
17942                 local mdtfree2=$(do_facet $facet \
17943                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17944                 [ $mdtfree2 -gt $mdtfree1 ] ||
17945                         error "MDT space is not freed after migration"
17946         fi
17947         return 0
17948 }
17949 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
17950
17951 test_272c() {
17952         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17953                 skip "Need MDS version at least 2.11.50"
17954
17955         local dom=$DIR/$tdir/$tfile
17956         mkdir -p $DIR/$tdir
17957         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
17958
17959         local mdtidx=$($LFS getstripe -m $dom)
17960         local mdtname=MDT$(printf %04x $mdtidx)
17961         local facet=mds$((mdtidx + 1))
17962
17963         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
17964                 error "failed to write data into $dom"
17965         local old_md5=$(md5sum $dom)
17966         cancel_lru_locks mdc
17967         local mdtfree1=$(do_facet $facet \
17968                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17969
17970         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
17971                 error "failed to migrate to the new composite layout"
17972         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
17973                 error "MDT stripe was not removed"
17974
17975         cancel_lru_locks mdc
17976         local new_md5=$(md5sum $dom)
17977         [ "$old_md5" != "$new_md5" ] &&
17978                 error "$old_md5 != $new_md5"
17979
17980         # Skip free space checks with ZFS
17981         if [ "$(facet_fstype $facet)" != "zfs" ]; then
17982                 local mdtfree2=$(do_facet $facet \
17983                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17984                 [ $mdtfree2 -gt $mdtfree1 ] ||
17985                         error "MDS space is not freed after migration"
17986         fi
17987         return 0
17988 }
17989 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
17990
17991 test_273a() {
17992         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
17993                 skip "Need MDS version at least 2.11.50"
17994
17995         # Layout swap cannot be done if either file has DOM component,
17996         # this will never be supported, migration should be used instead
17997
17998         local dom=$DIR/$tdir/$tfile
17999         mkdir -p $DIR/$tdir
18000
18001         $LFS setstripe -c2 ${dom}_plain
18002         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
18003         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
18004                 error "can swap layout with DoM component"
18005         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
18006                 error "can swap layout with DoM component"
18007
18008         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
18009         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
18010                 error "can swap layout with DoM component"
18011         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
18012                 error "can swap layout with DoM component"
18013         return 0
18014 }
18015 run_test 273a "DoM: layout swapping should fail with DOM"
18016
18017 test_275() {
18018         remote_ost_nodsh && skip "remote OST with nodsh"
18019         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
18020                 skip "Need OST version >= 2.10.57"
18021
18022         local file=$DIR/$tfile
18023         local oss
18024
18025         oss=$(comma_list $(osts_nodes))
18026
18027         dd if=/dev/urandom of=$file bs=1M count=2 ||
18028                 error "failed to create a file"
18029         cancel_lru_locks osc
18030
18031         #lock 1
18032         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18033                 error "failed to read a file"
18034
18035 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
18036         $LCTL set_param fail_loc=0x8000031f
18037
18038         cancel_lru_locks osc &
18039         sleep 1
18040
18041 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
18042         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
18043         #IO takes another lock, but matches the PENDING one
18044         #and places it to the IO RPC
18045         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18046                 error "failed to read a file with PENDING lock"
18047 }
18048 run_test 275 "Read on a canceled duplicate lock"
18049
18050 test_276() {
18051         remote_ost_nodsh && skip "remote OST with nodsh"
18052         local pid
18053
18054         do_facet ost1 "(while true; do \
18055                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
18056                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
18057         pid=$!
18058
18059         for LOOP in $(seq 20); do
18060                 stop ost1
18061                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
18062         done
18063         kill -9 $pid
18064         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
18065                 rm $TMP/sanity_276_pid"
18066 }
18067 run_test 276 "Race between mount and obd_statfs"
18068
18069 cleanup_test_300() {
18070         trap 0
18071         umask $SAVE_UMASK
18072 }
18073 test_striped_dir() {
18074         local mdt_index=$1
18075         local stripe_count
18076         local stripe_index
18077
18078         mkdir -p $DIR/$tdir
18079
18080         SAVE_UMASK=$(umask)
18081         trap cleanup_test_300 RETURN EXIT
18082
18083         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
18084                                                 $DIR/$tdir/striped_dir ||
18085                 error "set striped dir error"
18086
18087         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
18088         [ "$mode" = "755" ] || error "expect 755 got $mode"
18089
18090         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
18091                 error "getdirstripe failed"
18092         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
18093         if [ "$stripe_count" != "2" ]; then
18094                 error "1:stripe_count is $stripe_count, expect 2"
18095         fi
18096         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
18097         if [ "$stripe_count" != "2" ]; then
18098                 error "2:stripe_count is $stripe_count, expect 2"
18099         fi
18100
18101         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
18102         if [ "$stripe_index" != "$mdt_index" ]; then
18103                 error "stripe_index is $stripe_index, expect $mdt_index"
18104         fi
18105
18106         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18107                 error "nlink error after create striped dir"
18108
18109         mkdir $DIR/$tdir/striped_dir/a
18110         mkdir $DIR/$tdir/striped_dir/b
18111
18112         stat $DIR/$tdir/striped_dir/a ||
18113                 error "create dir under striped dir failed"
18114         stat $DIR/$tdir/striped_dir/b ||
18115                 error "create dir under striped dir failed"
18116
18117         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
18118                 error "nlink error after mkdir"
18119
18120         rmdir $DIR/$tdir/striped_dir/a
18121         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
18122                 error "nlink error after rmdir"
18123
18124         rmdir $DIR/$tdir/striped_dir/b
18125         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18126                 error "nlink error after rmdir"
18127
18128         chattr +i $DIR/$tdir/striped_dir
18129         createmany -o $DIR/$tdir/striped_dir/f 10 &&
18130                 error "immutable flags not working under striped dir!"
18131         chattr -i $DIR/$tdir/striped_dir
18132
18133         rmdir $DIR/$tdir/striped_dir ||
18134                 error "rmdir striped dir error"
18135
18136         cleanup_test_300
18137
18138         true
18139 }
18140
18141 test_300a() {
18142         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18143                 skip "skipped for lustre < 2.7.0"
18144         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18145         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18146
18147         test_striped_dir 0 || error "failed on striped dir on MDT0"
18148         test_striped_dir 1 || error "failed on striped dir on MDT0"
18149 }
18150 run_test 300a "basic striped dir sanity test"
18151
18152 test_300b() {
18153         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18154                 skip "skipped for lustre < 2.7.0"
18155         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18156         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18157
18158         local i
18159         local mtime1
18160         local mtime2
18161         local mtime3
18162
18163         test_mkdir $DIR/$tdir || error "mkdir fail"
18164         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18165                 error "set striped dir error"
18166         for i in {0..9}; do
18167                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
18168                 sleep 1
18169                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
18170                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
18171                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
18172                 sleep 1
18173                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
18174                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
18175                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
18176         done
18177         true
18178 }
18179 run_test 300b "check ctime/mtime for striped dir"
18180
18181 test_300c() {
18182         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18183                 skip "skipped for lustre < 2.7.0"
18184         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18185         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18186
18187         local file_count
18188
18189         mkdir -p $DIR/$tdir
18190         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
18191                 error "set striped dir error"
18192
18193         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
18194                 error "chown striped dir failed"
18195
18196         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
18197                 error "create 5k files failed"
18198
18199         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
18200
18201         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
18202
18203         rm -rf $DIR/$tdir
18204 }
18205 run_test 300c "chown && check ls under striped directory"
18206
18207 test_300d() {
18208         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18209                 skip "skipped for lustre < 2.7.0"
18210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18211         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18212
18213         local stripe_count
18214         local file
18215
18216         mkdir -p $DIR/$tdir
18217         $LFS setstripe -c 2 $DIR/$tdir
18218
18219         #local striped directory
18220         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18221                 error "set striped dir error"
18222         createmany -o $DIR/$tdir/striped_dir/f 10 ||
18223                 error "create 10 files failed"
18224
18225         #remote striped directory
18226         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
18227                 error "set striped dir error"
18228         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
18229                 error "create 10 files failed"
18230
18231         for file in $(find $DIR/$tdir); do
18232                 stripe_count=$($LFS getstripe -c $file)
18233                 [ $stripe_count -eq 2 ] ||
18234                         error "wrong stripe $stripe_count for $file"
18235         done
18236
18237         rm -rf $DIR/$tdir
18238 }
18239 run_test 300d "check default stripe under striped directory"
18240
18241 test_300e() {
18242         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18243                 skip "Need MDS version at least 2.7.55"
18244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18245         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18246
18247         local stripe_count
18248         local file
18249
18250         mkdir -p $DIR/$tdir
18251
18252         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18253                 error "set striped dir error"
18254
18255         touch $DIR/$tdir/striped_dir/a
18256         touch $DIR/$tdir/striped_dir/b
18257         touch $DIR/$tdir/striped_dir/c
18258
18259         mkdir $DIR/$tdir/striped_dir/dir_a
18260         mkdir $DIR/$tdir/striped_dir/dir_b
18261         mkdir $DIR/$tdir/striped_dir/dir_c
18262
18263         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
18264                 error "set striped adir under striped dir error"
18265
18266         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
18267                 error "set striped bdir under striped dir error"
18268
18269         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
18270                 error "set striped cdir under striped dir error"
18271
18272         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
18273                 error "rename dir under striped dir fails"
18274
18275         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
18276                 error "rename dir under different stripes fails"
18277
18278         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
18279                 error "rename file under striped dir should succeed"
18280
18281         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
18282                 error "rename dir under striped dir should succeed"
18283
18284         rm -rf $DIR/$tdir
18285 }
18286 run_test 300e "check rename under striped directory"
18287
18288 test_300f() {
18289         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18290         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18291         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18292                 skip "Need MDS version at least 2.7.55"
18293
18294         local stripe_count
18295         local file
18296
18297         rm -rf $DIR/$tdir
18298         mkdir -p $DIR/$tdir
18299
18300         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18301                 error "set striped dir error"
18302
18303         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
18304                 error "set striped dir error"
18305
18306         touch $DIR/$tdir/striped_dir/a
18307         mkdir $DIR/$tdir/striped_dir/dir_a
18308         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
18309                 error "create striped dir under striped dir fails"
18310
18311         touch $DIR/$tdir/striped_dir1/b
18312         mkdir $DIR/$tdir/striped_dir1/dir_b
18313         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
18314                 error "create striped dir under striped dir fails"
18315
18316         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
18317                 error "rename dir under different striped dir should fail"
18318
18319         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
18320                 error "rename striped dir under diff striped dir should fail"
18321
18322         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
18323                 error "rename file under diff striped dirs fails"
18324
18325         rm -rf $DIR/$tdir
18326 }
18327 run_test 300f "check rename cross striped directory"
18328
18329 test_300_check_default_striped_dir()
18330 {
18331         local dirname=$1
18332         local default_count=$2
18333         local default_index=$3
18334         local stripe_count
18335         local stripe_index
18336         local dir_stripe_index
18337         local dir
18338
18339         echo "checking $dirname $default_count $default_index"
18340         $LFS setdirstripe -D -c $default_count -i $default_index \
18341                                 -t all_char $DIR/$tdir/$dirname ||
18342                 error "set default stripe on striped dir error"
18343         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
18344         [ $stripe_count -eq $default_count ] ||
18345                 error "expect $default_count get $stripe_count for $dirname"
18346
18347         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
18348         [ $stripe_index -eq $default_index ] ||
18349                 error "expect $default_index get $stripe_index for $dirname"
18350
18351         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
18352                                                 error "create dirs failed"
18353
18354         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
18355         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
18356         for dir in $(find $DIR/$tdir/$dirname/*); do
18357                 stripe_count=$($LFS getdirstripe -c $dir)
18358                 [ $stripe_count -eq $default_count ] ||
18359                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
18360                 error "stripe count $default_count != $stripe_count for $dir"
18361
18362                 stripe_index=$($LFS getdirstripe -i $dir)
18363                 [ $default_index -eq -1 ] ||
18364                         [ $stripe_index -eq $default_index ] ||
18365                         error "$stripe_index != $default_index for $dir"
18366
18367                 #check default stripe
18368                 stripe_count=$($LFS getdirstripe -D -c $dir)
18369                 [ $stripe_count -eq $default_count ] ||
18370                 error "default count $default_count != $stripe_count for $dir"
18371
18372                 stripe_index=$($LFS getdirstripe -D -i $dir)
18373                 [ $stripe_index -eq $default_index ] ||
18374                 error "default index $default_index != $stripe_index for $dir"
18375         done
18376         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
18377 }
18378
18379 test_300g() {
18380         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18381         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18382                 skip "Need MDS version at least 2.7.55"
18383
18384         local dir
18385         local stripe_count
18386         local stripe_index
18387
18388         mkdir $DIR/$tdir
18389         mkdir $DIR/$tdir/normal_dir
18390
18391         #Checking when client cache stripe index
18392         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
18393         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
18394                 error "create striped_dir failed"
18395
18396         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
18397                 error "create dir0 fails"
18398         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
18399         [ $stripe_index -eq 0 ] ||
18400                 error "dir0 expect index 0 got $stripe_index"
18401
18402         mkdir $DIR/$tdir/striped_dir/dir1 ||
18403                 error "create dir1 fails"
18404         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
18405         [ $stripe_index -eq 1 ] ||
18406                 error "dir1 expect index 1 got $stripe_index"
18407
18408         #check default stripe count/stripe index
18409         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
18410         test_300_check_default_striped_dir normal_dir 1 0
18411         test_300_check_default_striped_dir normal_dir 2 1
18412         test_300_check_default_striped_dir normal_dir 2 -1
18413
18414         #delete default stripe information
18415         echo "delete default stripeEA"
18416         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
18417                 error "set default stripe on striped dir error"
18418
18419         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
18420         for dir in $(find $DIR/$tdir/normal_dir/*); do
18421                 stripe_count=$($LFS getdirstripe -c $dir)
18422                 [ $stripe_count -eq 0 ] ||
18423                         error "expect 1 get $stripe_count for $dir"
18424                 stripe_index=$($LFS getdirstripe -i $dir)
18425                 [ $stripe_index -eq 0 ] ||
18426                         error "expect 0 get $stripe_index for $dir"
18427         done
18428 }
18429 run_test 300g "check default striped directory for normal directory"
18430
18431 test_300h() {
18432         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18433         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18434                 skip "Need MDS version at least 2.7.55"
18435
18436         local dir
18437         local stripe_count
18438
18439         mkdir $DIR/$tdir
18440         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18441                 error "set striped dir error"
18442
18443         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
18444         test_300_check_default_striped_dir striped_dir 1 0
18445         test_300_check_default_striped_dir striped_dir 2 1
18446         test_300_check_default_striped_dir striped_dir 2 -1
18447
18448         #delete default stripe information
18449         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
18450                 error "set default stripe on striped dir error"
18451
18452         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
18453         for dir in $(find $DIR/$tdir/striped_dir/*); do
18454                 stripe_count=$($LFS getdirstripe -c $dir)
18455                 [ $stripe_count -eq 0 ] ||
18456                         error "expect 1 get $stripe_count for $dir"
18457         done
18458 }
18459 run_test 300h "check default striped directory for striped directory"
18460
18461 test_300i() {
18462         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18463         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18464         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18465                 skip "Need MDS version at least 2.7.55"
18466
18467         local stripe_count
18468         local file
18469
18470         mkdir $DIR/$tdir
18471
18472         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18473                 error "set striped dir error"
18474
18475         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18476                 error "create files under striped dir failed"
18477
18478         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
18479                 error "set striped hashdir error"
18480
18481         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
18482                 error "create dir0 under hash dir failed"
18483         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
18484                 error "create dir1 under hash dir failed"
18485
18486         # unfortunately, we need to umount to clear dir layout cache for now
18487         # once we fully implement dir layout, we can drop this
18488         umount_client $MOUNT || error "umount failed"
18489         mount_client $MOUNT || error "mount failed"
18490
18491         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
18492         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
18493         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
18494
18495         #set the stripe to be unknown hash type
18496         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
18497         $LCTL set_param fail_loc=0x1901
18498         for ((i = 0; i < 10; i++)); do
18499                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
18500                         error "stat f-$i failed"
18501                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
18502         done
18503
18504         touch $DIR/$tdir/striped_dir/f0 &&
18505                 error "create under striped dir with unknown hash should fail"
18506
18507         $LCTL set_param fail_loc=0
18508
18509         umount_client $MOUNT || error "umount failed"
18510         mount_client $MOUNT || error "mount failed"
18511
18512         return 0
18513 }
18514 run_test 300i "client handle unknown hash type striped directory"
18515
18516 test_300j() {
18517         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18518         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18519         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18520                 skip "Need MDS version at least 2.7.55"
18521
18522         local stripe_count
18523         local file
18524
18525         mkdir $DIR/$tdir
18526
18527         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
18528         $LCTL set_param fail_loc=0x1702
18529         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18530                 error "set striped dir error"
18531
18532         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18533                 error "create files under striped dir failed"
18534
18535         $LCTL set_param fail_loc=0
18536
18537         rm -rf $DIR/$tdir || error "unlink striped dir fails"
18538
18539         return 0
18540 }
18541 run_test 300j "test large update record"
18542
18543 test_300k() {
18544         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18545         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18546         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18547                 skip "Need MDS version at least 2.7.55"
18548
18549         # this test needs a huge transaction
18550         local kb
18551         kb=$(do_facet $SINGLEMDS lctl get_param -n osd*.lustre-MDT0000.kbytestotal)
18552         [ $kb -lt $((1024*1024)) ] && skip "too small mds: $kb"
18553
18554         local stripe_count
18555         local file
18556
18557         mkdir $DIR/$tdir
18558
18559         #define OBD_FAIL_LARGE_STRIPE   0x1703
18560         $LCTL set_param fail_loc=0x1703
18561         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
18562                 error "set striped dir error"
18563         $LCTL set_param fail_loc=0
18564
18565         $LFS getdirstripe $DIR/$tdir/striped_dir ||
18566                 error "getstripeddir fails"
18567         rm -rf $DIR/$tdir/striped_dir ||
18568                 error "unlink striped dir fails"
18569
18570         return 0
18571 }
18572 run_test 300k "test large striped directory"
18573
18574 test_300l() {
18575         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18576         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18577         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18578                 skip "Need MDS version at least 2.7.55"
18579
18580         local stripe_index
18581
18582         test_mkdir -p $DIR/$tdir/striped_dir
18583         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
18584                         error "chown $RUNAS_ID failed"
18585         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
18586                 error "set default striped dir failed"
18587
18588         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
18589         $LCTL set_param fail_loc=0x80000158
18590         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
18591
18592         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
18593         [ $stripe_index -eq 1 ] ||
18594                 error "expect 1 get $stripe_index for $dir"
18595 }
18596 run_test 300l "non-root user to create dir under striped dir with stale layout"
18597
18598 test_300m() {
18599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18600         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
18601         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18602                 skip "Need MDS version at least 2.7.55"
18603
18604         mkdir -p $DIR/$tdir/striped_dir
18605         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
18606                 error "set default stripes dir error"
18607
18608         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
18609
18610         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
18611         [ $stripe_count -eq 0 ] ||
18612                         error "expect 0 get $stripe_count for a"
18613
18614         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
18615                 error "set default stripes dir error"
18616
18617         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
18618
18619         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
18620         [ $stripe_count -eq 0 ] ||
18621                         error "expect 0 get $stripe_count for b"
18622
18623         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
18624                 error "set default stripes dir error"
18625
18626         mkdir $DIR/$tdir/striped_dir/c &&
18627                 error "default stripe_index is invalid, mkdir c should fails"
18628
18629         rm -rf $DIR/$tdir || error "rmdir fails"
18630 }
18631 run_test 300m "setstriped directory on single MDT FS"
18632
18633 cleanup_300n() {
18634         local list=$(comma_list $(mdts_nodes))
18635
18636         trap 0
18637         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18638 }
18639
18640 test_300n() {
18641         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18642         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18643         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18644                 skip "Need MDS version at least 2.7.55"
18645         remote_mds_nodsh && skip "remote MDS with nodsh"
18646
18647         local stripe_index
18648         local list=$(comma_list $(mdts_nodes))
18649
18650         trap cleanup_300n RETURN EXIT
18651         mkdir -p $DIR/$tdir
18652         chmod 777 $DIR/$tdir
18653         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
18654                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
18655                 error "create striped dir succeeds with gid=0"
18656
18657         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
18658         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
18659                 error "create striped dir fails with gid=-1"
18660
18661         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18662         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
18663                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
18664                 error "set default striped dir succeeds with gid=0"
18665
18666
18667         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
18668         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
18669                 error "set default striped dir fails with gid=-1"
18670
18671
18672         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
18673         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
18674                                         error "create test_dir fails"
18675         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
18676                                         error "create test_dir1 fails"
18677         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
18678                                         error "create test_dir2 fails"
18679         cleanup_300n
18680 }
18681 run_test 300n "non-root user to create dir under striped dir with default EA"
18682
18683 test_300o() {
18684         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18685         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18686         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18687                 skip "Need MDS version at least 2.7.55"
18688
18689         local numfree1
18690         local numfree2
18691
18692         mkdir -p $DIR/$tdir
18693
18694         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
18695         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
18696         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
18697                 skip "not enough free inodes $numfree1 $numfree2"
18698         fi
18699
18700         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
18701         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
18702         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
18703                 skip "not enough free space $numfree1 $numfree2"
18704         fi
18705
18706         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
18707                 error "setdirstripe fails"
18708
18709         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
18710                 error "create dirs fails"
18711
18712         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
18713         ls $DIR/$tdir/striped_dir > /dev/null ||
18714                 error "ls striped dir fails"
18715         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
18716                 error "unlink big striped dir fails"
18717 }
18718 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
18719
18720 test_300p() {
18721         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18722         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18723         remote_mds_nodsh && skip "remote MDS with nodsh"
18724
18725         mkdir -p $DIR/$tdir
18726
18727         #define OBD_FAIL_OUT_ENOSPC     0x1704
18728         do_facet mds2 lctl set_param fail_loc=0x80001704
18729         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
18730                  && error "create striped directory should fail"
18731
18732         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
18733
18734         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
18735         true
18736 }
18737 run_test 300p "create striped directory without space"
18738
18739 test_300q() {
18740         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18741         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18742
18743         local fd=$(free_fd)
18744         local cmd="exec $fd<$tdir"
18745         cd $DIR
18746         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
18747         eval $cmd
18748         cmd="exec $fd<&-"
18749         trap "eval $cmd" EXIT
18750         cd $tdir || error "cd $tdir fails"
18751         rmdir  ../$tdir || error "rmdir $tdir fails"
18752         mkdir local_dir && error "create dir succeeds"
18753         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
18754         eval $cmd
18755         return 0
18756 }
18757 run_test 300q "create remote directory under orphan directory"
18758
18759 test_300r() {
18760         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
18761                 skip "Need MDS version at least 2.7.55" && return
18762         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
18763
18764         mkdir $DIR/$tdir
18765
18766         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
18767                 error "set striped dir error"
18768
18769         $LFS getdirstripe $DIR/$tdir/striped_dir ||
18770                 error "getstripeddir fails"
18771
18772         local stripe_count
18773         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
18774                       awk '/lmv_stripe_count:/ { print $2 }')
18775
18776         [ $MDSCOUNT -ne $stripe_count ] &&
18777                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
18778
18779         rm -rf $DIR/$tdir/striped_dir ||
18780                 error "unlink striped dir fails"
18781 }
18782 run_test 300r "test -1 striped directory"
18783
18784 prepare_remote_file() {
18785         mkdir $DIR/$tdir/src_dir ||
18786                 error "create remote source failed"
18787
18788         cp /etc/hosts $DIR/$tdir/src_dir/a ||
18789                  error "cp to remote source failed"
18790         touch $DIR/$tdir/src_dir/a
18791
18792         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
18793                 error "create remote target dir failed"
18794
18795         touch $DIR/$tdir/tgt_dir/b
18796
18797         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
18798                 error "rename dir cross MDT failed!"
18799
18800         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
18801                 error "src_child still exists after rename"
18802
18803         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
18804                 error "missing file(a) after rename"
18805
18806         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
18807                 error "diff after rename"
18808 }
18809
18810 test_310a() {
18811         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
18812         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18813
18814         local remote_file=$DIR/$tdir/tgt_dir/b
18815
18816         mkdir -p $DIR/$tdir
18817
18818         prepare_remote_file || error "prepare remote file failed"
18819
18820         #open-unlink file
18821         $OPENUNLINK $remote_file $remote_file ||
18822                 error "openunlink $remote_file failed"
18823         $CHECKSTAT -a $remote_file || error "$remote_file exists"
18824 }
18825 run_test 310a "open unlink remote file"
18826
18827 test_310b() {
18828         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
18829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18830
18831         local remote_file=$DIR/$tdir/tgt_dir/b
18832
18833         mkdir -p $DIR/$tdir
18834
18835         prepare_remote_file || error "prepare remote file failed"
18836
18837         ln $remote_file $DIR/$tfile || error "link failed for remote file"
18838         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
18839         $CHECKSTAT -t file $remote_file || error "check file failed"
18840 }
18841 run_test 310b "unlink remote file with multiple links while open"
18842
18843 test_310c() {
18844         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18845         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
18846
18847         local remote_file=$DIR/$tdir/tgt_dir/b
18848
18849         mkdir -p $DIR/$tdir
18850
18851         prepare_remote_file || error "prepare remote file failed"
18852
18853         ln $remote_file $DIR/$tfile || error "link failed for remote file"
18854         multiop_bg_pause $remote_file O_uc ||
18855                         error "mulitop failed for remote file"
18856         MULTIPID=$!
18857         $MULTIOP $DIR/$tfile Ouc
18858         kill -USR1 $MULTIPID
18859         wait $MULTIPID
18860 }
18861 run_test 310c "open-unlink remote file with multiple links"
18862
18863 #LU-4825
18864 test_311() {
18865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18866         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
18867         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
18868                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
18869         remote_mds_nodsh && skip "remote MDS with nodsh"
18870
18871         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
18872         local mdts=$(comma_list $(mdts_nodes))
18873
18874         mkdir -p $DIR/$tdir
18875         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18876         createmany -o $DIR/$tdir/$tfile. 1000
18877
18878         # statfs data is not real time, let's just calculate it
18879         old_iused=$((old_iused + 1000))
18880
18881         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
18882                         osp.*OST0000*MDT0000.create_count")
18883         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
18884                                 osp.*OST0000*MDT0000.max_create_count")
18885         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
18886
18887         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
18888         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
18889         [ $index -ne 0 ] || error "$tfile stripe index is 0"
18890
18891         unlinkmany $DIR/$tdir/$tfile. 1000
18892
18893         do_nodes $mdts "$LCTL set_param -n \
18894                         osp.*OST0000*.max_create_count=$max_count"
18895         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
18896                 do_nodes $mdts "$LCTL set_param -n \
18897                                 osp.*OST0000*.create_count=$count"
18898         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
18899                         grep "=0" && error "create_count is zero"
18900
18901         local new_iused
18902         for i in $(seq 120); do
18903                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
18904                 # system may be too busy to destroy all objs in time, use
18905                 # a somewhat small value to not fail autotest
18906                 [ $((old_iused - new_iused)) -gt 400 ] && break
18907                 sleep 1
18908         done
18909
18910         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
18911         [ $((old_iused - new_iused)) -gt 400 ] ||
18912                 error "objs not destroyed after unlink"
18913 }
18914 run_test 311 "disable OSP precreate, and unlink should destroy objs"
18915
18916 zfs_oid_to_objid()
18917 {
18918         local ost=$1
18919         local objid=$2
18920
18921         local vdevdir=$(dirname $(facet_vdevice $ost))
18922         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
18923         local zfs_zapid=$(do_facet $ost $cmd |
18924                           grep -w "/O/0/d$((objid%32))" -C 5 |
18925                           awk '/Object/{getline; print $1}')
18926         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
18927                           awk "/$objid = /"'{printf $3}')
18928
18929         echo $zfs_objid
18930 }
18931
18932 zfs_object_blksz() {
18933         local ost=$1
18934         local objid=$2
18935
18936         local vdevdir=$(dirname $(facet_vdevice $ost))
18937         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
18938         local blksz=$(do_facet $ost $cmd $objid |
18939                       awk '/dblk/{getline; printf $4}')
18940
18941         case "${blksz: -1}" in
18942                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
18943                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
18944                 *) ;;
18945         esac
18946
18947         echo $blksz
18948 }
18949
18950 test_312() { # LU-4856
18951         remote_ost_nodsh && skip "remote OST with nodsh"
18952         [ "$ost1_FSTYPE" = "zfs" ] ||
18953                 skip_env "the test only applies to zfs"
18954
18955         local max_blksz=$(do_facet ost1 \
18956                           $ZFS get -p recordsize $(facet_device ost1) |
18957                           awk '!/VALUE/{print $3}')
18958
18959         # to make life a little bit easier
18960         $LFS mkdir -c 1 -i 0 $DIR/$tdir
18961         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18962
18963         local tf=$DIR/$tdir/$tfile
18964         touch $tf
18965         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18966
18967         # Get ZFS object id
18968         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18969         # block size change by sequential overwrite
18970         local bs
18971
18972         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
18973                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
18974
18975                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
18976                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
18977         done
18978         rm -f $tf
18979
18980         # block size change by sequential append write
18981         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
18982         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
18983         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
18984         local count
18985
18986         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
18987                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
18988                         oflag=sync conv=notrunc
18989
18990                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
18991                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
18992                         error "blksz error, actual $blksz, " \
18993                                 "expected: 2 * $count * $PAGE_SIZE"
18994         done
18995         rm -f $tf
18996
18997         # random write
18998         touch $tf
18999         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19000         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19001
19002         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
19003         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19004         [ $blksz -eq $PAGE_SIZE ] ||
19005                 error "blksz error: $blksz, expected: $PAGE_SIZE"
19006
19007         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
19008         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19009         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
19010
19011         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
19012         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19013         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
19014 }
19015 run_test 312 "make sure ZFS adjusts its block size by write pattern"
19016
19017 test_313() {
19018         remote_ost_nodsh && skip "remote OST with nodsh"
19019
19020         local file=$DIR/$tfile
19021
19022         rm -f $file
19023         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
19024
19025         # define OBD_FAIL_TGT_RCVD_EIO           0x720
19026         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19027         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
19028                 error "write should failed"
19029         do_facet ost1 "$LCTL set_param fail_loc=0"
19030         rm -f $file
19031 }
19032 run_test 313 "io should fail after last_rcvd update fail"
19033
19034 test_314() {
19035         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19036
19037         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
19038         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19039         rm -f $DIR/$tfile
19040         wait_delete_completed
19041         do_facet ost1 "$LCTL set_param fail_loc=0"
19042 }
19043 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
19044
19045 test_315() { # LU-618
19046         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
19047
19048         local file=$DIR/$tfile
19049         rm -f $file
19050
19051         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
19052                 error "multiop file write failed"
19053         $MULTIOP $file oO_RDONLY:r4063232_c &
19054         PID=$!
19055
19056         sleep 2
19057
19058         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
19059         kill -USR1 $PID
19060
19061         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
19062         rm -f $file
19063 }
19064 run_test 315 "read should be accounted"
19065
19066 test_316() {
19067         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19068         large_xattr_enabled || skip_env "ea_inode feature disabled"
19069
19070         rm -rf $DIR/$tdir/d
19071         mkdir -p $DIR/$tdir/d
19072         chown nobody $DIR/$tdir/d
19073         touch $DIR/$tdir/d/file
19074
19075         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
19076 }
19077 run_test 316 "lfs mv"
19078
19079 test_317() {
19080         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
19081                 skip "Need MDS version at least 2.11.53"
19082         local trunc_sz
19083         local grant_blk_size
19084
19085         if [ "$(facet_fstype $facet)" == "zfs" ]; then
19086                 skip "LU-10370: no implementation for ZFS" && return
19087         fi
19088
19089         stack_trap "rm -f $DIR/$tfile" EXIT
19090         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
19091                         awk '/grant_block_size:/ { print $2; exit; }')
19092         #
19093         # Create File of size 5M. Truncate it to below size's and verify
19094         # blocks count.
19095         #
19096         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
19097                 error "Create file : $DIR/$tfile"
19098
19099         for trunc_sz in 2097152 4097 4000 509 0; do
19100                 $TRUNCATE $DIR/$tfile $trunc_sz ||
19101                         error "truncate $tfile to $trunc_sz failed"
19102                 local sz=$(stat --format=%s $DIR/$tfile)
19103                 local blk=$(stat --format=%b $DIR/$tfile)
19104                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
19105                                      grant_blk_size) * 8))
19106
19107                 if [[ $blk -ne $trunc_blk ]]; then
19108                         $(which stat) $DIR/$tfile
19109                         error "Expected Block $trunc_blk got $blk for $tfile"
19110                 fi
19111
19112                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
19113                         error "Expected Size $trunc_sz got $sz for $tfile"
19114         done
19115
19116         #
19117         # sparse file test
19118         # Create file with a hole and write actual two blocks. Block count
19119         # must be 16.
19120         #
19121         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
19122                 conv=fsync || error "Create file : $DIR/$tfile"
19123
19124         # Calculate the final truncate size.
19125         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
19126
19127         #
19128         # truncate to size $trunc_sz bytes. Strip the last block
19129         # The block count must drop to 8
19130         #
19131         $TRUNCATE $DIR/$tfile $trunc_sz ||
19132                 error "truncate $tfile to $trunc_sz failed"
19133
19134         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
19135         sz=$(stat --format=%s $DIR/$tfile)
19136         blk=$(stat --format=%b $DIR/$tfile)
19137
19138         if [[ $blk -ne $trunc_bsz ]]; then
19139                 $(which stat) $DIR/$tfile
19140                 error "Expected Block $trunc_bsz got $blk for $tfile"
19141         fi
19142
19143         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
19144                 error "Expected Size $trunc_sz got $sz for $tfile"
19145 }
19146 run_test 317 "Verify blocks get correctly update after truncate"
19147
19148 test_319() {
19149         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
19150
19151         local before=$(date +%s)
19152         local evict
19153         local mdir=$DIR/$tdir
19154         local file=$mdir/xxx
19155
19156         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
19157         touch $file
19158
19159 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
19160         $LCTL set_param fail_val=5 fail_loc=0x8000032c
19161         $LFS mv -m1 $file &
19162
19163         sleep 1
19164         dd if=$file of=/dev/null
19165         wait
19166         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
19167           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
19168
19169         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
19170 }
19171 run_test 319 "lost lease lock on migrate error"
19172
19173 test_fake_rw() {
19174         local read_write=$1
19175         if [ "$read_write" = "write" ]; then
19176                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
19177         elif [ "$read_write" = "read" ]; then
19178                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
19179         else
19180                 error "argument error"
19181         fi
19182
19183         # turn off debug for performance testing
19184         local saved_debug=$($LCTL get_param -n debug)
19185         $LCTL set_param debug=0
19186
19187         $LFS setstripe -c 1 -i 0 $DIR/$tfile
19188
19189         # get ost1 size - lustre-OST0000
19190         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
19191         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
19192         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
19193
19194         if [ "$read_write" = "read" ]; then
19195                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
19196         fi
19197
19198         local start_time=$(date +%s.%N)
19199         $dd_cmd bs=1M count=$blocks oflag=sync ||
19200                 error "real dd $read_write error"
19201         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
19202
19203         if [ "$read_write" = "write" ]; then
19204                 rm -f $DIR/$tfile
19205         fi
19206
19207         # define OBD_FAIL_OST_FAKE_RW           0x238
19208         do_facet ost1 $LCTL set_param fail_loc=0x238
19209
19210         local start_time=$(date +%s.%N)
19211         $dd_cmd bs=1M count=$blocks oflag=sync ||
19212                 error "fake dd $read_write error"
19213         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
19214
19215         if [ "$read_write" = "write" ]; then
19216                 # verify file size
19217                 cancel_lru_locks osc
19218                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
19219                         error "$tfile size not $blocks MB"
19220         fi
19221         do_facet ost1 $LCTL set_param fail_loc=0
19222
19223         echo "fake $read_write $duration_fake vs. normal $read_write" \
19224                 "$duration in seconds"
19225         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
19226                 error_not_in_vm "fake write is slower"
19227
19228         $LCTL set_param -n debug="$saved_debug"
19229         rm -f $DIR/$tfile
19230 }
19231 test_399a() { # LU-7655 for OST fake write
19232         remote_ost_nodsh && skip "remote OST with nodsh"
19233
19234         test_fake_rw write
19235 }
19236 run_test 399a "fake write should not be slower than normal write"
19237
19238 test_399b() { # LU-8726 for OST fake read
19239         remote_ost_nodsh && skip "remote OST with nodsh"
19240         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
19241                 skip_env "ldiskfs only test"
19242         fi
19243
19244         test_fake_rw read
19245 }
19246 run_test 399b "fake read should not be slower than normal read"
19247
19248 test_400a() { # LU-1606, was conf-sanity test_74
19249         if ! which $CC > /dev/null 2>&1; then
19250                 skip_env "$CC is not installed"
19251         fi
19252
19253         local extra_flags=''
19254         local out=$TMP/$tfile
19255         local prefix=/usr/include/lustre
19256         local prog
19257
19258         if ! [[ -d $prefix ]]; then
19259                 # Assume we're running in tree and fixup the include path.
19260                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
19261                 extra_flags+=" -L$LUSTRE/utils/.lib"
19262         fi
19263
19264         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
19265                 $CC -Wall -Werror $extra_flags -o $out $prog -llustreapi ||
19266                         error "client api broken"
19267         done
19268         rm -f $out
19269 }
19270 run_test 400a "Lustre client api program can compile and link"
19271
19272 test_400b() { # LU-1606, LU-5011
19273         local header
19274         local out=$TMP/$tfile
19275         local prefix=/usr/include/linux/lustre
19276
19277         # We use a hard coded prefix so that this test will not fail
19278         # when run in tree. There are headers in lustre/include/lustre/
19279         # that are not packaged (like lustre_idl.h) and have more
19280         # complicated include dependencies (like config.h and lnet/types.h).
19281         # Since this test about correct packaging we just skip them when
19282         # they don't exist (see below) rather than try to fixup cppflags.
19283
19284         if ! which $CC > /dev/null 2>&1; then
19285                 skip_env "$CC is not installed"
19286         fi
19287
19288         for header in $prefix/*.h; do
19289                 if ! [[ -f "$header" ]]; then
19290                         continue
19291                 fi
19292
19293                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
19294                         continue # lustre_ioctl.h is internal header
19295                 fi
19296
19297                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
19298                         error "cannot compile '$header'"
19299         done
19300         rm -f $out
19301 }
19302 run_test 400b "packaged headers can be compiled"
19303
19304 test_401a() { #LU-7437
19305         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
19306         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
19307
19308         #count the number of parameters by "list_param -R"
19309         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
19310         #count the number of parameters by listing proc files
19311         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
19312         echo "proc_dirs='$proc_dirs'"
19313         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
19314         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
19315                       sort -u | wc -l)
19316
19317         [ $params -eq $procs ] ||
19318                 error "found $params parameters vs. $procs proc files"
19319
19320         # test the list_param -D option only returns directories
19321         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
19322         #count the number of parameters by listing proc directories
19323         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
19324                 sort -u | wc -l)
19325
19326         [ $params -eq $procs ] ||
19327                 error "found $params parameters vs. $procs proc files"
19328 }
19329 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
19330
19331 test_401b() {
19332         local save=$($LCTL get_param -n jobid_var)
19333         local tmp=testing
19334
19335         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
19336                 error "no error returned when setting bad parameters"
19337
19338         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
19339         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
19340
19341         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
19342         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
19343         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
19344 }
19345 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
19346
19347 test_401c() {
19348         local jobid_var_old=$($LCTL get_param -n jobid_var)
19349         local jobid_var_new
19350
19351         $LCTL set_param jobid_var= &&
19352                 error "no error returned for 'set_param a='"
19353
19354         jobid_var_new=$($LCTL get_param -n jobid_var)
19355         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
19356                 error "jobid_var was changed by setting without value"
19357
19358         $LCTL set_param jobid_var &&
19359                 error "no error returned for 'set_param a'"
19360
19361         jobid_var_new=$($LCTL get_param -n jobid_var)
19362         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
19363                 error "jobid_var was changed by setting without value"
19364 }
19365 run_test 401c "Verify 'lctl set_param' without value fails in either format."
19366
19367 test_401d() {
19368         local jobid_var_old=$($LCTL get_param -n jobid_var)
19369         local jobid_var_new
19370         local new_value="foo=bar"
19371
19372         $LCTL set_param jobid_var=$new_value ||
19373                 error "'set_param a=b' did not accept a value containing '='"
19374
19375         jobid_var_new=$($LCTL get_param -n jobid_var)
19376         [[ "$jobid_var_new" == "$new_value" ]] ||
19377                 error "'set_param a=b' failed on a value containing '='"
19378
19379         # Reset the jobid_var to test the other format
19380         $LCTL set_param jobid_var=$jobid_var_old
19381         jobid_var_new=$($LCTL get_param -n jobid_var)
19382         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
19383                 error "failed to reset jobid_var"
19384
19385         $LCTL set_param jobid_var $new_value ||
19386                 error "'set_param a b' did not accept a value containing '='"
19387
19388         jobid_var_new=$($LCTL get_param -n jobid_var)
19389         [[ "$jobid_var_new" == "$new_value" ]] ||
19390                 error "'set_param a b' failed on a value containing '='"
19391
19392         $LCTL set_param jobid_var $jobid_var_old
19393         jobid_var_new=$($LCTL get_param -n jobid_var)
19394         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
19395                 error "failed to reset jobid_var"
19396 }
19397 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
19398
19399 test_402() {
19400         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
19401         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
19402                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
19403         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
19404                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
19405                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
19406         remote_mds_nodsh && skip "remote MDS with nodsh"
19407
19408         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
19409 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
19410         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
19411         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
19412                 echo "Touch failed - OK"
19413 }
19414 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
19415
19416 test_403() {
19417         local file1=$DIR/$tfile.1
19418         local file2=$DIR/$tfile.2
19419         local tfile=$TMP/$tfile
19420
19421         rm -f $file1 $file2 $tfile
19422
19423         touch $file1
19424         ln $file1 $file2
19425
19426         # 30 sec OBD_TIMEOUT in ll_getattr()
19427         # right before populating st_nlink
19428         $LCTL set_param fail_loc=0x80001409
19429         stat -c %h $file1 > $tfile &
19430
19431         # create an alias, drop all locks and reclaim the dentry
19432         < $file2
19433         cancel_lru_locks mdc
19434         cancel_lru_locks osc
19435         sysctl -w vm.drop_caches=2
19436
19437         wait
19438
19439         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
19440
19441         rm -f $tfile $file1 $file2
19442 }
19443 run_test 403 "i_nlink should not drop to zero due to aliasing"
19444
19445 test_404() { # LU-6601
19446         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
19447                 skip "Need server version newer than 2.8.52"
19448         remote_mds_nodsh && skip "remote MDS with nodsh"
19449
19450         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
19451                 awk '/osp .*-osc-MDT/ { print $4}')
19452
19453         local osp
19454         for osp in $mosps; do
19455                 echo "Deactivate: " $osp
19456                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
19457                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19458                         awk -vp=$osp '$4 == p { print $2 }')
19459                 [ $stat = IN ] || {
19460                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19461                         error "deactivate error"
19462                 }
19463                 echo "Activate: " $osp
19464                 do_facet $SINGLEMDS $LCTL --device %$osp activate
19465                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19466                         awk -vp=$osp '$4 == p { print $2 }')
19467                 [ $stat = UP ] || {
19468                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19469                         error "activate error"
19470                 }
19471         done
19472 }
19473 run_test 404 "validate manual {de}activated works properly for OSPs"
19474
19475 test_405() {
19476         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
19477         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
19478                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
19479                         skip "Layout swap lock is not supported"
19480
19481         check_swap_layouts_support
19482
19483         test_mkdir $DIR/$tdir
19484         swap_lock_test -d $DIR/$tdir ||
19485                 error "One layout swap locked test failed"
19486 }
19487 run_test 405 "Various layout swap lock tests"
19488
19489 test_406() {
19490         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19491         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
19492         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
19493         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19494         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
19495                 skip "Need MDS version at least 2.8.50"
19496
19497         local def_stripe_size=$($LFS getstripe -S $MOUNT)
19498         local test_pool=$TESTNAME
19499
19500         if ! combined_mgs_mds ; then
19501                 mount_mgs_client
19502         fi
19503         pool_add $test_pool || error "pool_add failed"
19504         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
19505                 error "pool_add_targets failed"
19506
19507         save_layout_restore_at_exit $MOUNT
19508
19509         # parent set default stripe count only, child will stripe from both
19510         # parent and fs default
19511         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
19512                 error "setstripe $MOUNT failed"
19513         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
19514         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
19515         for i in $(seq 10); do
19516                 local f=$DIR/$tdir/$tfile.$i
19517                 touch $f || error "touch failed"
19518                 local count=$($LFS getstripe -c $f)
19519                 [ $count -eq $OSTCOUNT ] ||
19520                         error "$f stripe count $count != $OSTCOUNT"
19521                 local offset=$($LFS getstripe -i $f)
19522                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
19523                 local size=$($LFS getstripe -S $f)
19524                 [ $size -eq $((def_stripe_size * 2)) ] ||
19525                         error "$f stripe size $size != $((def_stripe_size * 2))"
19526                 local pool=$($LFS getstripe -p $f)
19527                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
19528         done
19529
19530         # change fs default striping, delete parent default striping, now child
19531         # will stripe from new fs default striping only
19532         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
19533                 error "change $MOUNT default stripe failed"
19534         $LFS setstripe -c 0 $DIR/$tdir ||
19535                 error "delete $tdir default stripe failed"
19536         for i in $(seq 11 20); do
19537                 local f=$DIR/$tdir/$tfile.$i
19538                 touch $f || error "touch $f failed"
19539                 local count=$($LFS getstripe -c $f)
19540                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
19541                 local offset=$($LFS getstripe -i $f)
19542                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
19543                 local size=$($LFS getstripe -S $f)
19544                 [ $size -eq $def_stripe_size ] ||
19545                         error "$f stripe size $size != $def_stripe_size"
19546                 local pool=$($LFS getstripe -p $f)
19547                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
19548         done
19549
19550         unlinkmany $DIR/$tdir/$tfile. 1 20
19551
19552         local f=$DIR/$tdir/$tfile
19553         pool_remove_all_targets $test_pool $f
19554         pool_remove $test_pool $f
19555
19556         if ! combined_mgs_mds ; then
19557                 umount_mgs_client
19558         fi
19559 }
19560 run_test 406 "DNE support fs default striping"
19561
19562 test_407() {
19563         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19564         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
19565                 skip "Need MDS version at least 2.8.55"
19566         remote_mds_nodsh && skip "remote MDS with nodsh"
19567
19568         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
19569                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
19570         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
19571                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
19572         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
19573
19574         #define OBD_FAIL_DT_TXN_STOP    0x2019
19575         for idx in $(seq $MDSCOUNT); do
19576                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
19577         done
19578         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
19579         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
19580                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
19581         true
19582 }
19583 run_test 407 "transaction fail should cause operation fail"
19584
19585 test_408() {
19586         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
19587
19588         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
19589         lctl set_param fail_loc=0x8000040a
19590         # let ll_prepare_partial_page() fail
19591         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
19592
19593         rm -f $DIR/$tfile
19594
19595         # create at least 100 unused inodes so that
19596         # shrink_icache_memory(0) should not return 0
19597         touch $DIR/$tfile-{0..100}
19598         rm -f $DIR/$tfile-{0..100}
19599         sync
19600
19601         echo 2 > /proc/sys/vm/drop_caches
19602 }
19603 run_test 408 "drop_caches should not hang due to page leaks"
19604
19605 test_409()
19606 {
19607         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19608
19609         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
19610         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
19611         touch $DIR/$tdir/guard || error "(2) Fail to create"
19612
19613         local PREFIX=$(str_repeat 'A' 128)
19614         echo "Create 1K hard links start at $(date)"
19615         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
19616                 error "(3) Fail to hard link"
19617
19618         echo "Links count should be right although linkEA overflow"
19619         stat $DIR/$tdir/guard || error "(4) Fail to stat"
19620         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
19621         [ $linkcount -eq 1001 ] ||
19622                 error "(5) Unexpected hard links count: $linkcount"
19623
19624         echo "List all links start at $(date)"
19625         ls -l $DIR/$tdir/foo > /dev/null ||
19626                 error "(6) Fail to list $DIR/$tdir/foo"
19627
19628         echo "Unlink hard links start at $(date)"
19629         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
19630                 error "(7) Fail to unlink"
19631         echo "Unlink hard links finished at $(date)"
19632 }
19633 run_test 409 "Large amount of cross-MDTs hard links on the same file"
19634
19635 test_410()
19636 {
19637         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
19638                 skip "Need client version at least 2.9.59"
19639
19640         # Create a file, and stat it from the kernel
19641         local testfile=$DIR/$tfile
19642         touch $testfile
19643
19644         local run_id=$RANDOM
19645         local my_ino=$(stat --format "%i" $testfile)
19646
19647         # Try to insert the module. This will always fail as the
19648         # module is designed to not be inserted.
19649         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
19650             &> /dev/null
19651
19652         # Anything but success is a test failure
19653         dmesg | grep -q \
19654             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
19655             error "no inode match"
19656 }
19657 run_test 410 "Test inode number returned from kernel thread"
19658
19659 cleanup_test411_cgroup() {
19660         trap 0
19661         rmdir "$1"
19662 }
19663
19664 test_411() {
19665         local cg_basedir=/sys/fs/cgroup/memory
19666         # LU-9966
19667         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
19668                 skip "no setup for cgroup"
19669
19670         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
19671                 error "test file creation failed"
19672         cancel_lru_locks osc
19673
19674         # Create a very small memory cgroup to force a slab allocation error
19675         local cgdir=$cg_basedir/osc_slab_alloc
19676         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
19677         trap "cleanup_test411_cgroup $cgdir" EXIT
19678         echo 2M > $cgdir/memory.kmem.limit_in_bytes
19679         echo 1M > $cgdir/memory.limit_in_bytes
19680
19681         # Should not LBUG, just be killed by oom-killer
19682         # dd will return 0 even allocation failure in some environment.
19683         # So don't check return value
19684         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
19685         cleanup_test411_cgroup $cgdir
19686
19687         return 0
19688 }
19689 run_test 411 "Slab allocation error with cgroup does not LBUG"
19690
19691 test_412() {
19692         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19693         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
19694                 skip "Need server version at least 2.10.55"
19695         fi
19696
19697         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
19698                 error "mkdir failed"
19699         $LFS getdirstripe $DIR/$tdir
19700         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
19701         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
19702                 error "expect $((MDSCOUT - 1)) get $stripe_index"
19703         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
19704         [ $stripe_count -eq 2 ] ||
19705                 error "expect 2 get $stripe_count"
19706 }
19707 run_test 412 "mkdir on specific MDTs"
19708
19709 test_413() {
19710         [ $MDSCOUNT -lt 2 ] &&
19711                 skip "We need at least 2 MDTs for this test"
19712
19713         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
19714                 skip "Need server version at least 2.10.55"
19715         fi
19716
19717         mkdir $DIR/$tdir || error "mkdir failed"
19718
19719         # find MDT that is the most full
19720         local max=$($LFS df | grep MDT |
19721                 awk 'BEGIN { a=0 }
19722                         { sub("%", "", $5)
19723                           if (0+$5 >= a)
19724                           {
19725                                 a = $5
19726                                 b = $6
19727                           }
19728                         }
19729                      END { split(b, c, ":")
19730                            sub("]", "", c[2])
19731                            print c[2]
19732                          }')
19733
19734         for i in $(seq $((MDSCOUNT - 1))); do
19735                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
19736                         error "mkdir d$i failed"
19737                 $LFS getdirstripe $DIR/$tdir/d$i
19738                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
19739                 [ $stripe_index -ne $max ] ||
19740                         error "don't expect $max"
19741         done
19742 }
19743 run_test 413 "mkdir on less full MDTs"
19744
19745 test_414() {
19746 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
19747         $LCTL set_param fail_loc=0x80000521
19748         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
19749         rm -f $DIR/$tfile
19750 }
19751 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
19752
19753 test_415() {
19754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19755         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
19756                 skip "Need server version at least 2.11.52"
19757
19758         # LU-11102
19759         local total
19760         local setattr_pid
19761         local start_time
19762         local end_time
19763         local duration
19764
19765         total=500
19766         # this test may be slow on ZFS
19767         [ "$mds1_FSTYPE" == "zfs" ] && total=100
19768
19769         # though this test is designed for striped directory, let's test normal
19770         # directory too since lock is always saved as CoS lock.
19771         test_mkdir $DIR/$tdir || error "mkdir $tdir"
19772         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
19773
19774         (
19775                 while true; do
19776                         touch $DIR/$tdir
19777                 done
19778         ) &
19779         setattr_pid=$!
19780
19781         start_time=$(date +%s)
19782         for i in $(seq $total); do
19783                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
19784                         > /dev/null
19785         done
19786         end_time=$(date +%s)
19787         duration=$((end_time - start_time))
19788
19789         kill -9 $setattr_pid
19790
19791         echo "rename $total files took $duration sec"
19792         [ $duration -lt 100 ] || error "rename took $duration sec"
19793 }
19794 run_test 415 "lock revoke is not missing"
19795
19796 test_416() {
19797         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
19798                 skip "Need server version at least 2.11.55"
19799
19800         # define OBD_FAIL_OSD_TXN_START    0x19a
19801         do_facet mds1 lctl set_param fail_loc=0x19a
19802
19803         lfs mkdir -c $MDSCOUNT $DIR/$tdir
19804
19805         true
19806 }
19807 run_test 416 "transaction start failure won't cause system hung"
19808
19809 cleanup_417() {
19810         trap 0
19811         do_nodes $(comma_list $(mdts_nodes)) \
19812                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
19813         do_nodes $(comma_list $(mdts_nodes)) \
19814                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
19815         do_nodes $(comma_list $(mdts_nodes)) \
19816                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
19817 }
19818
19819 test_417() {
19820         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19821         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
19822                 skip "Need MDS version at least 2.11.56"
19823
19824         trap cleanup_417 RETURN EXIT
19825
19826         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
19827         do_nodes $(comma_list $(mdts_nodes)) \
19828                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
19829         $LFS migrate -m 0 $DIR/$tdir.1 &&
19830                 error "migrate dir $tdir.1 should fail"
19831
19832         do_nodes $(comma_list $(mdts_nodes)) \
19833                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
19834         $LFS mkdir -i 1 $DIR/$tdir.2 &&
19835                 error "create remote dir $tdir.2 should fail"
19836
19837         do_nodes $(comma_list $(mdts_nodes)) \
19838                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
19839         $LFS mkdir -c 2 $DIR/$tdir.3 &&
19840                 error "create striped dir $tdir.3 should fail"
19841         true
19842 }
19843 run_test 417 "disable remote dir, striped dir and dir migration"
19844
19845 # Checks that the outputs of df [-i] and lfs df [-i] match
19846 #
19847 # usage: check_lfs_df <blocks | inodes> <mountpoint>
19848 check_lfs_df() {
19849         local dir=$2
19850         local inodes
19851         local df_out
19852         local lfs_df_out
19853         local count
19854         local passed=false
19855
19856         # blocks or inodes
19857         [ "$1" == "blocks" ] && inodes= || inodes="-i"
19858
19859         for count in {1..100}; do
19860                 cancel_lru_locks
19861                 sync; sleep 0.2
19862
19863                 # read the lines of interest
19864                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
19865                         error "df $inodes $dir | tail -n +2 failed"
19866                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
19867                         error "lfs df $inodes $dir | grep summary: failed"
19868
19869                 # skip first substrings of each output as they are different
19870                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
19871                 # compare the two outputs
19872                 passed=true
19873                 for i in {1..5}; do
19874                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
19875                 done
19876                 $passed && break
19877         done
19878
19879         if ! $passed; then
19880                 df -P $inodes $dir
19881                 echo
19882                 lfs df $inodes $dir
19883                 error "df and lfs df $1 output mismatch: "      \
19884                       "df ${inodes}: ${df_out[*]}, "            \
19885                       "lfs df ${inodes}: ${lfs_df_out[*]}"
19886         fi
19887 }
19888
19889 test_418() {
19890         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19891
19892         local dir=$DIR/$tdir
19893         local numfiles=$((RANDOM % 4096 + 2))
19894         local numblocks=$((RANDOM % 256 + 1))
19895
19896         wait_delete_completed
19897         test_mkdir $dir
19898
19899         # check block output
19900         check_lfs_df blocks $dir
19901         # check inode output
19902         check_lfs_df inodes $dir
19903
19904         # create a single file and retest
19905         echo "Creating a single file and testing"
19906         createmany -o $dir/$tfile- 1 &>/dev/null ||
19907                 error "creating 1 file in $dir failed"
19908         check_lfs_df blocks $dir
19909         check_lfs_df inodes $dir
19910
19911         # create a random number of files
19912         echo "Creating $((numfiles - 1)) files and testing"
19913         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
19914                 error "creating $((numfiles - 1)) files in $dir failed"
19915
19916         # write a random number of blocks to the first test file
19917         echo "Writing $numblocks 4K blocks and testing"
19918         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
19919                 count=$numblocks &>/dev/null ||
19920                 error "dd to $dir/${tfile}-0 failed"
19921
19922         # retest
19923         check_lfs_df blocks $dir
19924         check_lfs_df inodes $dir
19925
19926         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
19927                 error "unlinking $numfiles files in $dir failed"
19928 }
19929 run_test 418 "df and lfs df outputs match"
19930
19931 test_419()
19932 {
19933         local dir=$DIR/$tdir
19934
19935         mkdir -p $dir
19936         touch $dir/file
19937
19938         cancel_lru_locks mdc
19939
19940         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
19941         $LCTL set_param fail_loc=0x1410
19942         cat $dir/file
19943         $LCTL set_param fail_loc=0
19944         rm -rf $dir
19945 }
19946 run_test 419 "Verify open file by name doesn't crash kernel"
19947
19948 test_420()
19949 {
19950         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
19951                 skip "Need MDS version at least 2.12.53"
19952
19953         local SAVE_UMASK=$(umask)
19954         local dir=$DIR/$tdir
19955         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
19956
19957         mkdir -p $dir
19958         umask 0000
19959         mkdir -m03777 $dir/testdir
19960         ls -dn $dir/testdir
19961         local dirperms=$(ls -dn $dir/testdir | awk '{print $1}')
19962         [ $dirperms == "drwxrwsrwt" ] ||
19963                 error "incorrect perms on $dir/testdir"
19964
19965         $PDSH ${uname}@localhost "PATH=$LUSTRE/tests:\$PATH; \
19966                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
19967         ls -n $dir/testdir/testfile
19968         local fileperms=$(ls -n $dir/testdir/testfile | awk '{print $1}')
19969         [ $fileperms == "-rwxr-xr-x" ] ||
19970                 error "incorrect perms on $dir/testdir/testfile"
19971
19972         umask $SAVE_UMASK
19973 }
19974 run_test 420 "clear SGID bit on non-directories for non-members"
19975
19976 prep_801() {
19977         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
19978         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
19979                 skip "Need server version at least 2.9.55"
19980
19981         start_full_debug_logging
19982 }
19983
19984 post_801() {
19985         stop_full_debug_logging
19986 }
19987
19988 barrier_stat() {
19989         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
19990                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
19991                            awk '/The barrier for/ { print $7 }')
19992                 echo $st
19993         else
19994                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
19995                 echo \'$st\'
19996         fi
19997 }
19998
19999 barrier_expired() {
20000         local expired
20001
20002         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
20003                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
20004                           awk '/will be expired/ { print $7 }')
20005         else
20006                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
20007         fi
20008
20009         echo $expired
20010 }
20011
20012 test_801a() {
20013         prep_801
20014
20015         echo "Start barrier_freeze at: $(date)"
20016         #define OBD_FAIL_BARRIER_DELAY          0x2202
20017         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
20018         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
20019
20020         sleep 2
20021         local b_status=$(barrier_stat)
20022         echo "Got barrier status at: $(date)"
20023         [ "$b_status" = "'freezing_p1'" ] ||
20024                 error "(1) unexpected barrier status $b_status"
20025
20026         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
20027         wait
20028         b_status=$(barrier_stat)
20029         [ "$b_status" = "'frozen'" ] ||
20030                 error "(2) unexpected barrier status $b_status"
20031
20032         local expired=$(barrier_expired)
20033         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
20034         sleep $((expired + 3))
20035
20036         b_status=$(barrier_stat)
20037         [ "$b_status" = "'expired'" ] ||
20038                 error "(3) unexpected barrier status $b_status"
20039
20040         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
20041                 error "(4) fail to freeze barrier"
20042
20043         b_status=$(barrier_stat)
20044         [ "$b_status" = "'frozen'" ] ||
20045                 error "(5) unexpected barrier status $b_status"
20046
20047         echo "Start barrier_thaw at: $(date)"
20048         #define OBD_FAIL_BARRIER_DELAY          0x2202
20049         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
20050         do_facet mgs $LCTL barrier_thaw $FSNAME &
20051
20052         sleep 2
20053         b_status=$(barrier_stat)
20054         echo "Got barrier status at: $(date)"
20055         [ "$b_status" = "'thawing'" ] ||
20056                 error "(6) unexpected barrier status $b_status"
20057
20058         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
20059         wait
20060         b_status=$(barrier_stat)
20061         [ "$b_status" = "'thawed'" ] ||
20062                 error "(7) unexpected barrier status $b_status"
20063
20064         #define OBD_FAIL_BARRIER_FAILURE        0x2203
20065         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
20066         do_facet mgs $LCTL barrier_freeze $FSNAME
20067
20068         b_status=$(barrier_stat)
20069         [ "$b_status" = "'failed'" ] ||
20070                 error "(8) unexpected barrier status $b_status"
20071
20072         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
20073         do_facet mgs $LCTL barrier_thaw $FSNAME
20074
20075         post_801
20076 }
20077 run_test 801a "write barrier user interfaces and stat machine"
20078
20079 test_801b() {
20080         prep_801
20081
20082         mkdir $DIR/$tdir || error "(1) fail to mkdir"
20083         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
20084         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
20085         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
20086         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
20087
20088         cancel_lru_locks mdc
20089
20090         # 180 seconds should be long enough
20091         do_facet mgs $LCTL barrier_freeze $FSNAME 180
20092
20093         local b_status=$(barrier_stat)
20094         [ "$b_status" = "'frozen'" ] ||
20095                 error "(6) unexpected barrier status $b_status"
20096
20097         mkdir $DIR/$tdir/d0/d10 &
20098         mkdir_pid=$!
20099
20100         touch $DIR/$tdir/d1/f13 &
20101         touch_pid=$!
20102
20103         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
20104         ln_pid=$!
20105
20106         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
20107         mv_pid=$!
20108
20109         rm -f $DIR/$tdir/d4/f12 &
20110         rm_pid=$!
20111
20112         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
20113
20114         # To guarantee taht the 'stat' is not blocked
20115         b_status=$(barrier_stat)
20116         [ "$b_status" = "'frozen'" ] ||
20117                 error "(8) unexpected barrier status $b_status"
20118
20119         # let above commands to run at background
20120         sleep 5
20121
20122         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
20123         ps -p $touch_pid || error "(10) touch should be blocked"
20124         ps -p $ln_pid || error "(11) link should be blocked"
20125         ps -p $mv_pid || error "(12) rename should be blocked"
20126         ps -p $rm_pid || error "(13) unlink should be blocked"
20127
20128         b_status=$(barrier_stat)
20129         [ "$b_status" = "'frozen'" ] ||
20130                 error "(14) unexpected barrier status $b_status"
20131
20132         do_facet mgs $LCTL barrier_thaw $FSNAME
20133         b_status=$(barrier_stat)
20134         [ "$b_status" = "'thawed'" ] ||
20135                 error "(15) unexpected barrier status $b_status"
20136
20137         wait $mkdir_pid || error "(16) mkdir should succeed"
20138         wait $touch_pid || error "(17) touch should succeed"
20139         wait $ln_pid || error "(18) link should succeed"
20140         wait $mv_pid || error "(19) rename should succeed"
20141         wait $rm_pid || error "(20) unlink should succeed"
20142
20143         post_801
20144 }
20145 run_test 801b "modification will be blocked by write barrier"
20146
20147 test_801c() {
20148         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20149
20150         prep_801
20151
20152         stop mds2 || error "(1) Fail to stop mds2"
20153
20154         do_facet mgs $LCTL barrier_freeze $FSNAME 30
20155
20156         local b_status=$(barrier_stat)
20157         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
20158                 do_facet mgs $LCTL barrier_thaw $FSNAME
20159                 error "(2) unexpected barrier status $b_status"
20160         }
20161
20162         do_facet mgs $LCTL barrier_rescan $FSNAME ||
20163                 error "(3) Fail to rescan barrier bitmap"
20164
20165         do_facet mgs $LCTL barrier_freeze $FSNAME 10
20166
20167         b_status=$(barrier_stat)
20168         [ "$b_status" = "'frozen'" ] ||
20169                 error "(4) unexpected barrier status $b_status"
20170
20171         do_facet mgs $LCTL barrier_thaw $FSNAME
20172         b_status=$(barrier_stat)
20173         [ "$b_status" = "'thawed'" ] ||
20174                 error "(5) unexpected barrier status $b_status"
20175
20176         local devname=$(mdsdevname 2)
20177
20178         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
20179
20180         do_facet mgs $LCTL barrier_rescan $FSNAME ||
20181                 error "(7) Fail to rescan barrier bitmap"
20182
20183         post_801
20184 }
20185 run_test 801c "rescan barrier bitmap"
20186
20187 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
20188 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
20189 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
20190
20191 cleanup_802a() {
20192         trap 0
20193
20194         stopall
20195         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
20196         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
20197         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
20198         setupall
20199 }
20200
20201 test_802a() {
20202
20203         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
20204         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
20205                 skip "Need server version at least 2.9.55"
20206
20207         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
20208
20209         mkdir $DIR/$tdir || error "(1) fail to mkdir"
20210
20211         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
20212                 error "(2) Fail to copy"
20213
20214         trap cleanup_802a EXIT
20215
20216         # sync by force before remount as readonly
20217         sync; sync_all_data; sleep 3; sync_all_data
20218
20219         stopall
20220
20221         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
20222         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
20223         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
20224
20225         echo "Mount the server as read only"
20226         setupall server_only || error "(3) Fail to start servers"
20227
20228         echo "Mount client without ro should fail"
20229         mount_client $MOUNT &&
20230                 error "(4) Mount client without 'ro' should fail"
20231
20232         echo "Mount client with ro should succeed"
20233         mount_client $MOUNT ro ||
20234                 error "(5) Mount client with 'ro' should succeed"
20235
20236         echo "Modify should be refused"
20237         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
20238
20239         echo "Read should be allowed"
20240         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
20241                 error "(7) Read should succeed under ro mode"
20242
20243         cleanup_802a
20244 }
20245 run_test 802a "simulate readonly device"
20246
20247 test_802b() {
20248         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20249         remote_mds_nodsh && skip "remote MDS with nodsh"
20250
20251         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
20252                 skip "readonly option not available"
20253
20254         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
20255
20256         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
20257                 error "(2) Fail to copy"
20258
20259         # write back all cached data before setting MDT to readonly
20260         cancel_lru_locks
20261         sync_all_data
20262
20263         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
20264         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
20265
20266         echo "Modify should be refused"
20267         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
20268
20269         echo "Read should be allowed"
20270         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
20271                 error "(7) Read should succeed under ro mode"
20272
20273         # disable readonly
20274         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
20275 }
20276 run_test 802b "be able to set MDTs to readonly"
20277
20278 test_803() {
20279         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20280         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
20281                 skip "MDS needs to be newer than 2.10.54"
20282
20283         mkdir -p $DIR/$tdir
20284         # Create some objects on all MDTs to trigger related logs objects
20285         for idx in $(seq $MDSCOUNT); do
20286                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
20287                         $DIR/$tdir/dir${idx} ||
20288                         error "Fail to create $DIR/$tdir/dir${idx}"
20289         done
20290
20291         sync; sleep 3
20292         wait_delete_completed # ensure old test cleanups are finished
20293         echo "before create:"
20294         $LFS df -i $MOUNT
20295         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20296
20297         for i in {1..10}; do
20298                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
20299                         error "Fail to create $DIR/$tdir/foo$i"
20300         done
20301
20302         sync; sleep 3
20303         echo "after create:"
20304         $LFS df -i $MOUNT
20305         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20306
20307         # allow for an llog to be cleaned up during the test
20308         [ $after_used -ge $((before_used + 10 - 1)) ] ||
20309                 error "before ($before_used) + 10 > after ($after_used)"
20310
20311         for i in {1..10}; do
20312                 rm -rf $DIR/$tdir/foo$i ||
20313                         error "Fail to remove $DIR/$tdir/foo$i"
20314         done
20315
20316         sleep 3 # avoid MDT return cached statfs
20317         wait_delete_completed
20318         echo "after unlink:"
20319         $LFS df -i $MOUNT
20320         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20321
20322         # allow for an llog to be created during the test
20323         [ $after_used -le $((before_used + 1)) ] ||
20324                 error "after ($after_used) > before ($before_used) + 1"
20325 }
20326 run_test 803 "verify agent object for remote object"
20327
20328 test_804() {
20329         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20330         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
20331                 skip "MDS needs to be newer than 2.10.54"
20332         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
20333
20334         mkdir -p $DIR/$tdir
20335         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
20336                 error "Fail to create $DIR/$tdir/dir0"
20337
20338         local fid=$($LFS path2fid $DIR/$tdir/dir0)
20339         local dev=$(mdsdevname 2)
20340
20341         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20342                 grep ${fid} || error "NOT found agent entry for dir0"
20343
20344         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
20345                 error "Fail to create $DIR/$tdir/dir1"
20346
20347         touch $DIR/$tdir/dir1/foo0 ||
20348                 error "Fail to create $DIR/$tdir/dir1/foo0"
20349         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
20350         local rc=0
20351
20352         for idx in $(seq $MDSCOUNT); do
20353                 dev=$(mdsdevname $idx)
20354                 do_facet mds${idx} \
20355                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20356                         grep ${fid} && rc=$idx
20357         done
20358
20359         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
20360                 error "Fail to rename foo0 to foo1"
20361         if [ $rc -eq 0 ]; then
20362                 for idx in $(seq $MDSCOUNT); do
20363                         dev=$(mdsdevname $idx)
20364                         do_facet mds${idx} \
20365                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20366                         grep ${fid} && rc=$idx
20367                 done
20368         fi
20369
20370         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
20371                 error "Fail to rename foo1 to foo2"
20372         if [ $rc -eq 0 ]; then
20373                 for idx in $(seq $MDSCOUNT); do
20374                         dev=$(mdsdevname $idx)
20375                         do_facet mds${idx} \
20376                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20377                         grep ${fid} && rc=$idx
20378                 done
20379         fi
20380
20381         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
20382
20383         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
20384                 error "Fail to link to $DIR/$tdir/dir1/foo2"
20385         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
20386                 error "Fail to rename foo2 to foo0"
20387         unlink $DIR/$tdir/dir1/foo0 ||
20388                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
20389         rm -rf $DIR/$tdir/dir0 ||
20390                 error "Fail to rm $DIR/$tdir/dir0"
20391
20392         for idx in $(seq $MDSCOUNT); do
20393                 dev=$(mdsdevname $idx)
20394                 rc=0
20395
20396                 stop mds${idx}
20397                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
20398                         rc=$?
20399                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
20400                         error "mount mds$idx failed"
20401                 df $MOUNT > /dev/null 2>&1
20402
20403                 # e2fsck should not return error
20404                 [ $rc -eq 0 ] ||
20405                         error "e2fsck detected error on MDT${idx}: rc=$rc"
20406         done
20407 }
20408 run_test 804 "verify agent entry for remote entry"
20409
20410 cleanup_805() {
20411         do_facet $SINGLEMDS zfs set quota=$old $fsset
20412         unlinkmany $DIR/$tdir/f- 1000000
20413         trap 0
20414 }
20415
20416 test_805() {
20417         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
20418         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
20419         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
20420                 skip "netfree not implemented before 0.7"
20421         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
20422                 skip "Need MDS version at least 2.10.57"
20423
20424         local fsset
20425         local freekb
20426         local usedkb
20427         local old
20428         local quota
20429         local pref="osd-zfs.lustre-MDT0000."
20430
20431         # limit available space on MDS dataset to meet nospace issue
20432         # quickly. then ZFS 0.7.2 can use reserved space if asked
20433         # properly (using netfree flag in osd_declare_destroy()
20434         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
20435         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
20436                 gawk '{print $3}')
20437         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
20438         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
20439         let "usedkb=usedkb-freekb"
20440         let "freekb=freekb/2"
20441         if let "freekb > 5000"; then
20442                 let "freekb=5000"
20443         fi
20444         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
20445         trap cleanup_805 EXIT
20446         mkdir $DIR/$tdir
20447         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
20448         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
20449         rm -rf $DIR/$tdir || error "not able to remove"
20450         do_facet $SINGLEMDS zfs set quota=$old $fsset
20451         trap 0
20452 }
20453 run_test 805 "ZFS can remove from full fs"
20454
20455 # Size-on-MDS test
20456 check_lsom_data()
20457 {
20458         local file=$1
20459         local size=$($LFS getsom -s $file)
20460         local expect=$(stat -c %s $file)
20461
20462         [[ $size == $expect ]] ||
20463                 error "$file expected size: $expect, got: $size"
20464
20465         local blocks=$($LFS getsom -b $file)
20466         expect=$(stat -c %b $file)
20467         [[ $blocks == $expect ]] ||
20468                 error "$file expected blocks: $expect, got: $blocks"
20469 }
20470
20471 check_lsom_size()
20472 {
20473         local size=$($LFS getsom -s $1)
20474         local expect=$2
20475
20476         [[ $size == $expect ]] ||
20477                 error "$file expected size: $expect, got: $size"
20478 }
20479
20480 test_806() {
20481         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20482                 skip "Need MDS version at least 2.11.52"
20483
20484         local bs=1048576
20485
20486         touch $DIR/$tfile || error "touch $tfile failed"
20487
20488         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
20489         save_lustre_params client "llite.*.xattr_cache" > $save
20490         lctl set_param llite.*.xattr_cache=0
20491         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
20492
20493         # single-threaded write
20494         echo "Test SOM for single-threaded write"
20495         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
20496                 error "write $tfile failed"
20497         check_lsom_size $DIR/$tfile $bs
20498
20499         local num=32
20500         local size=$(($num * $bs))
20501         local offset=0
20502         local i
20503
20504         echo "Test SOM for single client multi-threaded($num) write"
20505         $TRUNCATE $DIR/$tfile 0
20506         for ((i = 0; i < $num; i++)); do
20507                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20508                 local pids[$i]=$!
20509                 offset=$((offset + $bs))
20510         done
20511         for (( i=0; i < $num; i++ )); do
20512                 wait ${pids[$i]}
20513         done
20514         check_lsom_size $DIR/$tfile $size
20515
20516         $TRUNCATE $DIR/$tfile 0
20517         for ((i = 0; i < $num; i++)); do
20518                 offset=$((offset - $bs))
20519                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20520                 local pids[$i]=$!
20521         done
20522         for (( i=0; i < $num; i++ )); do
20523                 wait ${pids[$i]}
20524         done
20525         check_lsom_size $DIR/$tfile $size
20526
20527         # multi-client wirtes
20528         num=$(get_node_count ${CLIENTS//,/ })
20529         size=$(($num * $bs))
20530         offset=0
20531         i=0
20532
20533         echo "Test SOM for multi-client ($num) writes"
20534         $TRUNCATE $DIR/$tfile 0
20535         for client in ${CLIENTS//,/ }; do
20536                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20537                 local pids[$i]=$!
20538                 i=$((i + 1))
20539                 offset=$((offset + $bs))
20540         done
20541         for (( i=0; i < $num; i++ )); do
20542                 wait ${pids[$i]}
20543         done
20544         check_lsom_size $DIR/$tfile $offset
20545
20546         i=0
20547         $TRUNCATE $DIR/$tfile 0
20548         for client in ${CLIENTS//,/ }; do
20549                 offset=$((offset - $bs))
20550                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20551                 local pids[$i]=$!
20552                 i=$((i + 1))
20553         done
20554         for (( i=0; i < $num; i++ )); do
20555                 wait ${pids[$i]}
20556         done
20557         check_lsom_size $DIR/$tfile $size
20558
20559         # verify truncate
20560         echo "Test SOM for truncate"
20561         $TRUNCATE $DIR/$tfile 1048576
20562         check_lsom_size $DIR/$tfile 1048576
20563         $TRUNCATE $DIR/$tfile 1234
20564         check_lsom_size $DIR/$tfile 1234
20565
20566         # verify SOM blocks count
20567         echo "Verify SOM block count"
20568         $TRUNCATE $DIR/$tfile 0
20569         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
20570                 error "failed to write file $tfile"
20571         check_lsom_data $DIR/$tfile
20572 }
20573 run_test 806 "Verify Lazy Size on MDS"
20574
20575 test_807() {
20576         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
20577         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20578                 skip "Need MDS version at least 2.11.52"
20579
20580         # Registration step
20581         changelog_register || error "changelog_register failed"
20582         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
20583         changelog_users $SINGLEMDS | grep -q $cl_user ||
20584                 error "User $cl_user not found in changelog_users"
20585
20586         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
20587         save_lustre_params client "llite.*.xattr_cache" > $save
20588         lctl set_param llite.*.xattr_cache=0
20589         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
20590
20591         rm -rf $DIR/$tdir || error "rm $tdir failed"
20592         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
20593         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
20594         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
20595         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
20596                 error "truncate $tdir/trunc failed"
20597
20598         local bs=1048576
20599         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
20600                 error "write $tfile failed"
20601
20602         # multi-client wirtes
20603         local num=$(get_node_count ${CLIENTS//,/ })
20604         local offset=0
20605         local i=0
20606
20607         echo "Test SOM for multi-client ($num) writes"
20608         touch $DIR/$tfile || error "touch $tfile failed"
20609         $TRUNCATE $DIR/$tfile 0
20610         for client in ${CLIENTS//,/ }; do
20611                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
20612                 local pids[$i]=$!
20613                 i=$((i + 1))
20614                 offset=$((offset + $bs))
20615         done
20616         for (( i=0; i < $num; i++ )); do
20617                 wait ${pids[$i]}
20618         done
20619
20620         sleep 5
20621         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
20622         check_lsom_data $DIR/$tdir/trunc
20623         check_lsom_data $DIR/$tdir/single_dd
20624         check_lsom_data $DIR/$tfile
20625
20626         rm -rf $DIR/$tdir
20627         # Deregistration step
20628         changelog_deregister || error "changelog_deregister failed"
20629 }
20630 run_test 807 "verify LSOM syncing tool"
20631
20632 check_som_nologged()
20633 {
20634         local lines=$($LFS changelog $FSNAME-MDT0000 |
20635                 grep 'x=trusted.som' | wc -l)
20636         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
20637 }
20638
20639 test_808() {
20640         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
20641                 skip "Need MDS version at least 2.11.55"
20642
20643         # Registration step
20644         changelog_register || error "changelog_register failed"
20645
20646         touch $DIR/$tfile || error "touch $tfile failed"
20647         check_som_nologged
20648
20649         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
20650                 error "write $tfile failed"
20651         check_som_nologged
20652
20653         $TRUNCATE $DIR/$tfile 1234
20654         check_som_nologged
20655
20656         $TRUNCATE $DIR/$tfile 1048576
20657         check_som_nologged
20658
20659         # Deregistration step
20660         changelog_deregister || error "changelog_deregister failed"
20661 }
20662 run_test 808 "Check trusted.som xattr not logged in Changelogs"
20663
20664 check_som_nodata()
20665 {
20666         $LFS getsom $1
20667         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
20668 }
20669
20670 test_809() {
20671         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20672                 skip "Need MDS version at least 2.11.56"
20673
20674         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
20675                 error "failed to create DoM-only file $DIR/$tfile"
20676         touch $DIR/$tfile || error "touch $tfile failed"
20677         check_som_nodata $DIR/$tfile
20678
20679         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
20680                 error "write $tfile failed"
20681         check_som_nodata $DIR/$tfile
20682
20683         $TRUNCATE $DIR/$tfile 1234
20684         check_som_nodata $DIR/$tfile
20685
20686         $TRUNCATE $DIR/$tfile 4097
20687         check_som_nodata $DIR/$file
20688 }
20689 run_test 809 "Verify no SOM xattr store for DoM-only files"
20690
20691 test_810() {
20692         local ORIG
20693         local CSUM
20694
20695         # t10 seem to dislike partial pages
20696         lctl set_param osc.*.checksum_type=adler
20697         lctl set_param fail_loc=0x411
20698         dd if=/dev/urandom of=$DIR/$tfile bs=10240 count=2
20699         ORIG=$(md5sum $DIR/$tfile)
20700         lctl set_param ldlm.namespaces.*osc*.lru_size=clear
20701         CSUM=$(md5sum $DIR/$tfile)
20702         set_checksum_type adler
20703         if [ "$ORIG" != "$CSUM" ]; then
20704                 error "$ORIG != $CSUM"
20705         fi
20706 }
20707 run_test 810 "partial page writes on ZFS (LU-11663)"
20708
20709 test_811() {
20710         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
20711                 skip "Need MDS version at least 2.11.56"
20712
20713         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
20714         do_facet mds1 $LCTL set_param fail_loc=0x165
20715         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
20716
20717         stop mds1
20718         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
20719
20720         sleep 5
20721         [[ $(do_facet mds1 pgrep orph_.*-MDD | wc -l) -eq 0 ]] ||
20722                 error "MDD orphan cleanup thread not quit"
20723 }
20724 run_test 811 "orphan name stub can be cleaned up in startup"
20725
20726 test_812() {
20727         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
20728                 skip "OST < 2.12.51 doesn't support this fail_loc"
20729         [ "$SHARED_KEY" = true ] &&
20730                 skip "OSC connections never go IDLE with Shared-Keys enabled"
20731
20732         $LFS setstripe -c 1 -i 0 $DIR/$tfile
20733         # ensure ost1 is connected
20734         stat $DIR/$tfile >/dev/null || error "can't stat"
20735         wait_osc_import_state client ost1 FULL
20736         # no locks, no reqs to let the connection idle
20737         cancel_lru_locks osc
20738
20739         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
20740 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
20741         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
20742         wait_osc_import_state client ost1 CONNECTING
20743         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
20744
20745         stat $DIR/$tfile >/dev/null || error "can't stat file"
20746 }
20747 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
20748
20749 test_813() {
20750         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
20751         [ -z "$file_heat_sav" ] && skip "no file heat support"
20752
20753         local readsample
20754         local writesample
20755         local readbyte
20756         local writebyte
20757         local readsample1
20758         local writesample1
20759         local readbyte1
20760         local writebyte1
20761
20762         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
20763         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
20764
20765         $LCTL set_param -n llite.*.file_heat=1
20766         echo "Turn on file heat"
20767         echo "Period second: $period_second, Decay percentage: $decay_pct"
20768
20769         echo "QQQQ" > $DIR/$tfile
20770         echo "QQQQ" > $DIR/$tfile
20771         echo "QQQQ" > $DIR/$tfile
20772         cat $DIR/$tfile > /dev/null
20773         cat $DIR/$tfile > /dev/null
20774         cat $DIR/$tfile > /dev/null
20775         cat $DIR/$tfile > /dev/null
20776
20777         local out=$($LFS heat_get $DIR/$tfile)
20778
20779         $LFS heat_get $DIR/$tfile
20780         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20781         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20782         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20783         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20784
20785         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
20786         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
20787         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
20788         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
20789
20790         sleep $((period_second + 3))
20791         echo "Sleep $((period_second + 3)) seconds..."
20792         # The recursion formula to calculate the heat of the file f is as
20793         # follow:
20794         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
20795         # Where Hi is the heat value in the period between time points i*I and
20796         # (i+1)*I; Ci is the access count in the period; the symbol P refers
20797         # to the weight of Ci.
20798         out=$($LFS heat_get $DIR/$tfile)
20799         $LFS heat_get $DIR/$tfile
20800         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20801         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20802         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20803         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20804
20805         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
20806                 error "read sample ($readsample) is wrong"
20807         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
20808                 error "write sample ($writesample) is wrong"
20809         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
20810                 error "read bytes ($readbyte) is wrong"
20811         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
20812                 error "write bytes ($writebyte) is wrong"
20813
20814         echo "QQQQ" > $DIR/$tfile
20815         echo "QQQQ" > $DIR/$tfile
20816         echo "QQQQ" > $DIR/$tfile
20817         cat $DIR/$tfile > /dev/null
20818         cat $DIR/$tfile > /dev/null
20819         cat $DIR/$tfile > /dev/null
20820         cat $DIR/$tfile > /dev/null
20821
20822         sleep $((period_second + 3))
20823         echo "Sleep $((period_second + 3)) seconds..."
20824
20825         out=$($LFS heat_get $DIR/$tfile)
20826         $LFS heat_get $DIR/$tfile
20827         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20828         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20829         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20830         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20831
20832         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
20833                 4 * $decay_pct) / 100") -eq 1 ] ||
20834                 error "read sample ($readsample1) is wrong"
20835         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
20836                 3 * $decay_pct) / 100") -eq 1 ] ||
20837                 error "write sample ($writesample1) is wrong"
20838         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
20839                 20 * $decay_pct) / 100") -eq 1 ] ||
20840                 error "read bytes ($readbyte1) is wrong"
20841         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
20842                 15 * $decay_pct) / 100") -eq 1 ] ||
20843                 error "write bytes ($writebyte1) is wrong"
20844
20845         echo "Turn off file heat for the file $DIR/$tfile"
20846         $LFS heat_set -o $DIR/$tfile
20847
20848         echo "QQQQ" > $DIR/$tfile
20849         echo "QQQQ" > $DIR/$tfile
20850         echo "QQQQ" > $DIR/$tfile
20851         cat $DIR/$tfile > /dev/null
20852         cat $DIR/$tfile > /dev/null
20853         cat $DIR/$tfile > /dev/null
20854         cat $DIR/$tfile > /dev/null
20855
20856         out=$($LFS heat_get $DIR/$tfile)
20857         $LFS heat_get $DIR/$tfile
20858         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20859         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20860         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20861         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20862
20863         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
20864         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
20865         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
20866         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
20867
20868         echo "Trun on file heat for the file $DIR/$tfile"
20869         $LFS heat_set -O $DIR/$tfile
20870
20871         echo "QQQQ" > $DIR/$tfile
20872         echo "QQQQ" > $DIR/$tfile
20873         echo "QQQQ" > $DIR/$tfile
20874         cat $DIR/$tfile > /dev/null
20875         cat $DIR/$tfile > /dev/null
20876         cat $DIR/$tfile > /dev/null
20877         cat $DIR/$tfile > /dev/null
20878
20879         out=$($LFS heat_get $DIR/$tfile)
20880         $LFS heat_get $DIR/$tfile
20881         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20882         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20883         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20884         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20885
20886         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
20887         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
20888         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
20889         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
20890
20891         $LFS heat_set -c $DIR/$tfile
20892         $LCTL set_param -n llite.*.file_heat=0
20893         echo "Turn off file heat support for the Lustre filesystem"
20894
20895         echo "QQQQ" > $DIR/$tfile
20896         echo "QQQQ" > $DIR/$tfile
20897         echo "QQQQ" > $DIR/$tfile
20898         cat $DIR/$tfile > /dev/null
20899         cat $DIR/$tfile > /dev/null
20900         cat $DIR/$tfile > /dev/null
20901         cat $DIR/$tfile > /dev/null
20902
20903         out=$($LFS heat_get $DIR/$tfile)
20904         $LFS heat_get $DIR/$tfile
20905         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
20906         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
20907         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
20908         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
20909
20910         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
20911         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
20912         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
20913         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
20914
20915         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
20916         rm -f $DIR/$tfile
20917 }
20918 run_test 813 "File heat verfication"
20919
20920 #
20921 # tests that do cleanup/setup should be run at the end
20922 #
20923
20924 test_900() {
20925         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20926         local ls
20927
20928         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
20929         $LCTL set_param fail_loc=0x903
20930
20931         cancel_lru_locks MGC
20932
20933         FAIL_ON_ERROR=true cleanup
20934         FAIL_ON_ERROR=true setup
20935 }
20936 run_test 900 "umount should not race with any mgc requeue thread"
20937
20938 complete $SECONDS
20939 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
20940 check_and_cleanup_lustre
20941 if [ "$I_MOUNTED" != "yes" ]; then
20942         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
20943 fi
20944 exit_status