Whamcloud - gitweb
LU-11161 tests: stop running sanity test 160g
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: LU-9693 LU-6493 LU-9693 LU-11058
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c     77k"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054 ..
16 ALWAYS_EXCEPT="  407     253     312     $ALWAYS_EXCEPT"
17
18 # Check Grants after these tests
19 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
20
21 SRCDIR=$(cd $(dirname $0); echo $PWD)
22 export PATH=$PATH:/sbin
23
24 TMP=${TMP:-/tmp}
25 OSC=${OSC:-"osc"}
26
27 CC=${CC:-cc}
28 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
29 CREATETEST=${CREATETEST:-createtest}
30 LFS=${LFS:-lfs}
31 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
32 LCTL=${LCTL:-lctl}
33 OPENFILE=${OPENFILE:-openfile}
34 OPENUNLINK=${OPENUNLINK:-openunlink}
35 export MULTIOP=${MULTIOP:-multiop}
36 READS=${READS:-"reads"}
37 MUNLINK=${MUNLINK:-munlink}
38 SOCKETSERVER=${SOCKETSERVER:-socketserver}
39 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
40 MEMHOG=${MEMHOG:-memhog}
41 DIRECTIO=${DIRECTIO:-directio}
42 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
43 DEF_STRIPE_COUNT=-1
44 CHECK_GRANT=${CHECK_GRANT:-"yes"}
45 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
46 export PARALLEL=${PARALLEL:-"no"}
47
48 export NAME=${NAME:-local}
49
50 SAVE_PWD=$PWD
51
52 CLEANUP=${CLEANUP:-:}
53 SETUP=${SETUP:-:}
54 TRACE=${TRACE:-""}
55 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
56 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
57 . $LUSTRE/tests/test-framework.sh
58 init_test_env $@
59 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
60 init_logging
61
62 if [[ $MDSCOUNT -gt 1 ]]; then
63         # bug number:    LU-11161
64         ALWAYS_EXCEPT+=" 160g"
65 fi
66
67 #                                  5          12          (min)"
68 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
69
70 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
71         # bug number for skipped test: LU-1957
72         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
73         #                                               13    (min)"
74         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
75 fi
76
77 # Get the SLES distro version
78 #
79 # Returns a version string that should only be used in comparing
80 # strings returned by version_code()
81 sles_version_code()
82 {
83         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
84
85         # All SuSE Linux versions have one decimal. version_code expects two
86         local sles_version=$version.0
87         version_code $sles_version
88 }
89
90 # Check if we are running on Ubuntu or SLES so we can make decisions on
91 # what tests to run
92 if [ -r /etc/SuSE-release ]; then
93         sles_version=$(sles_version_code)
94         [ $sles_version -lt $(version_code 11.4.0) ] &&
95                 # bug number for skipped test: LU-4341
96                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
97         [ $sles_version -lt $(version_code 12.0.0) ] &&
98                 # bug number for skipped test: LU-3703
99                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
100 elif [ -r /etc/os-release ]; then
101         if grep -qi ubuntu /etc/os-release; then
102                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
103                                                 -e 's/^VERSION=//p' \
104                                                 /etc/os-release |
105                                                 awk '{ print $1 }'))
106
107                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
108                         # bug number for skipped test:
109                         #                LU-10334 LU-10335 LU-10335 LU-10335
110                         ALWAYS_EXCEPT+=" 103a     130a     130b     130c"
111                         #                LU-10335 LU-10335 LU-10365 LU-10366
112                         ALWAYS_EXCEPT+=" 130d     130e     400a     410"
113                 fi
114         fi
115 fi
116
117 FAIL_ON_ERROR=false
118
119 cleanup() {
120         echo -n "cln.."
121         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
122         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
123 }
124 setup() {
125         echo -n "mnt.."
126         load_modules
127         setupall || exit 10
128         echo "done"
129 }
130
131 check_swap_layouts_support()
132 {
133         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
134                 skip "Does not support layout lock."
135 }
136
137 check_and_setup_lustre
138 DIR=${DIR:-$MOUNT}
139 assert_DIR
140
141 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
142
143 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
144 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
145 rm -rf $DIR/[Rdfs][0-9]*
146
147 # $RUNAS_ID may get set incorrectly somewhere else
148 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
149
150 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
151
152 build_test_filter
153
154 if [ "${ONLY}" = "MOUNT" ] ; then
155         echo "Lustre is up, please go on"
156         exit
157 fi
158
159 echo "preparing for tests involving mounts"
160 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
161 touch $EXT2_DEV
162 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
163 echo # add a newline after mke2fs.
164
165 umask 077
166
167 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
168 lctl set_param debug=-1 2> /dev/null || true
169 test_0a() {
170         touch $DIR/$tfile
171         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
172         rm $DIR/$tfile
173         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
174 }
175 run_test 0a "touch; rm ====================="
176
177 test_0b() {
178         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
179         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
180 }
181 run_test 0b "chmod 0755 $DIR ============================="
182
183 test_0c() {
184         $LCTL get_param mdc.*.import | grep "state: FULL" ||
185                 error "import not FULL"
186         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
187                 error "bad target"
188 }
189 run_test 0c "check import proc"
190
191 test_0d() { # LU-3397
192         [ $(lustre_version_code mgs) -lt $(version_code 2.10.57) ] &&
193                 skip "proc exports not supported before 2.10.57"
194
195         local mgs_exp="mgs.MGS.exports"
196         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
197         local exp_client_nid
198         local exp_client_version
199         local exp_val
200         local imp_val
201         local temp_imp=$DIR/$tfile.import
202         local temp_exp=$DIR/$tfile.export
203
204         # save mgc import file to $temp_imp
205         $LCTL get_param mgc.*.import | tee $temp_imp
206         # Check if client uuid is found in MGS export
207         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
208                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
209                         $client_uuid ] &&
210                         break;
211         done
212         # save mgs export file to $temp_exp
213         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
214
215         # Compare the value of field "connect_flags"
216         imp_val=$(grep "connect_flags" $temp_imp)
217         exp_val=$(grep "connect_flags" $temp_exp)
218         [ "$exp_val" == "$imp_val" ] ||
219                 error "export flags '$exp_val' != import flags '$imp_val'"
220
221         # Compare the value of client version
222         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
223         exp_val=$(version_code $exp_client_version)
224         imp_val=$(lustre_version_code client)
225         [ "$exp_val" == "$imp_val" ] ||
226                 error "export client version '$exp_val' != '$imp_val'"
227 }
228 run_test 0d "check export proc ============================="
229
230 test_1() {
231         test_mkdir $DIR/$tdir
232         test_mkdir $DIR/$tdir/d2
233         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
234         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
235         rmdir $DIR/$tdir/d2
236         rmdir $DIR/$tdir
237         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
238 }
239 run_test 1 "mkdir; remkdir; rmdir"
240
241 test_2() {
242         test_mkdir $DIR/$tdir
243         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
244         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
245         rm -r $DIR/$tdir
246         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
247 }
248 run_test 2 "mkdir; touch; rmdir; check file"
249
250 test_3() {
251         test_mkdir $DIR/$tdir
252         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
253         touch $DIR/$tdir/$tfile
254         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
255         rm -r $DIR/$tdir
256         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
257 }
258 run_test 3 "mkdir; touch; rmdir; check dir"
259
260 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
261 test_4() {
262         test_mkdir -i 1 $DIR/$tdir
263
264         touch $DIR/$tdir/$tfile ||
265                 error "Create file under remote directory failed"
266
267         rmdir $DIR/$tdir &&
268                 error "Expect error removing in-use dir $DIR/$tdir"
269
270         test -d $DIR/$tdir || error "Remote directory disappeared"
271
272         rm -rf $DIR/$tdir || error "remove remote dir error"
273 }
274 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
275
276 test_5() {
277         test_mkdir $DIR/$tdir
278         test_mkdir $DIR/$tdir/d2
279         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
280         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
281         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
282 }
283 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
284
285 test_6a() {
286         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
287         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
288         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
289                 error "$tfile does not have perm 0666 or UID $UID"
290         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
291         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
292                 error "$tfile should be 0666 and owned by UID $UID"
293 }
294 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
295
296 test_6c() {
297         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
298
299         touch $DIR/$tfile
300         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
301         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
302                 error "$tfile should be owned by UID $RUNAS_ID"
303         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
304         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
305                 error "$tfile should be owned by UID $RUNAS_ID"
306 }
307 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
308
309 test_6e() {
310         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
311
312         touch $DIR/$tfile
313         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
314         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
315                 error "$tfile should be owned by GID $UID"
316         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
317         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
318                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
319 }
320 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
321
322 test_6g() {
323         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
324
325         test_mkdir $DIR/$tdir
326         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
327         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
328         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
329         test_mkdir $DIR/$tdir/d/subdir
330         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
331                 error "$tdir/d/subdir should be GID $RUNAS_GID"
332         if [[ $MDSCOUNT -gt 1 ]]; then
333                 # check remote dir sgid inherite
334                 $LFS mkdir -i 0 $DIR/$tdir.local ||
335                         error "mkdir $tdir.local failed"
336                 chmod g+s $DIR/$tdir.local ||
337                         error "chmod $tdir.local failed"
338                 chgrp $RUNAS_GID $DIR/$tdir.local ||
339                         error "chgrp $tdir.local failed"
340                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
341                         error "mkdir $tdir.remote failed"
342                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
343                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
344                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
345                         error "$tdir.remote should be mode 02755"
346         fi
347 }
348 run_test 6g "verify new dir in sgid dir inherits group"
349
350 test_6h() { # bug 7331
351         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
352
353         touch $DIR/$tfile || error "touch failed"
354         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
355         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
356                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
357         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
358                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
359 }
360 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
361
362 test_7a() {
363         test_mkdir $DIR/$tdir
364         $MCREATE $DIR/$tdir/$tfile
365         chmod 0666 $DIR/$tdir/$tfile
366         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
367                 error "$tdir/$tfile should be mode 0666"
368 }
369 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
370
371 test_7b() {
372         if [ ! -d $DIR/$tdir ]; then
373                 test_mkdir $DIR/$tdir
374         fi
375         $MCREATE $DIR/$tdir/$tfile
376         echo -n foo > $DIR/$tdir/$tfile
377         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
378         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
379 }
380 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
381
382 test_8() {
383         test_mkdir $DIR/$tdir
384         touch $DIR/$tdir/$tfile
385         chmod 0666 $DIR/$tdir/$tfile
386         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
387                 error "$tfile mode not 0666"
388 }
389 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
390
391 test_9() {
392         test_mkdir $DIR/$tdir
393         test_mkdir $DIR/$tdir/d2
394         test_mkdir $DIR/$tdir/d2/d3
395         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
396 }
397 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
398
399 test_10() {
400         test_mkdir $DIR/$tdir
401         test_mkdir $DIR/$tdir/d2
402         touch $DIR/$tdir/d2/$tfile
403         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
404                 error "$tdir/d2/$tfile not a file"
405 }
406 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
407
408 test_11() {
409         test_mkdir $DIR/$tdir
410         test_mkdir $DIR/$tdir/d2
411         chmod 0666 $DIR/$tdir/d2
412         chmod 0705 $DIR/$tdir/d2
413         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
414                 error "$tdir/d2 mode not 0705"
415 }
416 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
417
418 test_12() {
419         test_mkdir $DIR/$tdir
420         touch $DIR/$tdir/$tfile
421         chmod 0666 $DIR/$tdir/$tfile
422         chmod 0654 $DIR/$tdir/$tfile
423         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
424                 error "$tdir/d2 mode not 0654"
425 }
426 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
427
428 test_13() {
429         test_mkdir $DIR/$tdir
430         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
431         >  $DIR/$tdir/$tfile
432         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
433                 error "$tdir/$tfile size not 0 after truncate"
434 }
435 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
436
437 test_14() {
438         test_mkdir $DIR/$tdir
439         touch $DIR/$tdir/$tfile
440         rm $DIR/$tdir/$tfile
441         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
442 }
443 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
444
445 test_15() {
446         test_mkdir $DIR/$tdir
447         touch $DIR/$tdir/$tfile
448         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
449         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
450                 error "$tdir/${tfile_2} not a file after rename"
451         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
452 }
453 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
454
455 test_16() {
456         test_mkdir $DIR/$tdir
457         touch $DIR/$tdir/$tfile
458         rm -rf $DIR/$tdir/$tfile
459         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
460 }
461 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
462
463 test_17a() {
464         test_mkdir $DIR/$tdir
465         touch $DIR/$tdir/$tfile
466         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
467         ls -l $DIR/$tdir
468         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
469                 error "$tdir/l-exist not a symlink"
470         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
471                 error "$tdir/l-exist not referencing a file"
472         rm -f $DIR/$tdir/l-exist
473         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
474 }
475 run_test 17a "symlinks: create, remove (real)"
476
477 test_17b() {
478         test_mkdir $DIR/$tdir
479         ln -s no-such-file $DIR/$tdir/l-dangle
480         ls -l $DIR/$tdir
481         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
482                 error "$tdir/l-dangle not referencing no-such-file"
483         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
484                 error "$tdir/l-dangle not referencing non-existent file"
485         rm -f $DIR/$tdir/l-dangle
486         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
487 }
488 run_test 17b "symlinks: create, remove (dangling)"
489
490 test_17c() { # bug 3440 - don't save failed open RPC for replay
491         test_mkdir $DIR/$tdir
492         ln -s foo $DIR/$tdir/$tfile
493         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
494 }
495 run_test 17c "symlinks: open dangling (should return error)"
496
497 test_17d() {
498         test_mkdir $DIR/$tdir
499         ln -s foo $DIR/$tdir/$tfile
500         touch $DIR/$tdir/$tfile || error "creating to new symlink"
501 }
502 run_test 17d "symlinks: create dangling"
503
504 test_17e() {
505         test_mkdir $DIR/$tdir
506         local foo=$DIR/$tdir/$tfile
507         ln -s $foo $foo || error "create symlink failed"
508         ls -l $foo || error "ls -l failed"
509         ls $foo && error "ls not failed" || true
510 }
511 run_test 17e "symlinks: create recursive symlink (should return error)"
512
513 test_17f() {
514         test_mkdir $DIR/$tdir
515         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
516         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
517         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
518         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
519         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
520         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
521         ls -l  $DIR/$tdir
522 }
523 run_test 17f "symlinks: long and very long symlink name"
524
525 # str_repeat(S, N) generate a string that is string S repeated N times
526 str_repeat() {
527         local s=$1
528         local n=$2
529         local ret=''
530         while [ $((n -= 1)) -ge 0 ]; do
531                 ret=$ret$s
532         done
533         echo $ret
534 }
535
536 # Long symlinks and LU-2241
537 test_17g() {
538         test_mkdir $DIR/$tdir
539         local TESTS="59 60 61 4094 4095"
540
541         # Fix for inode size boundary in 2.1.4
542         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
543                 TESTS="4094 4095"
544
545         # Patch not applied to 2.2 or 2.3 branches
546         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
547         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
548                 TESTS="4094 4095"
549
550         # skip long symlink name for rhel6.5.
551         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
552         grep -q '6.5' /etc/redhat-release &>/dev/null &&
553                 TESTS="59 60 61 4062 4063"
554
555         for i in $TESTS; do
556                 local SYMNAME=$(str_repeat 'x' $i)
557                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
558                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
559         done
560 }
561 run_test 17g "symlinks: really long symlink name and inode boundaries"
562
563 test_17h() { #bug 17378
564         [ $PARALLEL == "yes" ] && skip "skip parallel run"
565         remote_mds_nodsh && skip "remote MDS with nodsh"
566
567         local mdt_idx
568
569         test_mkdir $DIR/$tdir
570         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
571         $LFS setstripe -c -1 $DIR/$tdir
572         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
573         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
574         touch $DIR/$tdir/$tfile || true
575 }
576 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
577
578 test_17i() { #bug 20018
579         [ $PARALLEL == "yes" ] && skip "skip parallel run"
580         remote_mds_nodsh && skip "remote MDS with nodsh"
581
582         local foo=$DIR/$tdir/$tfile
583         local mdt_idx
584
585         test_mkdir -c1 $DIR/$tdir
586         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
587         ln -s $foo $foo || error "create symlink failed"
588 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
589         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
590         ls -l $foo && error "error not detected"
591         return 0
592 }
593 run_test 17i "don't panic on short symlink (should return error)"
594
595 test_17k() { #bug 22301
596         [ $PARALLEL == "yes" ] && skip "skip parallel run"
597         [[ -z "$(which rsync 2>/dev/null)" ]] &&
598                 skip "no rsync command"
599         rsync --help | grep -q xattr ||
600                 skip_env "$(rsync --version | head -n1) does not support xattrs"
601         test_mkdir $DIR/$tdir
602         test_mkdir $DIR/$tdir.new
603         touch $DIR/$tdir/$tfile
604         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
605         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
606                 error "rsync failed with xattrs enabled"
607 }
608 run_test 17k "symlinks: rsync with xattrs enabled"
609
610 test_17l() { # LU-279
611         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
612                 skip "no getfattr command"
613
614         test_mkdir $DIR/$tdir
615         touch $DIR/$tdir/$tfile
616         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
617         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
618                 # -h to not follow symlinks. -m '' to list all the xattrs.
619                 # grep to remove first line: '# file: $path'.
620                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
621                 do
622                         lgetxattr_size_check $path $xattr ||
623                                 error "lgetxattr_size_check $path $xattr failed"
624                 done
625         done
626 }
627 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
628
629 # LU-1540
630 test_17m() {
631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
632         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
633                 skip_env "ldiskfs only test"
634         remote_mds_nodsh && skip "remote MDS with nodsh"
635         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
636         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
637                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
638
639         local short_sym="0123456789"
640         local wdir=$DIR/$tdir
641         local i
642
643         test_mkdir $wdir
644         long_sym=$short_sym
645         # create a long symlink file
646         for ((i = 0; i < 4; ++i)); do
647                 long_sym=${long_sym}${long_sym}
648         done
649
650         echo "create 512 short and long symlink files under $wdir"
651         for ((i = 0; i < 256; ++i)); do
652                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
653                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
654         done
655
656         echo "erase them"
657         rm -f $wdir/*
658         sync
659         wait_delete_completed
660
661         echo "recreate the 512 symlink files with a shorter string"
662         for ((i = 0; i < 512; ++i)); do
663                 # rewrite the symlink file with a shorter string
664                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
665                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
666         done
667
668         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
669         local devname=$(mdsdevname $mds_index)
670
671         echo "stop and checking mds${mds_index}:"
672         # e2fsck should not return error
673         stop mds${mds_index}
674         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
675         rc=$?
676
677         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
678                 error "start mds${mds_index} failed"
679         df $MOUNT > /dev/null 2>&1
680         [ $rc -eq 0 ] ||
681                 error "e2fsck detected error for short/long symlink: rc=$rc"
682         rm -f $wdir/*
683 }
684 run_test 17m "run e2fsck against MDT which contains short/long symlink"
685
686 check_fs_consistency_17n() {
687         local mdt_index
688         local rc=0
689
690         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
691         # so it only check MDT1/MDT2 instead of all of MDTs.
692         for mdt_index in 1 2; do
693                 local devname=$(mdsdevname $mdt_index)
694                 # e2fsck should not return error
695                 stop mds${mdt_index}
696                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
697                         rc=$((rc + $?))
698
699                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
700                         error "mount mds$mdt_index failed"
701                 df $MOUNT > /dev/null 2>&1
702         done
703         return $rc
704 }
705
706 test_17n() {
707         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
709         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
710                 skip_env "ldiskfs only test"
711         remote_mds_nodsh && skip "remote MDS with nodsh"
712         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
713         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
714                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
715
716         local i
717
718         test_mkdir $DIR/$tdir
719         for ((i=0; i<10; i++)); do
720                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
721                         error "create remote dir error $i"
722                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
723                         error "create files under remote dir failed $i"
724         done
725
726         check_fs_consistency_17n ||
727                 error "e2fsck report error after create files under remote dir"
728
729         for ((i = 0; i < 10; i++)); do
730                 rm -rf $DIR/$tdir/remote_dir_${i} ||
731                         error "destroy remote dir error $i"
732         done
733
734         check_fs_consistency_17n ||
735                 error "e2fsck report error after unlink files under remote dir"
736
737         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
738                 skip "lustre < 2.4.50 does not support migrate mv"
739
740         for ((i = 0; i < 10; i++)); do
741                 mkdir -p $DIR/$tdir/remote_dir_${i}
742                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
743                         error "create files under remote dir failed $i"
744                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
745                         error "migrate remote dir error $i"
746         done
747         check_fs_consistency_17n || error "e2fsck report error after migration"
748
749         for ((i = 0; i < 10; i++)); do
750                 rm -rf $DIR/$tdir/remote_dir_${i} ||
751                         error "destroy remote dir error $i"
752         done
753
754         check_fs_consistency_17n || error "e2fsck report error after unlink"
755 }
756 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
757
758 test_17o() {
759         remote_mds_nodsh && skip "remote MDS with nodsh"
760         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
761                 skip "Need MDS version at least 2.3.64"
762
763         local wdir=$DIR/${tdir}o
764         local mdt_index
765         local rc=0
766
767         test_mkdir $wdir
768         touch $wdir/$tfile
769         mdt_index=$($LFS getstripe -m $wdir/$tfile)
770         mdt_index=$((mdt_index + 1))
771
772         cancel_lru_locks mdc
773         #fail mds will wait the failover finish then set
774         #following fail_loc to avoid interfer the recovery process.
775         fail mds${mdt_index}
776
777         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
778         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
779         ls -l $wdir/$tfile && rc=1
780         do_facet mds${mdt_index} lctl set_param fail_loc=0
781         [[ $rc -eq 0 ]] || error "stat file should fail"
782 }
783 run_test 17o "stat file with incompat LMA feature"
784
785 test_18() {
786         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
787         ls $DIR || error "Failed to ls $DIR: $?"
788 }
789 run_test 18 "touch .../f ; ls ... =============================="
790
791 test_19a() {
792         touch $DIR/$tfile
793         ls -l $DIR
794         rm $DIR/$tfile
795         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
796 }
797 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
798
799 test_19b() {
800         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
801 }
802 run_test 19b "ls -l .../f19 (should return error) =============="
803
804 test_19c() {
805         [ $RUNAS_ID -eq $UID ] &&
806                 skip_env "RUNAS_ID = UID = $UID -- skipping"
807
808         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
809 }
810 run_test 19c "$RUNAS touch .../f19 (should return error) =="
811
812 test_19d() {
813         cat $DIR/f19 && error || true
814 }
815 run_test 19d "cat .../f19 (should return error) =============="
816
817 test_20() {
818         touch $DIR/$tfile
819         rm $DIR/$tfile
820         touch $DIR/$tfile
821         rm $DIR/$tfile
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
825 }
826 run_test 20 "touch .../f ; ls -l ..."
827
828 test_21() {
829         test_mkdir $DIR/$tdir
830         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
831         ln -s dangle $DIR/$tdir/link
832         echo foo >> $DIR/$tdir/link
833         cat $DIR/$tdir/dangle
834         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
835         $CHECKSTAT -f -t file $DIR/$tdir/link ||
836                 error "$tdir/link not linked to a file"
837 }
838 run_test 21 "write to dangling link"
839
840 test_22() {
841         local wdir=$DIR/$tdir
842         test_mkdir $wdir
843         chown $RUNAS_ID:$RUNAS_GID $wdir
844         (cd $wdir || error "cd $wdir failed";
845                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
846                 $RUNAS tar xf -)
847         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
848         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
849         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
850                 error "checkstat -u failed"
851 }
852 run_test 22 "unpack tar archive as non-root user"
853
854 # was test_23
855 test_23a() {
856         test_mkdir $DIR/$tdir
857         local file=$DIR/$tdir/$tfile
858
859         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
860         openfile -f O_CREAT:O_EXCL $file &&
861                 error "$file recreate succeeded" || true
862 }
863 run_test 23a "O_CREAT|O_EXCL in subdir"
864
865 test_23b() { # bug 18988
866         test_mkdir $DIR/$tdir
867         local file=$DIR/$tdir/$tfile
868
869         rm -f $file
870         echo foo > $file || error "write filed"
871         echo bar >> $file || error "append filed"
872         $CHECKSTAT -s 8 $file || error "wrong size"
873         rm $file
874 }
875 run_test 23b "O_APPEND check"
876
877 # LU-9409, size with O_APPEND and tiny writes
878 test_23c() {
879         local file=$DIR/$tfile
880
881         # single dd
882         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
883         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
884         rm -f $file
885
886         # racing tiny writes
887         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
888         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
889         wait
890         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
891         rm -f $file
892
893         #racing tiny & normal writes
894         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
895         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
896         wait
897         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
898         rm -f $file
899
900         #racing tiny & normal writes 2, ugly numbers
901         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
902         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
903         wait
904         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
905         rm -f $file
906 }
907 run_test 23c "O_APPEND size checks for tiny writes"
908
909 # LU-11069 file offset is correct after appending writes
910 test_23d() {
911         local file=$DIR/$tfile
912         local offset
913
914         echo CentaurHauls > $file
915         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
916         if ((offset != 26)); then
917                 error "wrong offset, expected 26, got '$offset'"
918         fi
919 }
920 run_test 23d "file offset is correct after appending writes"
921
922 # rename sanity
923 test_24a() {
924         echo '-- same directory rename'
925         test_mkdir $DIR/$tdir
926         touch $DIR/$tdir/$tfile.1
927         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
928         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
929 }
930 run_test 24a "rename file to non-existent target"
931
932 test_24b() {
933         test_mkdir $DIR/$tdir
934         touch $DIR/$tdir/$tfile.{1,2}
935         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
936         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
937         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
938 }
939 run_test 24b "rename file to existing target"
940
941 test_24c() {
942         test_mkdir $DIR/$tdir
943         test_mkdir $DIR/$tdir/d$testnum.1
944         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
945         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
946         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
947 }
948 run_test 24c "rename directory to non-existent target"
949
950 test_24d() {
951         test_mkdir -c1 $DIR/$tdir
952         test_mkdir -c1 $DIR/$tdir/d$testnum.1
953         test_mkdir -c1 $DIR/$tdir/d$testnum.2
954         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
955         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
956         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
957 }
958 run_test 24d "rename directory to existing target"
959
960 test_24e() {
961         echo '-- cross directory renames --'
962         test_mkdir $DIR/R5a
963         test_mkdir $DIR/R5b
964         touch $DIR/R5a/f
965         mv $DIR/R5a/f $DIR/R5b/g
966         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
967         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
968 }
969 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
970
971 test_24f() {
972         test_mkdir $DIR/R6a
973         test_mkdir $DIR/R6b
974         touch $DIR/R6a/f $DIR/R6b/g
975         mv $DIR/R6a/f $DIR/R6b/g
976         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
977         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
978 }
979 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
980
981 test_24g() {
982         test_mkdir $DIR/R7a
983         test_mkdir $DIR/R7b
984         test_mkdir $DIR/R7a/d
985         mv $DIR/R7a/d $DIR/R7b/e
986         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
987         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
988 }
989 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
990
991 test_24h() {
992         test_mkdir -c1 $DIR/R8a
993         test_mkdir -c1 $DIR/R8b
994         test_mkdir -c1 $DIR/R8a/d
995         test_mkdir -c1 $DIR/R8b/e
996         mrename $DIR/R8a/d $DIR/R8b/e
997         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
998         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
999 }
1000 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1001
1002 test_24i() {
1003         echo "-- rename error cases"
1004         test_mkdir $DIR/R9
1005         test_mkdir $DIR/R9/a
1006         touch $DIR/R9/f
1007         mrename $DIR/R9/f $DIR/R9/a
1008         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1009         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1010         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1011 }
1012 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1013
1014 test_24j() {
1015         test_mkdir $DIR/R10
1016         mrename $DIR/R10/f $DIR/R10/g
1017         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1018         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1019         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1020 }
1021 run_test 24j "source does not exist ============================"
1022
1023 test_24k() {
1024         test_mkdir $DIR/R11a
1025         test_mkdir $DIR/R11a/d
1026         touch $DIR/R11a/f
1027         mv $DIR/R11a/f $DIR/R11a/d
1028         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1029         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1030 }
1031 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1032
1033 # bug 2429 - rename foo foo foo creates invalid file
1034 test_24l() {
1035         f="$DIR/f24l"
1036         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1037 }
1038 run_test 24l "Renaming a file to itself ========================"
1039
1040 test_24m() {
1041         f="$DIR/f24m"
1042         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1043         # on ext3 this does not remove either the source or target files
1044         # though the "expected" operation would be to remove the source
1045         $CHECKSTAT -t file ${f} || error "${f} missing"
1046         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1047 }
1048 run_test 24m "Renaming a file to a hard link to itself ========="
1049
1050 test_24n() {
1051     f="$DIR/f24n"
1052     # this stats the old file after it was renamed, so it should fail
1053     touch ${f}
1054     $CHECKSTAT ${f} || error "${f} missing"
1055     mv ${f} ${f}.rename
1056     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1057     $CHECKSTAT -a ${f} || error "${f} exists"
1058 }
1059 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1060
1061 test_24o() {
1062         test_mkdir $DIR/$tdir
1063         rename_many -s random -v -n 10 $DIR/$tdir
1064 }
1065 run_test 24o "rename of files during htree split"
1066
1067 test_24p() {
1068         test_mkdir $DIR/R12a
1069         test_mkdir $DIR/R12b
1070         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1071         mrename $DIR/R12a $DIR/R12b
1072         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1073         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1074         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1075         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1076 }
1077 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1078
1079 cleanup_multiop_pause() {
1080         trap 0
1081         kill -USR1 $MULTIPID
1082 }
1083
1084 test_24q() {
1085         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1086
1087         test_mkdir $DIR/R13a
1088         test_mkdir $DIR/R13b
1089         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1090         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1091         MULTIPID=$!
1092
1093         trap cleanup_multiop_pause EXIT
1094         mrename $DIR/R13a $DIR/R13b
1095         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1096         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1097         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1098         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1099         cleanup_multiop_pause
1100         wait $MULTIPID || error "multiop close failed"
1101 }
1102 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1103
1104 test_24r() { #bug 3789
1105         test_mkdir $DIR/R14a
1106         test_mkdir $DIR/R14a/b
1107         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1108         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1109         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1110 }
1111 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1112
1113 test_24s() {
1114         test_mkdir $DIR/R15a
1115         test_mkdir $DIR/R15a/b
1116         test_mkdir $DIR/R15a/b/c
1117         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1118         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1119         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1120 }
1121 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1122 test_24t() {
1123         test_mkdir $DIR/R16a
1124         test_mkdir $DIR/R16a/b
1125         test_mkdir $DIR/R16a/b/c
1126         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1127         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1128         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1129 }
1130 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1131
1132 test_24u() { # bug12192
1133         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1134         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1135 }
1136 run_test 24u "create stripe file"
1137
1138 page_size() {
1139         local size
1140         size=$(getconf PAGE_SIZE 2>/dev/null)
1141         echo -n ${size:-4096}
1142 }
1143
1144 simple_cleanup_common() {
1145         local rc=0
1146         trap 0
1147         [ -z "$DIR" -o -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         [ $(facet_fstype $SINGLEMDS) = "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 -o $num_uniq -ne $nrfiles -o \
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         [[ $(lustre_version_code $SINGLEMDS) -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_24A() { # LU-3182
1288         local NFILES=5000
1289
1290         rm -rf $DIR/$tdir
1291         test_mkdir $DIR/$tdir
1292         trap simple_cleanup_common EXIT
1293         createmany -m $DIR/$tdir/$tfile $NFILES
1294         local t=$(ls $DIR/$tdir | wc -l)
1295         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1296         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1297         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1298                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1299         fi
1300
1301         simple_cleanup_common || error "Can not delete directories"
1302 }
1303 run_test 24A "readdir() returns correct number of entries."
1304
1305 test_24B() { # LU-4805
1306         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1307
1308         local count
1309
1310         test_mkdir $DIR/$tdir
1311         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1312                 error "create striped dir failed"
1313
1314         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1315         [ $count -eq 2 ] || error "Expected 2, got $count"
1316
1317         touch $DIR/$tdir/striped_dir/a
1318
1319         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1320         [ $count -eq 3 ] || error "Expected 3, got $count"
1321
1322         touch $DIR/$tdir/striped_dir/.f
1323
1324         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1325         [ $count -eq 4 ] || error "Expected 4, got $count"
1326
1327         rm -rf $DIR/$tdir || error "Can not delete directories"
1328 }
1329 run_test 24B "readdir for striped dir return correct number of entries"
1330
1331 test_24C() {
1332         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1333
1334         mkdir $DIR/$tdir
1335         mkdir $DIR/$tdir/d0
1336         mkdir $DIR/$tdir/d1
1337
1338         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1339                 error "create striped dir failed"
1340
1341         cd $DIR/$tdir/d0/striped_dir
1342
1343         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1344         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1345         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1346
1347         [ "$d0_ino" = "$parent_ino" ] ||
1348                 error ".. wrong, expect $d0_ino, get $parent_ino"
1349
1350         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1351                 error "mv striped dir failed"
1352
1353         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1354
1355         [ "$d1_ino" = "$parent_ino" ] ||
1356                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1357 }
1358 run_test 24C "check .. in striped dir"
1359
1360 test_24E() {
1361         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1363
1364         mkdir -p $DIR/$tdir
1365         mkdir $DIR/$tdir/src_dir
1366         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1367                 error "create remote source failed"
1368
1369         touch $DIR/$tdir/src_dir/src_child/a
1370
1371         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1372                 error "create remote target dir failed"
1373
1374         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1375                 error "create remote target child failed"
1376
1377         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1378                 error "rename dir cross MDT failed!"
1379
1380         find $DIR/$tdir
1381
1382         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1383                 error "src_child still exists after rename"
1384
1385         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1386                 error "missing file(a) after rename"
1387
1388         rm -rf $DIR/$tdir || error "Can not delete directories"
1389 }
1390 run_test 24E "cross MDT rename/link"
1391
1392 test_25a() {
1393         echo '== symlink sanity ============================================='
1394
1395         test_mkdir $DIR/d25
1396         ln -s d25 $DIR/s25
1397         touch $DIR/s25/foo ||
1398                 error "File creation in symlinked directory failed"
1399 }
1400 run_test 25a "create file in symlinked directory ==============="
1401
1402 test_25b() {
1403         [ ! -d $DIR/d25 ] && test_25a
1404         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1405 }
1406 run_test 25b "lookup file in symlinked directory ==============="
1407
1408 test_26a() {
1409         test_mkdir $DIR/d26
1410         test_mkdir $DIR/d26/d26-2
1411         ln -s d26/d26-2 $DIR/s26
1412         touch $DIR/s26/foo || error "File creation failed"
1413 }
1414 run_test 26a "multiple component symlink ======================="
1415
1416 test_26b() {
1417         test_mkdir -p $DIR/$tdir/d26-2
1418         ln -s $tdir/d26-2/foo $DIR/s26-2
1419         touch $DIR/s26-2 || error "File creation failed"
1420 }
1421 run_test 26b "multiple component symlink at end of lookup ======"
1422
1423 test_26c() {
1424         test_mkdir $DIR/d26.2
1425         touch $DIR/d26.2/foo
1426         ln -s d26.2 $DIR/s26.2-1
1427         ln -s s26.2-1 $DIR/s26.2-2
1428         ln -s s26.2-2 $DIR/s26.2-3
1429         chmod 0666 $DIR/s26.2-3/foo
1430 }
1431 run_test 26c "chain of symlinks"
1432
1433 # recursive symlinks (bug 439)
1434 test_26d() {
1435         ln -s d26-3/foo $DIR/d26-3
1436 }
1437 run_test 26d "create multiple component recursive symlink"
1438
1439 test_26e() {
1440         [ ! -h $DIR/d26-3 ] && test_26d
1441         rm $DIR/d26-3
1442 }
1443 run_test 26e "unlink multiple component recursive symlink"
1444
1445 # recursive symlinks (bug 7022)
1446 test_26f() {
1447         test_mkdir $DIR/$tdir
1448         test_mkdir $DIR/$tdir/$tfile
1449         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1450         test_mkdir -p lndir/bar1
1451         test_mkdir $DIR/$tdir/$tfile/$tfile
1452         cd $tfile                || error "cd $tfile failed"
1453         ln -s .. dotdot          || error "ln dotdot failed"
1454         ln -s dotdot/lndir lndir || error "ln lndir failed"
1455         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1456         output=`ls $tfile/$tfile/lndir/bar1`
1457         [ "$output" = bar1 ] && error "unexpected output"
1458         rm -r $tfile             || error "rm $tfile failed"
1459         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1460 }
1461 run_test 26f "rm -r of a directory which has recursive symlink"
1462
1463 test_27a() {
1464         test_mkdir $DIR/$tdir
1465         $LFS getstripe $DIR/$tdir
1466         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1467         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1468         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1469 }
1470 run_test 27a "one stripe file"
1471
1472 test_27b() {
1473         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1474
1475         test_mkdir $DIR/$tdir
1476         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1477         $LFS getstripe -c $DIR/$tdir/$tfile
1478         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1479                 error "two-stripe file doesn't have two stripes"
1480
1481         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1482 }
1483 run_test 27b "create and write to two stripe file"
1484
1485 test_27d() {
1486         test_mkdir $DIR/$tdir
1487         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1488                 error "setstripe failed"
1489         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1490         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1491 }
1492 run_test 27d "create file with default settings"
1493
1494 test_27e() {
1495         # LU-5839 adds check for existed layout before setting it
1496         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1497                 skip "Need MDS version at least 2.7.56"
1498
1499         test_mkdir $DIR/$tdir
1500         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1501         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1502         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1503 }
1504 run_test 27e "setstripe existing file (should return error)"
1505
1506 test_27f() {
1507         test_mkdir $DIR/$tdir
1508         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1509                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1510         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1511                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1512         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1513         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1514 }
1515 run_test 27f "setstripe with bad stripe size (should return error)"
1516
1517 test_27g() {
1518         test_mkdir $DIR/$tdir
1519         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1520         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1521                 error "$DIR/$tdir/$tfile has object"
1522 }
1523 run_test 27g "$LFS getstripe with no objects"
1524
1525 test_27i() {
1526         test_mkdir $DIR/$tdir
1527         touch $DIR/$tdir/$tfile || error "touch failed"
1528         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1529                 error "missing objects"
1530 }
1531 run_test 27i "$LFS getstripe with some objects"
1532
1533 test_27j() {
1534         test_mkdir $DIR/$tdir
1535         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1536                 error "setstripe failed" || true
1537 }
1538 run_test 27j "setstripe with bad stripe offset (should return error)"
1539
1540 test_27k() { # bug 2844
1541         test_mkdir $DIR/$tdir
1542         local file=$DIR/$tdir/$tfile
1543         local ll_max_blksize=$((4 * 1024 * 1024))
1544         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1545         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1546         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1547         dd if=/dev/zero of=$file bs=4k count=1
1548         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1549         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1550 }
1551 run_test 27k "limit i_blksize for broken user apps"
1552
1553 test_27l() {
1554         mcreate $DIR/$tfile || error "creating file"
1555         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1556                 error "setstripe should have failed" || true
1557 }
1558 run_test 27l "check setstripe permissions (should return error)"
1559
1560 test_27m() {
1561         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1562
1563         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1564                    head -n1)
1565         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1566                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1567         fi
1568         trap simple_cleanup_common EXIT
1569         test_mkdir $DIR/$tdir
1570         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1571         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1572                 error "dd should fill OST0"
1573         i=2
1574         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1575                 i=$((i + 1))
1576                 [ $i -gt 256 ] && break
1577         done
1578         i=$((i + 1))
1579         touch $DIR/$tdir/$tfile.$i
1580         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1581             awk '{print $1}'| grep -w "0") ] &&
1582                 error "OST0 was full but new created file still use it"
1583         i=$((i + 1))
1584         touch $DIR/$tdir/$tfile.$i
1585         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1586             awk '{print $1}'| grep -w "0") ] &&
1587                 error "OST0 was full but new created file still use it"
1588         simple_cleanup_common
1589 }
1590 run_test 27m "create file while OST0 was full"
1591
1592 sleep_maxage() {
1593         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1594                       awk '{ print $1 * 2; exit; }')
1595         sleep $delay
1596 }
1597
1598 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1599 # if the OST isn't full anymore.
1600 reset_enospc() {
1601         local OSTIDX=${1:-""}
1602
1603         local list=$(comma_list $(osts_nodes))
1604         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1605
1606         do_nodes $list lctl set_param fail_loc=0
1607         sync    # initiate all OST_DESTROYs from MDS to OST
1608         sleep_maxage
1609 }
1610
1611 exhaust_precreations() {
1612         local OSTIDX=$1
1613         local FAILLOC=$2
1614         local FAILIDX=${3:-$OSTIDX}
1615         local ofacet=ost$((OSTIDX + 1))
1616
1617         test_mkdir -p -c1 $DIR/$tdir
1618         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1619         local mfacet=mds$((mdtidx + 1))
1620         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1621
1622         local OST=$(ostname_from_index $OSTIDX)
1623
1624         # on the mdt's osc
1625         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1626         local last_id=$(do_facet $mfacet lctl get_param -n \
1627                         osc.$mdtosc_proc1.prealloc_last_id)
1628         local next_id=$(do_facet $mfacet lctl get_param -n \
1629                         osc.$mdtosc_proc1.prealloc_next_id)
1630
1631         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1632         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1633
1634         test_mkdir -p $DIR/$tdir/${OST}
1635         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1636 #define OBD_FAIL_OST_ENOSPC              0x215
1637         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1638         echo "Creating to objid $last_id on ost $OST..."
1639         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1640         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1641         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1642         sleep_maxage
1643 }
1644
1645 exhaust_all_precreations() {
1646         local i
1647         for (( i=0; i < OSTCOUNT; i++ )) ; do
1648                 exhaust_precreations $i $1 -1
1649         done
1650 }
1651
1652 test_27n() {
1653         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1655         remote_mds_nodsh && skip "remote MDS with nodsh"
1656         remote_ost_nodsh && skip "remote OST with nodsh"
1657
1658         reset_enospc
1659         rm -f $DIR/$tdir/$tfile
1660         exhaust_precreations 0 0x80000215
1661         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1662         touch $DIR/$tdir/$tfile || error "touch failed"
1663         $LFS getstripe $DIR/$tdir/$tfile
1664         reset_enospc
1665 }
1666 run_test 27n "create file with some full OSTs"
1667
1668 test_27o() {
1669         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1670         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1671         remote_mds_nodsh && skip "remote MDS with nodsh"
1672         remote_ost_nodsh && skip "remote OST with nodsh"
1673
1674         reset_enospc
1675         rm -f $DIR/$tdir/$tfile
1676         exhaust_all_precreations 0x215
1677
1678         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1679
1680         reset_enospc
1681         rm -rf $DIR/$tdir/*
1682 }
1683 run_test 27o "create file with all full OSTs (should error)"
1684
1685 test_27p() {
1686         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1688         remote_mds_nodsh && skip "remote MDS with nodsh"
1689         remote_ost_nodsh && skip "remote OST with nodsh"
1690
1691         reset_enospc
1692         rm -f $DIR/$tdir/$tfile
1693         test_mkdir $DIR/$tdir
1694
1695         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1696         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1697         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1698
1699         exhaust_precreations 0 0x80000215
1700         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1701         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1702         $LFS getstripe $DIR/$tdir/$tfile
1703
1704         reset_enospc
1705 }
1706 run_test 27p "append to a truncated file with some full OSTs"
1707
1708 test_27q() {
1709         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1711         remote_mds_nodsh && skip "remote MDS with nodsh"
1712         remote_ost_nodsh && skip "remote OST with nodsh"
1713
1714         reset_enospc
1715         rm -f $DIR/$tdir/$tfile
1716
1717         test_mkdir $DIR/$tdir
1718         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1719         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1720                 error "truncate $DIR/$tdir/$tfile failed"
1721         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1722
1723         exhaust_all_precreations 0x215
1724
1725         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1726         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1727
1728         reset_enospc
1729 }
1730 run_test 27q "append to truncated file with all OSTs full (should error)"
1731
1732 test_27r() {
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_precreations 0 0x80000215
1741
1742         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1743
1744         reset_enospc
1745 }
1746 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1747
1748 test_27s() { # bug 10725
1749         test_mkdir $DIR/$tdir
1750         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1751         local stripe_count=0
1752         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1753         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1754                 error "stripe width >= 2^32 succeeded" || true
1755
1756 }
1757 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1758
1759 test_27t() { # bug 10864
1760         WDIR=$(pwd)
1761         WLFS=$(which lfs)
1762         cd $DIR
1763         touch $tfile
1764         $WLFS getstripe $tfile
1765         cd $WDIR
1766 }
1767 run_test 27t "check that utils parse path correctly"
1768
1769 test_27u() { # bug 4900
1770         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1771         remote_mds_nodsh && skip "remote MDS with nodsh"
1772
1773         local index
1774         local list=$(comma_list $(mdts_nodes))
1775
1776 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1777         do_nodes $list $LCTL set_param fail_loc=0x139
1778         test_mkdir -p $DIR/$tdir
1779         trap simple_cleanup_common EXIT
1780         createmany -o $DIR/$tdir/t- 1000
1781         do_nodes $list $LCTL set_param fail_loc=0
1782
1783         TLOG=$TMP/$tfile.getstripe
1784         $LFS getstripe $DIR/$tdir > $TLOG
1785         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1786         unlinkmany $DIR/$tdir/t- 1000
1787         trap 0
1788         [[ $OBJS -gt 0 ]] &&
1789                 error "$OBJS objects created on OST-0. See $TLOG" ||
1790                 rm -f $TLOG
1791 }
1792 run_test 27u "skip object creation on OSC w/o objects"
1793
1794 test_27v() { # bug 4900
1795         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1797         remote_mds_nodsh && skip "remote MDS with nodsh"
1798         remote_ost_nodsh && skip "remote OST with nodsh"
1799
1800         exhaust_all_precreations 0x215
1801         reset_enospc
1802
1803         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1804
1805         touch $DIR/$tdir/$tfile
1806         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1807         # all except ost1
1808         for (( i=1; i < OSTCOUNT; i++ )); do
1809                 do_facet ost$i lctl set_param fail_loc=0x705
1810         done
1811         local START=`date +%s`
1812         createmany -o $DIR/$tdir/$tfile 32
1813
1814         local FINISH=`date +%s`
1815         local TIMEOUT=`lctl get_param -n timeout`
1816         local PROCESS=$((FINISH - START))
1817         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1818                error "$FINISH - $START >= $TIMEOUT / 2"
1819         sleep $((TIMEOUT / 2 - PROCESS))
1820         reset_enospc
1821 }
1822 run_test 27v "skip object creation on slow OST"
1823
1824 test_27w() { # bug 10997
1825         test_mkdir $DIR/$tdir
1826         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1827         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1828                 error "stripe size $size != 65536" || true
1829         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1830                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1831 }
1832 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1833
1834 test_27wa() {
1835         [[ $OSTCOUNT -lt 2 ]] &&
1836                 skip_env "skipping multiple stripe count/offset test"
1837
1838         test_mkdir $DIR/$tdir
1839         for i in $(seq 1 $OSTCOUNT); do
1840                 offset=$((i - 1))
1841                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1842                         error "setstripe -c $i -i $offset failed"
1843                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1844                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1845                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1846                 [ $index -ne $offset ] &&
1847                         error "stripe offset $index != $offset" || true
1848         done
1849 }
1850 run_test 27wa "check $LFS setstripe -c -i options"
1851
1852 test_27x() {
1853         remote_ost_nodsh && skip "remote OST with nodsh"
1854         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1856
1857         OFFSET=$(($OSTCOUNT - 1))
1858         OSTIDX=0
1859         local OST=$(ostname_from_index $OSTIDX)
1860
1861         test_mkdir $DIR/$tdir
1862         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1863         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1864         sleep_maxage
1865         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1866         for i in $(seq 0 $OFFSET); do
1867                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1868                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1869                 error "OST0 was degraded but new created file still use it"
1870         done
1871         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1872 }
1873 run_test 27x "create files while OST0 is degraded"
1874
1875 test_27y() {
1876         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1877         remote_mds_nodsh && skip "remote MDS with nodsh"
1878         remote_ost_nodsh && skip "remote OST with nodsh"
1879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1880
1881         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1882         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1883                 osc.$mdtosc.prealloc_last_id)
1884         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1885                 osc.$mdtosc.prealloc_next_id)
1886         local fcount=$((last_id - next_id))
1887         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1888         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1889
1890         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1891                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1892         local OST_DEACTIVE_IDX=-1
1893         local OSC
1894         local OSTIDX
1895         local OST
1896
1897         for OSC in $MDS_OSCS; do
1898                 OST=$(osc_to_ost $OSC)
1899                 OSTIDX=$(index_from_ostuuid $OST)
1900                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1901                         OST_DEACTIVE_IDX=$OSTIDX
1902                 fi
1903                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1904                         echo $OSC "is Deactivated:"
1905                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1906                 fi
1907         done
1908
1909         OSTIDX=$(index_from_ostuuid $OST)
1910         test_mkdir $DIR/$tdir
1911         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1912
1913         for OSC in $MDS_OSCS; do
1914                 OST=$(osc_to_ost $OSC)
1915                 OSTIDX=$(index_from_ostuuid $OST)
1916                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1917                         echo $OST "is degraded:"
1918                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1919                                                 obdfilter.$OST.degraded=1
1920                 fi
1921         done
1922
1923         sleep_maxage
1924         createmany -o $DIR/$tdir/$tfile $fcount
1925
1926         for OSC in $MDS_OSCS; do
1927                 OST=$(osc_to_ost $OSC)
1928                 OSTIDX=$(index_from_ostuuid $OST)
1929                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1930                         echo $OST "is recovered from degraded:"
1931                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1932                                                 obdfilter.$OST.degraded=0
1933                 else
1934                         do_facet $SINGLEMDS lctl --device %$OSC activate
1935                 fi
1936         done
1937
1938         # all osp devices get activated, hence -1 stripe count restored
1939         local stripe_count=0
1940
1941         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1942         # devices get activated.
1943         sleep_maxage
1944         $LFS setstripe -c -1 $DIR/$tfile
1945         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1946         rm -f $DIR/$tfile
1947         [ $stripe_count -ne $OSTCOUNT ] &&
1948                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1949         return 0
1950 }
1951 run_test 27y "create files while OST0 is degraded and the rest inactive"
1952
1953 check_seq_oid()
1954 {
1955         log "check file $1"
1956
1957         lmm_count=$($GETSTRIPE -c $1)
1958         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1959         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1960
1961         local old_ifs="$IFS"
1962         IFS=$'[:]'
1963         fid=($($LFS path2fid $1))
1964         IFS="$old_ifs"
1965
1966         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1967         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1968
1969         # compare lmm_seq and lu_fid->f_seq
1970         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1971         # compare lmm_object_id and lu_fid->oid
1972         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1973
1974         # check the trusted.fid attribute of the OST objects of the file
1975         local have_obdidx=false
1976         local stripe_nr=0
1977         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1978                 # skip lines up to and including "obdidx"
1979                 [ -z "$obdidx" ] && break
1980                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1981                 $have_obdidx || continue
1982
1983                 local ost=$((obdidx + 1))
1984                 local dev=$(ostdevname $ost)
1985                 local oid_hex
1986
1987                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1988
1989                 seq=$(echo $seq | sed -e "s/^0x//g")
1990                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1991                         oid_hex=$(echo $oid)
1992                 else
1993                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1994                 fi
1995                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1996
1997                 local ff=""
1998                 #
1999                 # Don't unmount/remount the OSTs if we don't need to do that.
2000                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2001                 # update too, until that use mount/ll_decode_filter_fid/mount.
2002                 # Re-enable when debugfs will understand new filter_fid.
2003                 #
2004                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2005                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2006                                 $dev 2>/dev/null" | grep "parent=")
2007                 fi
2008                 if [ -z "$ff" ]; then
2009                         stop ost$ost
2010                         mount_fstype ost$ost
2011                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2012                                 $(facet_mntpt ost$ost)/$obj_file)
2013                         unmount_fstype ost$ost
2014                         start ost$ost $dev $OST_MOUNT_OPTS
2015                         clients_up
2016                 fi
2017
2018                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2019
2020                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2021
2022                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2023                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2024                 #
2025                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2026                 #       stripe_size=1048576 component_id=1 component_start=0 \
2027                 #       component_end=33554432
2028                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2029                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2030                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2031                 local ff_pstripe
2032                 if grep -q 'stripe=' <<<$ff; then
2033                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2034                 else
2035                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2036                         # into f_ver in this case.  See comment on ff_parent.
2037                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2038                 fi
2039
2040                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2041                 [ $ff_pseq = $lmm_seq ] ||
2042                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2043                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2044                 [ $ff_poid = $lmm_oid ] ||
2045                         error "FF parent OID $ff_poid != $lmm_oid"
2046                 (($ff_pstripe == $stripe_nr)) ||
2047                         error "FF stripe $ff_pstripe != $stripe_nr"
2048
2049                 stripe_nr=$((stripe_nr + 1))
2050                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2051                         continue
2052                 if grep -q 'stripe_count=' <<<$ff; then
2053                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2054                                             -e 's/ .*//' <<<$ff)
2055                         [ $lmm_count = $ff_scnt ] ||
2056                                 error "FF stripe count $lmm_count != $ff_scnt"
2057                 fi
2058         done
2059 }
2060
2061 test_27z() {
2062         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2063         remote_ost_nodsh && skip "remote OST with nodsh"
2064
2065         test_mkdir $DIR/$tdir
2066         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2067                 { error "setstripe -c -1 failed"; return 1; }
2068         # We need to send a write to every object to get parent FID info set.
2069         # This _should_ also work for setattr, but does not currently.
2070         # touch $DIR/$tdir/$tfile-1 ||
2071         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2072                 { error "dd $tfile-1 failed"; return 2; }
2073         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2074                 { error "setstripe -c -1 failed"; return 3; }
2075         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2076                 { error "dd $tfile-2 failed"; return 4; }
2077
2078         # make sure write RPCs have been sent to OSTs
2079         sync; sleep 5; sync
2080
2081         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2082         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2083 }
2084 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2085
2086 test_27A() { # b=19102
2087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2088
2089         save_layout_restore_at_exit $MOUNT
2090         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2091         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2092                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2093         local default_size=$($GETSTRIPE -S $MOUNT)
2094         local default_offset=$($GETSTRIPE -i $MOUNT)
2095         local dsize=$(do_facet $SINGLEMDS \
2096                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2097         [ $default_size -eq $dsize ] ||
2098                 error "stripe size $default_size != $dsize"
2099         [ $default_offset -eq -1 ] ||
2100                 error "stripe offset $default_offset != -1"
2101 }
2102 run_test 27A "check filesystem-wide default LOV EA values"
2103
2104 test_27B() { # LU-2523
2105         test_mkdir $DIR/$tdir
2106         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2107         touch $DIR/$tdir/f0
2108         # open f1 with O_LOV_DELAY_CREATE
2109         # rename f0 onto f1
2110         # call setstripe ioctl on open file descriptor for f1
2111         # close
2112         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2113                 $DIR/$tdir/f0
2114
2115         rm -f $DIR/$tdir/f1
2116         # open f1 with O_LOV_DELAY_CREATE
2117         # unlink f1
2118         # call setstripe ioctl on open file descriptor for f1
2119         # close
2120         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2121
2122         # Allow multiop to fail in imitation of NFS's busted semantics.
2123         true
2124 }
2125 run_test 27B "call setstripe on open unlinked file/rename victim"
2126
2127 test_27C() { #LU-2871
2128         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2129
2130         declare -a ost_idx
2131         local index
2132         local found
2133         local i
2134         local j
2135
2136         test_mkdir $DIR/$tdir
2137         cd $DIR/$tdir
2138         for i in $(seq 0 $((OSTCOUNT - 1))); do
2139                 # set stripe across all OSTs starting from OST$i
2140                 $SETSTRIPE -i $i -c -1 $tfile$i
2141                 # get striping information
2142                 ost_idx=($($GETSTRIPE $tfile$i |
2143                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2144                 echo ${ost_idx[@]}
2145
2146                 # check the layout
2147                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2148                         error "${#ost_idx[@]} != $OSTCOUNT"
2149
2150                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2151                         found=0
2152                         for j in $(echo ${ost_idx[@]}); do
2153                                 if [ $index -eq $j ]; then
2154                                         found=1
2155                                         break
2156                                 fi
2157                         done
2158                         [ $found = 1 ] ||
2159                                 error "Can not find $index in ${ost_idx[@]}"
2160                 done
2161         done
2162 }
2163 run_test 27C "check full striping across all OSTs"
2164
2165 test_27D() {
2166         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2167         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2168         remote_mds_nodsh && skip "remote MDS with nodsh"
2169
2170         local POOL=${POOL:-testpool}
2171         local first_ost=0
2172         local last_ost=$(($OSTCOUNT - 1))
2173         local ost_step=1
2174         local ost_list=$(seq $first_ost $ost_step $last_ost)
2175         local ost_range="$first_ost $last_ost $ost_step"
2176
2177         if ! combined_mgs_mds ; then
2178                 mount_mgs_client
2179         fi
2180
2181         test_mkdir $DIR/$tdir
2182         pool_add $POOL || error "pool_add failed"
2183         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2184
2185         local skip27D
2186         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2187                 skip27D+="-s 29"
2188         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2189           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2190                 skip27D+=" -s 30,31"
2191         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2192                 error "llapi_layout_test failed"
2193
2194         destroy_test_pools || error "destroy test pools failed"
2195
2196         if ! combined_mgs_mds ; then
2197                 umount_mgs_client
2198         fi
2199 }
2200 run_test 27D "validate llapi_layout API"
2201
2202 # Verify that default_easize is increased from its initial value after
2203 # accessing a widely striped file.
2204 test_27E() {
2205         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2206         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2207                 skip "client does not have LU-3338 fix"
2208
2209         # 72 bytes is the minimum space required to store striping
2210         # information for a file striped across one OST:
2211         # (sizeof(struct lov_user_md_v3) +
2212         #  sizeof(struct lov_user_ost_data_v1))
2213         local min_easize=72
2214         $LCTL set_param -n llite.*.default_easize $min_easize ||
2215                 error "lctl set_param failed"
2216         local easize=$($LCTL get_param -n llite.*.default_easize)
2217
2218         [ $easize -eq $min_easize ] ||
2219                 error "failed to set default_easize"
2220
2221         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2222                 error "setstripe failed"
2223         cat $DIR/$tfile
2224         rm $DIR/$tfile
2225
2226         easize=$($LCTL get_param -n llite.*.default_easize)
2227
2228         [ $easize -gt $min_easize ] ||
2229                 error "default_easize not updated"
2230 }
2231 run_test 27E "check that default extended attribute size properly increases"
2232
2233 test_27F() { # LU-5346/LU-7975
2234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2235         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2236         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2237                 skip "Need MDS version at least 2.8.51"
2238         remote_ost_nodsh && skip "remote OST with nodsh"
2239
2240         test_mkdir $DIR/$tdir
2241         rm -f $DIR/$tdir/f0
2242         $SETSTRIPE -c 2 $DIR/$tdir
2243
2244         # stop all OSTs to reproduce situation for LU-7975 ticket
2245         for num in $(seq $OSTCOUNT); do
2246                 stop ost$num
2247         done
2248
2249         # open/create f0 with O_LOV_DELAY_CREATE
2250         # truncate f0 to a non-0 size
2251         # close
2252         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2253
2254         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2255         # open/write it again to force delayed layout creation
2256         cat /etc/hosts > $DIR/$tdir/f0 &
2257         catpid=$!
2258
2259         # restart OSTs
2260         for num in $(seq $OSTCOUNT); do
2261                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2262                         error "ost$num failed to start"
2263         done
2264
2265         wait $catpid || error "cat failed"
2266
2267         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2268         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2269
2270 }
2271 run_test 27F "Client resend delayed layout creation with non-zero size"
2272
2273 test_27G() { #LU-10629
2274         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2275         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2276         local POOL=${POOL:-testpool}
2277         local ostrange="0 0 1"
2278
2279         test_mkdir $DIR/$tdir
2280         pool_add $POOL || error "pool_add failed"
2281         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2282         $LFS setstripe -p $POOL $DIR/$tdir
2283
2284         local pool=$($LFS getstripe -p $DIR/$tdir)
2285
2286         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2287
2288         $LFS setstripe -d $DIR/$tdir
2289
2290         pool=$($LFS getstripe -p $DIR/$tdir)
2291
2292         rmdir $DIR/$tdir
2293
2294         [ -z "$pool" ] || error "'$pool' is not empty"
2295 }
2296 run_test 27G "Clear OST pool from stripe"
2297
2298 # createtest also checks that device nodes are created and
2299 # then visible correctly (#2091)
2300 test_28() { # bug 2091
2301         test_mkdir $DIR/d28
2302         $CREATETEST $DIR/d28/ct || error "createtest failed"
2303 }
2304 run_test 28 "create/mknod/mkdir with bad file types ============"
2305
2306 test_29() {
2307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2308
2309         sync; sleep 1; sync # flush out any dirty pages from previous tests
2310         cancel_lru_locks
2311         test_mkdir $DIR/d29
2312         touch $DIR/d29/foo
2313         log 'first d29'
2314         ls -l $DIR/d29
2315
2316         declare -i LOCKCOUNTORIG=0
2317         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2318                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2319         done
2320         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2321
2322         declare -i LOCKUNUSEDCOUNTORIG=0
2323         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2324                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2325         done
2326
2327         log 'second d29'
2328         ls -l $DIR/d29
2329         log 'done'
2330
2331         declare -i LOCKCOUNTCURRENT=0
2332         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2333                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2334         done
2335
2336         declare -i LOCKUNUSEDCOUNTCURRENT=0
2337         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2338                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2339         done
2340
2341         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2342                 $LCTL set_param -n ldlm.dump_namespaces ""
2343                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2344                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2345                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2346                 return 2
2347         fi
2348         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2349                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2350                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2351                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2352                 return 3
2353         fi
2354 }
2355 run_test 29 "IT_GETATTR regression  ============================"
2356
2357 test_30a() { # was test_30
2358         cp $(which ls) $DIR || cp /bin/ls $DIR
2359         $DIR/ls / || error "Can't execute binary from lustre"
2360         rm $DIR/ls
2361 }
2362 run_test 30a "execute binary from Lustre (execve) =============="
2363
2364 test_30b() {
2365         cp `which ls` $DIR || cp /bin/ls $DIR
2366         chmod go+rx $DIR/ls
2367         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2368         rm $DIR/ls
2369 }
2370 run_test 30b "execute binary from Lustre as non-root ==========="
2371
2372 test_30c() { # b=22376
2373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2374
2375         cp `which ls` $DIR || cp /bin/ls $DIR
2376         chmod a-rw $DIR/ls
2377         cancel_lru_locks mdc
2378         cancel_lru_locks osc
2379         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2380         rm -f $DIR/ls
2381 }
2382 run_test 30c "execute binary from Lustre without read perms ===="
2383
2384 test_31a() {
2385         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2386         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2387 }
2388 run_test 31a "open-unlink file =================================="
2389
2390 test_31b() {
2391         touch $DIR/f31 || error "touch $DIR/f31 failed"
2392         ln $DIR/f31 $DIR/f31b || error "ln failed"
2393         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2394         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2395 }
2396 run_test 31b "unlink file with multiple links while open ======="
2397
2398 test_31c() {
2399         touch $DIR/f31 || error "touch $DIR/f31 failed"
2400         ln $DIR/f31 $DIR/f31c || error "ln failed"
2401         multiop_bg_pause $DIR/f31 O_uc ||
2402                 error "multiop_bg_pause for $DIR/f31 failed"
2403         MULTIPID=$!
2404         $MULTIOP $DIR/f31c Ouc
2405         kill -USR1 $MULTIPID
2406         wait $MULTIPID
2407 }
2408 run_test 31c "open-unlink file with multiple links ============="
2409
2410 test_31d() {
2411         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2412         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2413 }
2414 run_test 31d "remove of open directory ========================="
2415
2416 test_31e() { # bug 2904
2417         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2418 }
2419 run_test 31e "remove of open non-empty directory ==============="
2420
2421 test_31f() { # bug 4554
2422         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2423
2424         set -vx
2425         test_mkdir $DIR/d31f
2426         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2427         cp /etc/hosts $DIR/d31f
2428         ls -l $DIR/d31f
2429         $GETSTRIPE $DIR/d31f/hosts
2430         multiop_bg_pause $DIR/d31f D_c || return 1
2431         MULTIPID=$!
2432
2433         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2434         test_mkdir $DIR/d31f
2435         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2436         cp /etc/hosts $DIR/d31f
2437         ls -l $DIR/d31f
2438         $GETSTRIPE $DIR/d31f/hosts
2439         multiop_bg_pause $DIR/d31f D_c || return 1
2440         MULTIPID2=$!
2441
2442         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2443         wait $MULTIPID || error "first opendir $MULTIPID failed"
2444
2445         sleep 6
2446
2447         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2448         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2449         set +vx
2450 }
2451 run_test 31f "remove of open directory with open-unlink file ==="
2452
2453 test_31g() {
2454         echo "-- cross directory link --"
2455         test_mkdir -c1 $DIR/${tdir}ga
2456         test_mkdir -c1 $DIR/${tdir}gb
2457         touch $DIR/${tdir}ga/f
2458         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2459         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2460         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2461         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2462         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2463 }
2464 run_test 31g "cross directory link==============="
2465
2466 test_31h() {
2467         echo "-- cross directory link --"
2468         test_mkdir -c1 $DIR/${tdir}
2469         test_mkdir -c1 $DIR/${tdir}/dir
2470         touch $DIR/${tdir}/f
2471         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2472         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2473         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2474         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2475         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2476 }
2477 run_test 31h "cross directory link under child==============="
2478
2479 test_31i() {
2480         echo "-- cross directory link --"
2481         test_mkdir -c1 $DIR/$tdir
2482         test_mkdir -c1 $DIR/$tdir/dir
2483         touch $DIR/$tdir/dir/f
2484         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2485         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2486         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2487         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2488         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2489 }
2490 run_test 31i "cross directory link under parent==============="
2491
2492 test_31j() {
2493         test_mkdir -c1 -p $DIR/$tdir
2494         test_mkdir -c1 -p $DIR/$tdir/dir1
2495         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2496         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2497         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2498         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2499         return 0
2500 }
2501 run_test 31j "link for directory==============="
2502
2503 test_31k() {
2504         test_mkdir -c1 -p $DIR/$tdir
2505         touch $DIR/$tdir/s
2506         touch $DIR/$tdir/exist
2507         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2508         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2509         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2510         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2511         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2512         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2513         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2514         return 0
2515 }
2516 run_test 31k "link to file: the same, non-existing, dir==============="
2517
2518 test_31m() {
2519         mkdir $DIR/d31m
2520         touch $DIR/d31m/s
2521         mkdir $DIR/d31m2
2522         touch $DIR/d31m2/exist
2523         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2524         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2525         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2526         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2527         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2528         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2529         return 0
2530 }
2531 run_test 31m "link to file: the same, non-existing, dir==============="
2532
2533 test_31n() {
2534         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2535         nlink=$(stat --format=%h $DIR/$tfile)
2536         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2537         local fd=$(free_fd)
2538         local cmd="exec $fd<$DIR/$tfile"
2539         eval $cmd
2540         cmd="exec $fd<&-"
2541         trap "eval $cmd" EXIT
2542         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2543         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2544         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2545         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2546         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2547         eval $cmd
2548 }
2549 run_test 31n "check link count of unlinked file"
2550
2551 link_one() {
2552         local TEMPNAME=$(mktemp $1_XXXXXX)
2553         mlink $TEMPNAME $1 2> /dev/null &&
2554                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2555         munlink $TEMPNAME
2556 }
2557
2558 test_31o() { # LU-2901
2559         test_mkdir $DIR/$tdir
2560         for LOOP in $(seq 100); do
2561                 rm -f $DIR/$tdir/$tfile*
2562                 for THREAD in $(seq 8); do
2563                         link_one $DIR/$tdir/$tfile.$LOOP &
2564                 done
2565                 wait
2566                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2567                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2568                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2569                         break || true
2570         done
2571 }
2572 run_test 31o "duplicate hard links with same filename"
2573
2574 test_31p() {
2575         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2576
2577         test_mkdir $DIR/$tdir
2578         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2579         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2580
2581         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2582                 error "open unlink test1 failed"
2583         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2584                 error "open unlink test2 failed"
2585
2586         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2587                 error "test1 still exists"
2588         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2589                 error "test2 still exists"
2590 }
2591 run_test 31p "remove of open striped directory"
2592
2593 cleanup_test32_mount() {
2594         local rc=0
2595         trap 0
2596         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2597         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2598         losetup -d $loopdev || true
2599         rm -rf $DIR/$tdir
2600         return $rc
2601 }
2602
2603 test_32a() {
2604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2605
2606         echo "== more mountpoints and symlinks ================="
2607         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2608         trap cleanup_test32_mount EXIT
2609         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2610         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2611                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2612         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2613                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2614         cleanup_test32_mount
2615 }
2616 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2617
2618 test_32b() {
2619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2620
2621         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2622         trap cleanup_test32_mount EXIT
2623         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2624         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2625                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2626         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2627                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2628         cleanup_test32_mount
2629 }
2630 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2631
2632 test_32c() {
2633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2634
2635         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2636         trap cleanup_test32_mount EXIT
2637         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2638         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2639                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2640         test_mkdir -p $DIR/$tdir/d2/test_dir
2641         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2642                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2643         cleanup_test32_mount
2644 }
2645 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2646
2647 test_32d() {
2648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2649
2650         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2651         trap cleanup_test32_mount EXIT
2652         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2653         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2654                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2655         test_mkdir -p $DIR/$tdir/d2/test_dir
2656         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2657                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2658         cleanup_test32_mount
2659 }
2660 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2661
2662 test_32e() {
2663         rm -fr $DIR/$tdir
2664         test_mkdir -p $DIR/$tdir/tmp
2665         local tmp_dir=$DIR/$tdir/tmp
2666         ln -s $DIR/$tdir $tmp_dir/symlink11
2667         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2668         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2669         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2670 }
2671 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2672
2673 test_32f() {
2674         rm -fr $DIR/$tdir
2675         test_mkdir -p $DIR/$tdir/tmp
2676         local tmp_dir=$DIR/$tdir/tmp
2677         ln -s $DIR/$tdir $tmp_dir/symlink11
2678         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2679         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2680         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2681 }
2682 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2683
2684 test_32g() {
2685         local tmp_dir=$DIR/$tdir/tmp
2686         test_mkdir -p $tmp_dir
2687         test_mkdir $DIR/${tdir}2
2688         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2689         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2690         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2691         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2692         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2693         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2694 }
2695 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2696
2697 test_32h() {
2698         rm -fr $DIR/$tdir $DIR/${tdir}2
2699         tmp_dir=$DIR/$tdir/tmp
2700         test_mkdir -p $tmp_dir
2701         test_mkdir $DIR/${tdir}2
2702         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2703         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2704         ls $tmp_dir/symlink12 || error "listing symlink12"
2705         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2706 }
2707 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2708
2709 test_32i() {
2710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2711
2712         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2713         trap cleanup_test32_mount EXIT
2714         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2715         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2716                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2717         touch $DIR/$tdir/test_file
2718         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
2719                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
2720         cleanup_test32_mount
2721 }
2722 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2723
2724 test_32j() {
2725         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2726
2727         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2728         trap cleanup_test32_mount EXIT
2729         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2730         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2731                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2732         touch $DIR/$tdir/test_file
2733         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
2734                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
2735         cleanup_test32_mount
2736 }
2737 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2738
2739 test_32k() {
2740         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2741
2742         rm -fr $DIR/$tdir
2743         trap cleanup_test32_mount EXIT
2744         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2745         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2746                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2747         test_mkdir -p $DIR/$tdir/d2
2748         touch $DIR/$tdir/d2/test_file || error "touch failed"
2749         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2750                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
2751         cleanup_test32_mount
2752 }
2753 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2754
2755 test_32l() {
2756         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2757
2758         rm -fr $DIR/$tdir
2759         trap cleanup_test32_mount EXIT
2760         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2761         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2762                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2763         test_mkdir -p $DIR/$tdir/d2
2764         touch $DIR/$tdir/d2/test_file || error "touch failed"
2765         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2766                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
2767         cleanup_test32_mount
2768 }
2769 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2770
2771 test_32m() {
2772         rm -fr $DIR/d32m
2773         test_mkdir -p $DIR/d32m/tmp
2774         TMP_DIR=$DIR/d32m/tmp
2775         ln -s $DIR $TMP_DIR/symlink11
2776         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2777         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
2778                 error "symlink11 not a link"
2779         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
2780                 error "symlink01 not a link"
2781 }
2782 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2783
2784 test_32n() {
2785         rm -fr $DIR/d32n
2786         test_mkdir -p $DIR/d32n/tmp
2787         TMP_DIR=$DIR/d32n/tmp
2788         ln -s $DIR $TMP_DIR/symlink11
2789         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2790         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
2791         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
2792 }
2793 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2794
2795 test_32o() {
2796         touch $DIR/$tfile
2797         test_mkdir -p $DIR/d32o/tmp
2798         TMP_DIR=$DIR/d32o/tmp
2799         ln -s $DIR/$tfile $TMP_DIR/symlink12
2800         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2801         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
2802                 error "symlink12 not a link"
2803         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
2804         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
2805                 error "$DIR/d32o/tmp/symlink12 not file type"
2806         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
2807                 error "$DIR/d32o/symlink02 not file type"
2808 }
2809 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2810
2811 test_32p() {
2812         log 32p_1
2813         rm -fr $DIR/d32p
2814         log 32p_2
2815         rm -f $DIR/$tfile
2816         log 32p_3
2817         touch $DIR/$tfile
2818         log 32p_4
2819         test_mkdir -p $DIR/d32p/tmp
2820         log 32p_5
2821         TMP_DIR=$DIR/d32p/tmp
2822         log 32p_6
2823         ln -s $DIR/$tfile $TMP_DIR/symlink12
2824         log 32p_7
2825         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2826         log 32p_8
2827         cat $DIR/d32p/tmp/symlink12 ||
2828                 error "Can't open $DIR/d32p/tmp/symlink12"
2829         log 32p_9
2830         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
2831         log 32p_10
2832 }
2833 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2834
2835 test_32q() {
2836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2837
2838         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2839         trap cleanup_test32_mount EXIT
2840         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2841         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2842         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2843                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2844         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2845         cleanup_test32_mount
2846 }
2847 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2848
2849 test_32r() {
2850         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2851
2852         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2853         trap cleanup_test32_mount EXIT
2854         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2855         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
2856         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2857                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2858         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2859         cleanup_test32_mount
2860 }
2861 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2862
2863 test_33aa() {
2864         rm -f $DIR/$tfile
2865         touch $DIR/$tfile
2866         chmod 444 $DIR/$tfile
2867         chown $RUNAS_ID $DIR/$tfile
2868         log 33_1
2869         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2870         log 33_2
2871 }
2872 run_test 33aa "write file with mode 444 (should return error)"
2873
2874 test_33a() {
2875         rm -fr $DIR/$tdir
2876         test_mkdir $DIR/$tdir
2877         chown $RUNAS_ID $DIR/$tdir
2878         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2879                 error "$RUNAS create $tdir/$tfile failed"
2880         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2881                 error "open RDWR" || true
2882 }
2883 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2884
2885 test_33b() {
2886         rm -fr $DIR/$tdir
2887         test_mkdir $DIR/$tdir
2888         chown $RUNAS_ID $DIR/$tdir
2889         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2890 }
2891 run_test 33b "test open file with malformed flags (No panic)"
2892
2893 test_33c() {
2894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2895         remote_ost_nodsh && skip "remote OST with nodsh"
2896
2897         local ostnum
2898         local ostname
2899         local write_bytes
2900         local all_zeros
2901
2902         all_zeros=:
2903         rm -fr $DIR/$tdir
2904         test_mkdir $DIR/$tdir
2905         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2906
2907         sync
2908         for ostnum in $(seq $OSTCOUNT); do
2909                 # test-framework's OST numbering is one-based, while Lustre's
2910                 # is zero-based
2911                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2912                 # Parsing llobdstat's output sucks; we could grep the /proc
2913                 # path, but that's likely to not be as portable as using the
2914                 # llobdstat utility.  So we parse lctl output instead.
2915                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2916                         obdfilter/$ostname/stats |
2917                         awk '/^write_bytes/ {print $7}' )
2918                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2919                 if (( ${write_bytes:-0} > 0 ))
2920                 then
2921                         all_zeros=false
2922                         break;
2923                 fi
2924         done
2925
2926         $all_zeros || return 0
2927
2928         # Write four bytes
2929         echo foo > $DIR/$tdir/bar
2930         # Really write them
2931         sync
2932
2933         # Total up write_bytes after writing.  We'd better find non-zeros.
2934         for ostnum in $(seq $OSTCOUNT); do
2935                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2936                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2937                         obdfilter/$ostname/stats |
2938                         awk '/^write_bytes/ {print $7}' )
2939                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2940                 if (( ${write_bytes:-0} > 0 ))
2941                 then
2942                         all_zeros=false
2943                         break;
2944                 fi
2945         done
2946
2947         if $all_zeros
2948         then
2949                 for ostnum in $(seq $OSTCOUNT); do
2950                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2951                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2952                         do_facet ost$ostnum lctl get_param -n \
2953                                 obdfilter/$ostname/stats
2954                 done
2955                 error "OST not keeping write_bytes stats (b22312)"
2956         fi
2957 }
2958 run_test 33c "test llobdstat and write_bytes"
2959
2960 test_33d() {
2961         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
2962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2963
2964         local MDTIDX=1
2965         local remote_dir=$DIR/$tdir/remote_dir
2966
2967         test_mkdir $DIR/$tdir
2968         $LFS mkdir -i $MDTIDX $remote_dir ||
2969                 error "create remote directory failed"
2970
2971         touch $remote_dir/$tfile
2972         chmod 444 $remote_dir/$tfile
2973         chown $RUNAS_ID $remote_dir/$tfile
2974
2975         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2976
2977         chown $RUNAS_ID $remote_dir
2978         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2979                                         error "create" || true
2980         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2981                                     error "open RDWR" || true
2982         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2983 }
2984 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2985
2986 test_33e() {
2987         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2988
2989         mkdir $DIR/$tdir
2990
2991         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2992         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2993         mkdir $DIR/$tdir/local_dir
2994
2995         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2996         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2997         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2998
2999         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3000                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3001
3002         rmdir $DIR/$tdir/* || error "rmdir failed"
3003
3004         umask 777
3005         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3006         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3007         mkdir $DIR/$tdir/local_dir
3008
3009         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3010         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3011         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3012
3013         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3014                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3015
3016         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3017
3018         umask 000
3019         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3020         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3021         mkdir $DIR/$tdir/local_dir
3022
3023         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3024         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3025         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3026
3027         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3028                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3029 }
3030 run_test 33e "mkdir and striped directory should have same mode"
3031
3032 cleanup_33f() {
3033         trap 0
3034         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3035 }
3036
3037 test_33f() {
3038         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3039         remote_mds_nodsh && skip "remote MDS with nodsh"
3040
3041         mkdir $DIR/$tdir
3042         chmod go+rwx $DIR/$tdir
3043         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3044         trap cleanup_33f EXIT
3045
3046         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3047                 error "cannot create striped directory"
3048
3049         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3050                 error "cannot create files in striped directory"
3051
3052         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3053                 error "cannot remove files in striped directory"
3054
3055         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3056                 error "cannot remove striped directory"
3057
3058         cleanup_33f
3059 }
3060 run_test 33f "nonroot user can create, access, and remove a striped directory"
3061
3062 test_33g() {
3063         mkdir -p $DIR/$tdir/dir2
3064
3065         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3066         echo $err
3067         [[ $err =~ "exists" ]] || error "Not exists error"
3068 }
3069 run_test 33g "nonroot user create already existing root created file"
3070
3071 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3072 test_34a() {
3073         rm -f $DIR/f34
3074         $MCREATE $DIR/f34 || error "mcreate failed"
3075         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3076                 error "getstripe failed"
3077         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3078         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3079                 error "getstripe failed"
3080         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3081                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3082 }
3083 run_test 34a "truncate file that has not been opened ==========="
3084
3085 test_34b() {
3086         [ ! -f $DIR/f34 ] && test_34a
3087         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3088                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3089         $OPENFILE -f O_RDONLY $DIR/f34
3090         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3091                 error "getstripe failed"
3092         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3093                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3094 }
3095 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3096
3097 test_34c() {
3098         [ ! -f $DIR/f34 ] && test_34a
3099         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3100                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3101         $OPENFILE -f O_RDWR $DIR/f34
3102         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
3103         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3104                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3105 }
3106 run_test 34c "O_RDWR opening file-with-size works =============="
3107
3108 test_34d() {
3109         [ ! -f $DIR/f34 ] && test_34a
3110         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3111                 error "dd failed"
3112         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3113                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3114         rm $DIR/f34
3115 }
3116 run_test 34d "write to sparse file ============================="
3117
3118 test_34e() {
3119         rm -f $DIR/f34e
3120         $MCREATE $DIR/f34e || error "mcreate failed"
3121         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3122         $CHECKSTAT -s 1000 $DIR/f34e ||
3123                 error "Size of $DIR/f34e not equal to 1000 bytes"
3124         $OPENFILE -f O_RDWR $DIR/f34e
3125         $CHECKSTAT -s 1000 $DIR/f34e ||
3126                 error "Size of $DIR/f34e not equal to 1000 bytes"
3127 }
3128 run_test 34e "create objects, some with size and some without =="
3129
3130 test_34f() { # bug 6242, 6243
3131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3132
3133         SIZE34F=48000
3134         rm -f $DIR/f34f
3135         $MCREATE $DIR/f34f || error "mcreate failed"
3136         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3137         dd if=$DIR/f34f of=$TMP/f34f
3138         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3139         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3140         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3141         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3142         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3143 }
3144 run_test 34f "read from a file with no objects until EOF ======="
3145
3146 test_34g() {
3147         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3148
3149         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3150                 error "dd failed"
3151         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3152         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3153                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3154         cancel_lru_locks osc
3155         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3156                 error "wrong size after lock cancel"
3157
3158         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3159         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3160                 error "expanding truncate failed"
3161         cancel_lru_locks osc
3162         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3163                 error "wrong expanded size after lock cancel"
3164 }
3165 run_test 34g "truncate long file ==============================="
3166
3167 test_34h() {
3168         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3169
3170         local gid=10
3171         local sz=1000
3172
3173         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3174         sync # Flush the cache so that multiop below does not block on cache
3175              # flush when getting the group lock
3176         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3177         MULTIPID=$!
3178
3179         # Since just timed wait is not good enough, let's do a sync write
3180         # that way we are sure enough time for a roundtrip + processing
3181         # passed + 2 seconds of extra margin.
3182         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
3183         rm $DIR/${tfile}-1
3184         sleep 2
3185
3186         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3187                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3188                 kill -9 $MULTIPID
3189         fi
3190         wait $MULTIPID
3191         local nsz=`stat -c %s $DIR/$tfile`
3192         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3193 }
3194 run_test 34h "ftruncate file under grouplock should not block"
3195
3196 test_35a() {
3197         cp /bin/sh $DIR/f35a
3198         chmod 444 $DIR/f35a
3199         chown $RUNAS_ID $DIR/f35a
3200         $RUNAS $DIR/f35a && error || true
3201         rm $DIR/f35a
3202 }
3203 run_test 35a "exec file with mode 444 (should return and not leak)"
3204
3205 test_36a() {
3206         rm -f $DIR/f36
3207         utime $DIR/f36 || error "utime failed for MDS"
3208 }
3209 run_test 36a "MDS utime check (mknod, utime)"
3210
3211 test_36b() {
3212         echo "" > $DIR/f36
3213         utime $DIR/f36 || error "utime failed for OST"
3214 }
3215 run_test 36b "OST utime check (open, utime)"
3216
3217 test_36c() {
3218         rm -f $DIR/d36/f36
3219         test_mkdir $DIR/d36
3220         chown $RUNAS_ID $DIR/d36
3221         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3222 }
3223 run_test 36c "non-root MDS utime check (mknod, utime)"
3224
3225 test_36d() {
3226         [ ! -d $DIR/d36 ] && test_36c
3227         echo "" > $DIR/d36/f36
3228         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3229 }
3230 run_test 36d "non-root OST utime check (open, utime)"
3231
3232 test_36e() {
3233         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3234
3235         test_mkdir $DIR/$tdir
3236         touch $DIR/$tdir/$tfile
3237         $RUNAS utime $DIR/$tdir/$tfile &&
3238                 error "utime worked, expected failure" || true
3239 }
3240 run_test 36e "utime on non-owned file (should return error)"
3241
3242 subr_36fh() {
3243         local fl="$1"
3244         local LANG_SAVE=$LANG
3245         local LC_LANG_SAVE=$LC_LANG
3246         export LANG=C LC_LANG=C # for date language
3247
3248         DATESTR="Dec 20  2000"
3249         test_mkdir $DIR/$tdir
3250         lctl set_param fail_loc=$fl
3251         date; date +%s
3252         cp /etc/hosts $DIR/$tdir/$tfile
3253         sync & # write RPC generated with "current" inode timestamp, but delayed
3254         sleep 1
3255         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3256         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3257         cancel_lru_locks osc
3258         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3259         date; date +%s
3260         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3261                 echo "BEFORE: $LS_BEFORE" && \
3262                 echo "AFTER : $LS_AFTER" && \
3263                 echo "WANT  : $DATESTR" && \
3264                 error "$DIR/$tdir/$tfile timestamps changed" || true
3265
3266         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3267 }
3268
3269 test_36f() {
3270         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3271
3272         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3273         subr_36fh "0x80000214"
3274 }
3275 run_test 36f "utime on file racing with OST BRW write =========="
3276
3277 test_36g() {
3278         remote_ost_nodsh && skip "remote OST with nodsh"
3279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3280
3281         local fmd_max_age
3282         local fmd_before
3283         local fmd_after
3284
3285         test_mkdir $DIR/$tdir
3286         fmd_max_age=$(do_facet ost1 \
3287                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3288                 head -n 1")
3289
3290         fmd_before=$(do_facet ost1 \
3291                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3292         touch $DIR/$tdir/$tfile
3293         sleep $((fmd_max_age + 12))
3294         fmd_after=$(do_facet ost1 \
3295                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3296
3297         echo "fmd_before: $fmd_before"
3298         echo "fmd_after: $fmd_after"
3299         [[ $fmd_after -gt $fmd_before ]] &&
3300                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3301                 error "fmd didn't expire after ping" || true
3302 }
3303 run_test 36g "filter mod data cache expiry ====================="
3304
3305 test_36h() {
3306         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3307
3308         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3309         subr_36fh "0x80000227"
3310 }
3311 run_test 36h "utime on file racing with OST BRW write =========="
3312
3313 test_36i() {
3314         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3315
3316         test_mkdir $DIR/$tdir
3317         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3318
3319         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3320         local new_mtime=$((mtime + 200))
3321
3322         #change Modify time of striped dir
3323         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3324                         error "change mtime failed"
3325
3326         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3327
3328         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3329 }
3330 run_test 36i "change mtime on striped directory"
3331
3332 # test_37 - duplicate with tests 32q 32r
3333
3334 test_38() {
3335         local file=$DIR/$tfile
3336         touch $file
3337         openfile -f O_DIRECTORY $file
3338         local RC=$?
3339         local ENOTDIR=20
3340         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3341         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3342 }
3343 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3344
3345 test_39a() { # was test_39
3346         touch $DIR/$tfile
3347         touch $DIR/${tfile}2
3348 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3349 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3350 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3351         sleep 2
3352         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3353         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3354                 echo "mtime"
3355                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3356                 echo "atime"
3357                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3358                 echo "ctime"
3359                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3360                 error "O_TRUNC didn't change timestamps"
3361         fi
3362 }
3363 run_test 39a "mtime changed on create"
3364
3365 test_39b() {
3366         test_mkdir -c1 $DIR/$tdir
3367         cp -p /etc/passwd $DIR/$tdir/fopen
3368         cp -p /etc/passwd $DIR/$tdir/flink
3369         cp -p /etc/passwd $DIR/$tdir/funlink
3370         cp -p /etc/passwd $DIR/$tdir/frename
3371         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3372
3373         sleep 1
3374         echo "aaaaaa" >> $DIR/$tdir/fopen
3375         echo "aaaaaa" >> $DIR/$tdir/flink
3376         echo "aaaaaa" >> $DIR/$tdir/funlink
3377         echo "aaaaaa" >> $DIR/$tdir/frename
3378
3379         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3380         local link_new=`stat -c %Y $DIR/$tdir/flink`
3381         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3382         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3383
3384         cat $DIR/$tdir/fopen > /dev/null
3385         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3386         rm -f $DIR/$tdir/funlink2
3387         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3388
3389         for (( i=0; i < 2; i++ )) ; do
3390                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3391                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3392                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3393                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3394
3395                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3396                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3397                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3398                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3399
3400                 cancel_lru_locks osc
3401                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3402         done
3403 }
3404 run_test 39b "mtime change on open, link, unlink, rename  ======"
3405
3406 # this should be set to past
3407 TEST_39_MTIME=`date -d "1 year ago" +%s`
3408
3409 # bug 11063
3410 test_39c() {
3411         touch $DIR1/$tfile
3412         sleep 2
3413         local mtime0=`stat -c %Y $DIR1/$tfile`
3414
3415         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3416         local mtime1=`stat -c %Y $DIR1/$tfile`
3417         [ "$mtime1" = $TEST_39_MTIME ] || \
3418                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3419
3420         local d1=`date +%s`
3421         echo hello >> $DIR1/$tfile
3422         local d2=`date +%s`
3423         local mtime2=`stat -c %Y $DIR1/$tfile`
3424         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3425                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3426
3427         mv $DIR1/$tfile $DIR1/$tfile-1
3428
3429         for (( i=0; i < 2; i++ )) ; do
3430                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3431                 [ "$mtime2" = "$mtime3" ] || \
3432                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3433
3434                 cancel_lru_locks osc
3435                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3436         done
3437 }
3438 run_test 39c "mtime change on rename ==========================="
3439
3440 # bug 21114
3441 test_39d() {
3442         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3443
3444         touch $DIR1/$tfile
3445         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3446
3447         for (( i=0; i < 2; i++ )) ; do
3448                 local mtime=`stat -c %Y $DIR1/$tfile`
3449                 [ $mtime = $TEST_39_MTIME ] || \
3450                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3451
3452                 cancel_lru_locks osc
3453                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3454         done
3455 }
3456 run_test 39d "create, utime, stat =============================="
3457
3458 # bug 21114
3459 test_39e() {
3460         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3461
3462         touch $DIR1/$tfile
3463         local mtime1=`stat -c %Y $DIR1/$tfile`
3464
3465         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3466
3467         for (( i=0; i < 2; i++ )) ; do
3468                 local mtime2=`stat -c %Y $DIR1/$tfile`
3469                 [ $mtime2 = $TEST_39_MTIME ] || \
3470                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3471
3472                 cancel_lru_locks osc
3473                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3474         done
3475 }
3476 run_test 39e "create, stat, utime, stat ========================"
3477
3478 # bug 21114
3479 test_39f() {
3480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3481
3482         touch $DIR1/$tfile
3483         mtime1=`stat -c %Y $DIR1/$tfile`
3484
3485         sleep 2
3486         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3487
3488         for (( i=0; i < 2; i++ )) ; do
3489                 local mtime2=`stat -c %Y $DIR1/$tfile`
3490                 [ $mtime2 = $TEST_39_MTIME ] || \
3491                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3492
3493                 cancel_lru_locks osc
3494                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3495         done
3496 }
3497 run_test 39f "create, stat, sleep, utime, stat ================="
3498
3499 # bug 11063
3500 test_39g() {
3501         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3502
3503         echo hello >> $DIR1/$tfile
3504         local mtime1=`stat -c %Y $DIR1/$tfile`
3505
3506         sleep 2
3507         chmod o+r $DIR1/$tfile
3508
3509         for (( i=0; i < 2; i++ )) ; do
3510                 local mtime2=`stat -c %Y $DIR1/$tfile`
3511                 [ "$mtime1" = "$mtime2" ] || \
3512                         error "lost mtime: $mtime2, should be $mtime1"
3513
3514                 cancel_lru_locks osc
3515                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3516         done
3517 }
3518 run_test 39g "write, chmod, stat ==============================="
3519
3520 # bug 11063
3521 test_39h() {
3522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3523
3524         touch $DIR1/$tfile
3525         sleep 1
3526
3527         local d1=`date`
3528         echo hello >> $DIR1/$tfile
3529         local mtime1=`stat -c %Y $DIR1/$tfile`
3530
3531         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3532         local d2=`date`
3533         if [ "$d1" != "$d2" ]; then
3534                 echo "write and touch not within one second"
3535         else
3536                 for (( i=0; i < 2; i++ )) ; do
3537                         local mtime2=`stat -c %Y $DIR1/$tfile`
3538                         [ "$mtime2" = $TEST_39_MTIME ] || \
3539                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3540
3541                         cancel_lru_locks osc
3542                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3543                 done
3544         fi
3545 }
3546 run_test 39h "write, utime within one second, stat ============="
3547
3548 test_39i() {
3549         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3550
3551         touch $DIR1/$tfile
3552         sleep 1
3553
3554         echo hello >> $DIR1/$tfile
3555         local mtime1=`stat -c %Y $DIR1/$tfile`
3556
3557         mv $DIR1/$tfile $DIR1/$tfile-1
3558
3559         for (( i=0; i < 2; i++ )) ; do
3560                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3561
3562                 [ "$mtime1" = "$mtime2" ] || \
3563                         error "lost mtime: $mtime2, should be $mtime1"
3564
3565                 cancel_lru_locks osc
3566                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3567         done
3568 }
3569 run_test 39i "write, rename, stat =============================="
3570
3571 test_39j() {
3572         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3573
3574         start_full_debug_logging
3575         touch $DIR1/$tfile
3576         sleep 1
3577
3578         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3579         lctl set_param fail_loc=0x80000412
3580         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3581                 error "multiop failed"
3582         local multipid=$!
3583         local mtime1=`stat -c %Y $DIR1/$tfile`
3584
3585         mv $DIR1/$tfile $DIR1/$tfile-1
3586
3587         kill -USR1 $multipid
3588         wait $multipid || error "multiop close failed"
3589
3590         for (( i=0; i < 2; i++ )) ; do
3591                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3592                 [ "$mtime1" = "$mtime2" ] ||
3593                         error "mtime is lost on close: $mtime2, " \
3594                               "should be $mtime1"
3595
3596                 cancel_lru_locks osc
3597                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3598         done
3599         lctl set_param fail_loc=0
3600         stop_full_debug_logging
3601 }
3602 run_test 39j "write, rename, close, stat ======================="
3603
3604 test_39k() {
3605         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3606
3607         touch $DIR1/$tfile
3608         sleep 1
3609
3610         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3611         local multipid=$!
3612         local mtime1=`stat -c %Y $DIR1/$tfile`
3613
3614         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3615
3616         kill -USR1 $multipid
3617         wait $multipid || error "multiop close failed"
3618
3619         for (( i=0; i < 2; i++ )) ; do
3620                 local mtime2=`stat -c %Y $DIR1/$tfile`
3621
3622                 [ "$mtime2" = $TEST_39_MTIME ] || \
3623                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3624
3625                 cancel_lru_locks osc
3626                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3627         done
3628 }
3629 run_test 39k "write, utime, close, stat ========================"
3630
3631 # this should be set to future
3632 TEST_39_ATIME=`date -d "1 year" +%s`
3633
3634 test_39l() {
3635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3636         remote_mds_nodsh && skip "remote MDS with nodsh"
3637
3638         local atime_diff=$(do_facet $SINGLEMDS \
3639                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3640         rm -rf $DIR/$tdir
3641         mkdir -p $DIR/$tdir
3642
3643         # test setting directory atime to future
3644         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3645         local atime=$(stat -c %X $DIR/$tdir)
3646         [ "$atime" = $TEST_39_ATIME ] ||
3647                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3648
3649         # test setting directory atime from future to now
3650         local now=$(date +%s)
3651         touch -a -d @$now $DIR/$tdir
3652
3653         atime=$(stat -c %X $DIR/$tdir)
3654         [ "$atime" -eq "$now"  ] ||
3655                 error "atime is not updated from future: $atime, $now"
3656
3657         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3658         sleep 3
3659
3660         # test setting directory atime when now > dir atime + atime_diff
3661         local d1=$(date +%s)
3662         ls $DIR/$tdir
3663         local d2=$(date +%s)
3664         cancel_lru_locks mdc
3665         atime=$(stat -c %X $DIR/$tdir)
3666         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3667                 error "atime is not updated  : $atime, should be $d2"
3668
3669         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3670         sleep 3
3671
3672         # test not setting directory atime when now < dir atime + atime_diff
3673         ls $DIR/$tdir
3674         cancel_lru_locks mdc
3675         atime=$(stat -c %X $DIR/$tdir)
3676         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3677                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3678
3679         do_facet $SINGLEMDS \
3680                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3681 }
3682 run_test 39l "directory atime update ==========================="
3683
3684 test_39m() {
3685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3686
3687         touch $DIR1/$tfile
3688         sleep 2
3689         local far_past_mtime=$(date -d "May 29 1953" +%s)
3690         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3691
3692         touch -m -d @$far_past_mtime $DIR1/$tfile
3693         touch -a -d @$far_past_atime $DIR1/$tfile
3694
3695         for (( i=0; i < 2; i++ )) ; do
3696                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3697                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3698                         error "atime or mtime set incorrectly"
3699
3700                 cancel_lru_locks osc
3701                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3702         done
3703 }
3704 run_test 39m "test atime and mtime before 1970"
3705
3706 test_39n() { # LU-3832
3707         remote_mds_nodsh && skip "remote MDS with nodsh"
3708
3709         local atime_diff=$(do_facet $SINGLEMDS \
3710                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3711         local atime0
3712         local atime1
3713         local atime2
3714
3715         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3716
3717         rm -rf $DIR/$tfile
3718         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3719         atime0=$(stat -c %X $DIR/$tfile)
3720
3721         sleep 5
3722         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3723         atime1=$(stat -c %X $DIR/$tfile)
3724
3725         sleep 5
3726         cancel_lru_locks mdc
3727         cancel_lru_locks osc
3728         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3729         atime2=$(stat -c %X $DIR/$tfile)
3730
3731         do_facet $SINGLEMDS \
3732                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3733
3734         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3735         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3736 }
3737 run_test 39n "check that O_NOATIME is honored"
3738
3739 test_39o() {
3740         TESTDIR=$DIR/$tdir/$tfile
3741         [ -e $TESTDIR ] && rm -rf $TESTDIR
3742         mkdir -p $TESTDIR
3743         cd $TESTDIR
3744         links1=2
3745         ls
3746         mkdir a b
3747         ls
3748         links2=$(stat -c %h .)
3749         [ $(($links1 + 2)) != $links2 ] &&
3750                 error "wrong links count $(($links1 + 2)) != $links2"
3751         rmdir b
3752         links3=$(stat -c %h .)
3753         [ $(($links1 + 1)) != $links3 ] &&
3754                 error "wrong links count $links1 != $links3"
3755         return 0
3756 }
3757 run_test 39o "directory cached attributes updated after create"
3758
3759 test_39p() {
3760         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3761
3762         local MDTIDX=1
3763         TESTDIR=$DIR/$tdir/$tdir
3764         [ -e $TESTDIR ] && rm -rf $TESTDIR
3765         test_mkdir -p $TESTDIR
3766         cd $TESTDIR
3767         links1=2
3768         ls
3769         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3770         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3771         ls
3772         links2=$(stat -c %h .)
3773         [ $(($links1 + 2)) != $links2 ] &&
3774                 error "wrong links count $(($links1 + 2)) != $links2"
3775         rmdir remote_dir2
3776         links3=$(stat -c %h .)
3777         [ $(($links1 + 1)) != $links3 ] &&
3778                 error "wrong links count $links1 != $links3"
3779         return 0
3780 }
3781 run_test 39p "remote directory cached attributes updated after create ========"
3782
3783
3784 test_39q() { # LU-8041
3785         local testdir=$DIR/$tdir
3786         mkdir -p $testdir
3787         multiop_bg_pause $testdir D_c || error "multiop failed"
3788         local multipid=$!
3789         cancel_lru_locks mdc
3790         kill -USR1 $multipid
3791         local atime=$(stat -c %X $testdir)
3792         [ "$atime" -ne 0 ] || error "atime is zero"
3793 }
3794 run_test 39q "close won't zero out atime"
3795
3796 test_40() {
3797         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3798         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3799                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3800         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3801                 error "$tfile is not 4096 bytes in size"
3802 }
3803 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3804
3805 test_41() {
3806         # bug 1553
3807         small_write $DIR/f41 18
3808 }
3809 run_test 41 "test small file write + fstat ====================="
3810
3811 count_ost_writes() {
3812         lctl get_param -n ${OSC}.*.stats |
3813                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3814                         END { printf("%0.0f", writes) }'
3815 }
3816
3817 # decent default
3818 WRITEBACK_SAVE=500
3819 DIRTY_RATIO_SAVE=40
3820 MAX_DIRTY_RATIO=50
3821 BG_DIRTY_RATIO_SAVE=10
3822 MAX_BG_DIRTY_RATIO=25
3823
3824 start_writeback() {
3825         trap 0
3826         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3827         # dirty_ratio, dirty_background_ratio
3828         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3829                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3830                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3831                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3832         else
3833                 # if file not here, we are a 2.4 kernel
3834                 kill -CONT `pidof kupdated`
3835         fi
3836 }
3837
3838 stop_writeback() {
3839         # setup the trap first, so someone cannot exit the test at the
3840         # exact wrong time and mess up a machine
3841         trap start_writeback EXIT
3842         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3843         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3844                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3845                 sysctl -w vm.dirty_writeback_centisecs=0
3846                 sysctl -w vm.dirty_writeback_centisecs=0
3847                 # save and increase /proc/sys/vm/dirty_ratio
3848                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3849                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3850                 # save and increase /proc/sys/vm/dirty_background_ratio
3851                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3852                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3853         else
3854                 # if file not here, we are a 2.4 kernel
3855                 kill -STOP `pidof kupdated`
3856         fi
3857 }
3858
3859 # ensure that all stripes have some grant before we test client-side cache
3860 setup_test42() {
3861         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3862                 dd if=/dev/zero of=$i bs=4k count=1
3863                 rm $i
3864         done
3865 }
3866
3867 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3868 # file truncation, and file removal.
3869 test_42a() {
3870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3871
3872         setup_test42
3873         cancel_lru_locks $OSC
3874         stop_writeback
3875         sync; sleep 1; sync # just to be safe
3876         BEFOREWRITES=`count_ost_writes`
3877         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3878         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3879         AFTERWRITES=`count_ost_writes`
3880         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3881                 error "$BEFOREWRITES < $AFTERWRITES"
3882         start_writeback
3883 }
3884 run_test 42a "ensure that we don't flush on close"
3885
3886 test_42b() {
3887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3888
3889         setup_test42
3890         cancel_lru_locks $OSC
3891         stop_writeback
3892         sync
3893         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3894         BEFOREWRITES=$(count_ost_writes)
3895         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3896         AFTERWRITES=$(count_ost_writes)
3897         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3898                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3899         fi
3900         BEFOREWRITES=$(count_ost_writes)
3901         sync || error "sync: $?"
3902         AFTERWRITES=$(count_ost_writes)
3903         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3904                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3905         fi
3906         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3907         start_writeback
3908         return 0
3909 }
3910 run_test 42b "test destroy of file with cached dirty data ======"
3911
3912 # if these tests just want to test the effect of truncation,
3913 # they have to be very careful.  consider:
3914 # - the first open gets a {0,EOF}PR lock
3915 # - the first write conflicts and gets a {0, count-1}PW
3916 # - the rest of the writes are under {count,EOF}PW
3917 # - the open for truncate tries to match a {0,EOF}PR
3918 #   for the filesize and cancels the PWs.
3919 # any number of fixes (don't get {0,EOF} on open, match
3920 # composite locks, do smarter file size management) fix
3921 # this, but for now we want these tests to verify that
3922 # the cancellation with truncate intent works, so we
3923 # start the file with a full-file pw lock to match against
3924 # until the truncate.
3925 trunc_test() {
3926         test=$1
3927         file=$DIR/$test
3928         offset=$2
3929         cancel_lru_locks $OSC
3930         stop_writeback
3931         # prime the file with 0,EOF PW to match
3932         touch $file
3933         $TRUNCATE $file 0
3934         sync; sync
3935         # now the real test..
3936         dd if=/dev/zero of=$file bs=1024 count=100
3937         BEFOREWRITES=`count_ost_writes`
3938         $TRUNCATE $file $offset
3939         cancel_lru_locks $OSC
3940         AFTERWRITES=`count_ost_writes`
3941         start_writeback
3942 }
3943
3944 test_42c() {
3945         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3946
3947         trunc_test 42c 1024
3948         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
3949                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3950         rm $file
3951 }
3952 run_test 42c "test partial truncate of file with cached dirty data"
3953
3954 test_42d() {
3955         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3956
3957         trunc_test 42d 0
3958         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
3959                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3960         rm $file
3961 }
3962 run_test 42d "test complete truncate of file with cached dirty data"
3963
3964 test_42e() { # bug22074
3965         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3966
3967         local TDIR=$DIR/${tdir}e
3968         local pagesz=$(page_size)
3969         local pages=16 # hardcoded 16 pages, don't change it.
3970         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3971         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3972         local max_dirty_mb
3973         local warmup_files
3974
3975         test_mkdir $DIR/${tdir}e
3976         $SETSTRIPE -c 1 $TDIR
3977         createmany -o $TDIR/f $files
3978
3979         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3980
3981         # we assume that with $OSTCOUNT files, at least one of them will
3982         # be allocated on OST0.
3983         warmup_files=$((OSTCOUNT * max_dirty_mb))
3984         createmany -o $TDIR/w $warmup_files
3985
3986         # write a large amount of data into one file and sync, to get good
3987         # avail_grant number from OST.
3988         for ((i=0; i<$warmup_files; i++)); do
3989                 idx=$($GETSTRIPE -i $TDIR/w$i)
3990                 [ $idx -ne 0 ] && continue
3991                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3992                 break
3993         done
3994         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3995         sync
3996         $LCTL get_param $proc_osc0/cur_dirty_bytes
3997         $LCTL get_param $proc_osc0/cur_grant_bytes
3998
3999         # create as much dirty pages as we can while not to trigger the actual
4000         # RPCs directly. but depends on the env, VFS may trigger flush during this
4001         # period, hopefully we are good.
4002         for ((i=0; i<$warmup_files; i++)); do
4003                 idx=$($GETSTRIPE -i $TDIR/w$i)
4004                 [ $idx -ne 0 ] && continue
4005                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4006         done
4007         $LCTL get_param $proc_osc0/cur_dirty_bytes
4008         $LCTL get_param $proc_osc0/cur_grant_bytes
4009
4010         # perform the real test
4011         $LCTL set_param $proc_osc0/rpc_stats 0
4012         for ((;i<$files; i++)); do
4013                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
4014                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
4015         done
4016         sync
4017         $LCTL get_param $proc_osc0/rpc_stats
4018
4019         local percent=0
4020         local have_ppr=false
4021         $LCTL get_param $proc_osc0/rpc_stats |
4022                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4023                         # skip lines until we are at the RPC histogram data
4024                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4025                         $have_ppr || continue
4026
4027                         # we only want the percent stat for < 16 pages
4028                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4029
4030                         percent=$((percent + WPCT))
4031                         if [[ $percent -gt 15 ]]; then
4032                                 error "less than 16-pages write RPCs" \
4033                                       "$percent% > 15%"
4034                                 break
4035                         fi
4036                 done
4037         rm -rf $TDIR
4038 }
4039 run_test 42e "verify sub-RPC writes are not done synchronously"
4040
4041 test_43A() { # was test_43
4042         test_mkdir $DIR/$tdir
4043         cp -p /bin/ls $DIR/$tdir/$tfile
4044         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4045         pid=$!
4046         # give multiop a chance to open
4047         sleep 1
4048
4049         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4050         kill -USR1 $pid
4051 }
4052 run_test 43A "execution of file opened for write should return -ETXTBSY"
4053
4054 test_43a() {
4055         test_mkdir $DIR/$tdir
4056         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
4057                 cp -p multiop $DIR/$tdir/multiop
4058         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
4059                 error "multiop open $TMP/$tfile.junk failed"
4060         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
4061         MULTIOP_PID=$!
4062         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
4063         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
4064         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
4065 }
4066 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4067
4068 test_43b() {
4069         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4070
4071         test_mkdir $DIR/$tdir
4072         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
4073                 cp -p multiop $DIR/$tdir/multiop
4074         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
4075                 error "multiop open $TMP/$tfile.junk failed"
4076         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
4077         MULTIOP_PID=$!
4078         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
4079         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
4080         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
4081 }
4082 run_test 43b "truncate of file being executed should return -ETXTBSY"
4083
4084 test_43c() {
4085         local testdir="$DIR/$tdir"
4086         test_mkdir $testdir
4087         cp $SHELL $testdir/
4088         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4089                 ( cd $testdir && md5sum -c )
4090 }
4091 run_test 43c "md5sum of copy into lustre"
4092
4093 test_44A() { # was test_44
4094         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4095
4096         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4097         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4098 }
4099 run_test 44A "zero length read from a sparse stripe"
4100
4101 test_44a() {
4102         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4103                 awk '{ print $2 }')
4104         [ -z "$nstripe" ] && skip "can't get stripe info"
4105         [[ $nstripe -gt $OSTCOUNT ]] &&
4106                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4107
4108         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4109                 awk '{ print $2 }')
4110         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4111                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4112                         awk '{ print $2 }')
4113         fi
4114
4115         OFFSETS="0 $((stride/2)) $((stride-1))"
4116         for offset in $OFFSETS; do
4117                 for i in $(seq 0 $((nstripe-1))); do
4118                         local GLOBALOFFSETS=""
4119                         # size in Bytes
4120                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4121                         local myfn=$DIR/d44a-$size
4122                         echo "--------writing $myfn at $size"
4123                         ll_sparseness_write $myfn $size ||
4124                                 error "ll_sparseness_write"
4125                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4126                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4127                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4128
4129                         for j in $(seq 0 $((nstripe-1))); do
4130                                 # size in Bytes
4131                                 size=$((((j + $nstripe )*$stride + $offset)))
4132                                 ll_sparseness_write $myfn $size ||
4133                                         error "ll_sparseness_write"
4134                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4135                         done
4136                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4137                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4138                         rm -f $myfn
4139                 done
4140         done
4141 }
4142 run_test 44a "test sparse pwrite ==============================="
4143
4144 dirty_osc_total() {
4145         tot=0
4146         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4147                 tot=$(($tot + $d))
4148         done
4149         echo $tot
4150 }
4151 do_dirty_record() {
4152         before=`dirty_osc_total`
4153         echo executing "\"$*\""
4154         eval $*
4155         after=`dirty_osc_total`
4156         echo before $before, after $after
4157 }
4158 test_45() {
4159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4160
4161         f="$DIR/f45"
4162         # Obtain grants from OST if it supports it
4163         echo blah > ${f}_grant
4164         stop_writeback
4165         sync
4166         do_dirty_record "echo blah > $f"
4167         [[ $before -eq $after ]] && error "write wasn't cached"
4168         do_dirty_record "> $f"
4169         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4170         do_dirty_record "echo blah > $f"
4171         [[ $before -eq $after ]] && error "write wasn't cached"
4172         do_dirty_record "sync"
4173         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4174         do_dirty_record "echo blah > $f"
4175         [[ $before -eq $after ]] && error "write wasn't cached"
4176         do_dirty_record "cancel_lru_locks osc"
4177         [[ $before -gt $after ]] ||
4178                 error "lock cancellation didn't lower dirty count"
4179         start_writeback
4180 }
4181 run_test 45 "osc io page accounting ============================"
4182
4183 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4184 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4185 # objects offset and an assert hit when an rpc was built with 1023's mapped
4186 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4187 test_46() {
4188         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4189
4190         f="$DIR/f46"
4191         stop_writeback
4192         sync
4193         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4194         sync
4195         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
4196         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4197         sync
4198         start_writeback
4199 }
4200 run_test 46 "dirtying a previously written page ================"
4201
4202 # test_47 is removed "Device nodes check" is moved to test_28
4203
4204 test_48a() { # bug 2399
4205         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
4206         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
4207                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4208
4209         test_mkdir $DIR/$tdir
4210         cd $DIR/$tdir
4211         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4212         test_mkdir $DIR/$tdir
4213         touch foo || error "'touch foo' failed after recreating cwd"
4214         test_mkdir bar
4215         touch .foo || error "'touch .foo' failed after recreating cwd"
4216         test_mkdir .bar
4217         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4218         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4219         cd . || error "'cd .' failed after recreating cwd"
4220         mkdir . && error "'mkdir .' worked after recreating cwd"
4221         rmdir . && error "'rmdir .' worked after recreating cwd"
4222         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4223         cd .. || error "'cd ..' failed after recreating cwd"
4224 }
4225 run_test 48a "Access renamed working dir (should return errors)="
4226
4227 test_48b() { # bug 2399
4228         rm -rf $DIR/$tdir
4229         test_mkdir $DIR/$tdir
4230         cd $DIR/$tdir
4231         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4232         touch foo && error "'touch foo' worked after removing cwd"
4233         mkdir foo && error "'mkdir foo' worked after removing cwd"
4234         touch .foo && error "'touch .foo' worked after removing cwd"
4235         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4236         ls . > /dev/null && error "'ls .' worked after removing cwd"
4237         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4238         mkdir . && error "'mkdir .' worked after removing cwd"
4239         rmdir . && error "'rmdir .' worked after removing cwd"
4240         ln -s . foo && error "'ln -s .' worked after removing cwd"
4241         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4242 }
4243 run_test 48b "Access removed working dir (should return errors)="
4244
4245 test_48c() { # bug 2350
4246         #lctl set_param debug=-1
4247         #set -vx
4248         rm -rf $DIR/$tdir
4249         test_mkdir -p $DIR/$tdir/dir
4250         cd $DIR/$tdir/dir
4251         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4252         $TRACE touch foo && error "touch foo worked after removing cwd"
4253         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4254         touch .foo && error "touch .foo worked after removing cwd"
4255         mkdir .foo && error "mkdir .foo worked after removing cwd"
4256         $TRACE ls . && error "'ls .' worked after removing cwd"
4257         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4258         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4259         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4260         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4261         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4262 }
4263 run_test 48c "Access removed working subdir (should return errors)"
4264
4265 test_48d() { # bug 2350
4266         #lctl set_param debug=-1
4267         #set -vx
4268         rm -rf $DIR/$tdir
4269         test_mkdir -p $DIR/$tdir/dir
4270         cd $DIR/$tdir/dir
4271         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4272         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4273         $TRACE touch foo && error "'touch foo' worked after removing parent"
4274         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4275         touch .foo && error "'touch .foo' worked after removing parent"
4276         mkdir .foo && error "mkdir .foo worked after removing parent"
4277         $TRACE ls . && error "'ls .' worked after removing parent"
4278         $TRACE ls .. && error "'ls ..' worked after removing parent"
4279         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4280         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4281         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4282         true
4283 }
4284 run_test 48d "Access removed parent subdir (should return errors)"
4285
4286 test_48e() { # bug 4134
4287         #lctl set_param debug=-1
4288         #set -vx
4289         rm -rf $DIR/$tdir
4290         test_mkdir -p $DIR/$tdir/dir
4291         cd $DIR/$tdir/dir
4292         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4293         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4294         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4295         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4296         # On a buggy kernel addition of "touch foo" after cd .. will
4297         # produce kernel oops in lookup_hash_it
4298         touch ../foo && error "'cd ..' worked after recreate parent"
4299         cd $DIR
4300         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4301 }
4302 run_test 48e "Access to recreated parent subdir (should return errors)"
4303
4304 test_49() { # LU-1030
4305         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4306         remote_ost_nodsh && skip "remote OST with nodsh"
4307
4308         # get ost1 size - lustre-OST0000
4309         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4310                 awk '{ print $4 }')
4311         # write 800M at maximum
4312         [[ $ost1_size -lt 2 ]] && ost1_size=2
4313         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4314
4315         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4316         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4317         local dd_pid=$!
4318
4319         # change max_pages_per_rpc while writing the file
4320         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4321         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4322         # loop until dd process exits
4323         while ps ax -opid | grep -wq $dd_pid; do
4324                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4325                 sleep $((RANDOM % 5 + 1))
4326         done
4327         # restore original max_pages_per_rpc
4328         $LCTL set_param $osc1_mppc=$orig_mppc
4329         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4330 }
4331 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4332
4333 test_50() {
4334         # bug 1485
4335         test_mkdir $DIR/$tdir
4336         cd $DIR/$tdir
4337         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4338 }
4339 run_test 50 "special situations: /proc symlinks  ==============="
4340
4341 test_51a() {    # was test_51
4342         # bug 1516 - create an empty entry right after ".." then split dir
4343         test_mkdir -c1 $DIR/$tdir
4344         touch $DIR/$tdir/foo
4345         $MCREATE $DIR/$tdir/bar
4346         rm $DIR/$tdir/foo
4347         createmany -m $DIR/$tdir/longfile 201
4348         FNUM=202
4349         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4350                 $MCREATE $DIR/$tdir/longfile$FNUM
4351                 FNUM=$(($FNUM + 1))
4352                 echo -n "+"
4353         done
4354         echo
4355         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4356 }
4357 run_test 51a "special situations: split htree with empty entry =="
4358
4359 cleanup_print_lfs_df () {
4360         trap 0
4361         $LFS df
4362         $LFS df -i
4363 }
4364
4365 test_51b() {
4366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4367
4368         local dir=$DIR/$tdir
4369         local nrdirs=$((65536 + 100))
4370
4371         # cleanup the directory
4372         rm -fr $dir
4373
4374         test_mkdir -c1 $dir
4375
4376         $LFS df
4377         $LFS df -i
4378         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
4379         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4380         [[ $numfree -lt $nrdirs ]] &&
4381                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
4382
4383         # need to check free space for the directories as well
4384         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4385         numfree=$(( blkfree / $(fs_inode_ksize) ))
4386         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
4387
4388         trap cleanup_print_lfs_df EXIT
4389
4390         # create files
4391         createmany -d $dir/d $nrdirs || {
4392                 unlinkmany $dir/d $nrdirs
4393                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4394         }
4395
4396         # really created :
4397         nrdirs=$(ls -U $dir | wc -l)
4398
4399         # unlink all but 100 subdirectories, then check it still works
4400         local left=100
4401         local delete=$((nrdirs - left))
4402
4403         $LFS df
4404         $LFS df -i
4405
4406         # for ldiskfs the nlink count should be 1, but this is OSD specific
4407         # and so this is listed for informational purposes only
4408         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4409         unlinkmany -d $dir/d $delete ||
4410                 error "unlink of first $delete subdirs failed"
4411
4412         echo "nlink between: $(stat -c %h $dir)"
4413         local found=$(ls -U $dir | wc -l)
4414         [ $found -ne $left ] &&
4415                 error "can't find subdirs: found only $found, expected $left"
4416
4417         unlinkmany -d $dir/d $delete $left ||
4418                 error "unlink of second $left subdirs failed"
4419         # regardless of whether the backing filesystem tracks nlink accurately
4420         # or not, the nlink count shouldn't be more than "." and ".." here
4421         local after=$(stat -c %h $dir)
4422         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4423                 echo "nlink after: $after"
4424
4425         cleanup_print_lfs_df
4426 }
4427 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4428
4429 test_51d() {
4430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4431         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
4432
4433         test_mkdir $DIR/$tdir
4434         createmany -o $DIR/$tdir/t- 1000
4435         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4436         for N in $(seq 0 $((OSTCOUNT - 1))); do
4437                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4438                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4439                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4440                         '($1 == '$N') { objs += 1 } \
4441                         END { printf("%0.0f", objs) }')
4442                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4443         done
4444         unlinkmany $DIR/$tdir/t- 1000
4445
4446         NLAST=0
4447         for N in $(seq 1 $((OSTCOUNT - 1))); do
4448                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4449                         error "OST $N has less objects vs OST $NLAST" \
4450                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4451                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4452                         error "OST $N has less objects vs OST $NLAST" \
4453                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4454
4455                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4456                         error "OST $N has less #0 objects vs OST $NLAST" \
4457                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4458                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4459                         error "OST $N has less #0 objects vs OST $NLAST" \
4460                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4461                 NLAST=$N
4462         done
4463         rm -f $TMP/$tfile
4464 }
4465 run_test 51d "check object distribution"
4466
4467 test_51e() {
4468         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4469                 skip_env "ldiskfs only test"
4470         fi
4471
4472         test_mkdir -c1 $DIR/$tdir
4473         test_mkdir -c1 $DIR/$tdir/d0
4474
4475         touch $DIR/$tdir/d0/foo
4476         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4477                 error "file exceed 65000 nlink limit!"
4478         unlinkmany $DIR/$tdir/d0/f- 65001
4479         return 0
4480 }
4481 run_test 51e "check file nlink limit"
4482
4483 test_51f() {
4484         test_mkdir $DIR/$tdir
4485
4486         local max=100000
4487         local ulimit_old=$(ulimit -n)
4488         local spare=20 # number of spare fd's for scripts/libraries, etc.
4489         local mdt=$($LFS getstripe -m $DIR/$tdir)
4490         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4491
4492         echo "MDT$mdt numfree=$numfree, max=$max"
4493         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4494         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4495                 while ! ulimit -n $((numfree + spare)); do
4496                         numfree=$((numfree * 3 / 4))
4497                 done
4498                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4499         else
4500                 echo "left ulimit at $ulimit_old"
4501         fi
4502
4503         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4504                 unlinkmany $DIR/$tdir/f $numfree
4505                 error "create+open $numfree files in $DIR/$tdir failed"
4506         }
4507         ulimit -n $ulimit_old
4508
4509         # if createmany exits at 120s there will be fewer than $numfree files
4510         unlinkmany $DIR/$tdir/f $numfree || true
4511 }
4512 run_test 51f "check many open files limit"
4513
4514 test_52a() {
4515         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4516         test_mkdir $DIR/$tdir
4517         touch $DIR/$tdir/foo
4518         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4519         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4520         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4521         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4522         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4523                                         error "link worked"
4524         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4525         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4526         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4527                                                      error "lsattr"
4528         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4529         cp -r $DIR/$tdir $TMP/
4530         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4531 }
4532 run_test 52a "append-only flag test (should return errors)"
4533
4534 test_52b() {
4535         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4536         test_mkdir $DIR/$tdir
4537         touch $DIR/$tdir/foo
4538         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4539         cat test > $DIR/$tdir/foo && error "cat test worked"
4540         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4541         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4542         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4543                                         error "link worked"
4544         echo foo >> $DIR/$tdir/foo && error "echo worked"
4545         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4546         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4547         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4548         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4549                                                         error "lsattr"
4550         chattr -i $DIR/$tdir/foo || error "chattr failed"
4551
4552         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4553 }
4554 run_test 52b "immutable flag test (should return errors) ======="
4555
4556 test_53() {
4557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4558         remote_mds_nodsh && skip "remote MDS with nodsh"
4559         remote_ost_nodsh && skip "remote OST with nodsh"
4560
4561         local param
4562         local param_seq
4563         local ostname
4564         local mds_last
4565         local mds_last_seq
4566         local ost_last
4567         local ost_last_seq
4568         local ost_last_id
4569         local ostnum
4570         local node
4571         local found=false
4572         local support_last_seq=true
4573
4574         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4575                 support_last_seq=false
4576
4577         # only test MDT0000
4578         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4579         local value
4580         for value in $(do_facet $SINGLEMDS \
4581                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4582                 param=$(echo ${value[0]} | cut -d "=" -f1)
4583                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4584
4585                 if $support_last_seq; then
4586                         param_seq=$(echo $param |
4587                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4588                         mds_last_seq=$(do_facet $SINGLEMDS \
4589                                        $LCTL get_param -n $param_seq)
4590                 fi
4591                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4592
4593                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4594                 node=$(facet_active_host ost$((ostnum+1)))
4595                 param="obdfilter.$ostname.last_id"
4596                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4597                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4598                         ost_last_id=$ost_last
4599
4600                         if $support_last_seq; then
4601                                 ost_last_id=$(echo $ost_last |
4602                                               awk -F':' '{print $2}' |
4603                                               sed -e "s/^0x//g")
4604                                 ost_last_seq=$(echo $ost_last |
4605                                                awk -F':' '{print $1}')
4606                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4607                         fi
4608
4609                         if [[ $ost_last_id != $mds_last ]]; then
4610                                 error "$ost_last_id != $mds_last"
4611                         else
4612                                 found=true
4613                                 break
4614                         fi
4615                 done
4616         done
4617         $found || error "can not match last_seq/last_id for $mdtosc"
4618         return 0
4619 }
4620 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4621
4622 test_54a() {
4623         perl -MSocket -e ';' || skip "no Socket perl module installed"
4624
4625         $SOCKETSERVER $DIR/socket ||
4626                 error "$SOCKETSERVER $DIR/socket failed: $?"
4627         $SOCKETCLIENT $DIR/socket ||
4628                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4629         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4630 }
4631 run_test 54a "unix domain socket test =========================="
4632
4633 test_54b() {
4634         f="$DIR/f54b"
4635         mknod $f c 1 3
4636         chmod 0666 $f
4637         dd if=/dev/zero of=$f bs=$(page_size) count=1
4638 }
4639 run_test 54b "char device works in lustre ======================"
4640
4641 find_loop_dev() {
4642         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4643         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4644         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4645
4646         for i in $(seq 3 7); do
4647                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4648                 LOOPDEV=$LOOPBASE$i
4649                 LOOPNUM=$i
4650                 break
4651         done
4652 }
4653
4654 cleanup_54c() {
4655         local rc=0
4656         loopdev="$DIR/loop54c"
4657
4658         trap 0
4659         $UMOUNT $DIR/$tdir || rc=$?
4660         losetup -d $loopdev || true
4661         losetup -d $LOOPDEV || true
4662         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4663         return $rc
4664 }
4665
4666 test_54c() {
4667         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4668
4669         loopdev="$DIR/loop54c"
4670
4671         find_loop_dev
4672         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
4673         trap cleanup_54c EXIT
4674         mknod $loopdev b 7 $LOOPNUM
4675         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4676         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4677         losetup $loopdev $DIR/$tfile ||
4678                 error "can't set up $loopdev for $DIR/$tfile"
4679         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4680         test_mkdir $DIR/$tdir
4681         mount -t ext2 $loopdev $DIR/$tdir ||
4682                 error "error mounting $loopdev on $DIR/$tdir"
4683         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4684                 error "dd write"
4685         df $DIR/$tdir
4686         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4687                 error "dd read"
4688         cleanup_54c
4689 }
4690 run_test 54c "block device works in lustre ====================="
4691
4692 test_54d() {
4693         f="$DIR/f54d"
4694         string="aaaaaa"
4695         mknod $f p
4696         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4697 }
4698 run_test 54d "fifo device works in lustre ======================"
4699
4700 test_54e() {
4701         f="$DIR/f54e"
4702         string="aaaaaa"
4703         cp -aL /dev/console $f
4704         echo $string > $f || error "echo $string to $f failed"
4705 }
4706 run_test 54e "console/tty device works in lustre ======================"
4707
4708 test_56a() {
4709         local numfiles=3
4710         local dir=$DIR/$tdir
4711
4712         rm -rf $dir
4713         test_mkdir -p $dir/dir
4714         for i in $(seq $numfiles); do
4715                 touch $dir/file$i
4716                 touch $dir/dir/file$i
4717         done
4718
4719         local numcomp=$($LFS getstripe --component-count $dir)
4720
4721         [[ $numcomp == 0 ]] && numcomp=1
4722
4723         # test lfs getstripe with --recursive
4724         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
4725
4726         [[ $filenum -eq $((numfiles * 2)) ]] ||
4727                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
4728         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
4729         [[ $filenum -eq $numfiles ]] ||
4730                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
4731         echo "$LFS getstripe showed obdidx or l_ost_idx"
4732
4733         # test lfs getstripe with file instead of dir
4734         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
4735         [[ $filenum -eq 1 ]] ||
4736                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
4737         echo "$LFS getstripe file1 passed"
4738
4739         #test lfs getstripe with --verbose
4740         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
4741         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4742                 error "$LFS getstripe --verbose $dir: "\
4743                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
4744         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
4745                 error "$LFS getstripe $dir: showed lmm_magic"
4746
4747         #test lfs getstripe with -v prints lmm_fid
4748         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
4749         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4750                 error "$LFS getstripe -v $dir: "\
4751                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
4752         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
4753                 error "$LFS getstripe $dir: showed lmm_fid by default"
4754         echo "$LFS getstripe --verbose passed"
4755
4756         #check for FID information
4757         local fid1=$($LFS getstripe --fid $dir/file1)
4758         local fid2=$($LFS getstripe --verbose $dir/file1 |
4759                      awk '/lmm_fid: / { print $2; exit; }')
4760         local fid3=$($LFS path2fid $dir/file1)
4761
4762         [ "$fid1" != "$fid2" ] &&
4763                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
4764         [ "$fid1" != "$fid3" ] &&
4765                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
4766         echo "$LFS getstripe --fid passed"
4767
4768         #test lfs getstripe with --obd
4769         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
4770                 error "$LFS getstripe --obd wrong_uuid: should return error"
4771
4772         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4773
4774         local ostidx=1
4775         local obduuid=$(ostuuid_from_index $ostidx)
4776         local found=$($LFS getstripe -r --obd $obduuid $dir |
4777                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
4778
4779         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
4780         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
4781                 ((filenum--))
4782         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
4783                 ((filenum--))
4784
4785         [[ $found -eq $filenum ]] ||
4786                 error "$LFS getstripe --obd: found $found expect $filenum"
4787         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
4788                 sed '/^[         ]*'${ostidx}'[  ]/d' |
4789                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4790                 error "$LFS getstripe --obd: should not show file on other obd"
4791         echo "$LFS getstripe --obd passed"
4792 }
4793 run_test 56a "check $LFS getstripe"
4794
4795 test_56b() {
4796         local dir=$DIR/$tdir
4797         local numdirs=3
4798
4799         test_mkdir $dir
4800         for i in $(seq $numdirs); do
4801                 test_mkdir $dir/dir$i
4802         done
4803
4804         # test lfs getdirstripe default mode is non-recursion, which is
4805         # different from lfs getstripe
4806         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
4807
4808         [[ $dircnt -eq 1 ]] ||
4809                 error "$LFS getdirstripe: found $dircnt, not 1"
4810         dircnt=$($LFS getdirstripe --recursive $dir |
4811                 grep -c lmv_stripe_count)
4812         [[ $dircnt -eq $((numdirs + 1)) ]] ||
4813                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
4814 }
4815 run_test 56b "check $LFS getdirstripe"
4816
4817 test_56c() {
4818         remote_ost_nodsh && skip "remote OST with nodsh"
4819
4820         local ost_idx=0
4821         local ost_name=$(ostname_from_index $ost_idx)
4822         local old_status=$(ost_dev_status $ost_idx)
4823
4824         [[ -z "$old_status" ]] ||
4825                 skip_env "OST $ost_name is in $old_status status"
4826
4827         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4828         sleep_maxage
4829
4830         local new_status=$(ost_dev_status $ost_idx)
4831
4832         [[ "$new_status" = "D" ]] ||
4833                 error "OST $ost_name is in status of '$new_status', not 'D'"
4834
4835         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4836         sleep_maxage
4837
4838         new_status=$(ost_dev_status $ost_idx)
4839         [[ -z "$new_status" ]] ||
4840                 error "OST $ost_name is in status of '$new_status', not ''"
4841 }
4842 run_test 56c "check 'lfs df' showing device status"
4843
4844 NUMFILES=3
4845 NUMDIRS=3
4846 setup_56() {
4847         local local_tdir="$1"
4848         local local_numfiles="$2"
4849         local local_numdirs="$3"
4850         local dir_params="$4"
4851         local dir_stripe_params="$5"
4852
4853         if [ ! -d "$local_tdir" ] ; then
4854                 test_mkdir -p $dir_stripe_params $local_tdir
4855                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
4856                 for i in $(seq $local_numfiles) ; do
4857                         touch $local_tdir/file$i
4858                 done
4859                 for i in $(seq $local_numdirs) ; do
4860                         test_mkdir $dir_stripe_params $local_tdir/dir$i
4861                         for j in $(seq $local_numfiles) ; do
4862                                 touch $local_tdir/dir$i/file$j
4863                         done
4864                 done
4865         fi
4866 }
4867
4868 setup_56_special() {
4869         local local_tdir=$1
4870         local local_numfiles=$2
4871         local local_numdirs=$3
4872
4873         setup_56 $local_tdir $local_numfiles $local_numdirs
4874
4875         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
4876                 for i in $(seq $local_numfiles) ; do
4877                         mknod $local_tdir/loop${i}b b 7 $i
4878                         mknod $local_tdir/null${i}c c 1 3
4879                         ln -s $local_tdir/file1 $local_tdir/link${i}
4880                 done
4881                 for i in $(seq $local_numdirs) ; do
4882                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
4883                         mknod $local_tdir/dir$i/null${i}c c 1 3
4884                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
4885                 done
4886         fi
4887 }
4888
4889 test_56g() {
4890         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4891         local expected=$(($NUMDIRS + 2))
4892
4893         setup_56 $dir $NUMFILES $NUMDIRS
4894
4895         # test lfs find with -name
4896         for i in $(seq $NUMFILES) ; do
4897                 local nums=$($LFS find -name "*$i" $dir | wc -l)
4898
4899                 [ $nums -eq $expected ] ||
4900                         error "lfs find -name '*$i' $dir wrong: "\
4901                               "found $nums, expected $expected"
4902         done
4903 }
4904 run_test 56g "check lfs find -name"
4905
4906 test_56h() {
4907         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4908         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4909
4910         setup_56 $dir $NUMFILES $NUMDIRS
4911
4912         # test lfs find with ! -name
4913         for i in $(seq $NUMFILES) ; do
4914                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
4915
4916                 [ $nums -eq $expected ] ||
4917                         error "lfs find ! -name '*$i' $dir wrong: "\
4918                               "found $nums, expected $expected"
4919         done
4920 }
4921 run_test 56h "check lfs find ! -name"
4922
4923 test_56i() {
4924         local dir=$DIR/$tdir
4925
4926         test_mkdir $dir
4927
4928         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
4929         local out=$($cmd)
4930
4931         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
4932 }
4933 run_test 56i "check 'lfs find -ost UUID' skips directories"
4934
4935 test_56j() {
4936         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4937
4938         setup_56_special $dir $NUMFILES $NUMDIRS
4939
4940         local expected=$((NUMDIRS + 1))
4941         local cmd="$LFS find -type d $dir"
4942         local nums=$($cmd | wc -l)
4943
4944         [ $nums -eq $expected ] ||
4945                 error "'$cmd' wrong: found $nums, expected $expected"
4946 }
4947 run_test 56j "check lfs find -type d"
4948
4949 test_56k() {
4950         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4951
4952         setup_56_special $dir $NUMFILES $NUMDIRS
4953
4954         local expected=$(((NUMDIRS + 1) * NUMFILES))
4955         local cmd="$LFS find -type f $dir"
4956         local nums=$($cmd | wc -l)
4957
4958         [ $nums -eq $expected ] ||
4959                 error "'$cmd' wrong: found $nums, expected $expected"
4960 }
4961 run_test 56k "check lfs find -type f"
4962
4963 test_56l() {
4964         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4965
4966         setup_56_special $dir $NUMFILES $NUMDIRS
4967
4968         local expected=$((NUMDIRS + NUMFILES))
4969         local cmd="$LFS find -type b $dir"
4970         local nums=$($cmd | wc -l)
4971
4972         [ $nums -eq $expected ] ||
4973                 error "'$cmd' wrong: found $nums, expected $expected"
4974 }
4975 run_test 56l "check lfs find -type b"
4976
4977 test_56m() {
4978         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4979
4980         setup_56_special $dir $NUMFILES $NUMDIRS
4981
4982         local expected=$((NUMDIRS + NUMFILES))
4983         local cmd="$LFS find -type c $dir"
4984         local nums=$($cmd | wc -l)
4985         [ $nums -eq $expected ] ||
4986                 error "'$cmd' wrong: found $nums, expected $expected"
4987 }
4988 run_test 56m "check lfs find -type c"
4989
4990 test_56n() {
4991         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4992         setup_56_special $dir $NUMFILES $NUMDIRS
4993
4994         local expected=$((NUMDIRS + NUMFILES))
4995         local cmd="$LFS find -type l $dir"
4996         local nums=$($cmd | wc -l)
4997
4998         [ $nums -eq $expected ] ||
4999                 error "'$cmd' wrong: found $nums, expected $expected"
5000 }
5001 run_test 56n "check lfs find -type l"
5002
5003 test_56o() {
5004         local dir=$DIR/$tdir
5005
5006         setup_56 $dir $NUMFILES $NUMDIRS
5007         utime $dir/file1 > /dev/null || error "utime (1)"
5008         utime $dir/file2 > /dev/null || error "utime (2)"
5009         utime $dir/dir1 > /dev/null || error "utime (3)"
5010         utime $dir/dir2 > /dev/null || error "utime (4)"
5011         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5012         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5013
5014         local expected=4
5015         local nums=$($LFS find -mtime +0 $dir | wc -l)
5016
5017         [ $nums -eq $expected ] ||
5018                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5019
5020         expected=12
5021         cmd="$LFS find -mtime 0 $dir"
5022         nums=$($cmd | wc -l)
5023         [ $nums -eq $expected ] ||
5024                 error "'$cmd' wrong: found $nums, expected $expected"
5025 }
5026 run_test 56o "check lfs find -mtime for old files"
5027
5028 test_56p() {
5029         [ $RUNAS_ID -eq $UID ] &&
5030                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5031
5032         local dir=$DIR/$tdir
5033
5034         setup_56 $dir $NUMFILES $NUMDIRS
5035         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5036
5037         local expected=$NUMFILES
5038         local cmd="$LFS find -uid $RUNAS_ID $dir"
5039         local nums=$($cmd | wc -l)
5040
5041         [ $nums -eq $expected ] ||
5042                 error "'$cmd' wrong: found $nums, expected $expected"
5043
5044         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5045         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5046         nums=$($cmd | wc -l)
5047         [ $nums -eq $expected ] ||
5048                 error "'$cmd' wrong: found $nums, expected $expected"
5049 }
5050 run_test 56p "check lfs find -uid and ! -uid"
5051
5052 test_56q() {
5053         [ $RUNAS_ID -eq $UID ] &&
5054                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5055
5056         local dir=$DIR/$tdir
5057
5058         setup_56 $dir $NUMFILES $NUMDIRS
5059         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5060
5061         local expected=$NUMFILES
5062         local cmd="$LFS find -gid $RUNAS_GID $dir"
5063         local nums=$($cmd | wc -l)
5064
5065         [ $nums -eq $expected ] ||
5066                 error "'$cmd' wrong: found $nums, expected $expected"
5067
5068         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5069         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5070         nums=$($cmd | wc -l)
5071         [ $nums -eq $expected ] ||
5072                 error "'$cmd' wrong: found $nums, expected $expected"
5073 }
5074 run_test 56q "check lfs find -gid and ! -gid"
5075
5076 test_56r() {
5077         local dir=$DIR/$tdir
5078
5079         setup_56 $dir $NUMFILES $NUMDIRS
5080
5081         local expected=12
5082         local cmd="$LFS find -size 0 -type f $dir"
5083         local nums=$($cmd | wc -l)
5084
5085         [ $nums -eq $expected ] ||
5086                 error "'$cmd' wrong: found $nums, expected $expected"
5087         expected=0
5088         cmd="$LFS find ! -size 0 -type f $dir"
5089         nums=$($cmd | wc -l)
5090         [ $nums -eq $expected ] ||
5091                 error "'$cmd' wrong: found $nums, expected $expected"
5092         echo "test" > $dir/$tfile
5093         echo "test2" > $dir/$tfile.2 && sync
5094         expected=1
5095         cmd="$LFS find -size 5 -type f $dir"
5096         nums=$($cmd | wc -l)
5097         [ $nums -eq $expected ] ||
5098                 error "'$cmd' wrong: found $nums, expected $expected"
5099         expected=1
5100         cmd="$LFS find -size +5 -type f $dir"
5101         nums=$($cmd | wc -l)
5102         [ $nums -eq $expected ] ||
5103                 error "'$cmd' wrong: found $nums, expected $expected"
5104         expected=2
5105         cmd="$LFS find -size +0 -type f $dir"
5106         nums=$($cmd | wc -l)
5107         [ $nums -eq $expected ] ||
5108                 error "'$cmd' wrong: found $nums, expected $expected"
5109         expected=2
5110         cmd="$LFS find ! -size -5 -type f $dir"
5111         nums=$($cmd | wc -l)
5112         [ $nums -eq $expected ] ||
5113                 error "'$cmd' wrong: found $nums, expected $expected"
5114         expected=12
5115         cmd="$LFS find -size -5 -type f $dir"
5116         nums=$($cmd | wc -l)
5117         [ $nums -eq $expected ] ||
5118                 error "'$cmd' wrong: found $nums, expected $expected"
5119 }
5120 run_test 56r "check lfs find -size works"
5121
5122 test_56s() { # LU-611 #LU-9369
5123         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
5124
5125         local dir=$DIR/$tdir
5126         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
5127
5128         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5129         for i in $(seq $NUMDIRS); do
5130                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
5131         done
5132
5133         local expected=$NUMDIRS
5134         local cmd="$LFS find -c $OSTCOUNT $dir"
5135         local nums=$($cmd | wc -l)
5136
5137         [ $nums -eq $expected ] || {
5138                 $LFS getstripe -R $dir
5139                 error "'$cmd' wrong: found $nums, expected $expected"
5140         }
5141
5142         expected=$((NUMDIRS + onestripe))
5143         cmd="$LFS find -stripe-count +0 -type f $dir"
5144         nums=$($cmd | wc -l)
5145         [ $nums -eq $expected ] || {
5146                 $LFS getstripe -R $dir
5147                 error "'$cmd' wrong: found $nums, expected $expected"
5148         }
5149
5150         expected=$onestripe
5151         cmd="$LFS find -stripe-count 1 -type f $dir"
5152         nums=$($cmd | wc -l)
5153         [ $nums -eq $expected ] || {
5154                 $LFS getstripe -R $dir
5155                 error "'$cmd' wrong: found $nums, expected $expected"
5156         }
5157
5158         cmd="$LFS find -stripe-count -2 -type f $dir"
5159         nums=$($cmd | wc -l)
5160         [ $nums -eq $expected ] || {
5161                 $LFS getstripe -R $dir
5162                 error "'$cmd' wrong: found $nums, expected $expected"
5163         }
5164
5165         expected=0
5166         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
5167         nums=$($cmd | wc -l)
5168         [ $nums -eq $expected ] || {
5169                 $LFS getstripe -R $dir
5170                 error "'$cmd' wrong: found $nums, expected $expected"
5171         }
5172 }
5173 run_test 56s "check lfs find -stripe-count works"
5174
5175 test_56t() { # LU-611 #LU-9369
5176         local dir=$DIR/$tdir
5177
5178         setup_56 $dir 0 $NUMDIRS
5179         for i in $(seq $NUMDIRS); do
5180                 $LFS setstripe -S 8M $dir/dir$i/$tfile
5181         done
5182
5183         local expected=$NUMDIRS
5184         local cmd="$LFS find -S 8M $dir"
5185         local nums=$($cmd | wc -l)
5186
5187         [ $nums -eq $expected ] || {
5188                 $LFS getstripe -R $dir
5189                 error "'$cmd' wrong: found $nums, expected $expected"
5190         }
5191         rm -rf $dir
5192
5193         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
5194
5195         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
5196
5197         expected=$(((NUMDIRS + 1) * NUMFILES))
5198         cmd="$LFS find -stripe-size 512k -type f $dir"
5199         nums=$($cmd | wc -l)
5200         [ $nums -eq $expected ] ||
5201                 error "'$cmd' wrong: found $nums, expected $expected"
5202
5203         cmd="$LFS find -stripe-size +320k -type f $dir"
5204         nums=$($cmd | wc -l)
5205         [ $nums -eq $expected ] ||
5206                 error "'$cmd' wrong: found $nums, expected $expected"
5207
5208         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
5209         cmd="$LFS find -stripe-size +200k -type f $dir"
5210         nums=$($cmd | wc -l)
5211         [ $nums -eq $expected ] ||
5212                 error "'$cmd' wrong: found $nums, expected $expected"
5213
5214         cmd="$LFS find -stripe-size -640k -type f $dir"
5215         nums=$($cmd | wc -l)
5216         [ $nums -eq $expected ] ||
5217                 error "'$cmd' wrong: found $nums, expected $expected"
5218
5219         expected=4
5220         cmd="$LFS find -stripe-size 256k -type f $dir"
5221         nums=$($cmd | wc -l)
5222         [ $nums -eq $expected ] ||
5223                 error "'$cmd' wrong: found $nums, expected $expected"
5224
5225         cmd="$LFS find -stripe-size -320k -type f $dir"
5226         nums=$($cmd | wc -l)
5227         [ $nums -eq $expected ] ||
5228                 error "'$cmd' wrong: found $nums, expected $expected"
5229
5230         expected=0
5231         cmd="$LFS find -stripe-size 1024k -type f $dir"
5232         nums=$($cmd | wc -l)
5233         [ $nums -eq $expected ] ||
5234                 error "'$cmd' wrong: found $nums, expected $expected"
5235 }
5236 run_test 56t "check lfs find -stripe-size works"
5237
5238 test_56u() { # LU-611
5239         local dir=$DIR/$tdir
5240
5241         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5242
5243         if [[ $OSTCOUNT -gt 1 ]]; then
5244                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5245                 onestripe=4
5246         else
5247                 onestripe=0
5248         fi
5249
5250         local expected=$(((NUMDIRS + 1) * NUMFILES))
5251         local cmd="$LFS find -stripe-index 0 -type f $dir"
5252         local nums=$($cmd | wc -l)
5253
5254         [ $nums -eq $expected ] ||
5255                 error "'$cmd' wrong: found $nums, expected $expected"
5256
5257         expected=$onestripe
5258         cmd="$LFS find -stripe-index 1 -type f $dir"
5259         nums=$($cmd | wc -l)
5260         [ $nums -eq $expected ] ||
5261                 error "'$cmd' wrong: found $nums, expected $expected"
5262
5263         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5264         nums=$($cmd | wc -l)
5265         [ $nums -eq $expected ] ||
5266                 error "'$cmd' wrong: found $nums, expected $expected"
5267
5268         expected=0
5269         # This should produce an error and not return any files
5270         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5271         nums=$($cmd 2>/dev/null | wc -l)
5272         [ $nums -eq $expected ] ||
5273                 error "'$cmd' wrong: found $nums, expected $expected"
5274
5275         if [[ $OSTCOUNT -gt 1 ]]; then
5276                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5277                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5278                 nums=$($cmd | wc -l)
5279                 [ $nums -eq $expected ] ||
5280                         error "'$cmd' wrong: found $nums, expected $expected"
5281         fi
5282 }
5283 run_test 56u "check lfs find -stripe-index works"
5284
5285 test_56v() {
5286         local mdt_idx=0
5287         local dir=$DIR/$tdir
5288
5289         setup_56 $dir $NUMFILES $NUMDIRS
5290
5291         UUID=$(mdtuuid_from_index $mdt_idx $dir)
5292         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
5293
5294         for file in $($LFS find -m $UUID $dir); do
5295                 file_midx=$($LFS getstripe -m $file)
5296                 [ $file_midx -eq $mdt_idx ] ||
5297                         error "lfs find -m $UUID != getstripe -m $file_midx"
5298         done
5299 }
5300 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
5301
5302 test_56w() {
5303         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5304         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5305
5306         local dir=$DIR/$tdir
5307
5308         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5309
5310         local stripe_size=$($LFS getstripe -S -d $dir) ||
5311                 error "$LFS getstripe -S -d $dir failed"
5312         stripe_size=${stripe_size%% *}
5313
5314         local file_size=$((stripe_size * OSTCOUNT))
5315         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5316         local required_space=$((file_num * file_size))
5317         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5318                            head -n1)
5319         [[ $free_space -le $((required_space / 1024)) ]] &&
5320                 skip_env "need $required_space, have $free_space kbytes"
5321
5322         local dd_bs=65536
5323         local dd_count=$((file_size / dd_bs))
5324
5325         # write data into the files
5326         local i
5327         local j
5328         local file
5329
5330         for i in $(seq $NUMFILES); do
5331                 file=$dir/file$i
5332                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5333                         error "write data into $file failed"
5334         done
5335         for i in $(seq $NUMDIRS); do
5336                 for j in $(seq $NUMFILES); do
5337                         file=$dir/dir$i/file$j
5338                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5339                                 error "write data into $file failed"
5340                 done
5341         done
5342
5343         # $LFS_MIGRATE will fail if hard link migration is unsupported
5344         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5345                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5346                         error "creating links to $dir/dir1/file1 failed"
5347         fi
5348
5349         local expected=-1
5350
5351         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5352
5353         # lfs_migrate file
5354         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5355
5356         echo "$cmd"
5357         eval $cmd || error "$cmd failed"
5358
5359         check_stripe_count $dir/file1 $expected
5360
5361         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5362         then
5363                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5364                 # OST 1 if it is on OST 0. This file is small enough to
5365                 # be on only one stripe.
5366                 file=$dir/migr_1_ost
5367                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5368                         error "write data into $file failed"
5369                 local obdidx=$($LFS getstripe -i $file)
5370                 local oldmd5=$(md5sum $file)
5371                 local newobdidx=0
5372
5373                 [[ $obdidx -eq 0 ]] && newobdidx=1
5374                 cmd="$LFS migrate -i $newobdidx $file"
5375                 echo $cmd
5376                 eval $cmd || error "$cmd failed"
5377
5378                 local realobdix=$($LFS getstripe -i $file)
5379                 local newmd5=$(md5sum $file)
5380
5381                 [[ $newobdidx -ne $realobdix ]] &&
5382                         error "new OST is different (was=$obdidx, "\
5383                               "wanted=$newobdidx, got=$realobdix)"
5384                 [[ "$oldmd5" != "$newmd5" ]] &&
5385                         error "md5sum differ: $oldmd5, $newmd5"
5386         fi
5387
5388         # lfs_migrate dir
5389         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5390         echo "$cmd"
5391         eval $cmd || error "$cmd failed"
5392
5393         for j in $(seq $NUMFILES); do
5394                 check_stripe_count $dir/dir1/file$j $expected
5395         done
5396
5397         # lfs_migrate works with lfs find
5398         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5399              $LFS_MIGRATE -y -c $expected"
5400         echo "$cmd"
5401         eval $cmd || error "$cmd failed"
5402
5403         for i in $(seq 2 $NUMFILES); do
5404                 check_stripe_count $dir/file$i $expected
5405         done
5406         for i in $(seq 2 $NUMDIRS); do
5407                 for j in $(seq $NUMFILES); do
5408                 check_stripe_count $dir/dir$i/file$j $expected
5409                 done
5410         done
5411 }
5412 run_test 56w "check lfs_migrate -c stripe_count works"
5413
5414 test_56wb() {
5415         local file1=$DIR/$tdir/file1
5416         local create_pool=false
5417         local initial_pool=$($LFS getstripe -p $DIR)
5418         local pool_list=()
5419         local pool=""
5420
5421         echo -n "Creating test dir..."
5422         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5423         echo "done."
5424
5425         echo -n "Creating test file..."
5426         touch $file1 || error "cannot create file"
5427         echo "done."
5428
5429         echo -n "Detecting existing pools..."
5430         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
5431
5432         if [ ${#pool_list[@]} -gt 0 ]; then
5433                 echo "${pool_list[@]}"
5434                 for thispool in "${pool_list[@]}"; do
5435                         if [[ -z "$initial_pool" ||
5436                               "$initial_pool" != "$thispool" ]]; then
5437                                 pool="$thispool"
5438                                 echo "Using existing pool '$pool'"
5439                                 break
5440                         fi
5441                 done
5442         else
5443                 echo "none detected."
5444         fi
5445         if [ -z "$pool" ]; then
5446                 pool=${POOL:-testpool}
5447                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5448                 echo -n "Creating pool '$pool'..."
5449                 create_pool=true
5450                 pool_add $pool &> /dev/null ||
5451                         error "pool_add failed"
5452                 echo "done."
5453
5454                 echo -n "Adding target to pool..."
5455                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5456                         error "pool_add_targets failed"
5457                 echo "done."
5458         fi
5459
5460         echo -n "Setting pool using -p option..."
5461         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5462                 error "migrate failed rc = $?"
5463         echo "done."
5464
5465         echo -n "Verifying test file is in pool after migrating..."
5466         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5467                 error "file was not migrated to pool $pool"
5468         echo "done."
5469
5470         echo -n "Removing test file from pool '$pool'..."
5471         $LFS migrate $file1 &> /dev/null ||
5472                 error "cannot remove from pool"
5473         [ "$($LFS getstripe -p $file1)" ] &&
5474                 error "pool still set"
5475         echo "done."
5476
5477         echo -n "Setting pool using --pool option..."
5478         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5479                 error "migrate failed rc = $?"
5480         echo "done."
5481
5482         # Clean up
5483         rm -f $file1
5484         if $create_pool; then
5485                 destroy_test_pools 2> /dev/null ||
5486                         error "destroy test pools failed"
5487         fi
5488 }
5489 run_test 56wb "check lfs_migrate pool support"
5490
5491 test_56wc() {
5492         local file1="$DIR/$tdir/file 1"
5493
5494         echo -n "Creating test dir..."
5495         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5496         local def_stripe_size=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
5497         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5498                 error "cannot set stripe"
5499         echo "done"
5500
5501         echo -n "Setting initial stripe for test file..."
5502         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5503                 error "cannot set stripe"
5504         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5505                 error "stripe size not set"
5506         echo "done."
5507
5508         # File currently set to -S 512K -c 1
5509
5510         # Ensure -c and -S options are rejected when -R is set
5511         echo -n "Verifying incompatible options are detected..."
5512         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5513                 error "incompatible -c and -R options not detected"
5514         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5515                 error "incompatible -S and -R options not detected"
5516         echo "done."
5517
5518         # Ensure unrecognized options are passed through to 'lfs migrate'
5519         echo -n "Verifying -S option is passed through to lfs migrate..."
5520         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5521                 error "migration failed"
5522         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5523                 error "file was not restriped"
5524         echo "done."
5525
5526         # File currently set to -S 1M -c 1
5527
5528         # Ensure long options are supported
5529         echo -n "Verifying long options supported..."
5530         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5531                 error "long option without argument not supported"
5532         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5533                 error "long option with argument not supported"
5534         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5535                 error "file not restriped with --stripe-size option"
5536         echo "done."
5537
5538         # File currently set to -S 512K -c 1
5539
5540         if [ "$OSTCOUNT" -gt 1 ]; then
5541                 echo -n "Verifying explicit stripe count can be set..."
5542                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5543                         error "migrate failed"
5544                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
5545                         error "file not restriped to explicit count"
5546                 echo "done."
5547         fi
5548
5549         # File currently set to -S 512K -c 1 or -S 512K -c 2
5550
5551         # Ensure parent striping is used if -R is set, and no stripe
5552         # count or size is specified
5553         echo -n "Setting stripe for parent directory..."
5554         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5555                 error "cannot set stripe"
5556         echo "done."
5557
5558         echo -n "Verifying restripe option uses parent stripe settings..."
5559         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5560                 error "migrate failed"
5561         [ $($LFS getstripe -S "$file1") -eq $def_stripe_size ] ||
5562                 error "file not restriped to parent settings"
5563         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
5564                 error "file not restriped to parent settings"
5565         echo "done."
5566
5567         # File currently set to -S 1M -c 1
5568
5569         # Ensure striping is preserved if -R is not set, and no stripe
5570         # count or size is specified
5571         echo -n "Verifying striping size preserved when not specified..."
5572         local orig_stripe_size=$($LFS getstripe -S "$file1" 2>/dev/null)
5573         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5574                 error "cannot set stripe on parent directory"
5575         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5576                 error "migrate failed"
5577         [ $($LFS getstripe -S "$file1") -eq $orig_stripe_size ] ||
5578                 error "file was restriped"
5579         echo "done."
5580
5581         # Ensure file name properly detected when final option has no argument
5582         echo -n "Verifying file name properly detected..."
5583         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5584                 error "file name interpreted as option argument"
5585         echo "done."
5586
5587         # Clean up
5588         rm -f "$file1"
5589 }
5590 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5591
5592 test_56wd() {
5593         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5594
5595         local file1=$DIR/$tdir/file1
5596
5597         echo -n "Creating test dir..."
5598         test_mkdir $DIR/$tdir || error "cannot create dir"
5599         echo "done."
5600
5601         echo -n "Creating test file..."
5602         touch $file1
5603         echo "done."
5604
5605         # Ensure 'lfs migrate' will fail by using a non-existent option,
5606         # and make sure rsync is not called to recover
5607         echo -n "Make sure --no-rsync option works..."
5608         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5609                 grep -q 'refusing to fall back to rsync' ||
5610                 error "rsync was called with --no-rsync set"
5611         echo "done."
5612
5613         # Ensure rsync is called without trying 'lfs migrate' first
5614         echo -n "Make sure --rsync option works..."
5615         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5616                 grep -q 'falling back to rsync' &&
5617                 error "lfs migrate was called with --rsync set"
5618         echo "done."
5619
5620         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
5621         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
5622                 grep -q 'at the same time' ||
5623                 error "--rsync and --no-rsync accepted concurrently"
5624         echo "done."
5625
5626         # Clean up
5627         rm -f $file1
5628 }
5629 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
5630
5631 test_56x() {
5632         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5633         check_swap_layouts_support
5634
5635         local dir=$DIR/$tdir
5636         local ref1=/etc/passwd
5637         local file1=$dir/file1
5638
5639         test_mkdir $dir || error "creating dir $dir"
5640         $LFS setstripe -c 2 $file1
5641         cp $ref1 $file1
5642         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5643         stripe=$($LFS getstripe -c $file1)
5644         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5645         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5646
5647         # clean up
5648         rm -f $file1
5649 }
5650 run_test 56x "lfs migration support"
5651
5652 test_56xa() {
5653         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5654         check_swap_layouts_support
5655
5656         local dir=$DIR/$tdir/$testnum
5657
5658         test_mkdir -p $dir
5659
5660         local ref1=/etc/passwd
5661         local file1=$dir/file1
5662
5663         $LFS setstripe -c 2 $file1
5664         cp $ref1 $file1
5665         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5666
5667         local stripe=$($LFS getstripe -c $file1)
5668
5669         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5670         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5671
5672         # clean up
5673         rm -f $file1
5674 }
5675 run_test 56xa "lfs migration --block support"
5676
5677 check_migrate_links() {
5678         local dir="$1"
5679         local file1="$dir/file1"
5680         local begin="$2"
5681         local count="$3"
5682         local total_count=$(($begin + $count - 1))
5683         local symlink_count=10
5684         local uniq_count=10
5685
5686         if [ ! -f "$file1" ]; then
5687                 echo -n "creating initial file..."
5688                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5689                         error "cannot setstripe initial file"
5690                 echo "done"
5691
5692                 echo -n "creating symlinks..."
5693                 for s in $(seq 1 $symlink_count); do
5694                         ln -s "$file1" "$dir/slink$s" ||
5695                                 error "cannot create symlinks"
5696                 done
5697                 echo "done"
5698
5699                 echo -n "creating nonlinked files..."
5700                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5701                         error "cannot create nonlinked files"
5702                 echo "done"
5703         fi
5704
5705         # create hard links
5706         if [ ! -f "$dir/file$total_count" ]; then
5707                 echo -n "creating hard links $begin:$total_count..."
5708                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5709                         /dev/null || error "cannot create hard links"
5710                 echo "done"
5711         fi
5712
5713         echo -n "checking number of hard links listed in xattrs..."
5714         local fid=$($LFS getstripe -F "$file1")
5715         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5716
5717         echo "${#paths[*]}"
5718         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5719                         skip "hard link list has unexpected size, skipping test"
5720         fi
5721         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5722                         error "link names should exceed xattrs size"
5723         fi
5724
5725         echo -n "migrating files..."
5726         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5727         local rc=$?
5728         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5729         echo "done"
5730
5731         # make sure all links have been properly migrated
5732         echo -n "verifying files..."
5733         fid=$($LFS getstripe -F "$file1") ||
5734                 error "cannot get fid for file $file1"
5735         for i in $(seq 2 $total_count); do
5736                 local fid2=$($LFS getstripe -F $dir/file$i)
5737
5738                 [ "$fid2" == "$fid" ] ||
5739                         error "migrated hard link has mismatched FID"
5740         done
5741
5742         # make sure hard links were properly detected, and migration was
5743         # performed only once for the entire link set; nonlinked files should
5744         # also be migrated
5745         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5746         local expected=$(($uniq_count + 1))
5747
5748         [ "$actual" -eq  "$expected" ] ||
5749                 error "hard links individually migrated ($actual != $expected)"
5750
5751         # make sure the correct number of hard links are present
5752         local hardlinks=$(stat -c '%h' "$file1")
5753
5754         [ $hardlinks -eq $total_count ] ||
5755                 error "num hard links $hardlinks != $total_count"
5756         echo "done"
5757
5758         return 0
5759 }
5760
5761 test_56xb() {
5762         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
5763                 skip "Need MDS version at least 2.10.55"
5764
5765         local dir="$DIR/$tdir"
5766
5767         test_mkdir "$dir" || error "cannot create dir $dir"
5768
5769         echo "testing lfs migrate mode when all links fit within xattrs"
5770         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 2 99
5771
5772         echo "testing rsync mode when all links fit within xattrs"
5773         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 2 99
5774
5775         echo "testing lfs migrate mode when all links do not fit within xattrs"
5776         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 101 100
5777
5778         echo "testing rsync mode when all links do not fit within xattrs"
5779         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 101 100
5780
5781         # clean up
5782         rm -rf $dir
5783 }
5784 run_test 56xb "lfs migration hard link support"
5785
5786 test_56y() {
5787         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5788                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
5789
5790         local res=""
5791         local dir=$DIR/$tdir
5792         local f1=$dir/file1
5793         local f2=$dir/file2
5794
5795         test_mkdir -p $dir || error "creating dir $dir"
5796         touch $f1 || error "creating std file $f1"
5797         $MULTIOP $f2 H2c || error "creating released file $f2"
5798
5799         # a directory can be raid0, so ask only for files
5800         res=$($LFS find $dir -L raid0 -type f | wc -l)
5801         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5802
5803         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
5804         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5805
5806         # only files can be released, so no need to force file search
5807         res=$($LFS find $dir -L released)
5808         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5809
5810         res=$($LFS find $dir -type f \! -L released)
5811         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5812 }
5813 run_test 56y "lfs find -L raid0|released"
5814
5815 test_56z() { # LU-4824
5816         # This checks to make sure 'lfs find' continues after errors
5817         # There are two classes of errors that should be caught:
5818         # - If multiple paths are provided, all should be searched even if one
5819         #   errors out
5820         # - If errors are encountered during the search, it should not terminate
5821         #   early
5822         local dir=$DIR/$tdir
5823         local i
5824
5825         test_mkdir $dir
5826         for i in d{0..9}; do
5827                 test_mkdir $dir/$i
5828         done
5829         touch $dir/d{0..9}/$tfile
5830         $LFS find $DIR/non_existent_dir $dir &&
5831                 error "$LFS find did not return an error"
5832         # Make a directory unsearchable. This should NOT be the last entry in
5833         # directory order.  Arbitrarily pick the 6th entry
5834         chmod 700 $($LFS find $dir -type d | sed '6!d')
5835
5836         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
5837
5838         # The user should be able to see 10 directories and 9 files
5839         [ $count == 19 ] || error "$LFS find did not continue after error"
5840 }
5841 run_test 56z "lfs find should continue after an error"
5842
5843 test_56aa() { # LU-5937
5844         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
5845
5846         local dir=$DIR/$tdir
5847
5848         mkdir $dir
5849         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
5850
5851         createmany -o $dir/striped_dir/${tfile}- 1024
5852         local dirs=$($LFS find --size +8k $dir/)
5853
5854         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5855 }
5856 run_test 56aa "lfs find --size under striped dir"
5857
5858 test_56ab() { # LU-10705
5859         test_mkdir $DIR/$tdir
5860         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
5861         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
5862         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
5863         # Flush writes to ensure valid blocks.  Need to be more thorough for
5864         # ZFS, since blocks are not allocated/returned to client immediately.
5865         sync_all_data
5866         wait_zfs_commit ost1 2
5867         cancel_lru_locks osc
5868         ls -ls $DIR/$tdir
5869
5870         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
5871
5872         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
5873
5874         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
5875         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
5876
5877         rm -f $DIR/$tdir/$tfile.[123]
5878 }
5879 run_test 56ab "lfs find --blocks"
5880
5881 test_56ba() {
5882         # Create composite files with one component
5883         local dir=$DIR/$tdir
5884
5885         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
5886         # Create composite files with three components
5887         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
5888         # Create non-composite files
5889         createmany -o $dir/${tfile}- 10
5890
5891         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
5892
5893         [[ $nfiles == 10 ]] ||
5894                 error "lfs find -E 1M found $nfiles != 10 files"
5895
5896         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
5897         [[ $nfiles == 25 ]] ||
5898                 error "lfs find ! -E 1M found $nfiles != 25 files"
5899
5900         # All files have a component that starts at 0
5901         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
5902         [[ $nfiles == 35 ]] ||
5903                 error "lfs find --component-start 0 - $nfiles != 35 files"
5904
5905         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
5906         [[ $nfiles == 15 ]] ||
5907                 error "lfs find --component-start 2M - $nfiles != 15 files"
5908
5909         # All files created here have a componenet that does not starts at 2M
5910         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
5911         [[ $nfiles == 35 ]] ||
5912                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
5913
5914         # Find files with a specified number of components
5915         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
5916         [[ $nfiles == 15 ]] ||
5917                 error "lfs find --component-count 3 - $nfiles != 15 files"
5918
5919         # Remember non-composite files have a component count of zero
5920         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
5921         [[ $nfiles == 10 ]] ||
5922                 error "lfs find --component-count 0 - $nfiles != 10 files"
5923
5924         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
5925         [[ $nfiles == 20 ]] ||
5926                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
5927
5928         # All files have a flag called "init"
5929         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
5930         [[ $nfiles == 35 ]] ||
5931                 error "lfs find --component-flags init - $nfiles != 35 files"
5932
5933         # Multi-component files will have a component not initialized
5934         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
5935         [[ $nfiles == 15 ]] ||
5936                 error "lfs find !--component-flags init - $nfiles != 15 files"
5937
5938         rm -rf $dir
5939
5940 }
5941 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
5942
5943 test_56ca() {
5944         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.57) ]] ||
5945                 skip "Need MDS version at least 2.10.57"
5946
5947         local td=$DIR/$tdir
5948         local tf=$td/$tfile
5949         local dir
5950         local nfiles
5951         local cmd
5952         local i
5953         local j
5954
5955         # create mirrored directories and mirrored files
5956         mkdir $td || error "mkdir $td failed"
5957         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
5958         createmany -o $tf- 10 || error "create $tf- failed"
5959
5960         for i in $(seq 2); do
5961                 dir=$td/dir$i
5962                 mkdir $dir || error "mkdir $dir failed"
5963                 $LFS mirror create -N$((3 + i)) $dir ||
5964                         error "create mirrored dir $dir failed"
5965                 createmany -o $dir/$tfile- 10 ||
5966                         error "create $dir/$tfile- failed"
5967         done
5968
5969         # change the states of some mirrored files
5970         echo foo > $tf-6
5971         for i in $(seq 2); do
5972                 dir=$td/dir$i
5973                 for j in $(seq 4 9); do
5974                         echo foo > $dir/$tfile-$j
5975                 done
5976         done
5977
5978         # find mirrored files with specific mirror count
5979         cmd="$LFS find --mirror-count 3 --type f $td"
5980         nfiles=$($cmd | wc -l)
5981         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
5982
5983         cmd="$LFS find ! --mirror-count 3 --type f $td"
5984         nfiles=$($cmd | wc -l)
5985         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
5986
5987         cmd="$LFS find --mirror-count +2 --type f $td"
5988         nfiles=$($cmd | wc -l)
5989         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
5990
5991         cmd="$LFS find --mirror-count -6 --type f $td"
5992         nfiles=$($cmd | wc -l)
5993         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
5994
5995         # find mirrored files with specific file state
5996         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
5997         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
5998
5999         cmd="$LFS find --mirror-state=ro --type f $td"
6000         nfiles=$($cmd | wc -l)
6001         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
6002
6003         cmd="$LFS find ! --mirror-state=ro --type f $td"
6004         nfiles=$($cmd | wc -l)
6005         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6006
6007         cmd="$LFS find --mirror-state=wp --type f $td"
6008         nfiles=$($cmd | wc -l)
6009         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6010
6011         cmd="$LFS find ! --mirror-state=sp --type f $td"
6012         nfiles=$($cmd | wc -l)
6013         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6014 }
6015 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6016
6017 test_57a() {
6018         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6019         # note test will not do anything if MDS is not local
6020         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
6021                 skip_env "ldiskfs only test"
6022         fi
6023         remote_mds_nodsh && skip "remote MDS with nodsh"
6024
6025         local MNTDEV="osd*.*MDT*.mntdev"
6026         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6027         [ -z "$DEV" ] && error "can't access $MNTDEV"
6028         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6029                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
6030                         error "can't access $DEV"
6031                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
6032                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
6033                 rm $TMP/t57a.dump
6034         done
6035 }
6036 run_test 57a "verify MDS filesystem created with large inodes =="
6037
6038 test_57b() {
6039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6040         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
6041                 skip_env "ldiskfs only test"
6042         fi
6043         remote_mds_nodsh && skip "remote MDS with nodsh"
6044
6045         local dir=$DIR/$tdir
6046         local filecount=100
6047         local file1=$dir/f1
6048         local fileN=$dir/f$filecount
6049
6050         rm -rf $dir || error "removing $dir"
6051         test_mkdir -c1 $dir
6052         local mdtidx=$($LFS getstripe -m $dir)
6053         local mdtname=MDT$(printf %04x $mdtidx)
6054         local facet=mds$((mdtidx + 1))
6055
6056         echo "mcreating $filecount files"
6057         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
6058
6059         # verify that files do not have EAs yet
6060         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
6061                 error "$file1 has an EA"
6062         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
6063                 error "$fileN has an EA"
6064
6065         sync
6066         sleep 1
6067         df $dir  #make sure we get new statfs data
6068         local mdsfree=$(do_facet $facet \
6069                         lctl get_param -n osd*.*$mdtname.kbytesfree)
6070         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6071         local file
6072
6073         echo "opening files to create objects/EAs"
6074         for file in $(seq -f $dir/f%g 1 $filecount); do
6075                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
6076                         error "opening $file"
6077         done
6078
6079         # verify that files have EAs now
6080         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
6081         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
6082
6083         sleep 1  #make sure we get new statfs data
6084         df $dir
6085         local mdsfree2=$(do_facet $facet \
6086                          lctl get_param -n osd*.*$mdtname.kbytesfree)
6087         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6088
6089         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
6090                 if [ "$mdsfree" != "$mdsfree2" ]; then
6091                         error "MDC before $mdcfree != after $mdcfree2"
6092                 else
6093                         echo "MDC before $mdcfree != after $mdcfree2"
6094                         echo "unable to confirm if MDS has large inodes"
6095                 fi
6096         fi
6097         rm -rf $dir
6098 }
6099 run_test 57b "default LOV EAs are stored inside large inodes ==="
6100
6101 test_58() {
6102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6103         [ -z "$(which wiretest 2>/dev/null)" ] &&
6104                         skip_env "could not find wiretest"
6105
6106         wiretest
6107 }
6108 run_test 58 "verify cross-platform wire constants =============="
6109
6110 test_59() {
6111         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6112
6113         echo "touch 130 files"
6114         createmany -o $DIR/f59- 130
6115         echo "rm 130 files"
6116         unlinkmany $DIR/f59- 130
6117         sync
6118         # wait for commitment of removal
6119         wait_delete_completed
6120 }
6121 run_test 59 "verify cancellation of llog records async ========="
6122
6123 TEST60_HEAD="test_60 run $RANDOM"
6124 test_60a() {
6125         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6126         remote_mgs_nodsh && skip "remote MGS with nodsh"
6127         do_facet mgs "! which run-llog.sh &> /dev/null" &&
6128                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
6129                         skip_env "missing subtest run-llog.sh"
6130
6131         log "$TEST60_HEAD - from kernel mode"
6132         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
6133         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
6134         do_facet mgs $LCTL dk > $TMP/$tfile
6135
6136         # LU-6388: test llog_reader
6137         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
6138         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
6139         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
6140                         skip_env "missing llog_reader"
6141         local fstype=$(facet_fstype mgs)
6142         [ $fstype != ldiskfs -a $fstype != zfs ] &&
6143                 skip_env "Only for ldiskfs or zfs type mgs"
6144
6145         local mntpt=$(facet_mntpt mgs)
6146         local mgsdev=$(mgsdevname 1)
6147         local fid_list
6148         local fid
6149         local rec_list
6150         local rec
6151         local rec_type
6152         local obj_file
6153         local path
6154         local seq
6155         local oid
6156         local pass=true
6157
6158         #get fid and record list
6159         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
6160                 tail -n 4))
6161         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
6162                 tail -n 4))
6163         #remount mgs as ldiskfs or zfs type
6164         stop mgs || error "stop mgs failed"
6165         mount_fstype mgs || error "remount mgs failed"
6166         for ((i = 0; i < ${#fid_list[@]}; i++)); do
6167                 fid=${fid_list[i]}
6168                 rec=${rec_list[i]}
6169                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
6170                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
6171                 oid=$((16#$oid))
6172
6173                 case $fstype in
6174                         ldiskfs )
6175                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
6176                         zfs )
6177                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
6178                 esac
6179                 echo "obj_file is $obj_file"
6180                 do_facet mgs $llog_reader $obj_file
6181
6182                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
6183                         awk '{ print $3 }' | sed -e "s/^type=//g")
6184                 if [ $rec_type != $rec ]; then
6185                         echo "FAILED test_60a wrong record type $rec_type," \
6186                               "should be $rec"
6187                         pass=false
6188                         break
6189                 fi
6190
6191                 #check obj path if record type is LLOG_LOGID_MAGIC
6192                 if [ "$rec" == "1064553b" ]; then
6193                         path=$(do_facet mgs $llog_reader $obj_file |
6194                                 grep "path=" | awk '{ print $NF }' |
6195                                 sed -e "s/^path=//g")
6196                         if [ $obj_file != $mntpt/$path ]; then
6197                                 echo "FAILED test_60a wrong obj path" \
6198                                       "$montpt/$path, should be $obj_file"
6199                                 pass=false
6200                                 break
6201                         fi
6202                 fi
6203         done
6204         rm -f $TMP/$tfile
6205         #restart mgs before "error", otherwise it will block the next test
6206         stop mgs || error "stop mgs failed"
6207         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
6208         $pass || error "test failed, see FAILED test_60a messages for specifics"
6209 }
6210 run_test 60a "llog_test run from kernel module and test llog_reader"
6211
6212 test_60aa() {
6213         remote_mgs_nodsh && skip "remote MGS with nodsh"
6214
6215         # test old logid format
6216         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
6217                 do_facet mgs $LCTL dl | grep MGS
6218                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
6219                         error "old llog_print failed"
6220         fi
6221
6222         # test new logid format
6223         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
6224                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
6225                         error "new llog_print failed"
6226         fi
6227 }
6228 run_test 60aa "llog_print works with FIDs and simple names"
6229
6230 test_60ab() {
6231         # test llog_print with params
6232
6233         [[ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.11.51) ]] ||
6234                 skip "Need server version greater than 2.11.51"
6235
6236         local yaml
6237         local orig_val
6238
6239         orig_val=$(do_facet mgs $LCTL get_param jobid_name)
6240         do_facet mgs $LCTL set_param -P jobid_name="testname"
6241
6242         yaml=$(do_facet mgs $LCTL --device MGS llog_print params |
6243             grep jobid_name | tail -n 1)
6244
6245         local param=`awk '{ print $10 }' <<< "$yaml"`
6246         local val=`awk '{ print $12 }' <<< "$yaml"`
6247         #return to the default
6248         do_facet mgs $LCTL set_param -P jobid_name=$orig_val
6249         [ $val = "testname" ] || error "bad value: $val"
6250         [ $param = "jobid_name," ] || error "Bad param: $param"
6251 }
6252 run_test 60ab "llog_print params output values from set_param -P"
6253
6254 test_60b() { # bug 6411
6255         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6256
6257         dmesg > $DIR/$tfile
6258         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
6259                                 /llog.test/ {
6260                                         if (marker)
6261                                                 from_marker++
6262                                         from_begin++
6263                                 }
6264                                 END {
6265                                         if (marker)
6266                                                 print from_marker
6267                                         else
6268                                                 print from_begin
6269                                 }")
6270         [[ $LLOG_COUNT -gt 100 ]] &&
6271                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
6272 }
6273 run_test 60b "limit repeated messages from CERROR/CWARN ========"
6274
6275 test_60c() {
6276         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6277
6278         echo "create 5000 files"
6279         createmany -o $DIR/f60c- 5000
6280 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
6281         lctl set_param fail_loc=0x80000137
6282         unlinkmany $DIR/f60c- 5000
6283         lctl set_param fail_loc=0
6284 }
6285 run_test 60c "unlink file when mds full"
6286
6287 test_60d() {
6288         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6289
6290         SAVEPRINTK=$(lctl get_param -n printk)
6291         # verify "lctl mark" is even working"
6292         MESSAGE="test message ID $RANDOM $$"
6293         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6294         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
6295
6296         lctl set_param printk=0 || error "set lnet.printk failed"
6297         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
6298         MESSAGE="new test message ID $RANDOM $$"
6299         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
6300         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6301         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
6302
6303         lctl set_param -n printk="$SAVEPRINTK"
6304 }
6305 run_test 60d "test printk console message masking"
6306
6307 test_60e() {
6308         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6309         remote_mds_nodsh && skip "remote MDS with nodsh"
6310
6311         touch $DIR/$tfile
6312 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
6313         do_facet mds1 lctl set_param fail_loc=0x15b
6314         rm $DIR/$tfile
6315 }
6316 run_test 60e "no space while new llog is being created"
6317
6318 test_61() {
6319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6320
6321         f="$DIR/f61"
6322         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
6323         cancel_lru_locks osc
6324         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6325         sync
6326 }
6327 run_test 61 "mmap() writes don't make sync hang ================"
6328
6329 # bug 2330 - insufficient obd_match error checking causes LBUG
6330 test_62() {
6331         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6332
6333         f="$DIR/f62"
6334         echo foo > $f
6335         cancel_lru_locks osc
6336         lctl set_param fail_loc=0x405
6337         cat $f && error "cat succeeded, expect -EIO"
6338         lctl set_param fail_loc=0
6339 }
6340 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6341 # match every page all of the time.
6342 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6343
6344 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6345 # Though this test is irrelevant anymore, it helped to reveal some
6346 # other grant bugs (LU-4482), let's keep it.
6347 test_63a() {   # was test_63
6348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6349
6350         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6351
6352         for i in `seq 10` ; do
6353                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6354                 sleep 5
6355                 kill $!
6356                 sleep 1
6357         done
6358
6359         rm -f $DIR/f63 || true
6360 }
6361 run_test 63a "Verify oig_wait interruption does not crash ======="
6362
6363 # bug 2248 - async write errors didn't return to application on sync
6364 # bug 3677 - async write errors left page locked
6365 test_63b() {
6366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6367
6368         debugsave
6369         lctl set_param debug=-1
6370
6371         # ensure we have a grant to do async writes
6372         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6373         rm $DIR/$tfile
6374
6375         sync    # sync lest earlier test intercept the fail_loc
6376
6377         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6378         lctl set_param fail_loc=0x80000406
6379         $MULTIOP $DIR/$tfile Owy && \
6380                 error "sync didn't return ENOMEM"
6381         sync; sleep 2; sync     # do a real sync this time to flush page
6382         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6383                 error "locked page left in cache after async error" || true
6384         debugrestore
6385 }
6386 run_test 63b "async write errors should be returned to fsync ==="
6387
6388 test_64a () {
6389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6390
6391         df $DIR
6392         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6393 }
6394 run_test 64a "verify filter grant calculations (in kernel) ====="
6395
6396 test_64b () {
6397         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6398
6399         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6400 }
6401 run_test 64b "check out-of-space detection on client"
6402
6403 test_64c() {
6404         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6405 }
6406 run_test 64c "verify grant shrink"
6407
6408 # this does exactly what osc_request.c:osc_announce_cached() does in
6409 # order to calculate max amount of grants to ask from server
6410 want_grant() {
6411         local tgt=$1
6412
6413         local page_size=$(get_page_size client)
6414
6415         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6416         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6417
6418         ((rpc_in_flight ++));
6419         nrpages=$((nrpages * rpc_in_flight))
6420
6421         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6422
6423         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / page_size))
6424
6425         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6426         local undirty=$((nrpages * page_size))
6427
6428         local max_extent_pages
6429         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6430             grep grant_max_extent_size | awk '{print $2}')
6431         max_extent_pages=$((max_extent_pages / page_size))
6432         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6433         local grant_extent_tax
6434         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6435             grep grant_extent_tax | awk '{print $2}')
6436
6437         undirty=$((undirty + nrextents * grant_extent_tax))
6438
6439         echo $undirty
6440 }
6441
6442 # this is size of unit for grant allocation. It should be equal to
6443 # what tgt_grant.c:tgt_grant_chunk() calculates
6444 grant_chunk() {
6445         local tgt=$1
6446         local max_brw_size
6447         local grant_extent_tax
6448
6449         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6450             grep max_brw_size | awk '{print $2}')
6451
6452         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6453             grep grant_extent_tax | awk '{print $2}')
6454
6455         echo $(((max_brw_size + grant_extent_tax) * 2))
6456 }
6457
6458 test_64d() {
6459         [ $(lustre_version_code ost1) -lt $(version_code 2.10.56) ] &&
6460                 skip "OST < 2.10.55 doesn't limit grants enough"
6461
6462         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6463         local file=$DIR/$tfile
6464
6465         [[ $($LCTL get_param osc.${tgt}.import |
6466              grep "connect_flags:.*grant_param") ]] ||
6467                 skip "no grant_param connect flag"
6468
6469         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6470
6471         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6472
6473         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6474         stack_trap "rm -f $file" EXIT
6475
6476         $SETSTRIPE $file -i 0 -c 1
6477         dd if=/dev/zero of=$file bs=1M count=1000 &
6478         ddpid=$!
6479
6480         while true
6481         do
6482                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6483                 if [[ $cur_grant -gt $max_cur_granted ]]
6484                 then
6485                         kill $ddpid
6486                         error "cur_grant $cur_grant > $max_cur_granted"
6487                 fi
6488                 kill -0 $ddpid
6489                 [[ $? -ne 0 ]] && break;
6490                 sleep 2
6491         done
6492
6493         rm -f $DIR/$tfile
6494         wait_delete_completed
6495         $LCTL set_param debug="$olddebug" 2> /dev/null || true
6496 }
6497 run_test 64d "check grant limit exceed"
6498
6499 # bug 1414 - set/get directories' stripe info
6500 test_65a() {
6501         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6502
6503         test_mkdir $DIR/$tdir
6504         touch $DIR/$tdir/f1
6505         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
6506 }
6507 run_test 65a "directory with no stripe info"
6508
6509 test_65b() {
6510         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6511
6512         test_mkdir $DIR/$tdir
6513         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6514
6515         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6516                                                 error "setstripe"
6517         touch $DIR/$tdir/f2
6518         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
6519 }
6520 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
6521
6522 test_65c() {
6523         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6524         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
6525
6526         test_mkdir $DIR/$tdir
6527         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
6528
6529         $LFS setstripe -S $((stripesize * 4)) -i 1 \
6530                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
6531         touch $DIR/$tdir/f3
6532         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
6533 }
6534 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
6535
6536 test_65d() {
6537         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6538
6539         test_mkdir $DIR/$tdir
6540         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
6541         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6542
6543         if [[ $STRIPECOUNT -le 0 ]]; then
6544                 sc=1
6545         elif [[ $STRIPECOUNT -gt 2000 ]]; then
6546 #LOV_MAX_STRIPE_COUNT is 2000
6547                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
6548         else
6549                 sc=$(($STRIPECOUNT - 1))
6550         fi
6551         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
6552         touch $DIR/$tdir/f4 $DIR/$tdir/f5
6553         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
6554                 error "lverify failed"
6555 }
6556 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
6557
6558 test_65e() {
6559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6560
6561         test_mkdir $DIR/$tdir
6562
6563         $SETSTRIPE $DIR/$tdir || error "setstripe"
6564         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
6565                                         error "no stripe info failed"
6566         touch $DIR/$tdir/f6
6567         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
6568 }
6569 run_test 65e "directory setstripe defaults"
6570
6571 test_65f() {
6572         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6573
6574         test_mkdir $DIR/${tdir}f
6575         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
6576 }
6577 run_test 65f "dir setstripe permission (should return error) ==="
6578
6579 test_65g() {
6580         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6581
6582         test_mkdir $DIR/$tdir
6583         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6584
6585         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6586                 error "setstripe -S failed"
6587         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
6588         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6589                 error "delete default stripe failed"
6590 }
6591 run_test 65g "directory setstripe -d"
6592
6593 test_65h() {
6594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6595
6596         test_mkdir $DIR/$tdir
6597         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6598
6599         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6600                 error "setstripe -S failed"
6601         test_mkdir $DIR/$tdir/dd1
6602         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
6603                 error "stripe info inherit failed"
6604 }
6605 run_test 65h "directory stripe info inherit ===================="
6606
6607 test_65i() {
6608         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6609
6610         save_layout_restore_at_exit $MOUNT
6611
6612         # bug6367: set non-default striping on root directory
6613         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
6614
6615         # bug12836: getstripe on -1 default directory striping
6616         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
6617
6618         # bug12836: getstripe -v on -1 default directory striping
6619         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
6620
6621         # bug12836: new find on -1 default directory striping
6622         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
6623 }
6624 run_test 65i "various tests to set root directory striping"
6625
6626 test_65j() { # bug6367
6627         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6628
6629         sync; sleep 1
6630
6631         # if we aren't already remounting for each test, do so for this test
6632         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
6633                 cleanup || error "failed to unmount"
6634                 setup
6635         fi
6636
6637         save_layout_restore_at_exit $MOUNT
6638
6639         $SETSTRIPE -d $MOUNT || error "setstripe failed"
6640 }
6641 run_test 65j "set default striping on root directory (bug 6367)="
6642
6643 cleaup_65k() {
6644         rm -rf $DIR/$tdir
6645         wait_delete_completed
6646         do_facet $SINGLEMDS "lctl set_param -n \
6647                 osp.$ost*MDT0000.max_create_count=$max_count"
6648         do_facet $SINGLEMDS "lctl set_param -n \
6649                 osp.$ost*MDT0000.create_count=$count"
6650         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6651         echo $INACTIVE_OSC "is Activate"
6652
6653         wait_osc_import_state mds ost$ostnum FULL
6654 }
6655
6656 test_65k() { # bug11679
6657         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6658         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6659         remote_mds_nodsh && skip "remote MDS with nodsh"
6660
6661         local disable_precreate=true
6662         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
6663                 disable_precreate=false
6664
6665         echo "Check OST status: "
6666         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
6667                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
6668
6669         for OSC in $MDS_OSCS; do
6670                 echo $OSC "is active"
6671                 do_facet $SINGLEMDS lctl --device %$OSC activate
6672         done
6673
6674         for INACTIVE_OSC in $MDS_OSCS; do
6675                 local ost=$(osc_to_ost $INACTIVE_OSC)
6676                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
6677                                lov.*md*.target_obd |
6678                                awk -F: /$ost/'{ print $1 }' | head -n 1)
6679
6680                 mkdir -p $DIR/$tdir
6681                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
6682                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
6683
6684                 echo "Deactivate: " $INACTIVE_OSC
6685                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
6686
6687                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
6688                               osp.$ost*MDT0000.create_count")
6689                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
6690                                   osp.$ost*MDT0000.max_create_count")
6691                 $disable_precreate &&
6692                         do_facet $SINGLEMDS "lctl set_param -n \
6693                                 osp.$ost*MDT0000.max_create_count=0"
6694
6695                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
6696                         [ -f $DIR/$tdir/$idx ] && continue
6697                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
6698                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
6699                                 { cleanup_65k;
6700                                   error "setstripe $idx should succeed"; }
6701                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
6702                 done
6703                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
6704                 rmdir $DIR/$tdir
6705
6706                 do_facet $SINGLEMDS "lctl set_param -n \
6707                         osp.$ost*MDT0000.max_create_count=$max_count"
6708                 do_facet $SINGLEMDS "lctl set_param -n \
6709                         osp.$ost*MDT0000.create_count=$count"
6710                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6711                 echo $INACTIVE_OSC "is Activate"
6712
6713                 wait_osc_import_state mds ost$ostnum FULL
6714         done
6715 }
6716 run_test 65k "validate manual striping works properly with deactivated OSCs"
6717
6718 test_65l() { # bug 12836
6719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6720
6721         test_mkdir -p $DIR/$tdir/test_dir
6722         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
6723         $LFS find -mtime -1 $DIR/$tdir >/dev/null
6724 }
6725 run_test 65l "lfs find on -1 stripe dir ========================"
6726
6727 test_65m() {
6728         local layout=$(save_layout $MOUNT)
6729         $RUNAS $SETSTRIPE -c 2 $MOUNT && {
6730                 restore_layout $MOUNT $layout
6731                 error "setstripe should fail by non-root users"
6732         }
6733         true
6734 }
6735 run_test 65m "normal user can't set filesystem default stripe"
6736
6737 # bug 2543 - update blocks count on client
6738 test_66() {
6739         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6740
6741         COUNT=${COUNT:-8}
6742         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6743         sync; sync_all_data; sync; sync_all_data
6744         cancel_lru_locks osc
6745         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6746         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6747 }
6748 run_test 66 "update inode blocks count on client ==============="
6749
6750 meminfo() {
6751         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6752 }
6753
6754 swap_used() {
6755         swapon -s | awk '($1 == "'$1'") { print $4 }'
6756 }
6757
6758 # bug5265, obdfilter oa2dentry return -ENOENT
6759 # #define OBD_FAIL_SRV_ENOENT 0x217
6760 test_69() {
6761         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6762         remote_ost_nodsh && skip "remote OST with nodsh"
6763
6764         f="$DIR/$tfile"
6765         $SETSTRIPE -c 1 -i 0 $f
6766
6767         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6768
6769         do_facet ost1 lctl set_param fail_loc=0x217
6770         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6771         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6772
6773         do_facet ost1 lctl set_param fail_loc=0
6774         $DIRECTIO write $f 0 2 || error "write error"
6775
6776         cancel_lru_locks osc
6777         $DIRECTIO read $f 0 1 || error "read error"
6778
6779         do_facet ost1 lctl set_param fail_loc=0x217
6780         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6781
6782         do_facet ost1 lctl set_param fail_loc=0
6783         rm -f $f
6784 }
6785 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6786
6787 test_71() {
6788         test_mkdir $DIR/$tdir
6789         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6790         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6791 }
6792 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6793
6794 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6795         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6796         [ "$RUNAS_ID" = "$UID" ] &&
6797                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6798         # Check that testing environment is properly set up. Skip if not
6799         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
6800                 skip_env "User $RUNAS_ID does not exist - skipping"
6801
6802         touch $DIR/$tfile
6803         chmod 777 $DIR/$tfile
6804         chmod ug+s $DIR/$tfile
6805         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6806                 error "$RUNAS dd $DIR/$tfile failed"
6807         # See if we are still setuid/sgid
6808         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6809                 error "S/gid is not dropped on write"
6810         # Now test that MDS is updated too
6811         cancel_lru_locks mdc
6812         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6813                 error "S/gid is not dropped on MDS"
6814         rm -f $DIR/$tfile
6815 }
6816 run_test 72a "Test that remove suid works properly (bug5695) ===="
6817
6818 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6819         local perm
6820
6821         [ "$RUNAS_ID" = "$UID" ] &&
6822                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6823         [ "$RUNAS_ID" -eq 0 ] &&
6824                 skip_env "RUNAS_ID = 0 -- skipping"
6825         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6826         # Check that testing environment is properly set up. Skip if not
6827         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
6828                 skip_env "User $RUNAS_ID does not exist - skipping"
6829
6830         touch $DIR/${tfile}-f{g,u}
6831         test_mkdir $DIR/${tfile}-dg
6832         test_mkdir $DIR/${tfile}-du
6833         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6834         chmod g+s $DIR/${tfile}-{f,d}g
6835         chmod u+s $DIR/${tfile}-{f,d}u
6836         for perm in 777 2777 4777; do
6837                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6838                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6839                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6840                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6841         done
6842         true
6843 }
6844 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6845
6846 # bug 3462 - multiple simultaneous MDC requests
6847 test_73() {
6848         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6849
6850         test_mkdir $DIR/d73-1
6851         test_mkdir $DIR/d73-2
6852         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6853         pid1=$!
6854
6855         lctl set_param fail_loc=0x80000129
6856         $MULTIOP $DIR/d73-1/f73-2 Oc &
6857         sleep 1
6858         lctl set_param fail_loc=0
6859
6860         $MULTIOP $DIR/d73-2/f73-3 Oc &
6861         pid3=$!
6862
6863         kill -USR1 $pid1
6864         wait $pid1 || return 1
6865
6866         sleep 25
6867
6868         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6869         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6870         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6871
6872         rm -rf $DIR/d73-*
6873 }
6874 run_test 73 "multiple MDC requests (should not deadlock)"
6875
6876 test_74a() { # bug 6149, 6184
6877         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6878
6879         touch $DIR/f74a
6880         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6881         #
6882         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6883         # will spin in a tight reconnection loop
6884         $LCTL set_param fail_loc=0x8000030e
6885         # get any lock that won't be difficult - lookup works.
6886         ls $DIR/f74a
6887         $LCTL set_param fail_loc=0
6888         rm -f $DIR/f74a
6889         true
6890 }
6891 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6892
6893 test_74b() { # bug 13310
6894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6895
6896         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6897         #
6898         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6899         # will spin in a tight reconnection loop
6900         $LCTL set_param fail_loc=0x8000030e
6901         # get a "difficult" lock
6902         touch $DIR/f74b
6903         $LCTL set_param fail_loc=0
6904         rm -f $DIR/f74b
6905         true
6906 }
6907 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6908
6909 test_74c() {
6910         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6911
6912         #define OBD_FAIL_LDLM_NEW_LOCK
6913         $LCTL set_param fail_loc=0x319
6914         touch $DIR/$tfile && error "touch successful"
6915         $LCTL set_param fail_loc=0
6916         true
6917 }
6918 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6919
6920 num_inodes() {
6921         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6922 }
6923
6924 test_76() { # Now for bug 20433, added originally in bug 1443
6925         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6926
6927         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6928
6929         cancel_lru_locks osc
6930         BEFORE_INODES=$(num_inodes)
6931         echo "before inodes: $BEFORE_INODES"
6932         local COUNT=1000
6933         [ "$SLOW" = "no" ] && COUNT=100
6934         for i in $(seq $COUNT); do
6935                 touch $DIR/$tfile
6936                 rm -f $DIR/$tfile
6937         done
6938         cancel_lru_locks osc
6939         AFTER_INODES=$(num_inodes)
6940         echo "after inodes: $AFTER_INODES"
6941         local wait=0
6942         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6943                 sleep 2
6944                 AFTER_INODES=$(num_inodes)
6945                 wait=$((wait+2))
6946                 echo "wait $wait seconds inodes: $AFTER_INODES"
6947                 if [ $wait -gt 30 ]; then
6948                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6949                 fi
6950         done
6951 }
6952 run_test 76 "confirm clients recycle inodes properly ===="
6953
6954
6955 export ORIG_CSUM=""
6956 set_checksums()
6957 {
6958         # Note: in sptlrpc modes which enable its own bulk checksum, the
6959         # original crc32_le bulk checksum will be automatically disabled,
6960         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6961         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6962         # In this case set_checksums() will not be no-op, because sptlrpc
6963         # bulk checksum will be enabled all through the test.
6964
6965         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6966         lctl set_param -n osc.*.checksums $1
6967         return 0
6968 }
6969
6970 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6971                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6972 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6973                              tr -d [] | head -n1)}
6974 set_checksum_type()
6975 {
6976         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6977         log "set checksum type to $1"
6978         return 0
6979 }
6980 F77_TMP=$TMP/f77-temp
6981 F77SZ=8
6982 setup_f77() {
6983         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6984                 error "error writing to $F77_TMP"
6985 }
6986
6987 test_77a() { # bug 10889
6988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6989         $GSS && skip_env "could not run with gss"
6990
6991         [ ! -f $F77_TMP ] && setup_f77
6992         set_checksums 1
6993         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6994         set_checksums 0
6995         rm -f $DIR/$tfile
6996 }
6997 run_test 77a "normal checksum read/write operation"
6998
6999 test_77b() { # bug 10889
7000         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7001         $GSS && skip_env "could not run with gss"
7002
7003         [ ! -f $F77_TMP ] && setup_f77
7004         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7005         $LCTL set_param fail_loc=0x80000409
7006         set_checksums 1
7007
7008         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7009                 error "dd error: $?"
7010         $LCTL set_param fail_loc=0
7011
7012         for algo in $CKSUM_TYPES; do
7013                 cancel_lru_locks osc
7014                 set_checksum_type $algo
7015                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7016                 $LCTL set_param fail_loc=0x80000408
7017                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7018                 $LCTL set_param fail_loc=0
7019         done
7020         set_checksums 0
7021         set_checksum_type $ORIG_CSUM_TYPE
7022         rm -f $DIR/$tfile
7023 }
7024 run_test 77b "checksum error on client write, read"
7025
7026 cleanup_77c() {
7027         trap 0
7028         set_checksums 0
7029         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7030         $check_ost &&
7031                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7032         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7033         $check_ost && [ -n "$ost_file_prefix" ] &&
7034                 do_facet ost1 rm -f ${ost_file_prefix}\*
7035 }
7036
7037 test_77c() {
7038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7039         $GSS && skip_env "could not run with gss"
7040         remote_ost_nodsh && skip "remote OST with nodsh"
7041
7042         local bad1
7043         local osc_file_prefix
7044         local osc_file
7045         local check_ost=false
7046         local ost_file_prefix
7047         local ost_file
7048         local orig_cksum
7049         local dump_cksum
7050         local fid
7051
7052         # ensure corruption will occur on first OSS/OST
7053         $LFS setstripe -i 0 $DIR/$tfile
7054
7055         [ ! -f $F77_TMP ] && setup_f77
7056         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7057                 error "dd write error: $?"
7058         fid=$($LFS path2fid $DIR/$tfile)
7059
7060         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
7061         then
7062                 check_ost=true
7063                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7064                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7065         else
7066                 echo "OSS do not support bulk pages dump upon error"
7067         fi
7068
7069         osc_file_prefix=$($LCTL get_param -n debug_path)
7070         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7071
7072         trap cleanup_77c EXIT
7073
7074         set_checksums 1
7075         # enable bulk pages dump upon error on Client
7076         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7077         # enable bulk pages dump upon error on OSS
7078         $check_ost &&
7079                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7080
7081         # flush Client cache to allow next read to reach OSS
7082         cancel_lru_locks osc
7083
7084         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7085         $LCTL set_param fail_loc=0x80000408
7086         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7087         $LCTL set_param fail_loc=0
7088
7089         rm -f $DIR/$tfile
7090
7091         # check cksum dump on Client
7092         osc_file=$(ls ${osc_file_prefix}*)
7093         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7094         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7095         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7096         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7097         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7098                      cksum)
7099         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7100         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7101                 error "dump content does not match on Client"
7102
7103         $check_ost || skip "No need to check cksum dump on OSS"
7104
7105         # check cksum dump on OSS
7106         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7107         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7108         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7109         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7110         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7111                 error "dump content does not match on OSS"
7112
7113         cleanup_77c
7114 }
7115 run_test 77c "checksum error on client read with debug"
7116
7117 test_77d() { # bug 10889
7118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7119         $GSS && skip_env "could not run with gss"
7120
7121         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7122         $LCTL set_param fail_loc=0x80000409
7123         set_checksums 1
7124         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7125                 error "direct write: rc=$?"
7126         $LCTL set_param fail_loc=0
7127         set_checksums 0
7128
7129         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7130         $LCTL set_param fail_loc=0x80000408
7131         set_checksums 1
7132         cancel_lru_locks osc
7133         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7134                 error "direct read: rc=$?"
7135         $LCTL set_param fail_loc=0
7136         set_checksums 0
7137 }
7138 run_test 77d "checksum error on OST direct write, read"
7139
7140 test_77f() { # bug 10889
7141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7142         $GSS && skip_env "could not run with gss"
7143
7144         set_checksums 1
7145         for algo in $CKSUM_TYPES; do
7146                 cancel_lru_locks osc
7147                 set_checksum_type $algo
7148                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7149                 $LCTL set_param fail_loc=0x409
7150                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
7151                         error "direct write succeeded"
7152                 $LCTL set_param fail_loc=0
7153         done
7154         set_checksum_type $ORIG_CSUM_TYPE
7155         set_checksums 0
7156 }
7157 run_test 77f "repeat checksum error on write (expect error)"
7158
7159 test_77g() { # bug 10889
7160         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7161         $GSS && skip_env "could not run with gss"
7162         remote_ost_nodsh && skip "remote OST with nodsh"
7163
7164         [ ! -f $F77_TMP ] && setup_f77
7165
7166         local file=$DIR/$tfile
7167         stack_trap "rm -f $file" EXIT
7168
7169         $SETSTRIPE -c 1 -i 0 $file
7170         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
7171         do_facet ost1 lctl set_param fail_loc=0x8000021a
7172         set_checksums 1
7173         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
7174                 error "write error: rc=$?"
7175         do_facet ost1 lctl set_param fail_loc=0
7176         set_checksums 0
7177
7178         cancel_lru_locks osc
7179         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
7180         do_facet ost1 lctl set_param fail_loc=0x8000021b
7181         set_checksums 1
7182         cmp $F77_TMP $file || error "file compare failed"
7183         do_facet ost1 lctl set_param fail_loc=0
7184         set_checksums 0
7185 }
7186 run_test 77g "checksum error on OST write, read"
7187
7188 test_77k() { # LU-10906
7189         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7190         $GSS && skip_env "could not run with gss"
7191
7192         local cksum_param="osc.$FSNAME*.checksums"
7193         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
7194         local checksum
7195         local i
7196
7197         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
7198         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
7199         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
7200                 EXIT
7201
7202         for i in 0 1; do
7203                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
7204                         error "failed to set checksum=$i on MGS"
7205                 wait_update $HOSTNAME "$get_checksum" $i
7206                 #remount
7207                 echo "remount client, checksum should be $i"
7208                 remount_client $MOUNT || "failed to remount client"
7209                 checksum=$(eval $get_checksum)
7210                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7211         done
7212
7213         for opt in "checksum" "nochecksum"; do
7214                 #remount with mount option
7215                 echo "remount client with option $opt, checksum should be $i"
7216                 umount_client $MOUNT || "failed to umount client"
7217                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
7218                         "failed to mount client with option '$opt'"
7219                 checksum=$(eval $get_checksum)
7220                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7221                 i=$((i - 1))
7222         done
7223
7224         remount_client $MOUNT || "failed to remount client"
7225 }
7226 run_test 77k "enable/disable checksum correctly"
7227
7228 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
7229 rm -f $F77_TMP
7230 unset F77_TMP
7231
7232 cleanup_test_78() {
7233         trap 0
7234         rm -f $DIR/$tfile
7235 }
7236
7237 test_78() { # bug 10901
7238         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7239         remote_ost || skip_env "local OST"
7240
7241         NSEQ=5
7242         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
7243         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
7244         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
7245         echo "MemTotal: $MEMTOTAL"
7246
7247         # reserve 256MB of memory for the kernel and other running processes,
7248         # and then take 1/2 of the remaining memory for the read/write buffers.
7249         if [ $MEMTOTAL -gt 512 ] ;then
7250                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
7251         else
7252                 # for those poor memory-starved high-end clusters...
7253                 MEMTOTAL=$((MEMTOTAL / 2))
7254         fi
7255         echo "Mem to use for directio: $MEMTOTAL"
7256
7257         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
7258         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
7259         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
7260         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
7261                 head -n1)
7262         echo "Smallest OST: $SMALLESTOST"
7263         [[ $SMALLESTOST -lt 10240 ]] &&
7264                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
7265
7266         trap cleanup_test_78 EXIT
7267
7268         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
7269                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
7270
7271         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
7272         echo "File size: $F78SIZE"
7273         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
7274         for i in $(seq 1 $NSEQ); do
7275                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
7276                 echo directIO rdwr round $i of $NSEQ
7277                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
7278         done
7279
7280         cleanup_test_78
7281 }
7282 run_test 78 "handle large O_DIRECT writes correctly ============"
7283
7284 test_79() { # bug 12743
7285         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7286
7287         wait_delete_completed
7288
7289         BKTOTAL=$(calc_osc_kbytes kbytestotal)
7290         BKFREE=$(calc_osc_kbytes kbytesfree)
7291         BKAVAIL=$(calc_osc_kbytes kbytesavail)
7292
7293         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
7294         DFTOTAL=`echo $STRING | cut -d, -f1`
7295         DFUSED=`echo $STRING  | cut -d, -f2`
7296         DFAVAIL=`echo $STRING | cut -d, -f3`
7297         DFFREE=$(($DFTOTAL - $DFUSED))
7298
7299         ALLOWANCE=$((64 * $OSTCOUNT))
7300
7301         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
7302            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
7303                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
7304         fi
7305         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
7306            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
7307                 error "df free($DFFREE) mismatch OST free($BKFREE)"
7308         fi
7309         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
7310            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
7311                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
7312         fi
7313 }
7314 run_test 79 "df report consistency check ======================="
7315
7316 test_80() { # bug 10718
7317         remote_ost_nodsh && skip "remote OST with nodsh"
7318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7319
7320         # relax strong synchronous semantics for slow backends like ZFS
7321         local soc="obdfilter.*.sync_on_lock_cancel"
7322         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
7323         local hosts=
7324         if [ "$soc_old" != "never" ] &&
7325                 [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
7326                         hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
7327                                 facet_active_host $host; done | sort -u)
7328                         do_nodes $hosts lctl set_param $soc=never
7329         fi
7330
7331         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
7332         sync; sleep 1; sync
7333         local BEFORE=`date +%s`
7334         cancel_lru_locks osc
7335         local AFTER=`date +%s`
7336         local DIFF=$((AFTER-BEFORE))
7337         if [ $DIFF -gt 1 ] ; then
7338                 error "elapsed for 1M@1T = $DIFF"
7339         fi
7340
7341         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
7342
7343         rm -f $DIR/$tfile
7344 }
7345 run_test 80 "Page eviction is equally fast at high offsets too  ===="
7346
7347 test_81a() { # LU-456
7348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7349         remote_ost_nodsh && skip "remote OST with nodsh"
7350
7351         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7352         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
7353         do_facet ost1 lctl set_param fail_loc=0x80000228
7354
7355         # write should trigger a retry and success
7356         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
7357         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7358         RC=$?
7359         if [ $RC -ne 0 ] ; then
7360                 error "write should success, but failed for $RC"
7361         fi
7362 }
7363 run_test 81a "OST should retry write when get -ENOSPC ==============="
7364
7365 test_81b() { # LU-456
7366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7367         remote_ost_nodsh && skip "remote OST with nodsh"
7368
7369         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
7370         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
7371         do_facet ost1 lctl set_param fail_loc=0x228
7372
7373         # write should retry several times and return -ENOSPC finally
7374         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
7375         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
7376         RC=$?
7377         ENOSPC=28
7378         if [ $RC -ne $ENOSPC ] ; then
7379                 error "dd should fail for -ENOSPC, but succeed."
7380         fi
7381 }
7382 run_test 81b "OST should return -ENOSPC when retry still fails ======="
7383
7384 test_82() { # LU-1031
7385         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
7386         local gid1=14091995
7387         local gid2=16022000
7388
7389         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
7390         local MULTIPID1=$!
7391         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
7392         local MULTIPID2=$!
7393         kill -USR1 $MULTIPID2
7394         sleep 2
7395         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
7396                 error "First grouplock does not block second one"
7397         else
7398                 echo "Second grouplock blocks first one"
7399         fi
7400         kill -USR1 $MULTIPID1
7401         wait $MULTIPID1
7402         wait $MULTIPID2
7403 }
7404 run_test 82 "Basic grouplock test"
7405
7406 test_83() {
7407         local sfile="/boot/System.map-$(uname -r)"
7408         [ ! -f $sfile ] && skip "No $sfile found"
7409         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
7410         $LCTL set_param fail_loc=0x140d
7411         cp $sfile $DIR/$tfile || error "write failed"
7412         diff -c $sfile $DIR/$tfile || error "files are different"
7413         $LCTL set_param fail_loc=0
7414         rm -f $DIR/$tfile
7415 }
7416 run_test 83 "Short write in ptask ==============================="
7417
7418 test_99() {
7419         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
7420
7421         test_mkdir $DIR/$tdir.cvsroot
7422         chown $RUNAS_ID $DIR/$tdir.cvsroot
7423
7424         cd $TMP
7425         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
7426
7427         cd /etc/init.d
7428         # some versions of cvs import exit(1) when asked to import links or
7429         # files they can't read.  ignore those files.
7430         local toignore=$(find . -type l -printf '-I %f\n' -o \
7431                          ! -perm /4 -printf '-I %f\n')
7432         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
7433                 $tdir.reposname vtag rtag
7434
7435         cd $DIR
7436         test_mkdir $DIR/$tdir.reposname
7437         chown $RUNAS_ID $DIR/$tdir.reposname
7438         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
7439
7440         cd $DIR/$tdir.reposname
7441         $RUNAS touch foo99
7442         $RUNAS cvs add -m 'addmsg' foo99
7443         $RUNAS cvs update
7444         $RUNAS cvs commit -m 'nomsg' foo99
7445         rm -fr $DIR/$tdir.cvsroot
7446 }
7447 run_test 99 "cvs strange file/directory operations"
7448
7449 test_100() {
7450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7451         [[ "$NETTYPE" =~ tcp ]] ||
7452                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
7453         remote_ost_nodsh && skip "remote OST with nodsh"
7454         remote_mds_nodsh && skip "remote MDS with nodsh"
7455         remote_servers ||
7456                 skip "useless for local single node setup"
7457
7458         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
7459                 [ "$PROT" != "tcp" ] && continue
7460                 RPORT=$(echo $REMOTE | cut -d: -f2)
7461                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
7462
7463                 rc=0
7464                 LPORT=`echo $LOCAL | cut -d: -f2`
7465                 if [ $LPORT -ge 1024 ]; then
7466                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
7467                         netstat -tna
7468                         error_exit "local: $LPORT > 1024, remote: $RPORT"
7469                 fi
7470         done
7471         [ "$rc" = 0 ] || error_exit "privileged port not found" )
7472 }
7473 run_test 100 "check local port using privileged port ==========="
7474
7475 function get_named_value()
7476 {
7477     local tag
7478
7479     tag=$1
7480     while read ;do
7481         line=$REPLY
7482         case $line in
7483         $tag*)
7484             echo $line | sed "s/^$tag[ ]*//"
7485             break
7486             ;;
7487         esac
7488     done
7489 }
7490
7491 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
7492                    awk '/^max_cached_mb/ { print $2 }')
7493
7494 cleanup_101a() {
7495         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
7496         trap 0
7497 }
7498
7499 test_101a() {
7500         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7501         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
7502
7503         local s
7504         local discard
7505         local nreads=10000
7506         local cache_limit=32
7507
7508         $LCTL set_param -n osc.*-osc*.rpc_stats 0
7509         trap cleanup_101a EXIT
7510         $LCTL set_param -n llite.*.read_ahead_stats 0
7511         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
7512
7513         #
7514         # randomly read 10000 of 64K chunks from file 3x 32MB in size
7515         #
7516         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
7517         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
7518
7519         discard=0
7520         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
7521                 get_named_value 'read but discarded' | cut -d" " -f1); do
7522                         discard=$(($discard + $s))
7523         done
7524         cleanup_101a
7525
7526         if [[ $(($discard * 10)) -gt $nreads ]]; then
7527                 $LCTL get_param osc.*-osc*.rpc_stats
7528                 $LCTL get_param llite.*.read_ahead_stats
7529                 error "too many ($discard) discarded pages"
7530         fi
7531         rm -f $DIR/$tfile || true
7532 }
7533 run_test 101a "check read-ahead for random reads"
7534
7535 setup_test101bc() {
7536         test_mkdir $DIR/$tdir
7537         local STRIPE_SIZE=$1
7538         local FILE_LENGTH=$2
7539         STRIPE_OFFSET=0
7540
7541         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
7542
7543         local list=$(comma_list $(osts_nodes))
7544         set_osd_param $list '' read_cache_enable 0
7545         set_osd_param $list '' writethrough_cache_enable 0
7546
7547         trap cleanup_test101bc EXIT
7548         # prepare the read-ahead file
7549         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
7550
7551         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
7552                                 count=$FILE_SIZE_MB 2> /dev/null
7553
7554 }
7555
7556 cleanup_test101bc() {
7557         trap 0
7558         rm -rf $DIR/$tdir
7559         rm -f $DIR/$tfile
7560
7561         local list=$(comma_list $(osts_nodes))
7562         set_osd_param $list '' read_cache_enable 1
7563         set_osd_param $list '' writethrough_cache_enable 1
7564 }
7565
7566 calc_total() {
7567         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
7568 }
7569
7570 ra_check_101() {
7571         local READ_SIZE=$1
7572         local STRIPE_SIZE=$2
7573         local FILE_LENGTH=$3
7574         local RA_INC=1048576
7575         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
7576         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
7577                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
7578         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
7579                         get_named_value 'read but discarded' |
7580                         cut -d" " -f1 | calc_total)
7581         if [[ $DISCARD -gt $discard_limit ]]; then
7582                 $LCTL get_param llite.*.read_ahead_stats
7583                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
7584         else
7585                 echo "Read-ahead success for size ${READ_SIZE}"
7586         fi
7587 }
7588
7589 test_101b() {
7590         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7591         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7592
7593         local STRIPE_SIZE=1048576
7594         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
7595
7596         if [ $SLOW == "yes" ]; then
7597                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
7598         else
7599                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
7600         fi
7601
7602         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
7603
7604         # prepare the read-ahead file
7605         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7606         cancel_lru_locks osc
7607         for BIDX in 2 4 8 16 32 64 128 256
7608         do
7609                 local BSIZE=$((BIDX*4096))
7610                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
7611                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
7612                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
7613                 $LCTL set_param -n llite.*.read_ahead_stats 0
7614                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
7615                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
7616                 cancel_lru_locks osc
7617                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
7618         done
7619         cleanup_test101bc
7620         true
7621 }
7622 run_test 101b "check stride-io mode read-ahead ================="
7623
7624 test_101c() {
7625         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7626
7627         local STRIPE_SIZE=1048576
7628         local FILE_LENGTH=$((STRIPE_SIZE*100))
7629         local nreads=10000
7630         local osc_rpc_stats
7631
7632         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7633
7634         cancel_lru_locks osc
7635         $LCTL set_param osc.*.rpc_stats 0
7636         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
7637         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
7638                 local stats=$($LCTL get_param -n $osc_rpc_stats)
7639                 local lines=$(echo "$stats" | awk 'END {print NR;}')
7640                 local size
7641
7642                 if [ $lines -le 20 ]; then
7643                         continue
7644                 fi
7645                 for size in 1 2 4 8; do
7646                         local rpc=$(echo "$stats" |
7647                                     awk '($1 == "'$size':") {print $2; exit; }')
7648                         [ $rpc != 0 ] &&
7649                                 error "Small $((size*4))k read IO $rpc !"
7650                 done
7651                 echo "$osc_rpc_stats check passed!"
7652         done
7653         cleanup_test101bc
7654         true
7655 }
7656 run_test 101c "check stripe_size aligned read-ahead ================="
7657
7658 set_read_ahead() {
7659         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
7660         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
7661 }
7662
7663 test_101d() {
7664         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7665
7666         local file=$DIR/$tfile
7667         local sz_MB=${FILESIZE_101d:-500}
7668         local ra_MB=${READAHEAD_MB:-40}
7669
7670         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
7671         [ $free_MB -lt $sz_MB ] &&
7672                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
7673
7674         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
7675         $SETSTRIPE -c -1 $file || error "setstripe failed"
7676
7677         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
7678         echo Cancel LRU locks on lustre client to flush the client cache
7679         cancel_lru_locks osc
7680
7681         echo Disable read-ahead
7682         local old_READAHEAD=$(set_read_ahead 0)
7683
7684         echo Reading the test file $file with read-ahead disabled
7685         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7686
7687         echo Cancel LRU locks on lustre client to flush the client cache
7688         cancel_lru_locks osc
7689         echo Enable read-ahead with ${ra_MB}MB
7690         set_read_ahead $ra_MB
7691
7692         echo Reading the test file $file with read-ahead enabled
7693         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7694
7695         echo "read-ahead disabled time read $raOFF"
7696         echo "read-ahead enabled  time read $raON"
7697
7698         set_read_ahead $old_READAHEAD
7699         rm -f $file
7700         wait_delete_completed
7701
7702         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
7703                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
7704 }
7705 run_test 101d "file read with and without read-ahead enabled"
7706
7707 test_101e() {
7708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7709
7710         local file=$DIR/$tfile
7711         local size_KB=500  #KB
7712         local count=100
7713         local bsize=1024
7714
7715         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
7716         local need_KB=$((count * size_KB))
7717         [[ $free_KB -le $need_KB ]] &&
7718                 skip_env "Need free space $need_KB, have $free_KB"
7719
7720         echo "Creating $count ${size_KB}K test files"
7721         for ((i = 0; i < $count; i++)); do
7722                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
7723         done
7724
7725         echo "Cancel LRU locks on lustre client to flush the client cache"
7726         cancel_lru_locks $OSC
7727
7728         echo "Reset readahead stats"
7729         $LCTL set_param -n llite.*.read_ahead_stats 0
7730
7731         for ((i = 0; i < $count; i++)); do
7732                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
7733         done
7734
7735         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7736                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
7737
7738         for ((i = 0; i < $count; i++)); do
7739                 rm -rf $file.$i 2>/dev/null
7740         done
7741
7742         #10000 means 20% reads are missing in readahead
7743         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
7744 }
7745 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
7746
7747 test_101f() {
7748         which iozone || skip_env "no iozone installed"
7749
7750         local old_debug=$($LCTL get_param debug)
7751         old_debug=${old_debug#*=}
7752         $LCTL set_param debug="reada mmap"
7753
7754         # create a test file
7755         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
7756
7757         echo Cancel LRU locks on lustre client to flush the client cache
7758         cancel_lru_locks osc
7759
7760         echo Reset readahead stats
7761         $LCTL set_param -n llite.*.read_ahead_stats 0
7762
7763         echo mmap read the file with small block size
7764         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
7765                 > /dev/null 2>&1
7766
7767         echo checking missing pages
7768         $LCTL get_param llite.*.read_ahead_stats
7769         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7770                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7771
7772         $LCTL set_param debug="$old_debug"
7773         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7774         rm -f $DIR/$tfile
7775 }
7776 run_test 101f "check mmap read performance"
7777
7778 test_101g_brw_size_test() {
7779         local mb=$1
7780         local pages=$((mb * 1048576 / $(page_size)))
7781         local file=$DIR/$tfile
7782
7783         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7784                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7785         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7786                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7787                         return 2
7788         done
7789
7790         stack_trap "rm -f $file" EXIT
7791         $LCTL set_param -n osc.*.rpc_stats=0
7792
7793         # 10 RPCs should be enough for the test
7794         local count=10
7795         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
7796                 { error "dd write ${mb} MB blocks failed"; return 3; }
7797         cancel_lru_locks osc
7798         dd of=/dev/null if=$file bs=${mb}M count=$count ||
7799                 { error "dd write ${mb} MB blocks failed"; return 4; }
7800
7801         # calculate number of full-sized read and write RPCs
7802         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7803                 sed -n '/pages per rpc/,/^$/p' |
7804                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7805                 END { print reads,writes }'))
7806         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7807                 return 5
7808         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7809                 return 6
7810
7811         return 0
7812 }
7813
7814 test_101g() {
7815         remote_ost_nodsh && skip "remote OST with nodsh"
7816
7817         local rpcs
7818         local osts=$(get_facets OST)
7819         local list=$(comma_list $(osts_nodes))
7820         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7821         local brw_size="obdfilter.*.brw_size"
7822         local ostver=$(lustre_version_code ost1)
7823         local cliver=$(lustre_version_code client)
7824
7825         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7826
7827         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7828         if [ $ostver -ge $(version_code 2.8.52) -o \
7829              \( $ostver -ge $(version_code 2.7.17) -a \
7830                 $ostver -lt $(version_code 2.7.50) \) ] &&
7831            [ $cliver -ge $(version_code 2.8.52) -o \
7832              \( $cliver -ge $(version_code 2.7.17) -a \
7833                 $cliver -lt $(version_code 2.7.50) \) ]; then
7834                 [ $ostver -ge $(version_code 2.9.52) ] && suffix="M"
7835                 if [[ $orig_mb -lt 16 ]]; then
7836                         save_lustre_params $osts "$brw_size" > $p
7837                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7838                                 error "set 16MB RPC size failed"
7839
7840                         echo "remount client to enable new RPC size"
7841                         remount_client $MOUNT || error "remount_client failed"
7842                 fi
7843
7844                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7845                 # should be able to set brw_size=12, but no rpc_stats for that
7846                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7847         fi
7848
7849         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7850
7851         if [[ $orig_mb -lt 16 ]]; then
7852                 restore_lustre_params < $p
7853                 remount_client $MOUNT || error "remount_client restore failed"
7854         fi
7855
7856         rm -f $p $DIR/$tfile
7857 }
7858 run_test 101g "Big bulk(4/16 MiB) readahead"
7859
7860 setup_test102() {
7861         test_mkdir $DIR/$tdir
7862         chown $RUNAS_ID $DIR/$tdir
7863         STRIPE_SIZE=65536
7864         STRIPE_OFFSET=1
7865         STRIPE_COUNT=$OSTCOUNT
7866         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7867
7868         trap cleanup_test102 EXIT
7869         cd $DIR
7870         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7871         cd $DIR/$tdir
7872         for num in 1 2 3 4; do
7873                 for count in $(seq 1 $STRIPE_COUNT); do
7874                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7875                                 local size=`expr $STRIPE_SIZE \* $num`
7876                                 local file=file"$num-$idx-$count"
7877                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7878                         done
7879                 done
7880         done
7881
7882         cd $DIR
7883         $1 tar cf $TMP/f102.tar $tdir --xattrs
7884 }
7885
7886 cleanup_test102() {
7887         trap 0
7888         rm -f $TMP/f102.tar
7889         rm -rf $DIR/d0.sanity/d102
7890 }
7891
7892 test_102a() {
7893         [ "$UID" != 0 ] && skip "must run as root"
7894         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7895                 skip_env "must have user_xattr"
7896
7897         [ -z "$(which setfattr 2>/dev/null)" ] &&
7898                 skip_env "could not find setfattr"
7899
7900         local testfile=$DIR/$tfile
7901
7902         touch $testfile
7903         echo "set/get xattr..."
7904         setfattr -n trusted.name1 -v value1 $testfile ||
7905                 error "setfattr -n trusted.name1=value1 $testfile failed"
7906         getfattr -n trusted.name1 $testfile 2> /dev/null |
7907           grep "trusted.name1=.value1" ||
7908                 error "$testfile missing trusted.name1=value1"
7909
7910         setfattr -n user.author1 -v author1 $testfile ||
7911                 error "setfattr -n user.author1=author1 $testfile failed"
7912         getfattr -n user.author1 $testfile 2> /dev/null |
7913           grep "user.author1=.author1" ||
7914                 error "$testfile missing trusted.author1=author1"
7915
7916         echo "listxattr..."
7917         setfattr -n trusted.name2 -v value2 $testfile ||
7918                 error "$testfile unable to set trusted.name2"
7919         setfattr -n trusted.name3 -v value3 $testfile ||
7920                 error "$testfile unable to set trusted.name3"
7921         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7922             grep "trusted.name" | wc -l) -eq 3 ] ||
7923                 error "$testfile missing 3 trusted.name xattrs"
7924
7925         setfattr -n user.author2 -v author2 $testfile ||
7926                 error "$testfile unable to set user.author2"
7927         setfattr -n user.author3 -v author3 $testfile ||
7928                 error "$testfile unable to set user.author3"
7929         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7930             grep "user.author" | wc -l) -eq 3 ] ||
7931                 error "$testfile missing 3 user.author xattrs"
7932
7933         echo "remove xattr..."
7934         setfattr -x trusted.name1 $testfile ||
7935                 error "$testfile error deleting trusted.name1"
7936         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7937                 error "$testfile did not delete trusted.name1 xattr"
7938
7939         setfattr -x user.author1 $testfile ||
7940                 error "$testfile error deleting user.author1"
7941         echo "set lustre special xattr ..."
7942         $LFS setstripe -c1 $testfile
7943         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7944                 awk -F "=" '/trusted.lov/ { print $2 }' )
7945         setfattr -n "trusted.lov" -v $lovea $testfile ||
7946                 error "$testfile doesn't ignore setting trusted.lov again"
7947         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7948                 error "$testfile allow setting invalid trusted.lov"
7949         rm -f $testfile
7950 }
7951 run_test 102a "user xattr test =================================="
7952
7953 test_102b() {
7954         [ -z "$(which setfattr 2>/dev/null)" ] &&
7955                 skip_env "could not find setfattr"
7956         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7957
7958         # b10930: get/set/list trusted.lov xattr
7959         echo "get/set/list trusted.lov xattr ..."
7960         local testfile=$DIR/$tfile
7961         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7962                 error "setstripe failed"
7963         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7964                 error "getstripe failed"
7965         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7966                 error "can't get trusted.lov from $testfile"
7967
7968         local testfile2=${testfile}2
7969         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7970                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7971
7972         $MCREATE $testfile2
7973         setfattr -n trusted.lov -v $value $testfile2
7974         local stripe_size=$($GETSTRIPE -S $testfile2)
7975         local stripe_count=$($GETSTRIPE -c $testfile2)
7976         [[ $stripe_size -eq 65536 ]] ||
7977                 error "stripe size $stripe_size != 65536"
7978         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7979                 error "stripe count $stripe_count != $STRIPECOUNT"
7980         rm -f $DIR/$tfile
7981 }
7982 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7983
7984 test_102c() {
7985         [ -z "$(which setfattr 2>/dev/null)" ] &&
7986                 skip_env "could not find setfattr"
7987         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7988
7989         # b10930: get/set/list lustre.lov xattr
7990         echo "get/set/list lustre.lov xattr ..."
7991         test_mkdir $DIR/$tdir
7992         chown $RUNAS_ID $DIR/$tdir
7993         local testfile=$DIR/$tdir/$tfile
7994         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7995                 error "setstripe failed"
7996         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7997                 error "getstripe failed"
7998         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7999         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8000
8001         local testfile2=${testfile}2
8002         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8003                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8004
8005         $RUNAS $MCREATE $testfile2
8006         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8007         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
8008         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
8009         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8010         [ $stripe_count -eq $STRIPECOUNT ] ||
8011                 error "stripe count $stripe_count != $STRIPECOUNT"
8012 }
8013 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8014
8015 compare_stripe_info1() {
8016         local stripe_index_all_zero=true
8017
8018         for num in 1 2 3 4; do
8019                 for count in $(seq 1 $STRIPE_COUNT); do
8020                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8021                                 local size=$((STRIPE_SIZE * num))
8022                                 local file=file"$num-$offset-$count"
8023                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8024                                 [[ $stripe_size -ne $size ]] &&
8025                                     error "$file: size $stripe_size != $size"
8026                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8027                                 # allow fewer stripes to be created, ORI-601
8028                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8029                                     error "$file: count $stripe_count != $count"
8030                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8031                                 [[ $stripe_index -ne 0 ]] &&
8032                                         stripe_index_all_zero=false
8033                         done
8034                 done
8035         done
8036         $stripe_index_all_zero &&
8037                 error "all files are being extracted starting from OST index 0"
8038         return 0
8039 }
8040
8041 have_xattrs_include() {
8042         tar --help | grep -q xattrs-include &&
8043                 echo --xattrs-include="lustre.*"
8044 }
8045
8046 test_102d() {
8047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8048         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8049
8050         XINC=$(have_xattrs_include)
8051         setup_test102
8052         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8053         cd $DIR/$tdir/$tdir
8054         compare_stripe_info1
8055 }
8056 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8057
8058 test_102f() {
8059         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8060         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8061
8062         XINC=$(have_xattrs_include)
8063         setup_test102
8064         test_mkdir $DIR/$tdir.restore
8065         cd $DIR
8066         tar cf - --xattrs $tdir | tar xf - \
8067                 -C $DIR/$tdir.restore --xattrs $XINC
8068         cd $DIR/$tdir.restore/$tdir
8069         compare_stripe_info1
8070 }
8071 run_test 102f "tar copy files, not keep osts"
8072
8073 grow_xattr() {
8074         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8075                 skip "must have user_xattr"
8076         [ -z "$(which setfattr 2>/dev/null)" ] &&
8077                 skip_env "could not find setfattr"
8078         [ -z "$(which getfattr 2>/dev/null)" ] &&
8079                 skip_env "could not find getfattr"
8080
8081         local xsize=${1:-1024}  # in bytes
8082         local file=$DIR/$tfile
8083         local value="$(generate_string $xsize)"
8084         local xbig=trusted.big
8085
8086         touch $file
8087         log "save $xbig on $file"
8088         setfattr -n $xbig -v $value $file ||
8089                 error "saving $xbig on $file failed"
8090
8091         local orig=$(get_xattr_value $xbig $file)
8092         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8093
8094         local xsml=trusted.sml
8095         log "save $xsml on $file"
8096         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8097
8098         local new=$(get_xattr_value $xbig $file)
8099         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8100
8101         log "grow $xsml on $file"
8102         setfattr -n $xsml -v "$value" $file ||
8103                 error "growing $xsml on $file failed"
8104
8105         new=$(get_xattr_value $xbig $file)
8106         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8107         log "$xbig still valid after growing $xsml"
8108
8109         rm -f $file
8110 }
8111
8112 test_102h() { # bug 15777
8113         grow_xattr 1024
8114 }
8115 run_test 102h "grow xattr from inside inode to external block"
8116
8117 test_102ha() {
8118         large_xattr_enabled || skip_env "large_xattr disabled"
8119
8120         grow_xattr $(max_xattr_size)
8121 }
8122 run_test 102ha "grow xattr from inside inode to external inode"
8123
8124 test_102i() { # bug 17038
8125         [ -z "$(which getfattr 2>/dev/null)" ] &&
8126                 skip "could not find getfattr"
8127
8128         touch $DIR/$tfile
8129         ln -s $DIR/$tfile $DIR/${tfile}link
8130         getfattr -n trusted.lov $DIR/$tfile ||
8131                 error "lgetxattr on $DIR/$tfile failed"
8132         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
8133                 grep -i "no such attr" ||
8134                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
8135         rm -f $DIR/$tfile $DIR/${tfile}link
8136 }
8137 run_test 102i "lgetxattr test on symbolic link ============"
8138
8139 test_102j() {
8140         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8141         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8142
8143         XINC=$(have_xattrs_include)
8144         setup_test102 "$RUNAS"
8145         chown $RUNAS_ID $DIR/$tdir
8146         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8147         cd $DIR/$tdir/$tdir
8148         compare_stripe_info1 "$RUNAS"
8149 }
8150 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
8151
8152 test_102k() {
8153         [ -z "$(which setfattr 2>/dev/null)" ] &&
8154                 skip "could not find setfattr"
8155
8156         touch $DIR/$tfile
8157         # b22187 just check that does not crash for regular file.
8158         setfattr -n trusted.lov $DIR/$tfile
8159         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
8160         local test_kdir=$DIR/$tdir
8161         test_mkdir $test_kdir
8162         local default_size=$($LFS getstripe -S $test_kdir)
8163         local default_count=$($LFS getstripe -c $test_kdir)
8164         local default_offset=$($LFS getstripe -i $test_kdir)
8165         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
8166                 error 'dir setstripe failed'
8167         setfattr -n trusted.lov $test_kdir
8168         local stripe_size=$($LFS getstripe -S $test_kdir)
8169         local stripe_count=$($LFS getstripe -c $test_kdir)
8170         local stripe_offset=$($LFS getstripe -i $test_kdir)
8171         [ $stripe_size -eq $default_size ] ||
8172                 error "stripe size $stripe_size != $default_size"
8173         [ $stripe_count -eq $default_count ] ||
8174                 error "stripe count $stripe_count != $default_count"
8175         [ $stripe_offset -eq $default_offset ] ||
8176                 error "stripe offset $stripe_offset != $default_offset"
8177         rm -rf $DIR/$tfile $test_kdir
8178 }
8179 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
8180
8181 test_102l() {
8182         [ -z "$(which getfattr 2>/dev/null)" ] &&
8183                 skip "could not find getfattr"
8184
8185         # LU-532 trusted. xattr is invisible to non-root
8186         local testfile=$DIR/$tfile
8187
8188         touch $testfile
8189
8190         echo "listxattr as user..."
8191         chown $RUNAS_ID $testfile
8192         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
8193             grep -q "trusted" &&
8194                 error "$testfile trusted xattrs are user visible"
8195
8196         return 0;
8197 }
8198 run_test 102l "listxattr size test =================================="
8199
8200 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
8201         local path=$DIR/$tfile
8202         touch $path
8203
8204         listxattr_size_check $path || error "listattr_size_check $path failed"
8205 }
8206 run_test 102m "Ensure listxattr fails on small bufffer ========"
8207
8208 cleanup_test102
8209
8210 getxattr() { # getxattr path name
8211         # Return the base64 encoding of the value of xattr name on path.
8212         local path=$1
8213         local name=$2
8214
8215         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
8216         # file: $path
8217         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8218         #
8219         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8220
8221         getfattr --absolute-names --encoding=base64 --name=$name $path |
8222                 awk -F= -v name=$name '$1 == name {
8223                         print substr($0, index($0, "=") + 1);
8224         }'
8225 }
8226
8227 test_102n() { # LU-4101 mdt: protect internal xattrs
8228         [ -z "$(which setfattr 2>/dev/null)" ] &&
8229                 skip "could not find setfattr"
8230         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
8231         then
8232                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
8233         fi
8234
8235         local file0=$DIR/$tfile.0
8236         local file1=$DIR/$tfile.1
8237         local xattr0=$TMP/$tfile.0
8238         local xattr1=$TMP/$tfile.1
8239         local namelist="lov lma lmv link fid version som hsm"
8240         local name
8241         local value
8242
8243         rm -rf $file0 $file1 $xattr0 $xattr1
8244         touch $file0 $file1
8245
8246         # Get 'before' xattrs of $file1.
8247         getfattr --absolute-names --dump --match=- $file1 > $xattr0
8248
8249         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
8250                 namelist+=" lfsck_namespace"
8251         for name in $namelist; do
8252                 # Try to copy xattr from $file0 to $file1.
8253                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8254
8255                 setfattr --name=trusted.$name --value="$value" $file1 ||
8256                         error "setxattr 'trusted.$name' failed"
8257
8258                 # Try to set a garbage xattr.
8259                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8260
8261                 if [[ x$name == "xlov" ]]; then
8262                         setfattr --name=trusted.lov --value="$value" $file1 &&
8263                         error "setxattr invalid 'trusted.lov' success"
8264                 else
8265                         setfattr --name=trusted.$name --value="$value" $file1 ||
8266                                 error "setxattr invalid 'trusted.$name' failed"
8267                 fi
8268
8269                 # Try to remove the xattr from $file1. We don't care if this
8270                 # appears to succeed or fail, we just don't want there to be
8271                 # any changes or crashes.
8272                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8273         done
8274
8275         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
8276         then
8277                 name="lfsck_ns"
8278                 # Try to copy xattr from $file0 to $file1.
8279                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8280
8281                 setfattr --name=trusted.$name --value="$value" $file1 ||
8282                         error "setxattr 'trusted.$name' failed"
8283
8284                 # Try to set a garbage xattr.
8285                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8286
8287                 setfattr --name=trusted.$name --value="$value" $file1 ||
8288                         error "setxattr 'trusted.$name' failed"
8289
8290                 # Try to remove the xattr from $file1. We don't care if this
8291                 # appears to succeed or fail, we just don't want there to be
8292                 # any changes or crashes.
8293                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8294         fi
8295
8296         # Get 'after' xattrs of file1.
8297         getfattr --absolute-names --dump --match=- $file1 > $xattr1
8298
8299         if ! diff $xattr0 $xattr1; then
8300                 error "before and after xattrs of '$file1' differ"
8301         fi
8302
8303         rm -rf $file0 $file1 $xattr0 $xattr1
8304
8305         return 0
8306 }
8307 run_test 102n "silently ignore setxattr on internal trusted xattrs"
8308
8309 test_102p() { # LU-4703 setxattr did not check ownership
8310         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
8311                 skip "MDS needs to be at least 2.5.56"
8312
8313         local testfile=$DIR/$tfile
8314
8315         touch $testfile
8316
8317         echo "setfacl as user..."
8318         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
8319         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
8320
8321         echo "setfattr as user..."
8322         setfacl -m "u:$RUNAS_ID:---" $testfile
8323         $RUNAS setfattr -x system.posix_acl_access $testfile
8324         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
8325 }
8326 run_test 102p "check setxattr(2) correctly fails without permission"
8327
8328 test_102q() {
8329         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
8330                 skip "MDS needs to be at least 2.6.92"
8331
8332         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
8333 }
8334 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
8335
8336 test_102r() {
8337         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
8338                 skip "MDS needs to be at least 2.6.93"
8339
8340         touch $DIR/$tfile || error "touch"
8341         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
8342         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
8343         rm $DIR/$tfile || error "rm"
8344
8345         #normal directory
8346         mkdir -p $DIR/$tdir || error "mkdir"
8347         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8348         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8349         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8350                 error "$testfile error deleting user.author1"
8351         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8352                 grep "user.$(basename $tdir)" &&
8353                 error "$tdir did not delete user.$(basename $tdir)"
8354         rmdir $DIR/$tdir || error "rmdir"
8355
8356         #striped directory
8357         test_mkdir $DIR/$tdir
8358         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8359         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8360         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8361                 error "$testfile error deleting user.author1"
8362         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8363                 grep "user.$(basename $tdir)" &&
8364                 error "$tdir did not delete user.$(basename $tdir)"
8365         rmdir $DIR/$tdir || error "rm striped dir"
8366 }
8367 run_test 102r "set EAs with empty values"
8368
8369 run_acl_subtest()
8370 {
8371     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
8372     return $?
8373 }
8374
8375 test_103a() {
8376         [ "$UID" != 0 ] && skip "must run as root"
8377         $GSS && skip_env "could not run under gss"
8378         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
8379                 skip_env "must have acl enabled"
8380         [ -z "$(which setfacl 2>/dev/null)" ] &&
8381                 skip_env "could not find setfacl"
8382         remote_mds_nodsh && skip "remote MDS with nodsh"
8383
8384         gpasswd -a daemon bin                           # LU-5641
8385         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
8386
8387         declare -a identity_old
8388
8389         for num in $(seq $MDSCOUNT); do
8390                 switch_identity $num true || identity_old[$num]=$?
8391         done
8392
8393         SAVE_UMASK=$(umask)
8394         umask 0022
8395         mkdir -p $DIR/$tdir
8396         cd $DIR/$tdir
8397
8398         echo "performing cp ..."
8399         run_acl_subtest cp || error "run_acl_subtest cp failed"
8400         echo "performing getfacl-noacl..."
8401         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
8402         echo "performing misc..."
8403         run_acl_subtest misc || error  "misc test failed"
8404         echo "performing permissions..."
8405         run_acl_subtest permissions || error "permissions failed"
8406         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
8407         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
8408              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
8409              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
8410         then
8411                 echo "performing permissions xattr..."
8412                 run_acl_subtest permissions_xattr ||
8413                         error "permissions_xattr failed"
8414         fi
8415         echo "performing setfacl..."
8416         run_acl_subtest setfacl || error  "setfacl test failed"
8417
8418         # inheritance test got from HP
8419         echo "performing inheritance..."
8420         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
8421         chmod +x make-tree || error "chmod +x failed"
8422         run_acl_subtest inheritance || error "inheritance test failed"
8423         rm -f make-tree
8424
8425         echo "LU-974 ignore umask when acl is enabled..."
8426         run_acl_subtest 974 || error "LU-974 umask test failed"
8427         if [ $MDSCOUNT -ge 2 ]; then
8428                 run_acl_subtest 974_remote ||
8429                         error "LU-974 umask test failed under remote dir"
8430         fi
8431
8432         echo "LU-2561 newly created file is same size as directory..."
8433         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
8434                 run_acl_subtest 2561 || error "LU-2561 test failed"
8435         else
8436                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
8437         fi
8438
8439         run_acl_subtest 4924 || error "LU-4924 test failed"
8440
8441         cd $SAVE_PWD
8442         umask $SAVE_UMASK
8443
8444         for num in $(seq $MDSCOUNT); do
8445                 if [ "${identity_old[$num]}" = 1 ]; then
8446                         switch_identity $num false || identity_old[$num]=$?
8447                 fi
8448         done
8449 }
8450 run_test 103a "acl test"
8451
8452 test_103b() {
8453         local U
8454
8455         for U in {0..511}; do
8456                 {
8457                 local O=$(printf "%04o" $U)
8458
8459                 umask $(printf "%04o" $((511 ^ $O)))
8460                 $LFS setstripe -c 1 $DIR/$tfile.s$O
8461                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
8462
8463                 (( $S == ($O & 0666) )) ||
8464                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
8465
8466                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
8467                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
8468                 (( $S == ($O & 0666) )) ||
8469                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
8470
8471                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
8472                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
8473                 (( $S == ($O & 0666) )) ||
8474                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
8475                 rm -f $DIR/$tfile.[smp]$0
8476                 } &
8477         done
8478         wait
8479 }
8480 run_test 103b "umask lfs setstripe"
8481
8482 test_103c() {
8483         mkdir -p $DIR/$tdir
8484         cp -rp $DIR/$tdir $DIR/$tdir.bak
8485
8486         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
8487                 error "$DIR/$tdir shouldn't contain default ACL"
8488         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
8489                 error "$DIR/$tdir.bak shouldn't contain default ACL"
8490         true
8491 }
8492 run_test 103c "'cp -rp' won't set empty acl"
8493
8494 test_104a() {
8495         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8496
8497         touch $DIR/$tfile
8498         lfs df || error "lfs df failed"
8499         lfs df -ih || error "lfs df -ih failed"
8500         lfs df -h $DIR || error "lfs df -h $DIR failed"
8501         lfs df -i $DIR || error "lfs df -i $DIR failed"
8502         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
8503         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
8504
8505         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
8506         lctl --device %$OSC deactivate
8507         lfs df || error "lfs df with deactivated OSC failed"
8508         lctl --device %$OSC activate
8509         # wait the osc back to normal
8510         wait_osc_import_state client ost FULL
8511
8512         lfs df || error "lfs df with reactivated OSC failed"
8513         rm -f $DIR/$tfile
8514 }
8515 run_test 104a "lfs df [-ih] [path] test ========================="
8516
8517 test_104b() {
8518         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8519         [ $RUNAS_ID -eq $UID ] &&
8520                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8521
8522         chmod 666 /dev/obd
8523         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
8524                         grep "Permission denied" | wc -l)))
8525         if [ $denied_cnt -ne 0 ]; then
8526                 error "lfs check servers test failed"
8527         fi
8528 }
8529 run_test 104b "$RUNAS lfs check servers test ===================="
8530
8531 test_105a() {
8532         # doesn't work on 2.4 kernels
8533         touch $DIR/$tfile
8534         if $(flock_is_enabled); then
8535                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
8536         else
8537                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
8538         fi
8539         rm -f $DIR/$tfile
8540 }
8541 run_test 105a "flock when mounted without -o flock test ========"
8542
8543 test_105b() {
8544         touch $DIR/$tfile
8545         if $(flock_is_enabled); then
8546                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
8547         else
8548                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
8549         fi
8550         rm -f $DIR/$tfile
8551 }
8552 run_test 105b "fcntl when mounted without -o flock test ========"
8553
8554 test_105c() {
8555         touch $DIR/$tfile
8556         if $(flock_is_enabled); then
8557                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
8558         else
8559                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
8560         fi
8561         rm -f $DIR/$tfile
8562 }
8563 run_test 105c "lockf when mounted without -o flock test"
8564
8565 test_105d() { # bug 15924
8566         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8567
8568         test_mkdir $DIR/$tdir
8569         flock_is_enabled || skip_env "mount w/o flock enabled"
8570         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
8571         $LCTL set_param fail_loc=0x80000315
8572         flocks_test 2 $DIR/$tdir
8573 }
8574 run_test 105d "flock race (should not freeze) ========"
8575
8576 test_105e() { # bug 22660 && 22040
8577         flock_is_enabled || skip_env "mount w/o flock enabled"
8578
8579         touch $DIR/$tfile
8580         flocks_test 3 $DIR/$tfile
8581 }
8582 run_test 105e "Two conflicting flocks from same process"
8583
8584 test_106() { #bug 10921
8585         test_mkdir $DIR/$tdir
8586         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
8587         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
8588 }
8589 run_test 106 "attempt exec of dir followed by chown of that dir"
8590
8591 test_107() {
8592         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8593
8594         CDIR=`pwd`
8595         local file=core
8596
8597         cd $DIR
8598         rm -f $file
8599
8600         local save_pattern=$(sysctl -n kernel.core_pattern)
8601         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
8602         sysctl -w kernel.core_pattern=$file
8603         sysctl -w kernel.core_uses_pid=0
8604
8605         ulimit -c unlimited
8606         sleep 60 &
8607         SLEEPPID=$!
8608
8609         sleep 1
8610
8611         kill -s 11 $SLEEPPID
8612         wait $SLEEPPID
8613         if [ -e $file ]; then
8614                 size=`stat -c%s $file`
8615                 [ $size -eq 0 ] && error "Fail to create core file $file"
8616         else
8617                 error "Fail to create core file $file"
8618         fi
8619         rm -f $file
8620         sysctl -w kernel.core_pattern=$save_pattern
8621         sysctl -w kernel.core_uses_pid=$save_uses_pid
8622         cd $CDIR
8623 }
8624 run_test 107 "Coredump on SIG"
8625
8626 test_110() {
8627         test_mkdir $DIR/$tdir
8628         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
8629         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
8630                 error "mkdir with 256 char should fail, but did not"
8631         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
8632                 error "create with 255 char failed"
8633         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
8634                 error "create with 256 char should fail, but did not"
8635
8636         ls -l $DIR/$tdir
8637         rm -rf $DIR/$tdir
8638 }
8639 run_test 110 "filename length checking"
8640
8641 #
8642 # Purpose: To verify dynamic thread (OSS) creation.
8643 #
8644 test_115() {
8645         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8646         remote_ost_nodsh && skip "remote OST with nodsh"
8647
8648         # Lustre does not stop service threads once they are started.
8649         # Reset number of running threads to default.
8650         stopall
8651         setupall
8652
8653         local OSTIO_pre
8654         local save_params="$TMP/sanity-$TESTNAME.parameters"
8655
8656         # Get ll_ost_io count before I/O
8657         OSTIO_pre=$(do_facet ost1 \
8658                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
8659         # Exit if lustre is not running (ll_ost_io not running).
8660         [ -z "$OSTIO_pre" ] && error "no OSS threads"
8661
8662         echo "Starting with $OSTIO_pre threads"
8663         local thread_max=$((OSTIO_pre * 2))
8664         local rpc_in_flight=$((thread_max * 2))
8665         # Number of I/O Process proposed to be started.
8666         local nfiles
8667         local facets=$(get_facets OST)
8668
8669         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
8670         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
8671
8672         # Set in_flight to $rpc_in_flight
8673         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
8674                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
8675         nfiles=${rpc_in_flight}
8676         # Set ost thread_max to $thread_max
8677         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
8678
8679         # 5 Minutes should be sufficient for max number of OSS
8680         # threads(thread_max) to be created.
8681         local timeout=300
8682
8683         # Start I/O.
8684         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
8685         test_mkdir $DIR/$tdir
8686         for i in $(seq $nfiles); do
8687                 local file=$DIR/$tdir/${tfile}-$i
8688                 $LFS setstripe -c -1 -i 0 $file
8689                 ($WTL $file $timeout)&
8690         done
8691
8692         # I/O Started - Wait for thread_started to reach thread_max or report
8693         # error if thread_started is more than thread_max.
8694         echo "Waiting for thread_started to reach thread_max"
8695         local thread_started=0
8696         local end_time=$((SECONDS + timeout))
8697
8698         while [ $SECONDS -le $end_time ] ; do
8699                 echo -n "."
8700                 # Get ost i/o thread_started count.
8701                 thread_started=$(do_facet ost1 \
8702                         "$LCTL get_param \
8703                         ost.OSS.ost_io.threads_started | cut -d= -f2")
8704                 # Break out if thread_started is equal/greater than thread_max
8705                 if [[ $thread_started -ge $thread_max ]]; then
8706                         echo ll_ost_io thread_started $thread_started, \
8707                                 equal/greater than thread_max $thread_max
8708                         break
8709                 fi
8710                 sleep 1
8711         done
8712
8713         # Cleanup - We have the numbers, Kill i/o jobs if running.
8714         jobcount=($(jobs -p))
8715         for i in $(seq 0 $((${#jobcount[@]}-1)))
8716         do
8717                 kill -9 ${jobcount[$i]}
8718                 if [ $? -ne 0 ] ; then
8719                         echo Warning: \
8720                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
8721                 fi
8722         done
8723
8724         # Cleanup files left by WTL binary.
8725         for i in $(seq $nfiles); do
8726                 local file=$DIR/$tdir/${tfile}-$i
8727                 rm -rf $file
8728                 if [ $? -ne 0 ] ; then
8729                         echo "Warning: Failed to delete file $file"
8730                 fi
8731         done
8732
8733         restore_lustre_params <$save_params
8734         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
8735
8736         # Error out if no new thread has started or Thread started is greater
8737         # than thread max.
8738         if [[ $thread_started -le $OSTIO_pre ||
8739                         $thread_started -gt $thread_max ]]; then
8740                 error "ll_ost_io: thread_started $thread_started" \
8741                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
8742                       "No new thread started or thread started greater " \
8743                       "than thread_max."
8744         fi
8745 }
8746 run_test 115 "verify dynamic thread creation===================="
8747
8748 free_min_max () {
8749         wait_delete_completed
8750         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8751         echo "OST kbytes available: ${AVAIL[@]}"
8752         MAXV=${AVAIL[0]}
8753         MAXI=0
8754         MINV=${AVAIL[0]}
8755         MINI=0
8756         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8757                 #echo OST $i: ${AVAIL[i]}kb
8758                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8759                         MAXV=${AVAIL[i]}
8760                         MAXI=$i
8761                 fi
8762                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8763                         MINV=${AVAIL[i]}
8764                         MINI=$i
8765                 fi
8766         done
8767         echo "Min free space: OST $MINI: $MINV"
8768         echo "Max free space: OST $MAXI: $MAXV"
8769 }
8770
8771 test_116a() { # was previously test_116()
8772         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8773         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8774         remote_mds_nodsh && skip "remote MDS with nodsh"
8775
8776         echo -n "Free space priority "
8777         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
8778                 head -n1
8779         declare -a AVAIL
8780         free_min_max
8781
8782         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
8783         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
8784         trap simple_cleanup_common EXIT
8785
8786         # Check if we need to generate uneven OSTs
8787         test_mkdir -p $DIR/$tdir/OST${MINI}
8788         local FILL=$((MINV / 4))
8789         local DIFF=$((MAXV - MINV))
8790         local DIFF2=$((DIFF * 100 / MINV))
8791
8792         local threshold=$(do_facet $SINGLEMDS \
8793                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
8794         threshold=${threshold%%%}
8795         echo -n "Check for uneven OSTs: "
8796         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
8797
8798         if [[ $DIFF2 -gt $threshold ]]; then
8799                 echo "ok"
8800                 echo "Don't need to fill OST$MINI"
8801         else
8802                 # generate uneven OSTs. Write 2% over the QOS threshold value
8803                 echo "no"
8804                 DIFF=$((threshold - DIFF2 + 2))
8805                 DIFF2=$((MINV * DIFF / 100))
8806                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
8807                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
8808                         error "setstripe failed"
8809                 DIFF=$((DIFF2 / 2048))
8810                 i=0
8811                 while [ $i -lt $DIFF ]; do
8812                         i=$((i + 1))
8813                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
8814                                 bs=2M count=1 2>/dev/null
8815                         echo -n .
8816                 done
8817                 echo .
8818                 sync
8819                 sleep_maxage
8820                 free_min_max
8821         fi
8822
8823         DIFF=$((MAXV - MINV))
8824         DIFF2=$((DIFF * 100 / MINV))
8825         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8826         if [ $DIFF2 -gt $threshold ]; then
8827                 echo "ok"
8828         else
8829                 echo "failed - QOS mode won't be used"
8830                 simple_cleanup_common
8831                 skip "QOS imbalance criteria not met"
8832         fi
8833
8834         MINI1=$MINI
8835         MINV1=$MINV
8836         MAXI1=$MAXI
8837         MAXV1=$MAXV
8838
8839         # now fill using QOS
8840         $SETSTRIPE -c 1 $DIR/$tdir
8841         FILL=$((FILL / 200))
8842         if [ $FILL -gt 600 ]; then
8843                 FILL=600
8844         fi
8845         echo "writing $FILL files to QOS-assigned OSTs"
8846         i=0
8847         while [ $i -lt $FILL ]; do
8848                 i=$((i + 1))
8849                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8850                         count=1 2>/dev/null
8851                 echo -n .
8852         done
8853         echo "wrote $i 200k files"
8854         sync
8855         sleep_maxage
8856
8857         echo "Note: free space may not be updated, so measurements might be off"
8858         free_min_max
8859         DIFF2=$((MAXV - MINV))
8860         echo "free space delta: orig $DIFF final $DIFF2"
8861         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8862         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8863         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8864         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8865         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8866         if [[ $DIFF -gt 0 ]]; then
8867                 FILL=$((DIFF2 * 100 / DIFF - 100))
8868                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8869         fi
8870
8871         # Figure out which files were written where
8872         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8873                awk '/'$MINI1': / {print $2; exit}')
8874         echo $UUID
8875         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8876         echo "$MINC files created on smaller OST $MINI1"
8877         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8878                awk '/'$MAXI1': / {print $2; exit}')
8879         echo $UUID
8880         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8881         echo "$MAXC files created on larger OST $MAXI1"
8882         if [[ $MINC -gt 0 ]]; then
8883                 FILL=$((MAXC * 100 / MINC - 100))
8884                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8885         fi
8886         [[ $MAXC -gt $MINC ]] ||
8887                 error_ignore LU-9 "stripe QOS didn't balance free space"
8888         simple_cleanup_common
8889 }
8890 run_test 116a "stripe QOS: free space balance ==================="
8891
8892 test_116b() { # LU-2093
8893         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8894         remote_mds_nodsh && skip "remote MDS with nodsh"
8895
8896 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8897         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8898                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8899         [ -z "$old_rr" ] && skip "no QOS"
8900         do_facet $SINGLEMDS lctl set_param \
8901                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8902         mkdir -p $DIR/$tdir
8903         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8904         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8905         do_facet $SINGLEMDS lctl set_param fail_loc=0
8906         rm -rf $DIR/$tdir
8907         do_facet $SINGLEMDS lctl set_param \
8908                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8909 }
8910 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8911
8912 test_117() # bug 10891
8913 {
8914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8915
8916         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8917         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8918         lctl set_param fail_loc=0x21e
8919         > $DIR/$tfile || error "truncate failed"
8920         lctl set_param fail_loc=0
8921         echo "Truncate succeeded."
8922         rm -f $DIR/$tfile
8923 }
8924 run_test 117 "verify osd extend =========="
8925
8926 NO_SLOW_RESENDCOUNT=4
8927 export OLD_RESENDCOUNT=""
8928 set_resend_count () {
8929         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8930         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8931         lctl set_param -n $PROC_RESENDCOUNT $1
8932         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8933 }
8934
8935 # for reduce test_118* time (b=14842)
8936 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8937
8938 # Reset async IO behavior after error case
8939 reset_async() {
8940         FILE=$DIR/reset_async
8941
8942         # Ensure all OSCs are cleared
8943         $SETSTRIPE -c -1 $FILE
8944         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8945         sync
8946         rm $FILE
8947 }
8948
8949 test_118a() #bug 11710
8950 {
8951         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8952
8953         reset_async
8954
8955         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8956         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8957         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8958
8959         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8960                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8961                 return 1;
8962         fi
8963         rm -f $DIR/$tfile
8964 }
8965 run_test 118a "verify O_SYNC works =========="
8966
8967 test_118b()
8968 {
8969         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8970         remote_ost_nodsh && skip "remote OST with nodsh"
8971
8972         reset_async
8973
8974         #define OBD_FAIL_SRV_ENOENT 0x217
8975         set_nodes_failloc "$(osts_nodes)" 0x217
8976         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8977         RC=$?
8978         set_nodes_failloc "$(osts_nodes)" 0
8979         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8980         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8981                     grep -c writeback)
8982
8983         if [[ $RC -eq 0 ]]; then
8984                 error "Must return error due to dropped pages, rc=$RC"
8985                 return 1;
8986         fi
8987
8988         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8989                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8990                 return 1;
8991         fi
8992
8993         echo "Dirty pages not leaked on ENOENT"
8994
8995         # Due to the above error the OSC will issue all RPCs syncronously
8996         # until a subsequent RPC completes successfully without error.
8997         $MULTIOP $DIR/$tfile Ow4096yc
8998         rm -f $DIR/$tfile
8999
9000         return 0
9001 }
9002 run_test 118b "Reclaim dirty pages on fatal error =========="
9003
9004 test_118c()
9005 {
9006         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9007
9008         # for 118c, restore the original resend count, LU-1940
9009         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
9010                                 set_resend_count $OLD_RESENDCOUNT
9011         remote_ost_nodsh && skip "remote OST with nodsh"
9012
9013         reset_async
9014
9015         #define OBD_FAIL_OST_EROFS               0x216
9016         set_nodes_failloc "$(osts_nodes)" 0x216
9017
9018         # multiop should block due to fsync until pages are written
9019         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9020         MULTIPID=$!
9021         sleep 1
9022
9023         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9024                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9025         fi
9026
9027         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9028                     grep -c writeback)
9029         if [[ $WRITEBACK -eq 0 ]]; then
9030                 error "No page in writeback, writeback=$WRITEBACK"
9031         fi
9032
9033         set_nodes_failloc "$(osts_nodes)" 0
9034         wait $MULTIPID
9035         RC=$?
9036         if [[ $RC -ne 0 ]]; then
9037                 error "Multiop fsync failed, rc=$RC"
9038         fi
9039
9040         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9041         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9042                     grep -c writeback)
9043         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9044                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9045         fi
9046
9047         rm -f $DIR/$tfile
9048         echo "Dirty pages flushed via fsync on EROFS"
9049         return 0
9050 }
9051 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
9052
9053 # continue to use small resend count to reduce test_118* time (b=14842)
9054 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9055
9056 test_118d()
9057 {
9058         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9059         remote_ost_nodsh && skip "remote OST with nodsh"
9060
9061         reset_async
9062
9063         #define OBD_FAIL_OST_BRW_PAUSE_BULK
9064         set_nodes_failloc "$(osts_nodes)" 0x214
9065         # multiop should block due to fsync until pages are written
9066         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9067         MULTIPID=$!
9068         sleep 1
9069
9070         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9071                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9072         fi
9073
9074         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9075                     grep -c writeback)
9076         if [[ $WRITEBACK -eq 0 ]]; then
9077                 error "No page in writeback, writeback=$WRITEBACK"
9078         fi
9079
9080         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
9081         set_nodes_failloc "$(osts_nodes)" 0
9082
9083         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9084         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9085                     grep -c writeback)
9086         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9087                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9088         fi
9089
9090         rm -f $DIR/$tfile
9091         echo "Dirty pages gaurenteed flushed via fsync"
9092         return 0
9093 }
9094 run_test 118d "Fsync validation inject a delay of the bulk =========="
9095
9096 test_118f() {
9097         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9098
9099         reset_async
9100
9101         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
9102         lctl set_param fail_loc=0x8000040a
9103
9104         # Should simulate EINVAL error which is fatal
9105         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9106         RC=$?
9107         if [[ $RC -eq 0 ]]; then
9108                 error "Must return error due to dropped pages, rc=$RC"
9109         fi
9110
9111         lctl set_param fail_loc=0x0
9112
9113         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9114         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9115         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9116                     grep -c writeback)
9117         if [[ $LOCKED -ne 0 ]]; then
9118                 error "Locked pages remain in cache, locked=$LOCKED"
9119         fi
9120
9121         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9122                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9123         fi
9124
9125         rm -f $DIR/$tfile
9126         echo "No pages locked after fsync"
9127
9128         reset_async
9129         return 0
9130 }
9131 run_test 118f "Simulate unrecoverable OSC side error =========="
9132
9133 test_118g() {
9134         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9135
9136         reset_async
9137
9138         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
9139         lctl set_param fail_loc=0x406
9140
9141         # simulate local -ENOMEM
9142         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9143         RC=$?
9144
9145         lctl set_param fail_loc=0
9146         if [[ $RC -eq 0 ]]; then
9147                 error "Must return error due to dropped pages, rc=$RC"
9148         fi
9149
9150         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9151         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9152         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9153                         grep -c writeback)
9154         if [[ $LOCKED -ne 0 ]]; then
9155                 error "Locked pages remain in cache, locked=$LOCKED"
9156         fi
9157
9158         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9159                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9160         fi
9161
9162         rm -f $DIR/$tfile
9163         echo "No pages locked after fsync"
9164
9165         reset_async
9166         return 0
9167 }
9168 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
9169
9170 test_118h() {
9171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9172         remote_ost_nodsh && skip "remote OST with nodsh"
9173
9174         reset_async
9175
9176         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9177         set_nodes_failloc "$(osts_nodes)" 0x20e
9178         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9179         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9180         RC=$?
9181
9182         set_nodes_failloc "$(osts_nodes)" 0
9183         if [[ $RC -eq 0 ]]; then
9184                 error "Must return error due to dropped pages, rc=$RC"
9185         fi
9186
9187         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9188         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9189         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9190                     grep -c writeback)
9191         if [[ $LOCKED -ne 0 ]]; then
9192                 error "Locked pages remain in cache, locked=$LOCKED"
9193         fi
9194
9195         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9196                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9197         fi
9198
9199         rm -f $DIR/$tfile
9200         echo "No pages locked after fsync"
9201
9202         return 0
9203 }
9204 run_test 118h "Verify timeout in handling recoverables errors  =========="
9205
9206 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9207
9208 test_118i() {
9209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9210         remote_ost_nodsh && skip "remote OST with nodsh"
9211
9212         reset_async
9213
9214         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9215         set_nodes_failloc "$(osts_nodes)" 0x20e
9216
9217         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9218         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9219         PID=$!
9220         sleep 5
9221         set_nodes_failloc "$(osts_nodes)" 0
9222
9223         wait $PID
9224         RC=$?
9225         if [[ $RC -ne 0 ]]; then
9226                 error "got error, but should be not, rc=$RC"
9227         fi
9228
9229         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9230         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9231         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9232         if [[ $LOCKED -ne 0 ]]; then
9233                 error "Locked pages remain in cache, locked=$LOCKED"
9234         fi
9235
9236         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9237                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9238         fi
9239
9240         rm -f $DIR/$tfile
9241         echo "No pages locked after fsync"
9242
9243         return 0
9244 }
9245 run_test 118i "Fix error before timeout in recoverable error  =========="
9246
9247 [ "$SLOW" = "no" ] && set_resend_count 4
9248
9249 test_118j() {
9250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9251         remote_ost_nodsh && skip "remote OST with nodsh"
9252
9253         reset_async
9254
9255         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
9256         set_nodes_failloc "$(osts_nodes)" 0x220
9257
9258         # return -EIO from OST
9259         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9260         RC=$?
9261         set_nodes_failloc "$(osts_nodes)" 0x0
9262         if [[ $RC -eq 0 ]]; then
9263                 error "Must return error due to dropped pages, rc=$RC"
9264         fi
9265
9266         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9267         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9268         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9269         if [[ $LOCKED -ne 0 ]]; then
9270                 error "Locked pages remain in cache, locked=$LOCKED"
9271         fi
9272
9273         # in recoverable error on OST we want resend and stay until it finished
9274         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9275                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9276         fi
9277
9278         rm -f $DIR/$tfile
9279         echo "No pages locked after fsync"
9280
9281         return 0
9282 }
9283 run_test 118j "Simulate unrecoverable OST side error =========="
9284
9285 test_118k()
9286 {
9287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9288         remote_ost_nodsh && skip "remote OSTs with nodsh"
9289
9290         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9291         set_nodes_failloc "$(osts_nodes)" 0x20e
9292         test_mkdir $DIR/$tdir
9293
9294         for ((i=0;i<10;i++)); do
9295                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
9296                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
9297                 SLEEPPID=$!
9298                 sleep 0.500s
9299                 kill $SLEEPPID
9300                 wait $SLEEPPID
9301         done
9302
9303         set_nodes_failloc "$(osts_nodes)" 0
9304         rm -rf $DIR/$tdir
9305 }
9306 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
9307
9308 test_118l() # LU-646
9309 {
9310         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9311
9312         test_mkdir $DIR/$tdir
9313         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
9314         rm -rf $DIR/$tdir
9315 }
9316 run_test 118l "fsync dir"
9317
9318 test_118m() # LU-3066
9319 {
9320         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9321
9322         test_mkdir $DIR/$tdir
9323         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
9324         rm -rf $DIR/$tdir
9325 }
9326 run_test 118m "fdatasync dir ========="
9327
9328 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9329
9330 test_118n()
9331 {
9332         local begin
9333         local end
9334
9335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9336         remote_ost_nodsh && skip "remote OSTs with nodsh"
9337
9338         # Sleep to avoid a cached response.
9339         #define OBD_STATFS_CACHE_SECONDS 1
9340         sleep 2
9341
9342         # Inject a 10 second delay in the OST_STATFS handler.
9343         #define OBD_FAIL_OST_STATFS_DELAY 0x242
9344         set_nodes_failloc "$(osts_nodes)" 0x242
9345
9346         begin=$SECONDS
9347         stat --file-system $MOUNT > /dev/null
9348         end=$SECONDS
9349
9350         set_nodes_failloc "$(osts_nodes)" 0
9351
9352         if ((end - begin > 20)); then
9353             error "statfs took $((end - begin)) seconds, expected 10"
9354         fi
9355 }
9356 run_test 118n "statfs() sends OST_STATFS requests in parallel"
9357
9358 test_119a() # bug 11737
9359 {
9360         BSIZE=$((512 * 1024))
9361         directio write $DIR/$tfile 0 1 $BSIZE
9362         # We ask to read two blocks, which is more than a file size.
9363         # directio will indicate an error when requested and actual
9364         # sizes aren't equeal (a normal situation in this case) and
9365         # print actual read amount.
9366         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
9367         if [ "$NOB" != "$BSIZE" ]; then
9368                 error "read $NOB bytes instead of $BSIZE"
9369         fi
9370         rm -f $DIR/$tfile
9371 }
9372 run_test 119a "Short directIO read must return actual read amount"
9373
9374 test_119b() # bug 11737
9375 {
9376         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9377
9378         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
9379         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
9380         sync
9381         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
9382                 error "direct read failed"
9383         rm -f $DIR/$tfile
9384 }
9385 run_test 119b "Sparse directIO read must return actual read amount"
9386
9387 test_119c() # bug 13099
9388 {
9389         BSIZE=1048576
9390         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
9391         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
9392         rm -f $DIR/$tfile
9393 }
9394 run_test 119c "Testing for direct read hitting hole"
9395
9396 test_119d() # bug 15950
9397 {
9398         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9399
9400         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
9401         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
9402         BSIZE=1048576
9403         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
9404         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
9405         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
9406         lctl set_param fail_loc=0x40d
9407         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
9408         pid_dio=$!
9409         sleep 1
9410         cat $DIR/$tfile > /dev/null &
9411         lctl set_param fail_loc=0
9412         pid_reads=$!
9413         wait $pid_dio
9414         log "the DIO writes have completed, now wait for the reads (should not block very long)"
9415         sleep 2
9416         [ -n "`ps h -p $pid_reads -o comm`" ] && \
9417         error "the read rpcs have not completed in 2s"
9418         rm -f $DIR/$tfile
9419         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
9420 }
9421 run_test 119d "The DIO path should try to send a new rpc once one is completed"
9422
9423 test_120a() {
9424         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9425         remote_mds_nodsh && skip "remote MDS with nodsh"
9426         test_mkdir $DIR/$tdir
9427         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9428                 skip_env "no early lock cancel on server"
9429
9430         lru_resize_disable mdc
9431         lru_resize_disable osc
9432         cancel_lru_locks mdc
9433         # asynchronous object destroy at MDT could cause bl ast to client
9434         cancel_lru_locks osc
9435
9436         stat $DIR/$tdir > /dev/null
9437         can1=$(do_facet $SINGLEMDS \
9438                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9439                awk '/ldlm_cancel/ {print $2}')
9440         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9441                awk '/ldlm_bl_callback/ {print $2}')
9442         test_mkdir -c1 $DIR/$tdir/d1
9443         can2=$(do_facet $SINGLEMDS \
9444                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9445                awk '/ldlm_cancel/ {print $2}')
9446         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9447                awk '/ldlm_bl_callback/ {print $2}')
9448         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9449         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9450         lru_resize_enable mdc
9451         lru_resize_enable osc
9452 }
9453 run_test 120a "Early Lock Cancel: mkdir test"
9454
9455 test_120b() {
9456         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9457         remote_mds_nodsh && skip "remote MDS with nodsh"
9458         test_mkdir $DIR/$tdir
9459         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9460                 skip_env "no early lock cancel on server"
9461
9462         lru_resize_disable mdc
9463         lru_resize_disable osc
9464         cancel_lru_locks mdc
9465         stat $DIR/$tdir > /dev/null
9466         can1=$(do_facet $SINGLEMDS \
9467                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9468                awk '/ldlm_cancel/ {print $2}')
9469         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9470                awk '/ldlm_bl_callback/ {print $2}')
9471         touch $DIR/$tdir/f1
9472         can2=$(do_facet $SINGLEMDS \
9473                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9474                awk '/ldlm_cancel/ {print $2}')
9475         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9476                awk '/ldlm_bl_callback/ {print $2}')
9477         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9478         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9479         lru_resize_enable mdc
9480         lru_resize_enable osc
9481 }
9482 run_test 120b "Early Lock Cancel: create test"
9483
9484 test_120c() {
9485         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9486         remote_mds_nodsh && skip "remote MDS with nodsh"
9487         test_mkdir -c1 $DIR/$tdir
9488         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9489                 skip "no early lock cancel on server"
9490
9491         lru_resize_disable mdc
9492         lru_resize_disable osc
9493         test_mkdir -c1 $DIR/$tdir/d1
9494         test_mkdir -c1 $DIR/$tdir/d2
9495         touch $DIR/$tdir/d1/f1
9496         cancel_lru_locks mdc
9497         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
9498         can1=$(do_facet $SINGLEMDS \
9499                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9500                awk '/ldlm_cancel/ {print $2}')
9501         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9502                awk '/ldlm_bl_callback/ {print $2}')
9503         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9504         can2=$(do_facet $SINGLEMDS \
9505                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9506                awk '/ldlm_cancel/ {print $2}')
9507         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9508                awk '/ldlm_bl_callback/ {print $2}')
9509         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9510         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9511         lru_resize_enable mdc
9512         lru_resize_enable osc
9513 }
9514 run_test 120c "Early Lock Cancel: link test"
9515
9516 test_120d() {
9517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9518         remote_mds_nodsh && skip "remote MDS with nodsh"
9519         test_mkdir -c1 $DIR/$tdir
9520         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9521                 skip_env "no early lock cancel on server"
9522
9523         lru_resize_disable mdc
9524         lru_resize_disable osc
9525         touch $DIR/$tdir
9526         cancel_lru_locks mdc
9527         stat $DIR/$tdir > /dev/null
9528         can1=$(do_facet $SINGLEMDS \
9529                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9530                awk '/ldlm_cancel/ {print $2}')
9531         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9532                awk '/ldlm_bl_callback/ {print $2}')
9533         chmod a+x $DIR/$tdir
9534         can2=$(do_facet $SINGLEMDS \
9535                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9536                awk '/ldlm_cancel/ {print $2}')
9537         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9538                awk '/ldlm_bl_callback/ {print $2}')
9539         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9540         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9541         lru_resize_enable mdc
9542         lru_resize_enable osc
9543 }
9544 run_test 120d "Early Lock Cancel: setattr test"
9545
9546 test_120e() {
9547         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9548         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9549                 skip_env "no early lock cancel on server"
9550         remote_mds_nodsh && skip "remote MDS with nodsh"
9551
9552         local dlmtrace_set=false
9553
9554         test_mkdir -c1 $DIR/$tdir
9555         lru_resize_disable mdc
9556         lru_resize_disable osc
9557         ! $LCTL get_param debug | grep -q dlmtrace &&
9558                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
9559         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
9560         cancel_lru_locks mdc
9561         cancel_lru_locks osc
9562         dd if=$DIR/$tdir/f1 of=/dev/null
9563         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
9564         # XXX client can not do early lock cancel of OST lock
9565         # during unlink (LU-4206), so cancel osc lock now.
9566         sleep 2
9567         cancel_lru_locks osc
9568         can1=$(do_facet $SINGLEMDS \
9569                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9570                awk '/ldlm_cancel/ {print $2}')
9571         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9572                awk '/ldlm_bl_callback/ {print $2}')
9573         unlink $DIR/$tdir/f1
9574         sleep 5
9575         can2=$(do_facet $SINGLEMDS \
9576                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9577                awk '/ldlm_cancel/ {print $2}')
9578         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9579                awk '/ldlm_bl_callback/ {print $2}')
9580         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
9581                 $LCTL dk $TMP/cancel.debug.txt
9582         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
9583                 $LCTL dk $TMP/blocking.debug.txt
9584         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
9585         lru_resize_enable mdc
9586         lru_resize_enable osc
9587 }
9588 run_test 120e "Early Lock Cancel: unlink test"
9589
9590 test_120f() {
9591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9592         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9593                 skip_env "no early lock cancel on server"
9594         remote_mds_nodsh && skip "remote MDS with nodsh"
9595
9596         test_mkdir -c1 $DIR/$tdir
9597         lru_resize_disable mdc
9598         lru_resize_disable osc
9599         test_mkdir -c1 $DIR/$tdir/d1
9600         test_mkdir -c1 $DIR/$tdir/d2
9601         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
9602         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
9603         cancel_lru_locks mdc
9604         cancel_lru_locks osc
9605         dd if=$DIR/$tdir/d1/f1 of=/dev/null
9606         dd if=$DIR/$tdir/d2/f2 of=/dev/null
9607         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
9608         # XXX client can not do early lock cancel of OST lock
9609         # during rename (LU-4206), so cancel osc lock now.
9610         sleep 2
9611         cancel_lru_locks osc
9612         can1=$(do_facet $SINGLEMDS \
9613                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9614                awk '/ldlm_cancel/ {print $2}')
9615         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9616                awk '/ldlm_bl_callback/ {print $2}')
9617         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9618         sleep 5
9619         can2=$(do_facet $SINGLEMDS \
9620                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9621                awk '/ldlm_cancel/ {print $2}')
9622         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9623                awk '/ldlm_bl_callback/ {print $2}')
9624         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9625         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9626         lru_resize_enable mdc
9627         lru_resize_enable osc
9628 }
9629 run_test 120f "Early Lock Cancel: rename test"
9630
9631 test_120g() {
9632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9633         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9634                 skip_env "no early lock cancel on server"
9635         remote_mds_nodsh && skip "remote MDS with nodsh"
9636
9637         lru_resize_disable mdc
9638         lru_resize_disable osc
9639         count=10000
9640         echo create $count files
9641         test_mkdir $DIR/$tdir
9642         cancel_lru_locks mdc
9643         cancel_lru_locks osc
9644         t0=$(date +%s)
9645
9646         can0=$(do_facet $SINGLEMDS \
9647                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9648                awk '/ldlm_cancel/ {print $2}')
9649         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9650                awk '/ldlm_bl_callback/ {print $2}')
9651         createmany -o $DIR/$tdir/f $count
9652         sync
9653         can1=$(do_facet $SINGLEMDS \
9654                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9655                awk '/ldlm_cancel/ {print $2}')
9656         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9657                awk '/ldlm_bl_callback/ {print $2}')
9658         t1=$(date +%s)
9659         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
9660         echo rm $count files
9661         rm -r $DIR/$tdir
9662         sync
9663         can2=$(do_facet $SINGLEMDS \
9664                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9665                awk '/ldlm_cancel/ {print $2}')
9666         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9667                awk '/ldlm_bl_callback/ {print $2}')
9668         t2=$(date +%s)
9669         echo total: $count removes in $((t2-t1))
9670         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
9671         sleep 2
9672         # wait for commitment of removal
9673         lru_resize_enable mdc
9674         lru_resize_enable osc
9675 }
9676 run_test 120g "Early Lock Cancel: performance test"
9677
9678 test_121() { #bug #10589
9679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9680
9681         rm -rf $DIR/$tfile
9682         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
9683 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
9684         lctl set_param fail_loc=0x310
9685         cancel_lru_locks osc > /dev/null
9686         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
9687         lctl set_param fail_loc=0
9688         [[ $reads -eq $writes ]] ||
9689                 error "read $reads blocks, must be $writes blocks"
9690 }
9691 run_test 121 "read cancel race ========="
9692
9693 test_123a() { # was test 123, statahead(bug 11401)
9694         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9695
9696         SLOWOK=0
9697         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
9698                 log "testing UP system. Performance may be lower than expected."
9699                 SLOWOK=1
9700         fi
9701
9702         rm -rf $DIR/$tdir
9703         test_mkdir $DIR/$tdir
9704         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
9705         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
9706         MULT=10
9707         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
9708                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
9709
9710                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9711                 lctl set_param -n llite.*.statahead_max 0
9712                 lctl get_param llite.*.statahead_max
9713                 cancel_lru_locks mdc
9714                 cancel_lru_locks osc
9715                 stime=`date +%s`
9716                 time ls -l $DIR/$tdir | wc -l
9717                 etime=`date +%s`
9718                 delta=$((etime - stime))
9719                 log "ls $i files without statahead: $delta sec"
9720                 lctl set_param llite.*.statahead_max=$max
9721
9722                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9723                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
9724                 cancel_lru_locks mdc
9725                 cancel_lru_locks osc
9726                 stime=`date +%s`
9727                 time ls -l $DIR/$tdir | wc -l
9728                 etime=`date +%s`
9729                 delta_sa=$((etime - stime))
9730                 log "ls $i files with statahead: $delta_sa sec"
9731                 lctl get_param -n llite.*.statahead_stats
9732                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9733
9734                 [[ $swrong -lt $ewrong ]] &&
9735                         log "statahead was stopped, maybe too many locks held!"
9736                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
9737
9738                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9739                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9740                     lctl set_param -n llite.*.statahead_max 0
9741                     lctl get_param llite.*.statahead_max
9742                     cancel_lru_locks mdc
9743                     cancel_lru_locks osc
9744                     stime=`date +%s`
9745                     time ls -l $DIR/$tdir | wc -l
9746                     etime=`date +%s`
9747                     delta=$((etime - stime))
9748                     log "ls $i files again without statahead: $delta sec"
9749                     lctl set_param llite.*.statahead_max=$max
9750                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9751                         if [  $SLOWOK -eq 0 ]; then
9752                                 error "ls $i files is slower with statahead!"
9753                         else
9754                                 log "ls $i files is slower with statahead!"
9755                         fi
9756                         break
9757                     fi
9758                 fi
9759
9760                 [ $delta -gt 20 ] && break
9761                 [ $delta -gt 8 ] && MULT=$((50 / delta))
9762                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
9763         done
9764         log "ls done"
9765
9766         stime=`date +%s`
9767         rm -r $DIR/$tdir
9768         sync
9769         etime=`date +%s`
9770         delta=$((etime - stime))
9771         log "rm -r $DIR/$tdir/: $delta seconds"
9772         log "rm done"
9773         lctl get_param -n llite.*.statahead_stats
9774 }
9775 run_test 123a "verify statahead work"
9776
9777 test_123b () { # statahead(bug 15027)
9778         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9779
9780         test_mkdir $DIR/$tdir
9781         createmany -o $DIR/$tdir/$tfile-%d 1000
9782
9783         cancel_lru_locks mdc
9784         cancel_lru_locks osc
9785
9786 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
9787         lctl set_param fail_loc=0x80000803
9788         ls -lR $DIR/$tdir > /dev/null
9789         log "ls done"
9790         lctl set_param fail_loc=0x0
9791         lctl get_param -n llite.*.statahead_stats
9792         rm -r $DIR/$tdir
9793         sync
9794
9795 }
9796 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
9797
9798 test_124a() {
9799         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9800         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9801                 skip_env "no lru resize on server"
9802
9803         local NR=2000
9804
9805         test_mkdir $DIR/$tdir
9806
9807         log "create $NR files at $DIR/$tdir"
9808         createmany -o $DIR/$tdir/f $NR ||
9809                 error "failed to create $NR files in $DIR/$tdir"
9810
9811         cancel_lru_locks mdc
9812         ls -l $DIR/$tdir > /dev/null
9813
9814         local NSDIR=""
9815         local LRU_SIZE=0
9816         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
9817                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
9818                 LRU_SIZE=$($LCTL get_param -n $PARAM)
9819                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
9820                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
9821                         log "NSDIR=$NSDIR"
9822                         log "NS=$(basename $NSDIR)"
9823                         break
9824                 fi
9825         done
9826
9827         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
9828                 skip "Not enough cached locks created!"
9829         fi
9830         log "LRU=$LRU_SIZE"
9831
9832         local SLEEP=30
9833
9834         # We know that lru resize allows one client to hold $LIMIT locks
9835         # for 10h. After that locks begin to be killed by client.
9836         local MAX_HRS=10
9837         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
9838         log "LIMIT=$LIMIT"
9839         if [ $LIMIT -lt $LRU_SIZE ]; then
9840                 skip "Limit is too small $LIMIT"
9841         fi
9842
9843         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
9844         # killing locks. Some time was spent for creating locks. This means
9845         # that up to the moment of sleep finish we must have killed some of
9846         # them (10-100 locks). This depends on how fast ther were created.
9847         # Many of them were touched in almost the same moment and thus will
9848         # be killed in groups.
9849         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
9850
9851         # Use $LRU_SIZE_B here to take into account real number of locks
9852         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
9853         local LRU_SIZE_B=$LRU_SIZE
9854         log "LVF=$LVF"
9855         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
9856         log "OLD_LVF=$OLD_LVF"
9857         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
9858
9859         # Let's make sure that we really have some margin. Client checks
9860         # cached locks every 10 sec.
9861         SLEEP=$((SLEEP+20))
9862         log "Sleep ${SLEEP} sec"
9863         local SEC=0
9864         while ((SEC<$SLEEP)); do
9865                 echo -n "..."
9866                 sleep 5
9867                 SEC=$((SEC+5))
9868                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9869                 echo -n "$LRU_SIZE"
9870         done
9871         echo ""
9872         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9873         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9874
9875         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9876                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9877                 unlinkmany $DIR/$tdir/f $NR
9878                 return
9879         }
9880
9881         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9882         log "unlink $NR files at $DIR/$tdir"
9883         unlinkmany $DIR/$tdir/f $NR
9884 }
9885 run_test 124a "lru resize ======================================="
9886
9887 get_max_pool_limit()
9888 {
9889         local limit=$($LCTL get_param \
9890                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9891         local max=0
9892         for l in $limit; do
9893                 if [[ $l -gt $max ]]; then
9894                         max=$l
9895                 fi
9896         done
9897         echo $max
9898 }
9899
9900 test_124b() {
9901         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9902         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9903                 skip_env "no lru resize on server"
9904
9905         LIMIT=$(get_max_pool_limit)
9906
9907         NR=$(($(default_lru_size)*20))
9908         if [[ $NR -gt $LIMIT ]]; then
9909                 log "Limit lock number by $LIMIT locks"
9910                 NR=$LIMIT
9911         fi
9912
9913         IFree=$(mdsrate_inodes_available)
9914         if [ $IFree -lt $NR ]; then
9915                 log "Limit lock number by $IFree inodes"
9916                 NR=$IFree
9917         fi
9918
9919         lru_resize_disable mdc
9920         test_mkdir -p $DIR/$tdir/disable_lru_resize
9921
9922         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9923         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9924         cancel_lru_locks mdc
9925         stime=`date +%s`
9926         PID=""
9927         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9928         PID="$PID $!"
9929         sleep 2
9930         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9931         PID="$PID $!"
9932         sleep 2
9933         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9934         PID="$PID $!"
9935         wait $PID
9936         etime=`date +%s`
9937         nolruresize_delta=$((etime-stime))
9938         log "ls -la time: $nolruresize_delta seconds"
9939         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9940         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9941
9942         lru_resize_enable mdc
9943         test_mkdir -p $DIR/$tdir/enable_lru_resize
9944
9945         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9946         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9947         cancel_lru_locks mdc
9948         stime=`date +%s`
9949         PID=""
9950         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9951         PID="$PID $!"
9952         sleep 2
9953         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9954         PID="$PID $!"
9955         sleep 2
9956         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9957         PID="$PID $!"
9958         wait $PID
9959         etime=`date +%s`
9960         lruresize_delta=$((etime-stime))
9961         log "ls -la time: $lruresize_delta seconds"
9962         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9963
9964         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9965                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9966         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9967                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9968         else
9969                 log "lru resize performs the same with no lru resize"
9970         fi
9971         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9972 }
9973 run_test 124b "lru resize (performance test) ======================="
9974
9975 test_124c() {
9976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9977         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9978                 skip_env "no lru resize on server"
9979
9980         # cache ununsed locks on client
9981         local nr=100
9982         cancel_lru_locks mdc
9983         test_mkdir $DIR/$tdir
9984         createmany -o $DIR/$tdir/f $nr ||
9985                 error "failed to create $nr files in $DIR/$tdir"
9986         ls -l $DIR/$tdir > /dev/null
9987
9988         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9989         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9990         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9991         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9992         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9993
9994         # set lru_max_age to 1 sec
9995         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9996         echo "sleep $((recalc_p * 2)) seconds..."
9997         sleep $((recalc_p * 2))
9998
9999         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
10000         # restore lru_max_age
10001         $LCTL set_param -n $nsdir.lru_max_age $max_age
10002         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
10003         unlinkmany $DIR/$tdir/f $nr
10004 }
10005 run_test 124c "LRUR cancel very aged locks"
10006
10007 test_125() { # 13358
10008         $LCTL get_param -n llite.*.client_type | grep -q local ||
10009                 skip "must run as local client"
10010         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
10011                 skip_env "must have acl enabled"
10012         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
10013
10014         test_mkdir $DIR/$tdir
10015         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
10016         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
10017         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
10018 }
10019 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
10020
10021 test_126() { # bug 12829/13455
10022         $GSS && skip_env "must run as gss disabled"
10023         $LCTL get_param -n llite.*.client_type | grep -q local ||
10024                 skip "must run as local client"
10025         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
10026
10027         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
10028         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
10029         rm -f $DIR/$tfile
10030         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
10031 }
10032 run_test 126 "check that the fsgid provided by the client is taken into account"
10033
10034 test_127a() { # bug 15521
10035         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10036
10037         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
10038         $LCTL set_param osc.*.stats=0
10039         FSIZE=$((2048 * 1024))
10040         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10041         cancel_lru_locks osc
10042         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
10043
10044         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
10045         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10046                 echo "got $COUNT $NAME"
10047                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
10048                 eval $NAME=$COUNT || error "Wrong proc format"
10049
10050                 case $NAME in
10051                         read_bytes|write_bytes)
10052                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
10053                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
10054                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
10055                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
10056                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
10057                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
10058                                 error "sumsquare is too small: $SUMSQ"
10059                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
10060                                 error "sumsquare is too big: $SUMSQ"
10061                         ;;
10062                         *) ;;
10063                 esac
10064         done < $DIR/${tfile}.tmp
10065
10066         #check that we actually got some stats
10067         [ "$read_bytes" ] || error "Missing read_bytes stats"
10068         [ "$write_bytes" ] || error "Missing write_bytes stats"
10069         [ "$read_bytes" != 0 ] || error "no read done"
10070         [ "$write_bytes" != 0 ] || error "no write done"
10071 }
10072 run_test 127a "verify the client stats are sane"
10073
10074 test_127b() { # bug LU-333
10075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10076
10077         $LCTL set_param llite.*.stats=0
10078         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
10079
10080         # perform 2 reads and writes so MAX is different from SUM.
10081         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10082         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10083         cancel_lru_locks osc
10084         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
10085         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
10086
10087         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
10088         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10089                 echo "got $COUNT $NAME"
10090                 eval $NAME=$COUNT || error "Wrong proc format"
10091
10092         case $NAME in
10093                 read_bytes)
10094                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
10095                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
10096                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
10097                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
10098                         ;;
10099                 write_bytes)
10100                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
10101                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
10102                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
10103                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
10104                         ;;
10105                         *) ;;
10106                 esac
10107         done < $TMP/${tfile}.tmp
10108
10109         #check that we actually got some stats
10110         [ "$read_bytes" ] || error "Missing read_bytes stats"
10111         [ "$write_bytes" ] || error "Missing write_bytes stats"
10112         [ "$read_bytes" != 0 ] || error "no read done"
10113         [ "$write_bytes" != 0 ] || error "no write done"
10114
10115         rm -f $TMP/${tfile}.tmp
10116 }
10117 run_test 127b "verify the llite client stats are sane"
10118
10119 test_128() { # bug 15212
10120         touch $DIR/$tfile
10121         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
10122                 find $DIR/$tfile
10123                 find $DIR/$tfile
10124         EOF
10125
10126         result=$(grep error $TMP/$tfile.log)
10127         rm -f $DIR/$tfile $TMP/$tfile.log
10128         [ -z "$result" ] ||
10129                 error "consecutive find's under interactive lfs failed"
10130 }
10131 run_test 128 "interactive lfs for 2 consecutive find's"
10132
10133 set_dir_limits () {
10134         local mntdev
10135         local canondev
10136         local node
10137
10138         local ldproc=/proc/fs/ldiskfs
10139         local facets=$(get_facets MDS)
10140
10141         for facet in ${facets//,/ }; do
10142                 canondev=$(ldiskfs_canon \
10143                            *.$(convert_facet2label $facet).mntdev $facet)
10144                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
10145                         ldproc=/sys/fs/ldiskfs
10146                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
10147                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
10148         done
10149 }
10150
10151 check_mds_dmesg() {
10152         local facets=$(get_facets MDS)
10153         for facet in ${facets//,/ }; do
10154                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
10155         done
10156         return 1
10157 }
10158
10159 test_129() {
10160         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10161         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
10162                 skip "Need MDS version with at least 2.5.56"
10163         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
10164                 skip_env "ldiskfs only test"
10165         fi
10166         remote_mds_nodsh && skip "remote MDS with nodsh"
10167
10168         local ENOSPC=28
10169         local EFBIG=27
10170         local has_warning=false
10171
10172         rm -rf $DIR/$tdir
10173         mkdir -p $DIR/$tdir
10174
10175         # block size of mds1
10176         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
10177         set_dir_limits $maxsize $maxsize
10178         local dirsize=$(stat -c%s "$DIR/$tdir")
10179         local nfiles=0
10180         while [[ $dirsize -le $maxsize ]]; do
10181                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
10182                 rc=$?
10183                 if ! $has_warning; then
10184                         check_mds_dmesg '"is approaching"' && has_warning=true
10185                 fi
10186                 # check two errors:
10187                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
10188                 # EFBIG for previous versions included in ldiskfs series
10189                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
10190                         set_dir_limits 0 0
10191                         echo "return code $rc received as expected"
10192
10193                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
10194                                 error_exit "create failed w/o dir size limit"
10195
10196                         check_mds_dmesg '"has reached"' ||
10197                                 error_exit "reached message should be output"
10198
10199                         [ $has_warning = "false" ] &&
10200                                 error_exit "warning message should be output"
10201
10202                         dirsize=$(stat -c%s "$DIR/$tdir")
10203
10204                         [[ $dirsize -ge $maxsize ]] && return 0
10205                         error_exit "current dir size $dirsize, " \
10206                                    "previous limit $maxsize"
10207                 elif [ $rc -ne 0 ]; then
10208                         set_dir_limits 0 0
10209                         error_exit "return $rc received instead of expected " \
10210                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
10211                 fi
10212                 nfiles=$((nfiles + 1))
10213                 dirsize=$(stat -c%s "$DIR/$tdir")
10214         done
10215
10216         set_dir_limits 0 0
10217         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
10218 }
10219 run_test 129 "test directory size limit ========================"
10220
10221 OLDIFS="$IFS"
10222 cleanup_130() {
10223         trap 0
10224         IFS="$OLDIFS"
10225 }
10226
10227 test_130a() {
10228         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10229         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10230
10231         trap cleanup_130 EXIT RETURN
10232
10233         local fm_file=$DIR/$tfile
10234         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
10235         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
10236                 error "dd failed for $fm_file"
10237
10238         # LU-1795: test filefrag/FIEMAP once, even if unsupported
10239         filefrag -ves $fm_file
10240         RC=$?
10241         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10242                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10243         [ $RC != 0 ] && error "filefrag $fm_file failed"
10244
10245         filefrag_op=$(filefrag -ve -k $fm_file |
10246                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10247         lun=$($GETSTRIPE -i $fm_file)
10248
10249         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
10250         IFS=$'\n'
10251         tot_len=0
10252         for line in $filefrag_op
10253         do
10254                 frag_lun=`echo $line | cut -d: -f5`
10255                 ext_len=`echo $line | cut -d: -f4`
10256                 if (( $frag_lun != $lun )); then
10257                         cleanup_130
10258                         error "FIEMAP on 1-stripe file($fm_file) failed"
10259                         return
10260                 fi
10261                 (( tot_len += ext_len ))
10262         done
10263
10264         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
10265                 cleanup_130
10266                 error "FIEMAP on 1-stripe file($fm_file) failed;"
10267                 return
10268         fi
10269
10270         cleanup_130
10271
10272         echo "FIEMAP on single striped file succeeded"
10273 }
10274 run_test 130a "FIEMAP (1-stripe file)"
10275
10276 test_130b() {
10277         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
10278
10279         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10280         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10281
10282         trap cleanup_130 EXIT RETURN
10283
10284         local fm_file=$DIR/$tfile
10285         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
10286                         error "setstripe on $fm_file"
10287         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10288                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10289
10290         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
10291                 error "dd failed on $fm_file"
10292
10293         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10294         filefrag_op=$(filefrag -ve -k $fm_file |
10295                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10296
10297         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10298                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10299
10300         IFS=$'\n'
10301         tot_len=0
10302         num_luns=1
10303         for line in $filefrag_op
10304         do
10305                 frag_lun=$(echo $line | cut -d: -f5 |
10306                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10307                 ext_len=$(echo $line | cut -d: -f4)
10308                 if (( $frag_lun != $last_lun )); then
10309                         if (( tot_len != 1024 )); then
10310                                 cleanup_130
10311                                 error "FIEMAP on $fm_file failed; returned " \
10312                                 "len $tot_len for OST $last_lun instead of 1024"
10313                                 return
10314                         else
10315                                 (( num_luns += 1 ))
10316                                 tot_len=0
10317                         fi
10318                 fi
10319                 (( tot_len += ext_len ))
10320                 last_lun=$frag_lun
10321         done
10322         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
10323                 cleanup_130
10324                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10325                         "luns or wrong len for OST $last_lun"
10326                 return
10327         fi
10328
10329         cleanup_130
10330
10331         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
10332 }
10333 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
10334
10335 test_130c() {
10336         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10337
10338         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10339         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10340
10341         trap cleanup_130 EXIT RETURN
10342
10343         local fm_file=$DIR/$tfile
10344         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
10345         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10346                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10347
10348         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
10349                         error "dd failed on $fm_file"
10350
10351         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10352         filefrag_op=$(filefrag -ve -k $fm_file |
10353                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10354
10355         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10356                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10357
10358         IFS=$'\n'
10359         tot_len=0
10360         num_luns=1
10361         for line in $filefrag_op
10362         do
10363                 frag_lun=$(echo $line | cut -d: -f5 |
10364                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10365                 ext_len=$(echo $line | cut -d: -f4)
10366                 if (( $frag_lun != $last_lun )); then
10367                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
10368                         if (( logical != 512 )); then
10369                                 cleanup_130
10370                                 error "FIEMAP on $fm_file failed; returned " \
10371                                 "logical start for lun $logical instead of 512"
10372                                 return
10373                         fi
10374                         if (( tot_len != 512 )); then
10375                                 cleanup_130
10376                                 error "FIEMAP on $fm_file failed; returned " \
10377                                 "len $tot_len for OST $last_lun instead of 1024"
10378                                 return
10379                         else
10380                                 (( num_luns += 1 ))
10381                                 tot_len=0
10382                         fi
10383                 fi
10384                 (( tot_len += ext_len ))
10385                 last_lun=$frag_lun
10386         done
10387         if (( num_luns != 2 || tot_len != 512 )); then
10388                 cleanup_130
10389                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10390                         "luns or wrong len for OST $last_lun"
10391                 return
10392         fi
10393
10394         cleanup_130
10395
10396         echo "FIEMAP on 2-stripe file with hole succeeded"
10397 }
10398 run_test 130c "FIEMAP (2-stripe file with hole)"
10399
10400 test_130d() {
10401         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
10402
10403         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10404         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10405
10406         trap cleanup_130 EXIT RETURN
10407
10408         local fm_file=$DIR/$tfile
10409         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
10410                         error "setstripe on $fm_file"
10411         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10412                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10413
10414         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
10415         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
10416                 error "dd failed on $fm_file"
10417
10418         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10419         filefrag_op=$(filefrag -ve -k $fm_file |
10420                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10421
10422         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10423                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10424
10425         IFS=$'\n'
10426         tot_len=0
10427         num_luns=1
10428         for line in $filefrag_op
10429         do
10430                 frag_lun=$(echo $line | cut -d: -f5 |
10431                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10432                 ext_len=$(echo $line | cut -d: -f4)
10433                 if (( $frag_lun != $last_lun )); then
10434                         if (( tot_len != 1024 )); then
10435                                 cleanup_130
10436                                 error "FIEMAP on $fm_file failed; returned " \
10437                                 "len $tot_len for OST $last_lun instead of 1024"
10438                                 return
10439                         else
10440                                 (( num_luns += 1 ))
10441                                 tot_len=0
10442                         fi
10443                 fi
10444                 (( tot_len += ext_len ))
10445                 last_lun=$frag_lun
10446         done
10447         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
10448                 cleanup_130
10449                 error "FIEMAP on $fm_file failed; returned wrong number of " \
10450                         "luns or wrong len for OST $last_lun"
10451                 return
10452         fi
10453
10454         cleanup_130
10455
10456         echo "FIEMAP on N-stripe file succeeded"
10457 }
10458 run_test 130d "FIEMAP (N-stripe file)"
10459
10460 test_130e() {
10461         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10462
10463         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10464         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
10465
10466         trap cleanup_130 EXIT RETURN
10467
10468         local fm_file=$DIR/$tfile
10469         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
10470         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
10471                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
10472
10473         NUM_BLKS=512
10474         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
10475         for ((i = 0; i < $NUM_BLKS; i++))
10476         do
10477                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
10478         done
10479
10480         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10481         filefrag_op=$(filefrag -ve -k $fm_file |
10482                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10483
10484         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10485                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10486
10487         IFS=$'\n'
10488         tot_len=0
10489         num_luns=1
10490         for line in $filefrag_op
10491         do
10492                 frag_lun=$(echo $line | cut -d: -f5 |
10493                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10494                 ext_len=$(echo $line | cut -d: -f4)
10495                 if (( $frag_lun != $last_lun )); then
10496                         if (( tot_len != $EXPECTED_LEN )); then
10497                                 cleanup_130
10498                                 error "FIEMAP on $fm_file failed; returned " \
10499                                 "len $tot_len for OST $last_lun instead " \
10500                                 "of $EXPECTED_LEN"
10501                                 return
10502                         else
10503                                 (( num_luns += 1 ))
10504                                 tot_len=0
10505                         fi
10506                 fi
10507                 (( tot_len += ext_len ))
10508                 last_lun=$frag_lun
10509         done
10510         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
10511                 cleanup_130
10512                 error "FIEMAP on $fm_file failed; returned wrong number " \
10513                         "of luns or wrong len for OST $last_lun"
10514                 return
10515         fi
10516
10517         cleanup_130
10518
10519         echo "FIEMAP with continuation calls succeeded"
10520 }
10521 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
10522
10523 test_130f() {
10524         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10525         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
10526
10527         local fm_file=$DIR/$tfile
10528         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
10529                 error "multiop create with lov_delay_create on $fm_file"
10530
10531         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10532         filefrag_extents=$(filefrag -vek $fm_file |
10533                            awk '/extents? found/ { print $2 }')
10534         if [[ "$filefrag_extents" != "0" ]]; then
10535                 error "FIEMAP on $fm_file failed; " \
10536                       "returned $filefrag_extents expected 0"
10537         fi
10538
10539         rm -f $fm_file
10540 }
10541 run_test 130f "FIEMAP (unstriped file)"
10542
10543 # Test for writev/readv
10544 test_131a() {
10545         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
10546                 error "writev test failed"
10547         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
10548                 error "readv failed"
10549         rm -f $DIR/$tfile
10550 }
10551 run_test 131a "test iov's crossing stripe boundary for writev/readv"
10552
10553 test_131b() {
10554         local fsize=$((524288 + 1048576 + 1572864))
10555         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
10556                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10557                         error "append writev test failed"
10558
10559         ((fsize += 1572864 + 1048576))
10560         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
10561                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10562                         error "append writev test failed"
10563         rm -f $DIR/$tfile
10564 }
10565 run_test 131b "test append writev"
10566
10567 test_131c() {
10568         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
10569         error "NOT PASS"
10570 }
10571 run_test 131c "test read/write on file w/o objects"
10572
10573 test_131d() {
10574         rwv -f $DIR/$tfile -w -n 1 1572864
10575         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
10576         if [ "$NOB" != 1572864 ]; then
10577                 error "Short read filed: read $NOB bytes instead of 1572864"
10578         fi
10579         rm -f $DIR/$tfile
10580 }
10581 run_test 131d "test short read"
10582
10583 test_131e() {
10584         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
10585         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
10586         error "read hitting hole failed"
10587         rm -f $DIR/$tfile
10588 }
10589 run_test 131e "test read hitting hole"
10590
10591 check_stats() {
10592         local facet=$1
10593         local op=$2
10594         local want=${3:-0}
10595         local res
10596
10597         case $facet in
10598         mds*) res=$(do_facet $facet \
10599                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
10600                  ;;
10601         ost*) res=$(do_facet $facet \
10602                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
10603                  ;;
10604         *) error "Wrong facet '$facet'" ;;
10605         esac
10606         echo $res
10607         [ "$res" ] || error "The counter for $op on $facet was not incremented"
10608         # if the argument $3 is zero, it means any stat increment is ok.
10609         if [[ $want -gt 0 ]]; then
10610                 local count=$(echo $res | awk '{ print $2 }')
10611                 [[ $count -ne $want ]] &&
10612                         error "The $op counter on $facet is $count, not $want"
10613         fi
10614 }
10615
10616 test_133a() {
10617         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10618         remote_ost_nodsh && skip "remote OST with nodsh"
10619         remote_mds_nodsh && skip "remote MDS with nodsh"
10620         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10621                 skip_env "MDS doesn't support rename stats"
10622
10623         local testdir=$DIR/${tdir}/stats_testdir
10624
10625         mkdir -p $DIR/${tdir}
10626
10627         # clear stats.
10628         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10629         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10630
10631         # verify mdt stats first.
10632         mkdir ${testdir} || error "mkdir failed"
10633         check_stats $SINGLEMDS "mkdir" 1
10634         touch ${testdir}/${tfile} || error "touch failed"
10635         check_stats $SINGLEMDS "open" 1
10636         check_stats $SINGLEMDS "close" 1
10637         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
10638                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
10639                 check_stats $SINGLEMDS "mknod" 2
10640         }
10641         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
10642         check_stats $SINGLEMDS "unlink" 1
10643         rm -f ${testdir}/${tfile} || error "file remove failed"
10644         check_stats $SINGLEMDS "unlink" 2
10645
10646         # remove working dir and check mdt stats again.
10647         rmdir ${testdir} || error "rmdir failed"
10648         check_stats $SINGLEMDS "rmdir" 1
10649
10650         local testdir1=$DIR/${tdir}/stats_testdir1
10651         mkdir -p ${testdir}
10652         mkdir -p ${testdir1}
10653         touch ${testdir1}/test1
10654         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
10655         check_stats $SINGLEMDS "crossdir_rename" 1
10656
10657         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
10658         check_stats $SINGLEMDS "samedir_rename" 1
10659
10660         rm -rf $DIR/${tdir}
10661 }
10662 run_test 133a "Verifying MDT stats ========================================"
10663
10664 test_133b() {
10665         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10666         remote_ost_nodsh && skip "remote OST with nodsh"
10667         remote_mds_nodsh && skip "remote MDS with nodsh"
10668
10669         local testdir=$DIR/${tdir}/stats_testdir
10670
10671         mkdir -p ${testdir} || error "mkdir failed"
10672         touch ${testdir}/${tfile} || error "touch failed"
10673         cancel_lru_locks mdc
10674
10675         # clear stats.
10676         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10677         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10678
10679         # extra mdt stats verification.
10680         chmod 444 ${testdir}/${tfile} || error "chmod failed"
10681         check_stats $SINGLEMDS "setattr" 1
10682         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10683         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
10684         then            # LU-1740
10685                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
10686                 check_stats $SINGLEMDS "getattr" 1
10687         fi
10688         $LFS df || error "lfs failed"
10689         check_stats $SINGLEMDS "statfs" 1
10690
10691         rm -rf $DIR/${tdir}
10692 }
10693 run_test 133b "Verifying extra MDT stats =================================="
10694
10695 test_133c() {
10696         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10697         remote_ost_nodsh && skip "remote OST with nodsh"
10698         remote_mds_nodsh && skip "remote MDS with nodsh"
10699
10700         local testdir=$DIR/$tdir/stats_testdir
10701
10702         test_mkdir -p $testdir
10703
10704         # verify obdfilter stats.
10705         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
10706         sync
10707         cancel_lru_locks osc
10708         wait_delete_completed
10709
10710         # clear stats.
10711         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10712         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10713
10714         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
10715                 error "dd failed"
10716         sync
10717         cancel_lru_locks osc
10718         check_stats ost1 "write" 1
10719
10720         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
10721         check_stats ost1 "read" 1
10722
10723         > $testdir/$tfile || error "truncate failed"
10724         check_stats ost1 "punch" 1
10725
10726         rm -f $testdir/$tfile || error "file remove failed"
10727         wait_delete_completed
10728         check_stats ost1 "destroy" 1
10729
10730         rm -rf $DIR/$tdir
10731 }
10732 run_test 133c "Verifying OST stats ========================================"
10733
10734 order_2() {
10735         local value=$1
10736         local orig=$value
10737         local order=1
10738
10739         while [ $value -ge 2 ]; do
10740                 order=$((order*2))
10741                 value=$((value/2))
10742         done
10743
10744         if [ $orig -gt $order ]; then
10745                 order=$((order*2))
10746         fi
10747         echo $order
10748 }
10749
10750 size_in_KMGT() {
10751     local value=$1
10752     local size=('K' 'M' 'G' 'T');
10753     local i=0
10754     local size_string=$value
10755
10756     while [ $value -ge 1024 ]; do
10757         if [ $i -gt 3 ]; then
10758             #T is the biggest unit we get here, if that is bigger,
10759             #just return XXXT
10760             size_string=${value}T
10761             break
10762         fi
10763         value=$((value >> 10))
10764         if [ $value -lt 1024 ]; then
10765             size_string=${value}${size[$i]}
10766             break
10767         fi
10768         i=$((i + 1))
10769     done
10770
10771     echo $size_string
10772 }
10773
10774 get_rename_size() {
10775         local size=$1
10776         local context=${2:-.}
10777         local sample=$(do_facet $SINGLEMDS $LCTL \
10778                 get_param mdt.$FSNAME-MDT0000.rename_stats |
10779                 grep -A1 $context |
10780                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
10781         echo $sample
10782 }
10783
10784 test_133d() {
10785         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10786         remote_ost_nodsh && skip "remote OST with nodsh"
10787         remote_mds_nodsh && skip "remote MDS with nodsh"
10788         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10789                 skip_env "MDS doesn't support rename stats"
10790
10791         local testdir1=$DIR/${tdir}/stats_testdir1
10792         local testdir2=$DIR/${tdir}/stats_testdir2
10793         mkdir -p $DIR/${tdir}
10794
10795         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10796
10797         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
10798         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
10799
10800         createmany -o $testdir1/test 512 || error "createmany failed"
10801
10802         # check samedir rename size
10803         mv ${testdir1}/test0 ${testdir1}/test_0
10804
10805         local testdir1_size=$(ls -l $DIR/${tdir} |
10806                 awk '/stats_testdir1/ {print $5}')
10807         local testdir2_size=$(ls -l $DIR/${tdir} |
10808                 awk '/stats_testdir2/ {print $5}')
10809
10810         testdir1_size=$(order_2 $testdir1_size)
10811         testdir2_size=$(order_2 $testdir2_size)
10812
10813         testdir1_size=$(size_in_KMGT $testdir1_size)
10814         testdir2_size=$(size_in_KMGT $testdir2_size)
10815
10816         echo "source rename dir size: ${testdir1_size}"
10817         echo "target rename dir size: ${testdir2_size}"
10818
10819         local cmd="do_facet $SINGLEMDS $LCTL "
10820         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
10821
10822         eval $cmd || error "$cmd failed"
10823         local samedir=$($cmd | grep 'same_dir')
10824         local same_sample=$(get_rename_size $testdir1_size)
10825         [ -z "$samedir" ] && error "samedir_rename_size count error"
10826         [[ $same_sample -eq 1 ]] ||
10827                 error "samedir_rename_size error $same_sample"
10828         echo "Check same dir rename stats success"
10829
10830         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10831
10832         # check crossdir rename size
10833         mv ${testdir1}/test_0 ${testdir2}/test_0
10834
10835         testdir1_size=$(ls -l $DIR/${tdir} |
10836                 awk '/stats_testdir1/ {print $5}')
10837         testdir2_size=$(ls -l $DIR/${tdir} |
10838                 awk '/stats_testdir2/ {print $5}')
10839
10840         testdir1_size=$(order_2 $testdir1_size)
10841         testdir2_size=$(order_2 $testdir2_size)
10842
10843         testdir1_size=$(size_in_KMGT $testdir1_size)
10844         testdir2_size=$(size_in_KMGT $testdir2_size)
10845
10846         echo "source rename dir size: ${testdir1_size}"
10847         echo "target rename dir size: ${testdir2_size}"
10848
10849         eval $cmd || error "$cmd failed"
10850         local crossdir=$($cmd | grep 'crossdir')
10851         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10852         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10853         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10854         [[ $src_sample -eq 1 ]] ||
10855                 error "crossdir_rename_size error $src_sample"
10856         [[ $tgt_sample -eq 1 ]] ||
10857                 error "crossdir_rename_size error $tgt_sample"
10858         echo "Check cross dir rename stats success"
10859         rm -rf $DIR/${tdir}
10860 }
10861 run_test 133d "Verifying rename_stats ========================================"
10862
10863 test_133e() {
10864         remote_mds_nodsh && skip "remote MDS with nodsh"
10865         remote_ost_nodsh && skip "remote OST with nodsh"
10866         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10867
10868         local testdir=$DIR/${tdir}/stats_testdir
10869         local ctr f0 f1 bs=32768 count=42 sum
10870
10871         mkdir -p ${testdir} || error "mkdir failed"
10872
10873         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10874
10875         for ctr in {write,read}_bytes; do
10876                 sync
10877                 cancel_lru_locks osc
10878
10879                 do_facet ost1 $LCTL set_param -n \
10880                         "obdfilter.*.exports.clear=clear"
10881
10882                 if [ $ctr = write_bytes ]; then
10883                         f0=/dev/zero
10884                         f1=${testdir}/${tfile}
10885                 else
10886                         f0=${testdir}/${tfile}
10887                         f1=/dev/null
10888                 fi
10889
10890                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10891                         error "dd failed"
10892                 sync
10893                 cancel_lru_locks osc
10894
10895                 sum=$(do_facet ost1 $LCTL get_param \
10896                         "obdfilter.*.exports.*.stats" |
10897                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10898                                 $1 == ctr { sum += $7 }
10899                                 END { printf("%0.0f", sum) }')
10900
10901                 if ((sum != bs * count)); then
10902                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10903                 fi
10904         done
10905
10906         rm -rf $DIR/${tdir}
10907 }
10908 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10909
10910 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10911
10912 test_133f() {
10913         # First without trusting modes.
10914         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10915         echo "proc_dirs='$proc_dirs'"
10916         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10917         find $proc_dirs -exec cat '{}' \; &> /dev/null
10918
10919         # Second verifying readability.
10920         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10921
10922         # Verifing writability with badarea_io.
10923         find $proc_dirs \
10924                 -ignore_readdir_race \
10925                 -type f \
10926                 -not -name force_lbug \
10927                 -not -name changelog_mask \
10928                 -exec badarea_io '{}' \; ||
10929                         error "find $proc_dirs failed"
10930 }
10931 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
10932
10933 test_133g() {
10934         remote_mds_nodsh && skip "remote MDS with nodsh"
10935         remote_ost_nodsh && skip "remote OST with nodsh"
10936
10937         # eventually, this can also be replaced with "lctl get_param -R",
10938         # but not until that option is always available on the server
10939         local facet
10940         for facet in mds1 ost1; do
10941                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10942                         skip_noexit "Too old lustre on $facet"
10943                 local facet_proc_dirs=$(do_facet $facet \
10944                                         \\\ls -d $proc_regexp 2>/dev/null)
10945                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10946                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10947                 do_facet $facet find $facet_proc_dirs \
10948                         ! -name req_history \
10949                         -exec cat '{}' \\\; &> /dev/null
10950
10951                 do_facet $facet find $facet_proc_dirs \
10952                         ! -name req_history \
10953                         -type f \
10954                         -exec cat '{}' \\\; &> /dev/null ||
10955                                 error "proc file read failed"
10956
10957                 do_facet $facet find $facet_proc_dirs \
10958                         -ignore_readdir_race \
10959                         -type f \
10960                         -not -name force_lbug \
10961                         -not -name changelog_mask \
10962                         -exec badarea_io '{}' \\\; ||
10963                                 error "$facet find $facet_proc_dirs failed"
10964         done
10965
10966         # remount the FS in case writes/reads /proc break the FS
10967         cleanup || error "failed to unmount"
10968         setup || error "failed to setup"
10969         true
10970 }
10971 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
10972
10973 test_133h() {
10974         remote_mds_nodsh && skip "remote MDS with nodsh"
10975         remote_ost_nodsh && skip "remote OST with nodsh"
10976         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10977                 skip "Need MDS version at least 2.9.54"
10978
10979         local facet
10980
10981         for facet in client mds1 ost1; do
10982                 local facet_proc_dirs=$(do_facet $facet \
10983                                         \\\ls -d $proc_regexp 2> /dev/null)
10984                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10985                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10986                 # Get the list of files that are missing the terminating newline
10987                 local missing=($(do_facet $facet \
10988                         find ${facet_proc_dirs} -type f \|              \
10989                                 while read F\; do                       \
10990                                         awk -v FS='\v' -v RS='\v\v'     \
10991                                         "'END { if(NR>0 &&              \
10992                                         \\\$NF !~ /.*\\\n\$/)           \
10993                                                 print FILENAME}'"       \
10994                                         '\$F'\;                         \
10995                                 done 2>/dev/null))
10996                 [ ${#missing[*]} -eq 0 ] ||
10997                         error "files do not end with newline: ${missing[*]}"
10998         done
10999 }
11000 run_test 133h "Proc files should end with newlines"
11001
11002 test_134a() {
11003         remote_mds_nodsh && skip "remote MDS with nodsh"
11004         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
11005                 skip "Need MDS version at least 2.7.54"
11006
11007         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11008         cancel_lru_locks mdc
11009
11010         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11011         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11012         [ $unused -eq 0 ] || error "$unused locks are not cleared"
11013
11014         local nr=1000
11015         createmany -o $DIR/$tdir/f $nr ||
11016                 error "failed to create $nr files in $DIR/$tdir"
11017         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11018
11019         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
11020         do_facet mds1 $LCTL set_param fail_loc=0x327
11021         do_facet mds1 $LCTL set_param fail_val=500
11022         touch $DIR/$tdir/m
11023
11024         echo "sleep 10 seconds ..."
11025         sleep 10
11026         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
11027
11028         do_facet mds1 $LCTL set_param fail_loc=0
11029         do_facet mds1 $LCTL set_param fail_val=0
11030         [ $lck_cnt -lt $unused ] ||
11031                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
11032
11033         rm $DIR/$tdir/m
11034         unlinkmany $DIR/$tdir/f $nr
11035 }
11036 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
11037
11038 test_134b() {
11039         remote_mds_nodsh && skip "remote MDS with nodsh"
11040         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
11041                 skip "Need MDS version at least 2.7.54"
11042
11043         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11044         cancel_lru_locks mdc
11045
11046         local low_wm=$(do_facet mds1 $LCTL get_param -n \
11047                         ldlm.lock_reclaim_threshold_mb)
11048         # disable reclaim temporarily
11049         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
11050
11051         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
11052         do_facet mds1 $LCTL set_param fail_loc=0x328
11053         do_facet mds1 $LCTL set_param fail_val=500
11054
11055         $LCTL set_param debug=+trace
11056
11057         local nr=600
11058         createmany -o $DIR/$tdir/f $nr &
11059         local create_pid=$!
11060
11061         echo "Sleep $TIMEOUT seconds ..."
11062         sleep $TIMEOUT
11063         if ! ps -p $create_pid  > /dev/null 2>&1; then
11064                 do_facet mds1 $LCTL set_param fail_loc=0
11065                 do_facet mds1 $LCTL set_param fail_val=0
11066                 do_facet mds1 $LCTL set_param \
11067                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
11068                 error "createmany finished incorrectly!"
11069         fi
11070         do_facet mds1 $LCTL set_param fail_loc=0
11071         do_facet mds1 $LCTL set_param fail_val=0
11072         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
11073         wait $create_pid || return 1
11074
11075         unlinkmany $DIR/$tdir/f $nr
11076 }
11077 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
11078
11079 test_140() { #bug-17379
11080         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11081
11082         test_mkdir $DIR/$tdir
11083         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
11084         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
11085
11086         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
11087         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
11088         local i=0
11089         while i=$((i + 1)); do
11090                 test_mkdir $i
11091                 cd $i || error "Changing to $i"
11092                 ln -s ../stat stat || error "Creating stat symlink"
11093                 # Read the symlink until ELOOP present,
11094                 # not LBUGing the system is considered success,
11095                 # we didn't overrun the stack.
11096                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
11097                 if [ $ret -ne 0 ]; then
11098                         if [ $ret -eq 40 ]; then
11099                                 break  # -ELOOP
11100                         else
11101                                 error "Open stat symlink"
11102                                         return
11103                         fi
11104                 fi
11105         done
11106         i=$((i - 1))
11107         echo "The symlink depth = $i"
11108         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
11109                                         error "Invalid symlink depth"
11110
11111         # Test recursive symlink
11112         ln -s symlink_self symlink_self
11113         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
11114         echo "open symlink_self returns $ret"
11115         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
11116 }
11117 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
11118
11119 test_150() {
11120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11121
11122         local TF="$TMP/$tfile"
11123
11124         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11125         cp $TF $DIR/$tfile
11126         cancel_lru_locks $OSC
11127         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
11128         remount_client $MOUNT
11129         df -P $MOUNT
11130         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
11131
11132         $TRUNCATE $TF 6000
11133         $TRUNCATE $DIR/$tfile 6000
11134         cancel_lru_locks $OSC
11135         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
11136
11137         echo "12345" >>$TF
11138         echo "12345" >>$DIR/$tfile
11139         cancel_lru_locks $OSC
11140         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
11141
11142         echo "12345" >>$TF
11143         echo "12345" >>$DIR/$tfile
11144         cancel_lru_locks $OSC
11145         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
11146
11147         rm -f $TF
11148         true
11149 }
11150 run_test 150 "truncate/append tests"
11151
11152 #LU-2902 roc_hit was not able to read all values from lproc
11153 function roc_hit_init() {
11154         local list=$(comma_list $(osts_nodes))
11155         local dir=$DIR/$tdir-check
11156         local file=$dir/$tfile
11157         local BEFORE
11158         local AFTER
11159         local idx
11160
11161         test_mkdir $dir
11162         #use setstripe to do a write to every ost
11163         for i in $(seq 0 $((OSTCOUNT-1))); do
11164                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
11165                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
11166                 idx=$(printf %04x $i)
11167                 BEFORE=$(get_osd_param $list *OST*$idx stats |
11168                         awk '$1 == "cache_access" {sum += $7}
11169                                 END { printf("%0.0f", sum) }')
11170
11171                 cancel_lru_locks osc
11172                 cat $file >/dev/null
11173
11174                 AFTER=$(get_osd_param $list *OST*$idx stats |
11175                         awk '$1 == "cache_access" {sum += $7}
11176                                 END { printf("%0.0f", sum) }')
11177
11178                 echo BEFORE:$BEFORE AFTER:$AFTER
11179                 if ! let "AFTER - BEFORE == 4"; then
11180                         rm -rf $dir
11181                         error "roc_hit is not safe to use"
11182                 fi
11183                 rm $file
11184         done
11185
11186         rm -rf $dir
11187 }
11188
11189 function roc_hit() {
11190         local list=$(comma_list $(osts_nodes))
11191         echo $(get_osd_param $list '' stats |
11192                 awk '$1 == "cache_hit" {sum += $7}
11193                         END { printf("%0.0f", sum) }')
11194 }
11195
11196 function set_cache() {
11197         local on=1
11198
11199         if [ "$2" == "off" ]; then
11200                 on=0;
11201         fi
11202         local list=$(comma_list $(osts_nodes))
11203         set_osd_param $list '' $1_cache_enable $on
11204
11205         cancel_lru_locks osc
11206 }
11207
11208 test_151() {
11209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11210         remote_ost_nodsh && skip "remote OST with nodsh"
11211
11212         local CPAGES=3
11213         local list=$(comma_list $(osts_nodes))
11214
11215         # check whether obdfilter is cache capable at all
11216         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
11217                 skip "not cache-capable obdfilter"
11218         fi
11219
11220         # check cache is enabled on all obdfilters
11221         if get_osd_param $list '' read_cache_enable | grep 0; then
11222                 skip "oss cache is disabled"
11223         fi
11224
11225         set_osd_param $list '' writethrough_cache_enable 1
11226
11227         # check write cache is enabled on all obdfilters
11228         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
11229                 skip "oss write cache is NOT enabled"
11230         fi
11231
11232         roc_hit_init
11233
11234         #define OBD_FAIL_OBD_NO_LRU  0x609
11235         do_nodes $list $LCTL set_param fail_loc=0x609
11236
11237         # pages should be in the case right after write
11238         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
11239                 error "dd failed"
11240
11241         local BEFORE=$(roc_hit)
11242         cancel_lru_locks osc
11243         cat $DIR/$tfile >/dev/null
11244         local AFTER=$(roc_hit)
11245
11246         do_nodes $list $LCTL set_param fail_loc=0
11247
11248         if ! let "AFTER - BEFORE == CPAGES"; then
11249                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11250         fi
11251
11252         # the following read invalidates the cache
11253         cancel_lru_locks osc
11254         set_osd_param $list '' read_cache_enable 0
11255         cat $DIR/$tfile >/dev/null
11256
11257         # now data shouldn't be found in the cache
11258         BEFORE=$(roc_hit)
11259         cancel_lru_locks osc
11260         cat $DIR/$tfile >/dev/null
11261         AFTER=$(roc_hit)
11262         if let "AFTER - BEFORE != 0"; then
11263                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11264         fi
11265
11266         set_osd_param $list '' read_cache_enable 1
11267         rm -f $DIR/$tfile
11268 }
11269 run_test 151 "test cache on oss and controls ==============================="
11270
11271 test_152() {
11272         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11273
11274         local TF="$TMP/$tfile"
11275
11276         # simulate ENOMEM during write
11277 #define OBD_FAIL_OST_NOMEM      0x226
11278         lctl set_param fail_loc=0x80000226
11279         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11280         cp $TF $DIR/$tfile
11281         sync || error "sync failed"
11282         lctl set_param fail_loc=0
11283
11284         # discard client's cache
11285         cancel_lru_locks osc
11286
11287         # simulate ENOMEM during read
11288         lctl set_param fail_loc=0x80000226
11289         cmp $TF $DIR/$tfile || error "cmp failed"
11290         lctl set_param fail_loc=0
11291
11292         rm -f $TF
11293 }
11294 run_test 152 "test read/write with enomem ============================"
11295
11296 test_153() {
11297         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
11298 }
11299 run_test 153 "test if fdatasync does not crash ======================="
11300
11301 dot_lustre_fid_permission_check() {
11302         local fid=$1
11303         local ffid=$MOUNT/.lustre/fid/$fid
11304         local test_dir=$2
11305
11306         echo "stat fid $fid"
11307         stat $ffid > /dev/null || error "stat $ffid failed."
11308         echo "touch fid $fid"
11309         touch $ffid || error "touch $ffid failed."
11310         echo "write to fid $fid"
11311         cat /etc/hosts > $ffid || error "write $ffid failed."
11312         echo "read fid $fid"
11313         diff /etc/hosts $ffid || error "read $ffid failed."
11314         echo "append write to fid $fid"
11315         cat /etc/hosts >> $ffid || error "append write $ffid failed."
11316         echo "rename fid $fid"
11317         mv $ffid $test_dir/$tfile.1 &&
11318                 error "rename $ffid to $tfile.1 should fail."
11319         touch $test_dir/$tfile.1
11320         mv $test_dir/$tfile.1 $ffid &&
11321                 error "rename $tfile.1 to $ffid should fail."
11322         rm -f $test_dir/$tfile.1
11323         echo "truncate fid $fid"
11324         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
11325         echo "link fid $fid"
11326         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
11327         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
11328                 echo "setfacl fid $fid"
11329                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
11330                 echo "getfacl fid $fid"
11331                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
11332         fi
11333         echo "unlink fid $fid"
11334         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
11335         echo "mknod fid $fid"
11336         mknod $ffid c 1 3 && error "mknod $ffid should fail."
11337
11338         fid=[0xf00000400:0x1:0x0]
11339         ffid=$MOUNT/.lustre/fid/$fid
11340
11341         echo "stat non-exist fid $fid"
11342         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
11343         echo "write to non-exist fid $fid"
11344         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
11345         echo "link new fid $fid"
11346         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
11347
11348         mkdir -p $test_dir/$tdir
11349         touch $test_dir/$tdir/$tfile
11350         fid=$($LFS path2fid $test_dir/$tdir)
11351         rc=$?
11352         [ $rc -ne 0 ] &&
11353                 error "error: could not get fid for $test_dir/$dir/$tfile."
11354
11355         ffid=$MOUNT/.lustre/fid/$fid
11356
11357         echo "ls $fid"
11358         ls $ffid > /dev/null || error "ls $ffid failed."
11359         echo "touch $fid/$tfile.1"
11360         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
11361
11362         echo "touch $MOUNT/.lustre/fid/$tfile"
11363         touch $MOUNT/.lustre/fid/$tfile && \
11364                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
11365
11366         echo "setxattr to $MOUNT/.lustre/fid"
11367         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
11368
11369         echo "listxattr for $MOUNT/.lustre/fid"
11370         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
11371
11372         echo "delxattr from $MOUNT/.lustre/fid"
11373         setfattr -x trusted.name1 $MOUNT/.lustre/fid
11374
11375         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
11376         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
11377                 error "touch invalid fid should fail."
11378
11379         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
11380         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
11381                 error "touch non-normal fid should fail."
11382
11383         echo "rename $tdir to $MOUNT/.lustre/fid"
11384         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
11385                 error "rename to $MOUNT/.lustre/fid should fail."
11386
11387         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
11388         then            # LU-3547
11389                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
11390                 local new_obf_mode=777
11391
11392                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
11393                 chmod $new_obf_mode $DIR/.lustre/fid ||
11394                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
11395
11396                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
11397                 [ $obf_mode -eq $new_obf_mode ] ||
11398                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
11399
11400                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
11401                 chmod $old_obf_mode $DIR/.lustre/fid ||
11402                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
11403         fi
11404
11405         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
11406         fid=$($LFS path2fid $test_dir/$tfile-2)
11407
11408         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
11409         then # LU-5424
11410                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
11411                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
11412                         error "create lov data thru .lustre failed"
11413         fi
11414         echo "cp /etc/passwd $test_dir/$tfile-2"
11415         cp /etc/passwd $test_dir/$tfile-2 ||
11416                 error "copy to $test_dir/$tfile-2 failed."
11417         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
11418         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
11419                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
11420
11421         rm -rf $test_dir/tfile.lnk
11422         rm -rf $test_dir/$tfile-2
11423 }
11424
11425 test_154A() {
11426         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11427                 skip "Need MDS version at least 2.4.1"
11428
11429         local tf=$DIR/$tfile
11430         touch $tf
11431
11432         local fid=$($LFS path2fid $tf)
11433         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
11434
11435         # check that we get the same pathname back
11436         local found=$($LFS fid2path $MOUNT "$fid")
11437         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
11438         [ "$found" == "$tf" ] ||
11439                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
11440 }
11441 run_test 154A "lfs path2fid and fid2path basic checks"
11442
11443 test_154B() {
11444         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11445                 skip "Need MDS version at least 2.4.1"
11446
11447         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11448         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11449         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
11450         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
11451
11452         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
11453         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
11454
11455         # check that we get the same pathname
11456         echo "PFID: $PFID, name: $name"
11457         local FOUND=$($LFS fid2path $MOUNT "$PFID")
11458         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
11459         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
11460                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
11461
11462         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
11463 }
11464 run_test 154B "verify the ll_decode_linkea tool"
11465
11466 test_154a() {
11467         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11468         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11469         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
11470                 skip "Need MDS version at least 2.2.51"
11471         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11472
11473         cp /etc/hosts $DIR/$tfile
11474
11475         fid=$($LFS path2fid $DIR/$tfile)
11476         rc=$?
11477         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
11478
11479         dot_lustre_fid_permission_check "$fid" $DIR ||
11480                 error "dot lustre permission check $fid failed"
11481
11482         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
11483
11484         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
11485
11486         touch $MOUNT/.lustre/file &&
11487                 error "creation is not allowed under .lustre"
11488
11489         mkdir $MOUNT/.lustre/dir &&
11490                 error "mkdir is not allowed under .lustre"
11491
11492         rm -rf $DIR/$tfile
11493 }
11494 run_test 154a "Open-by-FID"
11495
11496 test_154b() {
11497         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11498         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11499         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
11500         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
11501                 skip "Need MDS version at least 2.2.51"
11502
11503         local remote_dir=$DIR/$tdir/remote_dir
11504         local MDTIDX=1
11505         local rc=0
11506
11507         mkdir -p $DIR/$tdir
11508         $LFS mkdir -i $MDTIDX $remote_dir ||
11509                 error "create remote directory failed"
11510
11511         cp /etc/hosts $remote_dir/$tfile
11512
11513         fid=$($LFS path2fid $remote_dir/$tfile)
11514         rc=$?
11515         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
11516
11517         dot_lustre_fid_permission_check "$fid" $remote_dir ||
11518                 error "dot lustre permission check $fid failed"
11519         rm -rf $DIR/$tdir
11520 }
11521 run_test 154b "Open-by-FID for remote directory"
11522
11523 test_154c() {
11524         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11525                 skip "Need MDS version at least 2.4.1"
11526
11527         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
11528         local FID1=$($LFS path2fid $DIR/$tfile.1)
11529         local FID2=$($LFS path2fid $DIR/$tfile.2)
11530         local FID3=$($LFS path2fid $DIR/$tfile.3)
11531
11532         local N=1
11533         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
11534                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
11535                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
11536                 local want=FID$N
11537                 [ "$FID" = "${!want}" ] ||
11538                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
11539                 N=$((N + 1))
11540         done
11541
11542         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
11543         do
11544                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
11545                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
11546                 N=$((N + 1))
11547         done
11548 }
11549 run_test 154c "lfs path2fid and fid2path multiple arguments"
11550
11551 test_154d() {
11552         remote_mds_nodsh && skip "remote MDS with nodsh"
11553         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
11554                 skip "Need MDS version at least 2.5.53"
11555
11556         if remote_mds; then
11557                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
11558         else
11559                 nid="0@lo"
11560         fi
11561         local proc_ofile="mdt.*.exports.'$nid'.open_files"
11562         local fd
11563         local cmd
11564
11565         rm -f $DIR/$tfile
11566         touch $DIR/$tfile
11567
11568         local fid=$($LFS path2fid $DIR/$tfile)
11569         # Open the file
11570         fd=$(free_fd)
11571         cmd="exec $fd<$DIR/$tfile"
11572         eval $cmd
11573         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
11574         echo "$fid_list" | grep "$fid"
11575         rc=$?
11576
11577         cmd="exec $fd>/dev/null"
11578         eval $cmd
11579         if [ $rc -ne 0 ]; then
11580                 error "FID $fid not found in open files list $fid_list"
11581         fi
11582 }
11583 run_test 154d "Verify open file fid"
11584
11585 test_154e()
11586 {
11587         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
11588                 skip "Need MDS version at least 2.6.50"
11589
11590         if ls -a $MOUNT | grep -q '^\.lustre$'; then
11591                 error ".lustre returned by readdir"
11592         fi
11593 }
11594 run_test 154e ".lustre is not returned by readdir"
11595
11596 test_154f() {
11597         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11598
11599         # create parent directory on a single MDT to avoid cross-MDT hardlinks
11600         test_mkdir -p -c1 $DIR/$tdir/d
11601         # test dirs inherit from its stripe
11602         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
11603         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
11604         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
11605         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
11606         touch $DIR/f
11607
11608         # get fid of parents
11609         local FID0=$($LFS path2fid $DIR/$tdir/d)
11610         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
11611         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
11612         local FID3=$($LFS path2fid $DIR)
11613
11614         # check that path2fid --parents returns expected <parent_fid>/name
11615         # 1) test for a directory (single parent)
11616         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
11617         [ "$parent" == "$FID0/foo1" ] ||
11618                 error "expected parent: $FID0/foo1, got: $parent"
11619
11620         # 2) test for a file with nlink > 1 (multiple parents)
11621         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
11622         echo "$parent" | grep -F "$FID1/$tfile" ||
11623                 error "$FID1/$tfile not returned in parent list"
11624         echo "$parent" | grep -F "$FID2/link" ||
11625                 error "$FID2/link not returned in parent list"
11626
11627         # 3) get parent by fid
11628         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
11629         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11630         echo "$parent" | grep -F "$FID1/$tfile" ||
11631                 error "$FID1/$tfile not returned in parent list (by fid)"
11632         echo "$parent" | grep -F "$FID2/link" ||
11633                 error "$FID2/link not returned in parent list (by fid)"
11634
11635         # 4) test for entry in root directory
11636         parent=$($LFS path2fid --parents $DIR/f)
11637         echo "$parent" | grep -F "$FID3/f" ||
11638                 error "$FID3/f not returned in parent list"
11639
11640         # 5) test it on root directory
11641         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
11642                 error "$MOUNT should not have parents"
11643
11644         # enable xattr caching and check that linkea is correctly updated
11645         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11646         save_lustre_params client "llite.*.xattr_cache" > $save
11647         lctl set_param llite.*.xattr_cache 1
11648
11649         # 6.1) linkea update on rename
11650         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
11651
11652         # get parents by fid
11653         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11654         # foo1 should no longer be returned in parent list
11655         echo "$parent" | grep -F "$FID1" &&
11656                 error "$FID1 should no longer be in parent list"
11657         # the new path should appear
11658         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
11659                 error "$FID2/$tfile.moved is not in parent list"
11660
11661         # 6.2) linkea update on unlink
11662         rm -f $DIR/$tdir/d/foo2/link
11663         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11664         # foo2/link should no longer be returned in parent list
11665         echo "$parent" | grep -F "$FID2/link" &&
11666                 error "$FID2/link should no longer be in parent list"
11667         true
11668
11669         rm -f $DIR/f
11670         restore_lustre_params < $save
11671         rm -f $save
11672 }
11673 run_test 154f "get parent fids by reading link ea"
11674
11675 test_154g()
11676 {
11677         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
11678         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
11679            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
11680                 skip "Need MDS version at least 2.6.92"
11681
11682         mkdir -p $DIR/$tdir
11683         llapi_fid_test -d $DIR/$tdir
11684 }
11685 run_test 154g "various llapi FID tests"
11686
11687 test_155_small_load() {
11688     local temp=$TMP/$tfile
11689     local file=$DIR/$tfile
11690
11691     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
11692         error "dd of=$temp bs=6096 count=1 failed"
11693     cp $temp $file
11694     cancel_lru_locks $OSC
11695     cmp $temp $file || error "$temp $file differ"
11696
11697     $TRUNCATE $temp 6000
11698     $TRUNCATE $file 6000
11699     cmp $temp $file || error "$temp $file differ (truncate1)"
11700
11701     echo "12345" >>$temp
11702     echo "12345" >>$file
11703     cmp $temp $file || error "$temp $file differ (append1)"
11704
11705     echo "12345" >>$temp
11706     echo "12345" >>$file
11707     cmp $temp $file || error "$temp $file differ (append2)"
11708
11709     rm -f $temp $file
11710     true
11711 }
11712
11713 test_155_big_load() {
11714         remote_ost_nodsh && skip "remote OST with nodsh"
11715
11716         local temp=$TMP/$tfile
11717         local file=$DIR/$tfile
11718
11719         free_min_max
11720         local cache_size=$(do_facet ost$((MAXI+1)) \
11721                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
11722         local large_file_size=$((cache_size * 2))
11723
11724         echo "OSS cache size: $cache_size KB"
11725         echo "Large file size: $large_file_size KB"
11726
11727         [ $MAXV -le $large_file_size ] &&
11728                 skip_env "max available OST size needs > $large_file_size KB"
11729
11730         $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
11731
11732         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
11733                 error "dd of=$temp bs=$large_file_size count=1k failed"
11734         cp $temp $file
11735         ls -lh $temp $file
11736         cancel_lru_locks osc
11737         cmp $temp $file || error "$temp $file differ"
11738
11739         rm -f $temp $file
11740         true
11741 }
11742
11743 save_writethrough() {
11744         local facets=$(get_facets OST)
11745
11746         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
11747         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
11748 }
11749
11750 test_155a() {
11751         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11752
11753         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11754
11755         save_writethrough $p
11756
11757         set_cache read on
11758         set_cache writethrough on
11759         test_155_small_load
11760         restore_lustre_params < $p
11761         rm -f $p
11762 }
11763 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
11764
11765 test_155b() {
11766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11767
11768         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11769
11770         save_writethrough $p
11771
11772         set_cache read on
11773         set_cache writethrough off
11774         test_155_small_load
11775         restore_lustre_params < $p
11776         rm -f $p
11777 }
11778 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
11779
11780 test_155c() {
11781         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11782
11783         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11784
11785         save_writethrough $p
11786
11787         set_cache read off
11788         set_cache writethrough on
11789         test_155_small_load
11790         restore_lustre_params < $p
11791         rm -f $p
11792 }
11793 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
11794
11795 test_155d() {
11796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11797
11798         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11799
11800         save_writethrough $p
11801
11802         set_cache read off
11803         set_cache writethrough off
11804         test_155_small_load
11805         restore_lustre_params < $p
11806         rm -f $p
11807 }
11808 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
11809
11810 test_155e() {
11811         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11812
11813         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11814
11815         save_writethrough $p
11816
11817         set_cache read on
11818         set_cache writethrough on
11819         test_155_big_load
11820         restore_lustre_params < $p
11821         rm -f $p
11822 }
11823 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11824
11825 test_155f() {
11826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11827
11828         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11829
11830         save_writethrough $p
11831
11832         set_cache read on
11833         set_cache writethrough off
11834         test_155_big_load
11835         restore_lustre_params < $p
11836         rm -f $p
11837 }
11838 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11839
11840 test_155g() {
11841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11842
11843         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11844
11845         save_writethrough $p
11846
11847         set_cache read off
11848         set_cache writethrough on
11849         test_155_big_load
11850         restore_lustre_params < $p
11851         rm -f $p
11852 }
11853 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11854
11855 test_155h() {
11856         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11857
11858         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11859
11860         save_writethrough $p
11861
11862         set_cache read off
11863         set_cache writethrough off
11864         test_155_big_load
11865         restore_lustre_params < $p
11866         rm -f $p
11867 }
11868 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11869
11870 test_156() {
11871         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11872         remote_ost_nodsh && skip "remote OST with nodsh"
11873         [ "$(facet_fstype ost1)" = "zfs" -a \
11874            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11875                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
11876
11877         local CPAGES=3
11878         local BEFORE
11879         local AFTER
11880         local file="$DIR/$tfile"
11881         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11882
11883         save_writethrough $p
11884         roc_hit_init
11885
11886         log "Turn on read and write cache"
11887         set_cache read on
11888         set_cache writethrough on
11889
11890         log "Write data and read it back."
11891         log "Read should be satisfied from the cache."
11892         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11893         BEFORE=$(roc_hit)
11894         cancel_lru_locks osc
11895         cat $file >/dev/null
11896         AFTER=$(roc_hit)
11897         if ! let "AFTER - BEFORE == CPAGES"; then
11898                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11899         else
11900                 log "cache hits:: before: $BEFORE, after: $AFTER"
11901         fi
11902
11903         log "Read again; it should be satisfied from the cache."
11904         BEFORE=$AFTER
11905         cancel_lru_locks osc
11906         cat $file >/dev/null
11907         AFTER=$(roc_hit)
11908         if ! let "AFTER - BEFORE == CPAGES"; then
11909                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11910         else
11911                 log "cache hits:: before: $BEFORE, after: $AFTER"
11912         fi
11913
11914         log "Turn off the read cache and turn on the write cache"
11915         set_cache read off
11916         set_cache writethrough on
11917
11918         log "Read again; it should be satisfied from the cache."
11919         BEFORE=$(roc_hit)
11920         cancel_lru_locks osc
11921         cat $file >/dev/null
11922         AFTER=$(roc_hit)
11923         if ! let "AFTER - BEFORE == CPAGES"; then
11924                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11925         else
11926                 log "cache hits:: before: $BEFORE, after: $AFTER"
11927         fi
11928
11929         log "Read again; it should not be satisfied from the cache."
11930         BEFORE=$AFTER
11931         cancel_lru_locks osc
11932         cat $file >/dev/null
11933         AFTER=$(roc_hit)
11934         if ! let "AFTER - BEFORE == 0"; then
11935                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11936         else
11937                 log "cache hits:: before: $BEFORE, after: $AFTER"
11938         fi
11939
11940         log "Write data and read it back."
11941         log "Read should be satisfied from the cache."
11942         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11943         BEFORE=$(roc_hit)
11944         cancel_lru_locks osc
11945         cat $file >/dev/null
11946         AFTER=$(roc_hit)
11947         if ! let "AFTER - BEFORE == CPAGES"; then
11948                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11949         else
11950                 log "cache hits:: before: $BEFORE, after: $AFTER"
11951         fi
11952
11953         log "Read again; it should not be satisfied from the cache."
11954         BEFORE=$AFTER
11955         cancel_lru_locks osc
11956         cat $file >/dev/null
11957         AFTER=$(roc_hit)
11958         if ! let "AFTER - BEFORE == 0"; then
11959                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11960         else
11961                 log "cache hits:: before: $BEFORE, after: $AFTER"
11962         fi
11963
11964         log "Turn off read and write cache"
11965         set_cache read off
11966         set_cache writethrough off
11967
11968         log "Write data and read it back"
11969         log "It should not be satisfied from the cache."
11970         rm -f $file
11971         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11972         cancel_lru_locks osc
11973         BEFORE=$(roc_hit)
11974         cat $file >/dev/null
11975         AFTER=$(roc_hit)
11976         if ! let "AFTER - BEFORE == 0"; then
11977                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11978         else
11979                 log "cache hits:: before: $BEFORE, after: $AFTER"
11980         fi
11981
11982         log "Turn on the read cache and turn off the write cache"
11983         set_cache read on
11984         set_cache writethrough off
11985
11986         log "Write data and read it back"
11987         log "It should not be satisfied from the cache."
11988         rm -f $file
11989         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11990         BEFORE=$(roc_hit)
11991         cancel_lru_locks osc
11992         cat $file >/dev/null
11993         AFTER=$(roc_hit)
11994         if ! let "AFTER - BEFORE == 0"; then
11995                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11996         else
11997                 log "cache hits:: before: $BEFORE, after: $AFTER"
11998         fi
11999
12000         log "Read again; it should be satisfied from the cache."
12001         BEFORE=$(roc_hit)
12002         cancel_lru_locks osc
12003         cat $file >/dev/null
12004         AFTER=$(roc_hit)
12005         if ! let "AFTER - BEFORE == CPAGES"; then
12006                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12007         else
12008                 log "cache hits:: before: $BEFORE, after: $AFTER"
12009         fi
12010
12011         restore_lustre_params < $p
12012         rm -f $p $file
12013 }
12014 run_test 156 "Verification of tunables"
12015
12016 test_160a() {
12017         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12018         remote_mds_nodsh && skip "remote MDS with nodsh"
12019         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
12020                 skip "Need MDS version at least 2.2.0"
12021
12022         changelog_register || error "changelog_register failed"
12023         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12024         changelog_users $SINGLEMDS | grep -q $cl_user ||
12025                 error "User $cl_user not found in changelog_users"
12026
12027         # change something
12028         test_mkdir -p $DIR/$tdir/pics/2008/zachy
12029         changelog_clear 0 || error "changelog_clear failed"
12030         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
12031         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
12032         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
12033         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
12034         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
12035         rm $DIR/$tdir/pics/desktop.jpg
12036
12037         changelog_dump | tail -10
12038
12039         echo "verifying changelog mask"
12040         changelog_chmask "-MKDIR"
12041         changelog_chmask "-CLOSE"
12042
12043         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
12044         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
12045
12046         changelog_chmask "+MKDIR"
12047         changelog_chmask "+CLOSE"
12048
12049         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
12050         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
12051
12052         changelog_dump | tail -10
12053         MKDIRS=$(changelog_dump | grep -c "MKDIR")
12054         CLOSES=$(changelog_dump | grep -c "CLOSE")
12055         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
12056         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
12057
12058         # verify contents
12059         echo "verifying target fid"
12060         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
12061         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
12062         [ "$fidc" == "$fidf" ] ||
12063                 error "changelog '$tfile' fid $fidc != file fid $fidf"
12064         echo "verifying parent fid"
12065         # The FID returned from the Changelog may be the directory shard on
12066         # a different MDT, and not the FID returned by path2fid on the parent.
12067         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
12068         # since this is what will matter when recreating this file in the tree.
12069         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
12070         local pathp=$($LFS fid2path $MOUNT "$fidp")
12071         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
12072                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
12073
12074         echo "getting records for $cl_user"
12075         changelog_users $SINGLEMDS
12076         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
12077         local nclr=3
12078         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
12079                 error "changelog_clear failed"
12080         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
12081         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
12082         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
12083                 error "user index expect $user_rec1 + $nclr != $user_rec2"
12084
12085         local min0_rec=$(changelog_users $SINGLEMDS |
12086                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
12087         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
12088                           awk '{ print $1; exit; }')
12089
12090         changelog_dump | tail -n 5
12091         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
12092         [ $first_rec == $((min0_rec + 1)) ] ||
12093                 error "first index should be $min0_rec + 1 not $first_rec"
12094
12095         # LU-3446 changelog index reset on MDT restart
12096         local cur_rec1=$(changelog_users $SINGLEMDS |
12097                          awk '/^current.index:/ { print $NF }')
12098         changelog_clear 0 ||
12099                 error "clear all changelog records for $cl_user failed"
12100         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
12101         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
12102                 error "Fail to start $SINGLEMDS"
12103         local cur_rec2=$(changelog_users $SINGLEMDS |
12104                          awk '/^current.index:/ { print $NF }')
12105         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
12106         [ $cur_rec1 == $cur_rec2 ] ||
12107                 error "current index should be $cur_rec1 not $cur_rec2"
12108
12109         echo "verifying users from this test are deregistered"
12110         changelog_deregister || error "changelog_deregister failed"
12111         changelog_users $SINGLEMDS | grep -q $cl_user &&
12112                 error "User '$cl_user' still in changelog_users"
12113
12114         # lctl get_param -n mdd.*.changelog_users
12115         # current index: 144
12116         # ID    index (idle seconds)
12117         # cl3   144 (2)
12118         if ! changelog_users $SINGLEMDS | grep "^cl"; then
12119                 # this is the normal case where all users were deregistered
12120                 # make sure no new records are added when no users are present
12121                 local last_rec1=$(changelog_users $SINGLEMDS |
12122                                   awk '/^current.index:/ { print $NF }')
12123                 touch $DIR/$tdir/chloe
12124                 local last_rec2=$(changelog_users $SINGLEMDS |
12125                                   awk '/^current.index:/ { print $NF }')
12126                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
12127                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
12128         else
12129                 # any changelog users must be leftovers from a previous test
12130                 changelog_users $SINGLEMDS
12131                 echo "other changelog users; can't verify off"
12132         fi
12133 }
12134 run_test 160a "changelog sanity"
12135
12136 test_160b() { # LU-3587
12137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12138         remote_mds_nodsh && skip "remote MDS with nodsh"
12139         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
12140                 skip "Need MDS version at least 2.2.0"
12141
12142         changelog_register || error "changelog_register failed"
12143         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12144         changelog_users $SINGLEMDS | grep -q $cl_user ||
12145                 error "User '$cl_user' not found in changelog_users"
12146
12147         local longname1=$(str_repeat a 255)
12148         local longname2=$(str_repeat b 255)
12149
12150         cd $DIR
12151         echo "creating very long named file"
12152         touch $longname1 || error "create of '$longname1' failed"
12153         echo "renaming very long named file"
12154         mv $longname1 $longname2
12155
12156         changelog_dump | grep RENME | tail -n 5
12157         rm -f $longname2
12158 }
12159 run_test 160b "Verify that very long rename doesn't crash in changelog"
12160
12161 test_160c() {
12162         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12163         remote_mds_nodsh && skip "remote MDS with nodsh"
12164
12165         local rc=0
12166         local server_version=$(lustre_version_code $SINGLEMDS)
12167
12168         [[ $server_version -gt $(version_code 2.5.57) ]] ||
12169                 [[ $server_version -gt $(version_code 2.5.1) &&
12170                    $server_version -lt $(version_code 2.5.50) ]] ||
12171                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
12172
12173         # Registration step
12174         changelog_register || error "changelog_register failed"
12175
12176         rm -rf $DIR/$tdir
12177         mkdir -p $DIR/$tdir
12178         $MCREATE $DIR/$tdir/foo_160c
12179         changelog_chmask "-TRUNC"
12180         $TRUNCATE $DIR/$tdir/foo_160c 200
12181         changelog_chmask "+TRUNC"
12182         $TRUNCATE $DIR/$tdir/foo_160c 199
12183         changelog_dump | tail -n 5
12184         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
12185         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
12186 }
12187 run_test 160c "verify that changelog log catch the truncate event"
12188
12189 test_160d() {
12190         remote_mds_nodsh && skip "remote MDS with nodsh"
12191         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12192         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12193         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.60) ]] ||
12194                 skip "Need MDS version at least 2.7.60"
12195
12196         # Registration step
12197         changelog_register || error "changelog_register failed"
12198
12199         mkdir -p $DIR/$tdir/migrate_dir
12200         changelog_clear 0 || error "changelog_clear failed"
12201
12202         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
12203         changelog_dump | tail -n 5
12204         local migrates=$(changelog_dump | grep -c "MIGRT")
12205         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
12206 }
12207 run_test 160d "verify that changelog log catch the migrate event"
12208
12209 test_160e() {
12210         remote_mds_nodsh && skip "remote MDS with nodsh"
12211
12212         # Create a user
12213         changelog_register || error "changelog_register failed"
12214
12215         # Delete a future user (expect fail)
12216         local MDT0=$(facet_svc $SINGLEMDS)
12217         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
12218         local rc=$?
12219
12220         if [ $rc -eq 0 ]; then
12221                 error "Deleted non-existant user cl77"
12222         elif [ $rc -ne 2 ]; then
12223                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
12224         fi
12225
12226         # Clear to a bad index (1 billion should be safe)
12227         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
12228         rc=$?
12229
12230         if [ $rc -eq 0 ]; then
12231                 error "Successfully cleared to invalid CL index"
12232         elif [ $rc -ne 22 ]; then
12233                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
12234         fi
12235 }
12236 run_test 160e "changelog negative testing (should return errors)"
12237
12238 test_160f() {
12239         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12240         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
12241                 { skip "Need MDS version at least 2.10.56"; return 0; }
12242
12243         local mdts=$(comma_list $(mdts_nodes))
12244
12245         # Create a user
12246         changelog_register || error "first changelog_register failed"
12247         changelog_register || error "second changelog_register failed"
12248         local cl_users
12249         declare -A cl_user1
12250         declare -A cl_user2
12251         local user_rec1
12252         local user_rec2
12253         local i
12254
12255         # generate some changelog records to accumulate on each MDT
12256         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12257         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12258                 error "create $DIR/$tdir/$tfile failed"
12259
12260         # check changelogs have been generated
12261         local nbcl=$(changelog_dump | wc -l)
12262         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12263
12264         for param in "changelog_max_idle_time=10" \
12265                      "changelog_gc=1" \
12266                      "changelog_min_gc_interval=2" \
12267                      "changelog_min_free_cat_entries=3"; do
12268                 local MDT0=$(facet_svc $SINGLEMDS)
12269                 local var="${param%=*}"
12270                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12271
12272                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12273                 do_nodes $mdts $LCTL set_param mdd.*.$param
12274         done
12275
12276         # force cl_user2 to be idle (1st part)
12277         sleep 9
12278
12279         # simulate changelog catalog almost full
12280         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12281         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12282
12283         for i in $(seq $MDSCOUNT); do
12284                 cl_users=(${CL_USERS[mds$i]})
12285                 cl_user1[mds$i]="${cl_users[0]}"
12286                 cl_user2[mds$i]="${cl_users[1]}"
12287
12288                 [ -n "${cl_user1[mds$i]}" ] ||
12289                         error "mds$i: no user registered"
12290                 [ -n "${cl_user2[mds$i]}" ] ||
12291                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12292
12293                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12294                 [ -n "$user_rec1" ] ||
12295                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12296                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12297                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12298                 [ -n "$user_rec2" ] ||
12299                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12300                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12301                      "$user_rec1 + 2 == $user_rec2"
12302                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12303                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12304                               "$user_rec1 + 2, but is $user_rec2"
12305                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12306                 [ -n "$user_rec2" ] ||
12307                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12308                 [ $user_rec1 == $user_rec2 ] ||
12309                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12310                               "$user_rec1, but is $user_rec2"
12311         done
12312
12313         # force cl_user2 to be idle (2nd part) and to reach
12314         # changelog_max_idle_time
12315         sleep 2
12316
12317         # generate one more changelog to trigger fail_loc
12318         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12319                 error "create $DIR/$tdir/${tfile}bis failed"
12320
12321         # ensure gc thread is done
12322         for i in $(mdts_nodes); do
12323                 wait_update $i \
12324                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
12325                         error "$i: GC-thread not done"
12326         done
12327
12328         local first_rec
12329         for i in $(seq $MDSCOUNT); do
12330                 # check cl_user1 still registered
12331                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12332                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12333                 # check cl_user2 unregistered
12334                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12335                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12336
12337                 # check changelogs are present and starting at $user_rec1 + 1
12338                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12339                 [ -n "$user_rec1" ] ||
12340                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12341                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12342                             awk '{ print $1; exit; }')
12343
12344                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12345                 [ $((user_rec1 + 1)) == $first_rec ] ||
12346                         error "mds$i: first index should be $user_rec1 + 1, " \
12347                               "but is $first_rec"
12348         done
12349 }
12350 run_test 160f "changelog garbage collect (timestamped users)"
12351
12352 test_160g() {
12353         remote_mds_nodsh && skip "remote MDS with nodsh"
12354         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
12355                 skip "Need MDS version at least 2.10.56"
12356
12357         local mdts=$(comma_list $(mdts_nodes))
12358
12359         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
12360         do_nodes $mdts $LCTL set_param fail_loc=0x1314
12361
12362         # Create a user
12363         changelog_register || error "first changelog_register failed"
12364         changelog_register || error "second changelog_register failed"
12365         local cl_users
12366         declare -A cl_user1
12367         declare -A cl_user2
12368         local user_rec1
12369         local user_rec2
12370         local i
12371
12372         # generate some changelog records to accumulate on each MDT
12373         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
12374         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12375                 error "create $DIR/$tdir/$tfile failed"
12376
12377         # check changelogs have been generated
12378         local nbcl=$(changelog_dump | wc -l)
12379         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12380
12381         for param in "changelog_max_idle_indexes=$((nbcl / 2))" \
12382                      "changelog_gc=1" \
12383                      "changelog_min_gc_interval=2" \
12384                      "changelog_min_free_cat_entries=3"; do
12385                 local MDT0=$(facet_svc $SINGLEMDS)
12386                 local var="${param%=*}"
12387                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12388
12389                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12390                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
12391                         error "unable to set mdd.*.$param"
12392         done
12393
12394         # simulate changelog catalog almost full
12395         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
12396         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
12397
12398         for i in $(seq $MDSCOUNT); do
12399                 cl_users=(${CL_USERS[mds$i]})
12400                 cl_user1[mds$i]="${cl_users[0]}"
12401                 cl_user2[mds$i]="${cl_users[1]}"
12402
12403                 [ -n "${cl_user1[mds$i]}" ] ||
12404                         error "mds$i: no user registered"
12405                 [ -n "${cl_user2[mds$i]}" ] ||
12406                         error "mds$i: only ${cl_user1[mds$i]} is registered"
12407
12408                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12409                 [ -n "$user_rec1" ] ||
12410                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12411                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12412                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12413                 [ -n "$user_rec2" ] ||
12414                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12415                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12416                      "$user_rec1 + 2 == $user_rec2"
12417                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12418                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12419                               "$user_rec1 + 2, but is $user_rec2"
12420                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12421                 [ -n "$user_rec2" ] ||
12422                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12423                 [ $user_rec1 == $user_rec2 ] ||
12424                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12425                               "$user_rec1, but is $user_rec2"
12426         done
12427
12428         # ensure we are past the previous changelog_min_gc_interval set above
12429         sleep 2
12430
12431         # generate one more changelog to trigger fail_loc
12432         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12433                 error "create $DIR/$tdir/${tfile}bis failed"
12434
12435         # ensure gc thread is done
12436         for i in $(mdts_nodes); do
12437                 wait_update $i \
12438                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
12439                         error "$i: GC-thread not done"
12440         done
12441
12442         local first_rec
12443         for i in $(seq $MDSCOUNT); do
12444                 # check cl_user1 still registered
12445                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12446                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12447                 # check cl_user2 unregistered
12448                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12449                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12450
12451                 # check changelogs are present and starting at $user_rec1 + 1
12452                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12453                 [ -n "$user_rec1" ] ||
12454                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12455                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12456                             awk '{ print $1; exit; }')
12457
12458                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12459                 [ $((user_rec1 + 1)) == $first_rec ] ||
12460                         error "mds$i: first index should be $user_rec1 + 1, " \
12461                               "but is $first_rec"
12462         done
12463 }
12464 run_test 160g "changelog garbage collect (old users)"
12465
12466 test_160h() {
12467         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12468         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.56) ]] ||
12469                 { skip "Need MDS version at least 2.10.56"; return 0; }
12470
12471         local mdts=$(comma_list $(mdts_nodes))
12472
12473         # Create a user
12474         changelog_register || error "first changelog_register failed"
12475         changelog_register || error "second changelog_register failed"
12476         local cl_users
12477         declare -A cl_user1
12478         declare -A cl_user2
12479         local user_rec1
12480         local user_rec2
12481         local i
12482
12483         # generate some changelog records to accumulate on each MDT
12484         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
12485         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12486                 error "create $DIR/$tdir/$tfile failed"
12487
12488         # check changelogs have been generated
12489         local nbcl=$(changelog_dump | wc -l)
12490         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12491
12492         for param in "changelog_max_idle_time=10" \
12493                      "changelog_gc=1" \
12494                      "changelog_min_gc_interval=2"; do
12495                 local MDT0=$(facet_svc $SINGLEMDS)
12496                 local var="${param%=*}"
12497                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
12498
12499                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
12500                 do_nodes $mdts $LCTL set_param mdd.*.$param
12501         done
12502
12503         # force cl_user2 to be idle (1st part)
12504         sleep 9
12505
12506         for i in $(seq $MDSCOUNT); do
12507                 cl_users=(${CL_USERS[mds$i]})
12508                 cl_user1[mds$i]="${cl_users[0]}"
12509                 cl_user2[mds$i]="${cl_users[1]}"
12510
12511                 [ -n "${cl_user1[mds$i]}" ] ||
12512                         error "mds$i: no user registered"
12513                 [ -n "${cl_user2[mds$i]}" ] ||
12514                         error "mds$i: only ${cl_user2[mds$i]} is registered"
12515
12516                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12517                 [ -n "$user_rec1" ] ||
12518                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12519                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
12520                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12521                 [ -n "$user_rec2" ] ||
12522                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12523                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
12524                      "$user_rec1 + 2 == $user_rec2"
12525                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
12526                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
12527                               "$user_rec1 + 2, but is $user_rec2"
12528                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
12529                 [ -n "$user_rec2" ] ||
12530                         error "mds$i: User ${cl_user2[mds$i]} not registered"
12531                 [ $user_rec1 == $user_rec2 ] ||
12532                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
12533                               "$user_rec1, but is $user_rec2"
12534         done
12535
12536         # force cl_user2 to be idle (2nd part) and to reach
12537         # changelog_max_idle_time
12538         sleep 2
12539
12540         # force each GC-thread start and block then
12541         # one per MDT/MDD, set fail_val accordingly
12542         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
12543         do_nodes $mdts $LCTL set_param fail_loc=0x1316
12544
12545         # generate more changelogs to trigger fail_loc
12546         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12547                 error "create $DIR/$tdir/${tfile}bis failed"
12548
12549         # stop MDT to stop GC-thread, should be done in back-ground as it will
12550         # block waiting for the thread to be released and exit
12551         declare -A stop_pids
12552         for i in $(seq $MDSCOUNT); do
12553                 stop mds$i &
12554                 stop_pids[mds$i]=$!
12555         done
12556
12557         for i in $(mdts_nodes); do
12558                 local facet
12559                 local nb=0
12560                 local facets=$(facets_up_on_host $i)
12561
12562                 for facet in ${facets//,/ }; do
12563                         if [[ $facet == mds* ]]; then
12564                                 nb=$((nb + 1))
12565                         fi
12566                 done
12567                 # ensure each MDS's gc threads are still present and all in "R"
12568                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
12569                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
12570                         error "$i: expected $nb GC-thread"
12571                 wait_update $i \
12572                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
12573                         "R" 20 ||
12574                         error "$i: GC-thread not found in R-state"
12575                 # check umounts of each MDT on MDS have reached kthread_stop()
12576                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
12577                         error "$i: expected $nb umount"
12578                 wait_update $i \
12579                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
12580                         error "$i: umount not found in D-state"
12581         done
12582
12583         # release all GC-threads
12584         do_nodes $mdts $LCTL set_param fail_loc=0
12585
12586         # wait for MDT stop to complete
12587         for i in $(seq $MDSCOUNT); do
12588                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
12589         done
12590
12591         # XXX
12592         # may try to check if any orphan changelog records are present
12593         # via ldiskfs/zfs and llog_reader...
12594
12595         # re-start/mount MDTs
12596         for i in $(seq $MDSCOUNT); do
12597                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
12598                         error "Fail to start mds$i"
12599         done
12600
12601         local first_rec
12602         for i in $(seq $MDSCOUNT); do
12603                 # check cl_user1 still registered
12604                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
12605                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12606                 # check cl_user2 unregistered
12607                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
12608                         error "mds$i: User ${cl_user2[mds$i]} still registered"
12609
12610                 # check changelogs are present and starting at $user_rec1 + 1
12611                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
12612                 [ -n "$user_rec1" ] ||
12613                         error "mds$i: User ${cl_user1[mds$i]} not registered"
12614                 first_rec=$($LFS changelog $(facet_svc mds$i) |
12615                             awk '{ print $1; exit; }')
12616
12617                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
12618                 [ $((user_rec1 + 1)) == $first_rec ] ||
12619                         error "mds$i: first index should be $user_rec1 + 1, " \
12620                               "but is $first_rec"
12621         done
12622 }
12623 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
12624               "during mount"
12625
12626 test_160i() {
12627
12628         local mdts=$(comma_list $(mdts_nodes))
12629
12630         changelog_register || error "first changelog_register failed"
12631
12632         # generate some changelog records to accumulate on each MDT
12633         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
12634         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
12635                 error "create $DIR/$tdir/$tfile failed"
12636
12637         # check changelogs have been generated
12638         local nbcl=$(changelog_dump | wc -l)
12639         [[ $nbcl -eq 0 ]] && error "no changelogs found"
12640
12641         # simulate race between register and unregister
12642         # XXX as fail_loc is set per-MDS, with DNE configs the race
12643         # simulation will only occur for one MDT per MDS and for the
12644         # others the normal race scenario will take place
12645         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
12646         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
12647         do_nodes $mdts $LCTL set_param fail_val=1
12648
12649         # unregister 1st user
12650         changelog_deregister &
12651         local pid1=$!
12652         # wait some time for deregister work to reach race rdv
12653         sleep 2
12654         # register 2nd user
12655         changelog_register || error "2nd user register failed"
12656
12657         wait $pid1 || error "1st user deregister failed"
12658
12659         local i
12660         local last_rec
12661         declare -A LAST_REC
12662         for i in $(seq $MDSCOUNT); do
12663                 if changelog_users mds$i | grep "^cl"; then
12664                         # make sure new records are added with one user present
12665                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
12666                                           awk '/^current.index:/ { print $NF }')
12667                 else
12668                         error "mds$i has no user registered"
12669                 fi
12670         done
12671
12672         # generate more changelog records to accumulate on each MDT
12673         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
12674                 error "create $DIR/$tdir/${tfile}bis failed"
12675
12676         for i in $(seq $MDSCOUNT); do
12677                 last_rec=$(changelog_users $SINGLEMDS |
12678                            awk '/^current.index:/ { print $NF }')
12679                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
12680                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
12681                         error "changelogs are off on mds$i"
12682         done
12683 }
12684 run_test 160i "changelog user register/unregister race"
12685
12686 test_161a() {
12687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12688
12689         test_mkdir -c1 $DIR/$tdir
12690         cp /etc/hosts $DIR/$tdir/$tfile
12691         test_mkdir -c1 $DIR/$tdir/foo1
12692         test_mkdir -c1 $DIR/$tdir/foo2
12693         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
12694         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
12695         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
12696         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
12697         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
12698         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
12699                 $LFS fid2path $DIR $FID
12700                 error "bad link ea"
12701         fi
12702         # middle
12703         rm $DIR/$tdir/foo2/zachary
12704         # last
12705         rm $DIR/$tdir/foo2/thor
12706         # first
12707         rm $DIR/$tdir/$tfile
12708         # rename
12709         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
12710         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
12711                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
12712         rm $DIR/$tdir/foo2/maggie
12713
12714         # overflow the EA
12715         local longname=$tfile.avg_len_is_thirty_two_
12716         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
12717                 error_noexit 'failed to unlink many hardlinks'" EXIT
12718         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
12719                 error "failed to hardlink many files"
12720         links=$($LFS fid2path $DIR $FID | wc -l)
12721         echo -n "${links}/1000 links in link EA"
12722         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
12723 }
12724 run_test 161a "link ea sanity"
12725
12726 test_161b() {
12727         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12728         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
12729
12730         local MDTIDX=1
12731         local remote_dir=$DIR/$tdir/remote_dir
12732
12733         mkdir -p $DIR/$tdir
12734         $LFS mkdir -i $MDTIDX $remote_dir ||
12735                 error "create remote directory failed"
12736
12737         cp /etc/hosts $remote_dir/$tfile
12738         mkdir -p $remote_dir/foo1
12739         mkdir -p $remote_dir/foo2
12740         ln $remote_dir/$tfile $remote_dir/foo1/sofia
12741         ln $remote_dir/$tfile $remote_dir/foo2/zachary
12742         ln $remote_dir/$tfile $remote_dir/foo1/luna
12743         ln $remote_dir/$tfile $remote_dir/foo2/thor
12744
12745         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
12746                      tr -d ']')
12747         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
12748                 $LFS fid2path $DIR $FID
12749                 error "bad link ea"
12750         fi
12751         # middle
12752         rm $remote_dir/foo2/zachary
12753         # last
12754         rm $remote_dir/foo2/thor
12755         # first
12756         rm $remote_dir/$tfile
12757         # rename
12758         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
12759         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
12760         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
12761                 $LFS fid2path $DIR $FID
12762                 error "bad link rename"
12763         fi
12764         rm $remote_dir/foo2/maggie
12765
12766         # overflow the EA
12767         local longname=filename_avg_len_is_thirty_two_
12768         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
12769                 error "failed to hardlink many files"
12770         links=$($LFS fid2path $DIR $FID | wc -l)
12771         echo -n "${links}/1000 links in link EA"
12772         [[ ${links} -gt 60 ]] ||
12773                 error "expected at least 60 links in link EA"
12774         unlinkmany $remote_dir/foo2/$longname 1000 ||
12775         error "failed to unlink many hardlinks"
12776 }
12777 run_test 161b "link ea sanity under remote directory"
12778
12779 test_161c() {
12780         remote_mds_nodsh && skip "remote MDS with nodsh"
12781         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12782         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
12783                 skip "Need MDS version at least 2.1.5"
12784
12785         # define CLF_RENAME_LAST 0x0001
12786         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
12787         changelog_register || error "changelog_register failed"
12788
12789         rm -rf $DIR/$tdir
12790         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
12791         touch $DIR/$tdir/foo_161c
12792         touch $DIR/$tdir/bar_161c
12793         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12794         changelog_dump | grep RENME | tail -n 5
12795         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
12796         changelog_clear 0 || error "changelog_clear failed"
12797         if [ x$flags != "x0x1" ]; then
12798                 error "flag $flags is not 0x1"
12799         fi
12800
12801         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
12802         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
12803         touch $DIR/$tdir/foo_161c
12804         touch $DIR/$tdir/bar_161c
12805         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12806         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12807         changelog_dump | grep RENME | tail -n 5
12808         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
12809         changelog_clear 0 || error "changelog_clear failed"
12810         if [ x$flags != "x0x0" ]; then
12811                 error "flag $flags is not 0x0"
12812         fi
12813         echo "rename overwrite a target having nlink > 1," \
12814                 "changelog record has flags of $flags"
12815
12816         # rename doesn't overwrite a target (changelog flag 0x0)
12817         touch $DIR/$tdir/foo_161c
12818         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
12819         changelog_dump | grep RENME | tail -n 5
12820         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
12821         changelog_clear 0 || error "changelog_clear failed"
12822         if [ x$flags != "x0x0" ]; then
12823                 error "flag $flags is not 0x0"
12824         fi
12825         echo "rename doesn't overwrite a target," \
12826                 "changelog record has flags of $flags"
12827
12828         # define CLF_UNLINK_LAST 0x0001
12829         # unlink a file having nlink = 1 (changelog flag 0x1)
12830         rm -f $DIR/$tdir/foo2_161c
12831         changelog_dump | grep UNLNK | tail -n 5
12832         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
12833         changelog_clear 0 || error "changelog_clear failed"
12834         if [ x$flags != "x0x1" ]; then
12835                 error "flag $flags is not 0x1"
12836         fi
12837         echo "unlink a file having nlink = 1," \
12838                 "changelog record has flags of $flags"
12839
12840         # unlink a file having nlink > 1 (changelog flag 0x0)
12841         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12842         rm -f $DIR/$tdir/foobar_161c
12843         changelog_dump | grep UNLNK | tail -n 5
12844         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
12845         changelog_clear 0 || error "changelog_clear failed"
12846         if [ x$flags != "x0x0" ]; then
12847                 error "flag $flags is not 0x0"
12848         fi
12849         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
12850 }
12851 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
12852
12853 test_161d() {
12854         remote_mds_nodsh && skip "remote MDS with nodsh"
12855
12856         local pid
12857         local fid
12858
12859         changelog_register || error "changelog_register failed"
12860
12861         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
12862         # interfer with $MOUNT/.lustre/fid/ access
12863         mkdir $DIR/$tdir
12864         [[ $? -eq 0 ]] || error "mkdir failed"
12865
12866         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
12867         $LCTL set_param fail_loc=0x8000140c
12868         # 5s pause
12869         $LCTL set_param fail_val=5
12870
12871         # create file
12872         echo foofoo > $DIR/$tdir/$tfile &
12873         pid=$!
12874
12875         # wait for create to be delayed
12876         sleep 2
12877
12878         ps -p $pid
12879         [[ $? -eq 0 ]] || error "create should be blocked"
12880
12881         local tempfile=$(mktemp)
12882         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
12883         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
12884         # some delay may occur during ChangeLog publishing and file read just
12885         # above, that could allow file write to happen finally
12886         [[ -s $tempfile ]] && echo "file should be empty"
12887
12888         $LCTL set_param fail_loc=0
12889
12890         wait $pid
12891         [[ $? -eq 0 ]] || error "create failed"
12892 }
12893 run_test 161d "create with concurrent .lustre/fid access"
12894
12895 check_path() {
12896         local expected="$1"
12897         shift
12898         local fid="$2"
12899
12900         local path
12901         path=$($LFS fid2path "$@")
12902         local rc=$?
12903
12904         if [ $rc -ne 0 ]; then
12905                 error "path looked up of '$expected' failed: rc=$rc"
12906         elif [ "$path" != "$expected" ]; then
12907                 error "path looked up '$path' instead of '$expected'"
12908         else
12909                 echo "FID '$fid' resolves to path '$path' as expected"
12910         fi
12911 }
12912
12913 test_162a() { # was test_162
12914         test_mkdir -p -c1 $DIR/$tdir/d2
12915         touch $DIR/$tdir/d2/$tfile
12916         touch $DIR/$tdir/d2/x1
12917         touch $DIR/$tdir/d2/x2
12918         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
12919         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
12920         # regular file
12921         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
12922         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
12923
12924         # softlink
12925         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
12926         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
12927         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
12928
12929         # softlink to wrong file
12930         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
12931         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
12932         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
12933
12934         # hardlink
12935         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
12936         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
12937         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
12938         # fid2path dir/fsname should both work
12939         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
12940         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
12941
12942         # hardlink count: check that there are 2 links
12943         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
12944         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
12945
12946         # hardlink indexing: remove the first link
12947         rm $DIR/$tdir/d2/p/q/r/hlink
12948         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
12949 }
12950 run_test 162a "path lookup sanity"
12951
12952 test_162b() {
12953         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12954         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12955
12956         mkdir $DIR/$tdir
12957         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
12958                                 error "create striped dir failed"
12959
12960         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
12961                                         tail -n 1 | awk '{print $2}')
12962         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
12963
12964         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
12965         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
12966
12967         # regular file
12968         for ((i=0;i<5;i++)); do
12969                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
12970                         error "get fid for f$i failed"
12971                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
12972
12973                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
12974                         error "get fid for d$i failed"
12975                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
12976         done
12977
12978         return 0
12979 }
12980 run_test 162b "striped directory path lookup sanity"
12981
12982 # LU-4239: Verify fid2path works with paths 100 or more directories deep
12983 test_162c() {
12984         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
12985                 skip "Need MDS version at least 2.7.51"
12986
12987         local lpath=$tdir.local
12988         local rpath=$tdir.remote
12989
12990         test_mkdir $DIR/$lpath
12991         test_mkdir $DIR/$rpath
12992
12993         for ((i = 0; i <= 101; i++)); do
12994                 lpath="$lpath/$i"
12995                 mkdir $DIR/$lpath
12996                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
12997                         error "get fid for local directory $DIR/$lpath failed"
12998                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
12999
13000                 rpath="$rpath/$i"
13001                 test_mkdir $DIR/$rpath
13002                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
13003                         error "get fid for remote directory $DIR/$rpath failed"
13004                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
13005         done
13006
13007         return 0
13008 }
13009 run_test 162c "fid2path works with paths 100 or more directories deep"
13010
13011 test_169() {
13012         # do directio so as not to populate the page cache
13013         log "creating a 10 Mb file"
13014         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13015         log "starting reads"
13016         dd if=$DIR/$tfile of=/dev/null bs=4096 &
13017         log "truncating the file"
13018         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13019         log "killing dd"
13020         kill %+ || true # reads might have finished
13021         echo "wait until dd is finished"
13022         wait
13023         log "removing the temporary file"
13024         rm -rf $DIR/$tfile || error "tmp file removal failed"
13025 }
13026 run_test 169 "parallel read and truncate should not deadlock"
13027
13028 test_170() {
13029         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13030
13031         $LCTL clear     # bug 18514
13032         $LCTL debug_daemon start $TMP/${tfile}_log_good
13033         touch $DIR/$tfile
13034         $LCTL debug_daemon stop
13035         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
13036                 error "sed failed to read log_good"
13037
13038         $LCTL debug_daemon start $TMP/${tfile}_log_good
13039         rm -rf $DIR/$tfile
13040         $LCTL debug_daemon stop
13041
13042         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
13043                error "lctl df log_bad failed"
13044
13045         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13046         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13047
13048         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
13049         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
13050
13051         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
13052                 error "bad_line good_line1 good_line2 are empty"
13053
13054         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13055         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
13056         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
13057
13058         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
13059         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13060         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13061
13062         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
13063                 error "bad_line_new good_line_new are empty"
13064
13065         local expected_good=$((good_line1 + good_line2*2))
13066
13067         rm -f $TMP/${tfile}*
13068         # LU-231, short malformed line may not be counted into bad lines
13069         if [ $bad_line -ne $bad_line_new ] &&
13070                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
13071                 error "expected $bad_line bad lines, but got $bad_line_new"
13072                 return 1
13073         fi
13074
13075         if [ $expected_good -ne $good_line_new ]; then
13076                 error "expected $expected_good good lines, but got $good_line_new"
13077                 return 2
13078         fi
13079         true
13080 }
13081 run_test 170 "test lctl df to handle corrupted log ====================="
13082
13083 test_171() { # bug20592
13084         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13085
13086         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
13087         $LCTL set_param fail_loc=0x50e
13088         $LCTL set_param fail_val=3000
13089         multiop_bg_pause $DIR/$tfile O_s || true
13090         local MULTIPID=$!
13091         kill -USR1 $MULTIPID
13092         # cause log dump
13093         sleep 3
13094         wait $MULTIPID
13095         if dmesg | grep "recursive fault"; then
13096                 error "caught a recursive fault"
13097         fi
13098         $LCTL set_param fail_loc=0
13099         true
13100 }
13101 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
13102
13103 # it would be good to share it with obdfilter-survey/iokit-libecho code
13104 setup_obdecho_osc () {
13105         local rc=0
13106         local ost_nid=$1
13107         local obdfilter_name=$2
13108         echo "Creating new osc for $obdfilter_name on $ost_nid"
13109         # make sure we can find loopback nid
13110         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
13111
13112         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
13113                            ${obdfilter_name}_osc_UUID || rc=2; }
13114         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
13115                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
13116         return $rc
13117 }
13118
13119 cleanup_obdecho_osc () {
13120         local obdfilter_name=$1
13121         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
13122         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
13123         return 0
13124 }
13125
13126 obdecho_test() {
13127         local OBD=$1
13128         local node=$2
13129         local pages=${3:-64}
13130         local rc=0
13131         local id
13132
13133         local count=10
13134         local obd_size=$(get_obd_size $node $OBD)
13135         local page_size=$(get_page_size $node)
13136         if [[ -n "$obd_size" ]]; then
13137                 local new_count=$((obd_size / (pages * page_size / 1024)))
13138                 [[ $new_count -ge $count ]] || count=$new_count
13139         fi
13140
13141         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
13142         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
13143                            rc=2; }
13144         if [ $rc -eq 0 ]; then
13145             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
13146             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
13147         fi
13148         echo "New object id is $id"
13149         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
13150                            rc=4; }
13151         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
13152                            "test_brw $count w v $pages $id" || rc=4; }
13153         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
13154                            rc=4; }
13155         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
13156                                         "cleanup" || rc=5; }
13157         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
13158                                         "detach" || rc=6; }
13159         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
13160         return $rc
13161 }
13162
13163 test_180a() {
13164         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13165
13166         if ! module_loaded obdecho; then
13167                 load_module obdecho/obdecho &&
13168                         stack_trap "rmmod obdecho" EXIT ||
13169                         error "unable to load obdecho on client"
13170         fi
13171
13172         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
13173         local host=$($LCTL get_param -n osc.$osc.import |
13174                      awk '/current_connection:/ { print $2 }' )
13175         local target=$($LCTL get_param -n osc.$osc.import |
13176                        awk '/target:/ { print $2 }' )
13177         target=${target%_UUID}
13178
13179         if [ -n "$target" ]; then
13180                 setup_obdecho_osc $host $target &&
13181                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
13182                         { error "obdecho setup failed with $?"; return; }
13183
13184                 obdecho_test ${target}_osc client ||
13185                         error "obdecho_test failed on ${target}_osc"
13186         else
13187                 $LCTL get_param osc.$osc.import
13188                 error "there is no osc.$osc.import target"
13189         fi
13190 }
13191 run_test 180a "test obdecho on osc"
13192
13193 test_180b() {
13194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13195         remote_ost_nodsh && skip "remote OST with nodsh"
13196
13197         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13198                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13199                 error "failed to load module obdecho"
13200
13201         local target=$(do_facet ost1 $LCTL dl |
13202                        awk '/obdfilter/ { print $4; exit; }')
13203
13204         if [ -n "$target" ]; then
13205                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
13206         else
13207                 do_facet ost1 $LCTL dl
13208                 error "there is no obdfilter target on ost1"
13209         fi
13210 }
13211 run_test 180b "test obdecho directly on obdfilter"
13212
13213 test_180c() { # LU-2598
13214         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13215         remote_ost_nodsh && skip "remote OST with nodsh"
13216         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
13217                 skip "Need MDS version at least 2.4.0"
13218
13219         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
13220                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
13221                 error "failed to load module obdecho"
13222
13223         local target=$(do_facet ost1 $LCTL dl |
13224                        awk '/obdfilter/ { print $4; exit; }')
13225
13226         if [ -n "$target" ]; then
13227                 local pages=16384 # 64MB bulk I/O RPC size
13228
13229                 obdecho_test "$target" ost1 "$pages" ||
13230                         error "obdecho_test with pages=$pages failed with $?"
13231         else
13232                 do_facet ost1 $LCTL dl
13233                 error "there is no obdfilter target on ost1"
13234         fi
13235 }
13236 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
13237
13238 test_181() { # bug 22177
13239         test_mkdir $DIR/$tdir
13240         # create enough files to index the directory
13241         createmany -o $DIR/$tdir/foobar 4000
13242         # print attributes for debug purpose
13243         lsattr -d .
13244         # open dir
13245         multiop_bg_pause $DIR/$tdir D_Sc || return 1
13246         MULTIPID=$!
13247         # remove the files & current working dir
13248         unlinkmany $DIR/$tdir/foobar 4000
13249         rmdir $DIR/$tdir
13250         kill -USR1 $MULTIPID
13251         wait $MULTIPID
13252         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
13253         return 0
13254 }
13255 run_test 181 "Test open-unlinked dir ========================"
13256
13257 test_182() {
13258         local fcount=1000
13259         local tcount=10
13260
13261         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13262
13263         $LCTL set_param mdc.*.rpc_stats=clear
13264
13265         for (( i = 0; i < $tcount; i++ )) ; do
13266                 mkdir $DIR/$tdir/$i
13267         done
13268
13269         for (( i = 0; i < $tcount; i++ )) ; do
13270                 createmany -o $DIR/$tdir/$i/f- $fcount &
13271         done
13272         wait
13273
13274         for (( i = 0; i < $tcount; i++ )) ; do
13275                 unlinkmany $DIR/$tdir/$i/f- $fcount &
13276         done
13277         wait
13278
13279         $LCTL get_param mdc.*.rpc_stats
13280
13281         rm -rf $DIR/$tdir
13282 }
13283 run_test 182 "Test parallel modify metadata operations ================"
13284
13285 test_183() { # LU-2275
13286         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13287         remote_mds_nodsh && skip "remote MDS with nodsh"
13288         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
13289                 skip "Need MDS version at least 2.3.56"
13290
13291         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13292         echo aaa > $DIR/$tdir/$tfile
13293
13294 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
13295         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
13296
13297         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
13298         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
13299
13300         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
13301
13302         # Flush negative dentry cache
13303         touch $DIR/$tdir/$tfile
13304
13305         # We are not checking for any leaked references here, they'll
13306         # become evident next time we do cleanup with module unload.
13307         rm -rf $DIR/$tdir
13308 }
13309 run_test 183 "No crash or request leak in case of strange dispositions ========"
13310
13311 # test suite 184 is for LU-2016, LU-2017
13312 test_184a() {
13313         check_swap_layouts_support
13314
13315         dir0=$DIR/$tdir/$testnum
13316         test_mkdir -p -c1 $dir0
13317         ref1=/etc/passwd
13318         ref2=/etc/group
13319         file1=$dir0/f1
13320         file2=$dir0/f2
13321         $SETSTRIPE -c1 $file1
13322         cp $ref1 $file1
13323         $SETSTRIPE -c2 $file2
13324         cp $ref2 $file2
13325         gen1=$($GETSTRIPE -g $file1)
13326         gen2=$($GETSTRIPE -g $file2)
13327
13328         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
13329         gen=$($GETSTRIPE -g $file1)
13330         [[ $gen1 != $gen ]] ||
13331                 "Layout generation on $file1 does not change"
13332         gen=$($GETSTRIPE -g $file2)
13333         [[ $gen2 != $gen ]] ||
13334                 "Layout generation on $file2 does not change"
13335
13336         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
13337         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
13338
13339         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
13340 }
13341 run_test 184a "Basic layout swap"
13342
13343 test_184b() {
13344         check_swap_layouts_support
13345
13346         dir0=$DIR/$tdir/$testnum
13347         mkdir -p $dir0 || error "creating dir $dir0"
13348         file1=$dir0/f1
13349         file2=$dir0/f2
13350         file3=$dir0/f3
13351         dir1=$dir0/d1
13352         dir2=$dir0/d2
13353         mkdir $dir1 $dir2
13354         $SETSTRIPE -c1 $file1
13355         $SETSTRIPE -c2 $file2
13356         $SETSTRIPE -c1 $file3
13357         chown $RUNAS_ID $file3
13358         gen1=$($GETSTRIPE -g $file1)
13359         gen2=$($GETSTRIPE -g $file2)
13360
13361         $LFS swap_layouts $dir1 $dir2 &&
13362                 error "swap of directories layouts should fail"
13363         $LFS swap_layouts $dir1 $file1 &&
13364                 error "swap of directory and file layouts should fail"
13365         $RUNAS $LFS swap_layouts $file1 $file2 &&
13366                 error "swap of file we cannot write should fail"
13367         $LFS swap_layouts $file1 $file3 &&
13368                 error "swap of file with different owner should fail"
13369         /bin/true # to clear error code
13370 }
13371 run_test 184b "Forbidden layout swap (will generate errors)"
13372
13373 test_184c() {
13374         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
13375         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
13376         check_swap_layouts_support
13377
13378         local dir0=$DIR/$tdir/$testnum
13379         mkdir -p $dir0 || error "creating dir $dir0"
13380
13381         local ref1=$dir0/ref1
13382         local ref2=$dir0/ref2
13383         local file1=$dir0/file1
13384         local file2=$dir0/file2
13385         # create a file large enough for the concurrent test
13386         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
13387         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
13388         echo "ref file size: ref1($(stat -c %s $ref1))," \
13389              "ref2($(stat -c %s $ref2))"
13390
13391         cp $ref2 $file2
13392         dd if=$ref1 of=$file1 bs=16k &
13393         local DD_PID=$!
13394
13395         # Make sure dd starts to copy file
13396         while [ ! -f $file1 ]; do sleep 0.1; done
13397
13398         $LFS swap_layouts $file1 $file2
13399         local rc=$?
13400         wait $DD_PID
13401         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
13402         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
13403
13404         # how many bytes copied before swapping layout
13405         local copied=$(stat -c %s $file2)
13406         local remaining=$(stat -c %s $ref1)
13407         remaining=$((remaining - copied))
13408         echo "Copied $copied bytes before swapping layout..."
13409
13410         cmp -n $copied $file1 $ref2 | grep differ &&
13411                 error "Content mismatch [0, $copied) of ref2 and file1"
13412         cmp -n $copied $file2 $ref1 ||
13413                 error "Content mismatch [0, $copied) of ref1 and file2"
13414         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
13415                 error "Content mismatch [$copied, EOF) of ref1 and file1"
13416
13417         # clean up
13418         rm -f $ref1 $ref2 $file1 $file2
13419 }
13420 run_test 184c "Concurrent write and layout swap"
13421
13422 test_184d() {
13423         check_swap_layouts_support
13424         [ -z "$(which getfattr 2>/dev/null)" ] &&
13425                 skip_env "no getfattr command"
13426
13427         local file1=$DIR/$tdir/$tfile-1
13428         local file2=$DIR/$tdir/$tfile-2
13429         local file3=$DIR/$tdir/$tfile-3
13430         local lovea1
13431         local lovea2
13432
13433         mkdir -p $DIR/$tdir
13434         touch $file1 || error "create $file1 failed"
13435         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
13436                 error "create $file2 failed"
13437         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
13438                 error "create $file3 failed"
13439         lovea1=$(get_layout_param $file1)
13440
13441         $LFS swap_layouts $file2 $file3 ||
13442                 error "swap $file2 $file3 layouts failed"
13443         $LFS swap_layouts $file1 $file2 ||
13444                 error "swap $file1 $file2 layouts failed"
13445
13446         lovea2=$(get_layout_param $file2)
13447         echo "$lovea1"
13448         echo "$lovea2"
13449         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
13450
13451         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
13452         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
13453 }
13454 run_test 184d "allow stripeless layouts swap"
13455
13456 test_184e() {
13457         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
13458                 skip "Need MDS version at least 2.6.94"
13459         check_swap_layouts_support
13460         [ -z "$(which getfattr 2>/dev/null)" ] &&
13461                 skip_env "no getfattr command"
13462
13463         local file1=$DIR/$tdir/$tfile-1
13464         local file2=$DIR/$tdir/$tfile-2
13465         local file3=$DIR/$tdir/$tfile-3
13466         local lovea
13467
13468         mkdir -p $DIR/$tdir
13469         touch $file1 || error "create $file1 failed"
13470         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
13471                 error "create $file2 failed"
13472         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
13473                 error "create $file3 failed"
13474
13475         $LFS swap_layouts $file1 $file2 ||
13476                 error "swap $file1 $file2 layouts failed"
13477
13478         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
13479         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
13480
13481         echo 123 > $file1 || error "Should be able to write into $file1"
13482
13483         $LFS swap_layouts $file1 $file3 ||
13484                 error "swap $file1 $file3 layouts failed"
13485
13486         echo 123 > $file1 || error "Should be able to write into $file1"
13487
13488         rm -rf $file1 $file2 $file3
13489 }
13490 run_test 184e "Recreate layout after stripeless layout swaps"
13491
13492 test_185() { # LU-2441
13493         # LU-3553 - no volatile file support in old servers
13494         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
13495                 skip "Need MDS version at least 2.3.60"
13496
13497         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
13498         touch $DIR/$tdir/spoo
13499         local mtime1=$(stat -c "%Y" $DIR/$tdir)
13500         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
13501                 error "cannot create/write a volatile file"
13502         [ "$FILESET" == "" ] &&
13503         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
13504                 error "FID is still valid after close"
13505
13506         multiop_bg_pause $DIR/$tdir vVw4096_c
13507         local multi_pid=$!
13508
13509         local OLD_IFS=$IFS
13510         IFS=":"
13511         local fidv=($fid)
13512         IFS=$OLD_IFS
13513         # assume that the next FID for this client is sequential, since stdout
13514         # is unfortunately eaten by multiop_bg_pause
13515         local n=$((${fidv[1]} + 1))
13516         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
13517         if [ "$FILESET" == "" ]; then
13518                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
13519                         error "FID is missing before close"
13520         fi
13521         kill -USR1 $multi_pid
13522         # 1 second delay, so if mtime change we will see it
13523         sleep 1
13524         local mtime2=$(stat -c "%Y" $DIR/$tdir)
13525         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
13526 }
13527 run_test 185 "Volatile file support"
13528
13529 test_187a() {
13530         remote_mds_nodsh && skip "remote MDS with nodsh"
13531         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
13532                 skip "Need MDS version at least 2.3.0"
13533
13534         local dir0=$DIR/$tdir/$testnum
13535         mkdir -p $dir0 || error "creating dir $dir0"
13536
13537         local file=$dir0/file1
13538         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
13539         local dv1=$($LFS data_version $file)
13540         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
13541         local dv2=$($LFS data_version $file)
13542         [[ $dv1 != $dv2 ]] ||
13543                 error "data version did not change on write $dv1 == $dv2"
13544
13545         # clean up
13546         rm -f $file1
13547 }
13548 run_test 187a "Test data version change"
13549
13550 test_187b() {
13551         remote_mds_nodsh && skip "remote MDS with nodsh"
13552         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
13553                 skip "Need MDS version at least 2.3.0"
13554
13555         local dir0=$DIR/$tdir/$testnum
13556         mkdir -p $dir0 || error "creating dir $dir0"
13557
13558         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
13559         [[ ${DV[0]} != ${DV[1]} ]] ||
13560                 error "data version did not change on write"\
13561                       " ${DV[0]} == ${DV[1]}"
13562
13563         # clean up
13564         rm -f $file1
13565 }
13566 run_test 187b "Test data version change on volatile file"
13567
13568 test_200() {
13569         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13570         remote_mgs_nodsh && skip "remote MGS with nodsh"
13571         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13572
13573         local POOL=${POOL:-cea1}
13574         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
13575         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
13576         # Pool OST targets
13577         local first_ost=0
13578         local last_ost=$(($OSTCOUNT - 1))
13579         local ost_step=2
13580         local ost_list=$(seq $first_ost $ost_step $last_ost)
13581         local ost_range="$first_ost $last_ost $ost_step"
13582         local test_path=$POOL_ROOT/$POOL_DIR_NAME
13583         local file_dir=$POOL_ROOT/file_tst
13584         local subdir=$test_path/subdir
13585         local rc=0
13586
13587         if ! combined_mgs_mds ; then
13588                 mount_mgs_client
13589         fi
13590
13591         while : ; do
13592                 # former test_200a test_200b
13593                 pool_add $POOL                          || { rc=$? ; break; }
13594                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
13595                 # former test_200c test_200d
13596                 mkdir -p $test_path
13597                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
13598                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
13599                 mkdir -p $subdir
13600                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
13601                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
13602                                                         || { rc=$? ; break; }
13603                 # former test_200e test_200f
13604                 local files=$((OSTCOUNT*3))
13605                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
13606                                                         || { rc=$? ; break; }
13607                 pool_create_files $POOL $file_dir $files "$ost_list" \
13608                                                         || { rc=$? ; break; }
13609                 # former test_200g test_200h
13610                 pool_lfs_df $POOL                       || { rc=$? ; break; }
13611                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
13612
13613                 # former test_201a test_201b test_201c
13614                 pool_remove_first_target $POOL          || { rc=$? ; break; }
13615
13616                 local f=$test_path/$tfile
13617                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
13618                 pool_remove $POOL $f                    || { rc=$? ; break; }
13619                 break
13620         done
13621
13622         destroy_test_pools
13623
13624         if ! combined_mgs_mds ; then
13625                 umount_mgs_client
13626         fi
13627         return $rc
13628 }
13629 run_test 200 "OST pools"
13630
13631 # usage: default_attr <count | size | offset>
13632 default_attr() {
13633         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
13634 }
13635
13636 # usage: check_default_stripe_attr
13637 check_default_stripe_attr() {
13638         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
13639         case $1 in
13640         --stripe-count|-c)
13641                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
13642         --stripe-size|-S)
13643                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
13644         --stripe-index|-i)
13645                 EXPECTED=-1;;
13646         *)
13647                 error "unknown getstripe attr '$1'"
13648         esac
13649
13650         [ $ACTUAL == $EXPECTED ] ||
13651                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
13652 }
13653
13654 test_204a() {
13655         test_mkdir $DIR/$tdir
13656         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
13657
13658         check_default_stripe_attr --stripe-count
13659         check_default_stripe_attr --stripe-size
13660         check_default_stripe_attr --stripe-index
13661 }
13662 run_test 204a "Print default stripe attributes"
13663
13664 test_204b() {
13665         test_mkdir $DIR/$tdir
13666         $SETSTRIPE --stripe-count 1 $DIR/$tdir
13667
13668         check_default_stripe_attr --stripe-size
13669         check_default_stripe_attr --stripe-index
13670 }
13671 run_test 204b "Print default stripe size and offset"
13672
13673 test_204c() {
13674         test_mkdir $DIR/$tdir
13675         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
13676
13677         check_default_stripe_attr --stripe-count
13678         check_default_stripe_attr --stripe-index
13679 }
13680 run_test 204c "Print default stripe count and offset"
13681
13682 test_204d() {
13683         test_mkdir $DIR/$tdir
13684         $SETSTRIPE --stripe-index 0 $DIR/$tdir
13685
13686         check_default_stripe_attr --stripe-count
13687         check_default_stripe_attr --stripe-size
13688 }
13689 run_test 204d "Print default stripe count and size"
13690
13691 test_204e() {
13692         test_mkdir $DIR/$tdir
13693         $SETSTRIPE -d $DIR/$tdir
13694
13695         check_default_stripe_attr --stripe-count --raw
13696         check_default_stripe_attr --stripe-size --raw
13697         check_default_stripe_attr --stripe-index --raw
13698 }
13699 run_test 204e "Print raw stripe attributes"
13700
13701 test_204f() {
13702         test_mkdir $DIR/$tdir
13703         $SETSTRIPE --stripe-count 1 $DIR/$tdir
13704
13705         check_default_stripe_attr --stripe-size --raw
13706         check_default_stripe_attr --stripe-index --raw
13707 }
13708 run_test 204f "Print raw stripe size and offset"
13709
13710 test_204g() {
13711         test_mkdir $DIR/$tdir
13712         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
13713
13714         check_default_stripe_attr --stripe-count --raw
13715         check_default_stripe_attr --stripe-index --raw
13716 }
13717 run_test 204g "Print raw stripe count and offset"
13718
13719 test_204h() {
13720         test_mkdir $DIR/$tdir
13721         $SETSTRIPE --stripe-index 0 $DIR/$tdir
13722
13723         check_default_stripe_attr --stripe-count --raw
13724         check_default_stripe_attr --stripe-size --raw
13725 }
13726 run_test 204h "Print raw stripe count and size"
13727
13728 # Figure out which job scheduler is being used, if any,
13729 # or use a fake one
13730 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
13731         JOBENV=SLURM_JOB_ID
13732 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
13733         JOBENV=LSB_JOBID
13734 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
13735         JOBENV=PBS_JOBID
13736 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
13737         JOBENV=LOADL_STEP_ID
13738 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
13739         JOBENV=JOB_ID
13740 else
13741         $LCTL list_param jobid_name > /dev/null 2>&1
13742         if [ $? -eq 0 ]; then
13743                 JOBENV=nodelocal
13744         else
13745                 JOBENV=FAKE_JOBID
13746         fi
13747 fi
13748 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
13749
13750 verify_jobstats() {
13751         local cmd=($1)
13752         shift
13753         local facets="$@"
13754
13755 # we don't really need to clear the stats for this test to work, since each
13756 # command has a unique jobid, but it makes debugging easier if needed.
13757 #       for facet in $facets; do
13758 #               local dev=$(convert_facet2label $facet)
13759 #               # clear old jobstats
13760 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
13761 #       done
13762
13763         # use a new JobID for each test, or we might see an old one
13764         [ "$JOBENV" = "FAKE_JOBID" ] &&
13765                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13766
13767         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
13768
13769         [ "$JOBENV" = "nodelocal" ] && {
13770                 FAKE_JOBID=id.$testnum.%e.$RANDOM
13771                 $LCTL set_param jobid_name=$FAKE_JOBID
13772                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
13773         }
13774
13775         log "Test: ${cmd[*]}"
13776         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
13777
13778         if [ $JOBENV = "FAKE_JOBID" ]; then
13779                 FAKE_JOBID=$JOBVAL ${cmd[*]}
13780         else
13781                 ${cmd[*]}
13782         fi
13783
13784         # all files are created on OST0000
13785         for facet in $facets; do
13786                 local stats="*.$(convert_facet2label $facet).job_stats"
13787
13788                 # strip out libtool wrappers for in-tree executables
13789                 if [ $(do_facet $facet lctl get_param $stats |
13790                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
13791                         do_facet $facet lctl get_param $stats
13792                         error "No jobstats for $JOBVAL found on $facet::$stats"
13793                 fi
13794         done
13795 }
13796
13797 jobstats_set() {
13798         local new_jobenv=$1
13799
13800         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$new_jobenv
13801         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $new_jobenv
13802 }
13803
13804 test_205() { # Job stats
13805         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13806         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
13807                 skip "Need MDS version with at least 2.7.1"
13808         remote_mgs_nodsh && skip "remote MGS with nodsh"
13809         remote_mds_nodsh && skip "remote MDS with nodsh"
13810         remote_ost_nodsh && skip "remote OST with nodsh"
13811         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
13812                 skip "Server doesn't support jobstats"
13813         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
13814
13815         local old_jobenv=$($LCTL get_param -n jobid_var)
13816         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
13817         stack_trap "do_facet mgs \
13818                 $LCTL conf_param $FSNAME.sys.jobid_var=$old_jobenv" EXIT
13819
13820         changelog_register
13821
13822         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
13823                                 mdt.*.job_cleanup_interval | head -n 1)
13824         local new_interval=5
13825         do_facet $SINGLEMDS \
13826                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
13827         stack_trap "do_facet $SINGLEMDS \
13828                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
13829         local start=$SECONDS
13830
13831         local cmd
13832         # mkdir
13833         cmd="mkdir $DIR/$tdir"
13834         verify_jobstats "$cmd" "$SINGLEMDS"
13835         # rmdir
13836         cmd="rmdir $DIR/$tdir"
13837         verify_jobstats "$cmd" "$SINGLEMDS"
13838         # mkdir on secondary MDT
13839         if [ $MDSCOUNT -gt 1 ]; then
13840                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
13841                 verify_jobstats "$cmd" "mds2"
13842         fi
13843         # mknod
13844         cmd="mknod $DIR/$tfile c 1 3"
13845         verify_jobstats "$cmd" "$SINGLEMDS"
13846         # unlink
13847         cmd="rm -f $DIR/$tfile"
13848         verify_jobstats "$cmd" "$SINGLEMDS"
13849         # create all files on OST0000 so verify_jobstats can find OST stats
13850         # open & close
13851         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
13852         verify_jobstats "$cmd" "$SINGLEMDS"
13853         # setattr
13854         cmd="touch $DIR/$tfile"
13855         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13856         # write
13857         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
13858         verify_jobstats "$cmd" "ost1"
13859         # read
13860         cancel_lru_locks osc
13861         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
13862         verify_jobstats "$cmd" "ost1"
13863         # truncate
13864         cmd="$TRUNCATE $DIR/$tfile 0"
13865         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13866         # rename
13867         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
13868         verify_jobstats "$cmd" "$SINGLEMDS"
13869         # jobstats expiry - sleep until old stats should be expired
13870         local left=$((new_interval + 5 - (SECONDS - start)))
13871         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
13872                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
13873                         "0" $left
13874         cmd="mkdir $DIR/$tdir.expire"
13875         verify_jobstats "$cmd" "$SINGLEMDS"
13876         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
13877             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
13878
13879         # Ensure that jobid are present in changelog (if supported by MDS)
13880         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ];then
13881                 changelog_dump | tail -10
13882                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
13883                 [ $jobids -eq 9 ] ||
13884                         error "Wrong changelog jobid count $jobids != 9"
13885
13886                 # LU-5862
13887                 JOBENV="disable"
13888                 jobstats_set $JOBENV
13889                 touch $DIR/$tfile
13890                 changelog_dump | grep $tfile
13891                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
13892                 [ $jobids -eq 0 ] ||
13893                         error "Unexpected jobids when jobid_var=$JOBENV"
13894         fi
13895
13896         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
13897         JOBENV="JOBCOMPLEX"
13898         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
13899
13900         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
13901 }
13902 run_test 205 "Verify job stats"
13903
13904 # LU-1480, LU-1773 and LU-1657
13905 test_206() {
13906         mkdir -p $DIR/$tdir
13907         $SETSTRIPE -c -1 $DIR/$tdir
13908 #define OBD_FAIL_LOV_INIT 0x1403
13909         $LCTL set_param fail_loc=0xa0001403
13910         $LCTL set_param fail_val=1
13911         touch $DIR/$tdir/$tfile || true
13912 }
13913 run_test 206 "fail lov_init_raid0() doesn't lbug"
13914
13915 test_207a() {
13916         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13917         local fsz=`stat -c %s $DIR/$tfile`
13918         cancel_lru_locks mdc
13919
13920         # do not return layout in getattr intent
13921 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
13922         $LCTL set_param fail_loc=0x170
13923         local sz=`stat -c %s $DIR/$tfile`
13924
13925         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
13926
13927         rm -rf $DIR/$tfile
13928 }
13929 run_test 207a "can refresh layout at glimpse"
13930
13931 test_207b() {
13932         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13933         local cksum=`md5sum $DIR/$tfile`
13934         local fsz=`stat -c %s $DIR/$tfile`
13935         cancel_lru_locks mdc
13936         cancel_lru_locks osc
13937
13938         # do not return layout in getattr intent
13939 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
13940         $LCTL set_param fail_loc=0x171
13941
13942         # it will refresh layout after the file is opened but before read issues
13943         echo checksum is "$cksum"
13944         echo "$cksum" |md5sum -c --quiet || error "file differs"
13945
13946         rm -rf $DIR/$tfile
13947 }
13948 run_test 207b "can refresh layout at open"
13949
13950 test_208() {
13951         # FIXME: in this test suite, only RD lease is used. This is okay
13952         # for now as only exclusive open is supported. After generic lease
13953         # is done, this test suite should be revised. - Jinshan
13954
13955         remote_mds_nodsh && skip "remote MDS with nodsh"
13956         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
13957                 skip "Need MDS version at least 2.4.52"
13958
13959         echo "==== test 1: verify get lease work"
13960         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
13961
13962         echo "==== test 2: verify lease can be broken by upcoming open"
13963         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13964         local PID=$!
13965         sleep 1
13966
13967         $MULTIOP $DIR/$tfile oO_RDONLY:c
13968         kill -USR1 $PID && wait $PID || error "break lease error"
13969
13970         echo "==== test 3: verify lease can't be granted if an open already exists"
13971         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
13972         local PID=$!
13973         sleep 1
13974
13975         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
13976         kill -USR1 $PID && wait $PID || error "open file error"
13977
13978         echo "==== test 4: lease can sustain over recovery"
13979         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
13980         PID=$!
13981         sleep 1
13982
13983         fail mds1
13984
13985         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
13986
13987         echo "==== test 5: lease broken can't be regained by replay"
13988         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13989         PID=$!
13990         sleep 1
13991
13992         # open file to break lease and then recovery
13993         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
13994         fail mds1
13995
13996         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
13997
13998         rm -f $DIR/$tfile
13999 }
14000 run_test 208 "Exclusive open"
14001
14002 test_209() {
14003         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
14004                 skip_env "must have disp_stripe"
14005
14006         touch $DIR/$tfile
14007         sync; sleep 5; sync;
14008
14009         echo 3 > /proc/sys/vm/drop_caches
14010         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14011
14012         # open/close 500 times
14013         for i in $(seq 500); do
14014                 cat $DIR/$tfile
14015         done
14016
14017         echo 3 > /proc/sys/vm/drop_caches
14018         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
14019
14020         echo "before: $req_before, after: $req_after"
14021         [ $((req_after - req_before)) -ge 300 ] &&
14022                 error "open/close requests are not freed"
14023         return 0
14024 }
14025 run_test 209 "read-only open/close requests should be freed promptly"
14026
14027 test_212() {
14028         size=`date +%s`
14029         size=$((size % 8192 + 1))
14030         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
14031         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
14032         rm -f $DIR/f212 $DIR/f212.xyz
14033 }
14034 run_test 212 "Sendfile test ============================================"
14035
14036 test_213() {
14037         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
14038         cancel_lru_locks osc
14039         lctl set_param fail_loc=0x8000040f
14040         # generate a read lock
14041         cat $DIR/$tfile > /dev/null
14042         # write to the file, it will try to cancel the above read lock.
14043         cat /etc/hosts >> $DIR/$tfile
14044 }
14045 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
14046
14047 test_214() { # for bug 20133
14048         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
14049         for (( i=0; i < 340; i++ )) ; do
14050                 touch $DIR/$tdir/d214c/a$i
14051         done
14052
14053         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
14054         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
14055         ls $DIR/d214c || error "ls $DIR/d214c failed"
14056         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
14057         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
14058 }
14059 run_test 214 "hash-indexed directory test - bug 20133"
14060
14061 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
14062 create_lnet_proc_files() {
14063         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
14064 }
14065
14066 # counterpart of create_lnet_proc_files
14067 remove_lnet_proc_files() {
14068         rm -f $TMP/lnet_$1.sys
14069 }
14070
14071 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14072 # 3rd arg as regexp for body
14073 check_lnet_proc_stats() {
14074         local l=$(cat "$TMP/lnet_$1" |wc -l)
14075         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
14076
14077         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
14078 }
14079
14080 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
14081 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
14082 # optional and can be regexp for 2nd line (lnet.routes case)
14083 check_lnet_proc_entry() {
14084         local blp=2          # blp stands for 'position of 1st line of body'
14085         [ -z "$5" ] || blp=3 # lnet.routes case
14086
14087         local l=$(cat "$TMP/lnet_$1" |wc -l)
14088         # subtracting one from $blp because the body can be empty
14089         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
14090
14091         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
14092                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
14093
14094         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
14095                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
14096
14097         # bail out if any unexpected line happened
14098         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
14099         [ "$?" != 0 ] || error "$2 misformatted"
14100 }
14101
14102 test_215() { # for bugs 18102, 21079, 21517
14103         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14104
14105         local N='(0|[1-9][0-9]*)'       # non-negative numeric
14106         local P='[1-9][0-9]*'           # positive numeric
14107         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
14108         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
14109         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
14110         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
14111
14112         local L1 # regexp for 1st line
14113         local L2 # regexp for 2nd line (optional)
14114         local BR # regexp for the rest (body)
14115
14116         # lnet.stats should look as 11 space-separated non-negative numerics
14117         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
14118         create_lnet_proc_files "stats"
14119         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
14120         remove_lnet_proc_files "stats"
14121
14122         # lnet.routes should look like this:
14123         # Routing disabled/enabled
14124         # net hops priority state router
14125         # where net is a string like tcp0, hops > 0, priority >= 0,
14126         # state is up/down,
14127         # router is a string like 192.168.1.1@tcp2
14128         L1="^Routing (disabled|enabled)$"
14129         L2="^net +hops +priority +state +router$"
14130         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
14131         create_lnet_proc_files "routes"
14132         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
14133         remove_lnet_proc_files "routes"
14134
14135         # lnet.routers should look like this:
14136         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
14137         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
14138         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
14139         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
14140         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
14141         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
14142         create_lnet_proc_files "routers"
14143         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
14144         remove_lnet_proc_files "routers"
14145
14146         # lnet.peers should look like this:
14147         # nid refs state last max rtr min tx min queue
14148         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
14149         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
14150         # numeric (0 or >0 or <0), queue >= 0.
14151         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
14152         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
14153         create_lnet_proc_files "peers"
14154         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
14155         remove_lnet_proc_files "peers"
14156
14157         # lnet.buffers  should look like this:
14158         # pages count credits min
14159         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
14160         L1="^pages +count +credits +min$"
14161         BR="^ +$N +$N +$I +$I$"
14162         create_lnet_proc_files "buffers"
14163         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
14164         remove_lnet_proc_files "buffers"
14165
14166         # lnet.nis should look like this:
14167         # nid status alive refs peer rtr max tx min
14168         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
14169         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
14170         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
14171         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
14172         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
14173         create_lnet_proc_files "nis"
14174         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
14175         remove_lnet_proc_files "nis"
14176
14177         # can we successfully write to lnet.stats?
14178         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
14179 }
14180 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
14181
14182 test_216() { # bug 20317
14183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14184         remote_ost_nodsh && skip "remote OST with nodsh"
14185
14186         local node
14187         local facets=$(get_facets OST)
14188         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14189
14190         save_lustre_params client "osc.*.contention_seconds" > $p
14191         save_lustre_params $facets \
14192                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
14193         save_lustre_params $facets \
14194                 "ldlm.namespaces.filter-*.contended_locks" >> $p
14195         save_lustre_params $facets \
14196                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
14197         clear_stats osc.*.osc_stats
14198
14199         # agressive lockless i/o settings
14200         do_nodes $(comma_list $(osts_nodes)) \
14201                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
14202                         ldlm.namespaces.filter-*.contended_locks=0 \
14203                         ldlm.namespaces.filter-*.contention_seconds=60"
14204         lctl set_param -n osc.*.contention_seconds=60
14205
14206         $DIRECTIO write $DIR/$tfile 0 10 4096
14207         $CHECKSTAT -s 40960 $DIR/$tfile
14208
14209         # disable lockless i/o
14210         do_nodes $(comma_list $(osts_nodes)) \
14211                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
14212                         ldlm.namespaces.filter-*.contended_locks=32 \
14213                         ldlm.namespaces.filter-*.contention_seconds=0"
14214         lctl set_param -n osc.*.contention_seconds=0
14215         clear_stats osc.*.osc_stats
14216
14217         dd if=/dev/zero of=$DIR/$tfile count=0
14218         $CHECKSTAT -s 0 $DIR/$tfile
14219
14220         restore_lustre_params <$p
14221         rm -f $p
14222         rm $DIR/$tfile
14223 }
14224 run_test 216 "check lockless direct write updates file size and kms correctly"
14225
14226 test_217() { # bug 22430
14227         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14228
14229         local node
14230         local nid
14231
14232         for node in $(nodes_list); do
14233                 nid=$(host_nids_address $node $NETTYPE)
14234                 if [[ $nid = *-* ]] ; then
14235                         echo "lctl ping $(h2nettype $nid)"
14236                         lctl ping $(h2nettype $nid)
14237                 else
14238                         echo "skipping $node (no hyphen detected)"
14239                 fi
14240         done
14241 }
14242 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
14243
14244 test_218() {
14245        # do directio so as not to populate the page cache
14246        log "creating a 10 Mb file"
14247        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14248        log "starting reads"
14249        dd if=$DIR/$tfile of=/dev/null bs=4096 &
14250        log "truncating the file"
14251        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14252        log "killing dd"
14253        kill %+ || true # reads might have finished
14254        echo "wait until dd is finished"
14255        wait
14256        log "removing the temporary file"
14257        rm -rf $DIR/$tfile || error "tmp file removal failed"
14258 }
14259 run_test 218 "parallel read and truncate should not deadlock"
14260
14261 test_219() {
14262         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14263
14264         # write one partial page
14265         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
14266         # set no grant so vvp_io_commit_write will do sync write
14267         $LCTL set_param fail_loc=0x411
14268         # write a full page at the end of file
14269         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
14270
14271         $LCTL set_param fail_loc=0
14272         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
14273         $LCTL set_param fail_loc=0x411
14274         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
14275
14276         # LU-4201
14277         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
14278         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
14279 }
14280 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
14281
14282 test_220() { #LU-325
14283         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14284         remote_ost_nodsh && skip "remote OST with nodsh"
14285         remote_mds_nodsh && skip "remote MDS with nodsh"
14286         remote_mgs_nodsh && skip "remote MGS with nodsh"
14287
14288         local OSTIDX=0
14289
14290         # create on MDT0000 so the last_id and next_id are correct
14291         mkdir $DIR/$tdir
14292         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
14293         OST=${OST%_UUID}
14294
14295         # on the mdt's osc
14296         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
14297         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
14298                         osc.$mdtosc_proc1.prealloc_last_id)
14299         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
14300                         osc.$mdtosc_proc1.prealloc_next_id)
14301
14302         $LFS df -i
14303
14304         if ! combined_mgs_mds ; then
14305                 mount_mgs_client
14306         fi
14307
14308         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
14309         #define OBD_FAIL_OST_ENOINO              0x229
14310         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
14311         create_pool $FSNAME.$TESTNAME || return 1
14312         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
14313
14314         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
14315
14316         MDSOBJS=$((last_id - next_id))
14317         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
14318
14319         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
14320         echo "OST still has $count kbytes free"
14321
14322         echo "create $MDSOBJS files @next_id..."
14323         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
14324
14325         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14326                         osc.$mdtosc_proc1.prealloc_last_id)
14327         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
14328                         osc.$mdtosc_proc1.prealloc_next_id)
14329
14330         echo "after creation, last_id=$last_id2, next_id=$next_id2"
14331         $LFS df -i
14332
14333         echo "cleanup..."
14334
14335         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
14336         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
14337
14338         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
14339                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
14340         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14341                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
14342         echo "unlink $MDSOBJS files @$next_id..."
14343         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
14344
14345         if ! combined_mgs_mds ; then
14346                 umount_mgs_client
14347         fi
14348 }
14349 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
14350
14351 test_221() {
14352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14353
14354         dd if=`which date` of=$MOUNT/date oflag=sync
14355         chmod +x $MOUNT/date
14356
14357         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
14358         $LCTL set_param fail_loc=0x80001401
14359
14360         $MOUNT/date > /dev/null
14361         rm -f $MOUNT/date
14362 }
14363 run_test 221 "make sure fault and truncate race to not cause OOM"
14364
14365 test_222a () {
14366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14367
14368         rm -rf $DIR/$tdir
14369         test_mkdir $DIR/$tdir
14370         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14371         createmany -o $DIR/$tdir/$tfile 10
14372         cancel_lru_locks mdc
14373         cancel_lru_locks osc
14374         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
14375         $LCTL set_param fail_loc=0x31a
14376         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
14377         $LCTL set_param fail_loc=0
14378         rm -r $DIR/$tdir
14379 }
14380 run_test 222a "AGL for ls should not trigger CLIO lock failure"
14381
14382 test_222b () {
14383         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14384
14385         rm -rf $DIR/$tdir
14386         test_mkdir $DIR/$tdir
14387         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14388         createmany -o $DIR/$tdir/$tfile 10
14389         cancel_lru_locks mdc
14390         cancel_lru_locks osc
14391         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
14392         $LCTL set_param fail_loc=0x31a
14393         rm -r $DIR/$tdir || error "AGL for rmdir failed"
14394         $LCTL set_param fail_loc=0
14395 }
14396 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
14397
14398 test_223 () {
14399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14400
14401         rm -rf $DIR/$tdir
14402         test_mkdir $DIR/$tdir
14403         $LFS setstripe -c 1 -i 0 $DIR/$tdir
14404         createmany -o $DIR/$tdir/$tfile 10
14405         cancel_lru_locks mdc
14406         cancel_lru_locks osc
14407         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
14408         $LCTL set_param fail_loc=0x31b
14409         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
14410         $LCTL set_param fail_loc=0
14411         rm -r $DIR/$tdir
14412 }
14413 run_test 223 "osc reenqueue if without AGL lock granted ======================="
14414
14415 test_224a() { # LU-1039, MRP-303
14416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14417
14418         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
14419         $LCTL set_param fail_loc=0x508
14420         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
14421         $LCTL set_param fail_loc=0
14422         df $DIR
14423 }
14424 run_test 224a "Don't panic on bulk IO failure"
14425
14426 test_224b() { # LU-1039, MRP-303
14427         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14428
14429         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
14430         cancel_lru_locks osc
14431         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
14432         $LCTL set_param fail_loc=0x515
14433         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
14434         $LCTL set_param fail_loc=0
14435         df $DIR
14436 }
14437 run_test 224b "Don't panic on bulk IO failure"
14438
14439 test_224c() { # LU-6441
14440         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14441         remote_mds_nodsh && skip "remote MDS with nodsh"
14442
14443         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
14444         save_writethrough $p
14445         set_cache writethrough on
14446
14447         local pages_per_rpc=$($LCTL get_param \
14448                                 osc.*.max_pages_per_rpc)
14449         local at_max=$($LCTL get_param -n at_max)
14450         local timeout=$($LCTL get_param -n timeout)
14451         local test_at="$LCTL get_param -n at_max"
14452         local param_at="$FSNAME.sys.at_max"
14453         local test_timeout="$LCTL get_param -n timeout"
14454         local param_timeout="$FSNAME.sys.timeout"
14455
14456         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
14457
14458         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
14459                 error "conf_param at_max=0 failed"
14460         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
14461                 error "conf_param timeout=5 failed"
14462
14463         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
14464         do_facet ost1 $LCTL set_param fail_loc=0x520
14465         $LFS setstripe -c 1 -i 0 $DIR/$tfile
14466         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
14467         sync
14468         do_facet ost1 $LCTL set_param fail_loc=0
14469
14470         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
14471                 error "conf_param at_max=$at_max failed"
14472         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
14473                 $timeout || error "conf_param timeout=$timeout failed"
14474
14475         $LCTL set_param -n $pages_per_rpc
14476         restore_lustre_params < $p
14477         rm -f $p
14478 }
14479 run_test 224c "Don't hang if one of md lost during large bulk RPC"
14480
14481 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
14482 test_225a () {
14483         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14484         if [ -z ${MDSSURVEY} ]; then
14485                 skip_env "mds-survey not found"
14486         fi
14487         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
14488                 skip "Need MDS version at least 2.2.51"
14489
14490         local mds=$(facet_host $SINGLEMDS)
14491         local target=$(do_nodes $mds 'lctl dl' |
14492                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
14493
14494         local cmd1="file_count=1000 thrhi=4"
14495         local cmd2="dir_count=2 layer=mdd stripe_count=0"
14496         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
14497         local cmd="$cmd1 $cmd2 $cmd3"
14498
14499         rm -f ${TMP}/mds_survey*
14500         echo + $cmd
14501         eval $cmd || error "mds-survey with zero-stripe failed"
14502         cat ${TMP}/mds_survey*
14503         rm -f ${TMP}/mds_survey*
14504 }
14505 run_test 225a "Metadata survey sanity with zero-stripe"
14506
14507 test_225b () {
14508         if [ -z ${MDSSURVEY} ]; then
14509                 skip_env "mds-survey not found"
14510         fi
14511         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
14512                 skip "Need MDS version at least 2.2.51"
14513         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14514         remote_mds_nodsh && skip "remote MDS with nodsh"
14515         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
14516                 skip_env "Need to mount OST to test"
14517         fi
14518
14519         local mds=$(facet_host $SINGLEMDS)
14520         local target=$(do_nodes $mds 'lctl dl' |
14521                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
14522
14523         local cmd1="file_count=1000 thrhi=4"
14524         local cmd2="dir_count=2 layer=mdd stripe_count=1"
14525         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
14526         local cmd="$cmd1 $cmd2 $cmd3"
14527
14528         rm -f ${TMP}/mds_survey*
14529         echo + $cmd
14530         eval $cmd || error "mds-survey with stripe_count failed"
14531         cat ${TMP}/mds_survey*
14532         rm -f ${TMP}/mds_survey*
14533 }
14534 run_test 225b "Metadata survey sanity with stripe_count = 1"
14535
14536 mcreate_path2fid () {
14537         local mode=$1
14538         local major=$2
14539         local minor=$3
14540         local name=$4
14541         local desc=$5
14542         local path=$DIR/$tdir/$name
14543         local fid
14544         local rc
14545         local fid_path
14546
14547         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
14548                 error "cannot create $desc"
14549
14550         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
14551         rc=$?
14552         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
14553
14554         fid_path=$($LFS fid2path $MOUNT $fid)
14555         rc=$?
14556         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
14557
14558         [ "$path" == "$fid_path" ] ||
14559                 error "fid2path returned $fid_path, expected $path"
14560
14561         echo "pass with $path and $fid"
14562 }
14563
14564 test_226a () {
14565         rm -rf $DIR/$tdir
14566         mkdir -p $DIR/$tdir
14567
14568         mcreate_path2fid 0010666 0 0 fifo "FIFO"
14569         mcreate_path2fid 0020666 1 3 null "character special file (null)"
14570         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
14571         mcreate_path2fid 0040666 0 0 dir "directory"
14572         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
14573         mcreate_path2fid 0100666 0 0 file "regular file"
14574         mcreate_path2fid 0120666 0 0 link "symbolic link"
14575         mcreate_path2fid 0140666 0 0 sock "socket"
14576 }
14577 run_test 226a "call path2fid and fid2path on files of all type"
14578
14579 test_226b () {
14580         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14581
14582         local MDTIDX=1
14583
14584         rm -rf $DIR/$tdir
14585         mkdir -p $DIR/$tdir
14586         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
14587                 error "create remote directory failed"
14588         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
14589         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
14590                                 "character special file (null)"
14591         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
14592                                 "character special file (no device)"
14593         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
14594         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
14595                                 "block special file (loop)"
14596         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
14597         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
14598         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
14599 }
14600 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
14601
14602 # LU-1299 Executing or running ldd on a truncated executable does not
14603 # cause an out-of-memory condition.
14604 test_227() {
14605         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14606         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
14607
14608         dd if=$(which date) of=$MOUNT/date bs=1k count=1
14609         chmod +x $MOUNT/date
14610
14611         $MOUNT/date > /dev/null
14612         ldd $MOUNT/date > /dev/null
14613         rm -f $MOUNT/date
14614 }
14615 run_test 227 "running truncated executable does not cause OOM"
14616
14617 # LU-1512 try to reuse idle OI blocks
14618 test_228a() {
14619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14620         remote_mds_nodsh && skip "remote MDS with nodsh"
14621         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
14622                 skip_env "ldiskfs only test"
14623
14624         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14625         local myDIR=$DIR/$tdir
14626
14627         mkdir -p $myDIR
14628         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14629         $LCTL set_param fail_loc=0x80001002
14630         createmany -o $myDIR/t- 10000
14631         $LCTL set_param fail_loc=0
14632         # The guard is current the largest FID holder
14633         touch $myDIR/guard
14634         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14635                     tr -d '[')
14636         local IDX=$(($SEQ % 64))
14637
14638         do_facet $SINGLEMDS sync
14639         # Make sure journal flushed.
14640         sleep 6
14641         local blk1=$(do_facet $SINGLEMDS \
14642                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14643                      grep Blockcount | awk '{print $4}')
14644
14645         # Remove old files, some OI blocks will become idle.
14646         unlinkmany $myDIR/t- 10000
14647         # Create new files, idle OI blocks should be reused.
14648         createmany -o $myDIR/t- 2000
14649         do_facet $SINGLEMDS sync
14650         # Make sure journal flushed.
14651         sleep 6
14652         local blk2=$(do_facet $SINGLEMDS \
14653                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14654                      grep Blockcount | awk '{print $4}')
14655
14656         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14657 }
14658 run_test 228a "try to reuse idle OI blocks"
14659
14660 test_228b() {
14661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14662         remote_mds_nodsh && skip "remote MDS with nodsh"
14663         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
14664                 skip_env "ldiskfs only test"
14665
14666         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14667         local myDIR=$DIR/$tdir
14668
14669         mkdir -p $myDIR
14670         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14671         $LCTL set_param fail_loc=0x80001002
14672         createmany -o $myDIR/t- 10000
14673         $LCTL set_param fail_loc=0
14674         # The guard is current the largest FID holder
14675         touch $myDIR/guard
14676         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14677                     tr -d '[')
14678         local IDX=$(($SEQ % 64))
14679
14680         do_facet $SINGLEMDS sync
14681         # Make sure journal flushed.
14682         sleep 6
14683         local blk1=$(do_facet $SINGLEMDS \
14684                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14685                      grep Blockcount | awk '{print $4}')
14686
14687         # Remove old files, some OI blocks will become idle.
14688         unlinkmany $myDIR/t- 10000
14689
14690         # stop the MDT
14691         stop $SINGLEMDS || error "Fail to stop MDT."
14692         # remount the MDT
14693         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
14694
14695         df $MOUNT || error "Fail to df."
14696         # Create new files, idle OI blocks should be reused.
14697         createmany -o $myDIR/t- 2000
14698         do_facet $SINGLEMDS sync
14699         # Make sure journal flushed.
14700         sleep 6
14701         local blk2=$(do_facet $SINGLEMDS \
14702                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14703                      grep Blockcount | awk '{print $4}')
14704
14705         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14706 }
14707 run_test 228b "idle OI blocks can be reused after MDT restart"
14708
14709 #LU-1881
14710 test_228c() {
14711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14712         remote_mds_nodsh && skip "remote MDS with nodsh"
14713         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
14714                 skip_env "ldiskfs only test"
14715
14716         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14717         local myDIR=$DIR/$tdir
14718
14719         mkdir -p $myDIR
14720         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14721         $LCTL set_param fail_loc=0x80001002
14722         # 20000 files can guarantee there are index nodes in the OI file
14723         createmany -o $myDIR/t- 20000
14724         $LCTL set_param fail_loc=0
14725         # The guard is current the largest FID holder
14726         touch $myDIR/guard
14727         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14728                     tr -d '[')
14729         local IDX=$(($SEQ % 64))
14730
14731         do_facet $SINGLEMDS sync
14732         # Make sure journal flushed.
14733         sleep 6
14734         local blk1=$(do_facet $SINGLEMDS \
14735                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14736                      grep Blockcount | awk '{print $4}')
14737
14738         # Remove old files, some OI blocks will become idle.
14739         unlinkmany $myDIR/t- 20000
14740         rm -f $myDIR/guard
14741         # The OI file should become empty now
14742
14743         # Create new files, idle OI blocks should be reused.
14744         createmany -o $myDIR/t- 2000
14745         do_facet $SINGLEMDS sync
14746         # Make sure journal flushed.
14747         sleep 6
14748         local blk2=$(do_facet $SINGLEMDS \
14749                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14750                      grep Blockcount | awk '{print $4}')
14751
14752         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14753 }
14754 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
14755
14756 test_229() { # LU-2482, LU-3448
14757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14758         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
14759         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
14760                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
14761
14762         rm -f $DIR/$tfile
14763
14764         # Create a file with a released layout and stripe count 2.
14765         $MULTIOP $DIR/$tfile H2c ||
14766                 error "failed to create file with released layout"
14767
14768         $GETSTRIPE -v $DIR/$tfile
14769
14770         local pattern=$($GETSTRIPE -L $DIR/$tfile)
14771         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
14772
14773         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
14774         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
14775         stat $DIR/$tfile || error "failed to stat released file"
14776
14777         chown $RUNAS_ID $DIR/$tfile ||
14778                 error "chown $RUNAS_ID $DIR/$tfile failed"
14779
14780         chgrp $RUNAS_ID $DIR/$tfile ||
14781                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
14782
14783         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
14784         rm $DIR/$tfile || error "failed to remove released file"
14785 }
14786 run_test 229 "getstripe/stat/rm/attr changes work on released files"
14787
14788 test_230a() {
14789         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14790         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14791
14792         local MDTIDX=1
14793
14794         test_mkdir $DIR/$tdir
14795         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
14796         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
14797         [ $mdt_idx -ne 0 ] &&
14798                 error "create local directory on wrong MDT $mdt_idx"
14799
14800         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
14801                         error "create remote directory failed"
14802         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
14803         [ $mdt_idx -ne $MDTIDX ] &&
14804                 error "create remote directory on wrong MDT $mdt_idx"
14805
14806         createmany -o $DIR/$tdir/test_230/t- 10 ||
14807                 error "create files on remote directory failed"
14808         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
14809         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
14810         rm -r $DIR/$tdir || error "unlink remote directory failed"
14811 }
14812 run_test 230a "Create remote directory and files under the remote directory"
14813
14814 test_230b() {
14815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14816         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14817
14818         local MDTIDX=1
14819         local mdt_index
14820         local i
14821         local file
14822         local pid
14823         local stripe_count
14824         local migrate_dir=$DIR/$tdir/migrate_dir
14825         local other_dir=$DIR/$tdir/other_dir
14826
14827         test_mkdir $DIR/$tdir
14828         test_mkdir -i0 -c1 $migrate_dir
14829         test_mkdir -i0 -c1 $other_dir
14830         for ((i=0; i<10; i++)); do
14831                 mkdir -p $migrate_dir/dir_${i}
14832                 createmany -o $migrate_dir/dir_${i}/f 10 ||
14833                         error "create files under remote dir failed $i"
14834         done
14835
14836         cp /etc/passwd $migrate_dir/$tfile
14837         cp /etc/passwd $other_dir/$tfile
14838         chattr +SAD $migrate_dir
14839         chattr +SAD $migrate_dir/$tfile
14840
14841         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14842         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14843         local old_dir_mode=$(stat -c%f $migrate_dir)
14844         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
14845
14846         mkdir -p $migrate_dir/dir_default_stripe2
14847         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
14848         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
14849
14850         mkdir -p $other_dir
14851         ln $migrate_dir/$tfile $other_dir/luna
14852         ln $migrate_dir/$tfile $migrate_dir/sofia
14853         ln $other_dir/$tfile $migrate_dir/david
14854         ln -s $migrate_dir/$tfile $other_dir/zachary
14855         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
14856         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
14857
14858         $LFS migrate -m $MDTIDX $migrate_dir ||
14859                 error "fails on migrating remote dir to MDT1"
14860
14861         echo "migratate to MDT1, then checking.."
14862         for ((i = 0; i < 10; i++)); do
14863                 for file in $(find $migrate_dir/dir_${i}); do
14864                         mdt_index=$($LFS getstripe -m $file)
14865                         [ $mdt_index == $MDTIDX ] ||
14866                                 error "$file is not on MDT${MDTIDX}"
14867                 done
14868         done
14869
14870         # the multiple link file should still in MDT0
14871         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
14872         [ $mdt_index == 0 ] ||
14873                 error "$file is not on MDT${MDTIDX}"
14874
14875         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14876         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14877                 error " expect $old_dir_flag get $new_dir_flag"
14878
14879         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14880         [ "$old_file_flag" = "$new_file_flag" ] ||
14881                 error " expect $old_file_flag get $new_file_flag"
14882
14883         local new_dir_mode=$(stat -c%f $migrate_dir)
14884         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14885                 error "expect mode $old_dir_mode get $new_dir_mode"
14886
14887         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14888         [ "$old_file_mode" = "$new_file_mode" ] ||
14889                 error "expect mode $old_file_mode get $new_file_mode"
14890
14891         diff /etc/passwd $migrate_dir/$tfile ||
14892                 error "$tfile different after migration"
14893
14894         diff /etc/passwd $other_dir/luna ||
14895                 error "luna different after migration"
14896
14897         diff /etc/passwd $migrate_dir/sofia ||
14898                 error "sofia different after migration"
14899
14900         diff /etc/passwd $migrate_dir/david ||
14901                 error "david different after migration"
14902
14903         diff /etc/passwd $other_dir/zachary ||
14904                 error "zachary different after migration"
14905
14906         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14907                 error "${tfile}_ln different after migration"
14908
14909         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14910                 error "${tfile}_ln_other different after migration"
14911
14912         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
14913         [ $stripe_count = 2 ] ||
14914                 error "dir strpe_count $d != 2 after migration."
14915
14916         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
14917         [ $stripe_count = 2 ] ||
14918                 error "file strpe_count $d != 2 after migration."
14919
14920         #migrate back to MDT0
14921         MDTIDX=0
14922
14923         $LFS migrate -m $MDTIDX $migrate_dir ||
14924                 error "fails on migrating remote dir to MDT0"
14925
14926         echo "migrate back to MDT0, checking.."
14927         for file in $(find $migrate_dir); do
14928                 mdt_index=$($LFS getstripe -m $file)
14929                 [ $mdt_index == $MDTIDX ] ||
14930                         error "$file is not on MDT${MDTIDX}"
14931         done
14932
14933         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14934         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14935                 error " expect $old_dir_flag get $new_dir_flag"
14936
14937         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14938         [ "$old_file_flag" = "$new_file_flag" ] ||
14939                 error " expect $old_file_flag get $new_file_flag"
14940
14941         local new_dir_mode=$(stat -c%f $migrate_dir)
14942         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14943                 error "expect mode $old_dir_mode get $new_dir_mode"
14944
14945         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14946         [ "$old_file_mode" = "$new_file_mode" ] ||
14947                 error "expect mode $old_file_mode get $new_file_mode"
14948
14949         diff /etc/passwd ${migrate_dir}/$tfile ||
14950                 error "$tfile different after migration"
14951
14952         diff /etc/passwd ${other_dir}/luna ||
14953                 error "luna different after migration"
14954
14955         diff /etc/passwd ${migrate_dir}/sofia ||
14956                 error "sofia different after migration"
14957
14958         diff /etc/passwd ${other_dir}/zachary ||
14959                 error "zachary different after migration"
14960
14961         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14962                 error "${tfile}_ln different after migration"
14963
14964         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14965                 error "${tfile}_ln_other different after migration"
14966
14967         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
14968         [ $stripe_count = 2 ] ||
14969                 error "dir strpe_count $d != 2 after migration."
14970
14971         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
14972         [ $stripe_count = 2 ] ||
14973                 error "file strpe_count $d != 2 after migration."
14974
14975         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14976 }
14977 run_test 230b "migrate directory"
14978
14979 test_230c() {
14980         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14981         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14982         remote_mds_nodsh && skip "remote MDS with nodsh"
14983
14984         local MDTIDX=1
14985         local mdt_index
14986         local file
14987         local migrate_dir=$DIR/$tdir/migrate_dir
14988
14989         #If migrating directory fails in the middle, all entries of
14990         #the directory is still accessiable.
14991         test_mkdir $DIR/$tdir
14992         test_mkdir -i0 -c1 $migrate_dir
14993         stat $migrate_dir
14994         createmany -o $migrate_dir/f 10 ||
14995                 error "create files under ${migrate_dir} failed"
14996
14997         #failed after migrating 5 entries
14998         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
14999         do_facet mds1 lctl set_param fail_loc=0x20001801
15000         do_facet mds1 lctl  set_param fail_val=5
15001         local t=$(ls $migrate_dir | wc -l)
15002         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
15003                 error "migrate should fail after 5 entries"
15004
15005         mkdir $migrate_dir/dir &&
15006                 error "mkdir succeeds under migrating directory"
15007         touch $migrate_dir/file &&
15008                 error "touch file succeeds under migrating directory"
15009
15010         local u=$(ls $migrate_dir | wc -l)
15011         [ "$u" == "$t" ] || error "$u != $t during migration"
15012
15013         for file in $(find $migrate_dir); do
15014                 stat $file || error "stat $file failed"
15015         done
15016
15017         do_facet mds1 lctl set_param fail_loc=0
15018         do_facet mds1 lctl set_param fail_val=0
15019
15020         $LFS migrate -m $MDTIDX $migrate_dir ||
15021                 error "migrate open files should failed with open files"
15022
15023         echo "Finish migration, then checking.."
15024         for file in $(find $migrate_dir); do
15025                 mdt_index=$($LFS getstripe -m $file)
15026                 [ $mdt_index == $MDTIDX ] ||
15027                         error "$file is not on MDT${MDTIDX}"
15028         done
15029
15030         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15031 }
15032 run_test 230c "check directory accessiblity if migration is failed"
15033
15034 test_230d() {
15035         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15036         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15037
15038         local MDTIDX=1
15039         local mdt_index
15040         local migrate_dir=$DIR/$tdir/migrate_dir
15041         local i
15042         local j
15043
15044         test_mkdir $DIR/$tdir
15045         test_mkdir -i0 -c1 $migrate_dir
15046
15047         for ((i=0; i<100; i++)); do
15048                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
15049                 createmany -o $migrate_dir/dir_${i}/f 100 ||
15050                         error "create files under remote dir failed $i"
15051         done
15052
15053         $LFS migrate -m $MDTIDX $migrate_dir ||
15054                 error "migrate remote dir error"
15055
15056         echo "Finish migration, then checking.."
15057         for file in $(find $migrate_dir); do
15058                 mdt_index=$($LFS getstripe -m $file)
15059                 [ $mdt_index == $MDTIDX ] ||
15060                         error "$file is not on MDT${MDTIDX}"
15061         done
15062
15063         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15064 }
15065 run_test 230d "check migrate big directory"
15066
15067 test_230e() {
15068         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15069         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15070
15071         local i
15072         local j
15073         local a_fid
15074         local b_fid
15075
15076         mkdir -p $DIR/$tdir
15077         mkdir $DIR/$tdir/migrate_dir
15078         mkdir $DIR/$tdir/other_dir
15079         touch $DIR/$tdir/migrate_dir/a
15080         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
15081         ls $DIR/$tdir/other_dir
15082
15083         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15084                 error "migrate dir fails"
15085
15086         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15087         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15088
15089         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15090         [ $mdt_index == 0 ] || error "a is not on MDT0"
15091
15092         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
15093                 error "migrate dir fails"
15094
15095         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
15096         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
15097
15098         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15099         [ $mdt_index == 1 ] || error "a is not on MDT1"
15100
15101         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
15102         [ $mdt_index == 1 ] || error "b is not on MDT1"
15103
15104         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15105         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
15106
15107         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
15108
15109         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15110 }
15111 run_test 230e "migrate mulitple local link files"
15112
15113 test_230f() {
15114         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15115         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15116
15117         local a_fid
15118         local ln_fid
15119
15120         mkdir -p $DIR/$tdir
15121         mkdir $DIR/$tdir/migrate_dir
15122         $LFS mkdir -i1 $DIR/$tdir/other_dir
15123         touch $DIR/$tdir/migrate_dir/a
15124         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
15125         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
15126         ls $DIR/$tdir/other_dir
15127
15128         # a should be migrated to MDT1, since no other links on MDT0
15129         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15130                 error "#1 migrate dir fails"
15131         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15132         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
15133         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15134         [ $mdt_index == 1 ] || error "a is not on MDT1"
15135
15136         # a should stay on MDT1, because it is a mulitple link file
15137         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15138                 error "#2 migrate dir fails"
15139         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15140         [ $mdt_index == 1 ] || error "a is not on MDT1"
15141
15142         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
15143                 error "#3 migrate dir fails"
15144
15145         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
15146         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
15147         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
15148
15149         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
15150         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
15151
15152         # a should be migrated to MDT0, since no other links on MDT1
15153         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
15154                 error "#4 migrate dir fails"
15155         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
15156         [ $mdt_index == 0 ] || error "a is not on MDT0"
15157
15158         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15159 }
15160 run_test 230f "migrate mulitple remote link files"
15161
15162 test_230g() {
15163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15164         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15165
15166         mkdir -p $DIR/$tdir/migrate_dir
15167
15168         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
15169                 error "migrating dir to non-exist MDT succeeds"
15170         true
15171 }
15172 run_test 230g "migrate dir to non-exist MDT"
15173
15174 test_230h() {
15175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15176         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15177         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
15178                 skip "Need MDS version at least 2.7.64"
15179
15180         local mdt_index
15181
15182         mkdir -p $DIR/$tdir/migrate_dir
15183
15184         $LFS migrate -m1 $DIR &&
15185                 error "migrating mountpoint1 should fail"
15186
15187         $LFS migrate -m1 $DIR/$tdir/.. &&
15188                 error "migrating mountpoint2 should fail"
15189
15190         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
15191                 error "migrating $tdir fail"
15192
15193         mdt_index=$($LFS getstripe -m $DIR/$tdir)
15194         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
15195
15196         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
15197         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
15198
15199 }
15200 run_test 230h "migrate .. and root"
15201
15202 test_230i() {
15203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15204         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15205
15206         mkdir -p $DIR/$tdir/migrate_dir
15207
15208         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
15209                 error "migration fails with a tailing slash"
15210
15211         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
15212                 error "migration fails with two tailing slashes"
15213 }
15214 run_test 230i "lfs migrate -m tolerates trailing slashes"
15215
15216 test_231a()
15217 {
15218         # For simplicity this test assumes that max_pages_per_rpc
15219         # is the same across all OSCs
15220         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
15221         local bulk_size=$((max_pages * 4096))
15222         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
15223                                        head -n 1)
15224
15225         mkdir -p $DIR/$tdir
15226         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
15227                 error "failed to set stripe with -S ${brw_size}M option"
15228
15229         # clear the OSC stats
15230         $LCTL set_param osc.*.stats=0 &>/dev/null
15231         stop_writeback
15232
15233         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
15234         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
15235                 oflag=direct &>/dev/null || error "dd failed"
15236
15237         sync; sleep 1; sync # just to be safe
15238         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
15239         if [ x$nrpcs != "x1" ]; then
15240                 $LCTL get_param osc.*.stats
15241                 error "found $nrpcs ost_write RPCs, not 1 as expected"
15242         fi
15243
15244         start_writeback
15245         # Drop the OSC cache, otherwise we will read from it
15246         cancel_lru_locks osc
15247
15248         # clear the OSC stats
15249         $LCTL set_param osc.*.stats=0 &>/dev/null
15250
15251         # Client reads $bulk_size.
15252         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
15253                 iflag=direct &>/dev/null || error "dd failed"
15254
15255         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
15256         if [ x$nrpcs != "x1" ]; then
15257                 $LCTL get_param osc.*.stats
15258                 error "found $nrpcs ost_read RPCs, not 1 as expected"
15259         fi
15260 }
15261 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
15262
15263 test_231b() {
15264         mkdir -p $DIR/$tdir
15265         local i
15266         for i in {0..1023}; do
15267                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
15268                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
15269                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
15270         done
15271         sync
15272 }
15273 run_test 231b "must not assert on fully utilized OST request buffer"
15274
15275 test_232a() {
15276         mkdir -p $DIR/$tdir
15277         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
15278
15279         #define OBD_FAIL_LDLM_OST_LVB            0x31c
15280         do_facet ost1 $LCTL set_param fail_loc=0x31c
15281
15282         # ignore dd failure
15283         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
15284
15285         do_facet ost1 $LCTL set_param fail_loc=0
15286         umount_client $MOUNT || error "umount failed"
15287         mount_client $MOUNT || error "mount failed"
15288         stop ost1 || error "cannot stop ost1"
15289         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
15290 }
15291 run_test 232a "failed lock should not block umount"
15292
15293 test_232b() {
15294         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.58) ] ||
15295                 skip "Need MDS version at least 2.10.58"
15296
15297         mkdir -p $DIR/$tdir
15298         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
15299         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
15300         sync
15301         cancel_lru_locks osc
15302
15303         #define OBD_FAIL_LDLM_OST_LVB            0x31c
15304         do_facet ost1 $LCTL set_param fail_loc=0x31c
15305
15306         # ignore failure
15307         $LFS data_version $DIR/$tdir/$tfile || true
15308
15309         do_facet ost1 $LCTL set_param fail_loc=0
15310         umount_client $MOUNT || error "umount failed"
15311         mount_client $MOUNT || error "mount failed"
15312         stop ost1 || error "cannot stop ost1"
15313         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
15314 }
15315 run_test 232b "failed data version lock should not block umount"
15316
15317 test_233a() {
15318         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
15319                 skip "Need MDS version at least 2.3.64"
15320         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
15321
15322         local fid=$($LFS path2fid $MOUNT)
15323
15324         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15325                 error "cannot access $MOUNT using its FID '$fid'"
15326 }
15327 run_test 233a "checking that OBF of the FS root succeeds"
15328
15329 test_233b() {
15330         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
15331                 skip "Need MDS version at least 2.5.90"
15332         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
15333
15334         local fid=$($LFS path2fid $MOUNT/.lustre)
15335
15336         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15337                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
15338
15339         fid=$($LFS path2fid $MOUNT/.lustre/fid)
15340         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
15341                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
15342 }
15343 run_test 233b "checking that OBF of the FS .lustre succeeds"
15344
15345 test_234() {
15346         local p="$TMP/sanityN-$TESTNAME.parameters"
15347         save_lustre_params client "llite.*.xattr_cache" > $p
15348         lctl set_param llite.*.xattr_cache 1 ||
15349                 skip_env "xattr cache is not supported"
15350
15351         mkdir -p $DIR/$tdir || error "mkdir failed"
15352         touch $DIR/$tdir/$tfile || error "touch failed"
15353         # OBD_FAIL_LLITE_XATTR_ENOMEM
15354         $LCTL set_param fail_loc=0x1405
15355         getfattr -n user.attr $DIR/$tdir/$tfile &&
15356                 error "getfattr should have failed with ENOMEM"
15357         $LCTL set_param fail_loc=0x0
15358         rm -rf $DIR/$tdir
15359
15360         restore_lustre_params < $p
15361         rm -f $p
15362 }
15363 run_test 234 "xattr cache should not crash on ENOMEM"
15364
15365 test_235() {
15366         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
15367                 skip "Need MDS version at least 2.4.52"
15368
15369         flock_deadlock $DIR/$tfile
15370         local RC=$?
15371         case $RC in
15372                 0)
15373                 ;;
15374                 124) error "process hangs on a deadlock"
15375                 ;;
15376                 *) error "error executing flock_deadlock $DIR/$tfile"
15377                 ;;
15378         esac
15379 }
15380 run_test 235 "LU-1715: flock deadlock detection does not work properly"
15381
15382 #LU-2935
15383 test_236() {
15384         check_swap_layouts_support
15385
15386         local ref1=/etc/passwd
15387         local ref2=/etc/group
15388         local file1=$DIR/$tdir/f1
15389         local file2=$DIR/$tdir/f2
15390
15391         test_mkdir -c1 $DIR/$tdir
15392         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
15393         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
15394         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
15395         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
15396         local fd=$(free_fd)
15397         local cmd="exec $fd<>$file2"
15398         eval $cmd
15399         rm $file2
15400         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
15401                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
15402         cmd="exec $fd>&-"
15403         eval $cmd
15404         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15405
15406         #cleanup
15407         rm -rf $DIR/$tdir
15408 }
15409 run_test 236 "Layout swap on open unlinked file"
15410
15411 # test to verify file handle related system calls
15412 # (name_to_handle_at/open_by_handle_at)
15413 # The new system calls are supported in glibc >= 2.14.
15414
15415 test_237() {
15416         echo "Test file_handle syscalls" > $DIR/$tfile ||
15417                 error "write failed"
15418         check_fhandle_syscalls $DIR/$tfile ||
15419                 error "check_fhandle_syscalls failed"
15420 }
15421 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
15422
15423 # LU-4659 linkea consistency
15424 test_238() {
15425         local server_version=$(lustre_version_code $SINGLEMDS)
15426
15427         [[ $server_version -gt $(version_code 2.5.57) ]] ||
15428                 [[ $server_version -gt $(version_code 2.5.1) &&
15429                    $server_version -lt $(version_code 2.5.50) ]] ||
15430                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
15431
15432         touch $DIR/$tfile
15433         ln $DIR/$tfile $DIR/$tfile.lnk
15434         touch $DIR/$tfile.new
15435         mv $DIR/$tfile.new $DIR/$tfile
15436         local fid1=$($LFS path2fid $DIR/$tfile)
15437         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
15438         local path1=$($LFS fid2path $FSNAME "$fid1")
15439         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
15440         local path2=$($LFS fid2path $FSNAME "$fid2")
15441         [ $tfile.lnk == $path2 ] ||
15442                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
15443         rm -f $DIR/$tfile*
15444 }
15445 run_test 238 "Verify linkea consistency"
15446
15447 test_239A() { # was test_239
15448         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
15449                 skip "Need MDS version at least 2.5.60"
15450
15451         local list=$(comma_list $(mdts_nodes))
15452
15453         mkdir -p $DIR/$tdir
15454         createmany -o $DIR/$tdir/f- 5000
15455         unlinkmany $DIR/$tdir/f- 5000
15456         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.53) ] &&
15457                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
15458         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
15459                         osp.*MDT*.sync_in_flight" | calc_sum)
15460         [ "$changes" -eq 0 ] || error "$changes not synced"
15461 }
15462 run_test 239A "osp_sync test"
15463
15464 test_239a() { #LU-5297
15465         remote_mds_nodsh && skip "remote MDS with nodsh"
15466
15467         touch $DIR/$tfile
15468         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
15469         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
15470         chgrp $RUNAS_GID $DIR/$tfile
15471         wait_delete_completed
15472 }
15473 run_test 239a "process invalid osp sync record correctly"
15474
15475 test_239b() { #LU-5297
15476         remote_mds_nodsh && skip "remote MDS with nodsh"
15477
15478         touch $DIR/$tfile1
15479         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
15480         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
15481         chgrp $RUNAS_GID $DIR/$tfile1
15482         wait_delete_completed
15483         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15484         touch $DIR/$tfile2
15485         chgrp $RUNAS_GID $DIR/$tfile2
15486         wait_delete_completed
15487 }
15488 run_test 239b "process osp sync record with ENOMEM error correctly"
15489
15490 test_240() {
15491         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15492         remote_mds_nodsh && skip "remote MDS with nodsh"
15493
15494         mkdir -p $DIR/$tdir
15495
15496         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
15497                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
15498         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
15499                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
15500
15501         umount_client $MOUNT || error "umount failed"
15502         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
15503         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
15504         mount_client $MOUNT || error "failed to mount client"
15505
15506         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
15507         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
15508 }
15509 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
15510
15511 test_241_bio() {
15512         for LOOP in $(seq $1); do
15513                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
15514                 cancel_lru_locks $OSC || true
15515         done
15516 }
15517
15518 test_241_dio() {
15519         for LOOP in $(seq $1); do
15520                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
15521                                                 iflag=direct 2>/dev/null
15522         done
15523 }
15524
15525 test_241a() { # was test_241
15526         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
15527         ls -la $DIR/$tfile
15528         cancel_lru_locks $OSC
15529         test_241_bio 1000 &
15530         PID=$!
15531         test_241_dio 1000
15532         wait $PID
15533 }
15534 run_test 241a "bio vs dio"
15535
15536 test_241b() {
15537         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
15538         ls -la $DIR/$tfile
15539         test_241_dio 1000 &
15540         PID=$!
15541         test_241_dio 1000
15542         wait $PID
15543 }
15544 run_test 241b "dio vs dio"
15545
15546 test_242() {
15547         remote_mds_nodsh && skip "remote MDS with nodsh"
15548
15549         mkdir -p $DIR/$tdir
15550         touch $DIR/$tdir/$tfile
15551
15552         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
15553         do_facet mds1 lctl set_param fail_loc=0x105
15554         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
15555
15556         do_facet mds1 lctl set_param fail_loc=0
15557         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
15558 }
15559 run_test 242 "mdt_readpage failure should not cause directory unreadable"
15560
15561 test_243()
15562 {
15563         test_mkdir $DIR/$tdir
15564         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
15565 }
15566 run_test 243 "various group lock tests"
15567
15568 test_244()
15569 {
15570         test_mkdir $DIR/$tdir
15571         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
15572         sendfile_grouplock $DIR/$tdir/$tfile || \
15573                 error "sendfile+grouplock failed"
15574         rm -rf $DIR/$tdir
15575 }
15576 run_test 244 "sendfile with group lock tests"
15577
15578 test_245() {
15579         local flagname="multi_mod_rpcs"
15580         local connect_data_name="max_mod_rpcs"
15581         local out
15582
15583         # check if multiple modify RPCs flag is set
15584         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
15585                 grep "connect_flags:")
15586         echo "$out"
15587
15588         echo "$out" | grep -qw $flagname
15589         if [ $? -ne 0 ]; then
15590                 echo "connect flag $flagname is not set"
15591                 return
15592         fi
15593
15594         # check if multiple modify RPCs data is set
15595         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
15596         echo "$out"
15597
15598         echo "$out" | grep -qw $connect_data_name ||
15599                 error "import should have connect data $connect_data_name"
15600 }
15601 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
15602
15603 test_246() { # LU-7371
15604         remote_ost_nodsh && skip "remote OST with nodsh"
15605         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
15606                 skip "Need OST version >= 2.7.62"
15607
15608         do_facet ost1 $LCTL set_param fail_val=4095
15609 #define OBD_FAIL_OST_READ_SIZE          0x234
15610         do_facet ost1 $LCTL set_param fail_loc=0x234
15611         $LFS setstripe $DIR/$tfile -i 0 -c 1
15612         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
15613         cancel_lru_locks $FSNAME-OST0000
15614         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
15615 }
15616 run_test 246 "Read file of size 4095 should return right length"
15617
15618 cleanup_247() {
15619         local submount=$1
15620
15621         trap 0
15622         umount_client $submount
15623         rmdir $submount
15624 }
15625
15626 test_247a() {
15627         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
15628                 grep -q subtree ||
15629                 skip_env "Fileset feature is not supported"
15630
15631         local submount=${MOUNT}_$tdir
15632
15633         mkdir $MOUNT/$tdir
15634         mkdir -p $submount || error "mkdir $submount failed"
15635         FILESET="$FILESET/$tdir" mount_client $submount ||
15636                 error "mount $submount failed"
15637         trap "cleanup_247 $submount" EXIT
15638         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
15639         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
15640                 error "read $MOUNT/$tdir/$tfile failed"
15641         cleanup_247 $submount
15642 }
15643 run_test 247a "mount subdir as fileset"
15644
15645 test_247b() {
15646         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
15647                 skip_env "Fileset feature is not supported"
15648
15649         local submount=${MOUNT}_$tdir
15650
15651         rm -rf $MOUNT/$tdir
15652         mkdir -p $submount || error "mkdir $submount failed"
15653         SKIP_FILESET=1
15654         FILESET="$FILESET/$tdir" mount_client $submount &&
15655                 error "mount $submount should fail"
15656         rmdir $submount
15657 }
15658 run_test 247b "mount subdir that dose not exist"
15659
15660 test_247c() {
15661         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
15662                 skip_env "Fileset feature is not supported"
15663
15664         local submount=${MOUNT}_$tdir
15665
15666         mkdir -p $MOUNT/$tdir/dir1
15667         mkdir -p $submount || error "mkdir $submount failed"
15668         trap "cleanup_247 $submount" EXIT
15669         FILESET="$FILESET/$tdir" mount_client $submount ||
15670                 error "mount $submount failed"
15671         local fid=$($LFS path2fid $MOUNT/)
15672         $LFS fid2path $submount $fid && error "fid2path should fail"
15673         cleanup_247 $submount
15674 }
15675 run_test 247c "running fid2path outside root"
15676
15677 test_247d() {
15678         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
15679                 skip "Fileset feature is not supported"
15680
15681         local submount=${MOUNT}_$tdir
15682
15683         mkdir -p $MOUNT/$tdir/dir1
15684         mkdir -p $submount || error "mkdir $submount failed"
15685         FILESET="$FILESET/$tdir" mount_client $submount ||
15686                 error "mount $submount failed"
15687         trap "cleanup_247 $submount" EXIT
15688         local fid=$($LFS path2fid $submount/dir1)
15689         $LFS fid2path $submount $fid || error "fid2path should succeed"
15690         cleanup_247 $submount
15691 }
15692 run_test 247d "running fid2path inside root"
15693
15694 # LU-8037
15695 test_247e() {
15696         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
15697                 grep -q subtree ||
15698                 skip "Fileset feature is not supported"
15699
15700         local submount=${MOUNT}_$tdir
15701
15702         mkdir $MOUNT/$tdir
15703         mkdir -p $submount || error "mkdir $submount failed"
15704         FILESET="$FILESET/.." mount_client $submount &&
15705                 error "mount $submount should fail"
15706         rmdir $submount
15707 }
15708 run_test 247e "mount .. as fileset"
15709
15710 test_248() {
15711         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
15712         [ -z "$fast_read_sav" ] && skip "no fast read support"
15713
15714         # create a large file for fast read verification
15715         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
15716
15717         # make sure the file is created correctly
15718         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
15719                 { rm -f $DIR/$tfile; skip "file creation error"; }
15720
15721         echo "Test 1: verify that fast read is 4 times faster on cache read"
15722
15723         # small read with fast read enabled
15724         $LCTL set_param -n llite.*.fast_read=1
15725         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
15726                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15727                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15728         # small read with fast read disabled
15729         $LCTL set_param -n llite.*.fast_read=0
15730         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
15731                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15732                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15733
15734         # verify that fast read is 4 times faster for cache read
15735         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
15736                 error_not_in_vm "fast read was not 4 times faster: " \
15737                            "$t_fast vs $t_slow"
15738
15739         echo "Test 2: verify the performance between big and small read"
15740         $LCTL set_param -n llite.*.fast_read=1
15741
15742         # 1k non-cache read
15743         cancel_lru_locks osc
15744         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15745                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15746                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15747
15748         # 1M non-cache read
15749         cancel_lru_locks osc
15750         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15751                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15752                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15753
15754         # verify that big IO is not 4 times faster than small IO
15755         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
15756                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
15757
15758         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
15759         rm -f $DIR/$tfile
15760 }
15761 run_test 248 "fast read verification"
15762
15763 test_249() { # LU-7890
15764         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
15765                 skip "Need at least version 2.8.54"
15766
15767         rm -f $DIR/$tfile
15768         $SETSTRIPE -c 1 $DIR/$tfile
15769         # Offset 2T == 4k * 512M
15770         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
15771                 error "dd to 2T offset failed"
15772 }
15773 run_test 249 "Write above 2T file size"
15774
15775 test_250() {
15776         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
15777          && skip "no 16TB file size limit on ZFS"
15778
15779         $SETSTRIPE -c 1 $DIR/$tfile
15780         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
15781         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
15782         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
15783         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
15784                 conv=notrunc,fsync && error "append succeeded"
15785         return 0
15786 }
15787 run_test 250 "Write above 16T limit"
15788
15789 test_251() {
15790         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
15791
15792         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
15793         #Skip once - writing the first stripe will succeed
15794         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15795         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
15796                 error "short write happened"
15797
15798         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15799         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
15800                 error "short read happened"
15801
15802         rm -f $DIR/$tfile
15803 }
15804 run_test 251 "Handling short read and write correctly"
15805
15806 test_252() {
15807         remote_mds_nodsh && skip "remote MDS with nodsh"
15808         remote_ost_nodsh && skip "remote OST with nodsh"
15809         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
15810              "$(facet_fstype mds1)" != "ldiskfs" ]; then
15811                 skip_env "ldiskfs only test"
15812         fi
15813
15814         local tgt
15815         local dev
15816         local out
15817         local uuid
15818         local num
15819         local gen
15820
15821         # check lr_reader on OST0000
15822         tgt=ost1
15823         dev=$(facet_device $tgt)
15824         out=$(do_facet $tgt $LR_READER $dev)
15825         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15826         echo "$out"
15827         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
15828         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
15829                 error "Invalid uuid returned by $LR_READER on target $tgt"
15830         echo -e "uuid returned by $LR_READER is '$uuid'\n"
15831
15832         # check lr_reader -c on MDT0000
15833         tgt=mds1
15834         dev=$(facet_device $tgt)
15835         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
15836                 skip "$LR_READER does not support additional options"
15837         fi
15838         out=$(do_facet $tgt $LR_READER -c $dev)
15839         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15840         echo "$out"
15841         num=$(echo "$out" | grep -c "mdtlov")
15842         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
15843                 error "Invalid number of mdtlov clients returned by $LR_READER"
15844         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
15845
15846         # check lr_reader -cr on MDT0000
15847         out=$(do_facet $tgt $LR_READER -cr $dev)
15848         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15849         echo "$out"
15850         echo "$out" | grep -q "^reply_data:$" ||
15851                 error "$LR_READER should have returned 'reply_data' section"
15852         num=$(echo "$out" | grep -c "client_generation")
15853         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
15854 }
15855 run_test 252 "check lr_reader tool"
15856
15857 test_253_fill_ost() {
15858         local size_mb #how many MB should we write to pass watermark
15859         local lwm=$3  #low watermark
15860         local free_10mb #10% of free space
15861
15862         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15863         size_mb=$((free_kb / 1024 - lwm))
15864         free_10mb=$((free_kb / 10240))
15865         #If 10% of free space cross low watermark use it
15866         if (( free_10mb > size_mb )); then
15867                 size_mb=$free_10mb
15868         else
15869                 #At least we need to store 1.1 of difference between
15870                 #free space and low watermark
15871                 size_mb=$((size_mb + size_mb / 10))
15872         fi
15873         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
15874                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
15875                          oflag=append conv=notrunc
15876         fi
15877
15878         sleep_maxage
15879
15880         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15881         echo "OST still has $((free_kb / 1024)) mbytes free"
15882 }
15883
15884 test_253() {
15885         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15886         remote_mds_nodsh && skip "remote MDS with nodsh"
15887         remote_mgs_nodsh && skip "remote MGS with nodsh"
15888
15889         local ostidx=0
15890         local rc=0
15891
15892         local ost_name=$($LFS osts |
15893                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
15894         # on the mdt's osc
15895         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
15896         do_facet $SINGLEMDS $LCTL get_param -n \
15897                 osp.$mdtosc_proc1.reserved_mb_high ||
15898                 skip  "remote MDS does not support reserved_mb_high"
15899
15900         rm -rf $DIR/$tdir
15901         wait_mds_ost_sync
15902         wait_delete_completed
15903         mkdir $DIR/$tdir
15904
15905         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
15906                         osp.$mdtosc_proc1.reserved_mb_high)
15907         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
15908                         osp.$mdtosc_proc1.reserved_mb_low)
15909         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
15910
15911         if ! combined_mgs_mds ; then
15912                 mount_mgs_client
15913         fi
15914         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
15915         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
15916                 error "Adding $ost_name to pool failed"
15917
15918         # Wait for client to see a OST at pool
15919         wait_update $HOSTNAME "$LCTL get_param -n
15920                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
15921                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
15922                 error "Client can not see the pool"
15923         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
15924                 error "Setstripe failed"
15925
15926         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
15927         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
15928         echo "OST still has $((blocks/1024)) mbytes free"
15929
15930         local new_lwm=$((blocks/1024-10))
15931         do_facet $SINGLEMDS $LCTL set_param \
15932                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
15933         do_facet $SINGLEMDS $LCTL set_param \
15934                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
15935
15936         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15937
15938         #First enospc could execute orphan deletion so repeat.
15939         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15940
15941         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15942                         osp.$mdtosc_proc1.prealloc_status)
15943         echo "prealloc_status $oa_status"
15944
15945         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
15946                 error "File creation should fail"
15947         #object allocation was stopped, but we still able to append files
15948         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
15949                 error "Append failed"
15950         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
15951
15952         wait_delete_completed
15953
15954         sleep_maxage
15955
15956         for i in $(seq 10 12); do
15957                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
15958                         error "File creation failed after rm";
15959         done
15960
15961         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15962                         osp.$mdtosc_proc1.prealloc_status)
15963         echo "prealloc_status $oa_status"
15964
15965         if (( oa_status != 0 )); then
15966                 error "Object allocation still disable after rm"
15967         fi
15968         do_facet $SINGLEMDS $LCTL set_param \
15969                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
15970         do_facet $SINGLEMDS $LCTL set_param \
15971                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
15972
15973
15974         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
15975                 error "Remove $ost_name from pool failed"
15976         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15977                 error "Pool destroy fialed"
15978
15979         if ! combined_mgs_mds ; then
15980                 umount_mgs_client
15981         fi
15982 }
15983 run_test 253 "Check object allocation limit"
15984
15985 test_254() {
15986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15987         remote_mds_nodsh && skip "remote MDS with nodsh"
15988         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
15989                 skip "MDS does not support changelog_size"
15990
15991         local cl_user
15992         local MDT0=$(facet_svc $SINGLEMDS)
15993
15994         changelog_register || error "changelog_register failed"
15995
15996         changelog_clear 0 || error "changelog_clear failed"
15997
15998         local size1=$(do_facet $SINGLEMDS \
15999                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16000         echo "Changelog size $size1"
16001
16002         rm -rf $DIR/$tdir
16003         $LFS mkdir -i 0 $DIR/$tdir
16004         # change something
16005         mkdir -p $DIR/$tdir/pics/2008/zachy
16006         touch $DIR/$tdir/pics/2008/zachy/timestamp
16007         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
16008         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
16009         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
16010         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
16011         rm $DIR/$tdir/pics/desktop.jpg
16012
16013         local size2=$(do_facet $SINGLEMDS \
16014                       $LCTL get_param -n mdd.$MDT0.changelog_size)
16015         echo "Changelog size after work $size2"
16016
16017         (( $size2 > $size1 )) ||
16018                 error "new Changelog size=$size2 less than old size=$size1"
16019 }
16020 run_test 254 "Check changelog size"
16021
16022 ladvise_no_type()
16023 {
16024         local type=$1
16025         local file=$2
16026
16027         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
16028                 awk -F: '{print $2}' | grep $type > /dev/null
16029         if [ $? -ne 0 ]; then
16030                 return 0
16031         fi
16032         return 1
16033 }
16034
16035 ladvise_no_ioctl()
16036 {
16037         local file=$1
16038
16039         lfs ladvise -a willread $file > /dev/null 2>&1
16040         if [ $? -eq 0 ]; then
16041                 return 1
16042         fi
16043
16044         lfs ladvise -a willread $file 2>&1 |
16045                 grep "Inappropriate ioctl for device" > /dev/null
16046         if [ $? -eq 0 ]; then
16047                 return 0
16048         fi
16049         return 1
16050 }
16051
16052 percent() {
16053         bc <<<"scale=2; ($1 - $2) * 100 / $2"
16054 }
16055
16056 # run a random read IO workload
16057 # usage: random_read_iops <filename> <filesize> <iosize>
16058 random_read_iops() {
16059         local file=$1
16060         local fsize=$2
16061         local iosize=${3:-4096}
16062
16063         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
16064                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
16065 }
16066
16067 drop_file_oss_cache() {
16068         local file="$1"
16069         local nodes="$2"
16070
16071         $LFS ladvise -a dontneed $file 2>/dev/null ||
16072                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
16073 }
16074
16075 ladvise_willread_performance()
16076 {
16077         local repeat=10
16078         local average_origin=0
16079         local average_cache=0
16080         local average_ladvise=0
16081
16082         for ((i = 1; i <= $repeat; i++)); do
16083                 echo "Iter $i/$repeat: reading without willread hint"
16084                 cancel_lru_locks osc
16085                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16086                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
16087                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
16088                 average_origin=$(bc <<<"$average_origin + $speed_origin")
16089
16090                 cancel_lru_locks osc
16091                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
16092                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
16093                 average_cache=$(bc <<<"$average_cache + $speed_cache")
16094
16095                 cancel_lru_locks osc
16096                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
16097                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
16098                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
16099                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
16100                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
16101         done
16102         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
16103         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
16104         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
16105
16106         speedup_cache=$(percent $average_cache $average_origin)
16107         speedup_ladvise=$(percent $average_ladvise $average_origin)
16108
16109         echo "Average uncached read: $average_origin"
16110         echo "Average speedup with OSS cached read: " \
16111                 "$average_cache = +$speedup_cache%"
16112         echo "Average speedup with ladvise willread: " \
16113                 "$average_ladvise = +$speedup_ladvise%"
16114
16115         local lowest_speedup=20
16116         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
16117                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
16118                         "got $average_cache%. Skipping ladvise willread check."
16119                 return 0
16120         fi
16121
16122         # the test won't work on ZFS until it supports 'ladvise dontneed', but
16123         # it is still good to run until then to exercise 'ladvise willread'
16124         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16125                 [ "$(facet_fstype ost1)" = "zfs" ] &&
16126                 echo "osd-zfs does not support dontneed or drop_caches" &&
16127                 return 0
16128
16129         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
16130         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
16131                 error_not_in_vm "Speedup with willread is less than " \
16132                         "$lowest_speedup%, got $average_ladvise%"
16133 }
16134
16135 test_255a() {
16136         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
16137                 skip "lustre < 2.8.54 does not support ladvise "
16138         remote_ost_nodsh && skip "remote OST with nodsh"
16139
16140         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
16141
16142         ladvise_no_type willread $DIR/$tfile &&
16143                 skip "willread ladvise is not supported"
16144
16145         ladvise_no_ioctl $DIR/$tfile &&
16146                 skip "ladvise ioctl is not supported"
16147
16148         local size_mb=100
16149         local size=$((size_mb * 1048576))
16150         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16151                 error "dd to $DIR/$tfile failed"
16152
16153         lfs ladvise -a willread $DIR/$tfile ||
16154                 error "Ladvise failed with no range argument"
16155
16156         lfs ladvise -a willread -s 0 $DIR/$tfile ||
16157                 error "Ladvise failed with no -l or -e argument"
16158
16159         lfs ladvise -a willread -e 1 $DIR/$tfile ||
16160                 error "Ladvise failed with only -e argument"
16161
16162         lfs ladvise -a willread -l 1 $DIR/$tfile ||
16163                 error "Ladvise failed with only -l argument"
16164
16165         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
16166                 error "End offset should not be smaller than start offset"
16167
16168         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
16169                 error "End offset should not be equal to start offset"
16170
16171         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
16172                 error "Ladvise failed with overflowing -s argument"
16173
16174         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
16175                 error "Ladvise failed with overflowing -e argument"
16176
16177         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
16178                 error "Ladvise failed with overflowing -l argument"
16179
16180         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
16181                 error "Ladvise succeeded with conflicting -l and -e arguments"
16182
16183         echo "Synchronous ladvise should wait"
16184         local delay=4
16185 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
16186         do_nodes $(comma_list $(osts_nodes)) \
16187                 $LCTL set_param fail_val=$delay fail_loc=0x237
16188
16189         local start_ts=$SECONDS
16190         lfs ladvise -a willread $DIR/$tfile ||
16191                 error "Ladvise failed with no range argument"
16192         local end_ts=$SECONDS
16193         local inteval_ts=$((end_ts - start_ts))
16194
16195         if [ $inteval_ts -lt $(($delay - 1)) ]; then
16196                 error "Synchronous advice didn't wait reply"
16197         fi
16198
16199         echo "Asynchronous ladvise shouldn't wait"
16200         local start_ts=$SECONDS
16201         lfs ladvise -a willread -b $DIR/$tfile ||
16202                 error "Ladvise failed with no range argument"
16203         local end_ts=$SECONDS
16204         local inteval_ts=$((end_ts - start_ts))
16205
16206         if [ $inteval_ts -gt $(($delay / 2)) ]; then
16207                 error "Asynchronous advice blocked"
16208         fi
16209
16210         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
16211         ladvise_willread_performance
16212 }
16213 run_test 255a "check 'lfs ladvise -a willread'"
16214
16215 facet_meminfo() {
16216         local facet=$1
16217         local info=$2
16218
16219         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
16220 }
16221
16222 test_255b() {
16223         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
16224                 skip "lustre < 2.8.54 does not support ladvise "
16225         remote_ost_nodsh && skip "remote OST with nodsh"
16226
16227         lfs setstripe -c 1 -i 0 $DIR/$tfile
16228
16229         ladvise_no_type dontneed $DIR/$tfile &&
16230                 skip "dontneed ladvise is not supported"
16231
16232         ladvise_no_ioctl $DIR/$tfile &&
16233                 skip "ladvise ioctl is not supported"
16234
16235         ! $LFS ladvise -a dontneed $DIR/$tfile &&
16236                 [ "$(facet_fstype ost1)" = "zfs" ] &&
16237                 skip "zfs-osd does not support 'ladvise dontneed'"
16238
16239         local size_mb=100
16240         local size=$((size_mb * 1048576))
16241         # In order to prevent disturbance of other processes, only check 3/4
16242         # of the memory usage
16243         local kibibytes=$((size_mb * 1024 * 3 / 4))
16244
16245         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
16246                 error "dd to $DIR/$tfile failed"
16247
16248         local total=$(facet_meminfo ost1 MemTotal)
16249         echo "Total memory: $total KiB"
16250
16251         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
16252         local before_read=$(facet_meminfo ost1 Cached)
16253         echo "Cache used before read: $before_read KiB"
16254
16255         lfs ladvise -a willread $DIR/$tfile ||
16256                 error "Ladvise willread failed"
16257         local after_read=$(facet_meminfo ost1 Cached)
16258         echo "Cache used after read: $after_read KiB"
16259
16260         lfs ladvise -a dontneed $DIR/$tfile ||
16261                 error "Ladvise dontneed again failed"
16262         local no_read=$(facet_meminfo ost1 Cached)
16263         echo "Cache used after dontneed ladvise: $no_read KiB"
16264
16265         if [ $total -lt $((before_read + kibibytes)) ]; then
16266                 echo "Memory is too small, abort checking"
16267                 return 0
16268         fi
16269
16270         if [ $((before_read + kibibytes)) -gt $after_read ]; then
16271                 error "Ladvise willread should use more memory" \
16272                         "than $kibibytes KiB"
16273         fi
16274
16275         if [ $((no_read + kibibytes)) -gt $after_read ]; then
16276                 error "Ladvise dontneed should release more memory" \
16277                         "than $kibibytes KiB"
16278         fi
16279 }
16280 run_test 255b "check 'lfs ladvise -a dontneed'"
16281
16282 test_255c() {
16283         [ $(lustre_version_code ost1) -lt $(version_code 2.10.50) ] &&
16284                 skip "lustre < 2.10.53 does not support lockahead"
16285
16286         local count
16287         local new_count
16288         local difference
16289         local i
16290         local rc
16291
16292         test_mkdir -p $DIR/$tdir
16293         $SETSTRIPE -i 0 $DIR/$tdir
16294
16295         #test 10 returns only success/failure
16296         i=10
16297         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16298         rc=$?
16299         if [ $rc -eq 255 ]; then
16300                 error "Ladvise test${i} failed, ${rc}"
16301         fi
16302
16303         #test 11 counts lock enqueue requests, all others count new locks
16304         i=11
16305         count=$(do_facet ost1 \
16306                 $LCTL get_param -n ost.OSS.ost.stats)
16307         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
16308
16309         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16310         rc=$?
16311         if [ $rc -eq 255 ]; then
16312                 error "Ladvise test${i} failed, ${rc}"
16313         fi
16314
16315         new_count=$(do_facet ost1 \
16316                 $LCTL get_param -n ost.OSS.ost.stats)
16317         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
16318                    awk '{ print $2 }')
16319
16320         difference="$((new_count - count))"
16321         if [ $difference -ne $rc ]; then
16322                 error "Ladvise test${i}, bad enqueue count, returned " \
16323                       "${rc}, actual ${difference}"
16324         fi
16325
16326         for i in $(seq 12 21); do
16327                 # If we do not do this, we run the risk of having too many
16328                 # locks and starting lock cancellation while we are checking
16329                 # lock counts.
16330                 cancel_lru_locks osc
16331
16332                 count=$($LCTL get_param -n \
16333                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
16334
16335                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
16336                 rc=$?
16337                 if [ $rc -eq 255 ]; then
16338                         error "Ladvise test ${i} failed, ${rc}"
16339                 fi
16340
16341                 new_count=$($LCTL get_param -n \
16342                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
16343                 difference="$((new_count - count))"
16344
16345                 # Test 15 output is divided by 100 to map down to valid return
16346                 if [ $i -eq 15 ]; then
16347                         rc="$((rc * 100))"
16348                 fi
16349
16350                 if [ $difference -ne $rc ]; then
16351                         error "Ladvise test ${i}, bad lock count, returned " \
16352                               "${rc}, actual ${difference}"
16353                 fi
16354         done
16355
16356         #test 22 returns only success/failure
16357         i=22
16358         lockahead_test -d $DIR/$tdir -t $i -f $tfile
16359         rc=$?
16360         if [ $rc -eq 255 ]; then
16361                 error "Ladvise test${i} failed, ${rc}"
16362         fi
16363 }
16364 run_test 255c "suite of ladvise lockahead tests"
16365
16366 test_256() {
16367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16368         remote_mds_nodsh && skip "remote MDS with nodsh"
16369         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
16370                 skip "ldiskfs only test"
16371         changelog_users $SINGLEMDS | grep "^cl" &&
16372                 skip "active changelog user"
16373
16374         local cl_user
16375         local cat_sl
16376         local mdt_dev
16377
16378         mdt_dev=$(mdsdevname 1)
16379         echo $mdt_dev
16380
16381         changelog_register || error "changelog_register failed"
16382
16383         rm -rf $DIR/$tdir
16384         mkdir -p $DIR/$tdir
16385
16386         changelog_clear 0 || error "changelog_clear failed"
16387
16388         # change something
16389         touch $DIR/$tdir/{1..10}
16390
16391         # stop the MDT
16392         stop $SINGLEMDS || error "Fail to stop MDT"
16393
16394         # remount the MDT
16395
16396         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
16397
16398         #after mount new plainllog is used
16399         touch $DIR/$tdir/{11..19}
16400         local tmpfile=$(mktemp -u $tfile.XXXXXX)
16401         cat_sl=$(do_facet $SINGLEMDS "sync; \
16402                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
16403                  llog_reader $tmpfile | grep -c type=1064553b")
16404         do_facet $SINGLEMDS llog_reader $tmpfile
16405
16406         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
16407
16408         changelog_clear 0 || error "changelog_clear failed"
16409
16410         cat_sl=$(do_facet $SINGLEMDS "sync; \
16411                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
16412                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
16413
16414         if (( cat_sl == 2 )); then
16415                 error "Empty plain llog was not deleted from changelog catalog"
16416         elif (( cat_sl != 1 )); then
16417                 error "Active plain llog shouldn't be deleted from catalog"
16418         fi
16419 }
16420 run_test 256 "Check llog delete for empty and not full state"
16421
16422 test_257() {
16423         remote_mds_nodsh && skip "remote MDS with nodsh"
16424         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
16425                 skip "Need MDS version at least 2.8.55"
16426
16427         test_mkdir $DIR/$tdir
16428
16429         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
16430                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
16431         stat $DIR/$tdir
16432
16433 #define OBD_FAIL_MDS_XATTR_REP                  0x161
16434         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
16435         local facet=mds$((mdtidx + 1))
16436         set_nodes_failloc $(facet_active_host $facet) 0x80000161
16437         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
16438
16439         stop $facet || error "stop MDS failed"
16440         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
16441                 error "start MDS fail"
16442 }
16443 run_test 257 "xattr locks are not lost"
16444
16445 # Verify we take the i_mutex when security requires it
16446 test_258a() {
16447 #define OBD_FAIL_IMUTEX_SEC 0x141c
16448         $LCTL set_param fail_loc=0x141c
16449         touch $DIR/$tfile
16450         chmod u+s $DIR/$tfile
16451         chmod a+rwx $DIR/$tfile
16452         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
16453         RC=$?
16454         if [ $RC -ne 0 ]; then
16455                 error "error, failed to take i_mutex, rc=$?"
16456         fi
16457         rm -f $DIR/$tfile
16458 }
16459 run_test 258a
16460
16461 # Verify we do NOT take the i_mutex in the normal case
16462 test_258b() {
16463 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
16464         $LCTL set_param fail_loc=0x141d
16465         touch $DIR/$tfile
16466         chmod a+rwx $DIR
16467         chmod a+rw $DIR/$tfile
16468         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
16469         RC=$?
16470         if [ $RC -ne 0 ]; then
16471                 error "error, took i_mutex unnecessarily, rc=$?"
16472         fi
16473         rm -f $DIR/$tfile
16474
16475 }
16476 run_test 258b "verify i_mutex security behavior"
16477
16478 test_259() {
16479         local file=$DIR/$tfile
16480         local before
16481         local after
16482
16483         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
16484                 skip "ldiskfs only test" && return
16485
16486         stack_trap "rm -f $file" EXIT
16487
16488         wait_delete_completed
16489         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16490         echo "before: $before"
16491
16492         $LFS setstripe -i 0 -c 1 $file
16493         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
16494         sync_all_data
16495         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16496         echo "after write: $after"
16497
16498 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
16499         do_facet ost1 $LCTL set_param fail_loc=0x2301
16500         $TRUNCATE $file 0
16501         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16502         echo "after truncate: $after"
16503
16504         stop ost1
16505         do_facet ost1 $LCTL set_param fail_loc=0
16506         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16507         sleep 2
16508         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
16509         echo "after restart: $after"
16510         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
16511                 error "missing truncate?"
16512
16513         return 0
16514 }
16515 run_test 259 "crash at delayed truncate"
16516
16517 test_260() {
16518 #define OBD_FAIL_MDC_CLOSE               0x806
16519         $LCTL set_param fail_loc=0x80000806
16520         touch $DIR/$tfile
16521
16522 }
16523 run_test 260 "Check mdc_close fail"
16524
16525 ### Data-on-MDT sanity tests ###
16526 test_270a() {
16527         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16528                 skip "Need MDS version at least 2.10.55"
16529
16530         # create DoM file
16531         local dom=$DIR/$tdir/dom_file
16532         local tmp=$DIR/$tdir/tmp_file
16533
16534         mkdir -p $DIR/$tdir
16535
16536         # basic checks for DoM component creation
16537         $LFS setstripe -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
16538                 error "Can set MDT layout to non-first entry"
16539
16540         $LFS setstripe -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
16541                 error "Can define multiple entries as MDT layout"
16542
16543         $LFS setstripe -E 1M -L mdt $dom ||
16544                 error "Can't create DoM layout"
16545
16546         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
16547         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
16548         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
16549
16550         local mdtidx=$($LFS getstripe -m $dom)
16551         local mdtname=MDT$(printf %04x $mdtidx)
16552         local facet=mds$((mdtidx + 1))
16553         local space_check=1
16554
16555         # Skip free space checks with ZFS
16556         if [ "$(facet_fstype $facet)" == "zfs" ]; then
16557                 space_check=0
16558         fi
16559
16560         # write
16561         sync
16562         local mdtfree1=$(do_facet $facet \
16563                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16564         dd if=/dev/urandom of=$tmp bs=1024 count=100
16565         # check also direct IO along write
16566         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
16567         sync
16568         cmp $tmp $dom || error "file data is different"
16569         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
16570         if [ $space_check == 1 ]; then
16571                 local mdtfree2=$(do_facet $facet \
16572                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16573                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
16574                         error "MDT free space is wrong after write"
16575         fi
16576
16577         # truncate
16578         $TRUNCATE $dom 10000
16579         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
16580         if [ $space_check == 1 ]; then
16581                 mdtfree1=$(do_facet $facet \
16582                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16583                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
16584                         error "MDT free space is wrong after truncate"
16585         fi
16586
16587         # append
16588         cat $tmp >> $dom
16589         sync
16590         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
16591         if [ $space_check == 1 ]; then
16592                 mdtfree2=$(do_facet $facet \
16593                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16594                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
16595                         error "MDT free space is wrong after append"
16596         fi
16597
16598         # delete
16599         rm $dom
16600         if [ $space_check == 1 ]; then
16601                 mdtfree1=$(do_facet $facet \
16602                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
16603                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
16604                         error "MDT free space is wrong after removal"
16605         fi
16606
16607         # combined striping
16608         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
16609                 error "Can't create DoM + OST striping"
16610
16611         dd if=/dev/urandom of=$tmp bs=1024 count=2000
16612         # check also direct IO along write
16613         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
16614         sync
16615         cmp $tmp $dom || error "file data is different"
16616         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
16617         rm $dom
16618         rm $tmp
16619
16620         return 0
16621 }
16622 run_test 270a "DoM: basic functionality tests"
16623
16624 test_270b() {
16625         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16626                 skip "Need MDS version at least 2.10.55"
16627
16628         local dom=$DIR/$tdir/dom_file
16629         local max_size=1048576
16630
16631         mkdir -p $DIR/$tdir
16632         $LFS setstripe -E $max_size -L mdt $dom
16633
16634         # truncate over the limit
16635         $TRUNCATE $dom $(($max_size + 1)) &&
16636                 error "successful truncate over the maximum size"
16637         # write over the limit
16638         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
16639                 error "successful write over the maximum size"
16640         # append over the limit
16641         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
16642         echo "12345" >> $dom && error "successful append over the maximum size"
16643         rm $dom
16644
16645         return 0
16646 }
16647 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
16648
16649 test_270c() {
16650         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16651                 skip "Need MDS version at least 2.10.55"
16652
16653         mkdir -p $DIR/$tdir
16654         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16655
16656         # check files inherit DoM EA
16657         touch $DIR/$tdir/first
16658         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
16659                 error "bad pattern"
16660         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
16661                 error "bad stripe count"
16662         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
16663                 error "bad stripe size"
16664
16665         # check directory inherits DoM EA and uses it as default
16666         mkdir $DIR/$tdir/subdir
16667         touch $DIR/$tdir/subdir/second
16668         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
16669                 error "bad pattern in sub-directory"
16670         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
16671                 error "bad stripe count in sub-directory"
16672         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
16673                 error "bad stripe size in sub-directory"
16674         return 0
16675 }
16676 run_test 270c "DoM: DoM EA inheritance tests"
16677
16678 test_270d() {
16679         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16680                 skip "Need MDS version at least 2.10.55"
16681
16682         mkdir -p $DIR/$tdir
16683         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16684
16685         # inherit default DoM striping
16686         mkdir $DIR/$tdir/subdir
16687         touch $DIR/$tdir/subdir/f1
16688
16689         # change default directory striping
16690         $LFS setstripe -c 1 $DIR/$tdir/subdir
16691         touch $DIR/$tdir/subdir/f2
16692         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
16693                 error "wrong default striping in file 2"
16694         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
16695                 error "bad pattern in file 2"
16696         return 0
16697 }
16698 run_test 270d "DoM: change striping from DoM to RAID0"
16699
16700 test_270e() {
16701         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16702                 skip "Need MDS version at least 2.10.55"
16703
16704         mkdir -p $DIR/$tdir/dom
16705         mkdir -p $DIR/$tdir/norm
16706         DOMFILES=20
16707         NORMFILES=10
16708         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
16709         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
16710
16711         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
16712         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
16713
16714         # find DoM files by layout
16715         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
16716         [ $NUM -eq  $DOMFILES ] ||
16717                 error "lfs find -L: found $NUM, expected $DOMFILES"
16718         echo "Test 1: lfs find 20 DOM files by layout: OK"
16719
16720         # there should be 1 dir with default DOM striping
16721         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
16722         [ $NUM -eq  1 ] ||
16723                 error "lfs find -L: found $NUM, expected 1 dir"
16724         echo "Test 2: lfs find 1 DOM dir by layout: OK"
16725
16726         # find DoM files by stripe size
16727         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
16728         [ $NUM -eq  $DOMFILES ] ||
16729                 error "lfs find -S: found $NUM, expected $DOMFILES"
16730         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
16731
16732         # find files by stripe offset except DoM files
16733         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
16734         [ $NUM -eq  $NORMFILES ] ||
16735                 error "lfs find -i: found $NUM, expected $NORMFILES"
16736         echo "Test 5: lfs find no DOM files by stripe index: OK"
16737         return 0
16738 }
16739 run_test 270e "DoM: lfs find with DoM files test"
16740
16741 test_270f() {
16742         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16743                 skip "Need MDS version at least 2.10.55"
16744
16745         local mdtname=${FSNAME}-MDT0000-mdtlov
16746         local dom=$DIR/$tdir/dom_file
16747         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
16748                                                 lod.$mdtname.dom_stripesize)
16749         local dom_limit=131072
16750
16751         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
16752         local dom_current=$(do_facet mds1 $LCTL get_param -n \
16753                                                 lod.$mdtname.dom_stripesize)
16754         [ ${dom_limit} -eq ${dom_current} ] ||
16755                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
16756
16757         $LFS mkdir -i 0 -c 1 $DIR/$tdir
16758         $LFS setstripe -d $DIR/$tdir
16759         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
16760                 error "Can't set directory default striping"
16761
16762         # exceed maximum stripe size
16763         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
16764                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
16765         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
16766                 error "Able to create DoM component size more than LOD limit"
16767
16768         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
16769         dom_current=$(do_facet mds1 $LCTL get_param -n \
16770                                                 lod.$mdtname.dom_stripesize)
16771         [ 0 -eq ${dom_current} ] ||
16772                 error "Can't set zero DoM stripe limit"
16773         rm $dom
16774
16775         # attempt to create DoM file on server with disabled DoM should
16776         # remove DoM entry from layout and be succeed
16777         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
16778                 error "Can't create DoM file (DoM is disabled)"
16779         [ $($LFS getstripe -L $dom) == "mdt" ] &&
16780                 error "File has DoM component while DoM is disabled"
16781         rm $dom
16782
16783         # attempt to create DoM file with only DoM stripe should return error
16784         $LFS setstripe -E $dom_limit -L mdt $dom &&
16785                 error "Able to create DoM-only file while DoM is disabled"
16786
16787         # too low values to be aligned with smallest stripe size 64K
16788         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
16789         dom_current=$(do_facet mds1 $LCTL get_param -n \
16790                                                 lod.$mdtname.dom_stripesize)
16791         [ 30000 -eq ${dom_current} ] &&
16792                 error "Can set too small DoM stripe limit"
16793
16794         # 64K is a minimal stripe size in Lustre, expect limit of that size
16795         [ 65536 -eq ${dom_current} ] ||
16796                 error "Limit is not set to 64K but ${dom_current}"
16797
16798         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
16799         dom_current=$(do_facet mds1 $LCTL get_param -n \
16800                                                 lod.$mdtname.dom_stripesize)
16801         echo $dom_current
16802         [ 2147483648 -eq ${dom_current} ] &&
16803                 error "Can set too large DoM stripe limit"
16804
16805         do_facet mds1 $LCTL set_param -n \
16806                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
16807         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
16808                 error "Can't create DoM component size after limit change"
16809         do_facet mds1 $LCTL set_param -n \
16810                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
16811         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
16812                 error "Can't create DoM file after limit decrease"
16813         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
16814                 error "Can create big DoM component after limit decrease"
16815         touch ${dom}_def ||
16816                 error "Can't create file with old default layout"
16817
16818         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
16819         return 0
16820 }
16821 run_test 270f "DoM: maximum DoM stripe size checks"
16822
16823 test_271a() {
16824         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16825                 skip "Need MDS version at least 2.10.55"
16826
16827         local dom=$DIR/$tdir/dom
16828
16829         mkdir -p $DIR/$tdir
16830
16831         $LFS setstripe -E 1024K -L mdt $dom
16832
16833         lctl set_param -n mdc.*.stats=clear
16834         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16835         cat $dom > /dev/null
16836         local reads=$(lctl get_param -n mdc.*.stats |
16837                         awk '/ost_read/ {print $2}')
16838         [ -z $reads ] || error "Unexpected $reads READ RPCs"
16839         ls $dom
16840         rm -f $dom
16841 }
16842 run_test 271a "DoM: data is cached for read after write"
16843
16844 test_271b() {
16845         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16846                 skip "Need MDS version at least 2.10.55"
16847
16848         local dom=$DIR/$tdir/dom
16849
16850         mkdir -p $DIR/$tdir
16851
16852         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16853
16854         lctl set_param -n mdc.*.stats=clear
16855         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16856         cancel_lru_locks mdc
16857         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
16858         # second stat to check size is cached on client
16859         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
16860         local gls=$(lctl get_param -n mdc.*.stats |
16861                         awk '/ldlm_glimpse/ {print $2}')
16862         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16863         rm -f $dom
16864 }
16865 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
16866
16867 test_271ba() {
16868         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16869                 skip "Need MDS version at least 2.10.55"
16870
16871         local dom=$DIR/$tdir/dom
16872
16873         mkdir -p $DIR/$tdir
16874
16875         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16876
16877         lctl set_param -n mdc.*.stats=clear
16878         lctl set_param -n osc.*.stats=clear
16879         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
16880         cancel_lru_locks mdc
16881         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16882         # second stat to check size is cached on client
16883         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16884         local gls=$(lctl get_param -n mdc.*.stats |
16885                         awk '/ldlm_glimpse/ {print $2}')
16886         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16887         local gls=$(lctl get_param -n osc.*.stats |
16888                         awk '/ldlm_glimpse/ {print $2}')
16889         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
16890         rm -f $dom
16891 }
16892 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
16893
16894 test_271c() {
16895         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16896                 skip "Need MDS version at least 2.10.55"
16897
16898         local dom=$DIR/$tdir/dom
16899
16900         mkdir -p $DIR/$tdir
16901
16902         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16903
16904         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
16905         local facet=mds$((mdtidx + 1))
16906
16907         cancel_lru_locks mdc
16908         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
16909         createmany -o $dom 1000
16910         lctl set_param -n mdc.*.stats=clear
16911         smalliomany -w $dom 1000 200
16912         lctl get_param -n mdc.*.stats
16913         local enq=$(lctl get_param -n mdc.*.stats |
16914                         awk '/ldlm_ibits_enqueue/ {print $2}')
16915         # Each file has 1 open, 1 IO enqueues, total 2000
16916         # but now we have also +1 getxattr for security.capability, total 3000
16917         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
16918         unlinkmany $dom 1000
16919
16920         cancel_lru_locks mdc
16921         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
16922         createmany -o $dom 1000
16923         lctl set_param -n mdc.*.stats=clear
16924         smalliomany -w $dom 1000 200
16925         lctl get_param -n mdc.*.stats
16926         local enq_2=$(lctl get_param -n mdc.*.stats |
16927                         awk '/ldlm_ibits_enqueue/ {print $2}')
16928         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
16929         # for OPEN and IO lock.
16930         [ $((enq - enq_2)) -ge 1000 ] ||
16931                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
16932         unlinkmany $dom 1000
16933         return 0
16934 }
16935 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
16936
16937 test_275() {
16938         remote_ost_nodsh && skip "remote OST with nodsh"
16939         [ $(lustre_version_code ost1) -lt $(version_code 2.10.57) ] &&
16940                 skip "Need OST version >= 2.10.57"
16941
16942         local file=$DIR/$tfile
16943         local oss
16944
16945         oss=$(comma_list $(osts_nodes))
16946
16947         dd if=/dev/urandom of=$file bs=1M count=2 ||
16948                 error "failed to create a file"
16949         cancel_lru_locks osc
16950
16951         #lock 1
16952         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
16953                 error "failed to read a file"
16954
16955 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
16956         $LCTL set_param fail_loc=0x8000031f
16957
16958         cancel_lru_locks osc &
16959         sleep 1
16960
16961 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
16962         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
16963         #IO takes another lock, but matches the PENDING one
16964         #and places it to the IO RPC
16965         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
16966                 error "failed to read a file with PENDING lock"
16967 }
16968 run_test 275 "Read on a canceled duplicate lock"
16969
16970 test_276() {
16971         remote_ost_nodsh && skip "remote OST with nodsh"
16972         local pid
16973
16974         do_facet ost1 "(while true; do \
16975                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
16976                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
16977         pid=$!
16978
16979         for LOOP in $(seq 20); do
16980                 stop ost1
16981                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
16982         done
16983         kill -9 $pid
16984         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
16985                 rm $TMP/sanity_276_pid"
16986 }
16987 run_test 276 "Race between mount and obd_statfs"
16988
16989 cleanup_test_300() {
16990         trap 0
16991         umask $SAVE_UMASK
16992 }
16993 test_striped_dir() {
16994         local mdt_index=$1
16995         local stripe_count
16996         local stripe_index
16997
16998         mkdir -p $DIR/$tdir
16999
17000         SAVE_UMASK=$(umask)
17001         trap cleanup_test_300 RETURN EXIT
17002
17003         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
17004                                                 $DIR/$tdir/striped_dir ||
17005                 error "set striped dir error"
17006
17007         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
17008         [ "$mode" = "755" ] || error "expect 755 got $mode"
17009
17010         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
17011                 error "getdirstripe failed"
17012         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
17013         if [ "$stripe_count" != "2" ]; then
17014                 error "1:stripe_count is $stripe_count, expect 2"
17015         fi
17016         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
17017         if [ "$stripe_count" != "2" ]; then
17018                 error "2:stripe_count is $stripe_count, expect 2"
17019         fi
17020
17021         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
17022         if [ "$stripe_index" != "$mdt_index" ]; then
17023                 error "stripe_index is $stripe_index, expect $mdt_index"
17024         fi
17025
17026         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
17027                 error "nlink error after create striped dir"
17028
17029         mkdir $DIR/$tdir/striped_dir/a
17030         mkdir $DIR/$tdir/striped_dir/b
17031
17032         stat $DIR/$tdir/striped_dir/a ||
17033                 error "create dir under striped dir failed"
17034         stat $DIR/$tdir/striped_dir/b ||
17035                 error "create dir under striped dir failed"
17036
17037         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
17038                 error "nlink error after mkdir"
17039
17040         rmdir $DIR/$tdir/striped_dir/a
17041         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
17042                 error "nlink error after rmdir"
17043
17044         rmdir $DIR/$tdir/striped_dir/b
17045         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
17046                 error "nlink error after rmdir"
17047
17048         chattr +i $DIR/$tdir/striped_dir
17049         createmany -o $DIR/$tdir/striped_dir/f 10 &&
17050                 error "immutable flags not working under striped dir!"
17051         chattr -i $DIR/$tdir/striped_dir
17052
17053         rmdir $DIR/$tdir/striped_dir ||
17054                 error "rmdir striped dir error"
17055
17056         cleanup_test_300
17057
17058         true
17059 }
17060
17061 test_300a() {
17062         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17063                 skip "skipped for lustre < 2.7.0"
17064         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17065         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17066
17067         test_striped_dir 0 || error "failed on striped dir on MDT0"
17068         test_striped_dir 1 || error "failed on striped dir on MDT0"
17069 }
17070 run_test 300a "basic striped dir sanity test"
17071
17072 test_300b() {
17073         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17074                 skip "skipped for lustre < 2.7.0"
17075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17076         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17077
17078         local i
17079         local mtime1
17080         local mtime2
17081         local mtime3
17082
17083         test_mkdir $DIR/$tdir || error "mkdir fail"
17084         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17085                 error "set striped dir error"
17086         for i in {0..9}; do
17087                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
17088                 sleep 1
17089                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
17090                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
17091                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
17092                 sleep 1
17093                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
17094                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
17095                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
17096         done
17097         true
17098 }
17099 run_test 300b "check ctime/mtime for striped dir"
17100
17101 test_300c() {
17102         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17103                 skip "skipped for lustre < 2.7.0"
17104         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17105         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17106
17107         local file_count
17108
17109         mkdir -p $DIR/$tdir
17110         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
17111                 error "set striped dir error"
17112
17113         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
17114                 error "chown striped dir failed"
17115
17116         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
17117                 error "create 5k files failed"
17118
17119         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
17120
17121         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
17122
17123         rm -rf $DIR/$tdir
17124 }
17125 run_test 300c "chown && check ls under striped directory"
17126
17127 test_300d() {
17128         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
17129                 skip "skipped for lustre < 2.7.0"
17130         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17131         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17132
17133         local stripe_count
17134         local file
17135
17136         mkdir -p $DIR/$tdir
17137         $SETSTRIPE -c 2 $DIR/$tdir
17138
17139         #local striped directory
17140         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17141                 error "set striped dir error"
17142         createmany -o $DIR/$tdir/striped_dir/f 10 ||
17143                 error "create 10 files failed"
17144
17145         #remote striped directory
17146         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
17147                 error "set striped dir error"
17148         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
17149                 error "create 10 files failed"
17150
17151         for file in $(find $DIR/$tdir); do
17152                 stripe_count=$($LFS getstripe -c $file)
17153                 [ $stripe_count -eq 2 ] ||
17154                         error "wrong stripe $stripe_count for $file"
17155         done
17156
17157         rm -rf $DIR/$tdir
17158 }
17159 run_test 300d "check default stripe under striped directory"
17160
17161 test_300e() {
17162         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17163                 skip "Need MDS version at least 2.7.55"
17164         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17165         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17166
17167         local stripe_count
17168         local file
17169
17170         mkdir -p $DIR/$tdir
17171
17172         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17173                 error "set striped dir error"
17174
17175         touch $DIR/$tdir/striped_dir/a
17176         touch $DIR/$tdir/striped_dir/b
17177         touch $DIR/$tdir/striped_dir/c
17178
17179         mkdir $DIR/$tdir/striped_dir/dir_a
17180         mkdir $DIR/$tdir/striped_dir/dir_b
17181         mkdir $DIR/$tdir/striped_dir/dir_c
17182
17183         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
17184                 error "set striped adir under striped dir error"
17185
17186         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
17187                 error "set striped bdir under striped dir error"
17188
17189         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
17190                 error "set striped cdir under striped dir error"
17191
17192         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
17193                 error "rename dir under striped dir fails"
17194
17195         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
17196                 error "rename dir under different stripes fails"
17197
17198         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
17199                 error "rename file under striped dir should succeed"
17200
17201         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
17202                 error "rename dir under striped dir should succeed"
17203
17204         rm -rf $DIR/$tdir
17205 }
17206 run_test 300e "check rename under striped directory"
17207
17208 test_300f() {
17209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17210         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17211         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17212                 skip "Need MDS version at least 2.7.55"
17213
17214         local stripe_count
17215         local file
17216
17217         rm -rf $DIR/$tdir
17218         mkdir -p $DIR/$tdir
17219
17220         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
17221                 error "set striped dir error"
17222
17223         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
17224                 error "set striped dir error"
17225
17226         touch $DIR/$tdir/striped_dir/a
17227         mkdir $DIR/$tdir/striped_dir/dir_a
17228         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
17229                 error "create striped dir under striped dir fails"
17230
17231         touch $DIR/$tdir/striped_dir1/b
17232         mkdir $DIR/$tdir/striped_dir1/dir_b
17233         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
17234                 error "create striped dir under striped dir fails"
17235
17236         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
17237                 error "rename dir under different striped dir should fail"
17238
17239         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
17240                 error "rename striped dir under diff striped dir should fail"
17241
17242         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
17243                 error "rename file under diff striped dirs fails"
17244
17245         rm -rf $DIR/$tdir
17246 }
17247 run_test 300f "check rename cross striped directory"
17248
17249 test_300_check_default_striped_dir()
17250 {
17251         local dirname=$1
17252         local default_count=$2
17253         local default_index=$3
17254         local stripe_count
17255         local stripe_index
17256         local dir_stripe_index
17257         local dir
17258
17259         echo "checking $dirname $default_count $default_index"
17260         $LFS setdirstripe -D -c $default_count -i $default_index \
17261                                 -t all_char $DIR/$tdir/$dirname ||
17262                 error "set default stripe on striped dir error"
17263         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
17264         [ $stripe_count -eq $default_count ] ||
17265                 error "expect $default_count get $stripe_count for $dirname"
17266
17267         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
17268         [ $stripe_index -eq $default_index ] ||
17269                 error "expect $default_index get $stripe_index for $dirname"
17270
17271         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
17272                                                 error "create dirs failed"
17273
17274         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
17275         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
17276         for dir in $(find $DIR/$tdir/$dirname/*); do
17277                 stripe_count=$($LFS getdirstripe -c $dir)
17278                 [ $stripe_count -eq $default_count ] ||
17279                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
17280                 error "stripe count $default_count != $stripe_count for $dir"
17281
17282                 stripe_index=$($LFS getdirstripe -i $dir)
17283                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
17284                         error "$stripe_index != $default_index for $dir"
17285
17286                 #check default stripe
17287                 stripe_count=$($LFS getdirstripe -D -c $dir)
17288                 [ $stripe_count -eq $default_count ] ||
17289                 error "default count $default_count != $stripe_count for $dir"
17290
17291                 stripe_index=$($LFS getdirstripe -D -i $dir)
17292                 [ $stripe_index -eq $default_index ] ||
17293                 error "default index $default_index != $stripe_index for $dir"
17294         done
17295         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
17296 }
17297
17298 test_300g() {
17299         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17300         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17301                 skip "Need MDS version at least 2.7.55"
17302
17303         local dir
17304         local stripe_count
17305         local stripe_index
17306
17307         mkdir $DIR/$tdir
17308         mkdir $DIR/$tdir/normal_dir
17309
17310         #Checking when client cache stripe index
17311         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
17312         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
17313                 error "create striped_dir failed"
17314
17315         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
17316                 error "create dir0 fails"
17317         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
17318         [ $stripe_index -eq 0 ] ||
17319                 error "dir0 expect index 0 got $stripe_index"
17320
17321         mkdir $DIR/$tdir/striped_dir/dir1 ||
17322                 error "create dir1 fails"
17323         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
17324         [ $stripe_index -eq 1 ] ||
17325                 error "dir1 expect index 1 got $stripe_index"
17326
17327         #check default stripe count/stripe index
17328         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
17329         test_300_check_default_striped_dir normal_dir 1 0
17330         test_300_check_default_striped_dir normal_dir 2 1
17331         test_300_check_default_striped_dir normal_dir 2 -1
17332
17333         #delete default stripe information
17334         echo "delete default stripeEA"
17335         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
17336                 error "set default stripe on striped dir error"
17337
17338         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
17339         for dir in $(find $DIR/$tdir/normal_dir/*); do
17340                 stripe_count=$($LFS getdirstripe -c $dir)
17341                 [ $stripe_count -eq 0 ] ||
17342                         error "expect 1 get $stripe_count for $dir"
17343                 stripe_index=$($LFS getdirstripe -i $dir)
17344                 [ $stripe_index -eq 0 ] ||
17345                         error "expect 0 get $stripe_index for $dir"
17346         done
17347 }
17348 run_test 300g "check default striped directory for normal directory"
17349
17350 test_300h() {
17351         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17352         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17353                 skip "Need MDS version at least 2.7.55"
17354
17355         local dir
17356         local stripe_count
17357
17358         mkdir $DIR/$tdir
17359         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17360                 error "set striped dir error"
17361
17362         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
17363         test_300_check_default_striped_dir striped_dir 1 0
17364         test_300_check_default_striped_dir striped_dir 2 1
17365         test_300_check_default_striped_dir striped_dir 2 -1
17366
17367         #delete default stripe information
17368         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
17369                 error "set default stripe on striped dir error"
17370
17371         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
17372         for dir in $(find $DIR/$tdir/striped_dir/*); do
17373                 stripe_count=$($LFS getdirstripe -c $dir)
17374                 [ $stripe_count -eq 0 ] ||
17375                         error "expect 1 get $stripe_count for $dir"
17376         done
17377 }
17378 run_test 300h "check default striped directory for striped directory"
17379
17380 test_300i() {
17381         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17382         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17383         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17384                 skip "Need MDS version at least 2.7.55"
17385
17386         local stripe_count
17387         local file
17388
17389         mkdir $DIR/$tdir
17390
17391         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17392                 error "set striped dir error"
17393
17394         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
17395                 error "create files under striped dir failed"
17396
17397         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
17398                 error "set striped hashdir error"
17399
17400         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
17401                 error "create dir0 under hash dir failed"
17402         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
17403                 error "create dir1 under hash dir failed"
17404
17405         # unfortunately, we need to umount to clear dir layout cache for now
17406         # once we fully implement dir layout, we can drop this
17407         umount_client $MOUNT || error "umount failed"
17408         mount_client $MOUNT || error "mount failed"
17409
17410         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
17411         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
17412         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
17413
17414         #set the stripe to be unknown hash type
17415         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
17416         $LCTL set_param fail_loc=0x1901
17417         for ((i = 0; i < 10; i++)); do
17418                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
17419                         error "stat f-$i failed"
17420                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
17421         done
17422
17423         touch $DIR/$tdir/striped_dir/f0 &&
17424                 error "create under striped dir with unknown hash should fail"
17425
17426         $LCTL set_param fail_loc=0
17427
17428         umount_client $MOUNT || error "umount failed"
17429         mount_client $MOUNT || error "mount failed"
17430
17431         return 0
17432 }
17433 run_test 300i "client handle unknown hash type striped directory"
17434
17435 test_300j() {
17436         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17437         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17438         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17439                 skip "Need MDS version at least 2.7.55"
17440
17441         local stripe_count
17442         local file
17443
17444         mkdir $DIR/$tdir
17445
17446         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
17447         $LCTL set_param fail_loc=0x1702
17448         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17449                 error "set striped dir error"
17450
17451         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
17452                 error "create files under striped dir failed"
17453
17454         $LCTL set_param fail_loc=0
17455
17456         rm -rf $DIR/$tdir || error "unlink striped dir fails"
17457
17458         return 0
17459 }
17460 run_test 300j "test large update record"
17461
17462 test_300k() {
17463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17464         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17465         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17466                 skip "Need MDS version at least 2.7.55"
17467
17468         local stripe_count
17469         local file
17470
17471         mkdir $DIR/$tdir
17472
17473         #define OBD_FAIL_LARGE_STRIPE   0x1703
17474         $LCTL set_param fail_loc=0x1703
17475         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
17476                 error "set striped dir error"
17477         $LCTL set_param fail_loc=0
17478
17479         $LFS getdirstripe $DIR/$tdir/striped_dir ||
17480                 error "getstripeddir fails"
17481         rm -rf $DIR/$tdir/striped_dir ||
17482                 error "unlink striped dir fails"
17483
17484         return 0
17485 }
17486 run_test 300k "test large striped directory"
17487
17488 test_300l() {
17489         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17490         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17491         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17492                 skip "Need MDS version at least 2.7.55"
17493
17494         local stripe_index
17495
17496         test_mkdir -p $DIR/$tdir/striped_dir
17497         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
17498                         error "chown $RUNAS_ID failed"
17499         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
17500                 error "set default striped dir failed"
17501
17502         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
17503         $LCTL set_param fail_loc=0x80000158
17504         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
17505
17506         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
17507         [ $stripe_index -eq 1 ] ||
17508                 error "expect 1 get $stripe_index for $dir"
17509 }
17510 run_test 300l "non-root user to create dir under striped dir with stale layout"
17511
17512 test_300m() {
17513         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17514         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
17515         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17516                 skip "Need MDS version at least 2.7.55"
17517
17518         mkdir -p $DIR/$tdir/striped_dir
17519         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
17520                 error "set default stripes dir error"
17521
17522         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
17523
17524         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
17525         [ $stripe_count -eq 0 ] ||
17526                         error "expect 0 get $stripe_count for a"
17527
17528         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
17529                 error "set default stripes dir error"
17530
17531         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
17532
17533         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
17534         [ $stripe_count -eq 0 ] ||
17535                         error "expect 0 get $stripe_count for b"
17536
17537         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
17538                 error "set default stripes dir error"
17539
17540         mkdir $DIR/$tdir/striped_dir/c &&
17541                 error "default stripe_index is invalid, mkdir c should fails"
17542
17543         rm -rf $DIR/$tdir || error "rmdir fails"
17544 }
17545 run_test 300m "setstriped directory on single MDT FS"
17546
17547 cleanup_300n() {
17548         local list=$(comma_list $(mdts_nodes))
17549
17550         trap 0
17551         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
17552 }
17553
17554 test_300n() {
17555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17556         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17557         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17558                 skip "Need MDS version at least 2.7.55"
17559         remote_mds_nodsh && skip "remote MDS with nodsh"
17560
17561         local stripe_index
17562         local list=$(comma_list $(mdts_nodes))
17563
17564         trap cleanup_300n RETURN EXIT
17565         mkdir -p $DIR/$tdir
17566         chmod 777 $DIR/$tdir
17567         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
17568                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
17569                 error "create striped dir succeeds with gid=0"
17570
17571         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
17572         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
17573                 error "create striped dir fails with gid=-1"
17574
17575         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
17576         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
17577                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
17578                 error "set default striped dir succeeds with gid=0"
17579
17580
17581         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
17582         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
17583                 error "set default striped dir fails with gid=-1"
17584
17585
17586         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
17587         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
17588                                         error "create test_dir fails"
17589         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
17590                                         error "create test_dir1 fails"
17591         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
17592                                         error "create test_dir2 fails"
17593         cleanup_300n
17594 }
17595 run_test 300n "non-root user to create dir under striped dir with default EA"
17596
17597 test_300o() {
17598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17599         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17600         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
17601                 skip "Need MDS version at least 2.7.55"
17602
17603         local numfree1
17604         local numfree2
17605
17606         mkdir -p $DIR/$tdir
17607
17608         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
17609         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
17610         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
17611                 skip "not enough free inodes $numfree1 $numfree2"
17612         fi
17613
17614         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
17615         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
17616         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
17617                 skip "not enough free space $numfree1 $numfree2"
17618         fi
17619
17620         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
17621                 error "setdirstripe fails"
17622
17623         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
17624                 error "create dirs fails"
17625
17626         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
17627         ls $DIR/$tdir/striped_dir > /dev/null ||
17628                 error "ls striped dir fails"
17629         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
17630                 error "unlink big striped dir fails"
17631 }
17632 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
17633
17634 test_300p() {
17635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17636         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17637         remote_mds_nodsh && skip "remote MDS with nodsh"
17638
17639         mkdir -p $DIR/$tdir
17640
17641         #define OBD_FAIL_OUT_ENOSPC     0x1704
17642         do_facet mds2 lctl set_param fail_loc=0x80001704
17643         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
17644                  && error "create striped directory should fail"
17645
17646         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
17647
17648         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
17649         true
17650 }
17651 run_test 300p "create striped directory without space"
17652
17653 test_300q() {
17654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17655         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17656
17657         local fd=$(free_fd)
17658         local cmd="exec $fd<$tdir"
17659         cd $DIR
17660         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
17661         eval $cmd
17662         cmd="exec $fd<&-"
17663         trap "eval $cmd" EXIT
17664         cd $tdir || error "cd $tdir fails"
17665         rmdir  ../$tdir || error "rmdir $tdir fails"
17666         mkdir local_dir && error "create dir succeeds"
17667         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
17668         eval $cmd
17669         return 0
17670 }
17671 run_test 300q "create remote directory under orphan directory"
17672
17673 prepare_remote_file() {
17674         mkdir $DIR/$tdir/src_dir ||
17675                 error "create remote source failed"
17676
17677         cp /etc/hosts $DIR/$tdir/src_dir/a ||
17678                  error "cp to remote source failed"
17679         touch $DIR/$tdir/src_dir/a
17680
17681         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
17682                 error "create remote target dir failed"
17683
17684         touch $DIR/$tdir/tgt_dir/b
17685
17686         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
17687                 error "rename dir cross MDT failed!"
17688
17689         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
17690                 error "src_child still exists after rename"
17691
17692         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
17693                 error "missing file(a) after rename"
17694
17695         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
17696                 error "diff after rename"
17697 }
17698
17699 test_310a() {
17700         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
17701         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17702
17703         local remote_file=$DIR/$tdir/tgt_dir/b
17704
17705         mkdir -p $DIR/$tdir
17706
17707         prepare_remote_file || error "prepare remote file failed"
17708
17709         #open-unlink file
17710         $OPENUNLINK $remote_file $remote_file ||
17711                 error "openunlink $remote_file failed"
17712         $CHECKSTAT -a $remote_file || error "$remote_file exists"
17713 }
17714 run_test 310a "open unlink remote file"
17715
17716 test_310b() {
17717         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
17718         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17719
17720         local remote_file=$DIR/$tdir/tgt_dir/b
17721
17722         mkdir -p $DIR/$tdir
17723
17724         prepare_remote_file || error "prepare remote file failed"
17725
17726         ln $remote_file $DIR/$tfile || error "link failed for remote file"
17727         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
17728         $CHECKSTAT -t file $remote_file || error "check file failed"
17729 }
17730 run_test 310b "unlink remote file with multiple links while open"
17731
17732 test_310c() {
17733         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17734         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
17735
17736         local remote_file=$DIR/$tdir/tgt_dir/b
17737
17738         mkdir -p $DIR/$tdir
17739
17740         prepare_remote_file || error "prepare remote file failed"
17741
17742         ln $remote_file $DIR/$tfile || error "link failed for remote file"
17743         multiop_bg_pause $remote_file O_uc ||
17744                         error "mulitop failed for remote file"
17745         MULTIPID=$!
17746         $MULTIOP $DIR/$tfile Ouc
17747         kill -USR1 $MULTIPID
17748         wait $MULTIPID
17749 }
17750 run_test 310c "open-unlink remote file with multiple links"
17751
17752 #LU-4825
17753 test_311() {
17754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17755         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
17756         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
17757                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
17758         remote_mds_nodsh && skip "remote MDS with nodsh"
17759
17760         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
17761
17762         mkdir -p $DIR/$tdir
17763         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
17764         createmany -o $DIR/$tdir/$tfile. 1000
17765
17766         # statfs data is not real time, let's just calculate it
17767         old_iused=$((old_iused + 1000))
17768
17769         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
17770                         osp.*OST0000*MDT0000.create_count")
17771         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
17772                                 osp.*OST0000*MDT0000.max_create_count")
17773         for idx in $(seq $MDSCOUNT); do
17774                 do_facet mds$idx "lctl set_param -n \
17775                         osp.*OST0000*MDT000?.max_create_count=0"
17776         done
17777
17778         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
17779         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
17780         [ $index -ne 0 ] || error "$tfile stripe index is 0"
17781
17782         unlinkmany $DIR/$tdir/$tfile. 1000
17783
17784         for idx in $(seq $MDSCOUNT); do
17785                 do_facet mds$idx "lctl set_param -n \
17786                         osp.*OST0000*MDT000?.max_create_count=$max_count"
17787                 do_facet mds$idx "lctl set_param -n \
17788                         osp.*OST0000*MDT000?.create_count=$count"
17789         done
17790
17791         local new_iused
17792         for i in $(seq 120); do
17793                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
17794                 # system may be too busy to destroy all objs in time, use
17795                 # a somewhat small value to not fail autotest
17796                 [ $((old_iused - new_iused)) -gt 400 ] && break
17797                 sleep 1
17798         done
17799
17800         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
17801         [ $((old_iused - new_iused)) -gt 400 ] ||
17802                 error "objs not destroyed after unlink"
17803 }
17804 run_test 311 "disable OSP precreate, and unlink should destroy objs"
17805
17806 zfs_oid_to_objid()
17807 {
17808         local ost=$1
17809         local objid=$2
17810
17811         local vdevdir=$(dirname $(facet_vdevice $ost))
17812         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
17813         local zfs_zapid=$(do_facet $ost $cmd |
17814                           grep -w "/O/0/d$((objid%32))" -C 5 |
17815                           awk '/Object/{getline; print $1}')
17816         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
17817                           awk "/$objid = /"'{printf $3}')
17818
17819         echo $zfs_objid
17820 }
17821
17822 zfs_object_blksz() {
17823         local ost=$1
17824         local objid=$2
17825
17826         local vdevdir=$(dirname $(facet_vdevice $ost))
17827         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
17828         local blksz=$(do_facet $ost $cmd $objid |
17829                       awk '/dblk/{getline; printf $4}')
17830
17831         case "${blksz: -1}" in
17832                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
17833                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
17834                 *) ;;
17835         esac
17836
17837         echo $blksz
17838 }
17839
17840 test_312() { # LU-4856
17841         remote_ost_nodsh && skip "remote OST with nodsh"
17842         [ $(facet_fstype ost1) = "zfs" ] ||
17843                 skip_env "the test only applies to zfs"
17844
17845         local max_blksz=$(do_facet ost1 \
17846                           $ZFS get -p recordsize $(facet_device ost1) |
17847                           awk '!/VALUE/{print $3}')
17848         local min_blksz=$(getconf PAGE_SIZE)
17849
17850         # to make life a little bit easier
17851         $LFS mkdir -c 1 -i 0 $DIR/$tdir
17852         $LFS setstripe -c 1 -i 0 $DIR/$tdir
17853
17854         local tf=$DIR/$tdir/$tfile
17855         touch $tf
17856         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17857
17858         # Get ZFS object id
17859         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17860
17861         # block size change by sequential over write
17862         local blksz
17863         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
17864                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
17865
17866                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17867                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
17868         done
17869         rm -f $tf
17870
17871         # block size change by sequential append write
17872         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
17873         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17874         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17875
17876         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
17877                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
17878                         oflag=sync conv=notrunc
17879
17880                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17881                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
17882                         error "blksz error, actual $blksz, "    \
17883                                 "expected: 2 * $count * $min_blksz"
17884         done
17885         rm -f $tf
17886
17887         # random write
17888         touch $tf
17889         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17890         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17891
17892         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
17893         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17894         [ $blksz -eq $min_blksz ] ||
17895                 error "blksz error: $blksz, expected: $min_blksz"
17896
17897         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
17898         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17899         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
17900
17901         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
17902         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17903         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
17904 }
17905 run_test 312 "make sure ZFS adjusts its block size by write pattern"
17906
17907 test_313() {
17908         remote_ost_nodsh && skip "remote OST with nodsh"
17909
17910         local file=$DIR/$tfile
17911         rm -f $file
17912         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
17913
17914         # define OBD_FAIL_TGT_RCVD_EIO           0x720
17915         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17916         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
17917                 error "write should failed"
17918         do_facet ost1 "$LCTL set_param fail_loc=0"
17919         rm -f $file
17920 }
17921 run_test 313 "io should fail after last_rcvd update fail"
17922
17923 test_314() {
17924         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
17925
17926         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
17927         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17928         rm -f $DIR/$tfile
17929         wait_delete_completed
17930         do_facet ost1 "$LCTL set_param fail_loc=0"
17931 }
17932 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
17933
17934 test_315() { # LU-618
17935         local file=$DIR/$tfile
17936         rm -f $file
17937
17938         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
17939         $MULTIOP $file oO_RDONLY:r4096000_c &
17940         PID=$!
17941
17942         sleep 2
17943
17944         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
17945         kill -USR1 $PID
17946
17947         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
17948         rm -f $file
17949 }
17950 run_test 315 "read should be accounted"
17951
17952 test_316() {
17953         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17954         large_xattr_enabled || skip_env "large_xattr disabled"
17955
17956         rm -rf $DIR/$tdir/d
17957         mkdir -p $DIR/$tdir/d
17958         chown nobody $DIR/$tdir/d
17959         touch $DIR/$tdir/d/file
17960
17961         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
17962 }
17963 run_test 316 "lfs mv"
17964
17965 test_317() {
17966         local trunc_sz
17967         local grant_blk_size
17968
17969         if [ "$(facet_fstype $facet)" == "zfs" ]; then
17970                 skip "LU-10370: no implementation for ZFS" && return
17971         fi
17972
17973         stack_trap "rm -f $DIR/$tfile" EXIT
17974         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
17975                         awk '/grant_block_size:/ { print $2; exit; }')
17976         #
17977         # Create File of size 5M. Truncate it to below size's and verify
17978         # blocks count.
17979         #
17980         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
17981                 error "Create file : $DIR/$tfile"
17982
17983         for trunc_sz in 2097152 4097 4000 509 0; do
17984                 $TRUNCATE $DIR/$tfile $trunc_sz ||
17985                         error "truncate $tfile to $trunc_sz failed"
17986                 local sz=$(stat --format=%s $DIR/$tfile)
17987                 local blk=$(stat --format=%b $DIR/$tfile)
17988                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
17989                                      grant_blk_size) * 8))
17990
17991                 if [[ $blk -ne $trunc_blk ]]; then
17992                         $(which stat) $DIR/$tfile
17993                         error "Expected Block $trunc_blk got $blk for $tfile"
17994                 fi
17995
17996                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
17997                         error "Expected Size $trunc_sz got $sz for $tfile"
17998         done
17999
18000         #
18001         # sparse file test
18002         # Create file with a hole and write actual two blocks. Block count
18003         # must be 16.
18004         #
18005         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
18006                 conv=fsync || error "Create file : $DIR/$tfile"
18007
18008         # Calculate the final truncate size.
18009         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
18010
18011         #
18012         # truncate to size $trunc_sz bytes. Strip the last block
18013         # The block count must drop to 8
18014         #
18015         $TRUNCATE $DIR/$tfile $trunc_sz ||
18016                 error "truncate $tfile to $trunc_sz failed"
18017
18018         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
18019         sz=$(stat --format=%s $DIR/$tfile)
18020         blk=$(stat --format=%b $DIR/$tfile)
18021
18022         if [[ $blk -ne $trunc_bsz ]]; then
18023                 $(which stat) $DIR/$tfile
18024                 error "Expected Block $trunc_bsz got $blk for $tfile"
18025         fi
18026
18027         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
18028                 error "Expected Size $trunc_sz got $sz for $tfile"
18029 }
18030 run_test 317 "Verify blocks get correctly update after truncate"
18031
18032 test_fake_rw() {
18033         local read_write=$1
18034         if [ "$read_write" = "write" ]; then
18035                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
18036         elif [ "$read_write" = "read" ]; then
18037                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
18038         else
18039                 error "argument error"
18040         fi
18041
18042         # turn off debug for performance testing
18043         local saved_debug=$($LCTL get_param -n debug)
18044         $LCTL set_param debug=0
18045
18046         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
18047
18048         # get ost1 size - lustre-OST0000
18049         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
18050         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
18051         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
18052
18053         if [ "$read_write" = "read" ]; then
18054                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
18055         fi
18056
18057         local start_time=$(date +%s.%N)
18058         $dd_cmd bs=1M count=$blocks oflag=sync ||
18059                 error "real dd $read_write error"
18060         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
18061
18062         if [ "$read_write" = "write" ]; then
18063                 rm -f $DIR/$tfile
18064         fi
18065
18066         # define OBD_FAIL_OST_FAKE_RW           0x238
18067         do_facet ost1 $LCTL set_param fail_loc=0x238
18068
18069         local start_time=$(date +%s.%N)
18070         $dd_cmd bs=1M count=$blocks oflag=sync ||
18071                 error "fake dd $read_write error"
18072         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
18073
18074         if [ "$read_write" = "write" ]; then
18075                 # verify file size
18076                 cancel_lru_locks osc
18077                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
18078                         error "$tfile size not $blocks MB"
18079         fi
18080         do_facet ost1 $LCTL set_param fail_loc=0
18081
18082         echo "fake $read_write $duration_fake vs. normal $read_write" \
18083                 "$duration in seconds"
18084         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
18085                 error_not_in_vm "fake write is slower"
18086
18087         $LCTL set_param -n debug="$saved_debug"
18088         rm -f $DIR/$tfile
18089 }
18090 test_399a() { # LU-7655 for OST fake write
18091         remote_ost_nodsh && skip "remote OST with nodsh"
18092
18093         test_fake_rw write
18094 }
18095 run_test 399a "fake write should not be slower than normal write"
18096
18097 test_399b() { # LU-8726 for OST fake read
18098         remote_ost_nodsh && skip "remote OST with nodsh"
18099         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
18100                 skip_env "ldiskfs only test"
18101         fi
18102
18103         test_fake_rw read
18104 }
18105 run_test 399b "fake read should not be slower than normal read"
18106
18107 test_400a() { # LU-1606, was conf-sanity test_74
18108         if ! which $CC > /dev/null 2>&1; then
18109                 skip_env "$CC is not installed"
18110         fi
18111
18112         local extra_flags=''
18113         local out=$TMP/$tfile
18114         local prefix=/usr/include/lustre
18115         local prog
18116
18117         if ! [[ -d $prefix ]]; then
18118                 # Assume we're running in tree and fixup the include path.
18119                 extra_flags+=" -I$LUSTRE/include"
18120                 extra_flags+=" -L$LUSTRE/utils"
18121         fi
18122
18123         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
18124                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
18125                         error "client api broken"
18126         done
18127         rm -f $out
18128 }
18129 run_test 400a "Lustre client api program can compile and link"
18130
18131 test_400b() { # LU-1606, LU-5011
18132         local header
18133         local out=$TMP/$tfile
18134         local prefix=/usr/include/linux/lustre
18135
18136         # We use a hard coded prefix so that this test will not fail
18137         # when run in tree. There are headers in lustre/include/lustre/
18138         # that are not packaged (like lustre_idl.h) and have more
18139         # complicated include dependencies (like config.h and lnet/types.h).
18140         # Since this test about correct packaging we just skip them when
18141         # they don't exist (see below) rather than try to fixup cppflags.
18142
18143         if ! which $CC > /dev/null 2>&1; then
18144                 skip_env "$CC is not installed"
18145         fi
18146
18147         for header in $prefix/*.h; do
18148                 if ! [[ -f "$header" ]]; then
18149                         continue
18150                 fi
18151
18152                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
18153                         continue # lustre_ioctl.h is internal header
18154                 fi
18155
18156                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
18157                         error "cannot compile '$header'"
18158         done
18159         rm -f $out
18160 }
18161 run_test 400b "packaged headers can be compiled"
18162
18163 test_401a() { #LU-7437
18164         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
18165         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
18166
18167         #count the number of parameters by "list_param -R"
18168         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
18169         #count the number of parameters by listing proc files
18170         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
18171         echo "proc_dirs='$proc_dirs'"
18172         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
18173         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
18174                       sort -u | wc -l)
18175
18176         [ $params -eq $procs ] ||
18177                 error "found $params parameters vs. $procs proc files"
18178
18179         # test the list_param -D option only returns directories
18180         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
18181         #count the number of parameters by listing proc directories
18182         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
18183                 sort -u | wc -l)
18184
18185         [ $params -eq $procs ] ||
18186                 error "found $params parameters vs. $procs proc files"
18187 }
18188 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
18189
18190 test_401b() {
18191         local save=$($LCTL get_param -n jobid_var)
18192         local tmp=testing
18193
18194         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
18195                 error "no error returned when setting bad parameters"
18196
18197         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
18198         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
18199
18200         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
18201         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
18202         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
18203 }
18204 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
18205
18206 test_401c() {
18207         local jobid_var_old=$($LCTL get_param -n jobid_var)
18208         local jobid_var_new
18209
18210         $LCTL set_param jobid_var= &&
18211                 error "no error returned for 'set_param a='"
18212
18213         jobid_var_new=$($LCTL get_param -n jobid_var)
18214         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
18215                 error "jobid_var was changed by setting without value"
18216
18217         $LCTL set_param jobid_var &&
18218                 error "no error returned for 'set_param a'"
18219
18220         jobid_var_new=$($LCTL get_param -n jobid_var)
18221         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
18222                 error "jobid_var was changed by setting without value"
18223 }
18224 run_test 401c "Verify 'lctl set_param' without value fails in either format."
18225
18226 test_401d() {
18227         local jobid_var_old=$($LCTL get_param -n jobid_var)
18228         local jobid_var_new
18229         local new_value="foo=bar"
18230
18231         $LCTL set_param jobid_var=$new_value ||
18232                 error "'set_param a=b' did not accept a value containing '='"
18233
18234         jobid_var_new=$($LCTL get_param -n jobid_var)
18235         [[ "$jobid_var_new" == "$new_value" ]] ||
18236                 error "'set_param a=b' failed on a value containing '='"
18237
18238         # Reset the jobid_var to test the other format
18239         $LCTL set_param jobid_var=$jobid_var_old
18240         jobid_var_new=$($LCTL get_param -n jobid_var)
18241         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
18242                 error "failed to reset jobid_var"
18243
18244         $LCTL set_param jobid_var $new_value ||
18245                 error "'set_param a b' did not accept a value containing '='"
18246
18247         jobid_var_new=$($LCTL get_param -n jobid_var)
18248         [[ "$jobid_var_new" == "$new_value" ]] ||
18249                 error "'set_param a b' failed on a value containing '='"
18250
18251         $LCTL set_param jobid_var $jobid_var_old
18252         jobid_var_new=$($LCTL get_param -n jobid_var)
18253         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
18254                 error "failed to reset jobid_var"
18255 }
18256 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
18257
18258 test_402() {
18259         local server_version=$(lustre_version_code $SINGLEMDS)
18260         [[ $server_version -ge $(version_code 2.7.66) ]] ||
18261         [[ $server_version -ge $(version_code 2.7.18.4) &&
18262                 $server_version -lt $(version_code 2.7.50) ]] ||
18263         [[ $server_version -ge $(version_code 2.7.2) &&
18264                 $server_version -lt $(version_code 2.7.11) ]] ||
18265                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
18266         remote_mds_nodsh && skip "remote MDS with nodsh"
18267
18268         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
18269 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
18270         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
18271         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
18272                 echo "Touch failed - OK"
18273 }
18274 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
18275
18276 test_403() {
18277         local file1=$DIR/$tfile.1
18278         local file2=$DIR/$tfile.2
18279         local tfile=$TMP/$tfile
18280
18281         rm -f $file1 $file2 $tfile
18282
18283         touch $file1
18284         ln $file1 $file2
18285
18286         # 30 sec OBD_TIMEOUT in ll_getattr()
18287         # right before populating st_nlink
18288         $LCTL set_param fail_loc=0x80001409
18289         stat -c %h $file1 > $tfile &
18290
18291         # create an alias, drop all locks and reclaim the dentry
18292         < $file2
18293         cancel_lru_locks mdc
18294         cancel_lru_locks osc
18295         sysctl -w vm.drop_caches=2
18296
18297         wait
18298
18299         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
18300
18301         rm -f $tfile $file1 $file2
18302 }
18303 run_test 403 "i_nlink should not drop to zero due to aliasing"
18304
18305 test_404() { # LU-6601
18306         local server_version=$(lustre_version_code $SINGLEMDS)
18307         [[ $server_version -ge $(version_code 2.8.53) ]] ||
18308                 skip "Need server version newer than 2.8.52"
18309         remote_mds_nodsh && skip "remote MDS with nodsh"
18310
18311         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
18312                 awk '/osp .*-osc-MDT/ { print $4}')
18313
18314         local osp
18315         for osp in $mosps; do
18316                 echo "Deactivate: " $osp
18317                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
18318                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
18319                         awk -vp=$osp '$4 == p { print $2 }')
18320                 [ $stat = IN ] || {
18321                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
18322                         error "deactivate error"
18323                 }
18324                 echo "Activate: " $osp
18325                 do_facet $SINGLEMDS $LCTL --device %$osp activate
18326                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
18327                         awk -vp=$osp '$4 == p { print $2 }')
18328                 [ $stat = UP ] || {
18329                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
18330                         error "activate error"
18331                 }
18332         done
18333 }
18334 run_test 404 "validate manual {de}activated works properly for OSPs"
18335
18336 test_405() {
18337         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
18338         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
18339                 skip "Layout swap lock is not supported"
18340         check_swap_layouts_support
18341
18342         test_mkdir $DIR/$tdir
18343         swap_lock_test -d $DIR/$tdir ||
18344                 error "One layout swap locked test failed"
18345 }
18346 run_test 405 "Various layout swap lock tests"
18347
18348 test_406() {
18349         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18350         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
18351         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
18352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18353         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
18354                 skip "Need MDS version at least 2.8.50"
18355
18356         local def_stripe_size=$($LFS getstripe -S $MOUNT)
18357         local test_pool=$TESTNAME
18358
18359         if ! combined_mgs_mds ; then
18360                 mount_mgs_client
18361         fi
18362         pool_add $test_pool || error "pool_add failed"
18363         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
18364                 error "pool_add_targets failed"
18365
18366         save_layout_restore_at_exit $MOUNT
18367
18368         # parent set default stripe count only, child will stripe from both
18369         # parent and fs default
18370         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
18371                 error "setstripe $MOUNT failed"
18372         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
18373         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
18374         for i in $(seq 10); do
18375                 local f=$DIR/$tdir/$tfile.$i
18376                 touch $f || error "touch failed"
18377                 local count=$($LFS getstripe -c $f)
18378                 [ $count -eq $OSTCOUNT ] ||
18379                         error "$f stripe count $count != $OSTCOUNT"
18380                 local offset=$($LFS getstripe -i $f)
18381                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
18382                 local size=$($LFS getstripe -S $f)
18383                 [ $size -eq $((def_stripe_size * 2)) ] ||
18384                         error "$f stripe size $size != $((def_stripe_size * 2))"
18385                 local pool=$($LFS getstripe -p $f)
18386                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
18387         done
18388
18389         # change fs default striping, delete parent default striping, now child
18390         # will stripe from new fs default striping only
18391         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
18392                 error "change $MOUNT default stripe failed"
18393         $LFS setstripe -c 0 $DIR/$tdir ||
18394                 error "delete $tdir default stripe failed"
18395         for i in $(seq 11 20); do
18396                 local f=$DIR/$tdir/$tfile.$i
18397                 touch $f || error "touch $f failed"
18398                 local count=$($LFS getstripe -c $f)
18399                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
18400                 local offset=$($LFS getstripe -i $f)
18401                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
18402                 local size=$($LFS getstripe -S $f)
18403                 [ $size -eq $def_stripe_size ] ||
18404                         error "$f stripe size $size != $def_stripe_size"
18405                 local pool=$($LFS getstripe -p $f)
18406                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
18407         done
18408
18409         unlinkmany $DIR/$tdir/$tfile. 1 20
18410
18411         local f=$DIR/$tdir/$tfile
18412         pool_remove_all_targets $test_pool $f
18413         pool_remove $test_pool $f
18414
18415         if ! combined_mgs_mds ; then
18416                 umount_mgs_client
18417         fi
18418 }
18419 run_test 406 "DNE support fs default striping"
18420
18421 test_407() {
18422         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18423         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
18424                 skip "Need MDS version at least 2.8.55"
18425         remote_mds_nodsh && skip "remote MDS with nodsh"
18426
18427         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
18428                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
18429         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
18430                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
18431         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
18432
18433         #define OBD_FAIL_DT_TXN_STOP    0x2019
18434         for idx in $(seq $MDSCOUNT); do
18435                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
18436         done
18437         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
18438         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
18439                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
18440         true
18441 }
18442 run_test 407 "transaction fail should cause operation fail"
18443
18444 test_408() {
18445         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
18446
18447         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
18448         lctl set_param fail_loc=0x8000040a
18449         # let ll_prepare_partial_page() fail
18450         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
18451
18452         rm -f $DIR/$tfile
18453
18454         # create at least 100 unused inodes so that
18455         # shrink_icache_memory(0) should not return 0
18456         touch $DIR/$tfile-{0..100}
18457         rm -f $DIR/$tfile-{0..100}
18458         sync
18459
18460         echo 2 > /proc/sys/vm/drop_caches
18461 }
18462 run_test 408 "drop_caches should not hang due to page leaks"
18463
18464 test_409()
18465 {
18466         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18467         check_mount_and_prep
18468
18469         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
18470         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
18471         touch $DIR/$tdir/guard || error "(2) Fail to create"
18472
18473         local PREFIX=$(str_repeat 'A' 128)
18474         echo "Create 1K hard links start at $(date)"
18475         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
18476                 error "(3) Fail to hard link"
18477
18478         echo "Links count should be right although linkEA overflow"
18479         stat $DIR/$tdir/guard || error "(4) Fail to stat"
18480         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
18481         [ $linkcount -eq 1001 ] ||
18482                 error "(5) Unexpected hard links count: $linkcount"
18483
18484         echo "List all links start at $(date)"
18485         ls -l $DIR/$tdir/foo > /dev/null ||
18486                 error "(6) Fail to list $DIR/$tdir/foo"
18487
18488         echo "Unlink hard links start at $(date)"
18489         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
18490                 error "(7) Fail to unlink"
18491 }
18492 run_test 409 "Large amount of cross-MDTs hard links on the same file"
18493
18494 test_410()
18495 {
18496         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
18497                 skip "Need client version at least 2.9.59"
18498
18499         # Create a file, and stat it from the kernel
18500         local testfile=$DIR/$tfile
18501         touch $testfile
18502
18503         local run_id=$RANDOM
18504         local my_ino=$(stat --format "%i" $testfile)
18505
18506         # Try to insert the module. This will always fail as the
18507         # module is designed to not be inserted.
18508         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
18509             &> /dev/null
18510
18511         # Anything but success is a test failure
18512         dmesg | grep -q \
18513             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
18514             error "no inode match"
18515 }
18516 run_test 410 "Test inode number returned from kernel thread"
18517
18518 cleanup_test411_cgroup() {
18519         trap 0
18520         rmdir "$1"
18521 }
18522
18523 test_411() {
18524         local cg_basedir=/sys/fs/cgroup/memory
18525         # LU-9966
18526         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
18527                 skip "no setup for cgroup"
18528
18529         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
18530                 error "test file creation failed"
18531         cancel_lru_locks osc
18532
18533         # Create a very small memory cgroup to force a slab allocation error
18534         local cgdir=$cg_basedir/osc_slab_alloc
18535         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
18536         trap "cleanup_test411_cgroup $cgdir" EXIT
18537         echo 2M > $cgdir/memory.kmem.limit_in_bytes
18538         echo 1M > $cgdir/memory.limit_in_bytes
18539
18540         # Should not LBUG, just be killed by oom-killer
18541         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
18542                 error "fail to trigger a memory allocation error"
18543
18544         cleanup_test411_cgroup $cgdir
18545
18546         return 0
18547 }
18548 run_test 411 "Slab allocation error with cgroup does not LBUG"
18549
18550 test_412() {
18551         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18552         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
18553                 skip "Need server version at least 2.10.55"
18554         fi
18555
18556         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
18557                 error "mkdir failed"
18558         $LFS getdirstripe $DIR/$tdir
18559         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
18560         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
18561                 error "expect $((MDSCOUT - 1)) get $stripe_index"
18562         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
18563         [ $stripe_count -eq 2 ] ||
18564                 error "expect 2 get $stripe_count"
18565 }
18566 run_test 412 "mkdir on specific MDTs"
18567
18568 test_413() {
18569         [ $MDSCOUNT -lt 2 ] &&
18570                 skip "We need at least 2 MDTs for this test"
18571
18572         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
18573                 skip "Need server version at least 2.10.55"
18574         fi
18575
18576         mkdir $DIR/$tdir || error "mkdir failed"
18577
18578         # find MDT that is the most full
18579         local max=$($LFS df | grep MDT |
18580                 awk 'BEGIN { a=0 }
18581                         { sub("%", "", $5)
18582                           if (0+$5 >= a)
18583                           {
18584                                 a = $5
18585                                 b = $6
18586                           }
18587                         }
18588                      END { split(b, c, ":")
18589                            sub("]", "", c[2])
18590                            print c[2]
18591                          }')
18592
18593         for i in $(seq $((MDSCOUNT - 1))); do
18594                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
18595                         error "mkdir d$i failed"
18596                 $LFS getdirstripe $DIR/$tdir/d$i
18597                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
18598                 [ $stripe_index -ne $max ] ||
18599                         error "don't expect $max"
18600         done
18601 }
18602 run_test 413 "mkdir on less full MDTs"
18603
18604 test_414() {
18605 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
18606         $LCTL set_param fail_loc=0x80000521
18607         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
18608         rm -f $DIR/$tfile
18609 }
18610 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
18611
18612 prep_801() {
18613         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
18614         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
18615                 skip "Need server version at least 2.9.55"
18616
18617         start_full_debug_logging
18618 }
18619
18620 post_801() {
18621         stop_full_debug_logging
18622 }
18623
18624 barrier_stat() {
18625         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
18626                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
18627                            awk '/The barrier for/ { print $7 }')
18628                 echo $st
18629         else
18630                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
18631                 echo \'$st\'
18632         fi
18633 }
18634
18635 barrier_expired() {
18636         local expired
18637
18638         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
18639                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
18640                           awk '/will be expired/ { print $7 }')
18641         else
18642                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
18643         fi
18644
18645         echo $expired
18646 }
18647
18648 test_801a() {
18649         prep_801
18650
18651         echo "Start barrier_freeze at: $(date)"
18652         #define OBD_FAIL_BARRIER_DELAY          0x2202
18653         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
18654         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
18655
18656         sleep 2
18657         local b_status=$(barrier_stat)
18658         echo "Got barrier status at: $(date)"
18659         [ "$b_status" = "'freezing_p1'" ] ||
18660                 error "(1) unexpected barrier status $b_status"
18661
18662         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
18663         wait
18664         b_status=$(barrier_stat)
18665         [ "$b_status" = "'frozen'" ] ||
18666                 error "(2) unexpected barrier status $b_status"
18667
18668         local expired=$(barrier_expired)
18669         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
18670         sleep $((expired + 3))
18671
18672         b_status=$(barrier_stat)
18673         [ "$b_status" = "'expired'" ] ||
18674                 error "(3) unexpected barrier status $b_status"
18675
18676         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
18677                 error "(4) fail to freeze barrier"
18678
18679         b_status=$(barrier_stat)
18680         [ "$b_status" = "'frozen'" ] ||
18681                 error "(5) unexpected barrier status $b_status"
18682
18683         echo "Start barrier_thaw at: $(date)"
18684         #define OBD_FAIL_BARRIER_DELAY          0x2202
18685         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
18686         do_facet mgs $LCTL barrier_thaw $FSNAME &
18687
18688         sleep 2
18689         b_status=$(barrier_stat)
18690         echo "Got barrier status at: $(date)"
18691         [ "$b_status" = "'thawing'" ] ||
18692                 error "(6) unexpected barrier status $b_status"
18693
18694         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
18695         wait
18696         b_status=$(barrier_stat)
18697         [ "$b_status" = "'thawed'" ] ||
18698                 error "(7) unexpected barrier status $b_status"
18699
18700         #define OBD_FAIL_BARRIER_FAILURE        0x2203
18701         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
18702         do_facet mgs $LCTL barrier_freeze $FSNAME
18703
18704         b_status=$(barrier_stat)
18705         [ "$b_status" = "'failed'" ] ||
18706                 error "(8) unexpected barrier status $b_status"
18707
18708         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18709         do_facet mgs $LCTL barrier_thaw $FSNAME
18710
18711         post_801
18712 }
18713 run_test 801a "write barrier user interfaces and stat machine"
18714
18715 test_801b() {
18716         prep_801
18717
18718         mkdir $DIR/$tdir || error "(1) fail to mkdir"
18719         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
18720         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
18721         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
18722         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
18723
18724         cancel_lru_locks mdc
18725
18726         # 180 seconds should be long enough
18727         do_facet mgs $LCTL barrier_freeze $FSNAME 180
18728
18729         local b_status=$(barrier_stat)
18730         [ "$b_status" = "'frozen'" ] ||
18731                 error "(6) unexpected barrier status $b_status"
18732
18733         mkdir $DIR/$tdir/d0/d10 &
18734         mkdir_pid=$!
18735
18736         touch $DIR/$tdir/d1/f13 &
18737         touch_pid=$!
18738
18739         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
18740         ln_pid=$!
18741
18742         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
18743         mv_pid=$!
18744
18745         rm -f $DIR/$tdir/d4/f12 &
18746         rm_pid=$!
18747
18748         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
18749
18750         # To guarantee taht the 'stat' is not blocked
18751         b_status=$(barrier_stat)
18752         [ "$b_status" = "'frozen'" ] ||
18753                 error "(8) unexpected barrier status $b_status"
18754
18755         # let above commands to run at background
18756         sleep 5
18757
18758         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
18759         ps -p $touch_pid || error "(10) touch should be blocked"
18760         ps -p $ln_pid || error "(11) link should be blocked"
18761         ps -p $mv_pid || error "(12) rename should be blocked"
18762         ps -p $rm_pid || error "(13) unlink should be blocked"
18763
18764         b_status=$(barrier_stat)
18765         [ "$b_status" = "'frozen'" ] ||
18766                 error "(14) unexpected barrier status $b_status"
18767
18768         do_facet mgs $LCTL barrier_thaw $FSNAME
18769         b_status=$(barrier_stat)
18770         [ "$b_status" = "'thawed'" ] ||
18771                 error "(15) unexpected barrier status $b_status"
18772
18773         wait $mkdir_pid || error "(16) mkdir should succeed"
18774         wait $touch_pid || error "(17) touch should succeed"
18775         wait $ln_pid || error "(18) link should succeed"
18776         wait $mv_pid || error "(19) rename should succeed"
18777         wait $rm_pid || error "(20) unlink should succeed"
18778
18779         post_801
18780 }
18781 run_test 801b "modification will be blocked by write barrier"
18782
18783 test_801c() {
18784         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18785
18786         prep_801
18787
18788         stop mds2 || error "(1) Fail to stop mds2"
18789
18790         do_facet mgs $LCTL barrier_freeze $FSNAME 30
18791
18792         local b_status=$(barrier_stat)
18793         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
18794                 do_facet mgs $LCTL barrier_thaw $FSNAME
18795                 error "(2) unexpected barrier status $b_status"
18796         }
18797
18798         do_facet mgs $LCTL barrier_rescan $FSNAME ||
18799                 error "(3) Fail to rescan barrier bitmap"
18800
18801         do_facet mgs $LCTL barrier_freeze $FSNAME 10
18802
18803         b_status=$(barrier_stat)
18804         [ "$b_status" = "'frozen'" ] ||
18805                 error "(4) unexpected barrier status $b_status"
18806
18807         do_facet mgs $LCTL barrier_thaw $FSNAME
18808         b_status=$(barrier_stat)
18809         [ "$b_status" = "'thawed'" ] ||
18810                 error "(5) unexpected barrier status $b_status"
18811
18812         local devname=$(mdsdevname 2)
18813
18814         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
18815
18816         do_facet mgs $LCTL barrier_rescan $FSNAME ||
18817                 error "(7) Fail to rescan barrier bitmap"
18818
18819         post_801
18820 }
18821 run_test 801c "rescan barrier bitmap"
18822
18823 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
18824 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
18825 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
18826
18827 cleanup_802() {
18828         trap 0
18829
18830         stopall
18831         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
18832         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
18833         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
18834         setupall
18835 }
18836
18837 test_802() {
18838
18839         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
18840         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
18841                 skip "Need server version at least 2.9.55"
18842
18843         mkdir $DIR/$tdir || error "(1) fail to mkdir"
18844
18845         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
18846                 error "(2) Fail to copy"
18847
18848         trap cleanup_802 EXIT
18849
18850         # sync by force before remount as readonly
18851         sync; sync_all_data; sleep 3; sync_all_data
18852
18853         stopall
18854
18855         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
18856         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
18857         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
18858
18859         echo "Mount the server as read only"
18860         setupall server_only || error "(3) Fail to start servers"
18861
18862         echo "Mount client without ro should fail"
18863         mount_client $MOUNT &&
18864                 error "(4) Mount client without 'ro' should fail"
18865
18866         echo "Mount client with ro should succeed"
18867         mount_client $MOUNT ro ||
18868                 error "(5) Mount client with 'ro' should succeed"
18869
18870         echo "Modify should be refused"
18871         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
18872
18873         echo "Read should be allowed"
18874         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
18875                 error "(7) Read should succeed under ro mode"
18876
18877         cleanup_802
18878 }
18879 run_test 802 "simulate readonly device"
18880
18881 test_803() {
18882         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18883         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
18884                 skip "MDS needs to be newer than 2.10.54"
18885
18886         mkdir -p $DIR/$tdir
18887         # Create some objects on all MDTs to trigger related logs objects
18888         for idx in $(seq $MDSCOUNT); do
18889                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
18890                         $DIR/$tdir/dir${idx} ||
18891                         error "Fail to create $DIR/$tdir/dir${idx}"
18892         done
18893
18894         sync; sleep 3
18895         echo "before create:"
18896         $LFS df -i $MOUNT
18897         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
18898
18899         for ((i=0; i<10; i++)); do
18900                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
18901                         error "Fail to create $DIR/$tdir/foo$i"
18902         done
18903
18904         sync; sleep 3
18905         echo "after create:"
18906         $LFS df -i $MOUNT
18907         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
18908
18909         [ $after_used -ge $((before_used + 10)) ] ||
18910                 error "before ($before_used) + 10 > after ($after_used)"
18911
18912         for ((i=0; i<10; i++)); do
18913                 rm -rf $DIR/$tdir/foo$i ||
18914                         error "Fail to remove $DIR/$tdir/foo$i"
18915         done
18916
18917         sleep 3 # avoid MDT return cached statfs
18918         wait_delete_completed
18919         echo "after unlink:"
18920         $LFS df -i $MOUNT
18921         before_used=$after_used
18922         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
18923
18924         [ $after_used -le $((before_used - 8)) ] ||
18925                 error "before ($before_used) - 8 < after ($after_used)"
18926 }
18927 run_test 803 "verify agent object for remote object"
18928
18929 test_804() {
18930         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18931         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
18932                 skip "MDS needs to be newer than 2.10.54"
18933         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
18934                 skip_env "ldiskfs only test"
18935
18936         mkdir -p $DIR/$tdir
18937         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
18938                 error "Fail to create $DIR/$tdir/dir0"
18939
18940         local fid=$($LFS path2fid $DIR/$tdir/dir0)
18941         local dev=$(mdsdevname 2)
18942
18943         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18944                 grep ${fid} || error "NOT found agent entry for dir0"
18945
18946         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
18947                 error "Fail to create $DIR/$tdir/dir1"
18948
18949         touch $DIR/$tdir/dir1/foo0 ||
18950                 error "Fail to create $DIR/$tdir/dir1/foo0"
18951         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
18952         local rc=0
18953
18954         for idx in $(seq $MDSCOUNT); do
18955                 dev=$(mdsdevname $idx)
18956                 do_facet mds${idx} \
18957                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18958                         grep ${fid} && rc=$idx
18959         done
18960
18961         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
18962                 error "Fail to rename foo0 to foo1"
18963         if [ $rc -eq 0 ]; then
18964                 for idx in $(seq $MDSCOUNT); do
18965                         dev=$(mdsdevname $idx)
18966                         do_facet mds${idx} \
18967                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18968                         grep ${fid} && rc=$idx
18969                 done
18970         fi
18971
18972         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
18973                 error "Fail to rename foo1 to foo2"
18974         if [ $rc -eq 0 ]; then
18975                 for idx in $(seq $MDSCOUNT); do
18976                         dev=$(mdsdevname $idx)
18977                         do_facet mds${idx} \
18978                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18979                         grep ${fid} && rc=$idx
18980                 done
18981         fi
18982
18983         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
18984
18985         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
18986                 error "Fail to link to $DIR/$tdir/dir1/foo2"
18987         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
18988                 error "Fail to rename foo2 to foo0"
18989         unlink $DIR/$tdir/dir1/foo0 ||
18990                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
18991         rm -rf $DIR/$tdir/dir0 ||
18992                 error "Fail to rm $DIR/$tdir/dir0"
18993
18994         for idx in $(seq $MDSCOUNT); do
18995                 dev=$(mdsdevname $idx)
18996                 rc=0
18997
18998                 stop mds${idx}
18999                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
19000                         rc=$?
19001                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
19002                         error "mount mds$idx failed"
19003                 df $MOUNT > /dev/null 2>&1
19004
19005                 # e2fsck should not return error
19006                 [ $rc -eq 0 ] ||
19007                         error "e2fsck detected error on MDT${idx}: rc=$rc"
19008         done
19009 }
19010 run_test 804 "verify agent entry for remote entry"
19011
19012 cleanup_805() {
19013         do_facet $SINGLEMDS zfs set quota=$old $fsset
19014         unlinkmany $DIR/$tdir/f- 1000000
19015         trap 0
19016 }
19017
19018 test_805() {
19019         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
19020         [ "$(facet_fstype mds1)" != "zfs" ] &&
19021                 skip "ZFS specific test"
19022         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
19023                 skip "netfree not implemented before 0.7"
19024         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.57) ]] ||
19025                 skip "Need MDS version at least 2.10.57"
19026
19027         local fsset
19028         local freekb
19029         local usedkb
19030         local old
19031         local quota
19032         local pref="osd-zfs.lustre-MDT0000."
19033
19034         # limit available space on MDS dataset to meet nospace issue
19035         # quickly. then ZFS 0.7.2 can use reserved space if asked
19036         # properly (using netfree flag in osd_declare_destroy()
19037         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
19038         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
19039                 gawk '{print $3}')
19040         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
19041         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
19042         let "usedkb=usedkb-freekb"
19043         let "freekb=freekb/2"
19044         if let "freekb > 5000"; then
19045                 let "freekb=5000"
19046         fi
19047         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
19048         trap cleanup_805 EXIT
19049         mkdir $DIR/$tdir
19050         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
19051         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
19052         rm -rf $DIR/$tdir || error "not able to remove"
19053         do_facet $SINGLEMDS zfs set quota=$old $fsset
19054         trap 0
19055 }
19056 run_test 805 "ZFS can remove from full fs"
19057
19058 #
19059 # tests that do cleanup/setup should be run at the end
19060 #
19061
19062 test_900() {
19063         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19064         local ls
19065
19066         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
19067         $LCTL set_param fail_loc=0x903
19068
19069         cancel_lru_locks MGC
19070
19071         FAIL_ON_ERROR=true cleanup
19072         FAIL_ON_ERROR=true setup
19073 }
19074 run_test 900 "umount should not race with any mgc requeue thread"
19075
19076 complete $SECONDS
19077 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
19078 check_and_cleanup_lustre
19079 if [ "$I_MOUNTED" != "yes" ]; then
19080         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
19081 fi
19082 exit_status