Whamcloud - gitweb
LU-12483 tests: fix sanity test 60h running conditions
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # Check Grants after these tests
12 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
13 OSC=${OSC:-"osc"}
14
15 CC=${CC:-cc}
16 CREATETEST=${CREATETEST:-createtest}
17 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
18 OPENFILE=${OPENFILE:-openfile}
19 OPENUNLINK=${OPENUNLINK:-openunlink}
20 READS=${READS:-"reads"}
21 MUNLINK=${MUNLINK:-munlink}
22 SOCKETSERVER=${SOCKETSERVER:-socketserver}
23 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
24 MEMHOG=${MEMHOG:-memhog}
25 DIRECTIO=${DIRECTIO:-directio}
26 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
27 DEF_STRIPE_COUNT=-1
28 CHECK_GRANT=${CHECK_GRANT:-"yes"}
29 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
30 export PARALLEL=${PARALLEL:-"no"}
31
32 TRACE=${TRACE:-""}
33 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
34 LUSTRE=${LUSTRE:-$(dirname $0)/..}
35 . $LUSTRE/tests/test-framework.sh
36 init_test_env $@
37
38 init_logging
39
40 ALWAYS_EXCEPT="$SANITY_EXCEPT "
41 # bug number for skipped test: LU-9693 LU-6493 LU-9693 LU-11058
42 ALWAYS_EXCEPT+="                42a     42b     42c     77k"
43 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
44
45 # skipped tests: LU-8411 LU-9096 LU-9054 ..
46 ALWAYS_EXCEPT="  407     253     312     $ALWAYS_EXCEPT"
47
48 if $SHARED_KEY; then
49 # bug number for skipped tests: LU-9795 (all below)
50         ALWAYS_EXCEPT="$ALWAYS_EXCEPT   17n     60a     133g    300f"
51 fi
52
53 if [[ $(uname -m) = aarch64 ]]; then
54         # bug number:    LU-11596 (all below)
55         ALWAYS_EXCEPT+=" 42d 42e 63a 63b 64a 64b 64c"
56         # bug number:    LU-11671 LU-11665 LU-11594 LU-11667 LU-11729
57         ALWAYS_EXCEPT+=" 45       101c     103a     317      810"
58 fi
59
60 # skip nfs tests on kernels >= 4.12.0 until they are fixed
61 if [ $LINUX_VERSION_CODE -ge $(version_code 4.12.0) ]; then
62         # bug number:   LU-12661
63         ALWAYS_EXCEPT+=" 817"
64 fi
65
66 # skip cgroup tests on RHEL8.1 kernels until they are fixed
67 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
68       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
69         # bug number:   LU-13063
70         ALWAYS_EXCEPT+=" 411"
71 fi
72
73 #                                  5          12          (min)"
74 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
75
76 if [ "$mds1_FSTYPE" = "zfs" ]; then
77         # bug number for skipped test:
78         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
79         #                                               13    (min)"
80         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
81 fi
82
83 # Get the SLES distro version
84 #
85 # Returns a version string that should only be used in comparing
86 # strings returned by version_code()
87 sles_version_code()
88 {
89         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
90
91         # All SuSE Linux versions have one decimal. version_code expects two
92         local sles_version=$version.0
93         version_code $sles_version
94 }
95
96 # Check if we are running on Ubuntu or SLES so we can make decisions on
97 # what tests to run
98 if [ -r /etc/SuSE-release ]; then
99         sles_version=$(sles_version_code)
100         [ $sles_version -lt $(version_code 11.4.0) ] &&
101                 # bug number for skipped test: LU-4341
102                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
103         [ $sles_version -lt $(version_code 12.0.0) ] &&
104                 # bug number for skipped test: LU-3703
105                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
106 elif [ -r /etc/os-release ]; then
107         if grep -qi ubuntu /etc/os-release; then
108                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
109                                                 -e 's/^VERSION=//p' \
110                                                 /etc/os-release |
111                                                 awk '{ print $1 }'))
112
113                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
114                         # bug number for skipped test:
115                         #                LU-10334 LU-10366
116                         ALWAYS_EXCEPT+=" 103a     410"
117                 fi
118         fi
119 fi
120
121 build_test_filter
122 FAIL_ON_ERROR=false
123
124 cleanup() {
125         echo -n "cln.."
126         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
127         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
128 }
129 setup() {
130         echo -n "mnt.."
131         load_modules
132         setupall || exit 10
133         echo "done"
134 }
135
136 check_swap_layouts_support()
137 {
138         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
139                 skip "Does not support layout lock."
140 }
141
142 check_and_setup_lustre
143 DIR=${DIR:-$MOUNT}
144 assert_DIR
145
146 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
147
148 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
149 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
150 rm -rf $DIR/[Rdfs][0-9]*
151
152 # $RUNAS_ID may get set incorrectly somewhere else
153 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
154         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
155
156 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
157
158 if [ "${ONLY}" = "MOUNT" ] ; then
159         echo "Lustre is up, please go on"
160         exit
161 fi
162
163 echo "preparing for tests involving mounts"
164 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
165 touch $EXT2_DEV
166 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
167 echo # add a newline after mke2fs.
168
169 umask 077
170
171 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
172 lctl set_param debug=-1 2> /dev/null || true
173 test_0a() {
174         touch $DIR/$tfile
175         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
176         rm $DIR/$tfile
177         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
178 }
179 run_test 0a "touch; rm ====================="
180
181 test_0b() {
182         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
183         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
184 }
185 run_test 0b "chmod 0755 $DIR ============================="
186
187 test_0c() {
188         $LCTL get_param mdc.*.import | grep "state: FULL" ||
189                 error "import not FULL"
190         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
191                 error "bad target"
192 }
193 run_test 0c "check import proc"
194
195 test_0d() { # LU-3397
196         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
197                 skip "proc exports not supported before 2.10.57"
198
199         local mgs_exp="mgs.MGS.exports"
200         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
201         local exp_client_nid
202         local exp_client_version
203         local exp_val
204         local imp_val
205         local temp_imp=$DIR/$tfile.import
206         local temp_exp=$DIR/$tfile.export
207
208         # save mgc import file to $temp_imp
209         $LCTL get_param mgc.*.import | tee $temp_imp
210         # Check if client uuid is found in MGS export
211         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
212                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
213                         $client_uuid ] &&
214                         break;
215         done
216         # save mgs export file to $temp_exp
217         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
218
219         # Compare the value of field "connect_flags"
220         imp_val=$(grep "connect_flags" $temp_imp)
221         exp_val=$(grep "connect_flags" $temp_exp)
222         [ "$exp_val" == "$imp_val" ] ||
223                 error "export flags '$exp_val' != import flags '$imp_val'"
224
225         # Compare the value of client version
226         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
227         exp_val=$(version_code $exp_client_version)
228         imp_val=$CLIENT_VERSION
229         [ "$exp_val" == "$imp_val" ] ||
230                 error "export client version '$exp_val' != '$imp_val'"
231 }
232 run_test 0d "check export proc ============================="
233
234 test_1() {
235         test_mkdir $DIR/$tdir
236         test_mkdir $DIR/$tdir/d2
237         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
238         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
239         rmdir $DIR/$tdir/d2
240         rmdir $DIR/$tdir
241         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
242 }
243 run_test 1 "mkdir; remkdir; rmdir"
244
245 test_2() {
246         test_mkdir $DIR/$tdir
247         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
248         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
249         rm -r $DIR/$tdir
250         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
251 }
252 run_test 2 "mkdir; touch; rmdir; check file"
253
254 test_3() {
255         test_mkdir $DIR/$tdir
256         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
257         touch $DIR/$tdir/$tfile
258         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
259         rm -r $DIR/$tdir
260         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
261 }
262 run_test 3 "mkdir; touch; rmdir; check dir"
263
264 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
265 test_4() {
266         test_mkdir -i 1 $DIR/$tdir
267
268         touch $DIR/$tdir/$tfile ||
269                 error "Create file under remote directory failed"
270
271         rmdir $DIR/$tdir &&
272                 error "Expect error removing in-use dir $DIR/$tdir"
273
274         test -d $DIR/$tdir || error "Remote directory disappeared"
275
276         rm -rf $DIR/$tdir || error "remove remote dir error"
277 }
278 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
279
280 test_5() {
281         test_mkdir $DIR/$tdir
282         test_mkdir $DIR/$tdir/d2
283         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
284         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
285         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
286 }
287 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
288
289 test_6a() {
290         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
291         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
292         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
293                 error "$tfile does not have perm 0666 or UID $UID"
294         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
295         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
296                 error "$tfile should be 0666 and owned by UID $UID"
297 }
298 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
299
300 test_6c() {
301         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
302
303         touch $DIR/$tfile
304         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
305         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
306                 error "$tfile should be owned by UID $RUNAS_ID"
307         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
308         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
309                 error "$tfile should be owned by UID $RUNAS_ID"
310 }
311 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
312
313 test_6e() {
314         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
315
316         touch $DIR/$tfile
317         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
318         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
319                 error "$tfile should be owned by GID $UID"
320         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
321         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
322                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
323 }
324 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
325
326 test_6g() {
327         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
328
329         test_mkdir $DIR/$tdir
330         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
331         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
332         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
333         test_mkdir $DIR/$tdir/d/subdir
334         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
335                 error "$tdir/d/subdir should be GID $RUNAS_GID"
336         if [[ $MDSCOUNT -gt 1 ]]; then
337                 # check remote dir sgid inherite
338                 $LFS mkdir -i 0 $DIR/$tdir.local ||
339                         error "mkdir $tdir.local failed"
340                 chmod g+s $DIR/$tdir.local ||
341                         error "chmod $tdir.local failed"
342                 chgrp $RUNAS_GID $DIR/$tdir.local ||
343                         error "chgrp $tdir.local failed"
344                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
345                         error "mkdir $tdir.remote failed"
346                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
347                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
348                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
349                         error "$tdir.remote should be mode 02755"
350         fi
351 }
352 run_test 6g "verify new dir in sgid dir inherits group"
353
354 test_6h() { # bug 7331
355         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
356
357         touch $DIR/$tfile || error "touch failed"
358         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
359         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
360                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
361         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
362                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
363 }
364 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
365
366 test_7a() {
367         test_mkdir $DIR/$tdir
368         $MCREATE $DIR/$tdir/$tfile
369         chmod 0666 $DIR/$tdir/$tfile
370         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
371                 error "$tdir/$tfile should be mode 0666"
372 }
373 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
374
375 test_7b() {
376         if [ ! -d $DIR/$tdir ]; then
377                 test_mkdir $DIR/$tdir
378         fi
379         $MCREATE $DIR/$tdir/$tfile
380         echo -n foo > $DIR/$tdir/$tfile
381         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
382         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
383 }
384 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
385
386 test_8() {
387         test_mkdir $DIR/$tdir
388         touch $DIR/$tdir/$tfile
389         chmod 0666 $DIR/$tdir/$tfile
390         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
391                 error "$tfile mode not 0666"
392 }
393 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
394
395 test_9() {
396         test_mkdir $DIR/$tdir
397         test_mkdir $DIR/$tdir/d2
398         test_mkdir $DIR/$tdir/d2/d3
399         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
400 }
401 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
402
403 test_10() {
404         test_mkdir $DIR/$tdir
405         test_mkdir $DIR/$tdir/d2
406         touch $DIR/$tdir/d2/$tfile
407         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
408                 error "$tdir/d2/$tfile not a file"
409 }
410 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
411
412 test_11() {
413         test_mkdir $DIR/$tdir
414         test_mkdir $DIR/$tdir/d2
415         chmod 0666 $DIR/$tdir/d2
416         chmod 0705 $DIR/$tdir/d2
417         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
418                 error "$tdir/d2 mode not 0705"
419 }
420 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
421
422 test_12() {
423         test_mkdir $DIR/$tdir
424         touch $DIR/$tdir/$tfile
425         chmod 0666 $DIR/$tdir/$tfile
426         chmod 0654 $DIR/$tdir/$tfile
427         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
428                 error "$tdir/d2 mode not 0654"
429 }
430 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
431
432 test_13() {
433         test_mkdir $DIR/$tdir
434         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
435         >  $DIR/$tdir/$tfile
436         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
437                 error "$tdir/$tfile size not 0 after truncate"
438 }
439 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
440
441 test_14() {
442         test_mkdir $DIR/$tdir
443         touch $DIR/$tdir/$tfile
444         rm $DIR/$tdir/$tfile
445         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
446 }
447 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
448
449 test_15() {
450         test_mkdir $DIR/$tdir
451         touch $DIR/$tdir/$tfile
452         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
453         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
454                 error "$tdir/${tfile_2} not a file after rename"
455         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
456 }
457 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
458
459 test_16() {
460         test_mkdir $DIR/$tdir
461         touch $DIR/$tdir/$tfile
462         rm -rf $DIR/$tdir/$tfile
463         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
464 }
465 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
466
467 test_17a() {
468         test_mkdir $DIR/$tdir
469         touch $DIR/$tdir/$tfile
470         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
471         ls -l $DIR/$tdir
472         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
473                 error "$tdir/l-exist not a symlink"
474         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
475                 error "$tdir/l-exist not referencing a file"
476         rm -f $DIR/$tdir/l-exist
477         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
478 }
479 run_test 17a "symlinks: create, remove (real)"
480
481 test_17b() {
482         test_mkdir $DIR/$tdir
483         ln -s no-such-file $DIR/$tdir/l-dangle
484         ls -l $DIR/$tdir
485         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
486                 error "$tdir/l-dangle not referencing no-such-file"
487         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
488                 error "$tdir/l-dangle not referencing non-existent file"
489         rm -f $DIR/$tdir/l-dangle
490         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
491 }
492 run_test 17b "symlinks: create, remove (dangling)"
493
494 test_17c() { # bug 3440 - don't save failed open RPC for replay
495         test_mkdir $DIR/$tdir
496         ln -s foo $DIR/$tdir/$tfile
497         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
498 }
499 run_test 17c "symlinks: open dangling (should return error)"
500
501 test_17d() {
502         test_mkdir $DIR/$tdir
503         ln -s foo $DIR/$tdir/$tfile
504         touch $DIR/$tdir/$tfile || error "creating to new symlink"
505 }
506 run_test 17d "symlinks: create dangling"
507
508 test_17e() {
509         test_mkdir $DIR/$tdir
510         local foo=$DIR/$tdir/$tfile
511         ln -s $foo $foo || error "create symlink failed"
512         ls -l $foo || error "ls -l failed"
513         ls $foo && error "ls not failed" || true
514 }
515 run_test 17e "symlinks: create recursive symlink (should return error)"
516
517 test_17f() {
518         test_mkdir $DIR/$tdir
519         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
520         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
521         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
522         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
523         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
524         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890/aaaaaaaaaa/bbbbbbbbbb/cccccccccc/dddddddddd/eeeeeeeeee/ffffffffff/ $DIR/$tdir/666
525         ls -l  $DIR/$tdir
526 }
527 run_test 17f "symlinks: long and very long symlink name"
528
529 # str_repeat(S, N) generate a string that is string S repeated N times
530 str_repeat() {
531         local s=$1
532         local n=$2
533         local ret=''
534         while [ $((n -= 1)) -ge 0 ]; do
535                 ret=$ret$s
536         done
537         echo $ret
538 }
539
540 # Long symlinks and LU-2241
541 test_17g() {
542         test_mkdir $DIR/$tdir
543         local TESTS="59 60 61 4094 4095"
544
545         # Fix for inode size boundary in 2.1.4
546         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
547                 TESTS="4094 4095"
548
549         # Patch not applied to 2.2 or 2.3 branches
550         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
551         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
552                 TESTS="4094 4095"
553
554         # skip long symlink name for rhel6.5.
555         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
556         grep -q '6.5' /etc/redhat-release &>/dev/null &&
557                 TESTS="59 60 61 4062 4063"
558
559         for i in $TESTS; do
560                 local SYMNAME=$(str_repeat 'x' $i)
561                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
562                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
563         done
564 }
565 run_test 17g "symlinks: really long symlink name and inode boundaries"
566
567 test_17h() { #bug 17378
568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
569         remote_mds_nodsh && skip "remote MDS with nodsh"
570
571         local mdt_idx
572
573         test_mkdir $DIR/$tdir
574         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
575         $LFS setstripe -c -1 $DIR/$tdir
576         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
577         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
578         touch $DIR/$tdir/$tfile || true
579 }
580 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
581
582 test_17i() { #bug 20018
583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
584         remote_mds_nodsh && skip "remote MDS with nodsh"
585
586         local foo=$DIR/$tdir/$tfile
587         local mdt_idx
588
589         test_mkdir -c1 $DIR/$tdir
590         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
591         ln -s $foo $foo || error "create symlink failed"
592 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
593         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
594         ls -l $foo && error "error not detected"
595         return 0
596 }
597 run_test 17i "don't panic on short symlink (should return error)"
598
599 test_17k() { #bug 22301
600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
601         [[ -z "$(which rsync 2>/dev/null)" ]] &&
602                 skip "no rsync command"
603         rsync --help | grep -q xattr ||
604                 skip_env "$(rsync --version | head -n1) does not support xattrs"
605         test_mkdir $DIR/$tdir
606         test_mkdir $DIR/$tdir.new
607         touch $DIR/$tdir/$tfile
608         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
609         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
610                 error "rsync failed with xattrs enabled"
611 }
612 run_test 17k "symlinks: rsync with xattrs enabled"
613
614 test_17l() { # LU-279
615         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
616                 skip "no getfattr command"
617
618         test_mkdir $DIR/$tdir
619         touch $DIR/$tdir/$tfile
620         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
621         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
622                 # -h to not follow symlinks. -m '' to list all the xattrs.
623                 # grep to remove first line: '# file: $path'.
624                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
625                 do
626                         lgetxattr_size_check $path $xattr ||
627                                 error "lgetxattr_size_check $path $xattr failed"
628                 done
629         done
630 }
631 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
632
633 # LU-1540
634 test_17m() {
635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
636         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
637         remote_mds_nodsh && skip "remote MDS with nodsh"
638         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
639         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
640                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
641
642         local short_sym="0123456789"
643         local wdir=$DIR/$tdir
644         local i
645
646         test_mkdir $wdir
647         long_sym=$short_sym
648         # create a long symlink file
649         for ((i = 0; i < 4; ++i)); do
650                 long_sym=${long_sym}${long_sym}
651         done
652
653         echo "create 512 short and long symlink files under $wdir"
654         for ((i = 0; i < 256; ++i)); do
655                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
656                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
657         done
658
659         echo "erase them"
660         rm -f $wdir/*
661         sync
662         wait_delete_completed
663
664         echo "recreate the 512 symlink files with a shorter string"
665         for ((i = 0; i < 512; ++i)); do
666                 # rewrite the symlink file with a shorter string
667                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
668                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
669         done
670
671         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
672         local devname=$(mdsdevname $mds_index)
673
674         echo "stop and checking mds${mds_index}:"
675         # e2fsck should not return error
676         stop mds${mds_index}
677         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
678         rc=$?
679
680         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
681                 error "start mds${mds_index} failed"
682         df $MOUNT > /dev/null 2>&1
683         [ $rc -eq 0 ] ||
684                 error "e2fsck detected error for short/long symlink: rc=$rc"
685         rm -f $wdir/*
686 }
687 run_test 17m "run e2fsck against MDT which contains short/long symlink"
688
689 check_fs_consistency_17n() {
690         local mdt_index
691         local rc=0
692
693         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
694         # so it only check MDT1/MDT2 instead of all of MDTs.
695         for mdt_index in 1 2; do
696                 local devname=$(mdsdevname $mdt_index)
697                 # e2fsck should not return error
698                 stop mds${mdt_index}
699                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
700                         rc=$((rc + $?))
701
702                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
703                         error "mount mds$mdt_index failed"
704                 df $MOUNT > /dev/null 2>&1
705         done
706         return $rc
707 }
708
709 test_17n() {
710         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
712         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
713         remote_mds_nodsh && skip "remote MDS with nodsh"
714         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
715         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
716                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
717
718         local i
719
720         test_mkdir $DIR/$tdir
721         for ((i=0; i<10; i++)); do
722                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
723                         error "create remote dir error $i"
724                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
725                         error "create files under remote dir failed $i"
726         done
727
728         check_fs_consistency_17n ||
729                 error "e2fsck report error after create files under remote dir"
730
731         for ((i = 0; i < 10; i++)); do
732                 rm -rf $DIR/$tdir/remote_dir_${i} ||
733                         error "destroy remote dir error $i"
734         done
735
736         check_fs_consistency_17n ||
737                 error "e2fsck report error after unlink files under remote dir"
738
739         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
740                 skip "lustre < 2.4.50 does not support migrate mv"
741
742         for ((i = 0; i < 10; i++)); do
743                 mkdir -p $DIR/$tdir/remote_dir_${i}
744                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
745                         error "create files under remote dir failed $i"
746                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
747                         error "migrate remote dir error $i"
748         done
749         check_fs_consistency_17n || error "e2fsck report error after migration"
750
751         for ((i = 0; i < 10; i++)); do
752                 rm -rf $DIR/$tdir/remote_dir_${i} ||
753                         error "destroy remote dir error $i"
754         done
755
756         check_fs_consistency_17n || error "e2fsck report error after unlink"
757 }
758 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
759
760 test_17o() {
761         remote_mds_nodsh && skip "remote MDS with nodsh"
762         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
763                 skip "Need MDS version at least 2.3.64"
764
765         local wdir=$DIR/${tdir}o
766         local mdt_index
767         local rc=0
768
769         test_mkdir $wdir
770         touch $wdir/$tfile
771         mdt_index=$($LFS getstripe -m $wdir/$tfile)
772         mdt_index=$((mdt_index + 1))
773
774         cancel_lru_locks mdc
775         #fail mds will wait the failover finish then set
776         #following fail_loc to avoid interfer the recovery process.
777         fail mds${mdt_index}
778
779         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
780         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
781         ls -l $wdir/$tfile && rc=1
782         do_facet mds${mdt_index} lctl set_param fail_loc=0
783         [[ $rc -eq 0 ]] || error "stat file should fail"
784 }
785 run_test 17o "stat file with incompat LMA feature"
786
787 test_18() {
788         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
789         ls $DIR || error "Failed to ls $DIR: $?"
790 }
791 run_test 18 "touch .../f ; ls ... =============================="
792
793 test_19a() {
794         touch $DIR/$tfile
795         ls -l $DIR
796         rm $DIR/$tfile
797         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
798 }
799 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
800
801 test_19b() {
802         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
803 }
804 run_test 19b "ls -l .../f19 (should return error) =============="
805
806 test_19c() {
807         [ $RUNAS_ID -eq $UID ] &&
808                 skip_env "RUNAS_ID = UID = $UID -- skipping"
809
810         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
811 }
812 run_test 19c "$RUNAS touch .../f19 (should return error) =="
813
814 test_19d() {
815         cat $DIR/f19 && error || true
816 }
817 run_test 19d "cat .../f19 (should return error) =============="
818
819 test_20() {
820         touch $DIR/$tfile
821         rm $DIR/$tfile
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
827 }
828 run_test 20 "touch .../f ; ls -l ..."
829
830 test_21() {
831         test_mkdir $DIR/$tdir
832         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
833         ln -s dangle $DIR/$tdir/link
834         echo foo >> $DIR/$tdir/link
835         cat $DIR/$tdir/dangle
836         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
837         $CHECKSTAT -f -t file $DIR/$tdir/link ||
838                 error "$tdir/link not linked to a file"
839 }
840 run_test 21 "write to dangling link"
841
842 test_22() {
843         local wdir=$DIR/$tdir
844         test_mkdir $wdir
845         chown $RUNAS_ID:$RUNAS_GID $wdir
846         (cd $wdir || error "cd $wdir failed";
847                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
848                 $RUNAS tar xf -)
849         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
850         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
851         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
852                 error "checkstat -u failed"
853 }
854 run_test 22 "unpack tar archive as non-root user"
855
856 # was test_23
857 test_23a() {
858         test_mkdir $DIR/$tdir
859         local file=$DIR/$tdir/$tfile
860
861         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
862         openfile -f O_CREAT:O_EXCL $file &&
863                 error "$file recreate succeeded" || true
864 }
865 run_test 23a "O_CREAT|O_EXCL in subdir"
866
867 test_23b() { # bug 18988
868         test_mkdir $DIR/$tdir
869         local file=$DIR/$tdir/$tfile
870
871         rm -f $file
872         echo foo > $file || error "write filed"
873         echo bar >> $file || error "append filed"
874         $CHECKSTAT -s 8 $file || error "wrong size"
875         rm $file
876 }
877 run_test 23b "O_APPEND check"
878
879 # LU-9409, size with O_APPEND and tiny writes
880 test_23c() {
881         local file=$DIR/$tfile
882
883         # single dd
884         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
885         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
886         rm -f $file
887
888         # racing tiny writes
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
891         wait
892         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
893         rm -f $file
894
895         #racing tiny & normal writes
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
898         wait
899         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
900         rm -f $file
901
902         #racing tiny & normal writes 2, ugly numbers
903         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
904         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
905         wait
906         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
907         rm -f $file
908 }
909 run_test 23c "O_APPEND size checks for tiny writes"
910
911 # LU-11069 file offset is correct after appending writes
912 test_23d() {
913         local file=$DIR/$tfile
914         local offset
915
916         echo CentaurHauls > $file
917         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
918         if ((offset != 26)); then
919                 error "wrong offset, expected 26, got '$offset'"
920         fi
921 }
922 run_test 23d "file offset is correct after appending writes"
923
924 # rename sanity
925 test_24a() {
926         echo '-- same directory rename'
927         test_mkdir $DIR/$tdir
928         touch $DIR/$tdir/$tfile.1
929         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
930         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
931 }
932 run_test 24a "rename file to non-existent target"
933
934 test_24b() {
935         test_mkdir $DIR/$tdir
936         touch $DIR/$tdir/$tfile.{1,2}
937         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
938         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
939         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
940 }
941 run_test 24b "rename file to existing target"
942
943 test_24c() {
944         test_mkdir $DIR/$tdir
945         test_mkdir $DIR/$tdir/d$testnum.1
946         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
947         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
948         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
949 }
950 run_test 24c "rename directory to non-existent target"
951
952 test_24d() {
953         test_mkdir -c1 $DIR/$tdir
954         test_mkdir -c1 $DIR/$tdir/d$testnum.1
955         test_mkdir -c1 $DIR/$tdir/d$testnum.2
956         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
957         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
958         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
959 }
960 run_test 24d "rename directory to existing target"
961
962 test_24e() {
963         echo '-- cross directory renames --'
964         test_mkdir $DIR/R5a
965         test_mkdir $DIR/R5b
966         touch $DIR/R5a/f
967         mv $DIR/R5a/f $DIR/R5b/g
968         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
969         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
970 }
971 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
972
973 test_24f() {
974         test_mkdir $DIR/R6a
975         test_mkdir $DIR/R6b
976         touch $DIR/R6a/f $DIR/R6b/g
977         mv $DIR/R6a/f $DIR/R6b/g
978         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
979         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
980 }
981 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
982
983 test_24g() {
984         test_mkdir $DIR/R7a
985         test_mkdir $DIR/R7b
986         test_mkdir $DIR/R7a/d
987         mv $DIR/R7a/d $DIR/R7b/e
988         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
989         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
990 }
991 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
992
993 test_24h() {
994         test_mkdir -c1 $DIR/R8a
995         test_mkdir -c1 $DIR/R8b
996         test_mkdir -c1 $DIR/R8a/d
997         test_mkdir -c1 $DIR/R8b/e
998         mrename $DIR/R8a/d $DIR/R8b/e
999         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1000         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1001 }
1002 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1003
1004 test_24i() {
1005         echo "-- rename error cases"
1006         test_mkdir $DIR/R9
1007         test_mkdir $DIR/R9/a
1008         touch $DIR/R9/f
1009         mrename $DIR/R9/f $DIR/R9/a
1010         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1011         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1012         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1013 }
1014 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1015
1016 test_24j() {
1017         test_mkdir $DIR/R10
1018         mrename $DIR/R10/f $DIR/R10/g
1019         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1020         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1021         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1022 }
1023 run_test 24j "source does not exist ============================"
1024
1025 test_24k() {
1026         test_mkdir $DIR/R11a
1027         test_mkdir $DIR/R11a/d
1028         touch $DIR/R11a/f
1029         mv $DIR/R11a/f $DIR/R11a/d
1030         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1031         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1032 }
1033 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1034
1035 # bug 2429 - rename foo foo foo creates invalid file
1036 test_24l() {
1037         f="$DIR/f24l"
1038         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1039 }
1040 run_test 24l "Renaming a file to itself ========================"
1041
1042 test_24m() {
1043         f="$DIR/f24m"
1044         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1045         # on ext3 this does not remove either the source or target files
1046         # though the "expected" operation would be to remove the source
1047         $CHECKSTAT -t file ${f} || error "${f} missing"
1048         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1049 }
1050 run_test 24m "Renaming a file to a hard link to itself ========="
1051
1052 test_24n() {
1053     f="$DIR/f24n"
1054     # this stats the old file after it was renamed, so it should fail
1055     touch ${f}
1056     $CHECKSTAT ${f} || error "${f} missing"
1057     mv ${f} ${f}.rename
1058     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1059     $CHECKSTAT -a ${f} || error "${f} exists"
1060 }
1061 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1062
1063 test_24o() {
1064         test_mkdir $DIR/$tdir
1065         rename_many -s random -v -n 10 $DIR/$tdir
1066 }
1067 run_test 24o "rename of files during htree split"
1068
1069 test_24p() {
1070         test_mkdir $DIR/R12a
1071         test_mkdir $DIR/R12b
1072         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1073         mrename $DIR/R12a $DIR/R12b
1074         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1075         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1076         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1077         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1078 }
1079 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1080
1081 cleanup_multiop_pause() {
1082         trap 0
1083         kill -USR1 $MULTIPID
1084 }
1085
1086 test_24q() {
1087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1088
1089         test_mkdir $DIR/R13a
1090         test_mkdir $DIR/R13b
1091         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1092         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1093         MULTIPID=$!
1094
1095         trap cleanup_multiop_pause EXIT
1096         mrename $DIR/R13a $DIR/R13b
1097         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1098         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1099         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1100         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1101         cleanup_multiop_pause
1102         wait $MULTIPID || error "multiop close failed"
1103 }
1104 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1105
1106 test_24r() { #bug 3789
1107         test_mkdir $DIR/R14a
1108         test_mkdir $DIR/R14a/b
1109         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1110         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1111         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1112 }
1113 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1114
1115 test_24s() {
1116         test_mkdir $DIR/R15a
1117         test_mkdir $DIR/R15a/b
1118         test_mkdir $DIR/R15a/b/c
1119         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1120         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1121         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1122 }
1123 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1124 test_24t() {
1125         test_mkdir $DIR/R16a
1126         test_mkdir $DIR/R16a/b
1127         test_mkdir $DIR/R16a/b/c
1128         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1129         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1130         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1131 }
1132 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1133
1134 test_24u() { # bug12192
1135         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1136         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1137 }
1138 run_test 24u "create stripe file"
1139
1140 simple_cleanup_common() {
1141         local rc=0
1142         trap 0
1143         [ -z "$DIR" -o -z "$tdir" ] && return 0
1144
1145         local start=$SECONDS
1146         rm -rf $DIR/$tdir
1147         rc=$?
1148         wait_delete_completed
1149         echo "cleanup time $((SECONDS - start))"
1150         return $rc
1151 }
1152
1153 max_pages_per_rpc() {
1154         local mdtname="$(printf "MDT%04x" ${1:-0})"
1155         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1156 }
1157
1158 test_24v() {
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1160
1161         local nrfiles=${COUNT:-100000}
1162         local fname="$DIR/$tdir/$tfile"
1163
1164         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1165         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1166
1167         test_mkdir "$(dirname $fname)"
1168         # assume MDT0000 has the fewest inodes
1169         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1170         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1171         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1172
1173         trap simple_cleanup_common EXIT
1174
1175         createmany -m "$fname" $nrfiles
1176
1177         cancel_lru_locks mdc
1178         lctl set_param mdc.*.stats clear
1179
1180         # was previously test_24D: LU-6101
1181         # readdir() returns correct number of entries after cursor reload
1182         local num_ls=$(ls $DIR/$tdir | wc -l)
1183         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1184         local num_all=$(ls -a $DIR/$tdir | wc -l)
1185         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1186              $num_all -ne $((nrfiles + 2)) ]; then
1187                 error "Expected $nrfiles files, got $num_ls " \
1188                         "($num_uniq unique $num_all .&..)"
1189         fi
1190         # LU-5 large readdir
1191         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1192         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1193         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1194         # take into account of overhead in lu_dirpage header and end mark in
1195         # each page, plus one in rpc_num calculation.
1196         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1197         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1198         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1199         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1200         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1201         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1202         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1203         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1204                 error "large readdir doesn't take effect: " \
1205                       "$mds_readpage should be about $rpc_max"
1206
1207         simple_cleanup_common
1208 }
1209 run_test 24v "list large directory (test hash collision, b=17560)"
1210
1211 test_24w() { # bug21506
1212         SZ1=234852
1213         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1214         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1215         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1216         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1217         [[ "$SZ1" -eq "$SZ2" ]] ||
1218                 error "Error reading at the end of the file $tfile"
1219 }
1220 run_test 24w "Reading a file larger than 4Gb"
1221
1222 test_24x() {
1223         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1225         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1226                 skip "Need MDS version at least 2.7.56"
1227
1228         local MDTIDX=1
1229         local remote_dir=$DIR/$tdir/remote_dir
1230
1231         test_mkdir $DIR/$tdir
1232         $LFS mkdir -i $MDTIDX $remote_dir ||
1233                 error "create remote directory failed"
1234
1235         test_mkdir $DIR/$tdir/src_dir
1236         touch $DIR/$tdir/src_file
1237         test_mkdir $remote_dir/tgt_dir
1238         touch $remote_dir/tgt_file
1239
1240         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1241                 error "rename dir cross MDT failed!"
1242
1243         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1244                 error "rename file cross MDT failed!"
1245
1246         touch $DIR/$tdir/ln_file
1247         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1248                 error "ln file cross MDT failed"
1249
1250         rm -rf $DIR/$tdir || error "Can not delete directories"
1251 }
1252 run_test 24x "cross MDT rename/link"
1253
1254 test_24y() {
1255         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1257
1258         local remote_dir=$DIR/$tdir/remote_dir
1259         local mdtidx=1
1260
1261         test_mkdir $DIR/$tdir
1262         $LFS mkdir -i $mdtidx $remote_dir ||
1263                 error "create remote directory failed"
1264
1265         test_mkdir $remote_dir/src_dir
1266         touch $remote_dir/src_file
1267         test_mkdir $remote_dir/tgt_dir
1268         touch $remote_dir/tgt_file
1269
1270         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1271                 error "rename subdir in the same remote dir failed!"
1272
1273         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1274                 error "rename files in the same remote dir failed!"
1275
1276         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1277                 error "link files in the same remote dir failed!"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24y "rename/link on the same dir should succeed"
1282
1283 test_24z() {
1284         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1285         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1286                 skip "Need MDS version at least 2.12.51"
1287
1288         local index
1289
1290         for index in 0 1; do
1291                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1292                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1293         done
1294
1295         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1296
1297         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1298         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1299
1300         local mdts=$(comma_list $(mdts_nodes))
1301
1302         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1303         stack_trap "do_nodes $mdts $LCTL \
1304                 set_param mdt.*.enable_remote_rename=1" EXIT
1305
1306         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1307
1308         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1309         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1310 }
1311 run_test 24z "cross-MDT rename is done as cp"
1312
1313 test_24A() { # LU-3182
1314         local NFILES=5000
1315
1316         rm -rf $DIR/$tdir
1317         test_mkdir $DIR/$tdir
1318         trap simple_cleanup_common EXIT
1319         createmany -m $DIR/$tdir/$tfile $NFILES
1320         local t=$(ls $DIR/$tdir | wc -l)
1321         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1322         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1323         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1324                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1325         fi
1326
1327         simple_cleanup_common || error "Can not delete directories"
1328 }
1329 run_test 24A "readdir() returns correct number of entries."
1330
1331 test_24B() { # LU-4805
1332         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1333
1334         local count
1335
1336         test_mkdir $DIR/$tdir
1337         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1338                 error "create striped dir failed"
1339
1340         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1341         [ $count -eq 2 ] || error "Expected 2, got $count"
1342
1343         touch $DIR/$tdir/striped_dir/a
1344
1345         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1346         [ $count -eq 3 ] || error "Expected 3, got $count"
1347
1348         touch $DIR/$tdir/striped_dir/.f
1349
1350         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1351         [ $count -eq 4 ] || error "Expected 4, got $count"
1352
1353         rm -rf $DIR/$tdir || error "Can not delete directories"
1354 }
1355 run_test 24B "readdir for striped dir return correct number of entries"
1356
1357 test_24C() {
1358         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1359
1360         mkdir $DIR/$tdir
1361         mkdir $DIR/$tdir/d0
1362         mkdir $DIR/$tdir/d1
1363
1364         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1365                 error "create striped dir failed"
1366
1367         cd $DIR/$tdir/d0/striped_dir
1368
1369         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1370         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1371         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1372
1373         [ "$d0_ino" = "$parent_ino" ] ||
1374                 error ".. wrong, expect $d0_ino, get $parent_ino"
1375
1376         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1377                 error "mv striped dir failed"
1378
1379         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1380
1381         [ "$d1_ino" = "$parent_ino" ] ||
1382                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1383 }
1384 run_test 24C "check .. in striped dir"
1385
1386 test_24E() {
1387         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1388         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1389
1390         mkdir -p $DIR/$tdir
1391         mkdir $DIR/$tdir/src_dir
1392         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1393                 error "create remote source failed"
1394
1395         touch $DIR/$tdir/src_dir/src_child/a
1396
1397         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1398                 error "create remote target dir failed"
1399
1400         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1401                 error "create remote target child failed"
1402
1403         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1404                 error "rename dir cross MDT failed!"
1405
1406         find $DIR/$tdir
1407
1408         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1409                 error "src_child still exists after rename"
1410
1411         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1412                 error "missing file(a) after rename"
1413
1414         rm -rf $DIR/$tdir || error "Can not delete directories"
1415 }
1416 run_test 24E "cross MDT rename/link"
1417
1418 test_24F () {
1419         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1420
1421         local repeats=1000
1422         [ "$SLOW" = "no" ] && repeats=100
1423
1424         mkdir -p $DIR/$tdir
1425
1426         echo "$repeats repeats"
1427         for ((i = 0; i < repeats; i++)); do
1428                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1429                 touch $DIR/$tdir/test/a || error "touch fails"
1430                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1431                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1432         done
1433
1434         true
1435 }
1436 run_test 24F "hash order vs readdir (LU-11330)"
1437
1438 test_25a() {
1439         echo '== symlink sanity ============================================='
1440
1441         test_mkdir $DIR/d25
1442         ln -s d25 $DIR/s25
1443         touch $DIR/s25/foo ||
1444                 error "File creation in symlinked directory failed"
1445 }
1446 run_test 25a "create file in symlinked directory ==============="
1447
1448 test_25b() {
1449         [ ! -d $DIR/d25 ] && test_25a
1450         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1451 }
1452 run_test 25b "lookup file in symlinked directory ==============="
1453
1454 test_26a() {
1455         test_mkdir $DIR/d26
1456         test_mkdir $DIR/d26/d26-2
1457         ln -s d26/d26-2 $DIR/s26
1458         touch $DIR/s26/foo || error "File creation failed"
1459 }
1460 run_test 26a "multiple component symlink ======================="
1461
1462 test_26b() {
1463         test_mkdir -p $DIR/$tdir/d26-2
1464         ln -s $tdir/d26-2/foo $DIR/s26-2
1465         touch $DIR/s26-2 || error "File creation failed"
1466 }
1467 run_test 26b "multiple component symlink at end of lookup ======"
1468
1469 test_26c() {
1470         test_mkdir $DIR/d26.2
1471         touch $DIR/d26.2/foo
1472         ln -s d26.2 $DIR/s26.2-1
1473         ln -s s26.2-1 $DIR/s26.2-2
1474         ln -s s26.2-2 $DIR/s26.2-3
1475         chmod 0666 $DIR/s26.2-3/foo
1476 }
1477 run_test 26c "chain of symlinks"
1478
1479 # recursive symlinks (bug 439)
1480 test_26d() {
1481         ln -s d26-3/foo $DIR/d26-3
1482 }
1483 run_test 26d "create multiple component recursive symlink"
1484
1485 test_26e() {
1486         [ ! -h $DIR/d26-3 ] && test_26d
1487         rm $DIR/d26-3
1488 }
1489 run_test 26e "unlink multiple component recursive symlink"
1490
1491 # recursive symlinks (bug 7022)
1492 test_26f() {
1493         test_mkdir $DIR/$tdir
1494         test_mkdir $DIR/$tdir/$tfile
1495         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1496         test_mkdir -p lndir/bar1
1497         test_mkdir $DIR/$tdir/$tfile/$tfile
1498         cd $tfile                || error "cd $tfile failed"
1499         ln -s .. dotdot          || error "ln dotdot failed"
1500         ln -s dotdot/lndir lndir || error "ln lndir failed"
1501         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1502         output=`ls $tfile/$tfile/lndir/bar1`
1503         [ "$output" = bar1 ] && error "unexpected output"
1504         rm -r $tfile             || error "rm $tfile failed"
1505         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1506 }
1507 run_test 26f "rm -r of a directory which has recursive symlink"
1508
1509 test_27a() {
1510         test_mkdir $DIR/$tdir
1511         $LFS getstripe $DIR/$tdir
1512         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1513         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1514         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1515 }
1516 run_test 27a "one stripe file"
1517
1518 test_27b() {
1519         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1520
1521         test_mkdir $DIR/$tdir
1522         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1523         $LFS getstripe -c $DIR/$tdir/$tfile
1524         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1525                 error "two-stripe file doesn't have two stripes"
1526
1527         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1528 }
1529 run_test 27b "create and write to two stripe file"
1530
1531 test_27d() {
1532         test_mkdir $DIR/$tdir
1533         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1534                 error "setstripe failed"
1535         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1536         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1537 }
1538 run_test 27d "create file with default settings"
1539
1540 test_27e() {
1541         # LU-5839 adds check for existed layout before setting it
1542         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1543                 skip "Need MDS version at least 2.7.56"
1544
1545         test_mkdir $DIR/$tdir
1546         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1547         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1548         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1549 }
1550 run_test 27e "setstripe existing file (should return error)"
1551
1552 test_27f() {
1553         test_mkdir $DIR/$tdir
1554         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1555                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1556         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1557                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1558         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1559         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1560 }
1561 run_test 27f "setstripe with bad stripe size (should return error)"
1562
1563 test_27g() {
1564         test_mkdir $DIR/$tdir
1565         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1566         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1567                 error "$DIR/$tdir/$tfile has object"
1568 }
1569 run_test 27g "$LFS getstripe with no objects"
1570
1571 test_27ga() {
1572         test_mkdir $DIR/$tdir
1573         touch $DIR/$tdir/$tfile || error "touch failed"
1574         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1575         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1576         local rc=$?
1577         (( rc == 2 )) || error "getstripe did not return ENOENT"
1578 }
1579 run_test 27ga "$LFS getstripe with missing file (should return error)"
1580
1581 test_27i() {
1582         test_mkdir $DIR/$tdir
1583         touch $DIR/$tdir/$tfile || error "touch failed"
1584         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1585                 error "missing objects"
1586 }
1587 run_test 27i "$LFS getstripe with some objects"
1588
1589 test_27j() {
1590         test_mkdir $DIR/$tdir
1591         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1592                 error "setstripe failed" || true
1593 }
1594 run_test 27j "setstripe with bad stripe offset (should return error)"
1595
1596 test_27k() { # bug 2844
1597         test_mkdir $DIR/$tdir
1598         local file=$DIR/$tdir/$tfile
1599         local ll_max_blksize=$((4 * 1024 * 1024))
1600         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1601         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1602         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1603         dd if=/dev/zero of=$file bs=4k count=1
1604         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1605         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1606 }
1607 run_test 27k "limit i_blksize for broken user apps"
1608
1609 test_27l() {
1610         mcreate $DIR/$tfile || error "creating file"
1611         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1612                 error "setstripe should have failed" || true
1613 }
1614 run_test 27l "check setstripe permissions (should return error)"
1615
1616 test_27m() {
1617         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1618
1619         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1620                    head -n1)
1621         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1622                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1623         fi
1624         trap simple_cleanup_common EXIT
1625         test_mkdir $DIR/$tdir
1626         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1627         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1628                 error "dd should fill OST0"
1629         i=2
1630         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1631                 i=$((i + 1))
1632                 [ $i -gt 256 ] && break
1633         done
1634         i=$((i + 1))
1635         touch $DIR/$tdir/$tfile.$i
1636         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1637             awk '{print $1}'| grep -w "0") ] &&
1638                 error "OST0 was full but new created file still use it"
1639         i=$((i + 1))
1640         touch $DIR/$tdir/$tfile.$i
1641         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1642             awk '{print $1}'| grep -w "0") ] &&
1643                 error "OST0 was full but new created file still use it"
1644         simple_cleanup_common
1645 }
1646 run_test 27m "create file while OST0 was full"
1647
1648 sleep_maxage() {
1649         local delay=$(do_facet $SINGLEMDS lctl get_param -n lo[vd].*.qos_maxage |
1650                       awk '{ print $1 * 2; exit; }')
1651         sleep $delay
1652 }
1653
1654 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1655 # if the OST isn't full anymore.
1656 reset_enospc() {
1657         local OSTIDX=${1:-""}
1658
1659         local list=$(comma_list $(osts_nodes))
1660         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1661
1662         do_nodes $list lctl set_param fail_loc=0
1663         sync    # initiate all OST_DESTROYs from MDS to OST
1664         sleep_maxage
1665 }
1666
1667 exhaust_precreations() {
1668         local OSTIDX=$1
1669         local FAILLOC=$2
1670         local FAILIDX=${3:-$OSTIDX}
1671         local ofacet=ost$((OSTIDX + 1))
1672
1673         test_mkdir -p -c1 $DIR/$tdir
1674         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1675         local mfacet=mds$((mdtidx + 1))
1676         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1677
1678         local OST=$(ostname_from_index $OSTIDX)
1679
1680         # on the mdt's osc
1681         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1682         local last_id=$(do_facet $mfacet lctl get_param -n \
1683                         osp.$mdtosc_proc1.prealloc_last_id)
1684         local next_id=$(do_facet $mfacet lctl get_param -n \
1685                         osp.$mdtosc_proc1.prealloc_next_id)
1686
1687         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1688         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1689
1690         test_mkdir -p $DIR/$tdir/${OST}
1691         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1692 #define OBD_FAIL_OST_ENOSPC              0x215
1693         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1694         echo "Creating to objid $last_id on ost $OST..."
1695         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1696         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1697         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1698         sleep_maxage
1699 }
1700
1701 exhaust_all_precreations() {
1702         local i
1703         for (( i=0; i < OSTCOUNT; i++ )) ; do
1704                 exhaust_precreations $i $1 -1
1705         done
1706 }
1707
1708 test_27n() {
1709         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1711         remote_mds_nodsh && skip "remote MDS with nodsh"
1712         remote_ost_nodsh && skip "remote OST with nodsh"
1713
1714         reset_enospc
1715         rm -f $DIR/$tdir/$tfile
1716         exhaust_precreations 0 0x80000215
1717         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1718         touch $DIR/$tdir/$tfile || error "touch failed"
1719         $LFS getstripe $DIR/$tdir/$tfile
1720         reset_enospc
1721 }
1722 run_test 27n "create file with some full OSTs"
1723
1724 test_27o() {
1725         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1726         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1727         remote_mds_nodsh && skip "remote MDS with nodsh"
1728         remote_ost_nodsh && skip "remote OST with nodsh"
1729
1730         reset_enospc
1731         rm -f $DIR/$tdir/$tfile
1732         exhaust_all_precreations 0x215
1733
1734         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1735
1736         reset_enospc
1737         rm -rf $DIR/$tdir/*
1738 }
1739 run_test 27o "create file with all full OSTs (should error)"
1740
1741 function create_and_checktime() {
1742         local fname=$1
1743         local loops=$2
1744         local i
1745
1746         for ((i=0; i < $loops; i++)); do
1747                 local start=$SECONDS
1748                 multiop $fname-$i Oc
1749                 ((SECONDS-start < TIMEOUT)) ||
1750                         error "creation took " $((SECONDS-$start)) && return 1
1751         done
1752 }
1753
1754 test_27oo() {
1755         local mdts=$(comma_list $(mdts_nodes))
1756
1757         [ $MDS1_VERSION -lt $(version_code 2.12.6) ] &&
1758                 skip "Need MDS version at least 2.12.6"
1759
1760         local f0=$DIR/${tfile}-0
1761         local f1=$DIR/${tfile}-1
1762
1763         wait_delete_completed
1764
1765         # refill precreated objects
1766         $LFS setstripe -i0 -c1 $f0
1767
1768         saved=$(do_facet mds1 $LCTL get_param -n lov.*0000*.qos_threshold_rr)
1769         # force QoS allocation policy
1770         do_nodes $mdts $LCTL set_param lov.*.qos_threshold_rr=0%
1771         stack_trap "do_nodes $mdts $LCTL set_param \
1772                 lov.*.qos_threshold_rr=$saved" EXIT
1773         sleep_maxage
1774
1775         # one OST is unavailable, but still have few objects preallocated
1776         stop ost1
1777         stack_trap "start ost1 $(ostdevname 1) $OST_MOUNT_OPTS; \
1778                 rm -rf $f1 $DIR/$tdir*" EXIT
1779
1780         for ((i=0; i < 7; i++)); do
1781                 mkdir $DIR/$tdir$i || error "can't create dir"
1782                 $LFS setstripe -c$((OSTCOUNT-1)) $DIR/$tdir$i ||
1783                         error "can't set striping"
1784         done
1785         for ((i=0; i < 7; i++)); do
1786                 create_and_checktime $DIR/$tdir$i/$tfile 100 &
1787         done
1788         wait
1789 }
1790 run_test 27oo "don't let few threads to reserve too many objects"
1791
1792 test_27p() {
1793         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1795         remote_mds_nodsh && skip "remote MDS with nodsh"
1796         remote_ost_nodsh && skip "remote OST with nodsh"
1797
1798         reset_enospc
1799         rm -f $DIR/$tdir/$tfile
1800         test_mkdir $DIR/$tdir
1801
1802         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1803         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1804         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1805
1806         exhaust_precreations 0 0x80000215
1807         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1808         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1809         $LFS getstripe $DIR/$tdir/$tfile
1810
1811         reset_enospc
1812 }
1813 run_test 27p "append to a truncated file with some full OSTs"
1814
1815 test_27q() {
1816         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1817         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1818         remote_mds_nodsh && skip "remote MDS with nodsh"
1819         remote_ost_nodsh && skip "remote OST with nodsh"
1820
1821         reset_enospc
1822         rm -f $DIR/$tdir/$tfile
1823
1824         test_mkdir $DIR/$tdir
1825         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1826         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1827                 error "truncate $DIR/$tdir/$tfile failed"
1828         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1829
1830         exhaust_all_precreations 0x215
1831
1832         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1833         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1834
1835         reset_enospc
1836 }
1837 run_test 27q "append to truncated file with all OSTs full (should error)"
1838
1839 test_27r() {
1840         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1842         remote_mds_nodsh && skip "remote MDS with nodsh"
1843         remote_ost_nodsh && skip "remote OST with nodsh"
1844
1845         reset_enospc
1846         rm -f $DIR/$tdir/$tfile
1847         exhaust_precreations 0 0x80000215
1848
1849         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1850
1851         reset_enospc
1852 }
1853 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1854
1855 test_27s() { # bug 10725
1856         test_mkdir $DIR/$tdir
1857         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1858         local stripe_count=0
1859         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1860         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1861                 error "stripe width >= 2^32 succeeded" || true
1862
1863 }
1864 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1865
1866 test_27t() { # bug 10864
1867         WDIR=$(pwd)
1868         WLFS=$(which lfs)
1869         cd $DIR
1870         touch $tfile
1871         $WLFS getstripe $tfile
1872         cd $WDIR
1873 }
1874 run_test 27t "check that utils parse path correctly"
1875
1876 test_27u() { # bug 4900
1877         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1878         remote_mds_nodsh && skip "remote MDS with nodsh"
1879
1880         local index
1881         local list=$(comma_list $(mdts_nodes))
1882
1883 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1884         do_nodes $list $LCTL set_param fail_loc=0x139
1885         test_mkdir -p $DIR/$tdir
1886         trap simple_cleanup_common EXIT
1887         createmany -o $DIR/$tdir/t- 1000
1888         do_nodes $list $LCTL set_param fail_loc=0
1889
1890         TLOG=$TMP/$tfile.getstripe
1891         $LFS getstripe $DIR/$tdir > $TLOG
1892         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1893         unlinkmany $DIR/$tdir/t- 1000
1894         trap 0
1895         [[ $OBJS -gt 0 ]] &&
1896                 error "$OBJS objects created on OST-0. See $TLOG" ||
1897                 rm -f $TLOG
1898 }
1899 run_test 27u "skip object creation on OSC w/o objects"
1900
1901 test_27v() { # bug 4900
1902         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1903         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1904         remote_mds_nodsh && skip "remote MDS with nodsh"
1905         remote_ost_nodsh && skip "remote OST with nodsh"
1906
1907         exhaust_all_precreations 0x215
1908         reset_enospc
1909
1910         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1911
1912         touch $DIR/$tdir/$tfile
1913         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1914         # all except ost1
1915         for (( i=1; i < OSTCOUNT; i++ )); do
1916                 do_facet ost$i lctl set_param fail_loc=0x705
1917         done
1918         local START=`date +%s`
1919         createmany -o $DIR/$tdir/$tfile 32
1920
1921         local FINISH=`date +%s`
1922         local TIMEOUT=`lctl get_param -n timeout`
1923         local PROCESS=$((FINISH - START))
1924         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1925                error "$FINISH - $START >= $TIMEOUT / 2"
1926         sleep $((TIMEOUT / 2 - PROCESS))
1927         reset_enospc
1928 }
1929 run_test 27v "skip object creation on slow OST"
1930
1931 test_27w() { # bug 10997
1932         test_mkdir $DIR/$tdir
1933         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1934         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1935                 error "stripe size $size != 65536" || true
1936         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1937                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1938 }
1939 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1940
1941 test_27wa() {
1942         [[ $OSTCOUNT -lt 2 ]] &&
1943                 skip_env "skipping multiple stripe count/offset test"
1944
1945         test_mkdir $DIR/$tdir
1946         for i in $(seq 1 $OSTCOUNT); do
1947                 offset=$((i - 1))
1948                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1949                         error "setstripe -c $i -i $offset failed"
1950                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1951                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1952                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1953                 [ $index -ne $offset ] &&
1954                         error "stripe offset $index != $offset" || true
1955         done
1956 }
1957 run_test 27wa "check $LFS setstripe -c -i options"
1958
1959 test_27x() {
1960         remote_ost_nodsh && skip "remote OST with nodsh"
1961         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1963
1964         OFFSET=$(($OSTCOUNT - 1))
1965         OSTIDX=0
1966         local OST=$(ostname_from_index $OSTIDX)
1967
1968         test_mkdir $DIR/$tdir
1969         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1970         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1971         sleep_maxage
1972         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1973         for i in $(seq 0 $OFFSET); do
1974                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1975                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1976                 error "OST0 was degraded but new created file still use it"
1977         done
1978         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1979 }
1980 run_test 27x "create files while OST0 is degraded"
1981
1982 test_27y() {
1983         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1984         remote_mds_nodsh && skip "remote MDS with nodsh"
1985         remote_ost_nodsh && skip "remote OST with nodsh"
1986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1987
1988         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1989         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1990                 osp.$mdtosc.prealloc_last_id)
1991         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1992                 osp.$mdtosc.prealloc_next_id)
1993         local fcount=$((last_id - next_id))
1994         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1995         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1996
1997         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1998                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1999         local OST_DEACTIVE_IDX=-1
2000         local OSC
2001         local OSTIDX
2002         local OST
2003
2004         for OSC in $MDS_OSCS; do
2005                 OST=$(osc_to_ost $OSC)
2006                 OSTIDX=$(index_from_ostuuid $OST)
2007                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2008                         OST_DEACTIVE_IDX=$OSTIDX
2009                 fi
2010                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2011                         echo $OSC "is Deactivated:"
2012                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2013                 fi
2014         done
2015
2016         OSTIDX=$(index_from_ostuuid $OST)
2017         test_mkdir $DIR/$tdir
2018         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2019
2020         for OSC in $MDS_OSCS; do
2021                 OST=$(osc_to_ost $OSC)
2022                 OSTIDX=$(index_from_ostuuid $OST)
2023                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2024                         echo $OST "is degraded:"
2025                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2026                                                 obdfilter.$OST.degraded=1
2027                 fi
2028         done
2029
2030         sleep_maxage
2031         createmany -o $DIR/$tdir/$tfile $fcount
2032
2033         for OSC in $MDS_OSCS; do
2034                 OST=$(osc_to_ost $OSC)
2035                 OSTIDX=$(index_from_ostuuid $OST)
2036                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2037                         echo $OST "is recovered from degraded:"
2038                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2039                                                 obdfilter.$OST.degraded=0
2040                 else
2041                         do_facet $SINGLEMDS lctl --device %$OSC activate
2042                 fi
2043         done
2044
2045         # all osp devices get activated, hence -1 stripe count restored
2046         local stripe_count=0
2047
2048         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2049         # devices get activated.
2050         sleep_maxage
2051         $LFS setstripe -c -1 $DIR/$tfile
2052         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2053         rm -f $DIR/$tfile
2054         [ $stripe_count -ne $OSTCOUNT ] &&
2055                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2056         return 0
2057 }
2058 run_test 27y "create files while OST0 is degraded and the rest inactive"
2059
2060 check_seq_oid()
2061 {
2062         log "check file $1"
2063
2064         lmm_count=$($GETSTRIPE -c $1)
2065         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
2066         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
2067
2068         local old_ifs="$IFS"
2069         IFS=$'[:]'
2070         fid=($($LFS path2fid $1))
2071         IFS="$old_ifs"
2072
2073         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2074         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2075
2076         # compare lmm_seq and lu_fid->f_seq
2077         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2078         # compare lmm_object_id and lu_fid->oid
2079         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2080
2081         # check the trusted.fid attribute of the OST objects of the file
2082         local have_obdidx=false
2083         local stripe_nr=0
2084         $GETSTRIPE $1 | while read obdidx oid hex seq; do
2085                 # skip lines up to and including "obdidx"
2086                 [ -z "$obdidx" ] && break
2087                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2088                 $have_obdidx || continue
2089
2090                 local ost=$((obdidx + 1))
2091                 local dev=$(ostdevname $ost)
2092                 local oid_hex
2093
2094                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2095
2096                 seq=$(echo $seq | sed -e "s/^0x//g")
2097                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2098                         oid_hex=$(echo $oid)
2099                 else
2100                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2101                 fi
2102                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2103
2104                 local ff=""
2105                 #
2106                 # Don't unmount/remount the OSTs if we don't need to do that.
2107                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2108                 # update too, until that use mount/ll_decode_filter_fid/mount.
2109                 # Re-enable when debugfs will understand new filter_fid.
2110                 #
2111                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2112                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2113                                 $dev 2>/dev/null" | grep "parent=")
2114                 fi
2115                 if [ -z "$ff" ]; then
2116                         stop ost$ost
2117                         mount_fstype ost$ost
2118                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2119                                 $(facet_mntpt ost$ost)/$obj_file)
2120                         unmount_fstype ost$ost
2121                         start ost$ost $dev $OST_MOUNT_OPTS
2122                         clients_up
2123                 fi
2124
2125                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2126
2127                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2128
2129                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2130                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2131                 #
2132                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2133                 #       stripe_size=1048576 component_id=1 component_start=0 \
2134                 #       component_end=33554432
2135                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2136                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2137                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2138                 local ff_pstripe
2139                 if grep -q 'stripe=' <<<$ff; then
2140                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2141                 else
2142                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2143                         # into f_ver in this case.  See comment on ff_parent.
2144                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2145                 fi
2146
2147                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2148                 [ $ff_pseq = $lmm_seq ] ||
2149                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2150                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2151                 [ $ff_poid = $lmm_oid ] ||
2152                         error "FF parent OID $ff_poid != $lmm_oid"
2153                 (($ff_pstripe == $stripe_nr)) ||
2154                         error "FF stripe $ff_pstripe != $stripe_nr"
2155
2156                 stripe_nr=$((stripe_nr + 1))
2157                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2158                         continue
2159                 if grep -q 'stripe_count=' <<<$ff; then
2160                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2161                                             -e 's/ .*//' <<<$ff)
2162                         [ $lmm_count = $ff_scnt ] ||
2163                                 error "FF stripe count $lmm_count != $ff_scnt"
2164                 fi
2165         done
2166 }
2167
2168 test_27z() {
2169         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2170         remote_ost_nodsh && skip "remote OST with nodsh"
2171
2172         test_mkdir $DIR/$tdir
2173         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2174                 { error "setstripe -c -1 failed"; return 1; }
2175         # We need to send a write to every object to get parent FID info set.
2176         # This _should_ also work for setattr, but does not currently.
2177         # touch $DIR/$tdir/$tfile-1 ||
2178         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2179                 { error "dd $tfile-1 failed"; return 2; }
2180         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2181                 { error "setstripe -c -1 failed"; return 3; }
2182         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2183                 { error "dd $tfile-2 failed"; return 4; }
2184
2185         # make sure write RPCs have been sent to OSTs
2186         sync; sleep 5; sync
2187
2188         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2189         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2190 }
2191 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2192
2193 test_27A() { # b=19102
2194         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2195
2196         save_layout_restore_at_exit $MOUNT
2197         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2198         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2199                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2200         local default_size=$($GETSTRIPE -S $MOUNT)
2201         local default_offset=$($GETSTRIPE -i $MOUNT)
2202         local dsize=$(do_facet $SINGLEMDS \
2203                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2204         [ $default_size -eq $dsize ] ||
2205                 error "stripe size $default_size != $dsize"
2206         [ $default_offset -eq -1 ] ||
2207                 error "stripe offset $default_offset != -1"
2208 }
2209 run_test 27A "check filesystem-wide default LOV EA values"
2210
2211 test_27B() { # LU-2523
2212         test_mkdir $DIR/$tdir
2213         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2214         touch $DIR/$tdir/f0
2215         # open f1 with O_LOV_DELAY_CREATE
2216         # rename f0 onto f1
2217         # call setstripe ioctl on open file descriptor for f1
2218         # close
2219         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2220                 $DIR/$tdir/f0
2221
2222         rm -f $DIR/$tdir/f1
2223         # open f1 with O_LOV_DELAY_CREATE
2224         # unlink f1
2225         # call setstripe ioctl on open file descriptor for f1
2226         # close
2227         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2228
2229         # Allow multiop to fail in imitation of NFS's busted semantics.
2230         true
2231 }
2232 run_test 27B "call setstripe on open unlinked file/rename victim"
2233
2234 test_27C() { #LU-2871
2235         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2236
2237         declare -a ost_idx
2238         local index
2239         local found
2240         local i
2241         local j
2242
2243         test_mkdir $DIR/$tdir
2244         cd $DIR/$tdir
2245         for i in $(seq 0 $((OSTCOUNT - 1))); do
2246                 # set stripe across all OSTs starting from OST$i
2247                 $SETSTRIPE -i $i -c -1 $tfile$i
2248                 # get striping information
2249                 ost_idx=($($GETSTRIPE $tfile$i |
2250                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2251                 echo ${ost_idx[@]}
2252
2253                 # check the layout
2254                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2255                         error "${#ost_idx[@]} != $OSTCOUNT"
2256
2257                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2258                         found=0
2259                         for j in $(echo ${ost_idx[@]}); do
2260                                 if [ $index -eq $j ]; then
2261                                         found=1
2262                                         break
2263                                 fi
2264                         done
2265                         [ $found = 1 ] ||
2266                                 error "Can not find $index in ${ost_idx[@]}"
2267                 done
2268         done
2269 }
2270 run_test 27C "check full striping across all OSTs"
2271
2272 test_27D() {
2273         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2274         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2275         remote_mds_nodsh && skip "remote MDS with nodsh"
2276
2277         local POOL=${POOL:-testpool}
2278         local first_ost=0
2279         local last_ost=$(($OSTCOUNT - 1))
2280         local ost_step=1
2281         local ost_list=$(seq $first_ost $ost_step $last_ost)
2282         local ost_range="$first_ost $last_ost $ost_step"
2283
2284         test_mkdir $DIR/$tdir
2285         pool_add $POOL || error "pool_add failed"
2286         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2287
2288         local skip27D
2289         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2290                 skip27D+="-s 29"
2291         [ $MDS1_VERSION -lt $(version_code 2.9.55) -o \
2292           $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2293                 skip27D+=" -s 30,31"
2294         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2295                 error "llapi_layout_test failed"
2296
2297         destroy_test_pools || error "destroy test pools failed"
2298 }
2299 run_test 27D "validate llapi_layout API"
2300
2301 # Verify that default_easize is increased from its initial value after
2302 # accessing a widely striped file.
2303 test_27E() {
2304         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2305         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2306                 skip "client does not have LU-3338 fix"
2307
2308         # 72 bytes is the minimum space required to store striping
2309         # information for a file striped across one OST:
2310         # (sizeof(struct lov_user_md_v3) +
2311         #  sizeof(struct lov_user_ost_data_v1))
2312         local min_easize=72
2313         $LCTL set_param -n llite.*.default_easize $min_easize ||
2314                 error "lctl set_param failed"
2315         local easize=$($LCTL get_param -n llite.*.default_easize)
2316
2317         [ $easize -eq $min_easize ] ||
2318                 error "failed to set default_easize"
2319
2320         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2321                 error "setstripe failed"
2322         cat $DIR/$tfile
2323         rm $DIR/$tfile
2324
2325         easize=$($LCTL get_param -n llite.*.default_easize)
2326
2327         [ $easize -gt $min_easize ] ||
2328                 error "default_easize not updated"
2329 }
2330 run_test 27E "check that default extended attribute size properly increases"
2331
2332 test_27F() { # LU-5346/LU-7975
2333         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2334         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2335         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2336                 skip "Need MDS version at least 2.8.51"
2337         remote_ost_nodsh && skip "remote OST with nodsh"
2338
2339         test_mkdir $DIR/$tdir
2340         rm -f $DIR/$tdir/f0
2341         $SETSTRIPE -c 2 $DIR/$tdir
2342
2343         # stop all OSTs to reproduce situation for LU-7975 ticket
2344         for num in $(seq $OSTCOUNT); do
2345                 stop ost$num
2346         done
2347
2348         # open/create f0 with O_LOV_DELAY_CREATE
2349         # truncate f0 to a non-0 size
2350         # close
2351         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2352
2353         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2354         # open/write it again to force delayed layout creation
2355         cat /etc/hosts > $DIR/$tdir/f0 &
2356         catpid=$!
2357
2358         # restart OSTs
2359         for num in $(seq $OSTCOUNT); do
2360                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2361                         error "ost$num failed to start"
2362         done
2363
2364         wait $catpid || error "cat failed"
2365
2366         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2367         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2368
2369 }
2370 run_test 27F "Client resend delayed layout creation with non-zero size"
2371
2372 test_27G() { #LU-10629
2373         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2374                 skip "Need MDS version at least 2.11.51"
2375         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2376         remote_mds_nodsh && skip "remote MDS with nodsh"
2377         local POOL=${POOL:-testpool}
2378         local ostrange="0 0 1"
2379
2380         test_mkdir $DIR/$tdir
2381         pool_add $POOL || error "pool_add failed"
2382         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2383         $LFS setstripe -p $POOL $DIR/$tdir
2384
2385         local pool=$($LFS getstripe -p $DIR/$tdir)
2386
2387         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2388
2389         $LFS setstripe -d $DIR/$tdir
2390
2391         pool=$($LFS getstripe -p $DIR/$tdir)
2392
2393         rmdir $DIR/$tdir
2394
2395         [ -z "$pool" ] || error "'$pool' is not empty"
2396 }
2397 run_test 27G "Clear OST pool from stripe"
2398
2399 test_27H() {
2400         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2401                 skip "Need MDS version newer than 2.11.54"
2402         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2403         test_mkdir $DIR/$tdir
2404         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2405         touch $DIR/$tdir/$tfile
2406         $LFS getstripe -c $DIR/$tdir/$tfile
2407         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2408                 error "two-stripe file doesn't have two stripes"
2409
2410         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2411         $LFS getstripe -y $DIR/$tdir/$tfile
2412         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2413              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2414                 error "expected l_ost_idx: [02]$ not matched"
2415
2416         # make sure ost list have been cleared
2417         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
2418         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2419                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2420         touch $DIR/$tdir/f3
2421         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2422 }
2423 run_test 27H "Set specific OSTs stripe"
2424
2425 test_27M() {
2426         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2427                 skip "Need MDS version >= than 2.12.57"
2428         remote_mds_nodsh && skip "remote MDS with nodsh"
2429         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2430
2431         test_mkdir $DIR/$tdir
2432
2433         # Set default striping on directory
2434         local setcount=4
2435         local stripe_opt
2436
2437         # if we run against a 2.12 server which lacks overstring support
2438         # then the connect_flag will not report overstriping, even if client
2439         # is 2.14+
2440         if [[ $($LCTL get_param mdc.*.connect_flags) =~ overstriping ]]; then
2441                 stripe_opt="-C $setcount"
2442         elif (( $OSTCOUNT >= $setcount )); then
2443                 stripe_opt="-c $setcount"
2444         else
2445                 skip "server does not support overstriping"
2446         fi
2447         $LFS setstripe $stripe_opt $DIR/$tdir
2448
2449         echo 1 > $DIR/$tdir/${tfile}.1
2450         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2451         [ $count -eq $setcount ] ||
2452                 error "(1) stripe count $count, should be $setcount"
2453
2454         # Capture existing append_stripe_count setting for restore
2455         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2456         local mdts=$(comma_list $(mdts_nodes))
2457         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2458
2459         local appendcount=$orig_count
2460         echo 1 >> $DIR/$tdir/${tfile}.2_append
2461         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2462         [ $count -eq $appendcount ] ||
2463                 error "(2)stripe count $count, should be $appendcount for append"
2464
2465         # Disable O_APPEND striping, verify it works
2466         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2467
2468         # Should now get the default striping, which is 4
2469         setcount=4
2470         echo 1 >> $DIR/$tdir/${tfile}.3_append
2471         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2472         [ $count -eq $setcount ] ||
2473                 error "(3) stripe count $count, should be $setcount"
2474
2475         # Try changing the stripe count for append files
2476         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2477
2478         # Append striping is now 2 (directory default is still 4)
2479         appendcount=2
2480         echo 1 >> $DIR/$tdir/${tfile}.4_append
2481         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2482         [ $count -eq $appendcount ] ||
2483                 error "(4) stripe count $count, should be $appendcount for append"
2484
2485         # Test append stripe count of -1
2486         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2487         appendcount=$OSTCOUNT
2488         echo 1 >> $DIR/$tdir/${tfile}.5
2489         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2490         [ $count -eq $appendcount ] ||
2491                 error "(5) stripe count $count, should be $appendcount for append"
2492
2493         # Set append striping back to default of 1
2494         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2495
2496         # Try a new default striping, PFL + DOM
2497         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2498
2499         # Create normal DOM file, DOM returns stripe count == 0
2500         setcount=0
2501         touch $DIR/$tdir/${tfile}.6
2502         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2503         [ $count -eq $setcount ] ||
2504                 error "(6) stripe count $count, should be $setcount"
2505
2506         # Show
2507         appendcount=1
2508         echo 1 >> $DIR/$tdir/${tfile}.7_append
2509         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2510         [ $count -eq $appendcount ] ||
2511                 error "(7) stripe count $count, should be $appendcount for append"
2512
2513         # Clean up DOM layout
2514         $LFS setstripe -d $DIR/$tdir
2515
2516         save_layout_restore_at_exit $MOUNT
2517         # Now test that append striping works when layout is from root
2518         $LFS setstripe -c 2 $MOUNT
2519         # Make a special directory for this
2520         mkdir $DIR/${tdir}/${tdir}.2
2521
2522         # Verify for normal file
2523         setcount=2
2524         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2525         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2526         [ $count -eq $setcount ] ||
2527                 error "(8) stripe count $count, should be $setcount"
2528
2529         appendcount=1
2530         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2531         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2532         [ $count -eq $appendcount ] ||
2533                 error "(9) stripe count $count, should be $appendcount for append"
2534
2535         # Now test O_APPEND striping with pools
2536         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2537         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2538
2539         # Create the pool
2540         pool_add $TESTNAME || error "pool creation failed"
2541         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2542
2543         echo 1 >> $DIR/$tdir/${tfile}.10_append
2544
2545         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2546         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2547
2548         # Check that count is still correct
2549         appendcount=1
2550         echo 1 >> $DIR/$tdir/${tfile}.11_append
2551         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2552         [ $count -eq $appendcount ] ||
2553                 error "(11) stripe count $count, should be $appendcount for append"
2554
2555         # Disable O_APPEND stripe count, verify pool works separately
2556         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2557
2558         echo 1 >> $DIR/$tdir/${tfile}.12_append
2559
2560         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2561         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2562
2563         # Remove pool setting, verify it's not applied
2564         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2565
2566         echo 1 >> $DIR/$tdir/${tfile}.13_append
2567
2568         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2569         [ "$pool" = "" ] || error "(13) pool found: $pool"
2570 }
2571 run_test 27M "test O_APPEND striping"
2572
2573 # createtest also checks that device nodes are created and
2574 # then visible correctly (#2091)
2575 test_28() { # bug 2091
2576         test_mkdir $DIR/d28
2577         $CREATETEST $DIR/d28/ct || error "createtest failed"
2578 }
2579 run_test 28 "create/mknod/mkdir with bad file types ============"
2580
2581 test_29() {
2582         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2583
2584         sync; sleep 1; sync # flush out any dirty pages from previous tests
2585         cancel_lru_locks
2586         test_mkdir $DIR/d29
2587         touch $DIR/d29/foo
2588         log 'first d29'
2589         ls -l $DIR/d29
2590
2591         declare -i LOCKCOUNTORIG=0
2592         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2593                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2594         done
2595         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2596
2597         declare -i LOCKUNUSEDCOUNTORIG=0
2598         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2599                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2600         done
2601
2602         log 'second d29'
2603         ls -l $DIR/d29
2604         log 'done'
2605
2606         declare -i LOCKCOUNTCURRENT=0
2607         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2608                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2609         done
2610
2611         declare -i LOCKUNUSEDCOUNTCURRENT=0
2612         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2613                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2614         done
2615
2616         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2617                 $LCTL set_param -n ldlm.dump_namespaces ""
2618                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2619                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2620                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2621                 return 2
2622         fi
2623         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2624                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2625                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2626                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2627                 return 3
2628         fi
2629 }
2630 run_test 29 "IT_GETATTR regression  ============================"
2631
2632 test_30a() { # was test_30
2633         cp $(which ls) $DIR || cp /bin/ls $DIR
2634         $DIR/ls / || error "Can't execute binary from lustre"
2635         rm $DIR/ls
2636 }
2637 run_test 30a "execute binary from Lustre (execve) =============="
2638
2639 test_30b() {
2640         cp `which ls` $DIR || cp /bin/ls $DIR
2641         chmod go+rx $DIR/ls
2642         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2643         rm $DIR/ls
2644 }
2645 run_test 30b "execute binary from Lustre as non-root ==========="
2646
2647 test_30c() { # b=22376
2648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2649
2650         cp `which ls` $DIR || cp /bin/ls $DIR
2651         chmod a-rw $DIR/ls
2652         cancel_lru_locks mdc
2653         cancel_lru_locks osc
2654         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2655         rm -f $DIR/ls
2656 }
2657 run_test 30c "execute binary from Lustre without read perms ===="
2658
2659 test_31a() {
2660         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2661         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2662 }
2663 run_test 31a "open-unlink file =================================="
2664
2665 test_31b() {
2666         touch $DIR/f31 || error "touch $DIR/f31 failed"
2667         ln $DIR/f31 $DIR/f31b || error "ln failed"
2668         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2669         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2670 }
2671 run_test 31b "unlink file with multiple links while open ======="
2672
2673 test_31c() {
2674         touch $DIR/f31 || error "touch $DIR/f31 failed"
2675         ln $DIR/f31 $DIR/f31c || error "ln failed"
2676         multiop_bg_pause $DIR/f31 O_uc ||
2677                 error "multiop_bg_pause for $DIR/f31 failed"
2678         MULTIPID=$!
2679         $MULTIOP $DIR/f31c Ouc
2680         kill -USR1 $MULTIPID
2681         wait $MULTIPID
2682 }
2683 run_test 31c "open-unlink file with multiple links ============="
2684
2685 test_31d() {
2686         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2687         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2688 }
2689 run_test 31d "remove of open directory ========================="
2690
2691 test_31e() { # bug 2904
2692         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2693 }
2694 run_test 31e "remove of open non-empty directory ==============="
2695
2696 test_31f() { # bug 4554
2697         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2698
2699         set -vx
2700         test_mkdir $DIR/d31f
2701         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2702         cp /etc/hosts $DIR/d31f
2703         ls -l $DIR/d31f
2704         $GETSTRIPE $DIR/d31f/hosts
2705         multiop_bg_pause $DIR/d31f D_c || return 1
2706         MULTIPID=$!
2707
2708         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2709         test_mkdir $DIR/d31f
2710         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2711         cp /etc/hosts $DIR/d31f
2712         ls -l $DIR/d31f
2713         $GETSTRIPE $DIR/d31f/hosts
2714         multiop_bg_pause $DIR/d31f D_c || return 1
2715         MULTIPID2=$!
2716
2717         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2718         wait $MULTIPID || error "first opendir $MULTIPID failed"
2719
2720         sleep 6
2721
2722         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2723         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2724         set +vx
2725 }
2726 run_test 31f "remove of open directory with open-unlink file ==="
2727
2728 test_31g() {
2729         echo "-- cross directory link --"
2730         test_mkdir -c1 $DIR/${tdir}ga
2731         test_mkdir -c1 $DIR/${tdir}gb
2732         touch $DIR/${tdir}ga/f
2733         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2734         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2735         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2736         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2737         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2738 }
2739 run_test 31g "cross directory link==============="
2740
2741 test_31h() {
2742         echo "-- cross directory link --"
2743         test_mkdir -c1 $DIR/${tdir}
2744         test_mkdir -c1 $DIR/${tdir}/dir
2745         touch $DIR/${tdir}/f
2746         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2747         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2748         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2749         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2750         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2751 }
2752 run_test 31h "cross directory link under child==============="
2753
2754 test_31i() {
2755         echo "-- cross directory link --"
2756         test_mkdir -c1 $DIR/$tdir
2757         test_mkdir -c1 $DIR/$tdir/dir
2758         touch $DIR/$tdir/dir/f
2759         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2760         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2761         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2762         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2763         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2764 }
2765 run_test 31i "cross directory link under parent==============="
2766
2767 test_31j() {
2768         test_mkdir -c1 -p $DIR/$tdir
2769         test_mkdir -c1 -p $DIR/$tdir/dir1
2770         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2771         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2772         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2773         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2774         return 0
2775 }
2776 run_test 31j "link for directory==============="
2777
2778 test_31k() {
2779         test_mkdir -c1 -p $DIR/$tdir
2780         touch $DIR/$tdir/s
2781         touch $DIR/$tdir/exist
2782         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2783         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2784         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2785         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2786         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2787         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2788         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2789         return 0
2790 }
2791 run_test 31k "link to file: the same, non-existing, dir==============="
2792
2793 test_31m() {
2794         mkdir $DIR/d31m
2795         touch $DIR/d31m/s
2796         mkdir $DIR/d31m2
2797         touch $DIR/d31m2/exist
2798         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2799         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2800         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2801         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2802         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2803         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2804         return 0
2805 }
2806 run_test 31m "link to file: the same, non-existing, dir==============="
2807
2808 test_31n() {
2809         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2810         nlink=$(stat --format=%h $DIR/$tfile)
2811         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2812         local fd=$(free_fd)
2813         local cmd="exec $fd<$DIR/$tfile"
2814         eval $cmd
2815         cmd="exec $fd<&-"
2816         trap "eval $cmd" EXIT
2817         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2818         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2819         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2820         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2821         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2822         eval $cmd
2823 }
2824 run_test 31n "check link count of unlinked file"
2825
2826 link_one() {
2827         local TEMPNAME=$(mktemp $1_XXXXXX)
2828         mlink $TEMPNAME $1 2> /dev/null &&
2829                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2830         munlink $TEMPNAME
2831 }
2832
2833 test_31o() { # LU-2901
2834         test_mkdir $DIR/$tdir
2835         for LOOP in $(seq 100); do
2836                 rm -f $DIR/$tdir/$tfile*
2837                 for THREAD in $(seq 8); do
2838                         link_one $DIR/$tdir/$tfile.$LOOP &
2839                 done
2840                 wait
2841                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2842                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2843                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2844                         break || true
2845         done
2846 }
2847 run_test 31o "duplicate hard links with same filename"
2848
2849 test_31p() {
2850         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2851
2852         test_mkdir $DIR/$tdir
2853         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2854         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2855
2856         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2857                 error "open unlink test1 failed"
2858         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2859                 error "open unlink test2 failed"
2860
2861         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2862                 error "test1 still exists"
2863         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2864                 error "test2 still exists"
2865 }
2866 run_test 31p "remove of open striped directory"
2867
2868 cleanup_test32_mount() {
2869         local rc=0
2870         trap 0
2871         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2872         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2873         losetup -d $loopdev || true
2874         rm -rf $DIR/$tdir
2875         return $rc
2876 }
2877
2878 test_32a() {
2879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2880
2881         echo "== more mountpoints and symlinks ================="
2882         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2883         trap cleanup_test32_mount EXIT
2884         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2885         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2886                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2887         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2888                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2889         cleanup_test32_mount
2890 }
2891 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2892
2893 test_32b() {
2894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2895
2896         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2897         trap cleanup_test32_mount EXIT
2898         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2899         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2900                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2901         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2902                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2903         cleanup_test32_mount
2904 }
2905 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2906
2907 test_32c() {
2908         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2909
2910         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2911         trap cleanup_test32_mount EXIT
2912         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2913         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2914                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2915         test_mkdir -p $DIR/$tdir/d2/test_dir
2916         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2917                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2918         cleanup_test32_mount
2919 }
2920 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2921
2922 test_32d() {
2923         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2924
2925         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2926         trap cleanup_test32_mount EXIT
2927         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2928         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2929                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2930         test_mkdir -p $DIR/$tdir/d2/test_dir
2931         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2932                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2933         cleanup_test32_mount
2934 }
2935 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2936
2937 test_32e() {
2938         rm -fr $DIR/$tdir
2939         test_mkdir -p $DIR/$tdir/tmp
2940         local tmp_dir=$DIR/$tdir/tmp
2941         ln -s $DIR/$tdir $tmp_dir/symlink11
2942         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2943         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2944         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2945 }
2946 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2947
2948 test_32f() {
2949         rm -fr $DIR/$tdir
2950         test_mkdir -p $DIR/$tdir/tmp
2951         local tmp_dir=$DIR/$tdir/tmp
2952         ln -s $DIR/$tdir $tmp_dir/symlink11
2953         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2954         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2955         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2956 }
2957 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2958
2959 test_32g() {
2960         local tmp_dir=$DIR/$tdir/tmp
2961         test_mkdir -p $tmp_dir
2962         test_mkdir $DIR/${tdir}2
2963         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2964         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2965         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2966         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2967         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2968         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2969 }
2970 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2971
2972 test_32h() {
2973         rm -fr $DIR/$tdir $DIR/${tdir}2
2974         tmp_dir=$DIR/$tdir/tmp
2975         test_mkdir -p $tmp_dir
2976         test_mkdir $DIR/${tdir}2
2977         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2978         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2979         ls $tmp_dir/symlink12 || error "listing symlink12"
2980         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2981 }
2982 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2983
2984 test_32i() {
2985         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2986
2987         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2988         trap cleanup_test32_mount EXIT
2989         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2990         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2991                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2992         touch $DIR/$tdir/test_file
2993         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
2994                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
2995         cleanup_test32_mount
2996 }
2997 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2998
2999 test_32j() {
3000         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3001
3002         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3003         trap cleanup_test32_mount EXIT
3004         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3005         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3006                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3007         touch $DIR/$tdir/test_file
3008         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3009                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3010         cleanup_test32_mount
3011 }
3012 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3013
3014 test_32k() {
3015         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3016
3017         rm -fr $DIR/$tdir
3018         trap cleanup_test32_mount EXIT
3019         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3020         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3021                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3022         test_mkdir -p $DIR/$tdir/d2
3023         touch $DIR/$tdir/d2/test_file || error "touch failed"
3024         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3025                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3026         cleanup_test32_mount
3027 }
3028 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3029
3030 test_32l() {
3031         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3032
3033         rm -fr $DIR/$tdir
3034         trap cleanup_test32_mount EXIT
3035         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3036         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3037                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3038         test_mkdir -p $DIR/$tdir/d2
3039         touch $DIR/$tdir/d2/test_file || error "touch failed"
3040         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3041                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3042         cleanup_test32_mount
3043 }
3044 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3045
3046 test_32m() {
3047         rm -fr $DIR/d32m
3048         test_mkdir -p $DIR/d32m/tmp
3049         TMP_DIR=$DIR/d32m/tmp
3050         ln -s $DIR $TMP_DIR/symlink11
3051         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3052         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3053                 error "symlink11 not a link"
3054         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3055                 error "symlink01 not a link"
3056 }
3057 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3058
3059 test_32n() {
3060         rm -fr $DIR/d32n
3061         test_mkdir -p $DIR/d32n/tmp
3062         TMP_DIR=$DIR/d32n/tmp
3063         ln -s $DIR $TMP_DIR/symlink11
3064         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3065         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3066         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3067 }
3068 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3069
3070 test_32o() {
3071         touch $DIR/$tfile
3072         test_mkdir -p $DIR/d32o/tmp
3073         TMP_DIR=$DIR/d32o/tmp
3074         ln -s $DIR/$tfile $TMP_DIR/symlink12
3075         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3076         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3077                 error "symlink12 not a link"
3078         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3079         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3080                 error "$DIR/d32o/tmp/symlink12 not file type"
3081         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3082                 error "$DIR/d32o/symlink02 not file type"
3083 }
3084 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3085
3086 test_32p() {
3087         log 32p_1
3088         rm -fr $DIR/d32p
3089         log 32p_2
3090         rm -f $DIR/$tfile
3091         log 32p_3
3092         touch $DIR/$tfile
3093         log 32p_4
3094         test_mkdir -p $DIR/d32p/tmp
3095         log 32p_5
3096         TMP_DIR=$DIR/d32p/tmp
3097         log 32p_6
3098         ln -s $DIR/$tfile $TMP_DIR/symlink12
3099         log 32p_7
3100         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3101         log 32p_8
3102         cat $DIR/d32p/tmp/symlink12 ||
3103                 error "Can't open $DIR/d32p/tmp/symlink12"
3104         log 32p_9
3105         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3106         log 32p_10
3107 }
3108 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3109
3110 test_32q() {
3111         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3112
3113         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3114         trap cleanup_test32_mount EXIT
3115         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3116         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3117         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3118                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3119         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3120         cleanup_test32_mount
3121 }
3122 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3123
3124 test_32r() {
3125         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3126
3127         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3128         trap cleanup_test32_mount EXIT
3129         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3130         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3131         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3132                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3133         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3134         cleanup_test32_mount
3135 }
3136 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3137
3138 test_33aa() {
3139         rm -f $DIR/$tfile
3140         touch $DIR/$tfile
3141         chmod 444 $DIR/$tfile
3142         chown $RUNAS_ID $DIR/$tfile
3143         log 33_1
3144         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3145         log 33_2
3146 }
3147 run_test 33aa "write file with mode 444 (should return error)"
3148
3149 test_33a() {
3150         rm -fr $DIR/$tdir
3151         test_mkdir $DIR/$tdir
3152         chown $RUNAS_ID $DIR/$tdir
3153         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3154                 error "$RUNAS create $tdir/$tfile failed"
3155         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3156                 error "open RDWR" || true
3157 }
3158 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3159
3160 test_33b() {
3161         rm -fr $DIR/$tdir
3162         test_mkdir $DIR/$tdir
3163         chown $RUNAS_ID $DIR/$tdir
3164         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3165 }
3166 run_test 33b "test open file with malformed flags (No panic)"
3167
3168 test_33c() {
3169         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3170         remote_ost_nodsh && skip "remote OST with nodsh"
3171
3172         local ostnum
3173         local ostname
3174         local write_bytes
3175         local all_zeros
3176
3177         all_zeros=:
3178         rm -fr $DIR/$tdir
3179         test_mkdir $DIR/$tdir
3180         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3181
3182         sync
3183         for ostnum in $(seq $OSTCOUNT); do
3184                 # test-framework's OST numbering is one-based, while Lustre's
3185                 # is zero-based
3186                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3187                 # Parsing llobdstat's output sucks; we could grep the /proc
3188                 # path, but that's likely to not be as portable as using the
3189                 # llobdstat utility.  So we parse lctl output instead.
3190                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3191                         obdfilter/$ostname/stats |
3192                         awk '/^write_bytes/ {print $7}' )
3193                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3194                 if (( ${write_bytes:-0} > 0 ))
3195                 then
3196                         all_zeros=false
3197                         break;
3198                 fi
3199         done
3200
3201         $all_zeros || return 0
3202
3203         # Write four bytes
3204         echo foo > $DIR/$tdir/bar
3205         # Really write them
3206         sync
3207
3208         # Total up write_bytes after writing.  We'd better find non-zeros.
3209         for ostnum in $(seq $OSTCOUNT); do
3210                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3211                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3212                         obdfilter/$ostname/stats |
3213                         awk '/^write_bytes/ {print $7}' )
3214                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3215                 if (( ${write_bytes:-0} > 0 ))
3216                 then
3217                         all_zeros=false
3218                         break;
3219                 fi
3220         done
3221
3222         if $all_zeros
3223         then
3224                 for ostnum in $(seq $OSTCOUNT); do
3225                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3226                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3227                         do_facet ost$ostnum lctl get_param -n \
3228                                 obdfilter/$ostname/stats
3229                 done
3230                 error "OST not keeping write_bytes stats (b22312)"
3231         fi
3232 }
3233 run_test 33c "test llobdstat and write_bytes"
3234
3235 test_33d() {
3236         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3237         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3238
3239         local MDTIDX=1
3240         local remote_dir=$DIR/$tdir/remote_dir
3241
3242         test_mkdir $DIR/$tdir
3243         $LFS mkdir -i $MDTIDX $remote_dir ||
3244                 error "create remote directory failed"
3245
3246         touch $remote_dir/$tfile
3247         chmod 444 $remote_dir/$tfile
3248         chown $RUNAS_ID $remote_dir/$tfile
3249
3250         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3251
3252         chown $RUNAS_ID $remote_dir
3253         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3254                                         error "create" || true
3255         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3256                                     error "open RDWR" || true
3257         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3258 }
3259 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3260
3261 test_33e() {
3262         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3263
3264         mkdir $DIR/$tdir
3265
3266         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3267         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3268         mkdir $DIR/$tdir/local_dir
3269
3270         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3271         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3272         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3273
3274         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3275                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3276
3277         rmdir $DIR/$tdir/* || error "rmdir failed"
3278
3279         umask 777
3280         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3281         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3282         mkdir $DIR/$tdir/local_dir
3283
3284         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3285         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3286         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3287
3288         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3289                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3290
3291         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3292
3293         umask 000
3294         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3295         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3296         mkdir $DIR/$tdir/local_dir
3297
3298         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3299         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3300         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3301
3302         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3303                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3304 }
3305 run_test 33e "mkdir and striped directory should have same mode"
3306
3307 cleanup_33f() {
3308         trap 0
3309         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3310 }
3311
3312 test_33f() {
3313         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3314         remote_mds_nodsh && skip "remote MDS with nodsh"
3315
3316         mkdir $DIR/$tdir
3317         chmod go+rwx $DIR/$tdir
3318         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3319         trap cleanup_33f EXIT
3320
3321         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3322                 error "cannot create striped directory"
3323
3324         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3325                 error "cannot create files in striped directory"
3326
3327         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3328                 error "cannot remove files in striped directory"
3329
3330         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3331                 error "cannot remove striped directory"
3332
3333         cleanup_33f
3334 }
3335 run_test 33f "nonroot user can create, access, and remove a striped directory"
3336
3337 test_33g() {
3338         mkdir -p $DIR/$tdir/dir2
3339
3340         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3341         echo $err
3342         [[ $err =~ "exists" ]] || error "Not exists error"
3343 }
3344 run_test 33g "nonroot user create already existing root created file"
3345
3346 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3347 test_34a() {
3348         rm -f $DIR/f34
3349         $MCREATE $DIR/f34 || error "mcreate failed"
3350         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3351                 error "getstripe failed"
3352         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3353         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3354                 error "getstripe failed"
3355         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3356                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3357 }
3358 run_test 34a "truncate file that has not been opened ==========="
3359
3360 test_34b() {
3361         [ ! -f $DIR/f34 ] && test_34a
3362         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3363                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3364         $OPENFILE -f O_RDONLY $DIR/f34
3365         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3366                 error "getstripe failed"
3367         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3368                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3369 }
3370 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3371
3372 test_34c() {
3373         [ ! -f $DIR/f34 ] && test_34a
3374         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3375                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3376         $OPENFILE -f O_RDWR $DIR/f34
3377         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
3378         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3379                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3380 }
3381 run_test 34c "O_RDWR opening file-with-size works =============="
3382
3383 test_34d() {
3384         [ ! -f $DIR/f34 ] && test_34a
3385         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3386                 error "dd failed"
3387         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3388                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3389         rm $DIR/f34
3390 }
3391 run_test 34d "write to sparse file ============================="
3392
3393 test_34e() {
3394         rm -f $DIR/f34e
3395         $MCREATE $DIR/f34e || error "mcreate failed"
3396         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3397         $CHECKSTAT -s 1000 $DIR/f34e ||
3398                 error "Size of $DIR/f34e not equal to 1000 bytes"
3399         $OPENFILE -f O_RDWR $DIR/f34e
3400         $CHECKSTAT -s 1000 $DIR/f34e ||
3401                 error "Size of $DIR/f34e not equal to 1000 bytes"
3402 }
3403 run_test 34e "create objects, some with size and some without =="
3404
3405 test_34f() { # bug 6242, 6243
3406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3407
3408         SIZE34F=48000
3409         rm -f $DIR/f34f
3410         $MCREATE $DIR/f34f || error "mcreate failed"
3411         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3412         dd if=$DIR/f34f of=$TMP/f34f
3413         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3414         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3415         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3416         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3417         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3418 }
3419 run_test 34f "read from a file with no objects until EOF ======="
3420
3421 test_34g() {
3422         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3423
3424         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3425                 error "dd failed"
3426         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3427         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3428                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3429         cancel_lru_locks osc
3430         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3431                 error "wrong size after lock cancel"
3432
3433         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3434         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3435                 error "expanding truncate failed"
3436         cancel_lru_locks osc
3437         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3438                 error "wrong expanded size after lock cancel"
3439 }
3440 run_test 34g "truncate long file ==============================="
3441
3442 test_34h() {
3443         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3444
3445         local gid=10
3446         local sz=1000
3447
3448         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3449         sync # Flush the cache so that multiop below does not block on cache
3450              # flush when getting the group lock
3451         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3452         MULTIPID=$!
3453
3454         # Since just timed wait is not good enough, let's do a sync write
3455         # that way we are sure enough time for a roundtrip + processing
3456         # passed + 2 seconds of extra margin.
3457         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3458         rm $DIR/${tfile}-1
3459         sleep 2
3460
3461         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3462                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3463                 kill -9 $MULTIPID
3464         fi
3465         wait $MULTIPID
3466         local nsz=`stat -c %s $DIR/$tfile`
3467         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3468 }
3469 run_test 34h "ftruncate file under grouplock should not block"
3470
3471 test_35a() {
3472         cp /bin/sh $DIR/f35a
3473         chmod 444 $DIR/f35a
3474         chown $RUNAS_ID $DIR/f35a
3475         $RUNAS $DIR/f35a && error || true
3476         rm $DIR/f35a
3477 }
3478 run_test 35a "exec file with mode 444 (should return and not leak)"
3479
3480 test_36a() {
3481         rm -f $DIR/f36
3482         utime $DIR/f36 || error "utime failed for MDS"
3483 }
3484 run_test 36a "MDS utime check (mknod, utime)"
3485
3486 test_36b() {
3487         echo "" > $DIR/f36
3488         utime $DIR/f36 || error "utime failed for OST"
3489 }
3490 run_test 36b "OST utime check (open, utime)"
3491
3492 test_36c() {
3493         rm -f $DIR/d36/f36
3494         test_mkdir $DIR/d36
3495         chown $RUNAS_ID $DIR/d36
3496         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3497 }
3498 run_test 36c "non-root MDS utime check (mknod, utime)"
3499
3500 test_36d() {
3501         [ ! -d $DIR/d36 ] && test_36c
3502         echo "" > $DIR/d36/f36
3503         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3504 }
3505 run_test 36d "non-root OST utime check (open, utime)"
3506
3507 test_36e() {
3508         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3509
3510         test_mkdir $DIR/$tdir
3511         touch $DIR/$tdir/$tfile
3512         $RUNAS utime $DIR/$tdir/$tfile &&
3513                 error "utime worked, expected failure" || true
3514 }
3515 run_test 36e "utime on non-owned file (should return error)"
3516
3517 subr_36fh() {
3518         local fl="$1"
3519         local LANG_SAVE=$LANG
3520         local LC_LANG_SAVE=$LC_LANG
3521         export LANG=C LC_LANG=C # for date language
3522
3523         DATESTR="Dec 20  2000"
3524         test_mkdir $DIR/$tdir
3525         lctl set_param fail_loc=$fl
3526         date; date +%s
3527         cp /etc/hosts $DIR/$tdir/$tfile
3528         sync & # write RPC generated with "current" inode timestamp, but delayed
3529         sleep 1
3530         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3531         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3532         cancel_lru_locks $OSC
3533         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3534         date; date +%s
3535         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3536                 echo "BEFORE: $LS_BEFORE" && \
3537                 echo "AFTER : $LS_AFTER" && \
3538                 echo "WANT  : $DATESTR" && \
3539                 error "$DIR/$tdir/$tfile timestamps changed" || true
3540
3541         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3542 }
3543
3544 test_36f() {
3545         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3546
3547         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3548         subr_36fh "0x80000214"
3549 }
3550 run_test 36f "utime on file racing with OST BRW write =========="
3551
3552 test_36g() {
3553         remote_ost_nodsh && skip "remote OST with nodsh"
3554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3555         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3556                 skip "Need MDS version at least 2.12.51"
3557
3558         local fmd_max_age
3559         local fmd
3560         local facet="ost1"
3561         local tgt="obdfilter"
3562
3563         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3564
3565         test_mkdir $DIR/$tdir
3566         fmd_max_age=$(do_facet $facet \
3567                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3568                 head -n 1")
3569
3570         echo "FMD max age: ${fmd_max_age}s"
3571         touch $DIR/$tdir/$tfile
3572         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3573                 gawk '{cnt=cnt+$1}  END{print cnt}')
3574         echo "FMD before: $fmd"
3575         [[ $fmd == 0 ]] &&
3576                 error "FMD wasn't create by touch"
3577         sleep $((fmd_max_age + 12))
3578         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3579                 gawk '{cnt=cnt+$1}  END{print cnt}')
3580         echo "FMD after: $fmd"
3581         [[ $fmd == 0 ]] ||
3582                 error "FMD wasn't expired by ping"
3583 }
3584 run_test 36g "FMD cache expiry ====================="
3585
3586 test_36h() {
3587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3588
3589         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3590         subr_36fh "0x80000227"
3591 }
3592 run_test 36h "utime on file racing with OST BRW write =========="
3593
3594 test_36i() {
3595         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3596
3597         test_mkdir $DIR/$tdir
3598         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3599
3600         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3601         local new_mtime=$((mtime + 200))
3602
3603         #change Modify time of striped dir
3604         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3605                         error "change mtime failed"
3606
3607         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3608
3609         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3610 }
3611 run_test 36i "change mtime on striped directory"
3612
3613 # test_37 - duplicate with tests 32q 32r
3614
3615 test_38() {
3616         local file=$DIR/$tfile
3617         touch $file
3618         openfile -f O_DIRECTORY $file
3619         local RC=$?
3620         local ENOTDIR=20
3621         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3622         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3623 }
3624 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3625
3626 test_39a() { # was test_39
3627         touch $DIR/$tfile
3628         touch $DIR/${tfile}2
3629 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3630 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3631 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3632         sleep 2
3633         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3634         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3635                 echo "mtime"
3636                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3637                 echo "atime"
3638                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3639                 echo "ctime"
3640                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3641                 error "O_TRUNC didn't change timestamps"
3642         fi
3643 }
3644 run_test 39a "mtime changed on create"
3645
3646 test_39b() {
3647         test_mkdir -c1 $DIR/$tdir
3648         cp -p /etc/passwd $DIR/$tdir/fopen
3649         cp -p /etc/passwd $DIR/$tdir/flink
3650         cp -p /etc/passwd $DIR/$tdir/funlink
3651         cp -p /etc/passwd $DIR/$tdir/frename
3652         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3653
3654         sleep 1
3655         echo "aaaaaa" >> $DIR/$tdir/fopen
3656         echo "aaaaaa" >> $DIR/$tdir/flink
3657         echo "aaaaaa" >> $DIR/$tdir/funlink
3658         echo "aaaaaa" >> $DIR/$tdir/frename
3659
3660         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3661         local link_new=`stat -c %Y $DIR/$tdir/flink`
3662         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3663         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3664
3665         cat $DIR/$tdir/fopen > /dev/null
3666         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3667         rm -f $DIR/$tdir/funlink2
3668         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3669
3670         for (( i=0; i < 2; i++ )) ; do
3671                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3672                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3673                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3674                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3675
3676                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3677                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3678                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3679                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3680
3681                 cancel_lru_locks $OSC
3682                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3683         done
3684 }
3685 run_test 39b "mtime change on open, link, unlink, rename  ======"
3686
3687 # this should be set to past
3688 TEST_39_MTIME=`date -d "1 year ago" +%s`
3689
3690 # bug 11063
3691 test_39c() {
3692         touch $DIR1/$tfile
3693         sleep 2
3694         local mtime0=`stat -c %Y $DIR1/$tfile`
3695
3696         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3697         local mtime1=`stat -c %Y $DIR1/$tfile`
3698         [ "$mtime1" = $TEST_39_MTIME ] || \
3699                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3700
3701         local d1=`date +%s`
3702         echo hello >> $DIR1/$tfile
3703         local d2=`date +%s`
3704         local mtime2=`stat -c %Y $DIR1/$tfile`
3705         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3706                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3707
3708         mv $DIR1/$tfile $DIR1/$tfile-1
3709
3710         for (( i=0; i < 2; i++ )) ; do
3711                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3712                 [ "$mtime2" = "$mtime3" ] || \
3713                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3714
3715                 cancel_lru_locks $OSC
3716                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3717         done
3718 }
3719 run_test 39c "mtime change on rename ==========================="
3720
3721 # bug 21114
3722 test_39d() {
3723         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3724
3725         touch $DIR1/$tfile
3726         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3727
3728         for (( i=0; i < 2; i++ )) ; do
3729                 local mtime=`stat -c %Y $DIR1/$tfile`
3730                 [ $mtime = $TEST_39_MTIME ] || \
3731                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3732
3733                 cancel_lru_locks $OSC
3734                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3735         done
3736 }
3737 run_test 39d "create, utime, stat =============================="
3738
3739 # bug 21114
3740 test_39e() {
3741         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3742
3743         touch $DIR1/$tfile
3744         local mtime1=`stat -c %Y $DIR1/$tfile`
3745
3746         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3747
3748         for (( i=0; i < 2; i++ )) ; do
3749                 local mtime2=`stat -c %Y $DIR1/$tfile`
3750                 [ $mtime2 = $TEST_39_MTIME ] || \
3751                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3752
3753                 cancel_lru_locks $OSC
3754                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3755         done
3756 }
3757 run_test 39e "create, stat, utime, stat ========================"
3758
3759 # bug 21114
3760 test_39f() {
3761         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3762
3763         touch $DIR1/$tfile
3764         mtime1=`stat -c %Y $DIR1/$tfile`
3765
3766         sleep 2
3767         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3768
3769         for (( i=0; i < 2; i++ )) ; do
3770                 local mtime2=`stat -c %Y $DIR1/$tfile`
3771                 [ $mtime2 = $TEST_39_MTIME ] || \
3772                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3773
3774                 cancel_lru_locks $OSC
3775                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3776         done
3777 }
3778 run_test 39f "create, stat, sleep, utime, stat ================="
3779
3780 # bug 11063
3781 test_39g() {
3782         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3783
3784         echo hello >> $DIR1/$tfile
3785         local mtime1=`stat -c %Y $DIR1/$tfile`
3786
3787         sleep 2
3788         chmod o+r $DIR1/$tfile
3789
3790         for (( i=0; i < 2; i++ )) ; do
3791                 local mtime2=`stat -c %Y $DIR1/$tfile`
3792                 [ "$mtime1" = "$mtime2" ] || \
3793                         error "lost mtime: $mtime2, should be $mtime1"
3794
3795                 cancel_lru_locks $OSC
3796                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3797         done
3798 }
3799 run_test 39g "write, chmod, stat ==============================="
3800
3801 # bug 11063
3802 test_39h() {
3803         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3804
3805         touch $DIR1/$tfile
3806         sleep 1
3807
3808         local d1=`date`
3809         echo hello >> $DIR1/$tfile
3810         local mtime1=`stat -c %Y $DIR1/$tfile`
3811
3812         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3813         local d2=`date`
3814         if [ "$d1" != "$d2" ]; then
3815                 echo "write and touch not within one second"
3816         else
3817                 for (( i=0; i < 2; i++ )) ; do
3818                         local mtime2=`stat -c %Y $DIR1/$tfile`
3819                         [ "$mtime2" = $TEST_39_MTIME ] || \
3820                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3821
3822                         cancel_lru_locks $OSC
3823                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3824                 done
3825         fi
3826 }
3827 run_test 39h "write, utime within one second, stat ============="
3828
3829 test_39i() {
3830         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3831
3832         touch $DIR1/$tfile
3833         sleep 1
3834
3835         echo hello >> $DIR1/$tfile
3836         local mtime1=`stat -c %Y $DIR1/$tfile`
3837
3838         mv $DIR1/$tfile $DIR1/$tfile-1
3839
3840         for (( i=0; i < 2; i++ )) ; do
3841                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3842
3843                 [ "$mtime1" = "$mtime2" ] || \
3844                         error "lost mtime: $mtime2, should be $mtime1"
3845
3846                 cancel_lru_locks $OSC
3847                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3848         done
3849 }
3850 run_test 39i "write, rename, stat =============================="
3851
3852 test_39j() {
3853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3854
3855         start_full_debug_logging
3856         touch $DIR1/$tfile
3857         sleep 1
3858
3859         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3860         lctl set_param fail_loc=0x80000412
3861         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3862                 error "multiop failed"
3863         local multipid=$!
3864         local mtime1=`stat -c %Y $DIR1/$tfile`
3865
3866         mv $DIR1/$tfile $DIR1/$tfile-1
3867
3868         kill -USR1 $multipid
3869         wait $multipid || error "multiop close failed"
3870
3871         for (( i=0; i < 2; i++ )) ; do
3872                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3873                 [ "$mtime1" = "$mtime2" ] ||
3874                         error "mtime is lost on close: $mtime2, " \
3875                               "should be $mtime1"
3876
3877                 cancel_lru_locks $OSC
3878                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3879         done
3880         lctl set_param fail_loc=0
3881         stop_full_debug_logging
3882 }
3883 run_test 39j "write, rename, close, stat ======================="
3884
3885 test_39k() {
3886         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3887
3888         touch $DIR1/$tfile
3889         sleep 1
3890
3891         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3892         local multipid=$!
3893         local mtime1=`stat -c %Y $DIR1/$tfile`
3894
3895         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3896
3897         kill -USR1 $multipid
3898         wait $multipid || error "multiop close failed"
3899
3900         for (( i=0; i < 2; i++ )) ; do
3901                 local mtime2=`stat -c %Y $DIR1/$tfile`
3902
3903                 [ "$mtime2" = $TEST_39_MTIME ] || \
3904                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3905
3906                 cancel_lru_locks osc
3907                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3908         done
3909 }
3910 run_test 39k "write, utime, close, stat ========================"
3911
3912 # this should be set to future
3913 TEST_39_ATIME=`date -d "1 year" +%s`
3914
3915 test_39l() {
3916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3917         remote_mds_nodsh && skip "remote MDS with nodsh"
3918
3919         local atime_diff=$(do_facet $SINGLEMDS \
3920                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3921         rm -rf $DIR/$tdir
3922         mkdir -p $DIR/$tdir
3923
3924         # test setting directory atime to future
3925         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3926         local atime=$(stat -c %X $DIR/$tdir)
3927         [ "$atime" = $TEST_39_ATIME ] ||
3928                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3929
3930         # test setting directory atime from future to now
3931         local now=$(date +%s)
3932         touch -a -d @$now $DIR/$tdir
3933
3934         atime=$(stat -c %X $DIR/$tdir)
3935         [ "$atime" -eq "$now"  ] ||
3936                 error "atime is not updated from future: $atime, $now"
3937
3938         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3939         sleep 3
3940
3941         # test setting directory atime when now > dir atime + atime_diff
3942         local d1=$(date +%s)
3943         ls $DIR/$tdir
3944         local d2=$(date +%s)
3945         cancel_lru_locks mdc
3946         atime=$(stat -c %X $DIR/$tdir)
3947         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3948                 error "atime is not updated  : $atime, should be $d2"
3949
3950         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3951         sleep 3
3952
3953         # test not setting directory atime when now < dir atime + atime_diff
3954         ls $DIR/$tdir
3955         cancel_lru_locks mdc
3956         atime=$(stat -c %X $DIR/$tdir)
3957         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3958                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3959
3960         do_facet $SINGLEMDS \
3961                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3962 }
3963 run_test 39l "directory atime update ==========================="
3964
3965 test_39m() {
3966         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3967
3968         touch $DIR1/$tfile
3969         sleep 2
3970         local far_past_mtime=$(date -d "May 29 1953" +%s)
3971         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3972
3973         touch -m -d @$far_past_mtime $DIR1/$tfile
3974         touch -a -d @$far_past_atime $DIR1/$tfile
3975
3976         for (( i=0; i < 2; i++ )) ; do
3977                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3978                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3979                         error "atime or mtime set incorrectly"
3980
3981                 cancel_lru_locks $OSC
3982                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3983         done
3984 }
3985 run_test 39m "test atime and mtime before 1970"
3986
3987 test_39n() { # LU-3832
3988         remote_mds_nodsh && skip "remote MDS with nodsh"
3989
3990         local atime_diff=$(do_facet $SINGLEMDS \
3991                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3992         local atime0
3993         local atime1
3994         local atime2
3995
3996         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3997
3998         rm -rf $DIR/$tfile
3999         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4000         atime0=$(stat -c %X $DIR/$tfile)
4001
4002         sleep 5
4003         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4004         atime1=$(stat -c %X $DIR/$tfile)
4005
4006         sleep 5
4007         cancel_lru_locks mdc
4008         cancel_lru_locks osc
4009         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4010         atime2=$(stat -c %X $DIR/$tfile)
4011
4012         do_facet $SINGLEMDS \
4013                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4014
4015         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4016         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4017 }
4018 run_test 39n "check that O_NOATIME is honored"
4019
4020 test_39o() {
4021         TESTDIR=$DIR/$tdir/$tfile
4022         [ -e $TESTDIR ] && rm -rf $TESTDIR
4023         mkdir -p $TESTDIR
4024         cd $TESTDIR
4025         links1=2
4026         ls
4027         mkdir a b
4028         ls
4029         links2=$(stat -c %h .)
4030         [ $(($links1 + 2)) != $links2 ] &&
4031                 error "wrong links count $(($links1 + 2)) != $links2"
4032         rmdir b
4033         links3=$(stat -c %h .)
4034         [ $(($links1 + 1)) != $links3 ] &&
4035                 error "wrong links count $links1 != $links3"
4036         return 0
4037 }
4038 run_test 39o "directory cached attributes updated after create"
4039
4040 test_39p() {
4041         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4042
4043         local MDTIDX=1
4044         TESTDIR=$DIR/$tdir/$tdir
4045         [ -e $TESTDIR ] && rm -rf $TESTDIR
4046         test_mkdir -p $TESTDIR
4047         cd $TESTDIR
4048         links1=2
4049         ls
4050         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4051         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4052         ls
4053         links2=$(stat -c %h .)
4054         [ $(($links1 + 2)) != $links2 ] &&
4055                 error "wrong links count $(($links1 + 2)) != $links2"
4056         rmdir remote_dir2
4057         links3=$(stat -c %h .)
4058         [ $(($links1 + 1)) != $links3 ] &&
4059                 error "wrong links count $links1 != $links3"
4060         return 0
4061 }
4062 run_test 39p "remote directory cached attributes updated after create ========"
4063
4064
4065 test_39q() { # LU-8041
4066         local testdir=$DIR/$tdir
4067         mkdir -p $testdir
4068         multiop_bg_pause $testdir D_c || error "multiop failed"
4069         local multipid=$!
4070         cancel_lru_locks mdc
4071         kill -USR1 $multipid
4072         local atime=$(stat -c %X $testdir)
4073         [ "$atime" -ne 0 ] || error "atime is zero"
4074 }
4075 run_test 39q "close won't zero out atime"
4076
4077 test_40() {
4078         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4079         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4080                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4081         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4082                 error "$tfile is not 4096 bytes in size"
4083 }
4084 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4085
4086 test_41() {
4087         # bug 1553
4088         small_write $DIR/f41 18
4089 }
4090 run_test 41 "test small file write + fstat ====================="
4091
4092 count_ost_writes() {
4093         lctl get_param -n ${OSC}.*.stats |
4094                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4095                         END { printf("%0.0f", writes) }'
4096 }
4097
4098 # decent default
4099 WRITEBACK_SAVE=500
4100 DIRTY_RATIO_SAVE=40
4101 MAX_DIRTY_RATIO=50
4102 BG_DIRTY_RATIO_SAVE=10
4103 MAX_BG_DIRTY_RATIO=25
4104
4105 start_writeback() {
4106         trap 0
4107         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4108         # dirty_ratio, dirty_background_ratio
4109         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4110                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4111                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4112                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4113         else
4114                 # if file not here, we are a 2.4 kernel
4115                 kill -CONT `pidof kupdated`
4116         fi
4117 }
4118
4119 stop_writeback() {
4120         # setup the trap first, so someone cannot exit the test at the
4121         # exact wrong time and mess up a machine
4122         trap start_writeback EXIT
4123         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4124         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4125                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4126                 sysctl -w vm.dirty_writeback_centisecs=0
4127                 sysctl -w vm.dirty_writeback_centisecs=0
4128                 # save and increase /proc/sys/vm/dirty_ratio
4129                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4130                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4131                 # save and increase /proc/sys/vm/dirty_background_ratio
4132                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4133                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4134         else
4135                 # if file not here, we are a 2.4 kernel
4136                 kill -STOP `pidof kupdated`
4137         fi
4138 }
4139
4140 # ensure that all stripes have some grant before we test client-side cache
4141 setup_test42() {
4142         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4143                 dd if=/dev/zero of=$i bs=4k count=1
4144                 rm $i
4145         done
4146 }
4147
4148 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4149 # file truncation, and file removal.
4150 test_42a() {
4151         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4152
4153         setup_test42
4154         cancel_lru_locks $OSC
4155         stop_writeback
4156         sync; sleep 1; sync # just to be safe
4157         BEFOREWRITES=`count_ost_writes`
4158         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4159         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4160         AFTERWRITES=`count_ost_writes`
4161         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4162                 error "$BEFOREWRITES < $AFTERWRITES"
4163         start_writeback
4164 }
4165 run_test 42a "ensure that we don't flush on close"
4166
4167 test_42b() {
4168         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4169
4170         setup_test42
4171         cancel_lru_locks $OSC
4172         stop_writeback
4173         sync
4174         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4175         BEFOREWRITES=$(count_ost_writes)
4176         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4177         AFTERWRITES=$(count_ost_writes)
4178         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4179                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4180         fi
4181         BEFOREWRITES=$(count_ost_writes)
4182         sync || error "sync: $?"
4183         AFTERWRITES=$(count_ost_writes)
4184         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4185                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4186         fi
4187         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4188         start_writeback
4189         return 0
4190 }
4191 run_test 42b "test destroy of file with cached dirty data ======"
4192
4193 # if these tests just want to test the effect of truncation,
4194 # they have to be very careful.  consider:
4195 # - the first open gets a {0,EOF}PR lock
4196 # - the first write conflicts and gets a {0, count-1}PW
4197 # - the rest of the writes are under {count,EOF}PW
4198 # - the open for truncate tries to match a {0,EOF}PR
4199 #   for the filesize and cancels the PWs.
4200 # any number of fixes (don't get {0,EOF} on open, match
4201 # composite locks, do smarter file size management) fix
4202 # this, but for now we want these tests to verify that
4203 # the cancellation with truncate intent works, so we
4204 # start the file with a full-file pw lock to match against
4205 # until the truncate.
4206 trunc_test() {
4207         test=$1
4208         file=$DIR/$test
4209         offset=$2
4210         cancel_lru_locks $OSC
4211         stop_writeback
4212         # prime the file with 0,EOF PW to match
4213         touch $file
4214         $TRUNCATE $file 0
4215         sync; sync
4216         # now the real test..
4217         dd if=/dev/zero of=$file bs=1024 count=100
4218         BEFOREWRITES=`count_ost_writes`
4219         $TRUNCATE $file $offset
4220         cancel_lru_locks $OSC
4221         AFTERWRITES=`count_ost_writes`
4222         start_writeback
4223 }
4224
4225 test_42c() {
4226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4227
4228         trunc_test 42c 1024
4229         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4230                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4231         rm $file
4232 }
4233 run_test 42c "test partial truncate of file with cached dirty data"
4234
4235 test_42d() {
4236         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4237
4238         trunc_test 42d 0
4239         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4240                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4241         rm $file
4242 }
4243 run_test 42d "test complete truncate of file with cached dirty data"
4244
4245 test_42e() { # bug22074
4246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4247
4248         local TDIR=$DIR/${tdir}e
4249         local pages=16 # hardcoded 16 pages, don't change it.
4250         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4251         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4252         local max_dirty_mb
4253         local warmup_files
4254
4255         test_mkdir $DIR/${tdir}e
4256         $SETSTRIPE -c 1 $TDIR
4257         createmany -o $TDIR/f $files
4258
4259         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4260
4261         # we assume that with $OSTCOUNT files, at least one of them will
4262         # be allocated on OST0.
4263         warmup_files=$((OSTCOUNT * max_dirty_mb))
4264         createmany -o $TDIR/w $warmup_files
4265
4266         # write a large amount of data into one file and sync, to get good
4267         # avail_grant number from OST.
4268         for ((i=0; i<$warmup_files; i++)); do
4269                 idx=$($GETSTRIPE -i $TDIR/w$i)
4270                 [ $idx -ne 0 ] && continue
4271                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4272                 break
4273         done
4274         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4275         sync
4276         $LCTL get_param $proc_osc0/cur_dirty_bytes
4277         $LCTL get_param $proc_osc0/cur_grant_bytes
4278
4279         # create as much dirty pages as we can while not to trigger the actual
4280         # RPCs directly. but depends on the env, VFS may trigger flush during this
4281         # period, hopefully we are good.
4282         for ((i=0; i<$warmup_files; i++)); do
4283                 idx=$($GETSTRIPE -i $TDIR/w$i)
4284                 [ $idx -ne 0 ] && continue
4285                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4286         done
4287         $LCTL get_param $proc_osc0/cur_dirty_bytes
4288         $LCTL get_param $proc_osc0/cur_grant_bytes
4289
4290         # perform the real test
4291         $LCTL set_param $proc_osc0/rpc_stats 0
4292         for ((;i<$files; i++)); do
4293                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
4294                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4295         done
4296         sync
4297         $LCTL get_param $proc_osc0/rpc_stats
4298
4299         local percent=0
4300         local have_ppr=false
4301         $LCTL get_param $proc_osc0/rpc_stats |
4302                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4303                         # skip lines until we are at the RPC histogram data
4304                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4305                         $have_ppr || continue
4306
4307                         # we only want the percent stat for < 16 pages
4308                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4309
4310                         percent=$((percent + WPCT))
4311                         if [[ $percent -gt 15 ]]; then
4312                                 error "less than 16-pages write RPCs" \
4313                                       "$percent% > 15%"
4314                                 break
4315                         fi
4316                 done
4317         rm -rf $TDIR
4318 }
4319 run_test 42e "verify sub-RPC writes are not done synchronously"
4320
4321 test_43A() { # was test_43
4322         test_mkdir $DIR/$tdir
4323         cp -p /bin/ls $DIR/$tdir/$tfile
4324         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4325         pid=$!
4326         # give multiop a chance to open
4327         sleep 1
4328
4329         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4330         kill -USR1 $pid
4331 }
4332 run_test 43A "execution of file opened for write should return -ETXTBSY"
4333
4334 test_43a() {
4335         test_mkdir $DIR/$tdir
4336         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4337         $DIR/$tdir/sleep 60 &
4338         SLEEP_PID=$!
4339         # Make sure exec of $tdir/sleep wins race with truncate
4340         sleep 1
4341         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4342         kill $SLEEP_PID
4343 }
4344 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4345
4346 test_43b() {
4347         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4348
4349         test_mkdir $DIR/$tdir
4350         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4351         $DIR/$tdir/sleep 60 &
4352         SLEEP_PID=$!
4353         # Make sure exec of $tdir/sleep wins race with truncate
4354         sleep 1
4355         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4356         kill $SLEEP_PID
4357 }
4358 run_test 43b "truncate of file being executed should return -ETXTBSY"
4359
4360 test_43c() {
4361         local testdir="$DIR/$tdir"
4362         test_mkdir $testdir
4363         cp $SHELL $testdir/
4364         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4365                 ( cd $testdir && md5sum -c )
4366 }
4367 run_test 43c "md5sum of copy into lustre"
4368
4369 test_44A() { # was test_44
4370         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4371
4372         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4373         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4374 }
4375 run_test 44A "zero length read from a sparse stripe"
4376
4377 test_44a() {
4378         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4379                 awk '{ print $2 }')
4380         [ -z "$nstripe" ] && skip "can't get stripe info"
4381         [[ $nstripe -gt $OSTCOUNT ]] &&
4382                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4383
4384         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4385                 awk '{ print $2 }')
4386         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4387                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4388                         awk '{ print $2 }')
4389         fi
4390
4391         OFFSETS="0 $((stride/2)) $((stride-1))"
4392         for offset in $OFFSETS; do
4393                 for i in $(seq 0 $((nstripe-1))); do
4394                         local GLOBALOFFSETS=""
4395                         # size in Bytes
4396                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4397                         local myfn=$DIR/d44a-$size
4398                         echo "--------writing $myfn at $size"
4399                         ll_sparseness_write $myfn $size ||
4400                                 error "ll_sparseness_write"
4401                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4402                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4403                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4404
4405                         for j in $(seq 0 $((nstripe-1))); do
4406                                 # size in Bytes
4407                                 size=$((((j + $nstripe )*$stride + $offset)))
4408                                 ll_sparseness_write $myfn $size ||
4409                                         error "ll_sparseness_write"
4410                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4411                         done
4412                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4413                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4414                         rm -f $myfn
4415                 done
4416         done
4417 }
4418 run_test 44a "test sparse pwrite ==============================="
4419
4420 dirty_osc_total() {
4421         tot=0
4422         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4423                 tot=$(($tot + $d))
4424         done
4425         echo $tot
4426 }
4427 do_dirty_record() {
4428         before=`dirty_osc_total`
4429         echo executing "\"$*\""
4430         eval $*
4431         after=`dirty_osc_total`
4432         echo before $before, after $after
4433 }
4434 test_45() {
4435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4436
4437         f="$DIR/f45"
4438         # Obtain grants from OST if it supports it
4439         echo blah > ${f}_grant
4440         stop_writeback
4441         sync
4442         do_dirty_record "echo blah > $f"
4443         [[ $before -eq $after ]] && error "write wasn't cached"
4444         do_dirty_record "> $f"
4445         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4446         do_dirty_record "echo blah > $f"
4447         [[ $before -eq $after ]] && error "write wasn't cached"
4448         do_dirty_record "sync"
4449         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4450         do_dirty_record "echo blah > $f"
4451         [[ $before -eq $after ]] && error "write wasn't cached"
4452         do_dirty_record "cancel_lru_locks osc"
4453         [[ $before -gt $after ]] ||
4454                 error "lock cancellation didn't lower dirty count"
4455         start_writeback
4456 }
4457 run_test 45 "osc io page accounting ============================"
4458
4459 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4460 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4461 # objects offset and an assert hit when an rpc was built with 1023's mapped
4462 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4463 test_46() {
4464         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4465
4466         f="$DIR/f46"
4467         stop_writeback
4468         sync
4469         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4470         sync
4471         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4472         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4473         sync
4474         start_writeback
4475 }
4476 run_test 46 "dirtying a previously written page ================"
4477
4478 # test_47 is removed "Device nodes check" is moved to test_28
4479
4480 test_48a() { # bug 2399
4481         [ "$mds1_FSTYPE" = "zfs" ] &&
4482         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4483                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4484
4485         test_mkdir $DIR/$tdir
4486         cd $DIR/$tdir
4487         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4488         test_mkdir $DIR/$tdir
4489         touch foo || error "'touch foo' failed after recreating cwd"
4490         test_mkdir bar
4491         touch .foo || error "'touch .foo' failed after recreating cwd"
4492         test_mkdir .bar
4493         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4494         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4495         cd . || error "'cd .' failed after recreating cwd"
4496         mkdir . && error "'mkdir .' worked after recreating cwd"
4497         rmdir . && error "'rmdir .' worked after recreating cwd"
4498         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4499         cd .. || error "'cd ..' failed after recreating cwd"
4500 }
4501 run_test 48a "Access renamed working dir (should return errors)="
4502
4503 test_48b() { # bug 2399
4504         rm -rf $DIR/$tdir
4505         test_mkdir $DIR/$tdir
4506         cd $DIR/$tdir
4507         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4508         touch foo && error "'touch foo' worked after removing cwd"
4509         mkdir foo && error "'mkdir foo' worked after removing cwd"
4510         touch .foo && error "'touch .foo' worked after removing cwd"
4511         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4512         ls . > /dev/null && error "'ls .' worked after removing cwd"
4513         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4514         mkdir . && error "'mkdir .' worked after removing cwd"
4515         rmdir . && error "'rmdir .' worked after removing cwd"
4516         ln -s . foo && error "'ln -s .' worked after removing cwd"
4517         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4518 }
4519 run_test 48b "Access removed working dir (should return errors)="
4520
4521 test_48c() { # bug 2350
4522         #lctl set_param debug=-1
4523         #set -vx
4524         rm -rf $DIR/$tdir
4525         test_mkdir -p $DIR/$tdir/dir
4526         cd $DIR/$tdir/dir
4527         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4528         $TRACE touch foo && error "touch foo worked after removing cwd"
4529         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4530         touch .foo && error "touch .foo worked after removing cwd"
4531         mkdir .foo && error "mkdir .foo worked after removing cwd"
4532         $TRACE ls . && error "'ls .' worked after removing cwd"
4533         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4534         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4535         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4536         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4537         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4538 }
4539 run_test 48c "Access removed working subdir (should return errors)"
4540
4541 test_48d() { # bug 2350
4542         #lctl set_param debug=-1
4543         #set -vx
4544         rm -rf $DIR/$tdir
4545         test_mkdir -p $DIR/$tdir/dir
4546         cd $DIR/$tdir/dir
4547         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4548         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4549         $TRACE touch foo && error "'touch foo' worked after removing parent"
4550         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4551         touch .foo && error "'touch .foo' worked after removing parent"
4552         mkdir .foo && error "mkdir .foo worked after removing parent"
4553         $TRACE ls . && error "'ls .' worked after removing parent"
4554         $TRACE ls .. && error "'ls ..' worked after removing parent"
4555         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4556         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4557         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4558         true
4559 }
4560 run_test 48d "Access removed parent subdir (should return errors)"
4561
4562 test_48e() { # bug 4134
4563         #lctl set_param debug=-1
4564         #set -vx
4565         rm -rf $DIR/$tdir
4566         test_mkdir -p $DIR/$tdir/dir
4567         cd $DIR/$tdir/dir
4568         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4569         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4570         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4571         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4572         # On a buggy kernel addition of "touch foo" after cd .. will
4573         # produce kernel oops in lookup_hash_it
4574         touch ../foo && error "'cd ..' worked after recreate parent"
4575         cd $DIR
4576         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4577 }
4578 run_test 48e "Access to recreated parent subdir (should return errors)"
4579
4580 test_49() { # LU-1030
4581         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4582         remote_ost_nodsh && skip "remote OST with nodsh"
4583
4584         # get ost1 size - lustre-OST0000
4585         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4586                 awk '{ print $4 }')
4587         # write 800M at maximum
4588         [[ $ost1_size -lt 2 ]] && ost1_size=2
4589         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4590
4591         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4592         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4593         local dd_pid=$!
4594
4595         # change max_pages_per_rpc while writing the file
4596         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4597         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4598         # loop until dd process exits
4599         while ps ax -opid | grep -wq $dd_pid; do
4600                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4601                 sleep $((RANDOM % 5 + 1))
4602         done
4603         # restore original max_pages_per_rpc
4604         $LCTL set_param $osc1_mppc=$orig_mppc
4605         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4606 }
4607 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4608
4609 test_50() {
4610         # bug 1485
4611         test_mkdir $DIR/$tdir
4612         cd $DIR/$tdir
4613         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4614 }
4615 run_test 50 "special situations: /proc symlinks  ==============="
4616
4617 test_51a() {    # was test_51
4618         # bug 1516 - create an empty entry right after ".." then split dir
4619         test_mkdir -c1 $DIR/$tdir
4620         touch $DIR/$tdir/foo
4621         $MCREATE $DIR/$tdir/bar
4622         rm $DIR/$tdir/foo
4623         createmany -m $DIR/$tdir/longfile 201
4624         FNUM=202
4625         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4626                 $MCREATE $DIR/$tdir/longfile$FNUM
4627                 FNUM=$(($FNUM + 1))
4628                 echo -n "+"
4629         done
4630         echo
4631         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4632 }
4633 run_test 51a "special situations: split htree with empty entry =="
4634
4635 cleanup_print_lfs_df () {
4636         trap 0
4637         $LFS df
4638         $LFS df -i
4639 }
4640
4641 test_51b() {
4642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4643
4644         local dir=$DIR/$tdir
4645         local nrdirs=$((65536 + 100))
4646
4647         # cleanup the directory
4648         rm -fr $dir
4649
4650         test_mkdir -c1 $dir
4651
4652         $LFS df
4653         $LFS df -i
4654         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
4655         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4656         [[ $numfree -lt $nrdirs ]] &&
4657                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
4658
4659         # need to check free space for the directories as well
4660         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4661         numfree=$(( blkfree / $(fs_inode_ksize) ))
4662         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
4663
4664         trap cleanup_print_lfs_df EXIT
4665
4666         # create files
4667         createmany -d $dir/d $nrdirs || {
4668                 unlinkmany $dir/d $nrdirs
4669                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4670         }
4671
4672         # really created :
4673         nrdirs=$(ls -U $dir | wc -l)
4674
4675         # unlink all but 100 subdirectories, then check it still works
4676         local left=100
4677         local delete=$((nrdirs - left))
4678
4679         $LFS df
4680         $LFS df -i
4681
4682         # for ldiskfs the nlink count should be 1, but this is OSD specific
4683         # and so this is listed for informational purposes only
4684         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4685         unlinkmany -d $dir/d $delete ||
4686                 error "unlink of first $delete subdirs failed"
4687
4688         echo "nlink between: $(stat -c %h $dir)"
4689         local found=$(ls -U $dir | wc -l)
4690         [ $found -ne $left ] &&
4691                 error "can't find subdirs: found only $found, expected $left"
4692
4693         unlinkmany -d $dir/d $delete $left ||
4694                 error "unlink of second $left subdirs failed"
4695         # regardless of whether the backing filesystem tracks nlink accurately
4696         # or not, the nlink count shouldn't be more than "." and ".." here
4697         local after=$(stat -c %h $dir)
4698         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4699                 echo "nlink after: $after"
4700
4701         cleanup_print_lfs_df
4702 }
4703 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4704
4705 test_51d() {
4706         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4707         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
4708
4709         test_mkdir $DIR/$tdir
4710         createmany -o $DIR/$tdir/t- 1000
4711         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4712         for N in $(seq 0 $((OSTCOUNT - 1))); do
4713                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4714                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4715                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4716                         '($1 == '$N') { objs += 1 } \
4717                         END { printf("%0.0f", objs) }')
4718                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4719         done
4720         unlinkmany $DIR/$tdir/t- 1000
4721
4722         NLAST=0
4723         for N in $(seq 1 $((OSTCOUNT - 1))); do
4724                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4725                         error "OST $N has less objects vs OST $NLAST" \
4726                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4727                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4728                         error "OST $N has less objects vs OST $NLAST" \
4729                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4730
4731                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4732                         error "OST $N has less #0 objects vs OST $NLAST" \
4733                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4734                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4735                         error "OST $N has less #0 objects vs OST $NLAST" \
4736                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4737                 NLAST=$N
4738         done
4739         rm -f $TMP/$tfile
4740 }
4741 run_test 51d "check object distribution"
4742
4743 test_51e() {
4744         if [ "$mds1_FSTYPE" != ldiskfs ]; then
4745                 skip_env "ldiskfs only test"
4746         fi
4747
4748         test_mkdir -c1 $DIR/$tdir
4749         test_mkdir -c1 $DIR/$tdir/d0
4750
4751         touch $DIR/$tdir/d0/foo
4752         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4753                 error "file exceed 65000 nlink limit!"
4754         unlinkmany $DIR/$tdir/d0/f- 65001
4755         return 0
4756 }
4757 run_test 51e "check file nlink limit"
4758
4759 test_51f() {
4760         test_mkdir $DIR/$tdir
4761
4762         local max=100000
4763         local ulimit_old=$(ulimit -n)
4764         local spare=20 # number of spare fd's for scripts/libraries, etc.
4765         local mdt=$($LFS getstripe -m $DIR/$tdir)
4766         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4767
4768         echo "MDT$mdt numfree=$numfree, max=$max"
4769         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4770         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4771                 while ! ulimit -n $((numfree + spare)); do
4772                         numfree=$((numfree * 3 / 4))
4773                 done
4774                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4775         else
4776                 echo "left ulimit at $ulimit_old"
4777         fi
4778
4779         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4780                 unlinkmany $DIR/$tdir/f $numfree
4781                 error "create+open $numfree files in $DIR/$tdir failed"
4782         }
4783         ulimit -n $ulimit_old
4784
4785         # if createmany exits at 120s there will be fewer than $numfree files
4786         unlinkmany $DIR/$tdir/f $numfree || true
4787 }
4788 run_test 51f "check many open files limit"
4789
4790 test_52a() {
4791         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4792         test_mkdir $DIR/$tdir
4793         touch $DIR/$tdir/foo
4794         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4795         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4796         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4797         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4798         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4799                                         error "link worked"
4800         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4801         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4802         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4803                                                      error "lsattr"
4804         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4805         cp -r $DIR/$tdir $TMP/
4806         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4807 }
4808 run_test 52a "append-only flag test (should return errors)"
4809
4810 test_52b() {
4811         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4812         test_mkdir $DIR/$tdir
4813         touch $DIR/$tdir/foo
4814         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4815         cat test > $DIR/$tdir/foo && error "cat test worked"
4816         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4817         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4818         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4819                                         error "link worked"
4820         echo foo >> $DIR/$tdir/foo && error "echo worked"
4821         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4822         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4823         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4824         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4825                                                         error "lsattr"
4826         chattr -i $DIR/$tdir/foo || error "chattr failed"
4827
4828         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4829 }
4830 run_test 52b "immutable flag test (should return errors) ======="
4831
4832 test_53() {
4833         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4834         remote_mds_nodsh && skip "remote MDS with nodsh"
4835         remote_ost_nodsh && skip "remote OST with nodsh"
4836
4837         local param
4838         local param_seq
4839         local ostname
4840         local mds_last
4841         local mds_last_seq
4842         local ost_last
4843         local ost_last_seq
4844         local ost_last_id
4845         local ostnum
4846         local node
4847         local found=false
4848         local support_last_seq=true
4849
4850         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
4851                 support_last_seq=false
4852
4853         # only test MDT0000
4854         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4855         local value
4856         for value in $(do_facet $SINGLEMDS \
4857                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
4858                 param=$(echo ${value[0]} | cut -d "=" -f1)
4859                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4860
4861                 if $support_last_seq; then
4862                         param_seq=$(echo $param |
4863                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4864                         mds_last_seq=$(do_facet $SINGLEMDS \
4865                                        $LCTL get_param -n $param_seq)
4866                 fi
4867                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4868
4869                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4870                 node=$(facet_active_host ost$((ostnum+1)))
4871                 param="obdfilter.$ostname.last_id"
4872                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4873                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4874                         ost_last_id=$ost_last
4875
4876                         if $support_last_seq; then
4877                                 ost_last_id=$(echo $ost_last |
4878                                               awk -F':' '{print $2}' |
4879                                               sed -e "s/^0x//g")
4880                                 ost_last_seq=$(echo $ost_last |
4881                                                awk -F':' '{print $1}')
4882                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4883                         fi
4884
4885                         if [[ $ost_last_id != $mds_last ]]; then
4886                                 error "$ost_last_id != $mds_last"
4887                         else
4888                                 found=true
4889                                 break
4890                         fi
4891                 done
4892         done
4893         $found || error "can not match last_seq/last_id for $mdtosc"
4894         return 0
4895 }
4896 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4897
4898 test_54a() {
4899         perl -MSocket -e ';' || skip "no Socket perl module installed"
4900
4901         $SOCKETSERVER $DIR/socket ||
4902                 error "$SOCKETSERVER $DIR/socket failed: $?"
4903         $SOCKETCLIENT $DIR/socket ||
4904                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4905         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4906 }
4907 run_test 54a "unix domain socket test =========================="
4908
4909 test_54b() {
4910         f="$DIR/f54b"
4911         mknod $f c 1 3
4912         chmod 0666 $f
4913         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
4914 }
4915 run_test 54b "char device works in lustre ======================"
4916
4917 find_loop_dev() {
4918         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4919         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4920         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4921
4922         for i in $(seq 3 7); do
4923                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4924                 LOOPDEV=$LOOPBASE$i
4925                 LOOPNUM=$i
4926                 break
4927         done
4928 }
4929
4930 cleanup_54c() {
4931         local rc=0
4932         loopdev="$DIR/loop54c"
4933
4934         trap 0
4935         $UMOUNT $DIR/$tdir || rc=$?
4936         losetup -d $loopdev || true
4937         losetup -d $LOOPDEV || true
4938         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4939         return $rc
4940 }
4941
4942 test_54c() {
4943         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4944
4945         loopdev="$DIR/loop54c"
4946
4947         find_loop_dev
4948         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
4949         trap cleanup_54c EXIT
4950         mknod $loopdev b 7 $LOOPNUM
4951         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4952         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
4953         losetup $loopdev $DIR/$tfile ||
4954                 error "can't set up $loopdev for $DIR/$tfile"
4955         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4956         test_mkdir $DIR/$tdir
4957         mount -t ext2 $loopdev $DIR/$tdir ||
4958                 error "error mounting $loopdev on $DIR/$tdir"
4959         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
4960                 error "dd write"
4961         df $DIR/$tdir
4962         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
4963                 error "dd read"
4964         cleanup_54c
4965 }
4966 run_test 54c "block device works in lustre ====================="
4967
4968 test_54d() {
4969         f="$DIR/f54d"
4970         string="aaaaaa"
4971         mknod $f p
4972         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4973 }
4974 run_test 54d "fifo device works in lustre ======================"
4975
4976 test_54e() {
4977         f="$DIR/f54e"
4978         string="aaaaaa"
4979         cp -aL /dev/console $f
4980         echo $string > $f || error "echo $string to $f failed"
4981 }
4982 run_test 54e "console/tty device works in lustre ======================"
4983
4984 test_56a() {
4985         local numfiles=3
4986         local dir=$DIR/$tdir
4987
4988         rm -rf $dir
4989         test_mkdir -p $dir/dir
4990         for i in $(seq $numfiles); do
4991                 touch $dir/file$i
4992                 touch $dir/dir/file$i
4993         done
4994
4995         local numcomp=$($LFS getstripe --component-count $dir)
4996
4997         [[ $numcomp == 0 ]] && numcomp=1
4998
4999         # test lfs getstripe with --recursive
5000         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5001
5002         [[ $filenum -eq $((numfiles * 2)) ]] ||
5003                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5004         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5005         [[ $filenum -eq $numfiles ]] ||
5006                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5007         echo "$LFS getstripe showed obdidx or l_ost_idx"
5008
5009         # test lfs getstripe with file instead of dir
5010         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5011         [[ $filenum -eq 1 ]] ||
5012                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5013         echo "$LFS getstripe file1 passed"
5014
5015         #test lfs getstripe with --verbose
5016         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5017         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5018                 error "$LFS getstripe --verbose $dir: "\
5019                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5020         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5021                 error "$LFS getstripe $dir: showed lmm_magic"
5022
5023         #test lfs getstripe with -v prints lmm_fid
5024         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5025         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5026                 error "$LFS getstripe -v $dir: "\
5027                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5028         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5029                 error "$LFS getstripe $dir: showed lmm_fid by default"
5030         echo "$LFS getstripe --verbose passed"
5031
5032         #check for FID information
5033         local fid1=$($LFS getstripe --fid $dir/file1)
5034         local fid2=$($LFS getstripe --verbose $dir/file1 |
5035                      awk '/lmm_fid: / { print $2; exit; }')
5036         local fid3=$($LFS path2fid $dir/file1)
5037
5038         [ "$fid1" != "$fid2" ] &&
5039                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5040         [ "$fid1" != "$fid3" ] &&
5041                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5042         echo "$LFS getstripe --fid passed"
5043
5044         #test lfs getstripe with --obd
5045         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5046                 error "$LFS getstripe --obd wrong_uuid: should return error"
5047
5048         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5049
5050         local ostidx=1
5051         local obduuid=$(ostuuid_from_index $ostidx)
5052         local found=$($LFS getstripe -r --obd $obduuid $dir |
5053                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5054
5055         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5056         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5057                 ((filenum--))
5058         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5059                 ((filenum--))
5060
5061         [[ $found -eq $filenum ]] ||
5062                 error "$LFS getstripe --obd: found $found expect $filenum"
5063         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5064                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5065                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5066                 error "$LFS getstripe --obd: should not show file on other obd"
5067         echo "$LFS getstripe --obd passed"
5068 }
5069 run_test 56a "check $LFS getstripe"
5070
5071 test_56b() {
5072         local dir=$DIR/$tdir
5073         local numdirs=3
5074
5075         test_mkdir $dir
5076         for i in $(seq $numdirs); do
5077                 test_mkdir $dir/dir$i
5078         done
5079
5080         # test lfs getdirstripe default mode is non-recursion, which is
5081         # different from lfs getstripe
5082         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5083
5084         [[ $dircnt -eq 1 ]] ||
5085                 error "$LFS getdirstripe: found $dircnt, not 1"
5086         dircnt=$($LFS getdirstripe --recursive $dir |
5087                 grep -c lmv_stripe_count)
5088         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5089                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5090 }
5091 run_test 56b "check $LFS getdirstripe"
5092
5093 test_56c() {
5094         remote_ost_nodsh && skip "remote OST with nodsh"
5095
5096         local ost_idx=0
5097         local ost_name=$(ostname_from_index $ost_idx)
5098         local old_status=$(ost_dev_status $ost_idx)
5099
5100         [[ -z "$old_status" ]] ||
5101                 skip_env "OST $ost_name is in $old_status status"
5102
5103         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5104         [[ $OST1_VERSION -ge $(version_code 2.12.3) ]] && do_facet ost1 \
5105                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5106         sleep_maxage
5107
5108         local new_status=$(ost_dev_status $ost_idx)
5109
5110         [[ "$new_status" =~ "D" ]] ||
5111                 error "$ost_name status is '$new_status', missing 'D'"
5112         if [[ $OST1_VERSION -ge $(version_code 2.12.3) ]]; then
5113                 [[ "$new_status" =~ "N" ]] ||
5114                         error "$ost_name status is '$new_status', missing 'N'"
5115         fi
5116
5117         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5118         [[ $OST1_VERSION -ge $(version_code 2.12.3) ]] && do_facet ost1 \
5119                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5120         sleep_maxage
5121
5122         new_status=$(ost_dev_status $ost_idx)
5123         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5124                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5125 }
5126 run_test 56c "check 'lfs df' showing device status"
5127
5128 NUMFILES=3
5129 NUMDIRS=3
5130 setup_56() {
5131         local local_tdir="$1"
5132         local local_numfiles="$2"
5133         local local_numdirs="$3"
5134         local dir_params="$4"
5135         local dir_stripe_params="$5"
5136
5137         if [ ! -d "$local_tdir" ] ; then
5138                 test_mkdir -p $dir_stripe_params $local_tdir
5139                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5140                 for i in $(seq $local_numfiles) ; do
5141                         touch $local_tdir/file$i
5142                 done
5143                 for i in $(seq $local_numdirs) ; do
5144                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5145                         for j in $(seq $local_numfiles) ; do
5146                                 touch $local_tdir/dir$i/file$j
5147                         done
5148                 done
5149         fi
5150 }
5151
5152 setup_56_special() {
5153         local local_tdir=$1
5154         local local_numfiles=$2
5155         local local_numdirs=$3
5156
5157         setup_56 $local_tdir $local_numfiles $local_numdirs
5158
5159         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5160                 for i in $(seq $local_numfiles) ; do
5161                         mknod $local_tdir/loop${i}b b 7 $i
5162                         mknod $local_tdir/null${i}c c 1 3
5163                         ln -s $local_tdir/file1 $local_tdir/link${i}
5164                 done
5165                 for i in $(seq $local_numdirs) ; do
5166                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5167                         mknod $local_tdir/dir$i/null${i}c c 1 3
5168                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5169                 done
5170         fi
5171 }
5172
5173 test_56g() {
5174         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5175         local expected=$(($NUMDIRS + 2))
5176
5177         setup_56 $dir $NUMFILES $NUMDIRS
5178
5179         # test lfs find with -name
5180         for i in $(seq $NUMFILES) ; do
5181                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5182
5183                 [ $nums -eq $expected ] ||
5184                         error "lfs find -name '*$i' $dir wrong: "\
5185                               "found $nums, expected $expected"
5186         done
5187 }
5188 run_test 56g "check lfs find -name"
5189
5190 test_56h() {
5191         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5192         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5193
5194         setup_56 $dir $NUMFILES $NUMDIRS
5195
5196         # test lfs find with ! -name
5197         for i in $(seq $NUMFILES) ; do
5198                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5199
5200                 [ $nums -eq $expected ] ||
5201                         error "lfs find ! -name '*$i' $dir wrong: "\
5202                               "found $nums, expected $expected"
5203         done
5204 }
5205 run_test 56h "check lfs find ! -name"
5206
5207 test_56i() {
5208         local dir=$DIR/$tdir
5209
5210         test_mkdir $dir
5211
5212         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5213         local out=$($cmd)
5214
5215         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5216 }
5217 run_test 56i "check 'lfs find -ost UUID' skips directories"
5218
5219 test_56j() {
5220         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5221
5222         setup_56_special $dir $NUMFILES $NUMDIRS
5223
5224         local expected=$((NUMDIRS + 1))
5225         local cmd="$LFS find -type d $dir"
5226         local nums=$($cmd | wc -l)
5227
5228         [ $nums -eq $expected ] ||
5229                 error "'$cmd' wrong: found $nums, expected $expected"
5230 }
5231 run_test 56j "check lfs find -type d"
5232
5233 test_56k() {
5234         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5235
5236         setup_56_special $dir $NUMFILES $NUMDIRS
5237
5238         local expected=$(((NUMDIRS + 1) * NUMFILES))
5239         local cmd="$LFS find -type f $dir"
5240         local nums=$($cmd | wc -l)
5241
5242         [ $nums -eq $expected ] ||
5243                 error "'$cmd' wrong: found $nums, expected $expected"
5244 }
5245 run_test 56k "check lfs find -type f"
5246
5247 test_56l() {
5248         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5249
5250         setup_56_special $dir $NUMFILES $NUMDIRS
5251
5252         local expected=$((NUMDIRS + NUMFILES))
5253         local cmd="$LFS find -type b $dir"
5254         local nums=$($cmd | wc -l)
5255
5256         [ $nums -eq $expected ] ||
5257                 error "'$cmd' wrong: found $nums, expected $expected"
5258 }
5259 run_test 56l "check lfs find -type b"
5260
5261 test_56m() {
5262         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5263
5264         setup_56_special $dir $NUMFILES $NUMDIRS
5265
5266         local expected=$((NUMDIRS + NUMFILES))
5267         local cmd="$LFS find -type c $dir"
5268         local nums=$($cmd | wc -l)
5269         [ $nums -eq $expected ] ||
5270                 error "'$cmd' wrong: found $nums, expected $expected"
5271 }
5272 run_test 56m "check lfs find -type c"
5273
5274 test_56n() {
5275         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5276         setup_56_special $dir $NUMFILES $NUMDIRS
5277
5278         local expected=$((NUMDIRS + NUMFILES))
5279         local cmd="$LFS find -type l $dir"
5280         local nums=$($cmd | wc -l)
5281
5282         [ $nums -eq $expected ] ||
5283                 error "'$cmd' wrong: found $nums, expected $expected"
5284 }
5285 run_test 56n "check lfs find -type l"
5286
5287 test_56o() {
5288         local dir=$DIR/$tdir
5289
5290         setup_56 $dir $NUMFILES $NUMDIRS
5291         utime $dir/file1 > /dev/null || error "utime (1)"
5292         utime $dir/file2 > /dev/null || error "utime (2)"
5293         utime $dir/dir1 > /dev/null || error "utime (3)"
5294         utime $dir/dir2 > /dev/null || error "utime (4)"
5295         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5296         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5297
5298         local expected=4
5299         local nums=$($LFS find -mtime +0 $dir | wc -l)
5300
5301         [ $nums -eq $expected ] ||
5302                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5303
5304         expected=12
5305         cmd="$LFS find -mtime 0 $dir"
5306         nums=$($cmd | wc -l)
5307         [ $nums -eq $expected ] ||
5308                 error "'$cmd' wrong: found $nums, expected $expected"
5309 }
5310 run_test 56o "check lfs find -mtime for old files"
5311
5312 test_56p() {
5313         [ $RUNAS_ID -eq $UID ] &&
5314                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5315
5316         local dir=$DIR/$tdir
5317
5318         setup_56 $dir $NUMFILES $NUMDIRS
5319         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5320
5321         local expected=$NUMFILES
5322         local cmd="$LFS find -uid $RUNAS_ID $dir"
5323         local nums=$($cmd | wc -l)
5324
5325         [ $nums -eq $expected ] ||
5326                 error "'$cmd' wrong: found $nums, expected $expected"
5327
5328         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5329         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5330         nums=$($cmd | wc -l)
5331         [ $nums -eq $expected ] ||
5332                 error "'$cmd' wrong: found $nums, expected $expected"
5333 }
5334 run_test 56p "check lfs find -uid and ! -uid"
5335
5336 test_56q() {
5337         [ $RUNAS_ID -eq $UID ] &&
5338                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5339
5340         local dir=$DIR/$tdir
5341
5342         setup_56 $dir $NUMFILES $NUMDIRS
5343         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5344
5345         local expected=$NUMFILES
5346         local cmd="$LFS find -gid $RUNAS_GID $dir"
5347         local nums=$($cmd | wc -l)
5348
5349         [ $nums -eq $expected ] ||
5350                 error "'$cmd' wrong: found $nums, expected $expected"
5351
5352         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5353         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5354         nums=$($cmd | wc -l)
5355         [ $nums -eq $expected ] ||
5356                 error "'$cmd' wrong: found $nums, expected $expected"
5357 }
5358 run_test 56q "check lfs find -gid and ! -gid"
5359
5360 test_56r() {
5361         local dir=$DIR/$tdir
5362
5363         setup_56 $dir $NUMFILES $NUMDIRS
5364
5365         local expected=12
5366         local cmd="$LFS find -size 0 -type f -lazy $dir"
5367         local nums=$($cmd | wc -l)
5368
5369         [ $nums -eq $expected ] ||
5370                 error "'$cmd' wrong: found $nums, expected $expected"
5371         cmd="$LFS find -size 0 -type f $dir"
5372         nums=$($cmd | wc -l)
5373         [ $nums -eq $expected ] ||
5374                 error "'$cmd' wrong: found $nums, expected $expected"
5375
5376         expected=0
5377         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5378         nums=$($cmd | wc -l)
5379         [ $nums -eq $expected ] ||
5380                 error "'$cmd' wrong: found $nums, expected $expected"
5381         cmd="$LFS find ! -size 0 -type f $dir"
5382         nums=$($cmd | wc -l)
5383         [ $nums -eq $expected ] ||
5384                 error "'$cmd' wrong: found $nums, expected $expected"
5385
5386         echo "test" > $dir/$tfile
5387         echo "test2" > $dir/$tfile.2 && sync
5388         expected=1
5389         cmd="$LFS find -size 5 -type f -lazy $dir"
5390         nums=$($cmd | wc -l)
5391         [ $nums -eq $expected ] ||
5392                 error "'$cmd' wrong: found $nums, expected $expected"
5393         cmd="$LFS find -size 5 -type f $dir"
5394         nums=$($cmd | wc -l)
5395         [ $nums -eq $expected ] ||
5396                 error "'$cmd' wrong: found $nums, expected $expected"
5397
5398         expected=1
5399         cmd="$LFS find -size +5 -type f -lazy $dir"
5400         nums=$($cmd | wc -l)
5401         [ $nums -eq $expected ] ||
5402                 error "'$cmd' wrong: found $nums, expected $expected"
5403         cmd="$LFS find -size +5 -type f $dir"
5404         nums=$($cmd | wc -l)
5405         [ $nums -eq $expected ] ||
5406                 error "'$cmd' wrong: found $nums, expected $expected"
5407
5408         expected=2
5409         cmd="$LFS find -size +0 -type f -lazy $dir"
5410         nums=$($cmd | wc -l)
5411         [ $nums -eq $expected ] ||
5412                 error "'$cmd' wrong: found $nums, expected $expected"
5413         cmd="$LFS find -size +0 -type f $dir"
5414         nums=$($cmd | wc -l)
5415         [ $nums -eq $expected ] ||
5416                 error "'$cmd' wrong: found $nums, expected $expected"
5417
5418         expected=2
5419         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5420         nums=$($cmd | wc -l)
5421         [ $nums -eq $expected ] ||
5422                 error "'$cmd' wrong: found $nums, expected $expected"
5423         cmd="$LFS find ! -size -5 -type f $dir"
5424         nums=$($cmd | wc -l)
5425         [ $nums -eq $expected ] ||
5426                 error "'$cmd' wrong: found $nums, expected $expected"
5427
5428         expected=12
5429         cmd="$LFS find -size -5 -type f -lazy $dir"
5430         nums=$($cmd | wc -l)
5431         [ $nums -eq $expected ] ||
5432                 error "'$cmd' wrong: found $nums, expected $expected"
5433         cmd="$LFS find -size -5 -type f $dir"
5434         nums=$($cmd | wc -l)
5435         [ $nums -eq $expected ] ||
5436                 error "'$cmd' wrong: found $nums, expected $expected"
5437 }
5438 run_test 56r "check lfs find -size works"
5439
5440 test_56ra_sub() {
5441         local expected=$1
5442         local glimpses=$2
5443         local cmd="$3"
5444
5445         cancel_lru_locks $OSC
5446
5447         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5448         local nums=$($cmd | wc -l)
5449
5450         [ $nums -eq $expected ] ||
5451                 error "'$cmd' wrong: found $nums, expected $expected"
5452
5453         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5454
5455         if (( rpcs_before + glimpses != rpcs_after )); then
5456                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5457                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
5458
5459                 if [[ $glimpses == 0 ]]; then
5460                         error "'$cmd' should not send glimpse RPCs to OST"
5461                 else
5462                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
5463                 fi
5464         fi
5465 }
5466
5467 test_56ra() {
5468         [[ $MDS1_VERSION -ge $(version_code 2.12.4) ]] ||
5469                 skip "MDS < 2.12.4 doesn't return LSOM data"
5470         local dir=$DIR/$tdir
5471
5472         [[ $OSC == "mdc" ]] && skip "DoM files"
5473
5474         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5475         # open and close all files to ensure LSOM is updated
5476         cancel_lru_locks $OSC
5477         find $dir -type f | xargs cat > /dev/null
5478
5479         #   expect_found  glimpse_rpcs  command_to_run
5480         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
5481         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
5482         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
5483         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
5484
5485         echo "test" > $dir/$tfile
5486         echo "test2" > $dir/$tfile.2 && sync
5487         cancel_lru_locks $OSC
5488         cat $dir/$tfile $dir/$tfile.2 > /dev/null
5489
5490         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
5491         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
5492         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
5493         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
5494
5495         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
5496         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
5497         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
5498         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
5499         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
5500         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
5501 }
5502 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
5503
5504 test_56s() { # LU-611 #LU-9369
5505         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
5506
5507         local dir=$DIR/$tdir
5508         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
5509
5510         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5511         for i in $(seq $NUMDIRS); do
5512                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
5513         done
5514
5515         local expected=$NUMDIRS
5516         local cmd="$LFS find -c $OSTCOUNT $dir"
5517         local nums=$($cmd | wc -l)
5518
5519         [ $nums -eq $expected ] || {
5520                 $LFS getstripe -R $dir
5521                 error "'$cmd' wrong: found $nums, expected $expected"
5522         }
5523
5524         expected=$((NUMDIRS + onestripe))
5525         cmd="$LFS find -stripe-count +0 -type f $dir"
5526         nums=$($cmd | wc -l)
5527         [ $nums -eq $expected ] || {
5528                 $LFS getstripe -R $dir
5529                 error "'$cmd' wrong: found $nums, expected $expected"
5530         }
5531
5532         expected=$onestripe
5533         cmd="$LFS find -stripe-count 1 -type f $dir"
5534         nums=$($cmd | wc -l)
5535         [ $nums -eq $expected ] || {
5536                 $LFS getstripe -R $dir
5537                 error "'$cmd' wrong: found $nums, expected $expected"
5538         }
5539
5540         cmd="$LFS find -stripe-count -2 -type f $dir"
5541         nums=$($cmd | wc -l)
5542         [ $nums -eq $expected ] || {
5543                 $LFS getstripe -R $dir
5544                 error "'$cmd' wrong: found $nums, expected $expected"
5545         }
5546
5547         expected=0
5548         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
5549         nums=$($cmd | wc -l)
5550         [ $nums -eq $expected ] || {
5551                 $LFS getstripe -R $dir
5552                 error "'$cmd' wrong: found $nums, expected $expected"
5553         }
5554 }
5555 run_test 56s "check lfs find -stripe-count works"
5556
5557 test_56t() { # LU-611 #LU-9369
5558         local dir=$DIR/$tdir
5559
5560         setup_56 $dir 0 $NUMDIRS
5561         for i in $(seq $NUMDIRS); do
5562                 $LFS setstripe -S 8M $dir/dir$i/$tfile
5563         done
5564
5565         local expected=$NUMDIRS
5566         local cmd="$LFS find -S 8M $dir"
5567         local nums=$($cmd | wc -l)
5568
5569         [ $nums -eq $expected ] || {
5570                 $LFS getstripe -R $dir
5571                 error "'$cmd' wrong: found $nums, expected $expected"
5572         }
5573         rm -rf $dir
5574
5575         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
5576
5577         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
5578
5579         expected=$(((NUMDIRS + 1) * NUMFILES))
5580         cmd="$LFS find -stripe-size 512k -type f $dir"
5581         nums=$($cmd | wc -l)
5582         [ $nums -eq $expected ] ||
5583                 error "'$cmd' wrong: found $nums, expected $expected"
5584
5585         cmd="$LFS find -stripe-size +320k -type f $dir"
5586         nums=$($cmd | wc -l)
5587         [ $nums -eq $expected ] ||
5588                 error "'$cmd' wrong: found $nums, expected $expected"
5589
5590         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
5591         cmd="$LFS find -stripe-size +200k -type f $dir"
5592         nums=$($cmd | wc -l)
5593         [ $nums -eq $expected ] ||
5594                 error "'$cmd' wrong: found $nums, expected $expected"
5595
5596         cmd="$LFS find -stripe-size -640k -type f $dir"
5597         nums=$($cmd | wc -l)
5598         [ $nums -eq $expected ] ||
5599                 error "'$cmd' wrong: found $nums, expected $expected"
5600
5601         expected=4
5602         cmd="$LFS find -stripe-size 256k -type f $dir"
5603         nums=$($cmd | wc -l)
5604         [ $nums -eq $expected ] ||
5605                 error "'$cmd' wrong: found $nums, expected $expected"
5606
5607         cmd="$LFS find -stripe-size -320k -type f $dir"
5608         nums=$($cmd | wc -l)
5609         [ $nums -eq $expected ] ||
5610                 error "'$cmd' wrong: found $nums, expected $expected"
5611
5612         expected=0
5613         cmd="$LFS find -stripe-size 1024k -type f $dir"
5614         nums=$($cmd | wc -l)
5615         [ $nums -eq $expected ] ||
5616                 error "'$cmd' wrong: found $nums, expected $expected"
5617 }
5618 run_test 56t "check lfs find -stripe-size works"
5619
5620 test_56u() { # LU-611
5621         local dir=$DIR/$tdir
5622
5623         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5624
5625         if [[ $OSTCOUNT -gt 1 ]]; then
5626                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5627                 onestripe=4
5628         else
5629                 onestripe=0
5630         fi
5631
5632         local expected=$(((NUMDIRS + 1) * NUMFILES))
5633         local cmd="$LFS find -stripe-index 0 -type f $dir"
5634         local nums=$($cmd | wc -l)
5635
5636         [ $nums -eq $expected ] ||
5637                 error "'$cmd' wrong: found $nums, expected $expected"
5638
5639         expected=$onestripe
5640         cmd="$LFS find -stripe-index 1 -type f $dir"
5641         nums=$($cmd | wc -l)
5642         [ $nums -eq $expected ] ||
5643                 error "'$cmd' wrong: found $nums, expected $expected"
5644
5645         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5646         nums=$($cmd | wc -l)
5647         [ $nums -eq $expected ] ||
5648                 error "'$cmd' wrong: found $nums, expected $expected"
5649
5650         expected=0
5651         # This should produce an error and not return any files
5652         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5653         nums=$($cmd 2>/dev/null | wc -l)
5654         [ $nums -eq $expected ] ||
5655                 error "'$cmd' wrong: found $nums, expected $expected"
5656
5657         if [[ $OSTCOUNT -gt 1 ]]; then
5658                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5659                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5660                 nums=$($cmd | wc -l)
5661                 [ $nums -eq $expected ] ||
5662                         error "'$cmd' wrong: found $nums, expected $expected"
5663         fi
5664 }
5665 run_test 56u "check lfs find -stripe-index works"
5666
5667 test_56v() {
5668         local mdt_idx=0
5669         local dir=$DIR/$tdir
5670
5671         setup_56 $dir $NUMFILES $NUMDIRS
5672
5673         UUID=$(mdtuuid_from_index $mdt_idx $dir)
5674         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
5675
5676         for file in $($LFS find -m $UUID $dir); do
5677                 file_midx=$($LFS getstripe -m $file)
5678                 [ $file_midx -eq $mdt_idx ] ||
5679                         error "lfs find -m $UUID != getstripe -m $file_midx"
5680         done
5681 }
5682 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
5683
5684 test_56w() {
5685         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5686         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5687
5688         local dir=$DIR/$tdir
5689
5690         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5691
5692         local stripe_size=$($LFS getstripe -S -d $dir) ||
5693                 error "$LFS getstripe -S -d $dir failed"
5694         stripe_size=${stripe_size%% *}
5695
5696         local file_size=$((stripe_size * OSTCOUNT))
5697         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5698         local required_space=$((file_num * file_size))
5699         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5700                            head -n1)
5701         [[ $free_space -le $((required_space / 1024)) ]] &&
5702                 skip_env "need $required_space, have $free_space kbytes"
5703
5704         local dd_bs=65536
5705         local dd_count=$((file_size / dd_bs))
5706
5707         # write data into the files
5708         local i
5709         local j
5710         local file
5711
5712         for i in $(seq $NUMFILES); do
5713                 file=$dir/file$i
5714                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5715                         error "write data into $file failed"
5716         done
5717         for i in $(seq $NUMDIRS); do
5718                 for j in $(seq $NUMFILES); do
5719                         file=$dir/dir$i/file$j
5720                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5721                                 error "write data into $file failed"
5722                 done
5723         done
5724
5725         # $LFS_MIGRATE will fail if hard link migration is unsupported
5726         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5727                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5728                         error "creating links to $dir/dir1/file1 failed"
5729         fi
5730
5731         local expected=-1
5732
5733         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5734
5735         # lfs_migrate file
5736         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5737
5738         echo "$cmd"
5739         eval $cmd || error "$cmd failed"
5740
5741         check_stripe_count $dir/file1 $expected
5742
5743         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
5744         then
5745                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5746                 # OST 1 if it is on OST 0. This file is small enough to
5747                 # be on only one stripe.
5748                 file=$dir/migr_1_ost
5749                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5750                         error "write data into $file failed"
5751                 local obdidx=$($LFS getstripe -i $file)
5752                 local oldmd5=$(md5sum $file)
5753                 local newobdidx=0
5754
5755                 [[ $obdidx -eq 0 ]] && newobdidx=1
5756                 cmd="$LFS migrate -i $newobdidx $file"
5757                 echo $cmd
5758                 eval $cmd || error "$cmd failed"
5759
5760                 local realobdix=$($LFS getstripe -i $file)
5761                 local newmd5=$(md5sum $file)
5762
5763                 [[ $newobdidx -ne $realobdix ]] &&
5764                         error "new OST is different (was=$obdidx, "\
5765                               "wanted=$newobdidx, got=$realobdix)"
5766                 [[ "$oldmd5" != "$newmd5" ]] &&
5767                         error "md5sum differ: $oldmd5, $newmd5"
5768         fi
5769
5770         # lfs_migrate dir
5771         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5772         echo "$cmd"
5773         eval $cmd || error "$cmd failed"
5774
5775         for j in $(seq $NUMFILES); do
5776                 check_stripe_count $dir/dir1/file$j $expected
5777         done
5778
5779         # lfs_migrate works with lfs find
5780         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5781              $LFS_MIGRATE -y -c $expected"
5782         echo "$cmd"
5783         eval $cmd || error "$cmd failed"
5784
5785         for i in $(seq 2 $NUMFILES); do
5786                 check_stripe_count $dir/file$i $expected
5787         done
5788         for i in $(seq 2 $NUMDIRS); do
5789                 for j in $(seq $NUMFILES); do
5790                 check_stripe_count $dir/dir$i/file$j $expected
5791                 done
5792         done
5793 }
5794 run_test 56w "check lfs_migrate -c stripe_count works"
5795
5796 test_56wb() {
5797         local file1=$DIR/$tdir/file1
5798         local create_pool=false
5799         local initial_pool=$($LFS getstripe -p $DIR)
5800         local pool_list=()
5801         local pool=""
5802
5803         echo -n "Creating test dir..."
5804         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5805         echo "done."
5806
5807         echo -n "Creating test file..."
5808         touch $file1 || error "cannot create file"
5809         echo "done."
5810
5811         echo -n "Detecting existing pools..."
5812         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
5813
5814         if [ ${#pool_list[@]} -gt 0 ]; then
5815                 echo "${pool_list[@]}"
5816                 for thispool in "${pool_list[@]}"; do
5817                         if [[ -z "$initial_pool" ||
5818                               "$initial_pool" != "$thispool" ]]; then
5819                                 pool="$thispool"
5820                                 echo "Using existing pool '$pool'"
5821                                 break
5822                         fi
5823                 done
5824         else
5825                 echo "none detected."
5826         fi
5827         if [ -z "$pool" ]; then
5828                 pool=${POOL:-testpool}
5829                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5830                 echo -n "Creating pool '$pool'..."
5831                 create_pool=true
5832                 pool_add $pool &> /dev/null ||
5833                         error "pool_add failed"
5834                 echo "done."
5835
5836                 echo -n "Adding target to pool..."
5837                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5838                         error "pool_add_targets failed"
5839                 echo "done."
5840         fi
5841
5842         echo -n "Setting pool using -p option..."
5843         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5844                 error "migrate failed rc = $?"
5845         echo "done."
5846
5847         echo -n "Verifying test file is in pool after migrating..."
5848         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5849                 error "file was not migrated to pool $pool"
5850         echo "done."
5851
5852         echo -n "Removing test file from pool '$pool'..."
5853         # "lfs migrate $file" won't remove the file from the pool
5854         # until some striping information is changed.
5855         $LFS migrate -c 1 $file1 &> /dev/null ||
5856                 error "cannot remove from pool"
5857         [ "$($LFS getstripe -p $file1)" ] &&
5858                 error "pool still set"
5859         echo "done."
5860
5861         echo -n "Setting pool using --pool option..."
5862         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5863                 error "migrate failed rc = $?"
5864         echo "done."
5865
5866         # Clean up
5867         rm -f $file1
5868         if $create_pool; then
5869                 destroy_test_pools 2> /dev/null ||
5870                         error "destroy test pools failed"
5871         fi
5872 }
5873 run_test 56wb "check lfs_migrate pool support"
5874
5875 test_56wc() {
5876         local file1="$DIR/$tdir/file1"
5877         local parent_ssize
5878         local parent_scount
5879         local cur_ssize
5880         local cur_scount
5881         local orig_ssize
5882
5883         echo -n "Creating test dir..."
5884         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5885         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5886                 error "cannot set stripe by '-S 1M -c 1'"
5887         echo "done"
5888
5889         echo -n "Setting initial stripe for test file..."
5890         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5891                 error "cannot set stripe"
5892         cur_ssize=$($LFS getstripe -S "$file1")
5893         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
5894         echo "done."
5895
5896         # File currently set to -S 512K -c 1
5897
5898         # Ensure -c and -S options are rejected when -R is set
5899         echo -n "Verifying incompatible options are detected..."
5900         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5901                 error "incompatible -c and -R options not detected"
5902         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5903                 error "incompatible -S and -R options not detected"
5904         echo "done."
5905
5906         # Ensure unrecognized options are passed through to 'lfs migrate'
5907         echo -n "Verifying -S option is passed through to lfs migrate..."
5908         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5909                 error "migration failed"
5910         cur_ssize=$($LFS getstripe -S "$file1")
5911         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
5912         echo "done."
5913
5914         # File currently set to -S 1M -c 1
5915
5916         # Ensure long options are supported
5917         echo -n "Verifying long options supported..."
5918         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5919                 error "long option without argument not supported"
5920         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5921                 error "long option with argument not supported"
5922         cur_ssize=$($LFS getstripe -S "$file1")
5923         [ $cur_ssize -eq 524288 ] ||
5924                 error "migrate --stripe-size $cur_ssize != 524288"
5925         echo "done."
5926
5927         # File currently set to -S 512K -c 1
5928
5929         if [ "$OSTCOUNT" -gt 1 ]; then
5930                 echo -n "Verifying explicit stripe count can be set..."
5931                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5932                         error "migrate failed"
5933                 cur_scount=$($LFS getstripe -c "$file1")
5934                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
5935                 echo "done."
5936         fi
5937
5938         # File currently set to -S 512K -c 1 or -S 512K -c 2
5939
5940         # Ensure parent striping is used if -R is set, and no stripe
5941         # count or size is specified
5942         echo -n "Setting stripe for parent directory..."
5943         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5944                 error "cannot set stripe '-S 2M -c 1'"
5945         echo "done."
5946
5947         echo -n "Verifying restripe option uses parent stripe settings..."
5948         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
5949         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
5950         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5951                 error "migrate failed"
5952         cur_ssize=$($LFS getstripe -S "$file1")
5953         [ $cur_ssize -eq $parent_ssize ] ||
5954                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
5955         cur_scount=$($LFS getstripe -c "$file1")
5956         [ $cur_scount -eq $parent_scount ] ||
5957                 error "migrate -R stripe_count $cur_scount != $parent_scount"
5958         echo "done."
5959
5960         # File currently set to -S 1M -c 1
5961
5962         # Ensure striping is preserved if -R is not set, and no stripe
5963         # count or size is specified
5964         echo -n "Verifying striping size preserved when not specified..."
5965         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
5966         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5967                 error "cannot set stripe on parent directory"
5968         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5969                 error "migrate failed"
5970         cur_ssize=$($LFS getstripe -S "$file1")
5971         [ $cur_ssize -eq $orig_ssize ] ||
5972                 error "migrate by default $cur_ssize != $orig_ssize"
5973         echo "done."
5974
5975         # Ensure file name properly detected when final option has no argument
5976         echo -n "Verifying file name properly detected..."
5977         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5978                 error "file name interpreted as option argument"
5979         echo "done."
5980
5981         # Clean up
5982         rm -f "$file1"
5983 }
5984 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5985
5986 test_56wd() {
5987         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5988
5989         local file1=$DIR/$tdir/file1
5990
5991         echo -n "Creating test dir..."
5992         test_mkdir $DIR/$tdir || error "cannot create dir"
5993         echo "done."
5994
5995         echo -n "Creating test file..."
5996         touch $file1
5997         echo "done."
5998
5999         # Ensure 'lfs migrate' will fail by using a non-existent option,
6000         # and make sure rsync is not called to recover
6001         echo -n "Make sure --no-rsync option works..."
6002         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6003                 grep -q 'refusing to fall back to rsync' ||
6004                 error "rsync was called with --no-rsync set"
6005         echo "done."
6006
6007         # Ensure rsync is called without trying 'lfs migrate' first
6008         echo -n "Make sure --rsync option works..."
6009         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6010                 grep -q 'falling back to rsync' &&
6011                 error "lfs migrate was called with --rsync set"
6012         echo "done."
6013
6014         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6015         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6016                 grep -q 'at the same time' ||
6017                 error "--rsync and --no-rsync accepted concurrently"
6018         echo "done."
6019
6020         # Clean up
6021         rm -f $file1
6022 }
6023 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6024
6025 test_56x() {
6026         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6027         check_swap_layouts_support
6028
6029         local dir=$DIR/$tdir
6030         local ref1=/etc/passwd
6031         local file1=$dir/file1
6032
6033         test_mkdir $dir || error "creating dir $dir"
6034         $LFS setstripe -c 2 $file1
6035         cp $ref1 $file1
6036         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6037         stripe=$($LFS getstripe -c $file1)
6038         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6039         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6040
6041         # clean up
6042         rm -f $file1
6043 }
6044 run_test 56x "lfs migration support"
6045
6046 test_56xa() {
6047         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6048         check_swap_layouts_support
6049
6050         local dir=$DIR/$tdir/$testnum
6051
6052         test_mkdir -p $dir
6053
6054         local ref1=/etc/passwd
6055         local file1=$dir/file1
6056
6057         $LFS setstripe -c 2 $file1
6058         cp $ref1 $file1
6059         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6060
6061         local stripe=$($LFS getstripe -c $file1)
6062
6063         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6064         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6065
6066         # clean up
6067         rm -f $file1
6068 }
6069 run_test 56xa "lfs migration --block support"
6070
6071 check_migrate_links() {
6072         local dir="$1"
6073         local file1="$dir/file1"
6074         local begin="$2"
6075         local count="$3"
6076         local total_count=$(($begin + $count - 1))
6077         local symlink_count=10
6078         local uniq_count=10
6079
6080         if [ ! -f "$file1" ]; then
6081                 echo -n "creating initial file..."
6082                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6083                         error "cannot setstripe initial file"
6084                 echo "done"
6085
6086                 echo -n "creating symlinks..."
6087                 for s in $(seq 1 $symlink_count); do
6088                         ln -s "$file1" "$dir/slink$s" ||
6089                                 error "cannot create symlinks"
6090                 done
6091                 echo "done"
6092
6093                 echo -n "creating nonlinked files..."
6094                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6095                         error "cannot create nonlinked files"
6096                 echo "done"
6097         fi
6098
6099         # create hard links
6100         if [ ! -f "$dir/file$total_count" ]; then
6101                 echo -n "creating hard links $begin:$total_count..."
6102                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6103                         /dev/null || error "cannot create hard links"
6104                 echo "done"
6105         fi
6106
6107         echo -n "checking number of hard links listed in xattrs..."
6108         local fid=$($LFS getstripe -F "$file1")
6109         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6110
6111         echo "${#paths[*]}"
6112         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6113                         skip "hard link list has unexpected size, skipping test"
6114         fi
6115         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6116                         error "link names should exceed xattrs size"
6117         fi
6118
6119         echo -n "migrating files..."
6120         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
6121         local rc=$?
6122         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6123         echo "done"
6124
6125         # make sure all links have been properly migrated
6126         echo -n "verifying files..."
6127         fid=$($LFS getstripe -F "$file1") ||
6128                 error "cannot get fid for file $file1"
6129         for i in $(seq 2 $total_count); do
6130                 local fid2=$($LFS getstripe -F $dir/file$i)
6131
6132                 [ "$fid2" == "$fid" ] ||
6133                         error "migrated hard link has mismatched FID"
6134         done
6135
6136         # make sure hard links were properly detected, and migration was
6137         # performed only once for the entire link set; nonlinked files should
6138         # also be migrated
6139         local actual=$(grep -c 'done' <<< "$migrate_out")
6140         local expected=$(($uniq_count + 1))
6141
6142         [ "$actual" -eq  "$expected" ] ||
6143                 error "hard links individually migrated ($actual != $expected)"
6144
6145         # make sure the correct number of hard links are present
6146         local hardlinks=$(stat -c '%h' "$file1")
6147
6148         [ $hardlinks -eq $total_count ] ||
6149                 error "num hard links $hardlinks != $total_count"
6150         echo "done"
6151
6152         return 0
6153 }
6154
6155 test_56xb() {
6156         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6157                 skip "Need MDS version at least 2.10.55"
6158
6159         local dir="$DIR/$tdir"
6160
6161         test_mkdir "$dir" || error "cannot create dir $dir"
6162
6163         echo "testing lfs migrate mode when all links fit within xattrs"
6164         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6165
6166         echo "testing rsync mode when all links fit within xattrs"
6167         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6168
6169         echo "testing lfs migrate mode when all links do not fit within xattrs"
6170         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6171
6172         echo "testing rsync mode when all links do not fit within xattrs"
6173         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6174
6175
6176         # clean up
6177         rm -rf $dir
6178 }
6179 run_test 56xb "lfs migration hard link support"
6180
6181 test_56xc() {
6182         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6183
6184         local dir="$DIR/$tdir"
6185
6186         test_mkdir "$dir" || error "cannot create dir $dir"
6187
6188         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6189         echo -n "Setting initial stripe for 20MB test file..."
6190         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6191                 error "cannot setstripe 20MB file"
6192         echo "done"
6193         echo -n "Sizing 20MB test file..."
6194         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6195         echo "done"
6196         echo -n "Verifying small file autostripe count is 1..."
6197         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6198                 error "cannot migrate 20MB file"
6199         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6200                 error "cannot get stripe for $dir/20mb"
6201         [ $stripe_count -eq 1 ] ||
6202                 error "unexpected stripe count $stripe_count for 20MB file"
6203         rm -f "$dir/20mb"
6204         echo "done"
6205
6206         # Test 2: File is small enough to fit within the available space on
6207         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6208         # have at least an additional 1KB for each desired stripe for test 3
6209         echo -n "Setting stripe for 1GB test file..."
6210         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6211         echo "done"
6212         echo -n "Sizing 1GB test file..."
6213         # File size is 1GB + 3KB
6214         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6215         echo "done"
6216
6217         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6218         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6219         if (( avail > 524288 * OSTCOUNT )); then
6220                 echo -n "Migrating 1GB file..."
6221                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6222                         error "cannot migrate 1GB file"
6223                 echo "done"
6224                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6225                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6226                         error "cannot getstripe for 1GB file"
6227                 [ $stripe_count -eq 2 ] ||
6228                         error "unexpected stripe count $stripe_count != 2"
6229                 echo "done"
6230         fi
6231
6232         # Test 3: File is too large to fit within the available space on
6233         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6234         if [ $OSTCOUNT -ge 3 ]; then
6235                 # The required available space is calculated as
6236                 # file size (1GB + 3KB) / OST count (3).
6237                 local kb_per_ost=349526
6238
6239                 echo -n "Migrating 1GB file with limit..."
6240                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6241                         error "cannot migrate 1GB file with limit"
6242                 echo "done"
6243
6244                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6245                 echo -n "Verifying 1GB autostripe count with limited space..."
6246                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6247                         error "unexpected stripe count $stripe_count (min 3)"
6248                 echo "done"
6249         fi
6250
6251         # clean up
6252         rm -rf $dir
6253 }
6254 run_test 56xc "lfs migration autostripe"
6255
6256 test_56xd() {
6257         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6258
6259         local dir=$DIR/$tdir
6260         local f_mgrt=$dir/$tfile.mgrt
6261         local f_yaml=$dir/$tfile.yaml
6262         local f_copy=$dir/$tfile.copy
6263         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6264         local layout_copy="-c 2 -S 2M -i 1"
6265         local yamlfile=$dir/yamlfile
6266         local layout_before;
6267         local layout_after;
6268
6269         test_mkdir "$dir" || error "cannot create dir $dir"
6270         $LFS setstripe $layout_yaml $f_yaml ||
6271                 error "cannot setstripe $f_yaml with layout $layout_yaml"
6272         $LFS getstripe --yaml $f_yaml > $yamlfile
6273         $LFS setstripe $layout_copy $f_copy ||
6274                 error "cannot setstripe $f_copy with layout $layout_copy"
6275         touch $f_mgrt
6276         dd if=/dev/zero of=$f_mgrt bs=1M count=4
6277
6278         # 1. test option --yaml
6279         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
6280                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
6281         layout_before=$(get_layout_param $f_yaml)
6282         layout_after=$(get_layout_param $f_mgrt)
6283         [ "$layout_after" == "$layout_before" ] ||
6284                 error "lfs_migrate --yaml: $layout_after != $layout_before"
6285
6286         # 2. test option --copy
6287         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
6288                 error "cannot migrate $f_mgrt with --copy $f_copy"
6289         layout_before=$(get_layout_param $f_copy)
6290         layout_after=$(get_layout_param $f_mgrt)
6291         [ "$layout_after" == "$layout_before" ] ||
6292                 error "lfs_migrate --copy: $layout_after != $layout_before"
6293 }
6294 run_test 56xd "check lfs_migrate --yaml and --copy support"
6295
6296 test_56xe() {
6297         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6298
6299         local dir=$DIR/$tdir
6300         local f_comp=$dir/$tfile
6301         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6302         local layout_before=""
6303         local layout_after=""
6304
6305         test_mkdir "$dir" || error "cannot create dir $dir"
6306         $LFS setstripe $layout $f_comp ||
6307                 error "cannot setstripe $f_comp with layout $layout"
6308         layout_before=$(get_layout_param $f_comp)
6309         dd if=/dev/zero of=$f_comp bs=1M count=4
6310
6311         # 1. migrate a comp layout file by lfs_migrate
6312         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
6313         layout_after=$(get_layout_param $f_comp)
6314         [ "$layout_before" == "$layout_after" ] ||
6315                 error "lfs_migrate: $layout_before != $layout_after"
6316
6317         # 2. migrate a comp layout file by lfs migrate
6318         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
6319         layout_after=$(get_layout_param $f_comp)
6320         [ "$layout_before" == "$layout_after" ] ||
6321                 error "lfs migrate: $layout_before != $layout_after"
6322 }
6323 run_test 56xe "migrate a composite layout file"
6324
6325 test_56y() {
6326         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6327                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6328
6329         local res=""
6330         local dir=$DIR/$tdir
6331         local f1=$dir/file1
6332         local f2=$dir/file2
6333
6334         test_mkdir -p $dir || error "creating dir $dir"
6335         touch $f1 || error "creating std file $f1"
6336         $MULTIOP $f2 H2c || error "creating released file $f2"
6337
6338         # a directory can be raid0, so ask only for files
6339         res=$($LFS find $dir -L raid0 -type f | wc -l)
6340         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6341
6342         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6343         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6344
6345         # only files can be released, so no need to force file search
6346         res=$($LFS find $dir -L released)
6347         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6348
6349         res=$($LFS find $dir -type f \! -L released)
6350         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6351 }
6352 run_test 56y "lfs find -L raid0|released"
6353
6354 test_56z() { # LU-4824
6355         # This checks to make sure 'lfs find' continues after errors
6356         # There are two classes of errors that should be caught:
6357         # - If multiple paths are provided, all should be searched even if one
6358         #   errors out
6359         # - If errors are encountered during the search, it should not terminate
6360         #   early
6361         local dir=$DIR/$tdir
6362         local i
6363
6364         test_mkdir $dir
6365         for i in d{0..9}; do
6366                 test_mkdir $dir/$i
6367                 touch $dir/$i/$tfile
6368         done
6369         $LFS find $DIR/non_existent_dir $dir &&
6370                 error "$LFS find did not return an error"
6371         # Make a directory unsearchable. This should NOT be the last entry in
6372         # directory order.  Arbitrarily pick the 6th entry
6373         chmod 700 $($LFS find $dir -type d | sed '6!d')
6374
6375         $RUNAS $LFS find $DIR/non_existent $dir
6376         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6377
6378         # The user should be able to see 10 directories and 9 files
6379         (( count == 19 )) ||
6380                 error "$LFS find found $count != 19 entries after error"
6381 }
6382 run_test 56z "lfs find should continue after an error"
6383
6384 test_56aa() { # LU-5937
6385         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6386
6387         local dir=$DIR/$tdir
6388
6389         mkdir $dir
6390         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6391
6392         createmany -o $dir/striped_dir/${tfile}- 1024
6393         local dirs=$($LFS find --size +8k $dir/)
6394
6395         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6396 }
6397 run_test 56aa "lfs find --size under striped dir"
6398
6399 test_56ab() { # LU-10705
6400         test_mkdir $DIR/$tdir
6401         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6402         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6403         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6404         # Flush writes to ensure valid blocks.  Need to be more thorough for
6405         # ZFS, since blocks are not allocated/returned to client immediately.
6406         sync_all_data
6407         wait_zfs_commit ost1 2
6408         cancel_lru_locks osc
6409         ls -ls $DIR/$tdir
6410
6411         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6412
6413         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6414
6415         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6416         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6417
6418         rm -f $DIR/$tdir/$tfile.[123]
6419 }
6420 run_test 56ab "lfs find --blocks"
6421
6422 test_56ba() {
6423         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6424                 skip "Need MDS version at least 2.10.50"
6425
6426         # Create composite files with one component
6427         local dir=$DIR/$tdir
6428
6429         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6430         # Create composite files with three components
6431         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6432         # Create non-composite files
6433         createmany -o $dir/${tfile}- 10
6434
6435         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6436
6437         [[ $nfiles == 10 ]] ||
6438                 error "lfs find -E 1M found $nfiles != 10 files"
6439
6440         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6441         [[ $nfiles == 25 ]] ||
6442                 error "lfs find ! -E 1M found $nfiles != 25 files"
6443
6444         # All files have a component that starts at 0
6445         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6446         [[ $nfiles == 35 ]] ||
6447                 error "lfs find --component-start 0 - $nfiles != 35 files"
6448
6449         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6450         [[ $nfiles == 15 ]] ||
6451                 error "lfs find --component-start 2M - $nfiles != 15 files"
6452
6453         # All files created here have a componenet that does not starts at 2M
6454         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6455         [[ $nfiles == 35 ]] ||
6456                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6457
6458         # Find files with a specified number of components
6459         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6460         [[ $nfiles == 15 ]] ||
6461                 error "lfs find --component-count 3 - $nfiles != 15 files"
6462
6463         # Remember non-composite files have a component count of zero
6464         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6465         [[ $nfiles == 10 ]] ||
6466                 error "lfs find --component-count 0 - $nfiles != 10 files"
6467
6468         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6469         [[ $nfiles == 20 ]] ||
6470                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6471
6472         # All files have a flag called "init"
6473         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6474         [[ $nfiles == 35 ]] ||
6475                 error "lfs find --component-flags init - $nfiles != 35 files"
6476
6477         # Multi-component files will have a component not initialized
6478         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6479         [[ $nfiles == 15 ]] ||
6480                 error "lfs find !--component-flags init - $nfiles != 15 files"
6481
6482         rm -rf $dir
6483
6484 }
6485 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6486
6487 test_56ca() {
6488         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6489                 skip "Need MDS version at least 2.10.57"
6490
6491         local td=$DIR/$tdir
6492         local tf=$td/$tfile
6493         local dir
6494         local nfiles
6495         local cmd
6496         local i
6497         local j
6498
6499         # create mirrored directories and mirrored files
6500         mkdir $td || error "mkdir $td failed"
6501         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
6502         createmany -o $tf- 10 || error "create $tf- failed"
6503
6504         for i in $(seq 2); do
6505                 dir=$td/dir$i
6506                 mkdir $dir || error "mkdir $dir failed"
6507                 $LFS mirror create -N$((3 + i)) $dir ||
6508                         error "create mirrored dir $dir failed"
6509                 createmany -o $dir/$tfile- 10 ||
6510                         error "create $dir/$tfile- failed"
6511         done
6512
6513         # change the states of some mirrored files
6514         echo foo > $tf-6
6515         for i in $(seq 2); do
6516                 dir=$td/dir$i
6517                 for j in $(seq 4 9); do
6518                         echo foo > $dir/$tfile-$j
6519                 done
6520         done
6521
6522         # find mirrored files with specific mirror count
6523         cmd="$LFS find --mirror-count 3 --type f $td"
6524         nfiles=$($cmd | wc -l)
6525         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
6526
6527         cmd="$LFS find ! --mirror-count 3 --type f $td"
6528         nfiles=$($cmd | wc -l)
6529         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
6530
6531         cmd="$LFS find --mirror-count +2 --type f $td"
6532         nfiles=$($cmd | wc -l)
6533         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6534
6535         cmd="$LFS find --mirror-count -6 --type f $td"
6536         nfiles=$($cmd | wc -l)
6537         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6538
6539         # find mirrored files with specific file state
6540         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
6541         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
6542
6543         cmd="$LFS find --mirror-state=ro --type f $td"
6544         nfiles=$($cmd | wc -l)
6545         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
6546
6547         cmd="$LFS find ! --mirror-state=ro --type f $td"
6548         nfiles=$($cmd | wc -l)
6549         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6550
6551         cmd="$LFS find --mirror-state=wp --type f $td"
6552         nfiles=$($cmd | wc -l)
6553         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6554
6555         cmd="$LFS find ! --mirror-state=sp --type f $td"
6556         nfiles=$($cmd | wc -l)
6557         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6558 }
6559 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6560
6561 test_57a() {
6562         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6563         # note test will not do anything if MDS is not local
6564         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6565                 skip_env "ldiskfs only test"
6566         fi
6567         remote_mds_nodsh && skip "remote MDS with nodsh"
6568
6569         local MNTDEV="osd*.*MDT*.mntdev"
6570         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6571         [ -z "$DEV" ] && error "can't access $MNTDEV"
6572         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6573                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
6574                         error "can't access $DEV"
6575                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
6576                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
6577                 rm $TMP/t57a.dump
6578         done
6579 }
6580 run_test 57a "verify MDS filesystem created with large inodes =="
6581
6582 test_57b() {
6583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6584         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6585                 skip_env "ldiskfs only test"
6586         fi
6587         remote_mds_nodsh && skip "remote MDS with nodsh"
6588
6589         local dir=$DIR/$tdir
6590         local filecount=100
6591         local file1=$dir/f1
6592         local fileN=$dir/f$filecount
6593
6594         rm -rf $dir || error "removing $dir"
6595         test_mkdir -c1 $dir
6596         local mdtidx=$($LFS getstripe -m $dir)
6597         local mdtname=MDT$(printf %04x $mdtidx)
6598         local facet=mds$((mdtidx + 1))
6599
6600         echo "mcreating $filecount files"
6601         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
6602
6603         # verify that files do not have EAs yet
6604         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
6605                 error "$file1 has an EA"
6606         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
6607                 error "$fileN has an EA"
6608
6609         sync
6610         sleep 1
6611         df $dir  #make sure we get new statfs data
6612         local mdsfree=$(do_facet $facet \
6613                         lctl get_param -n osd*.*$mdtname.kbytesfree)
6614         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6615         local file
6616
6617         echo "opening files to create objects/EAs"
6618         for file in $(seq -f $dir/f%g 1 $filecount); do
6619                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
6620                         error "opening $file"
6621         done
6622
6623         # verify that files have EAs now
6624         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
6625         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
6626
6627         sleep 1  #make sure we get new statfs data
6628         df $dir
6629         local mdsfree2=$(do_facet $facet \
6630                          lctl get_param -n osd*.*$mdtname.kbytesfree)
6631         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6632
6633         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
6634                 if [ "$mdsfree" != "$mdsfree2" ]; then
6635                         error "MDC before $mdcfree != after $mdcfree2"
6636                 else
6637                         echo "MDC before $mdcfree != after $mdcfree2"
6638                         echo "unable to confirm if MDS has large inodes"
6639                 fi
6640         fi
6641         rm -rf $dir
6642 }
6643 run_test 57b "default LOV EAs are stored inside large inodes ==="
6644
6645 test_58() {
6646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6647         [ -z "$(which wiretest 2>/dev/null)" ] &&
6648                         skip_env "could not find wiretest"
6649
6650         wiretest
6651 }
6652 run_test 58 "verify cross-platform wire constants =============="
6653
6654 test_59() {
6655         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6656
6657         echo "touch 130 files"
6658         createmany -o $DIR/f59- 130
6659         echo "rm 130 files"
6660         unlinkmany $DIR/f59- 130
6661         sync
6662         # wait for commitment of removal
6663         wait_delete_completed
6664 }
6665 run_test 59 "verify cancellation of llog records async ========="
6666
6667 TEST60_HEAD="test_60 run $RANDOM"
6668 test_60a() {
6669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6670         remote_mgs_nodsh && skip "remote MGS with nodsh"
6671         do_facet mgs "! which run-llog.sh &> /dev/null" &&
6672                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
6673                         skip_env "missing subtest run-llog.sh"
6674
6675         log "$TEST60_HEAD - from kernel mode"
6676         do_facet mgs "$LCTL dk > /dev/null"
6677         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
6678         do_facet mgs $LCTL dk > $TMP/$tfile
6679
6680         # LU-6388: test llog_reader
6681         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
6682         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
6683         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
6684                         skip_env "missing llog_reader"
6685         local fstype=$(facet_fstype mgs)
6686         [ $fstype != ldiskfs -a $fstype != zfs ] &&
6687                 skip_env "Only for ldiskfs or zfs type mgs"
6688
6689         local mntpt=$(facet_mntpt mgs)
6690         local mgsdev=$(mgsdevname 1)
6691         local fid_list
6692         local fid
6693         local rec_list
6694         local rec
6695         local rec_type
6696         local obj_file
6697         local path
6698         local seq
6699         local oid
6700         local pass=true
6701
6702         #get fid and record list
6703         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
6704                 tail -n 4))
6705         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
6706                 tail -n 4))
6707         #remount mgs as ldiskfs or zfs type
6708         stop mgs || error "stop mgs failed"
6709         mount_fstype mgs || error "remount mgs failed"
6710         for ((i = 0; i < ${#fid_list[@]}; i++)); do
6711                 fid=${fid_list[i]}
6712                 rec=${rec_list[i]}
6713                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
6714                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
6715                 oid=$((16#$oid))
6716
6717                 case $fstype in
6718                         ldiskfs )
6719                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
6720                         zfs )
6721                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
6722                 esac
6723                 echo "obj_file is $obj_file"
6724                 do_facet mgs $llog_reader $obj_file
6725
6726                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
6727                         awk '{ print $3 }' | sed -e "s/^type=//g")
6728                 if [ $rec_type != $rec ]; then
6729                         echo "FAILED test_60a wrong record type $rec_type," \
6730                               "should be $rec"
6731                         pass=false
6732                         break
6733                 fi
6734
6735                 #check obj path if record type is LLOG_LOGID_MAGIC
6736                 if [ "$rec" == "1064553b" ]; then
6737                         path=$(do_facet mgs $llog_reader $obj_file |
6738                                 grep "path=" | awk '{ print $NF }' |
6739                                 sed -e "s/^path=//g")
6740                         if [ $obj_file != $mntpt/$path ]; then
6741                                 echo "FAILED test_60a wrong obj path" \
6742                                       "$montpt/$path, should be $obj_file"
6743                                 pass=false
6744                                 break
6745                         fi
6746                 fi
6747         done
6748         rm -f $TMP/$tfile
6749         #restart mgs before "error", otherwise it will block the next test
6750         stop mgs || error "stop mgs failed"
6751         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
6752         $pass || error "test failed, see FAILED test_60a messages for specifics"
6753 }
6754 run_test 60a "llog_test run from kernel module and test llog_reader"
6755
6756 test_60b() { # bug 6411
6757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6758
6759         dmesg > $DIR/$tfile
6760         LLOG_COUNT=$(do_facet mgs dmesg |
6761                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
6762                           /llog_[a-z]*.c:[0-9]/ {
6763                                 if (marker)
6764                                         from_marker++
6765                                 from_begin++
6766                           }
6767                           END {
6768                                 if (marker)
6769                                         print from_marker
6770                                 else
6771                                         print from_begin
6772                           }")
6773
6774         [[ $LLOG_COUNT -gt 120 ]] &&
6775                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
6776 }
6777 run_test 60b "limit repeated messages from CERROR/CWARN"
6778
6779 test_60c() {
6780         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6781
6782         echo "create 5000 files"
6783         createmany -o $DIR/f60c- 5000
6784 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
6785         lctl set_param fail_loc=0x80000137
6786         unlinkmany $DIR/f60c- 5000
6787         lctl set_param fail_loc=0
6788 }
6789 run_test 60c "unlink file when mds full"
6790
6791 test_60d() {
6792         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6793
6794         SAVEPRINTK=$(lctl get_param -n printk)
6795         # verify "lctl mark" is even working"
6796         MESSAGE="test message ID $RANDOM $$"
6797         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6798         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
6799
6800         lctl set_param printk=0 || error "set lnet.printk failed"
6801         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
6802         MESSAGE="new test message ID $RANDOM $$"
6803         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
6804         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6805         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
6806
6807         lctl set_param -n printk="$SAVEPRINTK"
6808 }
6809 run_test 60d "test printk console message masking"
6810
6811 test_60e() {
6812         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6813         remote_mds_nodsh && skip "remote MDS with nodsh"
6814
6815         touch $DIR/$tfile
6816 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
6817         do_facet mds1 lctl set_param fail_loc=0x15b
6818         rm $DIR/$tfile
6819 }
6820 run_test 60e "no space while new llog is being created"
6821
6822 test_60g() {
6823         local pid
6824
6825         test_mkdir -c $MDSCOUNT $DIR/$tdir
6826         $LFS setdirstripe -D -i -1 -c $MDSCOUNT $DIR/$tdir
6827
6828         (
6829                 local index=0
6830                 while true; do
6831                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
6832                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
6833                         index=$((index + 1))
6834                 done
6835         ) &
6836
6837         pid=$!
6838
6839         for i in $(seq 100); do 
6840                 # define OBD_FAIL_OSD_TXN_START    0x19a
6841                 do_facet mds1 lctl set_param fail_loc=0x8000019a
6842                 usleep 100
6843         done
6844
6845         kill -9 $pid
6846
6847         mkdir $DIR/$tdir/new || error "mkdir failed"
6848         rmdir $DIR/$tdir/new || error "rmdir failed"
6849 }
6850 run_test 60g "transaction abort won't cause MDT hung"
6851
6852 test_60h() {
6853         [ $MDS1_VERSION -le $(version_code 2.12.4) ] &&
6854                 skip "Need MDS version at least 2.12.4"
6855         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
6856
6857         local f
6858
6859         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
6860         #define OBD_FAIL_MDS_STRIPE_FID          0x189
6861         for fail_loc in 0x80000188 0x80000189; do
6862                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
6863                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
6864                         error "mkdir $dir-$fail_loc failed"
6865                 for i in {0..10}; do
6866                         # create may fail on missing stripe
6867                         echo $i > $DIR/$tdir-$fail_loc/$i
6868                 done
6869                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6870                         error "getdirstripe $tdir-$fail_loc failed"
6871                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
6872                         error "migrate $tdir-$fail_loc failed"
6873                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6874                         error "getdirstripe $tdir-$fail_loc failed"
6875                 pushd $DIR/$tdir-$fail_loc
6876                 for f in *; do
6877                         echo $f | cmp $f - || error "$f data mismatch"
6878                 done
6879                 popd
6880                 rm -rf $DIR/$tdir-$fail_loc
6881         done
6882 }
6883 run_test 60h "striped directory with missing stripes can be accessed"
6884
6885 test_61a() {
6886         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6887
6888         f="$DIR/f61"
6889         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
6890         cancel_lru_locks osc
6891         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6892         sync
6893 }
6894 run_test 61a "mmap() writes don't make sync hang ================"
6895
6896 test_61b() {
6897         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
6898 }
6899 run_test 61b "mmap() of unstriped file is successful"
6900
6901 # bug 2330 - insufficient obd_match error checking causes LBUG
6902 test_62() {
6903         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6904
6905         f="$DIR/f62"
6906         echo foo > $f
6907         cancel_lru_locks osc
6908         lctl set_param fail_loc=0x405
6909         cat $f && error "cat succeeded, expect -EIO"
6910         lctl set_param fail_loc=0
6911 }
6912 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6913 # match every page all of the time.
6914 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6915
6916 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6917 # Though this test is irrelevant anymore, it helped to reveal some
6918 # other grant bugs (LU-4482), let's keep it.
6919 test_63a() {   # was test_63
6920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6921
6922         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6923
6924         for i in `seq 10` ; do
6925                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6926                 sleep 5
6927                 kill $!
6928                 sleep 1
6929         done
6930
6931         rm -f $DIR/f63 || true
6932 }
6933 run_test 63a "Verify oig_wait interruption does not crash ======="
6934
6935 # bug 2248 - async write errors didn't return to application on sync
6936 # bug 3677 - async write errors left page locked
6937 test_63b() {
6938         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6939
6940         debugsave
6941         lctl set_param debug=-1
6942
6943         # ensure we have a grant to do async writes
6944         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6945         rm $DIR/$tfile
6946
6947         sync    # sync lest earlier test intercept the fail_loc
6948
6949         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6950         lctl set_param fail_loc=0x80000406
6951         $MULTIOP $DIR/$tfile Owy && \
6952                 error "sync didn't return ENOMEM"
6953         sync; sleep 2; sync     # do a real sync this time to flush page
6954         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6955                 error "locked page left in cache after async error" || true
6956         debugrestore
6957 }
6958 run_test 63b "async write errors should be returned to fsync ==="
6959
6960 test_64a () {
6961         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6962
6963         df $DIR
6964         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6965 }
6966 run_test 64a "verify filter grant calculations (in kernel) ====="
6967
6968 test_64b () {
6969         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6970
6971         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6972 }
6973 run_test 64b "check out-of-space detection on client"
6974
6975 test_64c() {
6976         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6977 }
6978 run_test 64c "verify grant shrink"
6979
6980 import_param() {
6981         local tgt=$1
6982         local param=$2
6983
6984         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
6985 }
6986
6987 # this does exactly what osc_request.c:osc_announce_cached() does in
6988 # order to calculate max amount of grants to ask from server
6989 want_grant() {
6990         local tgt=$1
6991
6992         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
6993         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
6994
6995         ((rpc_in_flight++));
6996         nrpages=$((nrpages * rpc_in_flight))
6997
6998         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
6999
7000         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7001
7002         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7003         local undirty=$((nrpages * PAGE_SIZE))
7004
7005         local max_extent_pages
7006         max_extent_pages=$(import_param $tgt grant_max_extent_size)
7007         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7008         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7009         local grant_extent_tax
7010         grant_extent_tax=$(import_param $tgt grant_extent_tax)
7011
7012         undirty=$((undirty + nrextents * grant_extent_tax))
7013
7014         echo $undirty
7015 }
7016
7017 # this is size of unit for grant allocation. It should be equal to
7018 # what tgt_grant.c:tgt_grant_chunk() calculates
7019 grant_chunk() {
7020         local tgt=$1
7021         local max_brw_size
7022         local grant_extent_tax
7023
7024         max_brw_size=$(import_param $tgt max_brw_size)
7025
7026         grant_extent_tax=$(import_param $tgt grant_extent_tax)
7027
7028         echo $(((max_brw_size + grant_extent_tax) * 2))
7029 }
7030
7031 test_64d() {
7032         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
7033                 skip "OST < 2.10.55 doesn't limit grants enough"
7034
7035         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7036
7037         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
7038                 skip "no grant_param connect flag"
7039
7040         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
7041
7042         $LCTL set_param -n -n debug="$OLDDEBUG" || true
7043         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
7044
7045
7046         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7047         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
7048
7049         $LFS setstripe $DIR/$tfile -i 0 -c 1
7050         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
7051         ddpid=$!
7052
7053         while kill -0 $ddpid; do
7054                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
7055
7056                 if [[ $cur_grant -gt $max_cur_granted ]]; then
7057                         kill $ddpid
7058                         error "cur_grant $cur_grant > $max_cur_granted"
7059                 fi
7060
7061                 sleep 1
7062         done
7063 }
7064 run_test 64d "check grant limit exceed"
7065
7066 check_grants() {
7067         local tgt=$1
7068         local expected=$2
7069         local msg=$3
7070         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
7071
7072         ((cur_grants == expected)) ||
7073                 error "$msg: grants mismatch: $cur_grants, expected $expected"
7074 }
7075
7076 round_up_p2() {
7077         echo $((($1 + $2 - 1) & ~($2 - 1)))
7078 }
7079
7080 test_64e() {
7081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7082         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
7083                 skip "Need OSS version at least 2.11.56"
7084
7085         # Remount client to reset grant
7086         remount_client $MOUNT || error "failed to remount client"
7087         local osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7088         local init_grants=$(import_param $osc_tgt initial_grant)
7089
7090         check_grants $osc_tgt $init_grants "init grants"
7091
7092         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
7093         local max_brw_size=$(import_param $osc_tgt max_brw_size)
7094         local gbs=$(import_param $osc_tgt grant_block_size)
7095
7096         # write random number of bytes from max_brw_size / 4 to max_brw_size
7097         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
7098         # align for direct io
7099         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
7100         # round to grant consumption unit
7101         local wb_round_up=$(round_up_p2 $write_bytes gbs)
7102
7103         local grants=$((wb_round_up + extent_tax))
7104
7105         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
7106
7107         # define OBD_FAIL_TGT_NO_GRANT 0x725
7108         # make the server not grant more back
7109         do_facet ost1 $LCTL set_param fail_loc=0x725
7110         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
7111
7112         do_facet ost1 $LCTL set_param fail_loc=0
7113
7114         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
7115
7116         rm -f $DIR/$tfile || error "rm failed"
7117
7118         # Remount client to reset grant
7119         remount_client $MOUNT || error "failed to remount client"
7120         osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7121
7122         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
7123
7124         # define OBD_FAIL_TGT_NO_GRANT 0x725
7125         # make the server not grant more back
7126         do_facet ost1 $LCTL set_param fail_loc=0x725
7127         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
7128         do_facet ost1 $LCTL set_param fail_loc=0
7129
7130         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
7131 }
7132 run_test 64e "check grant consumption (no grant allocation)"
7133
7134 test_64f() {
7135         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7136
7137         # Remount client to reset grant
7138         remount_client $MOUNT || error "failed to remount client"
7139         local osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7140
7141         local init_grants=$(import_param $osc_tgt initial_grant)
7142         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
7143         local max_brw_size=$(import_param $osc_tgt max_brw_size)
7144         local gbs=$(import_param $osc_tgt grant_block_size)
7145         local chunk=$(grant_chunk $osc_tgt)
7146
7147         # write random number of bytes from max_brw_size / 4 to max_brw_size
7148         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
7149         # align for direct io
7150         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
7151         # round to grant consumption unit
7152         local wb_round_up=$(round_up_p2 $write_bytes gbs)
7153
7154         local grants=$((wb_round_up + extent_tax))
7155
7156         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
7157         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
7158                 error "error writing to $DIR/$tfile"
7159
7160         check_grants $osc_tgt $((init_grants - grants + chunk)) \
7161                 "direct io with grant allocation"
7162
7163         rm -f $DIR/$tfile || error "rm failed"
7164
7165         # Remount client to reset grant
7166         remount_client $MOUNT || error "failed to remount client"
7167         osc_tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
7168
7169         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
7170
7171         local cmd="oO_WRONLY:w${write_bytes}_yc"
7172
7173         $MULTIOP $DIR/$tfile $cmd &
7174         MULTIPID=$!
7175         sleep 1
7176
7177         check_grants $osc_tgt $((init_grants - grants)) \
7178                 "buffered io, not write rpc"
7179
7180         kill -USR1 $MULTIPID
7181         wait
7182
7183         check_grants $osc_tgt $((init_grants - grants + chunk)) \
7184                 "buffered io, one RPC"
7185 }
7186 run_test 64f "check grant consumption (with grant allocation)"
7187
7188 test_64h() {
7189         local cli=$($LFS getname $DIR); cli=${cli%% *}; cli=${cli#*-}
7190         local osc_tgt="$FSNAME-OST0000-osc-$cli"
7191         local num_exps=$(do_facet ost1 \
7192             $LCTL get_param -n obdfilter.*OST0000*.num_exports)
7193         local max_brw_size=$(import_param $osc_tgt max_brw_size)
7194         local avail=$($LCTL get_param -n osc.*OST0000-osc-$cli.kbytesavail)
7195         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7196
7197         # 10MiB is for file to be written, max_brw_size * 16 *
7198         # num_exps is space reserve so that tgt_grant_shrink() decided
7199         # to not shrink
7200         local expect=$((max_brw_size * 16 * num_exps + 10 * 1048576))
7201         (( avail * 1024 < expect )) &&
7202                 skip "need $expect bytes on ost1, have $(( avail * 1024 )) only"
7203
7204         save_lustre_params client "osc.*OST0000*.grant_shrink" > $p
7205         save_lustre_params client "osc.*OST0000*.grant_shrink_interval" >> $p
7206         stack_trap "restore_lustre_params < $p; rm -f $save" EXIT
7207         $LCTL set_param osc.*OST0000*.grant_shrink=1
7208         $LCTL set_param osc.*OST0000*.grant_shrink_interval=10
7209
7210         $LFS setstripe -c 1 -i 0 $DIR/$tfile
7211         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 oflag=sync
7212
7213         # drop cache so that coming read would do rpc
7214         cancel_lru_locks osc
7215
7216         # shrink interval is set to 10, pause for 7 seconds so that
7217         # grant thread did not wake up yet but coming read entered
7218         # shrink mode for rpc (osc_should_shrink_grant())
7219         sleep 7
7220
7221         declare -a cur_grant_bytes
7222         declare -a tot_granted
7223         cur_grant_bytes[0]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
7224         tot_granted[0]=$(do_facet ost1 \
7225             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
7226
7227         dd if=$DIR/$tfile bs=4K count=1 of=/dev/null
7228
7229         cur_grant_bytes[1]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
7230         tot_granted[1]=$(do_facet ost1 \
7231             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
7232
7233         # grant change should be equal on both sides
7234         (( cur_grant_bytes[0] - cur_grant_bytes[1] ==
7235                 tot_granted[0] - tot_granted[1])) ||
7236                 error "grant change mismatch, "                                \
7237                         "server: ${tot_granted[0]} to ${tot_granted[1]}, "     \
7238                         "client: ${cur_grant_bytes[0]} to ${cur_grant_bytes[1]}"
7239 }
7240 run_test 64h "grant shrink on read"
7241
7242 test_64i() {
7243         local cli
7244         local osc_tgt
7245         local cgb
7246         local testid=${TESTNAME/_/ }
7247
7248         (( $OST1_VERSION >= $(version_code 2.12.7) )) ||
7249                 skip "need OST at least 2.12.7 to avoid grant shrink on replay"
7250
7251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7252         remote_ost_nodsh && skip "remote OSTs with nodsh"
7253
7254         $LFS setstripe -c 1 -i 0 $DIR/$tfile
7255
7256         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
7257
7258         # lustre-ffff9fc75e850800 /mnt/lustre -> ffff9fc75e850800
7259         cli=$($LFS getname $DIR); cli=${cli%% *}; cli=${cli##*-}
7260         osc_tgt="$FSNAME-OST0000-osc-$cli"
7261         cgb=$($LCTL get_param -n osc.$osc_tgt.cur_grant_bytes)
7262
7263         # shrink grants and simulate rpc loss
7264         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC     0x513
7265         do_facet ost1 "$LCTL set_param fail_loc=0x80000513 fail_val=17"
7266         $LCTL set_param osc.$osc_tgt.cur_grant_bytes=$((cgb/2))
7267
7268         fail ost1
7269
7270         dd if=/dev/zero of=$DIR/$tfile oflag=append bs=1M count=8 conv=notrunc
7271
7272         do_facet ost1 dmesg | tac | sed "/$testid/,$ d" |
7273                 grep "GRANT, real grant" &&
7274                 error "client has more grants then it owns" || true
7275 }
7276 run_test 64i "shrink on reconnect"
7277
7278 # bug 1414 - set/get directories' stripe info
7279 test_65a() {
7280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7281
7282         test_mkdir $DIR/$tdir
7283         touch $DIR/$tdir/f1
7284         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7285 }
7286 run_test 65a "directory with no stripe info"
7287
7288 test_65b() {
7289         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7290
7291         test_mkdir $DIR/$tdir
7292         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7293
7294         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7295                                                 error "setstripe"
7296         touch $DIR/$tdir/f2
7297         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7298 }
7299 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7300
7301 test_65c() {
7302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7303         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7304
7305         test_mkdir $DIR/$tdir
7306         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
7307
7308         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7309                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7310         touch $DIR/$tdir/f3
7311         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7312 }
7313 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7314
7315 test_65d() {
7316         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7317
7318         test_mkdir $DIR/$tdir
7319         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
7320         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7321
7322         if [[ $STRIPECOUNT -le 0 ]]; then
7323                 sc=1
7324         elif [[ $STRIPECOUNT -gt 2000 ]]; then
7325 #LOV_MAX_STRIPE_COUNT is 2000
7326                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
7327         else
7328                 sc=$(($STRIPECOUNT - 1))
7329         fi
7330         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7331         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7332         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7333                 error "lverify failed"
7334 }
7335 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7336
7337 test_65e() {
7338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7339
7340         test_mkdir $DIR/$tdir
7341
7342         $SETSTRIPE $DIR/$tdir || error "setstripe"
7343         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
7344                                         error "no stripe info failed"
7345         touch $DIR/$tdir/f6
7346         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7347 }
7348 run_test 65e "directory setstripe defaults"
7349
7350 test_65f() {
7351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7352
7353         test_mkdir $DIR/${tdir}f
7354         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
7355 }
7356 run_test 65f "dir setstripe permission (should return error) ==="
7357
7358 test_65g() {
7359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7360
7361         test_mkdir $DIR/$tdir
7362         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7363
7364         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7365                 error "setstripe -S failed"
7366         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7367         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7368                 error "delete default stripe failed"
7369 }
7370 run_test 65g "directory setstripe -d"
7371
7372 test_65h() {
7373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7374
7375         test_mkdir $DIR/$tdir
7376         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7377
7378         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7379                 error "setstripe -S failed"
7380         test_mkdir $DIR/$tdir/dd1
7381         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
7382                 error "stripe info inherit failed"
7383 }
7384 run_test 65h "directory stripe info inherit ===================="
7385
7386 test_65i() {
7387         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7388
7389         save_layout_restore_at_exit $MOUNT
7390
7391         # bug6367: set non-default striping on root directory
7392         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7393
7394         # bug12836: getstripe on -1 default directory striping
7395         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7396
7397         # bug12836: getstripe -v on -1 default directory striping
7398         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7399
7400         # bug12836: new find on -1 default directory striping
7401         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7402 }
7403 run_test 65i "various tests to set root directory striping"
7404
7405 test_65j() { # bug6367
7406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7407
7408         sync; sleep 1
7409
7410         # if we aren't already remounting for each test, do so for this test
7411         if [ "$I_MOUNTED" = "yes" ]; then
7412                 cleanup || error "failed to unmount"
7413                 setup
7414         fi
7415
7416         save_layout_restore_at_exit $MOUNT
7417
7418         $SETSTRIPE -d $MOUNT || error "setstripe failed"
7419 }
7420 run_test 65j "set default striping on root directory (bug 6367)="
7421
7422 cleanup_65k() {
7423         rm -rf $DIR/$tdir
7424         wait_delete_completed
7425         do_facet $SINGLEMDS "lctl set_param -n \
7426                 osp.$ost*MDT0000.max_create_count=$max_count"
7427         do_facet $SINGLEMDS "lctl set_param -n \
7428                 osp.$ost*MDT0000.create_count=$count"
7429         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7430         echo $INACTIVE_OSC "is Activate"
7431
7432         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7433 }
7434
7435 test_65k() { # bug11679
7436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7437         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7438         remote_mds_nodsh && skip "remote MDS with nodsh"
7439
7440         local disable_precreate=true
7441         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7442                 disable_precreate=false
7443
7444         echo "Check OST status: "
7445         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7446                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7447
7448         for OSC in $MDS_OSCS; do
7449                 echo $OSC "is active"
7450                 do_facet $SINGLEMDS lctl --device %$OSC activate
7451         done
7452
7453         for INACTIVE_OSC in $MDS_OSCS; do
7454                 local ost=$(osc_to_ost $INACTIVE_OSC)
7455                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7456                                lov.*md*.target_obd |
7457                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7458
7459                 mkdir -p $DIR/$tdir
7460                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
7461                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7462
7463                 echo "Deactivate: " $INACTIVE_OSC
7464                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7465
7466                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7467                               osp.$ost*MDT0000.create_count")
7468                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7469                                   osp.$ost*MDT0000.max_create_count")
7470                 $disable_precreate &&
7471                         do_facet $SINGLEMDS "lctl set_param -n \
7472                                 osp.$ost*MDT0000.max_create_count=0"
7473
7474                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7475                         [ -f $DIR/$tdir/$idx ] && continue
7476                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
7477                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
7478                                 { cleanup_65k;
7479                                   error "setstripe $idx should succeed"; }
7480                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7481                 done
7482                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7483                 rmdir $DIR/$tdir
7484
7485                 do_facet $SINGLEMDS "lctl set_param -n \
7486                         osp.$ost*MDT0000.max_create_count=$max_count"
7487                 do_facet $SINGLEMDS "lctl set_param -n \
7488                         osp.$ost*MDT0000.create_count=$count"
7489                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7490                 echo $INACTIVE_OSC "is Activate"
7491
7492                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7493         done
7494 }
7495 run_test 65k "validate manual striping works properly with deactivated OSCs"
7496
7497 test_65l() { # bug 12836
7498         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7499
7500         test_mkdir -p $DIR/$tdir/test_dir
7501         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
7502         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7503 }
7504 run_test 65l "lfs find on -1 stripe dir ========================"
7505
7506 test_65m() {
7507         local layout=$(save_layout $MOUNT)
7508         $RUNAS $SETSTRIPE -c 2 $MOUNT && {
7509                 restore_layout $MOUNT $layout
7510                 error "setstripe should fail by non-root users"
7511         }
7512         true
7513 }
7514 run_test 65m "normal user can't set filesystem default stripe"
7515
7516 test_65n() {
7517         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.0) ]] ||
7518                 skip "Need MDS version at least 2.12.0"
7519         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7520
7521         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7522         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7523         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7524
7525         save_layout_restore_at_exit $MOUNT
7526
7527         # new subdirectory under root directory should not inherit
7528         # the default layout from root
7529         local dir1=$MOUNT/$tdir-1
7530         mkdir $dir1 || error "mkdir $dir1 failed"
7531         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7532                 error "$dir1 shouldn't have LOV EA"
7533
7534         # delete the default layout on root directory
7535         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7536
7537         local dir2=$MOUNT/$tdir-2
7538         mkdir $dir2 || error "mkdir $dir2 failed"
7539         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7540                 error "$dir2 shouldn't have LOV EA"
7541
7542         # set a new striping pattern on root directory
7543         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7544         local new_def_stripe_size=$((def_stripe_size * 2))
7545         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7546                 error "set stripe size on $MOUNT failed"
7547
7548         # new file created in $dir2 should inherit the new stripe size from
7549         # the filesystem default
7550         local file2=$dir2/$tfile-2
7551         touch $file2 || error "touch $file2 failed"
7552
7553         local file2_stripe_size=$($LFS getstripe -S $file2)
7554         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7555                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7556
7557         local dir3=$MOUNT/$tdir-3
7558         mkdir $dir3 || error "mkdir $dir3 failed"
7559         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
7560         # the root layout, which is the actual default layout that will be used
7561         # when new files are created in $dir3.
7562         local dir3_layout=$(get_layout_param $dir3)
7563         local root_dir_layout=$(get_layout_param $MOUNT)
7564         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
7565                 error "$dir3 should show the default layout from $MOUNT"
7566
7567         # set OST pool on root directory
7568         local pool=$TESTNAME
7569         pool_add $pool || error "add $pool failed"
7570         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7571                 error "add targets to $pool failed"
7572
7573         $LFS setstripe -p $pool $MOUNT ||
7574                 error "set OST pool on $MOUNT failed"
7575
7576         # new file created in $dir3 should inherit the pool from
7577         # the filesystem default
7578         local file3=$dir3/$tfile-3
7579         touch $file3 || error "touch $file3 failed"
7580
7581         local file3_pool=$($LFS getstripe -p $file3)
7582         [[ "$file3_pool" = "$pool" ]] ||
7583                 error "$file3 didn't inherit OST pool $pool"
7584
7585         local dir4=$MOUNT/$tdir-4
7586         mkdir $dir4 || error "mkdir $dir4 failed"
7587         local dir4_layout=$(get_layout_param $dir4)
7588         root_dir_layout=$(get_layout_param $MOUNT)
7589         echo "$LFS getstripe -d $dir4"
7590         $LFS getstripe -d $dir4
7591         echo "$LFS getstripe -d $MOUNT"
7592         $LFS getstripe -d $MOUNT
7593         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
7594                 error "$dir4 should show the default layout from $MOUNT"
7595
7596         # new file created in $dir4 should inherit the pool from
7597         # the filesystem default
7598         local file4=$dir4/$tfile-4
7599         touch $file4 || error "touch $file4 failed"
7600
7601         local file4_pool=$($LFS getstripe -p $file4)
7602         [[ "$file4_pool" = "$pool" ]] ||
7603                 error "$file4 didn't inherit OST pool $pool"
7604
7605         # new subdirectory under non-root directory should inherit
7606         # the default layout from its parent directory
7607         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7608                 error "set directory layout on $dir4 failed"
7609
7610         local dir5=$dir4/$tdir-5
7611         mkdir $dir5 || error "mkdir $dir5 failed"
7612
7613         dir4_layout=$(get_layout_param $dir4)
7614         local dir5_layout=$(get_layout_param $dir5)
7615         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7616                 error "$dir5 should inherit the default layout from $dir4"
7617
7618         # though subdir under ROOT doesn't inherit default layout, but
7619         # its sub dir/file should be created with default layout.
7620         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
7621         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
7622         [[ $MDS1_VERSION -ge $(version_code 2.12.3) &&
7623            $MDS1_VERSION -lt $(version_code 2.12.50) ]] ||
7624                 skip "Need MDS version at least 2.12.3 or 2.12.59"
7625
7626         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
7627         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
7628         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
7629
7630         if [ $default_lmv_hash == "none" ]; then
7631                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
7632         else
7633                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
7634                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
7635         fi
7636
7637         $LFS setdirstripe -D -c 2 $MOUNT ||
7638                 error "setdirstripe -D -c 2 failed"
7639         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
7640         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
7641         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
7642 }
7643 run_test 65n "don't inherit default layout from root for new subdirectories"
7644
7645 # bug 2543 - update blocks count on client
7646 test_66() {
7647         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7648
7649         COUNT=${COUNT:-8}
7650         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7651         sync; sync_all_data; sync; sync_all_data
7652         cancel_lru_locks osc
7653         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7654         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7655 }
7656 run_test 66 "update inode blocks count on client ==============="
7657
7658 meminfo() {
7659         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7660 }
7661
7662 swap_used() {
7663         swapon -s | awk '($1 == "'$1'") { print $4 }'
7664 }
7665
7666 # bug5265, obdfilter oa2dentry return -ENOENT
7667 # #define OBD_FAIL_SRV_ENOENT 0x217
7668 test_69() {
7669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7670         remote_ost_nodsh && skip "remote OST with nodsh"
7671
7672         f="$DIR/$tfile"
7673         $SETSTRIPE -c 1 -i 0 $f
7674
7675         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7676
7677         do_facet ost1 lctl set_param fail_loc=0x217
7678         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7679         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7680
7681         do_facet ost1 lctl set_param fail_loc=0
7682         $DIRECTIO write $f 0 2 || error "write error"
7683
7684         cancel_lru_locks osc
7685         $DIRECTIO read $f 0 1 || error "read error"
7686
7687         do_facet ost1 lctl set_param fail_loc=0x217
7688         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7689
7690         do_facet ost1 lctl set_param fail_loc=0
7691         rm -f $f
7692 }
7693 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
7694
7695 test_71() {
7696         test_mkdir $DIR/$tdir
7697         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
7698         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
7699 }
7700 run_test 71 "Running dbench on lustre (don't segment fault) ===="
7701
7702 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
7703         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7704         [ "$RUNAS_ID" = "$UID" ] &&
7705                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7706         # Check that testing environment is properly set up. Skip if not
7707         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
7708                 skip_env "User $RUNAS_ID does not exist - skipping"
7709
7710         touch $DIR/$tfile
7711         chmod 777 $DIR/$tfile
7712         chmod ug+s $DIR/$tfile
7713         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
7714                 error "$RUNAS dd $DIR/$tfile failed"
7715         # See if we are still setuid/sgid
7716         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7717                 error "S/gid is not dropped on write"
7718         # Now test that MDS is updated too
7719         cancel_lru_locks mdc
7720         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7721                 error "S/gid is not dropped on MDS"
7722         rm -f $DIR/$tfile
7723 }
7724 run_test 72a "Test that remove suid works properly (bug5695) ===="
7725
7726 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7727         local perm
7728
7729         [ "$RUNAS_ID" = "$UID" ] &&
7730                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7731         [ "$RUNAS_ID" -eq 0 ] &&
7732                 skip_env "RUNAS_ID = 0 -- skipping"
7733         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7734         # Check that testing environment is properly set up. Skip if not
7735         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7736                 skip_env "User $RUNAS_ID does not exist - skipping"
7737
7738         touch $DIR/${tfile}-f{g,u}
7739         test_mkdir $DIR/${tfile}-dg
7740         test_mkdir $DIR/${tfile}-du
7741         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7742         chmod g+s $DIR/${tfile}-{f,d}g
7743         chmod u+s $DIR/${tfile}-{f,d}u
7744         for perm in 777 2777 4777; do
7745                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7746                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7747                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7748                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7749         done
7750         true
7751 }
7752 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7753
7754 # bug 3462 - multiple simultaneous MDC requests
7755 test_73() {
7756         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7757
7758         test_mkdir $DIR/d73-1
7759         test_mkdir $DIR/d73-2
7760         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7761         pid1=$!
7762
7763         lctl set_param fail_loc=0x80000129
7764         $MULTIOP $DIR/d73-1/f73-2 Oc &
7765         sleep 1
7766         lctl set_param fail_loc=0
7767
7768         $MULTIOP $DIR/d73-2/f73-3 Oc &
7769         pid3=$!
7770
7771         kill -USR1 $pid1
7772         wait $pid1 || return 1
7773
7774         sleep 25
7775
7776         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7777         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7778         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7779
7780         rm -rf $DIR/d73-*
7781 }
7782 run_test 73 "multiple MDC requests (should not deadlock)"
7783
7784 test_74a() { # bug 6149, 6184
7785         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7786
7787         touch $DIR/f74a
7788         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7789         #
7790         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7791         # will spin in a tight reconnection loop
7792         $LCTL set_param fail_loc=0x8000030e
7793         # get any lock that won't be difficult - lookup works.
7794         ls $DIR/f74a
7795         $LCTL set_param fail_loc=0
7796         rm -f $DIR/f74a
7797         true
7798 }
7799 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
7800
7801 test_74b() { # bug 13310
7802         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7803
7804         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7805         #
7806         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7807         # will spin in a tight reconnection loop
7808         $LCTL set_param fail_loc=0x8000030e
7809         # get a "difficult" lock
7810         touch $DIR/f74b
7811         $LCTL set_param fail_loc=0
7812         rm -f $DIR/f74b
7813         true
7814 }
7815 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
7816
7817 test_74c() {
7818         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7819
7820         #define OBD_FAIL_LDLM_NEW_LOCK
7821         $LCTL set_param fail_loc=0x319
7822         touch $DIR/$tfile && error "touch successful"
7823         $LCTL set_param fail_loc=0
7824         true
7825 }
7826 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
7827
7828 num_inodes() {
7829         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
7830 }
7831
7832 test_76() { # Now for bug 20433, added originally in bug 1443
7833         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7834
7835         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
7836
7837         cancel_lru_locks osc
7838         BEFORE_INODES=$(num_inodes)
7839         echo "before inodes: $BEFORE_INODES"
7840         local COUNT=1000
7841         [ "$SLOW" = "no" ] && COUNT=100
7842         for i in $(seq $COUNT); do
7843                 touch $DIR/$tfile
7844                 rm -f $DIR/$tfile
7845         done
7846         cancel_lru_locks osc
7847         AFTER_INODES=$(num_inodes)
7848         echo "after inodes: $AFTER_INODES"
7849         local wait=0
7850         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
7851                 sleep 2
7852                 AFTER_INODES=$(num_inodes)
7853                 wait=$((wait+2))
7854                 echo "wait $wait seconds inodes: $AFTER_INODES"
7855                 if [ $wait -gt 30 ]; then
7856                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
7857                 fi
7858         done
7859 }
7860 run_test 76 "confirm clients recycle inodes properly ===="
7861
7862
7863 export ORIG_CSUM=""
7864 set_checksums()
7865 {
7866         # Note: in sptlrpc modes which enable its own bulk checksum, the
7867         # original crc32_le bulk checksum will be automatically disabled,
7868         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
7869         # will be checked by sptlrpc code against sptlrpc bulk checksum.
7870         # In this case set_checksums() will not be no-op, because sptlrpc
7871         # bulk checksum will be enabled all through the test.
7872
7873         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
7874         lctl set_param -n osc.*.checksums $1
7875         return 0
7876 }
7877
7878 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7879                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
7880 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7881                              tr -d [] | head -n1)}
7882 set_checksum_type()
7883 {
7884         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
7885         log "set checksum type to $1"
7886         return 0
7887 }
7888 F77_TMP=$TMP/f77-temp
7889 F77SZ=8
7890 setup_f77() {
7891         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
7892                 error "error writing to $F77_TMP"
7893 }
7894
7895 test_77a() { # bug 10889
7896         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7897         $GSS && skip_env "could not run with gss"
7898
7899         [ ! -f $F77_TMP ] && setup_f77
7900         set_checksums 1
7901         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
7902         set_checksums 0
7903         rm -f $DIR/$tfile
7904 }
7905 run_test 77a "normal checksum read/write operation"
7906
7907 test_77b() { # bug 10889
7908         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7909         $GSS && skip_env "could not run with gss"
7910
7911         [ ! -f $F77_TMP ] && setup_f77
7912         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7913         $LCTL set_param fail_loc=0x80000409
7914         set_checksums 1
7915
7916         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7917                 error "dd error: $?"
7918         $LCTL set_param fail_loc=0
7919
7920         for algo in $CKSUM_TYPES; do
7921                 cancel_lru_locks osc
7922                 set_checksum_type $algo
7923                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7924                 $LCTL set_param fail_loc=0x80000408
7925                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7926                 $LCTL set_param fail_loc=0
7927         done
7928         set_checksums 0
7929         set_checksum_type $ORIG_CSUM_TYPE
7930         rm -f $DIR/$tfile
7931 }
7932 run_test 77b "checksum error on client write, read"
7933
7934 cleanup_77c() {
7935         trap 0
7936         set_checksums 0
7937         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7938         $check_ost &&
7939                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7940         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7941         $check_ost && [ -n "$ost_file_prefix" ] &&
7942                 do_facet ost1 rm -f ${ost_file_prefix}\*
7943 }
7944
7945 test_77c() {
7946         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7947         $GSS && skip_env "could not run with gss"
7948         remote_ost_nodsh && skip "remote OST with nodsh"
7949
7950         local bad1
7951         local osc_file_prefix
7952         local osc_file
7953         local check_ost=false
7954         local ost_file_prefix
7955         local ost_file
7956         local orig_cksum
7957         local dump_cksum
7958         local fid
7959
7960         # ensure corruption will occur on first OSS/OST
7961         $LFS setstripe -i 0 $DIR/$tfile
7962
7963         [ ! -f $F77_TMP ] && setup_f77
7964         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7965                 error "dd write error: $?"
7966         fid=$($LFS path2fid $DIR/$tfile)
7967
7968         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
7969         then
7970                 check_ost=true
7971                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7972                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7973         else
7974                 echo "OSS do not support bulk pages dump upon error"
7975         fi
7976
7977         osc_file_prefix=$($LCTL get_param -n debug_path)
7978         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7979
7980         trap cleanup_77c EXIT
7981
7982         set_checksums 1
7983         # enable bulk pages dump upon error on Client
7984         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7985         # enable bulk pages dump upon error on OSS
7986         $check_ost &&
7987                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7988
7989         # flush Client cache to allow next read to reach OSS
7990         cancel_lru_locks osc
7991
7992         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7993         $LCTL set_param fail_loc=0x80000408
7994         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7995         $LCTL set_param fail_loc=0
7996
7997         rm -f $DIR/$tfile
7998
7999         # check cksum dump on Client
8000         osc_file=$(ls ${osc_file_prefix}*)
8001         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8002         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8003         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8004         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8005         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8006                      cksum)
8007         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8008         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8009                 error "dump content does not match on Client"
8010
8011         $check_ost || skip "No need to check cksum dump on OSS"
8012
8013         # check cksum dump on OSS
8014         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8015         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8016         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8017         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8018         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8019                 error "dump content does not match on OSS"
8020
8021         cleanup_77c
8022 }
8023 run_test 77c "checksum error on client read with debug"
8024
8025 test_77d() { # bug 10889
8026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8027         $GSS && skip_env "could not run with gss"
8028
8029         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8030         $LCTL set_param fail_loc=0x80000409
8031         set_checksums 1
8032         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8033                 error "direct write: rc=$?"
8034         $LCTL set_param fail_loc=0
8035         set_checksums 0
8036
8037         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8038         $LCTL set_param fail_loc=0x80000408
8039         set_checksums 1
8040         cancel_lru_locks osc
8041         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8042                 error "direct read: rc=$?"
8043         $LCTL set_param fail_loc=0
8044         set_checksums 0
8045 }
8046 run_test 77d "checksum error on OST direct write, read"
8047
8048 test_77f() { # bug 10889
8049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8050         $GSS && skip_env "could not run with gss"
8051
8052         set_checksums 1
8053         for algo in $CKSUM_TYPES; do
8054                 cancel_lru_locks osc
8055                 set_checksum_type $algo
8056                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8057                 $LCTL set_param fail_loc=0x409
8058                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8059                         error "direct write succeeded"
8060                 $LCTL set_param fail_loc=0
8061         done
8062         set_checksum_type $ORIG_CSUM_TYPE
8063         set_checksums 0
8064 }
8065 run_test 77f "repeat checksum error on write (expect error)"
8066
8067 test_77g() { # bug 10889
8068         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8069         $GSS && skip_env "could not run with gss"
8070         remote_ost_nodsh && skip "remote OST with nodsh"
8071
8072         [ ! -f $F77_TMP ] && setup_f77
8073
8074         local file=$DIR/$tfile
8075         stack_trap "rm -f $file" EXIT
8076
8077         $SETSTRIPE -c 1 -i 0 $file
8078         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8079         do_facet ost1 lctl set_param fail_loc=0x8000021a
8080         set_checksums 1
8081         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8082                 error "write error: rc=$?"
8083         do_facet ost1 lctl set_param fail_loc=0
8084         set_checksums 0
8085
8086         cancel_lru_locks osc
8087         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8088         do_facet ost1 lctl set_param fail_loc=0x8000021b
8089         set_checksums 1
8090         cmp $F77_TMP $file || error "file compare failed"
8091         do_facet ost1 lctl set_param fail_loc=0
8092         set_checksums 0
8093 }
8094 run_test 77g "checksum error on OST write, read"
8095
8096 test_77k() { # LU-10906
8097         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8098         $GSS && skip_env "could not run with gss"
8099
8100         local cksum_param="osc.$FSNAME*.checksums"
8101         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8102         local checksum
8103         local i
8104
8105         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8106         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8107         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8108                 EXIT
8109
8110         for i in 0 1; do
8111                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8112                         error "failed to set checksum=$i on MGS"
8113                 wait_update $HOSTNAME "$get_checksum" $i
8114                 #remount
8115                 echo "remount client, checksum should be $i"
8116                 remount_client $MOUNT || error "failed to remount client"
8117                 checksum=$(eval $get_checksum)
8118                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8119         done
8120
8121         for opt in "checksum" "nochecksum"; do
8122                 #remount with mount option
8123                 echo "remount client with option $opt, checksum should be $i"
8124                 umount_client $MOUNT || error "failed to umount client"
8125                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8126                         error "failed to mount client with option '$opt'"
8127                 checksum=$(eval $get_checksum)
8128                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8129                 i=$((i - 1))
8130         done
8131
8132         remount_client $MOUNT || error "failed to remount client"
8133 }
8134 run_test 77k "enable/disable checksum correctly"
8135
8136 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8137 rm -f $F77_TMP
8138 unset F77_TMP
8139
8140 cleanup_test_78() {
8141         trap 0
8142         rm -f $DIR/$tfile
8143 }
8144
8145 test_78() { # bug 10901
8146         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8147         remote_ost || skip_env "local OST"
8148
8149         NSEQ=5
8150         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8151         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8152         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8153         echo "MemTotal: $MEMTOTAL"
8154
8155         # reserve 256MB of memory for the kernel and other running processes,
8156         # and then take 1/2 of the remaining memory for the read/write buffers.
8157         if [ $MEMTOTAL -gt 512 ] ;then
8158                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8159         else
8160                 # for those poor memory-starved high-end clusters...
8161                 MEMTOTAL=$((MEMTOTAL / 2))
8162         fi
8163         echo "Mem to use for directio: $MEMTOTAL"
8164
8165         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8166         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8167         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8168         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8169                 head -n1)
8170         echo "Smallest OST: $SMALLESTOST"
8171         [[ $SMALLESTOST -lt 10240 ]] &&
8172                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8173
8174         trap cleanup_test_78 EXIT
8175
8176         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8177                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8178
8179         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8180         echo "File size: $F78SIZE"
8181         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8182         for i in $(seq 1 $NSEQ); do
8183                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8184                 echo directIO rdwr round $i of $NSEQ
8185                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8186         done
8187
8188         cleanup_test_78
8189 }
8190 run_test 78 "handle large O_DIRECT writes correctly ============"
8191
8192 test_79() { # bug 12743
8193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8194
8195         wait_delete_completed
8196
8197         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8198         BKFREE=$(calc_osc_kbytes kbytesfree)
8199         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8200
8201         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8202         DFTOTAL=`echo $STRING | cut -d, -f1`
8203         DFUSED=`echo $STRING  | cut -d, -f2`
8204         DFAVAIL=`echo $STRING | cut -d, -f3`
8205         DFFREE=$(($DFTOTAL - $DFUSED))
8206
8207         ALLOWANCE=$((64 * $OSTCOUNT))
8208
8209         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8210            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8211                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8212         fi
8213         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8214            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8215                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8216         fi
8217         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8218            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8219                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8220         fi
8221 }
8222 run_test 79 "df report consistency check ======================="
8223
8224 test_80() { # bug 10718
8225         remote_ost_nodsh && skip "remote OST with nodsh"
8226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8227
8228         # relax strong synchronous semantics for slow backends like ZFS
8229         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8230                 local soc="obdfilter.*.sync_lock_cancel"
8231                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8232
8233                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8234                 if [ -z "$save" ]; then
8235                         soc="obdfilter.*.sync_on_lock_cancel"
8236                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8237                 fi
8238
8239                 if [ "$save" != "never" ]; then
8240                         local hosts=$(comma_list $(osts_nodes))
8241
8242                         do_nodes $hosts $LCTL set_param $soc=never
8243                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8244                 fi
8245         fi
8246
8247         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8248         sync; sleep 1; sync
8249         local before=$(date +%s)
8250         cancel_lru_locks osc
8251         local after=$(date +%s)
8252         local diff=$((after - before))
8253         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8254
8255         rm -f $DIR/$tfile
8256 }
8257 run_test 80 "Page eviction is equally fast at high offsets too"
8258
8259 test_81a() { # LU-456
8260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8261         remote_ost_nodsh && skip "remote OST with nodsh"
8262
8263         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8264         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8265         do_facet ost1 lctl set_param fail_loc=0x80000228
8266
8267         # write should trigger a retry and success
8268         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8269         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8270         RC=$?
8271         if [ $RC -ne 0 ] ; then
8272                 error "write should success, but failed for $RC"
8273         fi
8274 }
8275 run_test 81a "OST should retry write when get -ENOSPC ==============="
8276
8277 test_81b() { # LU-456
8278         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8279         remote_ost_nodsh && skip "remote OST with nodsh"
8280
8281         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8282         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8283         do_facet ost1 lctl set_param fail_loc=0x228
8284
8285         # write should retry several times and return -ENOSPC finally
8286         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8287         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8288         RC=$?
8289         ENOSPC=28
8290         if [ $RC -ne $ENOSPC ] ; then
8291                 error "dd should fail for -ENOSPC, but succeed."
8292         fi
8293 }
8294 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8295
8296 test_82() { # LU-1031
8297         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8298         local gid1=14091995
8299         local gid2=16022000
8300
8301         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8302         local MULTIPID1=$!
8303         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8304         local MULTIPID2=$!
8305         kill -USR1 $MULTIPID2
8306         sleep 2
8307         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8308                 error "First grouplock does not block second one"
8309         else
8310                 echo "Second grouplock blocks first one"
8311         fi
8312         kill -USR1 $MULTIPID1
8313         wait $MULTIPID1
8314         wait $MULTIPID2
8315 }
8316 run_test 82 "Basic grouplock test"
8317
8318 test_99() {
8319         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8320
8321         test_mkdir $DIR/$tdir.cvsroot
8322         chown $RUNAS_ID $DIR/$tdir.cvsroot
8323
8324         cd $TMP
8325         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8326
8327         cd /etc/init.d
8328         # some versions of cvs import exit(1) when asked to import links or
8329         # files they can't read.  ignore those files.
8330         local toignore=$(find . -type l -printf '-I %f\n' -o \
8331                          ! -perm /4 -printf '-I %f\n')
8332         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8333                 $tdir.reposname vtag rtag
8334
8335         cd $DIR
8336         test_mkdir $DIR/$tdir.reposname
8337         chown $RUNAS_ID $DIR/$tdir.reposname
8338         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8339
8340         cd $DIR/$tdir.reposname
8341         $RUNAS touch foo99
8342         $RUNAS cvs add -m 'addmsg' foo99
8343         $RUNAS cvs update
8344         $RUNAS cvs commit -m 'nomsg' foo99
8345         rm -fr $DIR/$tdir.cvsroot
8346 }
8347 run_test 99 "cvs strange file/directory operations"
8348
8349 test_100() {
8350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8351         [[ "$NETTYPE" =~ tcp ]] ||
8352                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8353         remote_ost_nodsh && skip "remote OST with nodsh"
8354         remote_mds_nodsh && skip "remote MDS with nodsh"
8355         remote_servers ||
8356                 skip "useless for local single node setup"
8357
8358         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8359                 [ "$PROT" != "tcp" ] && continue
8360                 RPORT=$(echo $REMOTE | cut -d: -f2)
8361                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8362
8363                 rc=0
8364                 LPORT=`echo $LOCAL | cut -d: -f2`
8365                 if [ $LPORT -ge 1024 ]; then
8366                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8367                         netstat -tna
8368                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8369                 fi
8370         done
8371         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8372 }
8373 run_test 100 "check local port using privileged port ==========="
8374
8375 function get_named_value()
8376 {
8377     local tag
8378
8379     tag=$1
8380     while read ;do
8381         line=$REPLY
8382         case $line in
8383         $tag*)
8384             echo $line | sed "s/^$tag[ ]*//"
8385             break
8386             ;;
8387         esac
8388     done
8389 }
8390
8391 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8392                    awk '/^max_cached_mb/ { print $2 }')
8393
8394 cleanup_101a() {
8395         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8396         trap 0
8397 }
8398
8399 test_101a() {
8400         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8401         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
8402
8403         local s
8404         local discard
8405         local nreads=10000
8406         local cache_limit=32
8407
8408         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8409         trap cleanup_101a EXIT
8410         $LCTL set_param -n llite.*.read_ahead_stats 0
8411         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8412
8413         #
8414         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8415         #
8416         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8417         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8418
8419         discard=0
8420         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8421                 get_named_value 'read but discarded' | cut -d" " -f1); do
8422                         discard=$(($discard + $s))
8423         done
8424         cleanup_101a
8425
8426         if [[ $(($discard * 10)) -gt $nreads ]]; then
8427                 $LCTL get_param osc.*-osc*.rpc_stats
8428                 $LCTL get_param llite.*.read_ahead_stats
8429                 error "too many ($discard) discarded pages"
8430         fi
8431         rm -f $DIR/$tfile || true
8432 }
8433 run_test 101a "check read-ahead for random reads"
8434
8435 setup_test101bc() {
8436         test_mkdir $DIR/$tdir
8437         local STRIPE_SIZE=$1
8438         local FILE_LENGTH=$2
8439         STRIPE_OFFSET=0
8440
8441         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
8442
8443         local list=$(comma_list $(osts_nodes))
8444         set_osd_param $list '' read_cache_enable 0
8445         set_osd_param $list '' writethrough_cache_enable 0
8446
8447         trap cleanup_test101bc EXIT
8448         # prepare the read-ahead file
8449         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8450
8451         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
8452                                 count=$FILE_SIZE_MB 2> /dev/null
8453
8454 }
8455
8456 cleanup_test101bc() {
8457         trap 0
8458         rm -rf $DIR/$tdir
8459         rm -f $DIR/$tfile
8460
8461         local list=$(comma_list $(osts_nodes))
8462         set_osd_param $list '' read_cache_enable 1
8463         set_osd_param $list '' writethrough_cache_enable 1
8464 }
8465
8466 calc_total() {
8467         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8468 }
8469
8470 ra_check_101() {
8471         local READ_SIZE=$1
8472         local STRIPE_SIZE=$2
8473         local FILE_LENGTH=$3
8474         local RA_INC=1048576
8475         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8476         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8477                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8478         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8479                         get_named_value 'read but discarded' |
8480                         cut -d" " -f1 | calc_total)
8481         if [[ $DISCARD -gt $discard_limit ]]; then
8482                 $LCTL get_param llite.*.read_ahead_stats
8483                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8484         else
8485                 echo "Read-ahead success for size ${READ_SIZE}"
8486         fi
8487 }
8488
8489 test_101b() {
8490         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8491         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8492
8493         local STRIPE_SIZE=1048576
8494         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8495
8496         if [ $SLOW == "yes" ]; then
8497                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8498         else
8499                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8500         fi
8501
8502         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8503
8504         # prepare the read-ahead file
8505         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8506         cancel_lru_locks osc
8507         for BIDX in 2 4 8 16 32 64 128 256
8508         do
8509                 local BSIZE=$((BIDX*4096))
8510                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8511                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8512                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8513                 $LCTL set_param -n llite.*.read_ahead_stats 0
8514                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8515                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8516                 cancel_lru_locks osc
8517                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8518         done
8519         cleanup_test101bc
8520         true
8521 }
8522 run_test 101b "check stride-io mode read-ahead ================="
8523
8524 test_101c() {
8525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8526
8527         local STRIPE_SIZE=1048576
8528         local FILE_LENGTH=$((STRIPE_SIZE*100))
8529         local nreads=10000
8530         local rsize=65536
8531         local osc_rpc_stats
8532
8533         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8534
8535         cancel_lru_locks osc
8536         $LCTL set_param osc.*.rpc_stats 0
8537         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8538         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8539                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8540                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8541                 local size
8542
8543                 if [ $lines -le 20 ]; then
8544                         continue
8545                 fi
8546                 for size in 1 2 4 8; do
8547                         local rpc=$(echo "$stats" |
8548                                     awk '($1 == "'$size':") {print $2; exit; }')
8549                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8550                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8551                 done
8552                 echo "$osc_rpc_stats check passed!"
8553         done
8554         cleanup_test101bc
8555         true
8556 }
8557 run_test 101c "check stripe_size aligned read-ahead ================="
8558
8559 set_read_ahead() {
8560         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8561         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8562 }
8563
8564 test_101d() {
8565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8566
8567         local file=$DIR/$tfile
8568         local sz_MB=${FILESIZE_101d:-500}
8569         local ra_MB=${READAHEAD_MB:-40}
8570
8571         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8572         [ $free_MB -lt $sz_MB ] &&
8573                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8574
8575         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8576         $SETSTRIPE -c -1 $file || error "setstripe failed"
8577
8578         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8579         echo Cancel LRU locks on lustre client to flush the client cache
8580         cancel_lru_locks osc
8581
8582         echo Disable read-ahead
8583         local old_READAHEAD=$(set_read_ahead 0)
8584
8585         echo Reading the test file $file with read-ahead disabled
8586         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8587
8588         echo Cancel LRU locks on lustre client to flush the client cache
8589         cancel_lru_locks osc
8590         echo Enable read-ahead with ${ra_MB}MB
8591         set_read_ahead $ra_MB
8592
8593         echo Reading the test file $file with read-ahead enabled
8594         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8595
8596         echo "read-ahead disabled time read $raOFF"
8597         echo "read-ahead enabled  time read $raON"
8598
8599         set_read_ahead $old_READAHEAD
8600         rm -f $file
8601         wait_delete_completed
8602
8603         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
8604                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8605 }
8606 run_test 101d "file read with and without read-ahead enabled"
8607
8608 test_101e() {
8609         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8610
8611         local file=$DIR/$tfile
8612         local size_KB=500  #KB
8613         local count=100
8614         local bsize=1024
8615
8616         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8617         local need_KB=$((count * size_KB))
8618         [[ $free_KB -le $need_KB ]] &&
8619                 skip_env "Need free space $need_KB, have $free_KB"
8620
8621         echo "Creating $count ${size_KB}K test files"
8622         for ((i = 0; i < $count; i++)); do
8623                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8624         done
8625
8626         echo "Cancel LRU locks on lustre client to flush the client cache"
8627         cancel_lru_locks $OSC
8628
8629         echo "Reset readahead stats"
8630         $LCTL set_param -n llite.*.read_ahead_stats 0
8631
8632         for ((i = 0; i < $count; i++)); do
8633                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8634         done
8635
8636         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8637                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8638
8639         for ((i = 0; i < $count; i++)); do
8640                 rm -rf $file.$i 2>/dev/null
8641         done
8642
8643         #10000 means 20% reads are missing in readahead
8644         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8645 }
8646 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
8647
8648 test_101f() {
8649         which iozone || skip_env "no iozone installed"
8650
8651         local old_debug=$($LCTL get_param debug)
8652         old_debug=${old_debug#*=}
8653         $LCTL set_param debug="reada mmap"
8654
8655         # create a test file
8656         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
8657
8658         echo Cancel LRU locks on lustre client to flush the client cache
8659         cancel_lru_locks osc
8660
8661         echo Reset readahead stats
8662         $LCTL set_param -n llite.*.read_ahead_stats 0
8663
8664         echo mmap read the file with small block size
8665         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
8666                 > /dev/null 2>&1
8667
8668         echo checking missing pages
8669         $LCTL get_param llite.*.read_ahead_stats
8670         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8671                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
8672
8673         $LCTL set_param debug="$old_debug"
8674         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
8675         rm -f $DIR/$tfile
8676 }
8677 run_test 101f "check mmap read performance"
8678
8679 test_101g_brw_size_test() {
8680         local mb=$1
8681         local pages=$((mb * 1048576 / PAGE_SIZE))
8682         local file=$DIR/$tfile
8683
8684         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
8685                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
8686         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
8687                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
8688                         return 2
8689         done
8690
8691         stack_trap "rm -f $file" EXIT
8692         $LCTL set_param -n osc.*.rpc_stats=0
8693
8694         # 10 RPCs should be enough for the test
8695         local count=10
8696         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
8697                 { error "dd write ${mb} MB blocks failed"; return 3; }
8698         cancel_lru_locks osc
8699         dd of=/dev/null if=$file bs=${mb}M count=$count ||
8700                 { error "dd write ${mb} MB blocks failed"; return 4; }
8701
8702         # calculate number of full-sized read and write RPCs
8703         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
8704                 sed -n '/pages per rpc/,/^$/p' |
8705                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
8706                 END { print reads,writes }'))
8707         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
8708                 return 5
8709         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
8710                 return 6
8711
8712         return 0
8713 }
8714
8715 test_101g() {
8716         remote_ost_nodsh && skip "remote OST with nodsh"
8717
8718         local rpcs
8719         local osts=$(get_facets OST)
8720         local list=$(comma_list $(osts_nodes))
8721         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
8722         local brw_size="obdfilter.*.brw_size"
8723
8724         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8725
8726         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
8727         if [ $OST1_VERSION -ge $(version_code 2.8.52) -o \
8728              \( $OST1_VERSION -ge $(version_code 2.7.17) -a \
8729                 $OST1_VERSION -lt $(version_code 2.7.50) \) ] &&
8730            [ $CLIENT_VERSION -ge $(version_code 2.8.52) -o \
8731              \( $CLIENT_VERSION -ge $(version_code 2.7.17) -a \
8732                 $CLIENT_VERSION -lt $(version_code 2.7.50) \) ]; then
8733                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] && suffix="M"
8734                 if [[ $orig_mb -lt 16 ]]; then
8735                         save_lustre_params $osts "$brw_size" > $p
8736                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8737                                 error "set 16MB RPC size failed"
8738
8739                         echo "remount client to enable new RPC size"
8740                         remount_client $MOUNT || error "remount_client failed"
8741                 fi
8742
8743                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8744                 # should be able to set brw_size=12, but no rpc_stats for that
8745                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
8746         fi
8747
8748         test_101g_brw_size_test 4 || error "4MB RPC test failed"
8749
8750         if [[ $orig_mb -lt 16 ]]; then
8751                 restore_lustre_params < $p
8752                 remount_client $MOUNT || error "remount_client restore failed"
8753         fi
8754
8755         rm -f $p $DIR/$tfile
8756 }
8757 run_test 101g "Big bulk(4/16 MiB) readahead"
8758
8759 setup_test102() {
8760         test_mkdir $DIR/$tdir
8761         chown $RUNAS_ID $DIR/$tdir
8762         STRIPE_SIZE=65536
8763         STRIPE_OFFSET=1
8764         STRIPE_COUNT=$OSTCOUNT
8765         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
8766
8767         trap cleanup_test102 EXIT
8768         cd $DIR
8769         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
8770         cd $DIR/$tdir
8771         for num in 1 2 3 4; do
8772                 for count in $(seq 1 $STRIPE_COUNT); do
8773                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
8774                                 local size=`expr $STRIPE_SIZE \* $num`
8775                                 local file=file"$num-$idx-$count"
8776                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
8777                         done
8778                 done
8779         done
8780
8781         cd $DIR
8782         $1 tar cf $TMP/f102.tar $tdir --xattrs
8783 }
8784
8785 cleanup_test102() {
8786         trap 0
8787         rm -f $TMP/f102.tar
8788         rm -rf $DIR/d0.sanity/d102
8789 }
8790
8791 test_102a() {
8792         [ "$UID" != 0 ] && skip "must run as root"
8793         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
8794                 skip_env "must have user_xattr"
8795
8796         [ -z "$(which setfattr 2>/dev/null)" ] &&
8797                 skip_env "could not find setfattr"
8798
8799         local testfile=$DIR/$tfile
8800
8801         touch $testfile
8802         echo "set/get xattr..."
8803         setfattr -n trusted.name1 -v value1 $testfile ||
8804                 error "setfattr -n trusted.name1=value1 $testfile failed"
8805         getfattr -n trusted.name1 $testfile 2> /dev/null |
8806           grep "trusted.name1=.value1" ||
8807                 error "$testfile missing trusted.name1=value1"
8808
8809         setfattr -n user.author1 -v author1 $testfile ||
8810                 error "setfattr -n user.author1=author1 $testfile failed"
8811         getfattr -n user.author1 $testfile 2> /dev/null |
8812           grep "user.author1=.author1" ||
8813                 error "$testfile missing trusted.author1=author1"
8814
8815         echo "listxattr..."
8816         setfattr -n trusted.name2 -v value2 $testfile ||
8817                 error "$testfile unable to set trusted.name2"
8818         setfattr -n trusted.name3 -v value3 $testfile ||
8819                 error "$testfile unable to set trusted.name3"
8820         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
8821             grep "trusted.name" | wc -l) -eq 3 ] ||
8822                 error "$testfile missing 3 trusted.name xattrs"
8823
8824         setfattr -n user.author2 -v author2 $testfile ||
8825                 error "$testfile unable to set user.author2"
8826         setfattr -n user.author3 -v author3 $testfile ||
8827                 error "$testfile unable to set user.author3"
8828         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
8829             grep "user.author" | wc -l) -eq 3 ] ||
8830                 error "$testfile missing 3 user.author xattrs"
8831
8832         echo "remove xattr..."
8833         setfattr -x trusted.name1 $testfile ||
8834                 error "$testfile error deleting trusted.name1"
8835         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
8836                 error "$testfile did not delete trusted.name1 xattr"
8837
8838         setfattr -x user.author1 $testfile ||
8839                 error "$testfile error deleting user.author1"
8840         echo "set lustre special xattr ..."
8841         $LFS setstripe -c1 $testfile
8842         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
8843                 awk -F "=" '/trusted.lov/ { print $2 }' )
8844         setfattr -n "trusted.lov" -v $lovea $testfile ||
8845                 error "$testfile doesn't ignore setting trusted.lov again"
8846         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
8847                 error "$testfile allow setting invalid trusted.lov"
8848         rm -f $testfile
8849 }
8850 run_test 102a "user xattr test =================================="
8851
8852 check_102b_layout() {
8853         local layout="$*"
8854         local testfile=$DIR/$tfile
8855
8856         echo "test layout '$layout'"
8857         $LFS setstripe $layout $testfile || error "setstripe failed"
8858         $LFS getstripe -y $testfile
8859
8860         echo "get/set/list trusted.lov xattr ..." # b=10930
8861         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
8862         [[ "$value" =~ "trusted.lov" ]] ||
8863                 error "can't get trusted.lov from $testfile"
8864         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
8865                 error "getstripe failed"
8866
8867         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
8868
8869         value=$(cut -d= -f2 <<<$value)
8870         # LU-13168: truncated xattr should fail if short lov_user_md header
8871         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
8872                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
8873         for len in $lens; do
8874                 echo "setfattr $len $testfile.2"
8875                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
8876                         [ $len -lt 66 ] && error "short xattr len=$len worked"
8877         done
8878         local stripe_size=$($LFS getstripe -S $testfile.2)
8879         local stripe_count=$($LFS getstripe -c $testfile.2)
8880         [[ $stripe_size -eq 65536 ]] ||
8881                 error "stripe size $stripe_size != 65536"
8882         [[ $stripe_count -eq $stripe_count_orig ]] ||
8883                 error "stripe count $stripe_count != $stripe_count_orig"
8884         rm $testfile $testfile.2
8885 }
8886
8887 test_102b() {
8888         [ -z "$(which setfattr 2>/dev/null)" ] &&
8889                 skip_env "could not find setfattr"
8890         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8891
8892         # check plain layout
8893         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
8894
8895         # and also check composite layout
8896         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
8897
8898 }
8899 run_test 102b "getfattr/setfattr for trusted.lov EAs"
8900
8901 test_102c() {
8902         [ -z "$(which setfattr 2>/dev/null)" ] &&
8903                 skip_env "could not find setfattr"
8904         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8905
8906         # b10930: get/set/list lustre.lov xattr
8907         echo "get/set/list lustre.lov xattr ..."
8908         test_mkdir $DIR/$tdir
8909         chown $RUNAS_ID $DIR/$tdir
8910         local testfile=$DIR/$tdir/$tfile
8911         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8912                 error "setstripe failed"
8913         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
8914                 error "getstripe failed"
8915         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
8916         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8917
8918         local testfile2=${testfile}2
8919         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8920                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8921
8922         $RUNAS $MCREATE $testfile2
8923         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8924         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
8925         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
8926         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8927         [ $stripe_count -eq $STRIPECOUNT ] ||
8928                 error "stripe count $stripe_count != $STRIPECOUNT"
8929 }
8930 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8931
8932 compare_stripe_info1() {
8933         local stripe_index_all_zero=true
8934
8935         for num in 1 2 3 4; do
8936                 for count in $(seq 1 $STRIPE_COUNT); do
8937                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8938                                 local size=$((STRIPE_SIZE * num))
8939                                 local file=file"$num-$offset-$count"
8940                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8941                                 [[ $stripe_size -ne $size ]] &&
8942                                     error "$file: size $stripe_size != $size"
8943                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8944                                 # allow fewer stripes to be created, ORI-601
8945                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8946                                     error "$file: count $stripe_count != $count"
8947                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8948                                 [[ $stripe_index -ne 0 ]] &&
8949                                         stripe_index_all_zero=false
8950                         done
8951                 done
8952         done
8953         $stripe_index_all_zero &&
8954                 error "all files are being extracted starting from OST index 0"
8955         return 0
8956 }
8957
8958 have_xattrs_include() {
8959         tar --help | grep -q xattrs-include &&
8960                 echo --xattrs-include="lustre.*"
8961 }
8962
8963 test_102d() {
8964         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8965         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8966
8967         XINC=$(have_xattrs_include)
8968         setup_test102
8969         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8970         cd $DIR/$tdir/$tdir
8971         compare_stripe_info1
8972 }
8973 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8974
8975 test_102f() {
8976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8977         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8978
8979         XINC=$(have_xattrs_include)
8980         setup_test102
8981         test_mkdir $DIR/$tdir.restore
8982         cd $DIR
8983         tar cf - --xattrs $tdir | tar xf - \
8984                 -C $DIR/$tdir.restore --xattrs $XINC
8985         cd $DIR/$tdir.restore/$tdir
8986         compare_stripe_info1
8987 }
8988 run_test 102f "tar copy files, not keep osts"
8989
8990 grow_xattr() {
8991         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8992                 skip "must have user_xattr"
8993         [ -z "$(which setfattr 2>/dev/null)" ] &&
8994                 skip_env "could not find setfattr"
8995         [ -z "$(which getfattr 2>/dev/null)" ] &&
8996                 skip_env "could not find getfattr"
8997
8998         local xsize=${1:-1024}  # in bytes
8999         local file=$DIR/$tfile
9000         local value="$(generate_string $xsize)"
9001         local xbig=trusted.big
9002
9003         touch $file
9004         log "save $xbig on $file"
9005         setfattr -n $xbig -v $value $file ||
9006                 error "saving $xbig on $file failed"
9007
9008         local orig=$(get_xattr_value $xbig $file)
9009         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9010
9011         local xsml=trusted.sml
9012         log "save $xsml on $file"
9013         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9014
9015         local new=$(get_xattr_value $xbig $file)
9016         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9017
9018         log "grow $xsml on $file"
9019         setfattr -n $xsml -v "$value" $file ||
9020                 error "growing $xsml on $file failed"
9021
9022         new=$(get_xattr_value $xbig $file)
9023         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9024         log "$xbig still valid after growing $xsml"
9025
9026         rm -f $file
9027 }
9028
9029 test_102h() { # bug 15777
9030         grow_xattr 1024
9031 }
9032 run_test 102h "grow xattr from inside inode to external block"
9033
9034 test_102ha() {
9035         large_xattr_enabled || skip_env "ea_inode feature disabled"
9036
9037         grow_xattr $(max_xattr_size)
9038 }
9039 run_test 102ha "grow xattr from inside inode to external inode"
9040
9041 test_102i() { # bug 17038
9042         [ -z "$(which getfattr 2>/dev/null)" ] &&
9043                 skip "could not find getfattr"
9044
9045         touch $DIR/$tfile
9046         ln -s $DIR/$tfile $DIR/${tfile}link
9047         getfattr -n trusted.lov $DIR/$tfile ||
9048                 error "lgetxattr on $DIR/$tfile failed"
9049         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9050                 grep -i "no such attr" ||
9051                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9052         rm -f $DIR/$tfile $DIR/${tfile}link
9053 }
9054 run_test 102i "lgetxattr test on symbolic link ============"
9055
9056 test_102j() {
9057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9058         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9059
9060         XINC=$(have_xattrs_include)
9061         setup_test102 "$RUNAS"
9062         chown $RUNAS_ID $DIR/$tdir
9063         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9064         cd $DIR/$tdir/$tdir
9065         compare_stripe_info1 "$RUNAS"
9066 }
9067 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9068
9069 test_102k() {
9070         [ -z "$(which setfattr 2>/dev/null)" ] &&
9071                 skip "could not find setfattr"
9072
9073         touch $DIR/$tfile
9074         # b22187 just check that does not crash for regular file.
9075         setfattr -n trusted.lov $DIR/$tfile
9076         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9077         local test_kdir=$DIR/$tdir
9078         test_mkdir $test_kdir
9079         local default_size=$($LFS getstripe -S $test_kdir)
9080         local default_count=$($LFS getstripe -c $test_kdir)
9081         local default_offset=$($LFS getstripe -i $test_kdir)
9082         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9083                 error 'dir setstripe failed'
9084         setfattr -n trusted.lov $test_kdir
9085         local stripe_size=$($LFS getstripe -S $test_kdir)
9086         local stripe_count=$($LFS getstripe -c $test_kdir)
9087         local stripe_offset=$($LFS getstripe -i $test_kdir)
9088         [ $stripe_size -eq $default_size ] ||
9089                 error "stripe size $stripe_size != $default_size"
9090         [ $stripe_count -eq $default_count ] ||
9091                 error "stripe count $stripe_count != $default_count"
9092         [ $stripe_offset -eq $default_offset ] ||
9093                 error "stripe offset $stripe_offset != $default_offset"
9094         rm -rf $DIR/$tfile $test_kdir
9095 }
9096 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9097
9098 test_102l() {
9099         [ -z "$(which getfattr 2>/dev/null)" ] &&
9100                 skip "could not find getfattr"
9101
9102         # LU-532 trusted. xattr is invisible to non-root
9103         local testfile=$DIR/$tfile
9104
9105         touch $testfile
9106
9107         echo "listxattr as user..."
9108         chown $RUNAS_ID $testfile
9109         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9110             grep -q "trusted" &&
9111                 error "$testfile trusted xattrs are user visible"
9112
9113         return 0;
9114 }
9115 run_test 102l "listxattr size test =================================="
9116
9117 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9118         local path=$DIR/$tfile
9119         touch $path
9120
9121         listxattr_size_check $path || error "listattr_size_check $path failed"
9122 }
9123 run_test 102m "Ensure listxattr fails on small bufffer ========"
9124
9125 cleanup_test102
9126
9127 getxattr() { # getxattr path name
9128         # Return the base64 encoding of the value of xattr name on path.
9129         local path=$1
9130         local name=$2
9131
9132         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9133         # file: $path
9134         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9135         #
9136         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9137
9138         getfattr --absolute-names --encoding=base64 --name=$name $path |
9139                 awk -F= -v name=$name '$1 == name {
9140                         print substr($0, index($0, "=") + 1);
9141         }'
9142 }
9143
9144 test_102n() { # LU-4101 mdt: protect internal xattrs
9145         [ -z "$(which setfattr 2>/dev/null)" ] &&
9146                 skip "could not find setfattr"
9147         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9148         then
9149                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9150         fi
9151
9152         local file0=$DIR/$tfile.0
9153         local file1=$DIR/$tfile.1
9154         local xattr0=$TMP/$tfile.0
9155         local xattr1=$TMP/$tfile.1
9156         local namelist="lov lma lmv link fid version som hsm"
9157         local name
9158         local value
9159
9160         rm -rf $file0 $file1 $xattr0 $xattr1
9161         touch $file0 $file1
9162
9163         # Get 'before' xattrs of $file1.
9164         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9165
9166         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9167                 namelist+=" lfsck_namespace"
9168         for name in $namelist; do
9169                 # Try to copy xattr from $file0 to $file1.
9170                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9171
9172                 setfattr --name=trusted.$name --value="$value" $file1 ||
9173                         error "setxattr 'trusted.$name' failed"
9174
9175                 # Try to set a garbage xattr.
9176                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9177
9178                 if [[ x$name == "xlov" ]]; then
9179                         setfattr --name=trusted.lov --value="$value" $file1 &&
9180                         error "setxattr invalid 'trusted.lov' success"
9181                 else
9182                         setfattr --name=trusted.$name --value="$value" $file1 ||
9183                                 error "setxattr invalid 'trusted.$name' failed"
9184                 fi
9185
9186                 # Try to remove the xattr from $file1. We don't care if this
9187                 # appears to succeed or fail, we just don't want there to be
9188                 # any changes or crashes.
9189                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9190         done
9191
9192         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9193         then
9194                 name="lfsck_ns"
9195                 # Try to copy xattr from $file0 to $file1.
9196                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9197
9198                 setfattr --name=trusted.$name --value="$value" $file1 ||
9199                         error "setxattr 'trusted.$name' failed"
9200
9201                 # Try to set a garbage xattr.
9202                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9203
9204                 setfattr --name=trusted.$name --value="$value" $file1 ||
9205                         error "setxattr 'trusted.$name' failed"
9206
9207                 # Try to remove the xattr from $file1. We don't care if this
9208                 # appears to succeed or fail, we just don't want there to be
9209                 # any changes or crashes.
9210                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9211         fi
9212
9213         # Get 'after' xattrs of file1.
9214         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9215
9216         if ! diff $xattr0 $xattr1; then
9217                 error "before and after xattrs of '$file1' differ"
9218         fi
9219
9220         rm -rf $file0 $file1 $xattr0 $xattr1
9221
9222         return 0
9223 }
9224 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9225
9226 test_102p() { # LU-4703 setxattr did not check ownership
9227         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9228                 skip "MDS needs to be at least 2.5.56"
9229
9230         local testfile=$DIR/$tfile
9231
9232         touch $testfile
9233
9234         echo "setfacl as user..."
9235         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9236         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9237
9238         echo "setfattr as user..."
9239         setfacl -m "u:$RUNAS_ID:---" $testfile
9240         $RUNAS setfattr -x system.posix_acl_access $testfile
9241         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9242 }
9243 run_test 102p "check setxattr(2) correctly fails without permission"
9244
9245 test_102q() {
9246         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9247                 skip "MDS needs to be at least 2.6.92"
9248
9249         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9250 }
9251 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9252
9253 test_102r() {
9254         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9255                 skip "MDS needs to be at least 2.6.93"
9256
9257         touch $DIR/$tfile || error "touch"
9258         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9259         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9260         rm $DIR/$tfile || error "rm"
9261
9262         #normal directory
9263         mkdir -p $DIR/$tdir || error "mkdir"
9264         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9265         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9266         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9267                 error "$testfile error deleting user.author1"
9268         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9269                 grep "user.$(basename $tdir)" &&
9270                 error "$tdir did not delete user.$(basename $tdir)"
9271         rmdir $DIR/$tdir || error "rmdir"
9272
9273         #striped directory
9274         test_mkdir $DIR/$tdir
9275         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9276         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9277         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9278                 error "$testfile error deleting user.author1"
9279         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9280                 grep "user.$(basename $tdir)" &&
9281                 error "$tdir did not delete user.$(basename $tdir)"
9282         rmdir $DIR/$tdir || error "rm striped dir"
9283 }
9284 run_test 102r "set EAs with empty values"
9285
9286 test_102s() {
9287         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9288                 skip "MDS needs to be at least 2.11.52"
9289
9290         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9291
9292         save_lustre_params client "llite.*.xattr_cache" > $save
9293
9294         for cache in 0 1; do
9295                 lctl set_param llite.*.xattr_cache=$cache
9296
9297                 rm -f $DIR/$tfile
9298                 touch $DIR/$tfile || error "touch"
9299                 for prefix in lustre security system trusted user; do
9300                         # Note getxattr() may fail with 'Operation not
9301                         # supported' or 'No such attribute' depending
9302                         # on prefix and cache.
9303                         getfattr -n $prefix.n102s $DIR/$tfile &&
9304                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9305                 done
9306         done
9307
9308         restore_lustre_params < $save
9309 }
9310 run_test 102s "getting nonexistent xattrs should fail"
9311
9312 test_102t() {
9313         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9314                 skip "MDS needs to be at least 2.11.52"
9315
9316         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9317
9318         save_lustre_params client "llite.*.xattr_cache" > $save
9319
9320         for cache in 0 1; do
9321                 lctl set_param llite.*.xattr_cache=$cache
9322
9323                 for buf_size in 0 256; do
9324                         rm -f $DIR/$tfile
9325                         touch $DIR/$tfile || error "touch"
9326                         setfattr -n user.multiop $DIR/$tfile
9327                         $MULTIOP $DIR/$tfile oa$buf_size ||
9328                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9329                 done
9330         done
9331
9332         restore_lustre_params < $save
9333 }
9334 run_test 102t "zero length xattr values handled correctly"
9335
9336 run_acl_subtest()
9337 {
9338     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9339     return $?
9340 }
9341
9342 test_103a() {
9343         [ "$UID" != 0 ] && skip "must run as root"
9344         $GSS && skip_env "could not run under gss"
9345         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9346                 skip_env "must have acl enabled"
9347         [ -z "$(which setfacl 2>/dev/null)" ] &&
9348                 skip_env "could not find setfacl"
9349         remote_mds_nodsh && skip "remote MDS with nodsh"
9350
9351         gpasswd -a daemon bin                           # LU-5641
9352         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9353
9354         declare -a identity_old
9355
9356         for num in $(seq $MDSCOUNT); do
9357                 switch_identity $num true || identity_old[$num]=$?
9358         done
9359
9360         SAVE_UMASK=$(umask)
9361         umask 0022
9362         mkdir -p $DIR/$tdir
9363         cd $DIR/$tdir
9364
9365         echo "performing cp ..."
9366         run_acl_subtest cp || error "run_acl_subtest cp failed"
9367         echo "performing getfacl-noacl..."
9368         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9369         echo "performing misc..."
9370         run_acl_subtest misc || error  "misc test failed"
9371         echo "performing permissions..."
9372         run_acl_subtest permissions || error "permissions failed"
9373         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9374         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9375                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9376                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9377         then
9378                 echo "performing permissions xattr..."
9379                 run_acl_subtest permissions_xattr ||
9380                         error "permissions_xattr failed"
9381         fi
9382         echo "performing setfacl..."
9383         run_acl_subtest setfacl || error  "setfacl test failed"
9384
9385         # inheritance test got from HP
9386         echo "performing inheritance..."
9387         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9388         chmod +x make-tree || error "chmod +x failed"
9389         run_acl_subtest inheritance || error "inheritance test failed"
9390         rm -f make-tree
9391
9392         echo "LU-974 ignore umask when acl is enabled..."
9393         run_acl_subtest 974 || error "LU-974 umask test failed"
9394         if [ $MDSCOUNT -ge 2 ]; then
9395                 run_acl_subtest 974_remote ||
9396                         error "LU-974 umask test failed under remote dir"
9397         fi
9398
9399         echo "LU-2561 newly created file is same size as directory..."
9400         if [ "$mds1_FSTYPE" != "zfs" ]; then
9401                 run_acl_subtest 2561 || error "LU-2561 test failed"
9402         else
9403                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9404         fi
9405
9406         run_acl_subtest 4924 || error "LU-4924 test failed"
9407
9408         cd $SAVE_PWD
9409         umask $SAVE_UMASK
9410
9411         for num in $(seq $MDSCOUNT); do
9412                 if [ "${identity_old[$num]}" = 1 ]; then
9413                         switch_identity $num false || identity_old[$num]=$?
9414                 fi
9415         done
9416 }
9417 run_test 103a "acl test"
9418
9419 test_103b() {
9420         declare -a pids
9421         local U
9422
9423         for U in {0..511}; do
9424                 {
9425                 local O=$(printf "%04o" $U)
9426
9427                 umask $(printf "%04o" $((511 ^ $O)))
9428                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9429                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9430
9431                 (( $S == ($O & 0666) )) ||
9432                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9433
9434                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9435                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9436                 (( $S == ($O & 0666) )) ||
9437                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9438
9439                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9440                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9441                 (( $S == ($O & 0666) )) ||
9442                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9443                 rm -f $DIR/$tfile.[smp]$0
9444                 } &
9445                 local pid=$!
9446
9447                 # limit the concurrently running threads to 64. LU-11878
9448                 local idx=$((U % 64))
9449                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9450                 pids[idx]=$pid
9451         done
9452         wait
9453 }
9454 run_test 103b "umask lfs setstripe"
9455
9456 test_103c() {
9457         mkdir -p $DIR/$tdir
9458         cp -rp $DIR/$tdir $DIR/$tdir.bak
9459
9460         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9461                 error "$DIR/$tdir shouldn't contain default ACL"
9462         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9463                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9464         true
9465 }
9466 run_test 103c "'cp -rp' won't set empty acl"
9467
9468 test_104a() {
9469         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9470
9471         touch $DIR/$tfile
9472         lfs df || error "lfs df failed"
9473         lfs df -ih || error "lfs df -ih failed"
9474         lfs df -h $DIR || error "lfs df -h $DIR failed"
9475         lfs df -i $DIR || error "lfs df -i $DIR failed"
9476         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9477         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9478
9479         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9480         lctl --device %$OSC deactivate
9481         lfs df || error "lfs df with deactivated OSC failed"
9482         lctl --device %$OSC activate
9483         # wait the osc back to normal
9484         wait_osc_import_ready client ost
9485
9486         lfs df || error "lfs df with reactivated OSC failed"
9487         rm -f $DIR/$tfile
9488 }
9489 run_test 104a "lfs df [-ih] [path] test ========================="
9490
9491 test_104b() {
9492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9493         [ $RUNAS_ID -eq $UID ] &&
9494                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9495
9496         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9497                         grep "Permission denied" | wc -l)))
9498         if [ $denied_cnt -ne 0 ]; then
9499                 error "lfs check servers test failed"
9500         fi
9501 }
9502 run_test 104b "$RUNAS lfs check servers test ===================="
9503
9504 #
9505 # Verify $1 is within range of $2.
9506 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
9507 # $1 is <= 2% of $2. Else Fail.
9508 #
9509 value_in_range() {
9510         # Strip all units (M, G, T)
9511         actual=$(echo $1 | tr -d A-Z)
9512         expect=$(echo $2 | tr -d A-Z)
9513
9514         expect_lo=$(($expect * 98 / 100)) # 2% below
9515         expect_hi=$(($expect * 102 / 100)) # 2% above
9516
9517         # permit 2% drift above and below
9518         (( $actual >= $expect_lo && $actual <= $expect_hi ))
9519 }
9520
9521 test_104c() {
9522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9523         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
9524
9525         local ost_param="osd-zfs.$FSNAME-OST0000."
9526         local mdt_param="osd-zfs.$FSNAME-MDT0000."
9527         local ofacets=$(get_facets OST)
9528         local mfacets=$(get_facets MDS)
9529         local saved_ost_blocks=
9530         local saved_mdt_blocks=
9531
9532         echo "Before recordsize change"
9533         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
9534         df=($(df -h | grep "/mnt/lustre"$))
9535
9536         # For checking.
9537         echo "lfs output : ${lfs_df[*]}"
9538         echo "df  output : ${df[*]}"
9539
9540         for facet in ${ofacets//,/ }; do
9541                 if [ -z $saved_ost_blocks ]; then
9542                         saved_ost_blocks=$(do_facet $facet \
9543                                 lctl get_param -n $ost_param.blocksize)
9544                         echo "OST Blocksize: $saved_ost_blocks"
9545                 fi
9546                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
9547                 do_facet $facet zfs set recordsize=32768 $ost
9548         done
9549
9550         # BS too small. Sufficient for functional testing.
9551         for facet in ${mfacets//,/ }; do
9552                 if [ -z $saved_mdt_blocks ]; then
9553                         saved_mdt_blocks=$(do_facet $facet \
9554                                 lctl get_param -n $mdt_param.blocksize)
9555                         echo "MDT Blocksize: $saved_mdt_blocks"
9556                 fi
9557                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
9558                 do_facet $facet zfs set recordsize=32768 $mdt
9559         done
9560
9561         # Give new values chance to reflect change
9562         sleep 2
9563
9564         echo "After recordsize change"
9565         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
9566         df_after=($(df -h | grep "/mnt/lustre"$))
9567
9568         # For checking.
9569         echo "lfs output : ${lfs_df_after[*]}"
9570         echo "df  output : ${df_after[*]}"
9571
9572         # Verify lfs df
9573         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
9574                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
9575         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
9576                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
9577         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
9578                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
9579
9580         # Verify df
9581         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
9582                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
9583         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
9584                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
9585         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
9586                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
9587
9588         # Restore MDT recordize back to original
9589         for facet in ${mfacets//,/ }; do
9590                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
9591                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
9592         done
9593
9594         # Restore OST recordize back to original
9595         for facet in ${ofacets//,/ }; do
9596                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
9597                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
9598         done
9599
9600         return 0
9601 }
9602 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
9603
9604 test_105a() {
9605         # doesn't work on 2.4 kernels
9606         touch $DIR/$tfile
9607         if $(flock_is_enabled); then
9608                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9609         else
9610                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9611         fi
9612         rm -f $DIR/$tfile
9613 }
9614 run_test 105a "flock when mounted without -o flock test ========"
9615
9616 test_105b() {
9617         touch $DIR/$tfile
9618         if $(flock_is_enabled); then
9619                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9620         else
9621                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9622         fi
9623         rm -f $DIR/$tfile
9624 }
9625 run_test 105b "fcntl when mounted without -o flock test ========"
9626
9627 test_105c() {
9628         touch $DIR/$tfile
9629         if $(flock_is_enabled); then
9630                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9631         else
9632                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9633         fi
9634         rm -f $DIR/$tfile
9635 }
9636 run_test 105c "lockf when mounted without -o flock test"
9637
9638 test_105d() { # bug 15924
9639         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9640
9641         test_mkdir $DIR/$tdir
9642         flock_is_enabled || skip_env "mount w/o flock enabled"
9643         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9644         $LCTL set_param fail_loc=0x80000315
9645         flocks_test 2 $DIR/$tdir
9646 }
9647 run_test 105d "flock race (should not freeze) ========"
9648
9649 test_105e() { # bug 22660 && 22040
9650         flock_is_enabled || skip_env "mount w/o flock enabled"
9651
9652         touch $DIR/$tfile
9653         flocks_test 3 $DIR/$tfile
9654 }
9655 run_test 105e "Two conflicting flocks from same process"
9656
9657 test_106() { #bug 10921
9658         test_mkdir $DIR/$tdir
9659         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9660         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9661 }
9662 run_test 106 "attempt exec of dir followed by chown of that dir"
9663
9664 test_107() {
9665         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9666
9667         CDIR=`pwd`
9668         local file=core
9669
9670         cd $DIR
9671         rm -f $file
9672
9673         local save_pattern=$(sysctl -n kernel.core_pattern)
9674         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9675         sysctl -w kernel.core_pattern=$file
9676         sysctl -w kernel.core_uses_pid=0
9677
9678         ulimit -c unlimited
9679         sleep 60 &
9680         SLEEPPID=$!
9681
9682         sleep 1
9683
9684         kill -s 11 $SLEEPPID
9685         wait $SLEEPPID
9686         if [ -e $file ]; then
9687                 size=`stat -c%s $file`
9688                 [ $size -eq 0 ] && error "Fail to create core file $file"
9689         else
9690                 error "Fail to create core file $file"
9691         fi
9692         rm -f $file
9693         sysctl -w kernel.core_pattern=$save_pattern
9694         sysctl -w kernel.core_uses_pid=$save_uses_pid
9695         cd $CDIR
9696 }
9697 run_test 107 "Coredump on SIG"
9698
9699 test_110() {
9700         test_mkdir $DIR/$tdir
9701         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9702         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9703                 error "mkdir with 256 char should fail, but did not"
9704         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9705                 error "create with 255 char failed"
9706         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9707                 error "create with 256 char should fail, but did not"
9708
9709         ls -l $DIR/$tdir
9710         rm -rf $DIR/$tdir
9711 }
9712 run_test 110 "filename length checking"
9713
9714 #
9715 # Purpose: To verify dynamic thread (OSS) creation.
9716 #
9717 test_115() {
9718         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9719         remote_ost_nodsh && skip "remote OST with nodsh"
9720
9721         # Lustre does not stop service threads once they are started.
9722         # Reset number of running threads to default.
9723         stopall
9724         setupall
9725
9726         local OSTIO_pre
9727         local save_params="$TMP/sanity-$TESTNAME.parameters"
9728
9729         # Get ll_ost_io count before I/O
9730         OSTIO_pre=$(do_facet ost1 \
9731                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
9732         # Exit if lustre is not running (ll_ost_io not running).
9733         [ -z "$OSTIO_pre" ] && error "no OSS threads"
9734
9735         echo "Starting with $OSTIO_pre threads"
9736         local thread_max=$((OSTIO_pre * 2))
9737         local rpc_in_flight=$((thread_max * 2))
9738         # Number of I/O Process proposed to be started.
9739         local nfiles
9740         local facets=$(get_facets OST)
9741
9742         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
9743         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
9744
9745         # Set in_flight to $rpc_in_flight
9746         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
9747                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
9748         nfiles=${rpc_in_flight}
9749         # Set ost thread_max to $thread_max
9750         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
9751
9752         # 5 Minutes should be sufficient for max number of OSS
9753         # threads(thread_max) to be created.
9754         local timeout=300
9755
9756         # Start I/O.
9757         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
9758         test_mkdir $DIR/$tdir
9759         for i in $(seq $nfiles); do
9760                 local file=$DIR/$tdir/${tfile}-$i
9761                 $LFS setstripe -c -1 -i 0 $file
9762                 ($WTL $file $timeout)&
9763         done
9764
9765         # I/O Started - Wait for thread_started to reach thread_max or report
9766         # error if thread_started is more than thread_max.
9767         echo "Waiting for thread_started to reach thread_max"
9768         local thread_started=0
9769         local end_time=$((SECONDS + timeout))
9770
9771         while [ $SECONDS -le $end_time ] ; do
9772                 echo -n "."
9773                 # Get ost i/o thread_started count.
9774                 thread_started=$(do_facet ost1 \
9775                         "$LCTL get_param \
9776                         ost.OSS.ost_io.threads_started | cut -d= -f2")
9777                 # Break out if thread_started is equal/greater than thread_max
9778                 if [[ $thread_started -ge $thread_max ]]; then
9779                         echo ll_ost_io thread_started $thread_started, \
9780                                 equal/greater than thread_max $thread_max
9781                         break
9782                 fi
9783                 sleep 1
9784         done
9785
9786         # Cleanup - We have the numbers, Kill i/o jobs if running.
9787         jobcount=($(jobs -p))
9788         for i in $(seq 0 $((${#jobcount[@]}-1)))
9789         do
9790                 kill -9 ${jobcount[$i]}
9791                 if [ $? -ne 0 ] ; then
9792                         echo Warning: \
9793                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
9794                 fi
9795         done
9796
9797         # Cleanup files left by WTL binary.
9798         for i in $(seq $nfiles); do
9799                 local file=$DIR/$tdir/${tfile}-$i
9800                 rm -rf $file
9801                 if [ $? -ne 0 ] ; then
9802                         echo "Warning: Failed to delete file $file"
9803                 fi
9804         done
9805
9806         restore_lustre_params <$save_params
9807         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
9808
9809         # Error out if no new thread has started or Thread started is greater
9810         # than thread max.
9811         if [[ $thread_started -le $OSTIO_pre ||
9812                         $thread_started -gt $thread_max ]]; then
9813                 error "ll_ost_io: thread_started $thread_started" \
9814                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
9815                       "No new thread started or thread started greater " \
9816                       "than thread_max."
9817         fi
9818 }
9819 run_test 115 "verify dynamic thread creation===================="
9820
9821 free_min_max () {
9822         wait_delete_completed
9823         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
9824         echo "OST kbytes available: ${AVAIL[@]}"
9825         MAXV=${AVAIL[0]}
9826         MAXI=0
9827         MINV=${AVAIL[0]}
9828         MINI=0
9829         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
9830                 #echo OST $i: ${AVAIL[i]}kb
9831                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
9832                         MAXV=${AVAIL[i]}
9833                         MAXI=$i
9834                 fi
9835                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
9836                         MINV=${AVAIL[i]}
9837                         MINI=$i
9838                 fi
9839         done
9840         echo "Min free space: OST $MINI: $MINV"
9841         echo "Max free space: OST $MAXI: $MAXV"
9842 }
9843
9844 test_116a() { # was previously test_116()
9845         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9846         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9847         remote_mds_nodsh && skip "remote MDS with nodsh"
9848
9849         echo -n "Free space priority "
9850         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
9851                 head -n1
9852         declare -a AVAIL
9853         free_min_max
9854
9855         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
9856         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
9857         trap simple_cleanup_common EXIT
9858
9859         # Check if we need to generate uneven OSTs
9860         test_mkdir -p $DIR/$tdir/OST${MINI}
9861         local FILL=$((MINV / 4))
9862         local DIFF=$((MAXV - MINV))
9863         local DIFF2=$((DIFF * 100 / MINV))
9864
9865         local threshold=$(do_facet $SINGLEMDS \
9866                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
9867         threshold=${threshold%%%}
9868         echo -n "Check for uneven OSTs: "
9869         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
9870
9871         if [[ $DIFF2 -gt $threshold ]]; then
9872                 echo "ok"
9873                 echo "Don't need to fill OST$MINI"
9874         else
9875                 # generate uneven OSTs. Write 2% over the QOS threshold value
9876                 echo "no"
9877                 DIFF=$((threshold - DIFF2 + 2))
9878                 DIFF2=$((MINV * DIFF / 100))
9879                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
9880                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
9881                         error "setstripe failed"
9882                 DIFF=$((DIFF2 / 2048))
9883                 i=0
9884                 while [ $i -lt $DIFF ]; do
9885                         i=$((i + 1))
9886                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
9887                                 bs=2M count=1 2>/dev/null
9888                         echo -n .
9889                 done
9890                 echo .
9891                 sync
9892                 sleep_maxage
9893                 free_min_max
9894         fi
9895
9896         DIFF=$((MAXV - MINV))
9897         DIFF2=$((DIFF * 100 / MINV))
9898         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
9899         if [ $DIFF2 -gt $threshold ]; then
9900                 echo "ok"
9901         else
9902                 echo "failed - QOS mode won't be used"
9903                 simple_cleanup_common
9904                 skip "QOS imbalance criteria not met"
9905         fi
9906
9907         MINI1=$MINI
9908         MINV1=$MINV
9909         MAXI1=$MAXI
9910         MAXV1=$MAXV
9911
9912         # now fill using QOS
9913         $SETSTRIPE -c 1 $DIR/$tdir
9914         FILL=$((FILL / 200))
9915         if [ $FILL -gt 600 ]; then
9916                 FILL=600
9917         fi
9918         echo "writing $FILL files to QOS-assigned OSTs"
9919         i=0
9920         while [ $i -lt $FILL ]; do
9921                 i=$((i + 1))
9922                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
9923                         count=1 2>/dev/null
9924                 echo -n .
9925         done
9926         echo "wrote $i 200k files"
9927         sync
9928         sleep_maxage
9929
9930         echo "Note: free space may not be updated, so measurements might be off"
9931         free_min_max
9932         DIFF2=$((MAXV - MINV))
9933         echo "free space delta: orig $DIFF final $DIFF2"
9934         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
9935         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
9936         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
9937         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
9938         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
9939         if [[ $DIFF -gt 0 ]]; then
9940                 FILL=$((DIFF2 * 100 / DIFF - 100))
9941                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
9942         fi
9943
9944         # Figure out which files were written where
9945         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9946                awk '/'$MINI1': / {print $2; exit}')
9947         echo $UUID
9948         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9949         echo "$MINC files created on smaller OST $MINI1"
9950         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9951                awk '/'$MAXI1': / {print $2; exit}')
9952         echo $UUID
9953         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9954         echo "$MAXC files created on larger OST $MAXI1"
9955         if [[ $MINC -gt 0 ]]; then
9956                 FILL=$((MAXC * 100 / MINC - 100))
9957                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
9958         fi
9959         [[ $MAXC -gt $MINC ]] ||
9960                 error_ignore LU-9 "stripe QOS didn't balance free space"
9961         simple_cleanup_common
9962 }
9963 run_test 116a "stripe QOS: free space balance ==================="
9964
9965 test_116b() { # LU-2093
9966         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9967         remote_mds_nodsh && skip "remote MDS with nodsh"
9968
9969 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
9970         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
9971                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
9972         [ -z "$old_rr" ] && skip "no QOS"
9973         do_facet $SINGLEMDS lctl set_param \
9974                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
9975         mkdir -p $DIR/$tdir
9976         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
9977         createmany -o $DIR/$tdir/f- 20 || error "can't create"
9978         do_facet $SINGLEMDS lctl set_param fail_loc=0
9979         rm -rf $DIR/$tdir
9980         do_facet $SINGLEMDS lctl set_param \
9981                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
9982 }
9983 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
9984
9985 test_117() # bug 10891
9986 {
9987         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9988
9989         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
9990         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
9991         lctl set_param fail_loc=0x21e
9992         > $DIR/$tfile || error "truncate failed"
9993         lctl set_param fail_loc=0
9994         echo "Truncate succeeded."
9995         rm -f $DIR/$tfile
9996 }
9997 run_test 117 "verify osd extend =========="
9998
9999 NO_SLOW_RESENDCOUNT=4
10000 export OLD_RESENDCOUNT=""
10001 set_resend_count () {
10002         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10003         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10004         lctl set_param -n $PROC_RESENDCOUNT $1
10005         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10006 }
10007
10008 # for reduce test_118* time (b=14842)
10009 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10010
10011 # Reset async IO behavior after error case
10012 reset_async() {
10013         FILE=$DIR/reset_async
10014
10015         # Ensure all OSCs are cleared
10016         $SETSTRIPE -c -1 $FILE
10017         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10018         sync
10019         rm $FILE
10020 }
10021
10022 test_118a() #bug 11710
10023 {
10024         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10025
10026         reset_async
10027
10028         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10029         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10030         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10031
10032         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10033                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10034                 return 1;
10035         fi
10036         rm -f $DIR/$tfile
10037 }
10038 run_test 118a "verify O_SYNC works =========="
10039
10040 test_118b()
10041 {
10042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10043         remote_ost_nodsh && skip "remote OST with nodsh"
10044
10045         reset_async
10046
10047         #define OBD_FAIL_SRV_ENOENT 0x217
10048         set_nodes_failloc "$(osts_nodes)" 0x217
10049         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10050         RC=$?
10051         set_nodes_failloc "$(osts_nodes)" 0
10052         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10053         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10054                     grep -c writeback)
10055
10056         if [[ $RC -eq 0 ]]; then
10057                 error "Must return error due to dropped pages, rc=$RC"
10058                 return 1;
10059         fi
10060
10061         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10062                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10063                 return 1;
10064         fi
10065
10066         echo "Dirty pages not leaked on ENOENT"
10067
10068         # Due to the above error the OSC will issue all RPCs syncronously
10069         # until a subsequent RPC completes successfully without error.
10070         $MULTIOP $DIR/$tfile Ow4096yc
10071         rm -f $DIR/$tfile
10072
10073         return 0
10074 }
10075 run_test 118b "Reclaim dirty pages on fatal error =========="
10076
10077 test_118c()
10078 {
10079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10080
10081         # for 118c, restore the original resend count, LU-1940
10082         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10083                                 set_resend_count $OLD_RESENDCOUNT
10084         remote_ost_nodsh && skip "remote OST with nodsh"
10085
10086         reset_async
10087
10088         #define OBD_FAIL_OST_EROFS               0x216
10089         set_nodes_failloc "$(osts_nodes)" 0x216
10090
10091         # multiop should block due to fsync until pages are written
10092         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10093         MULTIPID=$!
10094         sleep 1
10095
10096         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10097                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10098         fi
10099
10100         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10101                     grep -c writeback)
10102         if [[ $WRITEBACK -eq 0 ]]; then
10103                 error "No page in writeback, writeback=$WRITEBACK"
10104         fi
10105
10106         set_nodes_failloc "$(osts_nodes)" 0
10107         wait $MULTIPID
10108         RC=$?
10109         if [[ $RC -ne 0 ]]; then
10110                 error "Multiop fsync failed, rc=$RC"
10111         fi
10112
10113         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10114         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10115                     grep -c writeback)
10116         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10117                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10118         fi
10119
10120         rm -f $DIR/$tfile
10121         echo "Dirty pages flushed via fsync on EROFS"
10122         return 0
10123 }
10124 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10125
10126 # continue to use small resend count to reduce test_118* time (b=14842)
10127 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10128
10129 test_118d()
10130 {
10131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10132         remote_ost_nodsh && skip "remote OST with nodsh"
10133
10134         reset_async
10135
10136         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10137         set_nodes_failloc "$(osts_nodes)" 0x214
10138         # multiop should block due to fsync until pages are written
10139         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10140         MULTIPID=$!
10141         sleep 1
10142
10143         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10144                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10145         fi
10146
10147         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10148                     grep -c writeback)
10149         if [[ $WRITEBACK -eq 0 ]]; then
10150                 error "No page in writeback, writeback=$WRITEBACK"
10151         fi
10152
10153         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10154         set_nodes_failloc "$(osts_nodes)" 0
10155
10156         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10157         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10158                     grep -c writeback)
10159         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10160                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10161         fi
10162
10163         rm -f $DIR/$tfile
10164         echo "Dirty pages gaurenteed flushed via fsync"
10165         return 0
10166 }
10167 run_test 118d "Fsync validation inject a delay of the bulk =========="
10168
10169 test_118f() {
10170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10171
10172         reset_async
10173
10174         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10175         lctl set_param fail_loc=0x8000040a
10176
10177         # Should simulate EINVAL error which is fatal
10178         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10179         RC=$?
10180         if [[ $RC -eq 0 ]]; then
10181                 error "Must return error due to dropped pages, rc=$RC"
10182         fi
10183
10184         lctl set_param fail_loc=0x0
10185
10186         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10187         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10188         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10189                     grep -c writeback)
10190         if [[ $LOCKED -ne 0 ]]; then
10191                 error "Locked pages remain in cache, locked=$LOCKED"
10192         fi
10193
10194         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10195                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10196         fi
10197
10198         rm -f $DIR/$tfile
10199         echo "No pages locked after fsync"
10200
10201         reset_async
10202         return 0
10203 }
10204 run_test 118f "Simulate unrecoverable OSC side error =========="
10205
10206 test_118g() {
10207         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10208
10209         reset_async
10210
10211         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10212         lctl set_param fail_loc=0x406
10213
10214         # simulate local -ENOMEM
10215         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10216         RC=$?
10217
10218         lctl set_param fail_loc=0
10219         if [[ $RC -eq 0 ]]; then
10220                 error "Must return error due to dropped pages, rc=$RC"
10221         fi
10222
10223         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10224         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10225         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10226                         grep -c writeback)
10227         if [[ $LOCKED -ne 0 ]]; then
10228                 error "Locked pages remain in cache, locked=$LOCKED"
10229         fi
10230
10231         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10232                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10233         fi
10234
10235         rm -f $DIR/$tfile
10236         echo "No pages locked after fsync"
10237
10238         reset_async
10239         return 0
10240 }
10241 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10242
10243 test_118h() {
10244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10245         remote_ost_nodsh && skip "remote OST with nodsh"
10246
10247         reset_async
10248
10249         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10250         set_nodes_failloc "$(osts_nodes)" 0x20e
10251         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10252         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10253         RC=$?
10254
10255         set_nodes_failloc "$(osts_nodes)" 0
10256         if [[ $RC -eq 0 ]]; then
10257                 error "Must return error due to dropped pages, rc=$RC"
10258         fi
10259
10260         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10261         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10262         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10263                     grep -c writeback)
10264         if [[ $LOCKED -ne 0 ]]; then
10265                 error "Locked pages remain in cache, locked=$LOCKED"
10266         fi
10267
10268         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10269                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10270         fi
10271
10272         rm -f $DIR/$tfile
10273         echo "No pages locked after fsync"
10274
10275         return 0
10276 }
10277 run_test 118h "Verify timeout in handling recoverables errors  =========="
10278
10279 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10280
10281 test_118i() {
10282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10283         remote_ost_nodsh && skip "remote OST with nodsh"
10284
10285         reset_async
10286
10287         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10288         set_nodes_failloc "$(osts_nodes)" 0x20e
10289
10290         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10291         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10292         PID=$!
10293         sleep 5
10294         set_nodes_failloc "$(osts_nodes)" 0
10295
10296         wait $PID
10297         RC=$?
10298         if [[ $RC -ne 0 ]]; then
10299                 error "got error, but should be not, rc=$RC"
10300         fi
10301
10302         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10303         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10304         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10305         if [[ $LOCKED -ne 0 ]]; then
10306                 error "Locked pages remain in cache, locked=$LOCKED"
10307         fi
10308
10309         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10310                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10311         fi
10312
10313         rm -f $DIR/$tfile
10314         echo "No pages locked after fsync"
10315
10316         return 0
10317 }
10318 run_test 118i "Fix error before timeout in recoverable error  =========="
10319
10320 [ "$SLOW" = "no" ] && set_resend_count 4
10321
10322 test_118j() {
10323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10324         remote_ost_nodsh && skip "remote OST with nodsh"
10325
10326         reset_async
10327
10328         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10329         set_nodes_failloc "$(osts_nodes)" 0x220
10330
10331         # return -EIO from OST
10332         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10333         RC=$?
10334         set_nodes_failloc "$(osts_nodes)" 0x0
10335         if [[ $RC -eq 0 ]]; then
10336                 error "Must return error due to dropped pages, rc=$RC"
10337         fi
10338
10339         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10340         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10341         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10342         if [[ $LOCKED -ne 0 ]]; then
10343                 error "Locked pages remain in cache, locked=$LOCKED"
10344         fi
10345
10346         # in recoverable error on OST we want resend and stay until it finished
10347         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10348                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10349         fi
10350
10351         rm -f $DIR/$tfile
10352         echo "No pages locked after fsync"
10353
10354         return 0
10355 }
10356 run_test 118j "Simulate unrecoverable OST side error =========="
10357
10358 test_118k()
10359 {
10360         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10361         remote_ost_nodsh && skip "remote OSTs with nodsh"
10362
10363         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10364         set_nodes_failloc "$(osts_nodes)" 0x20e
10365         test_mkdir $DIR/$tdir
10366
10367         for ((i=0;i<10;i++)); do
10368                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10369                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10370                 SLEEPPID=$!
10371                 sleep 0.500s
10372                 kill $SLEEPPID
10373                 wait $SLEEPPID
10374         done
10375
10376         set_nodes_failloc "$(osts_nodes)" 0
10377         rm -rf $DIR/$tdir
10378 }
10379 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10380
10381 test_118l() # LU-646
10382 {
10383         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10384
10385         test_mkdir $DIR/$tdir
10386         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10387         rm -rf $DIR/$tdir
10388 }
10389 run_test 118l "fsync dir"
10390
10391 test_118m() # LU-3066
10392 {
10393         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10394
10395         test_mkdir $DIR/$tdir
10396         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10397         rm -rf $DIR/$tdir
10398 }
10399 run_test 118m "fdatasync dir ========="
10400
10401 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10402
10403 test_118n()
10404 {
10405         local begin
10406         local end
10407
10408         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10409         remote_ost_nodsh && skip "remote OSTs with nodsh"
10410
10411         # Sleep to avoid a cached response.
10412         #define OBD_STATFS_CACHE_SECONDS 1
10413         sleep 2
10414
10415         # Inject a 10 second delay in the OST_STATFS handler.
10416         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10417         set_nodes_failloc "$(osts_nodes)" 0x242
10418
10419         begin=$SECONDS
10420         stat --file-system $MOUNT > /dev/null
10421         end=$SECONDS
10422
10423         set_nodes_failloc "$(osts_nodes)" 0
10424
10425         if ((end - begin > 20)); then
10426             error "statfs took $((end - begin)) seconds, expected 10"
10427         fi
10428 }
10429 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10430
10431 test_119a() # bug 11737
10432 {
10433         BSIZE=$((512 * 1024))
10434         directio write $DIR/$tfile 0 1 $BSIZE
10435         # We ask to read two blocks, which is more than a file size.
10436         # directio will indicate an error when requested and actual
10437         # sizes aren't equeal (a normal situation in this case) and
10438         # print actual read amount.
10439         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10440         if [ "$NOB" != "$BSIZE" ]; then
10441                 error "read $NOB bytes instead of $BSIZE"
10442         fi
10443         rm -f $DIR/$tfile
10444 }
10445 run_test 119a "Short directIO read must return actual read amount"
10446
10447 test_119b() # bug 11737
10448 {
10449         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10450
10451         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
10452         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10453         sync
10454         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10455                 error "direct read failed"
10456         rm -f $DIR/$tfile
10457 }
10458 run_test 119b "Sparse directIO read must return actual read amount"
10459
10460 test_119c() # bug 13099
10461 {
10462         BSIZE=1048576
10463         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10464         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10465         rm -f $DIR/$tfile
10466 }
10467 run_test 119c "Testing for direct read hitting hole"
10468
10469 test_119d() # bug 15950
10470 {
10471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10472
10473         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10474         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10475         BSIZE=1048576
10476         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10477         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10478         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10479         lctl set_param fail_loc=0x40d
10480         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10481         pid_dio=$!
10482         sleep 1
10483         cat $DIR/$tfile > /dev/null &
10484         lctl set_param fail_loc=0
10485         pid_reads=$!
10486         wait $pid_dio
10487         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10488         sleep 2
10489         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10490         error "the read rpcs have not completed in 2s"
10491         rm -f $DIR/$tfile
10492         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10493 }
10494 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10495
10496 test_120a() {
10497         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10498         remote_mds_nodsh && skip "remote MDS with nodsh"
10499         test_mkdir $DIR/$tdir
10500         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10501                 skip_env "no early lock cancel on server"
10502
10503         lru_resize_disable mdc
10504         lru_resize_disable osc
10505         cancel_lru_locks mdc
10506         # asynchronous object destroy at MDT could cause bl ast to client
10507         cancel_lru_locks osc
10508
10509         stat $DIR/$tdir > /dev/null
10510         can1=$(do_facet $SINGLEMDS \
10511                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10512                awk '/ldlm_cancel/ {print $2}')
10513         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10514                awk '/ldlm_bl_callback/ {print $2}')
10515         test_mkdir -c1 $DIR/$tdir/d1
10516         can2=$(do_facet $SINGLEMDS \
10517                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10518                awk '/ldlm_cancel/ {print $2}')
10519         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10520                awk '/ldlm_bl_callback/ {print $2}')
10521         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10522         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10523         lru_resize_enable mdc
10524         lru_resize_enable osc
10525 }
10526 run_test 120a "Early Lock Cancel: mkdir test"
10527
10528 test_120b() {
10529         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10530         remote_mds_nodsh && skip "remote MDS with nodsh"
10531         test_mkdir $DIR/$tdir
10532         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10533                 skip_env "no early lock cancel on server"
10534
10535         lru_resize_disable mdc
10536         lru_resize_disable osc
10537         cancel_lru_locks mdc
10538         stat $DIR/$tdir > /dev/null
10539         can1=$(do_facet $SINGLEMDS \
10540                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10541                awk '/ldlm_cancel/ {print $2}')
10542         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10543                awk '/ldlm_bl_callback/ {print $2}')
10544         touch $DIR/$tdir/f1
10545         can2=$(do_facet $SINGLEMDS \
10546                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10547                awk '/ldlm_cancel/ {print $2}')
10548         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10549                awk '/ldlm_bl_callback/ {print $2}')
10550         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10551         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10552         lru_resize_enable mdc
10553         lru_resize_enable osc
10554 }
10555 run_test 120b "Early Lock Cancel: create test"
10556
10557 test_120c() {
10558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10559         remote_mds_nodsh && skip "remote MDS with nodsh"
10560         test_mkdir -c1 $DIR/$tdir
10561         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10562                 skip "no early lock cancel on server"
10563
10564         lru_resize_disable mdc
10565         lru_resize_disable osc
10566         test_mkdir -c1 $DIR/$tdir/d1
10567         test_mkdir -c1 $DIR/$tdir/d2
10568         touch $DIR/$tdir/d1/f1
10569         cancel_lru_locks mdc
10570         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10571         can1=$(do_facet $SINGLEMDS \
10572                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10573                awk '/ldlm_cancel/ {print $2}')
10574         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10575                awk '/ldlm_bl_callback/ {print $2}')
10576         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10577         can2=$(do_facet $SINGLEMDS \
10578                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10579                awk '/ldlm_cancel/ {print $2}')
10580         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10581                awk '/ldlm_bl_callback/ {print $2}')
10582         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10583         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10584         lru_resize_enable mdc
10585         lru_resize_enable osc
10586 }
10587 run_test 120c "Early Lock Cancel: link test"
10588
10589 test_120d() {
10590         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10591         remote_mds_nodsh && skip "remote MDS with nodsh"
10592         test_mkdir -c1 $DIR/$tdir
10593         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10594                 skip_env "no early lock cancel on server"
10595
10596         lru_resize_disable mdc
10597         lru_resize_disable osc
10598         touch $DIR/$tdir
10599         cancel_lru_locks mdc
10600         stat $DIR/$tdir > /dev/null
10601         can1=$(do_facet $SINGLEMDS \
10602                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10603                awk '/ldlm_cancel/ {print $2}')
10604         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10605                awk '/ldlm_bl_callback/ {print $2}')
10606         chmod a+x $DIR/$tdir
10607         can2=$(do_facet $SINGLEMDS \
10608                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10609                awk '/ldlm_cancel/ {print $2}')
10610         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10611                awk '/ldlm_bl_callback/ {print $2}')
10612         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10613         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10614         lru_resize_enable mdc
10615         lru_resize_enable osc
10616 }
10617 run_test 120d "Early Lock Cancel: setattr test"
10618
10619 test_120e() {
10620         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10621         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10622                 skip_env "no early lock cancel on server"
10623         remote_mds_nodsh && skip "remote MDS with nodsh"
10624
10625         local dlmtrace_set=false
10626
10627         test_mkdir -c1 $DIR/$tdir
10628         lru_resize_disable mdc
10629         lru_resize_disable osc
10630         ! $LCTL get_param debug | grep -q dlmtrace &&
10631                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10632         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10633         cancel_lru_locks mdc
10634         cancel_lru_locks osc
10635         dd if=$DIR/$tdir/f1 of=/dev/null
10636         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10637         # XXX client can not do early lock cancel of OST lock
10638         # during unlink (LU-4206), so cancel osc lock now.
10639         sleep 2
10640         cancel_lru_locks osc
10641         can1=$(do_facet $SINGLEMDS \
10642                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10643                awk '/ldlm_cancel/ {print $2}')
10644         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10645                awk '/ldlm_bl_callback/ {print $2}')
10646         unlink $DIR/$tdir/f1
10647         sleep 5
10648         can2=$(do_facet $SINGLEMDS \
10649                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10650                awk '/ldlm_cancel/ {print $2}')
10651         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10652                awk '/ldlm_bl_callback/ {print $2}')
10653         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10654                 $LCTL dk $TMP/cancel.debug.txt
10655         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10656                 $LCTL dk $TMP/blocking.debug.txt
10657         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10658         lru_resize_enable mdc
10659         lru_resize_enable osc
10660 }
10661 run_test 120e "Early Lock Cancel: unlink test"
10662
10663 test_120f() {
10664         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10665         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10666                 skip_env "no early lock cancel on server"
10667         remote_mds_nodsh && skip "remote MDS with nodsh"
10668
10669         test_mkdir -c1 $DIR/$tdir
10670         lru_resize_disable mdc
10671         lru_resize_disable osc
10672         test_mkdir -c1 $DIR/$tdir/d1
10673         test_mkdir -c1 $DIR/$tdir/d2
10674         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10675         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10676         cancel_lru_locks mdc
10677         cancel_lru_locks osc
10678         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10679         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10680         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10681         # XXX client can not do early lock cancel of OST lock
10682         # during rename (LU-4206), so cancel osc lock now.
10683         sleep 2
10684         cancel_lru_locks osc
10685         can1=$(do_facet $SINGLEMDS \
10686                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10687                awk '/ldlm_cancel/ {print $2}')
10688         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10689                awk '/ldlm_bl_callback/ {print $2}')
10690         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10691         sleep 5
10692         can2=$(do_facet $SINGLEMDS \
10693                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10694                awk '/ldlm_cancel/ {print $2}')
10695         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10696                awk '/ldlm_bl_callback/ {print $2}')
10697         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10698         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10699         lru_resize_enable mdc
10700         lru_resize_enable osc
10701 }
10702 run_test 120f "Early Lock Cancel: rename test"
10703
10704 test_120g() {
10705         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10706         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10707                 skip_env "no early lock cancel on server"
10708         remote_mds_nodsh && skip "remote MDS with nodsh"
10709
10710         lru_resize_disable mdc
10711         lru_resize_disable osc
10712         count=10000
10713         echo create $count files
10714         test_mkdir $DIR/$tdir
10715         cancel_lru_locks mdc
10716         cancel_lru_locks osc
10717         t0=$(date +%s)
10718
10719         can0=$(do_facet $SINGLEMDS \
10720                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10721                awk '/ldlm_cancel/ {print $2}')
10722         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10723                awk '/ldlm_bl_callback/ {print $2}')
10724         createmany -o $DIR/$tdir/f $count
10725         sync
10726         can1=$(do_facet $SINGLEMDS \
10727                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10728                awk '/ldlm_cancel/ {print $2}')
10729         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10730                awk '/ldlm_bl_callback/ {print $2}')
10731         t1=$(date +%s)
10732         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
10733         echo rm $count files
10734         rm -r $DIR/$tdir
10735         sync
10736         can2=$(do_facet $SINGLEMDS \
10737                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10738                awk '/ldlm_cancel/ {print $2}')
10739         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10740                awk '/ldlm_bl_callback/ {print $2}')
10741         t2=$(date +%s)
10742         echo total: $count removes in $((t2-t1))
10743         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
10744         sleep 2
10745         # wait for commitment of removal
10746         lru_resize_enable mdc
10747         lru_resize_enable osc
10748 }
10749 run_test 120g "Early Lock Cancel: performance test"
10750
10751 test_121() { #bug #10589
10752         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10753
10754         rm -rf $DIR/$tfile
10755         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
10756 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
10757         lctl set_param fail_loc=0x310
10758         cancel_lru_locks osc > /dev/null
10759         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
10760         lctl set_param fail_loc=0
10761         [[ $reads -eq $writes ]] ||
10762                 error "read $reads blocks, must be $writes blocks"
10763 }
10764 run_test 121 "read cancel race ========="
10765
10766 test_123a() { # was test 123, statahead(bug 11401)
10767         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10768
10769         SLOWOK=0
10770         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
10771                 log "testing UP system. Performance may be lower than expected."
10772                 SLOWOK=1
10773         fi
10774
10775         rm -rf $DIR/$tdir
10776         test_mkdir $DIR/$tdir
10777         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
10778         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
10779         MULT=10
10780         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
10781                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
10782
10783                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10784                 lctl set_param -n llite.*.statahead_max 0
10785                 lctl get_param llite.*.statahead_max
10786                 cancel_lru_locks mdc
10787                 cancel_lru_locks osc
10788                 stime=`date +%s`
10789                 time ls -l $DIR/$tdir | wc -l
10790                 etime=`date +%s`
10791                 delta=$((etime - stime))
10792                 log "ls $i files without statahead: $delta sec"
10793                 lctl set_param llite.*.statahead_max=$max
10794
10795                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10796                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
10797                 cancel_lru_locks mdc
10798                 cancel_lru_locks osc
10799                 stime=`date +%s`
10800                 time ls -l $DIR/$tdir | wc -l
10801                 etime=`date +%s`
10802                 delta_sa=$((etime - stime))
10803                 log "ls $i files with statahead: $delta_sa sec"
10804                 lctl get_param -n llite.*.statahead_stats
10805                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10806
10807                 [[ $swrong -lt $ewrong ]] &&
10808                         log "statahead was stopped, maybe too many locks held!"
10809                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
10810
10811                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10812                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10813                     lctl set_param -n llite.*.statahead_max 0
10814                     lctl get_param llite.*.statahead_max
10815                     cancel_lru_locks mdc
10816                     cancel_lru_locks osc
10817                     stime=`date +%s`
10818                     time ls -l $DIR/$tdir | wc -l
10819                     etime=`date +%s`
10820                     delta=$((etime - stime))
10821                     log "ls $i files again without statahead: $delta sec"
10822                     lctl set_param llite.*.statahead_max=$max
10823                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10824                         if [  $SLOWOK -eq 0 ]; then
10825                                 error "ls $i files is slower with statahead!"
10826                         else
10827                                 log "ls $i files is slower with statahead!"
10828                         fi
10829                         break
10830                     fi
10831                 fi
10832
10833                 [ $delta -gt 20 ] && break
10834                 [ $delta -gt 8 ] && MULT=$((50 / delta))
10835                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
10836         done
10837         log "ls done"
10838
10839         stime=`date +%s`
10840         rm -r $DIR/$tdir
10841         sync
10842         etime=`date +%s`
10843         delta=$((etime - stime))
10844         log "rm -r $DIR/$tdir/: $delta seconds"
10845         log "rm done"
10846         lctl get_param -n llite.*.statahead_stats
10847 }
10848 run_test 123a "verify statahead work"
10849
10850 test_123b () { # statahead(bug 15027)
10851         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10852
10853         test_mkdir $DIR/$tdir
10854         createmany -o $DIR/$tdir/$tfile-%d 1000
10855
10856         cancel_lru_locks mdc
10857         cancel_lru_locks osc
10858
10859 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
10860         lctl set_param fail_loc=0x80000803
10861         ls -lR $DIR/$tdir > /dev/null
10862         log "ls done"
10863         lctl set_param fail_loc=0x0
10864         lctl get_param -n llite.*.statahead_stats
10865         rm -r $DIR/$tdir
10866         sync
10867
10868 }
10869 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
10870
10871 test_123c() {
10872         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
10873
10874         test_mkdir -i 0 -c 1 $DIR/$tdir.0
10875         test_mkdir -i 1 -c 1 $DIR/$tdir.1
10876         touch $DIR/$tdir.1/{1..3}
10877         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
10878
10879         remount_client $MOUNT
10880
10881         $MULTIOP $DIR/$tdir.0 Q
10882
10883         # let statahead to complete
10884         ls -l $DIR/$tdir.0 > /dev/null
10885
10886         testid=$(echo $TESTNAME | tr '_' ' ')
10887         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
10888                 error "statahead warning" || true
10889 }
10890 run_test 123c "Can not initialize inode warning on DNE statahead"
10891
10892 test_124a() {
10893         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10894         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10895                 skip_env "no lru resize on server"
10896
10897         local NR=2000
10898
10899         test_mkdir $DIR/$tdir
10900
10901         log "create $NR files at $DIR/$tdir"
10902         createmany -o $DIR/$tdir/f $NR ||
10903                 error "failed to create $NR files in $DIR/$tdir"
10904
10905         cancel_lru_locks mdc
10906         ls -l $DIR/$tdir > /dev/null
10907
10908         local NSDIR=""
10909         local LRU_SIZE=0
10910         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
10911                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
10912                 LRU_SIZE=$($LCTL get_param -n $PARAM)
10913                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
10914                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
10915                         log "NSDIR=$NSDIR"
10916                         log "NS=$(basename $NSDIR)"
10917                         break
10918                 fi
10919         done
10920
10921         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
10922                 skip "Not enough cached locks created!"
10923         fi
10924         log "LRU=$LRU_SIZE"
10925
10926         local SLEEP=30
10927
10928         # We know that lru resize allows one client to hold $LIMIT locks
10929         # for 10h. After that locks begin to be killed by client.
10930         local MAX_HRS=10
10931         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
10932         log "LIMIT=$LIMIT"
10933         if [ $LIMIT -lt $LRU_SIZE ]; then
10934                 skip "Limit is too small $LIMIT"
10935         fi
10936
10937         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
10938         # killing locks. Some time was spent for creating locks. This means
10939         # that up to the moment of sleep finish we must have killed some of
10940         # them (10-100 locks). This depends on how fast ther were created.
10941         # Many of them were touched in almost the same moment and thus will
10942         # be killed in groups.
10943         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
10944
10945         # Use $LRU_SIZE_B here to take into account real number of locks
10946         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
10947         local LRU_SIZE_B=$LRU_SIZE
10948         log "LVF=$LVF"
10949         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
10950         log "OLD_LVF=$OLD_LVF"
10951         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
10952
10953         # Let's make sure that we really have some margin. Client checks
10954         # cached locks every 10 sec.
10955         SLEEP=$((SLEEP+20))
10956         log "Sleep ${SLEEP} sec"
10957         local SEC=0
10958         while ((SEC<$SLEEP)); do
10959                 echo -n "..."
10960                 sleep 5
10961                 SEC=$((SEC+5))
10962                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
10963                 echo -n "$LRU_SIZE"
10964         done
10965         echo ""
10966         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
10967         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
10968
10969         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
10970                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
10971                 unlinkmany $DIR/$tdir/f $NR
10972                 return
10973         }
10974
10975         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
10976         log "unlink $NR files at $DIR/$tdir"
10977         unlinkmany $DIR/$tdir/f $NR
10978 }
10979 run_test 124a "lru resize ======================================="
10980
10981 get_max_pool_limit()
10982 {
10983         local limit=$($LCTL get_param \
10984                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
10985         local max=0
10986         for l in $limit; do
10987                 if [[ $l -gt $max ]]; then
10988                         max=$l
10989                 fi
10990         done
10991         echo $max
10992 }
10993
10994 test_124b() {
10995         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10996         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10997                 skip_env "no lru resize on server"
10998
10999         LIMIT=$(get_max_pool_limit)
11000
11001         NR=$(($(default_lru_size)*20))
11002         if [[ $NR -gt $LIMIT ]]; then
11003                 log "Limit lock number by $LIMIT locks"
11004                 NR=$LIMIT
11005         fi
11006
11007         IFree=$(mdsrate_inodes_available)
11008         if [ $IFree -lt $NR ]; then
11009                 log "Limit lock number by $IFree inodes"
11010                 NR=$IFree
11011         fi
11012
11013         lru_resize_disable mdc
11014         test_mkdir -p $DIR/$tdir/disable_lru_resize
11015
11016         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11017         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11018         cancel_lru_locks mdc
11019         stime=`date +%s`
11020         PID=""
11021         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11022         PID="$PID $!"
11023         sleep 2
11024         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11025         PID="$PID $!"
11026         sleep 2
11027         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11028         PID="$PID $!"
11029         wait $PID
11030         etime=`date +%s`
11031         nolruresize_delta=$((etime-stime))
11032         log "ls -la time: $nolruresize_delta seconds"
11033         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11034         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11035
11036         lru_resize_enable mdc
11037         test_mkdir -p $DIR/$tdir/enable_lru_resize
11038
11039         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11040         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11041         cancel_lru_locks mdc
11042         stime=`date +%s`
11043         PID=""
11044         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11045         PID="$PID $!"
11046         sleep 2
11047         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11048         PID="$PID $!"
11049         sleep 2
11050         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11051         PID="$PID $!"
11052         wait $PID
11053         etime=`date +%s`
11054         lruresize_delta=$((etime-stime))
11055         log "ls -la time: $lruresize_delta seconds"
11056         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11057
11058         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11059                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11060         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11061                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11062         else
11063                 log "lru resize performs the same with no lru resize"
11064         fi
11065         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11066 }
11067 run_test 124b "lru resize (performance test) ======================="
11068
11069 test_124c() {
11070         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11071         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11072                 skip_env "no lru resize on server"
11073
11074         # cache ununsed locks on client
11075         local nr=100
11076         cancel_lru_locks mdc
11077         test_mkdir $DIR/$tdir
11078         createmany -o $DIR/$tdir/f $nr ||
11079                 error "failed to create $nr files in $DIR/$tdir"
11080         ls -l $DIR/$tdir > /dev/null
11081
11082         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11083         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11084         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11085         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11086         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11087
11088         # set lru_max_age to 1 sec
11089         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11090         echo "sleep $((recalc_p * 2)) seconds..."
11091         sleep $((recalc_p * 2))
11092
11093         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11094         # restore lru_max_age
11095         $LCTL set_param -n $nsdir.lru_max_age $max_age
11096         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11097         unlinkmany $DIR/$tdir/f $nr
11098 }
11099 run_test 124c "LRUR cancel very aged locks"
11100
11101 test_124d() {
11102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11103         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11104                 skip_env "no lru resize on server"
11105
11106         # cache ununsed locks on client
11107         local nr=100
11108
11109         lru_resize_disable mdc
11110         stack_trap "lru_resize_enable mdc" EXIT
11111
11112         cancel_lru_locks mdc
11113
11114         # asynchronous object destroy at MDT could cause bl ast to client
11115         test_mkdir $DIR/$tdir
11116         createmany -o $DIR/$tdir/f $nr ||
11117                 error "failed to create $nr files in $DIR/$tdir"
11118         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11119
11120         ls -l $DIR/$tdir > /dev/null
11121
11122         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11123         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11124         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11125         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11126
11127         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11128
11129         # set lru_max_age to 1 sec
11130         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11131         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11132
11133         echo "sleep $((recalc_p * 2)) seconds..."
11134         sleep $((recalc_p * 2))
11135
11136         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11137
11138         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11139 }
11140 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11141
11142 test_125() { # 13358
11143         $LCTL get_param -n llite.*.client_type | grep -q local ||
11144                 skip "must run as local client"
11145         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11146                 skip_env "must have acl enabled"
11147         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11148
11149         test_mkdir $DIR/$tdir
11150         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11151         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11152         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11153 }
11154 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11155
11156 test_126() { # bug 12829/13455
11157         $GSS && skip_env "must run as gss disabled"
11158         $LCTL get_param -n llite.*.client_type | grep -q local ||
11159                 skip "must run as local client"
11160         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11161
11162         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11163         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11164         rm -f $DIR/$tfile
11165         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11166 }
11167 run_test 126 "check that the fsgid provided by the client is taken into account"
11168
11169 test_127a() { # bug 15521
11170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11171
11172         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11173         $LCTL set_param osc.*.stats=0
11174         FSIZE=$((2048 * 1024))
11175         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
11176         cancel_lru_locks osc
11177         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
11178
11179         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
11180         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
11181                 echo "got $COUNT $NAME"
11182                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
11183                 eval $NAME=$COUNT || error "Wrong proc format"
11184
11185                 case $NAME in
11186                         read_bytes|write_bytes)
11187                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
11188                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
11189                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
11190                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
11191                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
11192                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
11193                                 error "sumsquare is too small: $SUMSQ"
11194                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
11195                                 error "sumsquare is too big: $SUMSQ"
11196                         ;;
11197                         *) ;;
11198                 esac
11199         done < $DIR/${tfile}.tmp
11200
11201         #check that we actually got some stats
11202         [ "$read_bytes" ] || error "Missing read_bytes stats"
11203         [ "$write_bytes" ] || error "Missing write_bytes stats"
11204         [ "$read_bytes" != 0 ] || error "no read done"
11205         [ "$write_bytes" != 0 ] || error "no write done"
11206 }
11207 run_test 127a "verify the client stats are sane"
11208
11209 test_127b() { # bug LU-333
11210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11211         local name count samp unit min max sum sumsq
11212
11213         $LCTL set_param llite.*.stats=0
11214
11215         # perform 2 reads and writes so MAX is different from SUM.
11216         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11217         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11218         cancel_lru_locks osc
11219         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11220         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11221
11222         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11223         while read name count samp unit min max sum sumsq; do
11224                 echo "got $count $name"
11225                 eval $name=$count || error "Wrong proc format"
11226
11227                 case $name in
11228                 read_bytes)
11229                         [ $count -ne 2 ] && error "count is not 2: $count"
11230                         [ $min -ne $PAGE_SIZE ] &&
11231                                 error "min is not $PAGE_SIZE: $min"
11232                         [ $max -ne $PAGE_SIZE ] &&
11233                                 error "max is incorrect: $max"
11234                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11235                                 error "sum is wrong: $sum"
11236                         ;;
11237                 write_bytes)
11238                         [ $count -ne 2 ] && error "count is not 2: $count"
11239                         [ $min -ne $PAGE_SIZE ] &&
11240                                 error "min is not $PAGE_SIZE: $min"
11241                         [ $max -ne $PAGE_SIZE ] &&
11242                                 error "max is incorrect: $max"
11243                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11244                                 error "sum is wrong: $sum"
11245                         ;;
11246                 *) ;;
11247                 esac
11248         done < $TMP/$tfile.tmp
11249
11250         #check that we actually got some stats
11251         [ "$read_bytes" ] || error "Missing read_bytes stats"
11252         [ "$write_bytes" ] || error "Missing write_bytes stats"
11253         [ "$read_bytes" != 0 ] || error "no read done"
11254         [ "$write_bytes" != 0 ] || error "no write done"
11255
11256         rm -f $TMP/${tfile}.tmp
11257 }
11258 run_test 127b "verify the llite client stats are sane"
11259
11260 test_127c() { # LU-12394
11261         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11262         local size
11263         local bsize
11264         local reads
11265         local writes
11266         local count
11267
11268         $LCTL set_param llite.*.extents_stats=1
11269         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11270
11271         # Use two stripes so there is enough space in default config
11272         $LFS setstripe -c 2 $DIR/$tfile
11273
11274         # Extent stats start at 0-4K and go in power of two buckets
11275         # LL_HIST_START = 12 --> 2^12 = 4K
11276         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11277         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11278         # small configs
11279         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11280                 do
11281                 # Write and read, 2x each, second time at a non-zero offset
11282                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11283                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11284                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11285                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11286                 rm -f $DIR/$tfile
11287         done
11288
11289         $LCTL get_param llite.*.extents_stats
11290
11291         count=2
11292         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11293                 do
11294                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11295                                 grep -m 1 $bsize)
11296                 reads=$(echo $bucket | awk '{print $5}')
11297                 writes=$(echo $bucket | awk '{print $9}')
11298                 [ "$reads" -eq $count ] ||
11299                         error "$reads reads in < $bsize bucket, expect $count"
11300                 [ "$writes" -eq $count ] ||
11301                         error "$writes writes in < $bsize bucket, expect $count"
11302         done
11303
11304         # Test mmap write and read
11305         $LCTL set_param llite.*.extents_stats=c
11306         size=512
11307         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11308         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11309         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11310
11311         $LCTL get_param llite.*.extents_stats
11312
11313         count=$(((size*1024) / PAGE_SIZE))
11314
11315         bsize=$((2 * PAGE_SIZE / 1024))K
11316
11317         bucket=$($LCTL get_param -n llite.*.extents_stats |
11318                         grep -m 1 $bsize)
11319         reads=$(echo $bucket | awk '{print $5}')
11320         writes=$(echo $bucket | awk '{print $9}')
11321         # mmap writes fault in the page first, creating an additonal read
11322         [ "$reads" -eq $((2 * count)) ] ||
11323                 error "$reads reads in < $bsize bucket, expect $count"
11324         [ "$writes" -eq $count ] ||
11325                 error "$writes writes in < $bsize bucket, expect $count"
11326 }
11327 run_test 127c "test llite extent stats with regular & mmap i/o"
11328
11329 test_128() { # bug 15212
11330         touch $DIR/$tfile
11331         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11332                 find $DIR/$tfile
11333                 find $DIR/$tfile
11334         EOF
11335
11336         result=$(grep error $TMP/$tfile.log)
11337         rm -f $DIR/$tfile $TMP/$tfile.log
11338         [ -z "$result" ] ||
11339                 error "consecutive find's under interactive lfs failed"
11340 }
11341 run_test 128 "interactive lfs for 2 consecutive find's"
11342
11343 set_dir_limits () {
11344         local mntdev
11345         local canondev
11346         local node
11347
11348         local ldproc=/proc/fs/ldiskfs
11349         local facets=$(get_facets MDS)
11350
11351         for facet in ${facets//,/ }; do
11352                 canondev=$(ldiskfs_canon \
11353                            *.$(convert_facet2label $facet).mntdev $facet)
11354                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11355                         ldproc=/sys/fs/ldiskfs
11356                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11357                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11358         done
11359 }
11360
11361 check_mds_dmesg() {
11362         local facets=$(get_facets MDS)
11363         for facet in ${facets//,/ }; do
11364                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11365         done
11366         return 1
11367 }
11368
11369 test_129() {
11370         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11371         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11372                 skip "Need MDS version with at least 2.5.56"
11373         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11374                 skip_env "ldiskfs only test"
11375         fi
11376         remote_mds_nodsh && skip "remote MDS with nodsh"
11377
11378         local ENOSPC=28
11379         local EFBIG=27
11380         local has_warning=false
11381
11382         rm -rf $DIR/$tdir
11383         mkdir -p $DIR/$tdir
11384
11385         # block size of mds1
11386         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11387         set_dir_limits $maxsize $maxsize
11388         local dirsize=$(stat -c%s "$DIR/$tdir")
11389         local nfiles=0
11390         while [[ $dirsize -le $maxsize ]]; do
11391                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11392                 rc=$?
11393                 if ! $has_warning; then
11394                         check_mds_dmesg '"is approaching"' && has_warning=true
11395                 fi
11396                 # check two errors:
11397                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11398                 # EFBIG for previous versions included in ldiskfs series
11399                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
11400                         set_dir_limits 0 0
11401                         echo "return code $rc received as expected"
11402
11403                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11404                                 error_exit "create failed w/o dir size limit"
11405
11406                         check_mds_dmesg '"has reached"' ||
11407                                 error_exit "reached message should be output"
11408
11409                         [ $has_warning = "false" ] &&
11410                                 error_exit "warning message should be output"
11411
11412                         dirsize=$(stat -c%s "$DIR/$tdir")
11413
11414                         [[ $dirsize -ge $maxsize ]] && return 0
11415                         error_exit "current dir size $dirsize, " \
11416                                    "previous limit $maxsize"
11417                 elif [ $rc -ne 0 ]; then
11418                         set_dir_limits 0 0
11419                         error_exit "return $rc received instead of expected " \
11420                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11421                 fi
11422                 nfiles=$((nfiles + 1))
11423                 dirsize=$(stat -c%s "$DIR/$tdir")
11424         done
11425
11426         set_dir_limits 0 0
11427         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11428 }
11429 run_test 129 "test directory size limit ========================"
11430
11431 OLDIFS="$IFS"
11432 cleanup_130() {
11433         trap 0
11434         IFS="$OLDIFS"
11435 }
11436
11437 test_130a() {
11438         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11439         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11440
11441         trap cleanup_130 EXIT RETURN
11442
11443         local fm_file=$DIR/$tfile
11444         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11445         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11446                 error "dd failed for $fm_file"
11447
11448         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11449         filefrag -ves $fm_file
11450         RC=$?
11451         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11452                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11453         [ $RC != 0 ] && error "filefrag $fm_file failed"
11454
11455         filefrag_op=$(filefrag -ve -k $fm_file |
11456                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11457         lun=$($GETSTRIPE -i $fm_file)
11458
11459         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11460         IFS=$'\n'
11461         tot_len=0
11462         for line in $filefrag_op
11463         do
11464                 frag_lun=`echo $line | cut -d: -f5`
11465                 ext_len=`echo $line | cut -d: -f4`
11466                 if (( $frag_lun != $lun )); then
11467                         cleanup_130
11468                         error "FIEMAP on 1-stripe file($fm_file) failed"
11469                         return
11470                 fi
11471                 (( tot_len += ext_len ))
11472         done
11473
11474         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11475                 cleanup_130
11476                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11477                 return
11478         fi
11479
11480         cleanup_130
11481
11482         echo "FIEMAP on single striped file succeeded"
11483 }
11484 run_test 130a "FIEMAP (1-stripe file)"
11485
11486 test_130b() {
11487         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11488
11489         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11490         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11491
11492         trap cleanup_130 EXIT RETURN
11493
11494         local fm_file=$DIR/$tfile
11495         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11496                         error "setstripe on $fm_file"
11497         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11498                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11499
11500         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11501                 error "dd failed on $fm_file"
11502
11503         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11504         filefrag_op=$(filefrag -ve -k $fm_file |
11505                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11506
11507         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11508                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11509
11510         IFS=$'\n'
11511         tot_len=0
11512         num_luns=1
11513         for line in $filefrag_op
11514         do
11515                 frag_lun=$(echo $line | cut -d: -f5 |
11516                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11517                 ext_len=$(echo $line | cut -d: -f4)
11518                 if (( $frag_lun != $last_lun )); then
11519                         if (( tot_len != 1024 )); then
11520                                 cleanup_130
11521                                 error "FIEMAP on $fm_file failed; returned " \
11522                                 "len $tot_len for OST $last_lun instead of 1024"
11523                                 return
11524                         else
11525                                 (( num_luns += 1 ))
11526                                 tot_len=0
11527                         fi
11528                 fi
11529                 (( tot_len += ext_len ))
11530                 last_lun=$frag_lun
11531         done
11532         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11533                 cleanup_130
11534                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11535                         "luns or wrong len for OST $last_lun"
11536                 return
11537         fi
11538
11539         cleanup_130
11540
11541         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11542 }
11543 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11544
11545 test_130c() {
11546         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11547
11548         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11549         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11550
11551         trap cleanup_130 EXIT RETURN
11552
11553         local fm_file=$DIR/$tfile
11554         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11555         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11556                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11557
11558         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11559                         error "dd failed on $fm_file"
11560
11561         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11562         filefrag_op=$(filefrag -ve -k $fm_file |
11563                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11564
11565         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11566                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11567
11568         IFS=$'\n'
11569         tot_len=0
11570         num_luns=1
11571         for line in $filefrag_op
11572         do
11573                 frag_lun=$(echo $line | cut -d: -f5 |
11574                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11575                 ext_len=$(echo $line | cut -d: -f4)
11576                 if (( $frag_lun != $last_lun )); then
11577                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11578                         if (( logical != 512 )); then
11579                                 cleanup_130
11580                                 error "FIEMAP on $fm_file failed; returned " \
11581                                 "logical start for lun $logical instead of 512"
11582                                 return
11583                         fi
11584                         if (( tot_len != 512 )); then
11585                                 cleanup_130
11586                                 error "FIEMAP on $fm_file failed; returned " \
11587                                 "len $tot_len for OST $last_lun instead of 1024"
11588                                 return
11589                         else
11590                                 (( num_luns += 1 ))
11591                                 tot_len=0
11592                         fi
11593                 fi
11594                 (( tot_len += ext_len ))
11595                 last_lun=$frag_lun
11596         done
11597         if (( num_luns != 2 || tot_len != 512 )); then
11598                 cleanup_130
11599                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11600                         "luns or wrong len for OST $last_lun"
11601                 return
11602         fi
11603
11604         cleanup_130
11605
11606         echo "FIEMAP on 2-stripe file with hole succeeded"
11607 }
11608 run_test 130c "FIEMAP (2-stripe file with hole)"
11609
11610 test_130d() {
11611         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11612
11613         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11614         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11615
11616         trap cleanup_130 EXIT RETURN
11617
11618         local fm_file=$DIR/$tfile
11619         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11620                         error "setstripe on $fm_file"
11621         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11622                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11623
11624         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
11625         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11626                 error "dd failed on $fm_file"
11627
11628         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11629         filefrag_op=$(filefrag -ve -k $fm_file |
11630                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11631
11632         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11633                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11634
11635         IFS=$'\n'
11636         tot_len=0
11637         num_luns=1
11638         for line in $filefrag_op
11639         do
11640                 frag_lun=$(echo $line | cut -d: -f5 |
11641                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11642                 ext_len=$(echo $line | cut -d: -f4)
11643                 if (( $frag_lun != $last_lun )); then
11644                         if (( tot_len != 1024 )); then
11645                                 cleanup_130
11646                                 error "FIEMAP on $fm_file failed; returned " \
11647                                 "len $tot_len for OST $last_lun instead of 1024"
11648                                 return
11649                         else
11650                                 (( num_luns += 1 ))
11651                                 tot_len=0
11652                         fi
11653                 fi
11654                 (( tot_len += ext_len ))
11655                 last_lun=$frag_lun
11656         done
11657         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11658                 cleanup_130
11659                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11660                         "luns or wrong len for OST $last_lun"
11661                 return
11662         fi
11663
11664         cleanup_130
11665
11666         echo "FIEMAP on N-stripe file succeeded"
11667 }
11668 run_test 130d "FIEMAP (N-stripe file)"
11669
11670 test_130e() {
11671         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11672
11673         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11674         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11675
11676         trap cleanup_130 EXIT RETURN
11677
11678         local fm_file=$DIR/$tfile
11679         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11680         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11681                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11682
11683         NUM_BLKS=512
11684         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11685         for ((i = 0; i < $NUM_BLKS; i++))
11686         do
11687                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11688         done
11689
11690         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11691         filefrag_op=$(filefrag -ve -k $fm_file |
11692                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11693
11694         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11695                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11696
11697         IFS=$'\n'
11698         tot_len=0
11699         num_luns=1
11700         for line in $filefrag_op
11701         do
11702                 frag_lun=$(echo $line | cut -d: -f5 |
11703                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11704                 ext_len=$(echo $line | cut -d: -f4)
11705                 if (( $frag_lun != $last_lun )); then
11706                         if (( tot_len != $EXPECTED_LEN )); then
11707                                 cleanup_130
11708                                 error "FIEMAP on $fm_file failed; returned " \
11709                                 "len $tot_len for OST $last_lun instead " \
11710                                 "of $EXPECTED_LEN"
11711                                 return
11712                         else
11713                                 (( num_luns += 1 ))
11714                                 tot_len=0
11715                         fi
11716                 fi
11717                 (( tot_len += ext_len ))
11718                 last_lun=$frag_lun
11719         done
11720         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11721                 cleanup_130
11722                 error "FIEMAP on $fm_file failed; returned wrong number " \
11723                         "of luns or wrong len for OST $last_lun"
11724                 return
11725         fi
11726
11727         cleanup_130
11728
11729         echo "FIEMAP with continuation calls succeeded"
11730 }
11731 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11732
11733 test_130f() {
11734         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11735         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11736
11737         local fm_file=$DIR/$tfile
11738         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
11739                 error "multiop create with lov_delay_create on $fm_file"
11740
11741         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11742         filefrag_extents=$(filefrag -vek $fm_file |
11743                            awk '/extents? found/ { print $2 }')
11744         if [[ "$filefrag_extents" != "0" ]]; then
11745                 error "FIEMAP on $fm_file failed; " \
11746                       "returned $filefrag_extents expected 0"
11747         fi
11748
11749         rm -f $fm_file
11750 }
11751 run_test 130f "FIEMAP (unstriped file)"
11752
11753 # Test for writev/readv
11754 test_131a() {
11755         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
11756                 error "writev test failed"
11757         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
11758                 error "readv failed"
11759         rm -f $DIR/$tfile
11760 }
11761 run_test 131a "test iov's crossing stripe boundary for writev/readv"
11762
11763 test_131b() {
11764         local fsize=$((524288 + 1048576 + 1572864))
11765         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
11766                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11767                         error "append writev test failed"
11768
11769         ((fsize += 1572864 + 1048576))
11770         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
11771                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11772                         error "append writev test failed"
11773         rm -f $DIR/$tfile
11774 }
11775 run_test 131b "test append writev"
11776
11777 test_131c() {
11778         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
11779         error "NOT PASS"
11780 }
11781 run_test 131c "test read/write on file w/o objects"
11782
11783 test_131d() {
11784         rwv -f $DIR/$tfile -w -n 1 1572864
11785         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
11786         if [ "$NOB" != 1572864 ]; then
11787                 error "Short read filed: read $NOB bytes instead of 1572864"
11788         fi
11789         rm -f $DIR/$tfile
11790 }
11791 run_test 131d "test short read"
11792
11793 test_131e() {
11794         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
11795         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
11796         error "read hitting hole failed"
11797         rm -f $DIR/$tfile
11798 }
11799 run_test 131e "test read hitting hole"
11800
11801 check_stats() {
11802         local facet=$1
11803         local op=$2
11804         local want=${3:-0}
11805         local res
11806
11807         case $facet in
11808         mds*) res=$(do_facet $facet \
11809                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
11810                  ;;
11811         ost*) res=$(do_facet $facet \
11812                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
11813                  ;;
11814         *) error "Wrong facet '$facet'" ;;
11815         esac
11816         [ "$res" ] || error "The counter for $op on $facet was not incremented"
11817         # if the argument $3 is zero, it means any stat increment is ok.
11818         if [[ $want -gt 0 ]]; then
11819                 local count=$(echo $res | awk '{ print $2 }')
11820                 [[ $count -ne $want ]] &&
11821                         error "The $op counter on $facet is $count, not $want"
11822         fi
11823 }
11824
11825 test_133a() {
11826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11827         remote_ost_nodsh && skip "remote OST with nodsh"
11828         remote_mds_nodsh && skip "remote MDS with nodsh"
11829         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11830                 skip_env "MDS doesn't support rename stats"
11831
11832         local testdir=$DIR/${tdir}/stats_testdir
11833
11834         mkdir -p $DIR/${tdir}
11835
11836         # clear stats.
11837         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11838         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11839
11840         # verify mdt stats first.
11841         mkdir ${testdir} || error "mkdir failed"
11842         check_stats $SINGLEMDS "mkdir" 1
11843         touch ${testdir}/${tfile} || error "touch failed"
11844         check_stats $SINGLEMDS "open" 1
11845         check_stats $SINGLEMDS "close" 1
11846         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
11847                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
11848                 check_stats $SINGLEMDS "mknod" 2
11849         }
11850         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
11851         check_stats $SINGLEMDS "unlink" 1
11852         rm -f ${testdir}/${tfile} || error "file remove failed"
11853         check_stats $SINGLEMDS "unlink" 2
11854
11855         # remove working dir and check mdt stats again.
11856         rmdir ${testdir} || error "rmdir failed"
11857         check_stats $SINGLEMDS "rmdir" 1
11858
11859         local testdir1=$DIR/${tdir}/stats_testdir1
11860         mkdir -p ${testdir}
11861         mkdir -p ${testdir1}
11862         touch ${testdir1}/test1
11863         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
11864         check_stats $SINGLEMDS "crossdir_rename" 1
11865
11866         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
11867         check_stats $SINGLEMDS "samedir_rename" 1
11868
11869         rm -rf $DIR/${tdir}
11870 }
11871 run_test 133a "Verifying MDT stats ========================================"
11872
11873 test_133b() {
11874         local res
11875
11876         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11877         remote_ost_nodsh && skip "remote OST with nodsh"
11878         remote_mds_nodsh && skip "remote MDS with nodsh"
11879
11880         local testdir=$DIR/${tdir}/stats_testdir
11881
11882         mkdir -p ${testdir} || error "mkdir failed"
11883         touch ${testdir}/${tfile} || error "touch failed"
11884         cancel_lru_locks mdc
11885
11886         # clear stats.
11887         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11888         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11889
11890         # extra mdt stats verification.
11891         chmod 444 ${testdir}/${tfile} || error "chmod failed"
11892         check_stats $SINGLEMDS "setattr" 1
11893         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11894         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
11895         then            # LU-1740
11896                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
11897                 check_stats $SINGLEMDS "getattr" 1
11898         fi
11899         rm -rf $DIR/${tdir}
11900
11901         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
11902         # so the check below is not reliable
11903         [ $MDSCOUNT -eq 1 ] || return 0
11904
11905         # Sleep to avoid a cached response.
11906         #define OBD_STATFS_CACHE_SECONDS 1
11907         sleep 2
11908         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11909         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11910         $LFS df || error "lfs failed"
11911         check_stats $SINGLEMDS "statfs" 1
11912
11913         # check aggregated statfs (LU-10018)
11914         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
11915                 return 0
11916         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
11917                 return 0
11918         sleep 2
11919         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11920         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11921         df $DIR
11922         check_stats $SINGLEMDS "statfs" 1
11923
11924         # We want to check that the client didn't send OST_STATFS to
11925         # ost1 but the MDT also uses OST_STATFS for precreate. So some
11926         # extra care is needed here.
11927         if remote_mds; then
11928                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
11929                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
11930
11931                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
11932                 [ "$res" ] && error "OST got STATFS"
11933         fi
11934
11935         return 0
11936 }
11937 run_test 133b "Verifying extra MDT stats =================================="
11938
11939 test_133c() {
11940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11941         remote_ost_nodsh && skip "remote OST with nodsh"
11942         remote_mds_nodsh && skip "remote MDS with nodsh"
11943
11944         local testdir=$DIR/$tdir/stats_testdir
11945
11946         test_mkdir -p $testdir
11947
11948         # verify obdfilter stats.
11949         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
11950         sync
11951         cancel_lru_locks osc
11952         wait_delete_completed
11953
11954         # clear stats.
11955         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11956         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11957
11958         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
11959                 error "dd failed"
11960         sync
11961         cancel_lru_locks osc
11962         check_stats ost1 "write" 1
11963
11964         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
11965         check_stats ost1 "read" 1
11966
11967         > $testdir/$tfile || error "truncate failed"
11968         check_stats ost1 "punch" 1
11969
11970         rm -f $testdir/$tfile || error "file remove failed"
11971         wait_delete_completed
11972         check_stats ost1 "destroy" 1
11973
11974         rm -rf $DIR/$tdir
11975 }
11976 run_test 133c "Verifying OST stats ========================================"
11977
11978 order_2() {
11979         local value=$1
11980         local orig=$value
11981         local order=1
11982
11983         while [ $value -ge 2 ]; do
11984                 order=$((order*2))
11985                 value=$((value/2))
11986         done
11987
11988         if [ $orig -gt $order ]; then
11989                 order=$((order*2))
11990         fi
11991         echo $order
11992 }
11993
11994 size_in_KMGT() {
11995     local value=$1
11996     local size=('K' 'M' 'G' 'T');
11997     local i=0
11998     local size_string=$value
11999
12000     while [ $value -ge 1024 ]; do
12001         if [ $i -gt 3 ]; then
12002             #T is the biggest unit we get here, if that is bigger,
12003             #just return XXXT
12004             size_string=${value}T
12005             break
12006         fi
12007         value=$((value >> 10))
12008         if [ $value -lt 1024 ]; then
12009             size_string=${value}${size[$i]}
12010             break
12011         fi
12012         i=$((i + 1))
12013     done
12014
12015     echo $size_string
12016 }
12017
12018 get_rename_size() {
12019         local size=$1
12020         local context=${2:-.}
12021         local sample=$(do_facet $SINGLEMDS $LCTL \
12022                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12023                 grep -A1 $context |
12024                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12025         echo $sample
12026 }
12027
12028 test_133d() {
12029         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12030         remote_ost_nodsh && skip "remote OST with nodsh"
12031         remote_mds_nodsh && skip "remote MDS with nodsh"
12032         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12033                 skip_env "MDS doesn't support rename stats"
12034
12035         local testdir1=$DIR/${tdir}/stats_testdir1
12036         local testdir2=$DIR/${tdir}/stats_testdir2
12037         mkdir -p $DIR/${tdir}
12038
12039         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12040
12041         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12042         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12043
12044         createmany -o $testdir1/test 512 || error "createmany failed"
12045
12046         # check samedir rename size
12047         mv ${testdir1}/test0 ${testdir1}/test_0
12048
12049         local testdir1_size=$(ls -l $DIR/${tdir} |
12050                 awk '/stats_testdir1/ {print $5}')
12051         local testdir2_size=$(ls -l $DIR/${tdir} |
12052                 awk '/stats_testdir2/ {print $5}')
12053
12054         testdir1_size=$(order_2 $testdir1_size)
12055         testdir2_size=$(order_2 $testdir2_size)
12056
12057         testdir1_size=$(size_in_KMGT $testdir1_size)
12058         testdir2_size=$(size_in_KMGT $testdir2_size)
12059
12060         echo "source rename dir size: ${testdir1_size}"
12061         echo "target rename dir size: ${testdir2_size}"
12062
12063         local cmd="do_facet $SINGLEMDS $LCTL "
12064         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12065
12066         eval $cmd || error "$cmd failed"
12067         local samedir=$($cmd | grep 'same_dir')
12068         local same_sample=$(get_rename_size $testdir1_size)
12069         [ -z "$samedir" ] && error "samedir_rename_size count error"
12070         [[ $same_sample -eq 1 ]] ||
12071                 error "samedir_rename_size error $same_sample"
12072         echo "Check same dir rename stats success"
12073
12074         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12075
12076         # check crossdir rename size
12077         mv ${testdir1}/test_0 ${testdir2}/test_0
12078
12079         testdir1_size=$(ls -l $DIR/${tdir} |
12080                 awk '/stats_testdir1/ {print $5}')
12081         testdir2_size=$(ls -l $DIR/${tdir} |
12082                 awk '/stats_testdir2/ {print $5}')
12083
12084         testdir1_size=$(order_2 $testdir1_size)
12085         testdir2_size=$(order_2 $testdir2_size)
12086
12087         testdir1_size=$(size_in_KMGT $testdir1_size)
12088         testdir2_size=$(size_in_KMGT $testdir2_size)
12089
12090         echo "source rename dir size: ${testdir1_size}"
12091         echo "target rename dir size: ${testdir2_size}"
12092
12093         eval $cmd || error "$cmd failed"
12094         local crossdir=$($cmd | grep 'crossdir')
12095         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12096         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12097         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12098         [[ $src_sample -eq 1 ]] ||
12099                 error "crossdir_rename_size error $src_sample"
12100         [[ $tgt_sample -eq 1 ]] ||
12101                 error "crossdir_rename_size error $tgt_sample"
12102         echo "Check cross dir rename stats success"
12103         rm -rf $DIR/${tdir}
12104 }
12105 run_test 133d "Verifying rename_stats ========================================"
12106
12107 test_133e() {
12108         remote_mds_nodsh && skip "remote MDS with nodsh"
12109         remote_ost_nodsh && skip "remote OST with nodsh"
12110         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12111
12112         local testdir=$DIR/${tdir}/stats_testdir
12113         local ctr f0 f1 bs=32768 count=42 sum
12114
12115         mkdir -p ${testdir} || error "mkdir failed"
12116
12117         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
12118
12119         for ctr in {write,read}_bytes; do
12120                 sync
12121                 cancel_lru_locks osc
12122
12123                 do_facet ost1 $LCTL set_param -n \
12124                         "obdfilter.*.exports.clear=clear"
12125
12126                 if [ $ctr = write_bytes ]; then
12127                         f0=/dev/zero
12128                         f1=${testdir}/${tfile}
12129                 else
12130                         f0=${testdir}/${tfile}
12131                         f1=/dev/null
12132                 fi
12133
12134                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12135                         error "dd failed"
12136                 sync
12137                 cancel_lru_locks osc
12138
12139                 sum=$(do_facet ost1 $LCTL get_param \
12140                         "obdfilter.*.exports.*.stats" |
12141                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12142                                 $1 == ctr { sum += $7 }
12143                                 END { printf("%0.0f", sum) }')
12144
12145                 if ((sum != bs * count)); then
12146                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12147                 fi
12148         done
12149
12150         rm -rf $DIR/${tdir}
12151 }
12152 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12153
12154 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12155
12156 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12157 # not honor the -ignore_readdir_race option correctly. So we call
12158 # error_ignore() rather than error() in these cases. See LU-11152.
12159 error_133() {
12160         if (find --version; do_facet mds1 find --version) |
12161                 grep -q '\b4\.5\.1[1-4]\b'; then
12162                 error_ignore LU-11152 "$@"
12163         else
12164                 error "$@"
12165         fi
12166 }
12167
12168 test_133f() {
12169         # First without trusting modes.
12170         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12171         echo "proc_dirs='$proc_dirs'"
12172         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12173         find $proc_dirs -exec cat '{}' \; &> /dev/null
12174
12175         # Second verifying readability.
12176         $LCTL get_param -R '*' &> /dev/null
12177
12178         # Verifing writability with badarea_io.
12179         find $proc_dirs \
12180                 -ignore_readdir_race \
12181                 -type f \
12182                 -not -name force_lbug \
12183                 -not -name changelog_mask \
12184                 -exec badarea_io '{}' \; ||
12185                         error_133 "find $proc_dirs failed"
12186 }
12187 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12188
12189 test_133g() {
12190         remote_mds_nodsh && skip "remote MDS with nodsh"
12191         remote_ost_nodsh && skip "remote OST with nodsh"
12192
12193         # eventually, this can also be replaced with "lctl get_param -R",
12194         # but not until that option is always available on the server
12195         local facet
12196         for facet in mds1 ost1; do
12197                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
12198                         skip_noexit "Too old lustre on $facet"
12199                 local facet_proc_dirs=$(do_facet $facet \
12200                                         \\\ls -d $proc_regexp 2>/dev/null)
12201                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12202                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12203                 do_facet $facet find $facet_proc_dirs \
12204                         ! -name req_history \
12205                         -exec cat '{}' \\\; &> /dev/null
12206
12207                 do_facet $facet find $facet_proc_dirs \
12208                         ! -name req_history \
12209                         -type f \
12210                         -exec cat '{}' \\\; &> /dev/null ||
12211                                 error "proc file read failed"
12212
12213                 do_facet $facet find $facet_proc_dirs \
12214                         -ignore_readdir_race \
12215                         -type f \
12216                         -not -name force_lbug \
12217                         -not -name changelog_mask \
12218                         -exec badarea_io '{}' \\\; ||
12219                                 error_133 "$facet find $facet_proc_dirs failed"
12220         done
12221
12222         # remount the FS in case writes/reads /proc break the FS
12223         cleanup || error "failed to unmount"
12224         setup || error "failed to setup"
12225         true
12226 }
12227 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12228
12229 test_133h() {
12230         remote_mds_nodsh && skip "remote MDS with nodsh"
12231         remote_ost_nodsh && skip "remote OST with nodsh"
12232         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12233                 skip "Need MDS version at least 2.9.54"
12234
12235         local facet
12236
12237         for facet in client mds1 ost1; do
12238                 local facet_proc_dirs=$(do_facet $facet \
12239                                         \\\ls -d $proc_regexp 2> /dev/null)
12240                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12241                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12242                 # Get the list of files that are missing the terminating newline
12243                 local missing=($(do_facet $facet \
12244                         find ${facet_proc_dirs} -type f \|              \
12245                                 while read F\; do                       \
12246                                         awk -v FS='\v' -v RS='\v\v'     \
12247                                         "'END { if(NR>0 &&              \
12248                                         \\\$NF !~ /.*\\\n\$/)           \
12249                                                 print FILENAME}'"       \
12250                                         '\$F'\;                         \
12251                                 done 2>/dev/null))
12252                 [ ${#missing[*]} -eq 0 ] ||
12253                         error "files do not end with newline: ${missing[*]}"
12254         done
12255 }
12256 run_test 133h "Proc files should end with newlines"
12257
12258 test_134a() {
12259         remote_mds_nodsh && skip "remote MDS with nodsh"
12260         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12261                 skip "Need MDS version at least 2.7.54"
12262
12263         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12264         cancel_lru_locks mdc
12265
12266         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12267         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12268         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12269
12270         local nr=1000
12271         createmany -o $DIR/$tdir/f $nr ||
12272                 error "failed to create $nr files in $DIR/$tdir"
12273         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12274
12275         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12276         do_facet mds1 $LCTL set_param fail_loc=0x327
12277         do_facet mds1 $LCTL set_param fail_val=500
12278         touch $DIR/$tdir/m
12279
12280         echo "sleep 10 seconds ..."
12281         sleep 10
12282         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12283
12284         do_facet mds1 $LCTL set_param fail_loc=0
12285         do_facet mds1 $LCTL set_param fail_val=0
12286         [ $lck_cnt -lt $unused ] ||
12287                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12288
12289         rm $DIR/$tdir/m
12290         unlinkmany $DIR/$tdir/f $nr
12291 }
12292 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12293
12294 test_134b() {
12295         remote_mds_nodsh && skip "remote MDS with nodsh"
12296         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12297                 skip "Need MDS version at least 2.7.54"
12298
12299         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12300         cancel_lru_locks mdc
12301
12302         local low_wm=$(do_facet mds1 $LCTL get_param -n \
12303                         ldlm.lock_reclaim_threshold_mb)
12304         # disable reclaim temporarily
12305         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12306
12307         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12308         do_facet mds1 $LCTL set_param fail_loc=0x328
12309         do_facet mds1 $LCTL set_param fail_val=500
12310
12311         $LCTL set_param debug=+trace
12312
12313         local nr=600
12314         createmany -o $DIR/$tdir/f $nr &
12315         local create_pid=$!
12316
12317         echo "Sleep $TIMEOUT seconds ..."
12318         sleep $TIMEOUT
12319         if ! ps -p $create_pid  > /dev/null 2>&1; then
12320                 do_facet mds1 $LCTL set_param fail_loc=0
12321                 do_facet mds1 $LCTL set_param fail_val=0
12322                 do_facet mds1 $LCTL set_param \
12323                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12324                 error "createmany finished incorrectly!"
12325         fi
12326         do_facet mds1 $LCTL set_param fail_loc=0
12327         do_facet mds1 $LCTL set_param fail_val=0
12328         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12329         wait $create_pid || return 1
12330
12331         unlinkmany $DIR/$tdir/f $nr
12332 }
12333 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12334
12335 test_140() { #bug-17379
12336         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12337
12338         test_mkdir $DIR/$tdir
12339         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12340         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12341
12342         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12343         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12344         local i=0
12345         while i=$((i + 1)); do
12346                 test_mkdir $i
12347                 cd $i || error "Changing to $i"
12348                 ln -s ../stat stat || error "Creating stat symlink"
12349                 # Read the symlink until ELOOP present,
12350                 # not LBUGing the system is considered success,
12351                 # we didn't overrun the stack.
12352                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12353                 if [ $ret -ne 0 ]; then
12354                         if [ $ret -eq 40 ]; then
12355                                 break  # -ELOOP
12356                         else
12357                                 error "Open stat symlink"
12358                                         return
12359                         fi
12360                 fi
12361         done
12362         i=$((i - 1))
12363         echo "The symlink depth = $i"
12364         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
12365                                         error "Invalid symlink depth"
12366
12367         # Test recursive symlink
12368         ln -s symlink_self symlink_self
12369         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12370         echo "open symlink_self returns $ret"
12371         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12372 }
12373 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12374
12375 test_150() {
12376         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12377
12378         local TF="$TMP/$tfile"
12379
12380         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12381         cp $TF $DIR/$tfile
12382         cancel_lru_locks $OSC
12383         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12384         remount_client $MOUNT
12385         df -P $MOUNT
12386         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12387
12388         $TRUNCATE $TF 6000
12389         $TRUNCATE $DIR/$tfile 6000
12390         cancel_lru_locks $OSC
12391         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12392
12393         echo "12345" >>$TF
12394         echo "12345" >>$DIR/$tfile
12395         cancel_lru_locks $OSC
12396         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12397
12398         echo "12345" >>$TF
12399         echo "12345" >>$DIR/$tfile
12400         cancel_lru_locks $OSC
12401         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12402
12403         rm -f $TF
12404         true
12405 }
12406 run_test 150 "truncate/append tests"
12407
12408 #LU-2902 roc_hit was not able to read all values from lproc
12409 function roc_hit_init() {
12410         local list=$(comma_list $(osts_nodes))
12411         local dir=$DIR/$tdir-check
12412         local file=$dir/$tfile
12413         local BEFORE
12414         local AFTER
12415         local idx
12416
12417         test_mkdir $dir
12418         #use setstripe to do a write to every ost
12419         for i in $(seq 0 $((OSTCOUNT-1))); do
12420                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
12421                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12422                 idx=$(printf %04x $i)
12423                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12424                         awk '$1 == "cache_access" {sum += $7}
12425                                 END { printf("%0.0f", sum) }')
12426
12427                 cancel_lru_locks osc
12428                 cat $file >/dev/null
12429
12430                 AFTER=$(get_osd_param $list *OST*$idx stats |
12431                         awk '$1 == "cache_access" {sum += $7}
12432                                 END { printf("%0.0f", sum) }')
12433
12434                 echo BEFORE:$BEFORE AFTER:$AFTER
12435                 if ! let "AFTER - BEFORE == 4"; then
12436                         rm -rf $dir
12437                         error "roc_hit is not safe to use"
12438                 fi
12439                 rm $file
12440         done
12441
12442         rm -rf $dir
12443 }
12444
12445 function roc_hit() {
12446         local list=$(comma_list $(osts_nodes))
12447         echo $(get_osd_param $list '' stats |
12448                 awk '$1 == "cache_hit" {sum += $7}
12449                         END { printf("%0.0f", sum) }')
12450 }
12451
12452 function set_cache() {
12453         local on=1
12454
12455         if [ "$2" == "off" ]; then
12456                 on=0;
12457         fi
12458         local list=$(comma_list $(osts_nodes))
12459         set_osd_param $list '' $1_cache_enable $on
12460
12461         cancel_lru_locks osc
12462 }
12463
12464 test_151() {
12465         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12466         remote_ost_nodsh && skip "remote OST with nodsh"
12467
12468         local CPAGES=3
12469         local list=$(comma_list $(osts_nodes))
12470
12471         # check whether obdfilter is cache capable at all
12472         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12473                 skip "not cache-capable obdfilter"
12474         fi
12475
12476         # check cache is enabled on all obdfilters
12477         if get_osd_param $list '' read_cache_enable | grep 0; then
12478                 skip "oss cache is disabled"
12479         fi
12480
12481         set_osd_param $list '' writethrough_cache_enable 1
12482
12483         # check write cache is enabled on all obdfilters
12484         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12485                 skip "oss write cache is NOT enabled"
12486         fi
12487
12488         roc_hit_init
12489
12490         #define OBD_FAIL_OBD_NO_LRU  0x609
12491         do_nodes $list $LCTL set_param fail_loc=0x609
12492
12493         # pages should be in the case right after write
12494         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12495                 error "dd failed"
12496
12497         local BEFORE=$(roc_hit)
12498         cancel_lru_locks osc
12499         cat $DIR/$tfile >/dev/null
12500         local AFTER=$(roc_hit)
12501
12502         do_nodes $list $LCTL set_param fail_loc=0
12503
12504         if ! let "AFTER - BEFORE == CPAGES"; then
12505                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12506         fi
12507
12508         # the following read invalidates the cache
12509         cancel_lru_locks osc
12510         set_osd_param $list '' read_cache_enable 0
12511         cat $DIR/$tfile >/dev/null
12512
12513         # now data shouldn't be found in the cache
12514         BEFORE=$(roc_hit)
12515         cancel_lru_locks osc
12516         cat $DIR/$tfile >/dev/null
12517         AFTER=$(roc_hit)
12518         if let "AFTER - BEFORE != 0"; then
12519                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12520         fi
12521
12522         set_osd_param $list '' read_cache_enable 1
12523         rm -f $DIR/$tfile
12524 }
12525 run_test 151 "test cache on oss and controls ==============================="
12526
12527 test_152() {
12528         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12529
12530         local TF="$TMP/$tfile"
12531
12532         # simulate ENOMEM during write
12533 #define OBD_FAIL_OST_NOMEM      0x226
12534         lctl set_param fail_loc=0x80000226
12535         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12536         cp $TF $DIR/$tfile
12537         sync || error "sync failed"
12538         lctl set_param fail_loc=0
12539
12540         # discard client's cache
12541         cancel_lru_locks osc
12542
12543         # simulate ENOMEM during read
12544         lctl set_param fail_loc=0x80000226
12545         cmp $TF $DIR/$tfile || error "cmp failed"
12546         lctl set_param fail_loc=0
12547
12548         rm -f $TF
12549 }
12550 run_test 152 "test read/write with enomem ============================"
12551
12552 test_153() {
12553         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12554 }
12555 run_test 153 "test if fdatasync does not crash ======================="
12556
12557 dot_lustre_fid_permission_check() {
12558         local fid=$1
12559         local ffid=$MOUNT/.lustre/fid/$fid
12560         local test_dir=$2
12561
12562         echo "stat fid $fid"
12563         stat $ffid > /dev/null || error "stat $ffid failed."
12564         echo "touch fid $fid"
12565         touch $ffid || error "touch $ffid failed."
12566         echo "write to fid $fid"
12567         cat /etc/hosts > $ffid || error "write $ffid failed."
12568         echo "read fid $fid"
12569         diff /etc/hosts $ffid || error "read $ffid failed."
12570         echo "append write to fid $fid"
12571         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12572         echo "rename fid $fid"
12573         mv $ffid $test_dir/$tfile.1 &&
12574                 error "rename $ffid to $tfile.1 should fail."
12575         touch $test_dir/$tfile.1
12576         mv $test_dir/$tfile.1 $ffid &&
12577                 error "rename $tfile.1 to $ffid should fail."
12578         rm -f $test_dir/$tfile.1
12579         echo "truncate fid $fid"
12580         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12581         echo "link fid $fid"
12582         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12583         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12584                 echo "setfacl fid $fid"
12585                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12586                 echo "getfacl fid $fid"
12587                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12588         fi
12589         echo "unlink fid $fid"
12590         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12591         echo "mknod fid $fid"
12592         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12593
12594         fid=[0xf00000400:0x1:0x0]
12595         ffid=$MOUNT/.lustre/fid/$fid
12596
12597         echo "stat non-exist fid $fid"
12598         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12599         echo "write to non-exist fid $fid"
12600         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12601         echo "link new fid $fid"
12602         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12603
12604         mkdir -p $test_dir/$tdir
12605         touch $test_dir/$tdir/$tfile
12606         fid=$($LFS path2fid $test_dir/$tdir)
12607         rc=$?
12608         [ $rc -ne 0 ] &&
12609                 error "error: could not get fid for $test_dir/$dir/$tfile."
12610
12611         ffid=$MOUNT/.lustre/fid/$fid
12612
12613         echo "ls $fid"
12614         ls $ffid > /dev/null || error "ls $ffid failed."
12615         echo "touch $fid/$tfile.1"
12616         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12617
12618         echo "touch $MOUNT/.lustre/fid/$tfile"
12619         touch $MOUNT/.lustre/fid/$tfile && \
12620                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12621
12622         echo "setxattr to $MOUNT/.lustre/fid"
12623         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12624
12625         echo "listxattr for $MOUNT/.lustre/fid"
12626         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12627
12628         echo "delxattr from $MOUNT/.lustre/fid"
12629         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12630
12631         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12632         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12633                 error "touch invalid fid should fail."
12634
12635         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12636         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12637                 error "touch non-normal fid should fail."
12638
12639         echo "rename $tdir to $MOUNT/.lustre/fid"
12640         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12641                 error "rename to $MOUNT/.lustre/fid should fail."
12642
12643         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
12644         then            # LU-3547
12645                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
12646                 local new_obf_mode=777
12647
12648                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
12649                 chmod $new_obf_mode $DIR/.lustre/fid ||
12650                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
12651
12652                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
12653                 [ $obf_mode -eq $new_obf_mode ] ||
12654                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12655
12656                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12657                 chmod $old_obf_mode $DIR/.lustre/fid ||
12658                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12659         fi
12660
12661         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12662         fid=$($LFS path2fid $test_dir/$tfile-2)
12663
12664         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12665         then # LU-5424
12666                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12667                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12668                         error "create lov data thru .lustre failed"
12669         fi
12670         echo "cp /etc/passwd $test_dir/$tfile-2"
12671         cp /etc/passwd $test_dir/$tfile-2 ||
12672                 error "copy to $test_dir/$tfile-2 failed."
12673         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12674         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12675                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12676
12677         rm -rf $test_dir/tfile.lnk
12678         rm -rf $test_dir/$tfile-2
12679 }
12680
12681 test_154A() {
12682         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12683                 skip "Need MDS version at least 2.4.1"
12684
12685         local tf=$DIR/$tfile
12686         touch $tf
12687
12688         local fid=$($LFS path2fid $tf)
12689         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12690
12691         # check that we get the same pathname back
12692         local found=$($LFS fid2path $MOUNT "$fid")
12693         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12694         [ "$found" == "$tf" ] ||
12695                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12696 }
12697 run_test 154A "lfs path2fid and fid2path basic checks"
12698
12699 test_154B() {
12700         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12701                 skip "Need MDS version at least 2.4.1"
12702
12703         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12704         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12705         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12706         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12707
12708         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12709         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12710
12711         # check that we get the same pathname
12712         echo "PFID: $PFID, name: $name"
12713         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12714         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12715         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12716                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12717
12718         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12719 }
12720 run_test 154B "verify the ll_decode_linkea tool"
12721
12722 test_154a() {
12723         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12724         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12725         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12726                 skip "Need MDS version at least 2.2.51"
12727         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12728
12729         cp /etc/hosts $DIR/$tfile
12730
12731         fid=$($LFS path2fid $DIR/$tfile)
12732         rc=$?
12733         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12734
12735         dot_lustre_fid_permission_check "$fid" $DIR ||
12736                 error "dot lustre permission check $fid failed"
12737
12738         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
12739
12740         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
12741
12742         touch $MOUNT/.lustre/file &&
12743                 error "creation is not allowed under .lustre"
12744
12745         mkdir $MOUNT/.lustre/dir &&
12746                 error "mkdir is not allowed under .lustre"
12747
12748         rm -rf $DIR/$tfile
12749 }
12750 run_test 154a "Open-by-FID"
12751
12752 test_154b() {
12753         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12754         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12755         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12756         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12757                 skip "Need MDS version at least 2.2.51"
12758
12759         local remote_dir=$DIR/$tdir/remote_dir
12760         local MDTIDX=1
12761         local rc=0
12762
12763         mkdir -p $DIR/$tdir
12764         $LFS mkdir -i $MDTIDX $remote_dir ||
12765                 error "create remote directory failed"
12766
12767         cp /etc/hosts $remote_dir/$tfile
12768
12769         fid=$($LFS path2fid $remote_dir/$tfile)
12770         rc=$?
12771         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
12772
12773         dot_lustre_fid_permission_check "$fid" $remote_dir ||
12774                 error "dot lustre permission check $fid failed"
12775         rm -rf $DIR/$tdir
12776 }
12777 run_test 154b "Open-by-FID for remote directory"
12778
12779 test_154c() {
12780         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12781                 skip "Need MDS version at least 2.4.1"
12782
12783         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
12784         local FID1=$($LFS path2fid $DIR/$tfile.1)
12785         local FID2=$($LFS path2fid $DIR/$tfile.2)
12786         local FID3=$($LFS path2fid $DIR/$tfile.3)
12787
12788         local N=1
12789         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
12790                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
12791                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
12792                 local want=FID$N
12793                 [ "$FID" = "${!want}" ] ||
12794                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
12795                 N=$((N + 1))
12796         done
12797
12798         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
12799         do
12800                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
12801                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
12802                 N=$((N + 1))
12803         done
12804 }
12805 run_test 154c "lfs path2fid and fid2path multiple arguments"
12806
12807 test_154d() {
12808         remote_mds_nodsh && skip "remote MDS with nodsh"
12809         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
12810                 skip "Need MDS version at least 2.5.53"
12811
12812         if remote_mds; then
12813                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
12814         else
12815                 nid="0@lo"
12816         fi
12817         local proc_ofile="mdt.*.exports.'$nid'.open_files"
12818         local fd
12819         local cmd
12820
12821         rm -f $DIR/$tfile
12822         touch $DIR/$tfile
12823
12824         local fid=$($LFS path2fid $DIR/$tfile)
12825         # Open the file
12826         fd=$(free_fd)
12827         cmd="exec $fd<$DIR/$tfile"
12828         eval $cmd
12829         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
12830         echo "$fid_list" | grep "$fid"
12831         rc=$?
12832
12833         cmd="exec $fd>/dev/null"
12834         eval $cmd
12835         if [ $rc -ne 0 ]; then
12836                 error "FID $fid not found in open files list $fid_list"
12837         fi
12838 }
12839 run_test 154d "Verify open file fid"
12840
12841 test_154e()
12842 {
12843         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
12844                 skip "Need MDS version at least 2.6.50"
12845
12846         if ls -a $MOUNT | grep -q '^\.lustre$'; then
12847                 error ".lustre returned by readdir"
12848         fi
12849 }
12850 run_test 154e ".lustre is not returned by readdir"
12851
12852 test_154f() {
12853         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12854
12855         # create parent directory on a single MDT to avoid cross-MDT hardlinks
12856         test_mkdir -p -c1 $DIR/$tdir/d
12857         # test dirs inherit from its stripe
12858         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
12859         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
12860         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
12861         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
12862         touch $DIR/f
12863
12864         # get fid of parents
12865         local FID0=$($LFS path2fid $DIR/$tdir/d)
12866         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
12867         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
12868         local FID3=$($LFS path2fid $DIR)
12869
12870         # check that path2fid --parents returns expected <parent_fid>/name
12871         # 1) test for a directory (single parent)
12872         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
12873         [ "$parent" == "$FID0/foo1" ] ||
12874                 error "expected parent: $FID0/foo1, got: $parent"
12875
12876         # 2) test for a file with nlink > 1 (multiple parents)
12877         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
12878         echo "$parent" | grep -F "$FID1/$tfile" ||
12879                 error "$FID1/$tfile not returned in parent list"
12880         echo "$parent" | grep -F "$FID2/link" ||
12881                 error "$FID2/link not returned in parent list"
12882
12883         # 3) get parent by fid
12884         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
12885         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12886         echo "$parent" | grep -F "$FID1/$tfile" ||
12887                 error "$FID1/$tfile not returned in parent list (by fid)"
12888         echo "$parent" | grep -F "$FID2/link" ||
12889                 error "$FID2/link not returned in parent list (by fid)"
12890
12891         # 4) test for entry in root directory
12892         parent=$($LFS path2fid --parents $DIR/f)
12893         echo "$parent" | grep -F "$FID3/f" ||
12894                 error "$FID3/f not returned in parent list"
12895
12896         # 5) test it on root directory
12897         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
12898                 error "$MOUNT should not have parents"
12899
12900         # enable xattr caching and check that linkea is correctly updated
12901         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12902         save_lustre_params client "llite.*.xattr_cache" > $save
12903         lctl set_param llite.*.xattr_cache 1
12904
12905         # 6.1) linkea update on rename
12906         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
12907
12908         # get parents by fid
12909         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12910         # foo1 should no longer be returned in parent list
12911         echo "$parent" | grep -F "$FID1" &&
12912                 error "$FID1 should no longer be in parent list"
12913         # the new path should appear
12914         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
12915                 error "$FID2/$tfile.moved is not in parent list"
12916
12917         # 6.2) linkea update on unlink
12918         rm -f $DIR/$tdir/d/foo2/link
12919         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12920         # foo2/link should no longer be returned in parent list
12921         echo "$parent" | grep -F "$FID2/link" &&
12922                 error "$FID2/link should no longer be in parent list"
12923         true
12924
12925         rm -f $DIR/f
12926         restore_lustre_params < $save
12927         rm -f $save
12928 }
12929 run_test 154f "get parent fids by reading link ea"
12930
12931 test_154g()
12932 {
12933         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12934         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
12935            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
12936                 skip "Need MDS version at least 2.6.92"
12937
12938         mkdir -p $DIR/$tdir
12939         llapi_fid_test -d $DIR/$tdir
12940 }
12941 run_test 154g "various llapi FID tests"
12942
12943 test_155_small_load() {
12944     local temp=$TMP/$tfile
12945     local file=$DIR/$tfile
12946
12947     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
12948         error "dd of=$temp bs=6096 count=1 failed"
12949     cp $temp $file
12950     cancel_lru_locks $OSC
12951     cmp $temp $file || error "$temp $file differ"
12952
12953     $TRUNCATE $temp 6000
12954     $TRUNCATE $file 6000
12955     cmp $temp $file || error "$temp $file differ (truncate1)"
12956
12957     echo "12345" >>$temp
12958     echo "12345" >>$file
12959     cmp $temp $file || error "$temp $file differ (append1)"
12960
12961     echo "12345" >>$temp
12962     echo "12345" >>$file
12963     cmp $temp $file || error "$temp $file differ (append2)"
12964
12965     rm -f $temp $file
12966     true
12967 }
12968
12969 test_155_big_load() {
12970         remote_ost_nodsh && skip "remote OST with nodsh"
12971
12972         local temp=$TMP/$tfile
12973         local file=$DIR/$tfile
12974
12975         free_min_max
12976         local cache_size=$(do_facet ost$((MAXI+1)) \
12977                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
12978         local large_file_size=$((cache_size * 2))
12979
12980         echo "OSS cache size: $cache_size KB"
12981         echo "Large file size: $large_file_size KB"
12982
12983         [ $MAXV -le $large_file_size ] &&
12984                 skip_env "max available OST size needs > $large_file_size KB"
12985
12986         $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
12987
12988         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
12989                 error "dd of=$temp bs=$large_file_size count=1k failed"
12990         cp $temp $file
12991         ls -lh $temp $file
12992         cancel_lru_locks osc
12993         cmp $temp $file || error "$temp $file differ"
12994
12995         rm -f $temp $file
12996         true
12997 }
12998
12999 save_writethrough() {
13000         local facets=$(get_facets OST)
13001
13002         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13003 }
13004
13005 test_155a() {
13006         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13007
13008         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13009
13010         save_writethrough $p
13011
13012         set_cache read on
13013         set_cache writethrough on
13014         test_155_small_load
13015         restore_lustre_params < $p
13016         rm -f $p
13017 }
13018 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13019
13020 test_155b() {
13021         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13022
13023         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13024
13025         save_writethrough $p
13026
13027         set_cache read on
13028         set_cache writethrough off
13029         test_155_small_load
13030         restore_lustre_params < $p
13031         rm -f $p
13032 }
13033 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13034
13035 test_155c() {
13036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13037
13038         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13039
13040         save_writethrough $p
13041
13042         set_cache read off
13043         set_cache writethrough on
13044         test_155_small_load
13045         restore_lustre_params < $p
13046         rm -f $p
13047 }
13048 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13049
13050 test_155d() {
13051         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13052
13053         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13054
13055         save_writethrough $p
13056
13057         set_cache read off
13058         set_cache writethrough off
13059         test_155_small_load
13060         restore_lustre_params < $p
13061         rm -f $p
13062 }
13063 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13064
13065 test_155e() {
13066         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13067
13068         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13069
13070         save_writethrough $p
13071
13072         set_cache read on
13073         set_cache writethrough on
13074         test_155_big_load
13075         restore_lustre_params < $p
13076         rm -f $p
13077 }
13078 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13079
13080 test_155f() {
13081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13082
13083         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13084
13085         save_writethrough $p
13086
13087         set_cache read on
13088         set_cache writethrough off
13089         test_155_big_load
13090         restore_lustre_params < $p
13091         rm -f $p
13092 }
13093 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13094
13095 test_155g() {
13096         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13097
13098         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13099
13100         save_writethrough $p
13101
13102         set_cache read off
13103         set_cache writethrough on
13104         test_155_big_load
13105         restore_lustre_params < $p
13106         rm -f $p
13107 }
13108 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13109
13110 test_155h() {
13111         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13112
13113         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13114
13115         save_writethrough $p
13116
13117         set_cache read off
13118         set_cache writethrough off
13119         test_155_big_load
13120         restore_lustre_params < $p
13121         rm -f $p
13122 }
13123 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13124
13125 test_156() {
13126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13127         remote_ost_nodsh && skip "remote OST with nodsh"
13128         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13129                 skip "stats not implemented on old servers"
13130         [ "$ost1_FSTYPE" = "zfs" ] &&
13131                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13132
13133         local CPAGES=3
13134         local BEFORE
13135         local AFTER
13136         local file="$DIR/$tfile"
13137         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13138
13139         save_writethrough $p
13140         roc_hit_init
13141
13142         log "Turn on read and write cache"
13143         set_cache read on
13144         set_cache writethrough on
13145
13146         log "Write data and read it back."
13147         log "Read should be satisfied from the cache."
13148         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13149         BEFORE=$(roc_hit)
13150         cancel_lru_locks osc
13151         cat $file >/dev/null
13152         AFTER=$(roc_hit)
13153         if ! let "AFTER - BEFORE == CPAGES"; then
13154                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13155         else
13156                 log "cache hits:: before: $BEFORE, after: $AFTER"
13157         fi
13158
13159         log "Read again; it should be satisfied from the cache."
13160         BEFORE=$AFTER
13161         cancel_lru_locks osc
13162         cat $file >/dev/null
13163         AFTER=$(roc_hit)
13164         if ! let "AFTER - BEFORE == CPAGES"; then
13165                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13166         else
13167                 log "cache hits:: before: $BEFORE, after: $AFTER"
13168         fi
13169
13170         log "Turn off the read cache and turn on the write cache"
13171         set_cache read off
13172         set_cache writethrough on
13173
13174         log "Read again; it should be satisfied from the cache."
13175         BEFORE=$(roc_hit)
13176         cancel_lru_locks osc
13177         cat $file >/dev/null
13178         AFTER=$(roc_hit)
13179         if ! let "AFTER - BEFORE == CPAGES"; then
13180                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13181         else
13182                 log "cache hits:: before: $BEFORE, after: $AFTER"
13183         fi
13184
13185         log "Read again; it should not be satisfied from the cache."
13186         BEFORE=$AFTER
13187         cancel_lru_locks osc
13188         cat $file >/dev/null
13189         AFTER=$(roc_hit)
13190         if ! let "AFTER - BEFORE == 0"; then
13191                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13192         else
13193                 log "cache hits:: before: $BEFORE, after: $AFTER"
13194         fi
13195
13196         log "Write data and read it back."
13197         log "Read should be satisfied from the cache."
13198         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13199         BEFORE=$(roc_hit)
13200         cancel_lru_locks osc
13201         cat $file >/dev/null
13202         AFTER=$(roc_hit)
13203         if ! let "AFTER - BEFORE == CPAGES"; then
13204                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13205         else
13206                 log "cache hits:: before: $BEFORE, after: $AFTER"
13207         fi
13208
13209         log "Read again; it should not be satisfied from the cache."
13210         BEFORE=$AFTER
13211         cancel_lru_locks osc
13212         cat $file >/dev/null
13213         AFTER=$(roc_hit)
13214         if ! let "AFTER - BEFORE == 0"; then
13215                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13216         else
13217                 log "cache hits:: before: $BEFORE, after: $AFTER"
13218         fi
13219
13220         log "Turn off read and write cache"
13221         set_cache read off
13222         set_cache writethrough off
13223
13224         log "Write data and read it back"
13225         log "It should not be satisfied from the cache."
13226         rm -f $file
13227         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13228         cancel_lru_locks osc
13229         BEFORE=$(roc_hit)
13230         cat $file >/dev/null
13231         AFTER=$(roc_hit)
13232         if ! let "AFTER - BEFORE == 0"; then
13233                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13234         else
13235                 log "cache hits:: before: $BEFORE, after: $AFTER"
13236         fi
13237
13238         log "Turn on the read cache and turn off the write cache"
13239         set_cache read on
13240         set_cache writethrough off
13241
13242         log "Write data and read it back"
13243         log "It should not be satisfied from the cache."
13244         rm -f $file
13245         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13246         BEFORE=$(roc_hit)
13247         cancel_lru_locks osc
13248         cat $file >/dev/null
13249         AFTER=$(roc_hit)
13250         if ! let "AFTER - BEFORE == 0"; then
13251                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13252         else
13253                 log "cache hits:: before: $BEFORE, after: $AFTER"
13254         fi
13255
13256         log "Read again; it should be satisfied from the cache."
13257         BEFORE=$(roc_hit)
13258         cancel_lru_locks osc
13259         cat $file >/dev/null
13260         AFTER=$(roc_hit)
13261         if ! let "AFTER - BEFORE == CPAGES"; then
13262                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13263         else
13264                 log "cache hits:: before: $BEFORE, after: $AFTER"
13265         fi
13266
13267         restore_lustre_params < $p
13268         rm -f $p $file
13269 }
13270 run_test 156 "Verification of tunables"
13271
13272 test_160a() {
13273         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13274         remote_mds_nodsh && skip "remote MDS with nodsh"
13275         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13276                 skip "Need MDS version at least 2.2.0"
13277
13278         changelog_register || error "changelog_register failed"
13279         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13280         changelog_users $SINGLEMDS | grep -q $cl_user ||
13281                 error "User $cl_user not found in changelog_users"
13282
13283         # change something
13284         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13285         changelog_clear 0 || error "changelog_clear failed"
13286         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13287         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13288         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13289         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13290         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13291         rm $DIR/$tdir/pics/desktop.jpg
13292
13293         changelog_dump | tail -10
13294
13295         echo "verifying changelog mask"
13296         changelog_chmask "-MKDIR"
13297         changelog_chmask "-CLOSE"
13298
13299         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13300         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13301
13302         changelog_chmask "+MKDIR"
13303         changelog_chmask "+CLOSE"
13304
13305         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13306         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13307
13308         changelog_dump | tail -10
13309         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13310         CLOSES=$(changelog_dump | grep -c "CLOSE")
13311         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13312         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13313
13314         # verify contents
13315         echo "verifying target fid"
13316         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13317         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13318         [ "$fidc" == "$fidf" ] ||
13319                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13320         echo "verifying parent fid"
13321         # The FID returned from the Changelog may be the directory shard on
13322         # a different MDT, and not the FID returned by path2fid on the parent.
13323         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13324         # since this is what will matter when recreating this file in the tree.
13325         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13326         local pathp=$($LFS fid2path $MOUNT "$fidp")
13327         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13328                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13329
13330         echo "getting records for $cl_user"
13331         changelog_users $SINGLEMDS
13332         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13333         local nclr=3
13334         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13335                 error "changelog_clear failed"
13336         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13337         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13338         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13339                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13340
13341         local min0_rec=$(changelog_users $SINGLEMDS |
13342                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13343         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13344                           awk '{ print $1; exit; }')
13345
13346         changelog_dump | tail -n 5
13347         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13348         [ $first_rec == $((min0_rec + 1)) ] ||
13349                 error "first index should be $min0_rec + 1 not $first_rec"
13350
13351         # LU-3446 changelog index reset on MDT restart
13352         local cur_rec1=$(changelog_users $SINGLEMDS |
13353                          awk '/^current.index:/ { print $NF }')
13354         changelog_clear 0 ||
13355                 error "clear all changelog records for $cl_user failed"
13356         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13357         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13358                 error "Fail to start $SINGLEMDS"
13359         local cur_rec2=$(changelog_users $SINGLEMDS |
13360                          awk '/^current.index:/ { print $NF }')
13361         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13362         [ $cur_rec1 == $cur_rec2 ] ||
13363                 error "current index should be $cur_rec1 not $cur_rec2"
13364
13365         echo "verifying users from this test are deregistered"
13366         changelog_deregister || error "changelog_deregister failed"
13367         changelog_users $SINGLEMDS | grep -q $cl_user &&
13368                 error "User '$cl_user' still in changelog_users"
13369
13370         # lctl get_param -n mdd.*.changelog_users
13371         # current index: 144
13372         # ID    index (idle seconds)
13373         # cl3   144 (2)
13374         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13375                 # this is the normal case where all users were deregistered
13376                 # make sure no new records are added when no users are present
13377                 local last_rec1=$(changelog_users $SINGLEMDS |
13378                                   awk '/^current.index:/ { print $NF }')
13379                 touch $DIR/$tdir/chloe
13380                 local last_rec2=$(changelog_users $SINGLEMDS |
13381                                   awk '/^current.index:/ { print $NF }')
13382                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13383                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13384         else
13385                 # any changelog users must be leftovers from a previous test
13386                 changelog_users $SINGLEMDS
13387                 echo "other changelog users; can't verify off"
13388         fi
13389 }
13390 run_test 160a "changelog sanity"
13391
13392 test_160b() { # LU-3587
13393         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13394         remote_mds_nodsh && skip "remote MDS with nodsh"
13395         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13396                 skip "Need MDS version at least 2.2.0"
13397
13398         changelog_register || error "changelog_register failed"
13399         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13400         changelog_users $SINGLEMDS | grep -q $cl_user ||
13401                 error "User '$cl_user' not found in changelog_users"
13402
13403         local longname1=$(str_repeat a 255)
13404         local longname2=$(str_repeat b 255)
13405
13406         cd $DIR
13407         echo "creating very long named file"
13408         touch $longname1 || error "create of '$longname1' failed"
13409         echo "renaming very long named file"
13410         mv $longname1 $longname2
13411
13412         changelog_dump | grep RENME | tail -n 5
13413         rm -f $longname2
13414 }
13415 run_test 160b "Verify that very long rename doesn't crash in changelog"
13416
13417 test_160c() {
13418         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13419         remote_mds_nodsh && skip "remote MDS with nodsh"
13420
13421         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13422                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13423                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13424                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13425
13426         local rc=0
13427
13428         # Registration step
13429         changelog_register || error "changelog_register failed"
13430
13431         rm -rf $DIR/$tdir
13432         mkdir -p $DIR/$tdir
13433         $MCREATE $DIR/$tdir/foo_160c
13434         changelog_chmask "-TRUNC"
13435         $TRUNCATE $DIR/$tdir/foo_160c 200
13436         changelog_chmask "+TRUNC"
13437         $TRUNCATE $DIR/$tdir/foo_160c 199
13438         changelog_dump | tail -n 5
13439         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13440         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13441 }
13442 run_test 160c "verify that changelog log catch the truncate event"
13443
13444 test_160d() {
13445         remote_mds_nodsh && skip "remote MDS with nodsh"
13446         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13447         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13448         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13449                 skip "Need MDS version at least 2.7.60"
13450
13451         # Registration step
13452         changelog_register || error "changelog_register failed"
13453
13454         mkdir -p $DIR/$tdir/migrate_dir
13455         changelog_clear 0 || error "changelog_clear failed"
13456
13457         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13458         changelog_dump | tail -n 5
13459         local migrates=$(changelog_dump | grep -c "MIGRT")
13460         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13461 }
13462 run_test 160d "verify that changelog log catch the migrate event"
13463
13464 test_160e() {
13465         remote_mds_nodsh && skip "remote MDS with nodsh"
13466
13467         # Create a user
13468         changelog_register || error "changelog_register failed"
13469
13470         # Delete a future user (expect fail)
13471         local MDT0=$(facet_svc $SINGLEMDS)
13472         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13473         local rc=$?
13474
13475         if [ $rc -eq 0 ]; then
13476                 error "Deleted non-existant user cl77"
13477         elif [ $rc -ne 2 ]; then
13478                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13479         fi
13480
13481         # Clear to a bad index (1 billion should be safe)
13482         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13483         rc=$?
13484
13485         if [ $rc -eq 0 ]; then
13486                 error "Successfully cleared to invalid CL index"
13487         elif [ $rc -ne 22 ]; then
13488                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13489         fi
13490 }
13491 run_test 160e "changelog negative testing (should return errors)"
13492
13493 test_160f() {
13494         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13495         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13496                 skip "Need MDS version at least 2.10.56"
13497
13498         local mdts=$(comma_list $(mdts_nodes))
13499
13500         # Create a user
13501         changelog_register || error "first changelog_register failed"
13502         changelog_register || error "second changelog_register failed"
13503         local cl_users
13504         declare -A cl_user1
13505         declare -A cl_user2
13506         local user_rec1
13507         local user_rec2
13508         local i
13509
13510         # generate some changelog records to accumulate on each MDT
13511         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13512         log "$(date +%s): creating first files"
13513         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13514                 error "create $DIR/$tdir/$tfile failed"
13515
13516         # check changelogs have been generated
13517         local start=$SECONDS
13518         local idle_time=$((MDSCOUNT * 5 + 5))
13519         local nbcl=$(changelog_dump | wc -l)
13520         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13521
13522         for param in "changelog_max_idle_time=$idle_time" \
13523                      "changelog_gc=1" \
13524                      "changelog_min_gc_interval=2" \
13525                      "changelog_min_free_cat_entries=3"; do
13526                 local MDT0=$(facet_svc $SINGLEMDS)
13527                 local var="${param%=*}"
13528                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13529
13530                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13531                 do_nodes $mdts $LCTL set_param mdd.*.$param
13532         done
13533
13534         # force cl_user2 to be idle (1st part), but also cancel the
13535         # cl_user1 records so that it is not evicted later in the test.
13536         local sleep1=$((idle_time / 2))
13537         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
13538         sleep $sleep1
13539
13540         # simulate changelog catalog almost full
13541         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13542         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13543
13544         for i in $(seq $MDSCOUNT); do
13545                 cl_users=(${CL_USERS[mds$i]})
13546                 cl_user1[mds$i]="${cl_users[0]}"
13547                 cl_user2[mds$i]="${cl_users[1]}"
13548
13549                 [ -n "${cl_user1[mds$i]}" ] ||
13550                         error "mds$i: no user registered"
13551                 [ -n "${cl_user2[mds$i]}" ] ||
13552                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13553
13554                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13555                 [ -n "$user_rec1" ] ||
13556                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13557                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13558                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13559                 [ -n "$user_rec2" ] ||
13560                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13561                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13562                      "$user_rec1 + 2 == $user_rec2"
13563                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13564                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13565                               "$user_rec1 + 2, but is $user_rec2"
13566                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13567                 [ -n "$user_rec2" ] ||
13568                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13569                 [ $user_rec1 == $user_rec2 ] ||
13570                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13571                               "$user_rec1, but is $user_rec2"
13572         done
13573
13574         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
13575         local sleep2=$((idle_time - (SECONDS - start) + 1))
13576         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
13577         sleep $sleep2
13578
13579         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
13580         # cl_user1 should be OK because it recently processed records.
13581         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
13582         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
13583                 error "create $DIR/$tdir/${tfile}b failed"
13584
13585         # ensure gc thread is done
13586         for i in $(mdts_nodes); do
13587                 wait_update $i \
13588                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13589                         error "$i: GC-thread not done"
13590         done
13591
13592         local first_rec
13593         for i in $(seq $MDSCOUNT); do
13594                 # check cl_user1 still registered
13595                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13596                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13597                 # check cl_user2 unregistered
13598                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13599                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13600
13601                 # check changelogs are present and starting at $user_rec1 + 1
13602                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13603                 [ -n "$user_rec1" ] ||
13604                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13605                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13606                             awk '{ print $1; exit; }')
13607
13608                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13609                 [ $((user_rec1 + 1)) == $first_rec ] ||
13610                         error "mds$i: first index should be $user_rec1 + 1, " \
13611                               "but is $first_rec"
13612         done
13613 }
13614 run_test 160f "changelog garbage collect (timestamped users)"
13615
13616 test_160g() {
13617         remote_mds_nodsh && skip "remote MDS with nodsh"
13618         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13619                 skip "Need MDS version at least 2.10.56"
13620
13621         local mdts=$(comma_list $(mdts_nodes))
13622
13623         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
13624         do_nodes $mdts $LCTL set_param fail_loc=0x1314
13625
13626         # Create a user
13627         changelog_register || error "first changelog_register failed"
13628         changelog_register || error "second changelog_register failed"
13629         local cl_users
13630         declare -A cl_user1
13631         declare -A cl_user2
13632         local user_rec1
13633         local user_rec2
13634         local i
13635
13636         # generate some changelog records to accumulate on each MDT
13637         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13638         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13639                 error "create $DIR/$tdir/$tfile failed"
13640
13641         # check changelogs have been generated
13642         local nbcl=$(changelog_dump | wc -l)
13643         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13644
13645         # reduce the max_idle_indexes value to make sure we exceed it
13646         max_ndx=$((nbcl / 2 - 1))
13647
13648         for param in "changelog_max_idle_indexes=$max_ndx" \
13649                      "changelog_gc=1" \
13650                      "changelog_min_gc_interval=2" \
13651                      "changelog_min_free_cat_entries=3"; do
13652                 local MDT0=$(facet_svc $SINGLEMDS)
13653                 local var="${param%=*}"
13654                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13655
13656                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13657                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
13658                         error "unable to set mdd.*.$param"
13659         done
13660
13661         # simulate changelog catalog almost full
13662         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13663         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13664
13665         for i in $(seq $MDSCOUNT); do
13666                 cl_users=(${CL_USERS[mds$i]})
13667                 cl_user1[mds$i]="${cl_users[0]}"
13668                 cl_user2[mds$i]="${cl_users[1]}"
13669
13670                 [ -n "${cl_user1[mds$i]}" ] ||
13671                         error "mds$i: no user registered"
13672                 [ -n "${cl_user2[mds$i]}" ] ||
13673                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13674
13675                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13676                 [ -n "$user_rec1" ] ||
13677                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13678                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13679                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13680                 [ -n "$user_rec2" ] ||
13681                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13682                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13683                      "$user_rec1 + 2 == $user_rec2"
13684                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13685                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13686                               "$user_rec1 + 2, but is $user_rec2"
13687                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13688                 [ -n "$user_rec2" ] ||
13689                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13690                 [ $user_rec1 == $user_rec2 ] ||
13691                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13692                               "$user_rec1, but is $user_rec2"
13693         done
13694
13695         # ensure we are past the previous changelog_min_gc_interval set above
13696         sleep 2
13697
13698         # generate one more changelog to trigger fail_loc
13699         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13700                 error "create $DIR/$tdir/${tfile}bis failed"
13701
13702         # ensure gc thread is done
13703         for i in $(mdts_nodes); do
13704                 wait_update $i \
13705                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13706                         error "$i: GC-thread not done"
13707         done
13708
13709         local first_rec
13710         for i in $(seq $MDSCOUNT); do
13711                 # check cl_user1 still registered
13712                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13713                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13714                 # check cl_user2 unregistered
13715                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13716                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13717
13718                 # check changelogs are present and starting at $user_rec1 + 1
13719                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13720                 [ -n "$user_rec1" ] ||
13721                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13722                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13723                             awk '{ print $1; exit; }')
13724
13725                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13726                 [ $((user_rec1 + 1)) == $first_rec ] ||
13727                         error "mds$i: first index should be $user_rec1 + 1, " \
13728                               "but is $first_rec"
13729         done
13730 }
13731 run_test 160g "changelog garbage collect (old users)"
13732
13733 test_160h() {
13734         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13735         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13736                 skip "Need MDS version at least 2.10.56"
13737
13738         local mdts=$(comma_list $(mdts_nodes))
13739
13740         # Create a user
13741         changelog_register || error "first changelog_register failed"
13742         changelog_register || error "second changelog_register failed"
13743         local cl_users
13744         declare -A cl_user1
13745         declare -A cl_user2
13746         local user_rec1
13747         local user_rec2
13748         local i
13749
13750         # generate some changelog records to accumulate on each MDT
13751         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13752         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13753                 error "create $DIR/$tdir/$tfile failed"
13754
13755         # check changelogs have been generated
13756         local nbcl=$(changelog_dump | wc -l)
13757         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13758
13759         for param in "changelog_max_idle_time=10" \
13760                      "changelog_gc=1" \
13761                      "changelog_min_gc_interval=2"; do
13762                 local MDT0=$(facet_svc $SINGLEMDS)
13763                 local var="${param%=*}"
13764                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13765
13766                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13767                 do_nodes $mdts $LCTL set_param mdd.*.$param
13768         done
13769
13770         # force cl_user2 to be idle (1st part)
13771         sleep 9
13772
13773         for i in $(seq $MDSCOUNT); do
13774                 cl_users=(${CL_USERS[mds$i]})
13775                 cl_user1[mds$i]="${cl_users[0]}"
13776                 cl_user2[mds$i]="${cl_users[1]}"
13777
13778                 [ -n "${cl_user1[mds$i]}" ] ||
13779                         error "mds$i: no user registered"
13780                 [ -n "${cl_user2[mds$i]}" ] ||
13781                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13782
13783                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13784                 [ -n "$user_rec1" ] ||
13785                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13786                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13787                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13788                 [ -n "$user_rec2" ] ||
13789                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13790                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13791                      "$user_rec1 + 2 == $user_rec2"
13792                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13793                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13794                               "$user_rec1 + 2, but is $user_rec2"
13795                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13796                 [ -n "$user_rec2" ] ||
13797                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13798                 [ $user_rec1 == $user_rec2 ] ||
13799                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13800                               "$user_rec1, but is $user_rec2"
13801         done
13802
13803         # force cl_user2 to be idle (2nd part) and to reach
13804         # changelog_max_idle_time
13805         sleep 2
13806
13807         # force each GC-thread start and block then
13808         # one per MDT/MDD, set fail_val accordingly
13809         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
13810         do_nodes $mdts $LCTL set_param fail_loc=0x1316
13811
13812         # generate more changelogs to trigger fail_loc
13813         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13814                 error "create $DIR/$tdir/${tfile}bis failed"
13815
13816         # stop MDT to stop GC-thread, should be done in back-ground as it will
13817         # block waiting for the thread to be released and exit
13818         declare -A stop_pids
13819         for i in $(seq $MDSCOUNT); do
13820                 stop mds$i &
13821                 stop_pids[mds$i]=$!
13822         done
13823
13824         for i in $(mdts_nodes); do
13825                 local facet
13826                 local nb=0
13827                 local facets=$(facets_up_on_host $i)
13828
13829                 for facet in ${facets//,/ }; do
13830                         if [[ $facet == mds* ]]; then
13831                                 nb=$((nb + 1))
13832                         fi
13833                 done
13834                 # ensure each MDS's gc threads are still present and all in "R"
13835                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
13836                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
13837                         error "$i: expected $nb GC-thread"
13838                 wait_update $i \
13839                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
13840                         "R" 20 ||
13841                         error "$i: GC-thread not found in R-state"
13842                 # check umounts of each MDT on MDS have reached kthread_stop()
13843                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
13844                         error "$i: expected $nb umount"
13845                 wait_update $i \
13846                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
13847                         error "$i: umount not found in D-state"
13848         done
13849
13850         # release all GC-threads
13851         do_nodes $mdts $LCTL set_param fail_loc=0
13852
13853         # wait for MDT stop to complete
13854         for i in $(seq $MDSCOUNT); do
13855                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
13856         done
13857
13858         # XXX
13859         # may try to check if any orphan changelog records are present
13860         # via ldiskfs/zfs and llog_reader...
13861
13862         # re-start/mount MDTs
13863         for i in $(seq $MDSCOUNT); do
13864                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
13865                         error "Fail to start mds$i"
13866         done
13867
13868         local first_rec
13869         for i in $(seq $MDSCOUNT); do
13870                 # check cl_user1 still registered
13871                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13872                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13873                 # check cl_user2 unregistered
13874                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13875                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13876
13877                 # check changelogs are present and starting at $user_rec1 + 1
13878                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13879                 [ -n "$user_rec1" ] ||
13880                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13881                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13882                             awk '{ print $1; exit; }')
13883
13884                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13885                 [ $((user_rec1 + 1)) == $first_rec ] ||
13886                         error "mds$i: first index should be $user_rec1 + 1, " \
13887                               "but is $first_rec"
13888         done
13889 }
13890 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
13891               "during mount"
13892
13893 test_160i() {
13894
13895         local mdts=$(comma_list $(mdts_nodes))
13896
13897         changelog_register || error "first changelog_register failed"
13898
13899         # generate some changelog records to accumulate on each MDT
13900         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13901         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13902                 error "create $DIR/$tdir/$tfile failed"
13903
13904         # check changelogs have been generated
13905         local nbcl=$(changelog_dump | wc -l)
13906         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13907
13908         # simulate race between register and unregister
13909         # XXX as fail_loc is set per-MDS, with DNE configs the race
13910         # simulation will only occur for one MDT per MDS and for the
13911         # others the normal race scenario will take place
13912         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
13913         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
13914         do_nodes $mdts $LCTL set_param fail_val=1
13915
13916         # unregister 1st user
13917         changelog_deregister &
13918         local pid1=$!
13919         # wait some time for deregister work to reach race rdv
13920         sleep 2
13921         # register 2nd user
13922         changelog_register || error "2nd user register failed"
13923
13924         wait $pid1 || error "1st user deregister failed"
13925
13926         local i
13927         local last_rec
13928         declare -A LAST_REC
13929         for i in $(seq $MDSCOUNT); do
13930                 if changelog_users mds$i | grep "^cl"; then
13931                         # make sure new records are added with one user present
13932                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
13933                                           awk '/^current.index:/ { print $NF }')
13934                 else
13935                         error "mds$i has no user registered"
13936                 fi
13937         done
13938
13939         # generate more changelog records to accumulate on each MDT
13940         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13941                 error "create $DIR/$tdir/${tfile}bis failed"
13942
13943         for i in $(seq $MDSCOUNT); do
13944                 last_rec=$(changelog_users $SINGLEMDS |
13945                            awk '/^current.index:/ { print $NF }')
13946                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
13947                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
13948                         error "changelogs are off on mds$i"
13949         done
13950 }
13951 run_test 160i "changelog user register/unregister race"
13952
13953 test_160k() {
13954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13955         remote_mds_nodsh && skip "remote MDS with nodsh"
13956
13957         mkdir -p $DIR/$tdir/1/1
13958
13959         changelog_register || error "changelog_register failed"
13960         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13961
13962         changelog_users $SINGLEMDS | grep -q $cl_user ||
13963                 error "User '$cl_user' not found in changelog_users"
13964 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
13965         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
13966         rmdir $DIR/$tdir/1/1 & sleep 1
13967         mkdir $DIR/$tdir/2
13968         touch $DIR/$tdir/2/2
13969         rm -rf $DIR/$tdir/2
13970
13971         wait
13972         sleep 4
13973
13974         changelog_dump | grep rmdir || error "rmdir not recorded"
13975
13976         rm -rf $DIR/$tdir
13977         changelog_deregister
13978 }
13979 run_test 160k "Verify that changelog records are not lost"
13980
13981 test_160j() {
13982         remote_mds_nodsh && skip "remote MDS with nodsh"
13983         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
13984                 skip "Need MDS version at least 2.12.56"
13985
13986         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
13987
13988         changelog_register || error "first changelog_register failed"
13989
13990         # generate some changelog
13991         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13992         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13993                 error "create $DIR/$tdir/${tfile}bis failed"
13994
13995         # open the changelog device
13996         exec 3>/dev/changelog-$FSNAME-MDT0000
13997         exec 4</dev/changelog-$FSNAME-MDT0000
13998
13999         # umount the first lustre mount
14000         umount $MOUNT
14001
14002         # read changelog
14003         cat <&4 >/dev/null || error "read changelog failed"
14004
14005         # clear changelog
14006         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14007         changelog_users $SINGLEMDS | grep -q $cl_user ||
14008                 error "User $cl_user not found in changelog_users"
14009
14010         printf 'clear:'$cl_user':0' >&3
14011
14012         # close
14013         exec 3>&-
14014         exec 4<&-
14015
14016         # cleanup
14017         changelog_deregister || error "changelog_deregister failed"
14018
14019         umount $MOUNT2
14020         mount_client $MOUNT || error "mount_client on $MOUNT failed"
14021 }
14022 run_test 160j "client can be umounted  while its chanangelog is being used"
14023
14024 test_160m() {
14025         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14026         [[ $MDS1_VERSION -ge $(version_code 2.12.7) ]] ||
14027                 skip "Need MDS version at least 2.12.7"
14028         local cl_users
14029         local cl_user1
14030         local cl_user2
14031         local pid1
14032
14033         # Create a user
14034         changelog_register || error "first changelog_register failed"
14035         changelog_register || error "second changelog_register failed"
14036
14037         cl_users=(${CL_USERS[mds1]})
14038         cl_user1="${cl_users[0]}"
14039         cl_user2="${cl_users[1]}"
14040         # generate some changelog records to accumulate on MDT0
14041         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
14042         createmany -m $DIR/$tdir/$tfile 50 ||
14043                 error "create $DIR/$tdir/$tfile failed"
14044         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
14045         rm -f $DIR/$tdir
14046
14047         # check changelogs have been generated
14048         local nbcl=$(changelog_dump | wc -l)
14049         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14050
14051 #define OBD_FAIL_MDS_CHANGELOG_RACE      0x15f
14052         do_facet mds1 $LCTL set_param fail_loc=0x8000015f fail_val=0
14053
14054         __changelog_clear mds1 $cl_user1 +10
14055         __changelog_clear mds1 $cl_user2 0 &
14056         pid1=$!
14057         sleep 2
14058         __changelog_clear mds1 $cl_user1 0 ||
14059                 error "fail to cancel record for $cl_user1"
14060         wait $pid1
14061         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
14062 }
14063 run_test 160m "Changelog clear race"
14064
14065 test_160n() {
14066         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14067         [[ $MDS1_VERSION -ge $(version_code 2.12.7) ]] ||
14068                 skip "Need MDS version at least 2.12.7"
14069         local cl_users
14070         local cl_user1
14071         local cl_user2
14072         local pid1
14073         local first_rec
14074         local last_rec=0
14075
14076         # Create a user
14077         changelog_register || error "first changelog_register failed"
14078
14079         cl_users=(${CL_USERS[mds1]})
14080         cl_user1="${cl_users[0]}"
14081
14082         # generate some changelog records to accumulate on MDT0
14083         test_mkdir -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
14084         first_rec=$(changelog_users $SINGLEMDS |
14085                         awk '/^current.index:/ { print $NF }')
14086         while (( last_rec < (( first_rec + 65000)) )); do
14087                 createmany -m $DIR/$tdir/$tfile 10000 ||
14088                         error "create $DIR/$tdir/$tfile failed"
14089
14090                 for i in $(seq 0 10000); do
14091                         mrename $DIR/$tdir/$tfile$i $DIR/$tdir/$tfile-new$i \
14092                                 > /dev/null
14093                 done
14094
14095                 unlinkmany $DIR/$tdir/$tfile-new 10000 ||
14096                         error "unlinkmany failed unlink"
14097                 last_rec=$(changelog_users $SINGLEMDS |
14098                         awk '/^current.index:/ { print $NF }')
14099                 echo last record $last_rec
14100                 (( last_rec == 0 )) && error "no changelog found"
14101         done
14102
14103 #define OBD_FAIL_MDS_CHANGELOG_DEL       0x16c
14104         do_facet mds1 $LCTL set_param fail_loc=0x8000016c fail_val=0
14105
14106         __changelog_clear mds1 $cl_user1 0 &
14107         pid1=$!
14108         sleep 2
14109         __changelog_clear mds1 $cl_user1 0 ||
14110                 error "fail to cancel record for $cl_user1"
14111         wait $pid1
14112         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
14113 }
14114 run_test 160n "Changelog destroy race"
14115
14116 test_161a() {
14117         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14118
14119         test_mkdir -c1 $DIR/$tdir
14120         cp /etc/hosts $DIR/$tdir/$tfile
14121         test_mkdir -c1 $DIR/$tdir/foo1
14122         test_mkdir -c1 $DIR/$tdir/foo2
14123         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14124         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14125         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14126         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14127         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14128         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14129                 $LFS fid2path $DIR $FID
14130                 error "bad link ea"
14131         fi
14132         # middle
14133         rm $DIR/$tdir/foo2/zachary
14134         # last
14135         rm $DIR/$tdir/foo2/thor
14136         # first
14137         rm $DIR/$tdir/$tfile
14138         # rename
14139         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14140         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14141                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14142         rm $DIR/$tdir/foo2/maggie
14143
14144         # overflow the EA
14145         local longname=$tfile.avg_len_is_thirty_two_
14146         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14147                 error_noexit 'failed to unlink many hardlinks'" EXIT
14148         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14149                 error "failed to hardlink many files"
14150         links=$($LFS fid2path $DIR $FID | wc -l)
14151         echo -n "${links}/1000 links in link EA"
14152         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14153 }
14154 run_test 161a "link ea sanity"
14155
14156 test_161b() {
14157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14158         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14159
14160         local MDTIDX=1
14161         local remote_dir=$DIR/$tdir/remote_dir
14162
14163         mkdir -p $DIR/$tdir
14164         $LFS mkdir -i $MDTIDX $remote_dir ||
14165                 error "create remote directory failed"
14166
14167         cp /etc/hosts $remote_dir/$tfile
14168         mkdir -p $remote_dir/foo1
14169         mkdir -p $remote_dir/foo2
14170         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14171         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14172         ln $remote_dir/$tfile $remote_dir/foo1/luna
14173         ln $remote_dir/$tfile $remote_dir/foo2/thor
14174
14175         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14176                      tr -d ']')
14177         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14178                 $LFS fid2path $DIR $FID
14179                 error "bad link ea"
14180         fi
14181         # middle
14182         rm $remote_dir/foo2/zachary
14183         # last
14184         rm $remote_dir/foo2/thor
14185         # first
14186         rm $remote_dir/$tfile
14187         # rename
14188         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14189         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14190         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14191                 $LFS fid2path $DIR $FID
14192                 error "bad link rename"
14193         fi
14194         rm $remote_dir/foo2/maggie
14195
14196         # overflow the EA
14197         local longname=filename_avg_len_is_thirty_two_
14198         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14199                 error "failed to hardlink many files"
14200         links=$($LFS fid2path $DIR $FID | wc -l)
14201         echo -n "${links}/1000 links in link EA"
14202         [[ ${links} -gt 60 ]] ||
14203                 error "expected at least 60 links in link EA"
14204         unlinkmany $remote_dir/foo2/$longname 1000 ||
14205         error "failed to unlink many hardlinks"
14206 }
14207 run_test 161b "link ea sanity under remote directory"
14208
14209 test_161c() {
14210         remote_mds_nodsh && skip "remote MDS with nodsh"
14211         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14212         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14213                 skip "Need MDS version at least 2.1.5"
14214
14215         # define CLF_RENAME_LAST 0x0001
14216         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14217         changelog_register || error "changelog_register failed"
14218
14219         rm -rf $DIR/$tdir
14220         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14221         touch $DIR/$tdir/foo_161c
14222         touch $DIR/$tdir/bar_161c
14223         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14224         changelog_dump | grep RENME | tail -n 5
14225         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14226         changelog_clear 0 || error "changelog_clear failed"
14227         if [ x$flags != "x0x1" ]; then
14228                 error "flag $flags is not 0x1"
14229         fi
14230
14231         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14232         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14233         touch $DIR/$tdir/foo_161c
14234         touch $DIR/$tdir/bar_161c
14235         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14236         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14237         changelog_dump | grep RENME | tail -n 5
14238         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14239         changelog_clear 0 || error "changelog_clear failed"
14240         if [ x$flags != "x0x0" ]; then
14241                 error "flag $flags is not 0x0"
14242         fi
14243         echo "rename overwrite a target having nlink > 1," \
14244                 "changelog record has flags of $flags"
14245
14246         # rename doesn't overwrite a target (changelog flag 0x0)
14247         touch $DIR/$tdir/foo_161c
14248         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14249         changelog_dump | grep RENME | tail -n 5
14250         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14251         changelog_clear 0 || error "changelog_clear failed"
14252         if [ x$flags != "x0x0" ]; then
14253                 error "flag $flags is not 0x0"
14254         fi
14255         echo "rename doesn't overwrite a target," \
14256                 "changelog record has flags of $flags"
14257
14258         # define CLF_UNLINK_LAST 0x0001
14259         # unlink a file having nlink = 1 (changelog flag 0x1)
14260         rm -f $DIR/$tdir/foo2_161c
14261         changelog_dump | grep UNLNK | tail -n 5
14262         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14263         changelog_clear 0 || error "changelog_clear failed"
14264         if [ x$flags != "x0x1" ]; then
14265                 error "flag $flags is not 0x1"
14266         fi
14267         echo "unlink a file having nlink = 1," \
14268                 "changelog record has flags of $flags"
14269
14270         # unlink a file having nlink > 1 (changelog flag 0x0)
14271         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14272         rm -f $DIR/$tdir/foobar_161c
14273         changelog_dump | grep UNLNK | tail -n 5
14274         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14275         changelog_clear 0 || error "changelog_clear failed"
14276         if [ x$flags != "x0x0" ]; then
14277                 error "flag $flags is not 0x0"
14278         fi
14279         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14280 }
14281 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14282
14283 test_161d() {
14284         remote_mds_nodsh && skip "remote MDS with nodsh"
14285
14286         local pid
14287         local fid
14288
14289         changelog_register || error "changelog_register failed"
14290
14291         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14292         # interfer with $MOUNT/.lustre/fid/ access
14293         mkdir $DIR/$tdir
14294         [[ $? -eq 0 ]] || error "mkdir failed"
14295
14296         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14297         $LCTL set_param fail_loc=0x8000140c
14298         # 5s pause
14299         $LCTL set_param fail_val=5
14300
14301         # create file
14302         echo foofoo > $DIR/$tdir/$tfile &
14303         pid=$!
14304
14305         # wait for create to be delayed
14306         sleep 2
14307
14308         ps -p $pid
14309         [[ $? -eq 0 ]] || error "create should be blocked"
14310
14311         local tempfile=$(mktemp)
14312         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14313         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14314         # some delay may occur during ChangeLog publishing and file read just
14315         # above, that could allow file write to happen finally
14316         [[ -s $tempfile ]] && echo "file should be empty"
14317
14318         $LCTL set_param fail_loc=0
14319
14320         wait $pid
14321         [[ $? -eq 0 ]] || error "create failed"
14322 }
14323 run_test 161d "create with concurrent .lustre/fid access"
14324
14325 check_path() {
14326         local expected="$1"
14327         shift
14328         local fid="$2"
14329
14330         local path
14331         path=$($LFS fid2path "$@")
14332         local rc=$?
14333
14334         if [ $rc -ne 0 ]; then
14335                 error "path looked up of '$expected' failed: rc=$rc"
14336         elif [ "$path" != "$expected" ]; then
14337                 error "path looked up '$path' instead of '$expected'"
14338         else
14339                 echo "FID '$fid' resolves to path '$path' as expected"
14340         fi
14341 }
14342
14343 test_162a() { # was test_162
14344         test_mkdir -p -c1 $DIR/$tdir/d2
14345         touch $DIR/$tdir/d2/$tfile
14346         touch $DIR/$tdir/d2/x1
14347         touch $DIR/$tdir/d2/x2
14348         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14349         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14350         # regular file
14351         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14352         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14353
14354         # softlink
14355         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14356         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14357         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14358
14359         # softlink to wrong file
14360         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14361         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14362         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14363
14364         # hardlink
14365         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14366         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14367         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14368         # fid2path dir/fsname should both work
14369         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14370         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14371
14372         # hardlink count: check that there are 2 links
14373         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14374         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14375
14376         # hardlink indexing: remove the first link
14377         rm $DIR/$tdir/d2/p/q/r/hlink
14378         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14379 }
14380 run_test 162a "path lookup sanity"
14381
14382 test_162b() {
14383         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14384         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14385
14386         mkdir $DIR/$tdir
14387         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14388                                 error "create striped dir failed"
14389
14390         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14391                                         tail -n 1 | awk '{print $2}')
14392         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14393
14394         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14395         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14396
14397         # regular file
14398         for ((i=0;i<5;i++)); do
14399                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14400                         error "get fid for f$i failed"
14401                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14402
14403                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14404                         error "get fid for d$i failed"
14405                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14406         done
14407
14408         return 0
14409 }
14410 run_test 162b "striped directory path lookup sanity"
14411
14412 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14413 test_162c() {
14414         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14415                 skip "Need MDS version at least 2.7.51"
14416
14417         local lpath=$tdir.local
14418         local rpath=$tdir.remote
14419
14420         test_mkdir $DIR/$lpath
14421         test_mkdir $DIR/$rpath
14422
14423         for ((i = 0; i <= 101; i++)); do
14424                 lpath="$lpath/$i"
14425                 mkdir $DIR/$lpath
14426                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14427                         error "get fid for local directory $DIR/$lpath failed"
14428                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14429
14430                 rpath="$rpath/$i"
14431                 test_mkdir $DIR/$rpath
14432                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14433                         error "get fid for remote directory $DIR/$rpath failed"
14434                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14435         done
14436
14437         return 0
14438 }
14439 run_test 162c "fid2path works with paths 100 or more directories deep"
14440
14441 test_169() {
14442         # do directio so as not to populate the page cache
14443         log "creating a 10 Mb file"
14444         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14445         log "starting reads"
14446         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14447         log "truncating the file"
14448         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14449         log "killing dd"
14450         kill %+ || true # reads might have finished
14451         echo "wait until dd is finished"
14452         wait
14453         log "removing the temporary file"
14454         rm -rf $DIR/$tfile || error "tmp file removal failed"
14455 }
14456 run_test 169 "parallel read and truncate should not deadlock"
14457
14458 test_170() {
14459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14460
14461         $LCTL clear     # bug 18514
14462         $LCTL debug_daemon start $TMP/${tfile}_log_good
14463         touch $DIR/$tfile
14464         $LCTL debug_daemon stop
14465         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14466                 error "sed failed to read log_good"
14467
14468         $LCTL debug_daemon start $TMP/${tfile}_log_good
14469         rm -rf $DIR/$tfile
14470         $LCTL debug_daemon stop
14471
14472         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14473                error "lctl df log_bad failed"
14474
14475         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14476         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14477
14478         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14479         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14480
14481         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14482                 error "bad_line good_line1 good_line2 are empty"
14483
14484         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14485         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14486         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14487
14488         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14489         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14490         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14491
14492         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14493                 error "bad_line_new good_line_new are empty"
14494
14495         local expected_good=$((good_line1 + good_line2*2))
14496
14497         rm -f $TMP/${tfile}*
14498         # LU-231, short malformed line may not be counted into bad lines
14499         if [ $bad_line -ne $bad_line_new ] &&
14500                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14501                 error "expected $bad_line bad lines, but got $bad_line_new"
14502                 return 1
14503         fi
14504
14505         if [ $expected_good -ne $good_line_new ]; then
14506                 error "expected $expected_good good lines, but got $good_line_new"
14507                 return 2
14508         fi
14509         true
14510 }
14511 run_test 170 "test lctl df to handle corrupted log ====================="
14512
14513 test_171() { # bug20592
14514         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14515
14516         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14517         $LCTL set_param fail_loc=0x50e
14518         $LCTL set_param fail_val=3000
14519         multiop_bg_pause $DIR/$tfile O_s || true
14520         local MULTIPID=$!
14521         kill -USR1 $MULTIPID
14522         # cause log dump
14523         sleep 3
14524         wait $MULTIPID
14525         if dmesg | grep "recursive fault"; then
14526                 error "caught a recursive fault"
14527         fi
14528         $LCTL set_param fail_loc=0
14529         true
14530 }
14531 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14532
14533 # it would be good to share it with obdfilter-survey/iokit-libecho code
14534 setup_obdecho_osc () {
14535         local rc=0
14536         local ost_nid=$1
14537         local obdfilter_name=$2
14538         echo "Creating new osc for $obdfilter_name on $ost_nid"
14539         # make sure we can find loopback nid
14540         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14541
14542         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14543                            ${obdfilter_name}_osc_UUID || rc=2; }
14544         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14545                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14546         return $rc
14547 }
14548
14549 cleanup_obdecho_osc () {
14550         local obdfilter_name=$1
14551         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14552         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14553         return 0
14554 }
14555
14556 obdecho_test() {
14557         local OBD=$1
14558         local node=$2
14559         local pages=${3:-64}
14560         local rc=0
14561         local id
14562
14563         local count=10
14564         local obd_size=$(get_obd_size $node $OBD)
14565         local page_size=$(get_page_size $node)
14566         if [[ -n "$obd_size" ]]; then
14567                 local new_count=$((obd_size / (pages * page_size / 1024)))
14568                 [[ $new_count -ge $count ]] || count=$new_count
14569         fi
14570
14571         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14572         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14573                            rc=2; }
14574         if [ $rc -eq 0 ]; then
14575             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14576             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14577         fi
14578         echo "New object id is $id"
14579         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14580                            rc=4; }
14581         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14582                            "test_brw $count w v $pages $id" || rc=4; }
14583         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14584                            rc=4; }
14585         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
14586                                         "cleanup" || rc=5; }
14587         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
14588                                         "detach" || rc=6; }
14589         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14590         return $rc
14591 }
14592
14593 test_180a() {
14594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14595
14596         if ! module_loaded obdecho; then
14597                 load_module obdecho/obdecho &&
14598                         stack_trap "rmmod obdecho" EXIT ||
14599                         error "unable to load obdecho on client"
14600         fi
14601
14602         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14603         local host=$($LCTL get_param -n osc.$osc.import |
14604                      awk '/current_connection:/ { print $2 }' )
14605         local target=$($LCTL get_param -n osc.$osc.import |
14606                        awk '/target:/ { print $2 }' )
14607         target=${target%_UUID}
14608
14609         if [ -n "$target" ]; then
14610                 setup_obdecho_osc $host $target &&
14611                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14612                         { error "obdecho setup failed with $?"; return; }
14613
14614                 obdecho_test ${target}_osc client ||
14615                         error "obdecho_test failed on ${target}_osc"
14616         else
14617                 $LCTL get_param osc.$osc.import
14618                 error "there is no osc.$osc.import target"
14619         fi
14620 }
14621 run_test 180a "test obdecho on osc"
14622
14623 test_180b() {
14624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14625         remote_ost_nodsh && skip "remote OST with nodsh"
14626
14627         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14628                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14629                 error "failed to load module obdecho"
14630
14631         local target=$(do_facet ost1 $LCTL dl |
14632                        awk '/obdfilter/ { print $4; exit; }')
14633
14634         if [ -n "$target" ]; then
14635                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14636         else
14637                 do_facet ost1 $LCTL dl
14638                 error "there is no obdfilter target on ost1"
14639         fi
14640 }
14641 run_test 180b "test obdecho directly on obdfilter"
14642
14643 test_180c() { # LU-2598
14644         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14645         remote_ost_nodsh && skip "remote OST with nodsh"
14646         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14647                 skip "Need MDS version at least 2.4.0"
14648
14649         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14650                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14651                 error "failed to load module obdecho"
14652
14653         local target=$(do_facet ost1 $LCTL dl |
14654                        awk '/obdfilter/ { print $4; exit; }')
14655
14656         if [ -n "$target" ]; then
14657                 local pages=16384 # 64MB bulk I/O RPC size
14658
14659                 obdecho_test "$target" ost1 "$pages" ||
14660                         error "obdecho_test with pages=$pages failed with $?"
14661         else
14662                 do_facet ost1 $LCTL dl
14663                 error "there is no obdfilter target on ost1"
14664         fi
14665 }
14666 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14667
14668 test_181() { # bug 22177
14669         test_mkdir $DIR/$tdir
14670         # create enough files to index the directory
14671         createmany -o $DIR/$tdir/foobar 4000
14672         # print attributes for debug purpose
14673         lsattr -d .
14674         # open dir
14675         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14676         MULTIPID=$!
14677         # remove the files & current working dir
14678         unlinkmany $DIR/$tdir/foobar 4000
14679         rmdir $DIR/$tdir
14680         kill -USR1 $MULTIPID
14681         wait $MULTIPID
14682         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14683         return 0
14684 }
14685 run_test 181 "Test open-unlinked dir ========================"
14686
14687 test_182() {
14688         local fcount=1000
14689         local tcount=10
14690
14691         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14692
14693         $LCTL set_param mdc.*.rpc_stats=clear
14694
14695         for (( i = 0; i < $tcount; i++ )) ; do
14696                 mkdir $DIR/$tdir/$i
14697         done
14698
14699         for (( i = 0; i < $tcount; i++ )) ; do
14700                 createmany -o $DIR/$tdir/$i/f- $fcount &
14701         done
14702         wait
14703
14704         for (( i = 0; i < $tcount; i++ )) ; do
14705                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14706         done
14707         wait
14708
14709         $LCTL get_param mdc.*.rpc_stats
14710
14711         rm -rf $DIR/$tdir
14712 }
14713 run_test 182 "Test parallel modify metadata operations ================"
14714
14715 test_183() { # LU-2275
14716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14717         remote_mds_nodsh && skip "remote MDS with nodsh"
14718         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
14719                 skip "Need MDS version at least 2.3.56"
14720
14721         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14722         echo aaa > $DIR/$tdir/$tfile
14723
14724 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
14725         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
14726
14727         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
14728         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
14729
14730         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14731
14732         # Flush negative dentry cache
14733         touch $DIR/$tdir/$tfile
14734
14735         # We are not checking for any leaked references here, they'll
14736         # become evident next time we do cleanup with module unload.
14737         rm -rf $DIR/$tdir
14738 }
14739 run_test 183 "No crash or request leak in case of strange dispositions ========"
14740
14741 # test suite 184 is for LU-2016, LU-2017
14742 test_184a() {
14743         check_swap_layouts_support
14744
14745         dir0=$DIR/$tdir/$testnum
14746         test_mkdir -p -c1 $dir0
14747         ref1=/etc/passwd
14748         ref2=/etc/group
14749         file1=$dir0/f1
14750         file2=$dir0/f2
14751         $SETSTRIPE -c1 $file1
14752         cp $ref1 $file1
14753         $SETSTRIPE -c2 $file2
14754         cp $ref2 $file2
14755         gen1=$($GETSTRIPE -g $file1)
14756         gen2=$($GETSTRIPE -g $file2)
14757
14758         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
14759         gen=$($GETSTRIPE -g $file1)
14760         [[ $gen1 != $gen ]] ||
14761                 "Layout generation on $file1 does not change"
14762         gen=$($GETSTRIPE -g $file2)
14763         [[ $gen2 != $gen ]] ||
14764                 "Layout generation on $file2 does not change"
14765
14766         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
14767         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14768
14769         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
14770 }
14771 run_test 184a "Basic layout swap"
14772
14773 test_184b() {
14774         check_swap_layouts_support
14775
14776         dir0=$DIR/$tdir/$testnum
14777         mkdir -p $dir0 || error "creating dir $dir0"
14778         file1=$dir0/f1
14779         file2=$dir0/f2
14780         file3=$dir0/f3
14781         dir1=$dir0/d1
14782         dir2=$dir0/d2
14783         mkdir $dir1 $dir2
14784         $SETSTRIPE -c1 $file1
14785         $SETSTRIPE -c2 $file2
14786         $SETSTRIPE -c1 $file3
14787         chown $RUNAS_ID $file3
14788         gen1=$($GETSTRIPE -g $file1)
14789         gen2=$($GETSTRIPE -g $file2)
14790
14791         $LFS swap_layouts $dir1 $dir2 &&
14792                 error "swap of directories layouts should fail"
14793         $LFS swap_layouts $dir1 $file1 &&
14794                 error "swap of directory and file layouts should fail"
14795         $RUNAS $LFS swap_layouts $file1 $file2 &&
14796                 error "swap of file we cannot write should fail"
14797         $LFS swap_layouts $file1 $file3 &&
14798                 error "swap of file with different owner should fail"
14799         /bin/true # to clear error code
14800 }
14801 run_test 184b "Forbidden layout swap (will generate errors)"
14802
14803 test_184c() {
14804         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
14805         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
14806         check_swap_layouts_support
14807
14808         local dir0=$DIR/$tdir/$testnum
14809         mkdir -p $dir0 || error "creating dir $dir0"
14810
14811         local ref1=$dir0/ref1
14812         local ref2=$dir0/ref2
14813         local file1=$dir0/file1
14814         local file2=$dir0/file2
14815         # create a file large enough for the concurrent test
14816         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
14817         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
14818         echo "ref file size: ref1($(stat -c %s $ref1))," \
14819              "ref2($(stat -c %s $ref2))"
14820
14821         cp $ref2 $file2
14822         dd if=$ref1 of=$file1 bs=16k &
14823         local DD_PID=$!
14824
14825         # Make sure dd starts to copy file
14826         while [ ! -f $file1 ]; do sleep 0.1; done
14827
14828         $LFS swap_layouts $file1 $file2
14829         local rc=$?
14830         wait $DD_PID
14831         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
14832         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
14833
14834         # how many bytes copied before swapping layout
14835         local copied=$(stat -c %s $file2)
14836         local remaining=$(stat -c %s $ref1)
14837         remaining=$((remaining - copied))
14838         echo "Copied $copied bytes before swapping layout..."
14839
14840         cmp -n $copied $file1 $ref2 | grep differ &&
14841                 error "Content mismatch [0, $copied) of ref2 and file1"
14842         cmp -n $copied $file2 $ref1 ||
14843                 error "Content mismatch [0, $copied) of ref1 and file2"
14844         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
14845                 error "Content mismatch [$copied, EOF) of ref1 and file1"
14846
14847         # clean up
14848         rm -f $ref1 $ref2 $file1 $file2
14849 }
14850 run_test 184c "Concurrent write and layout swap"
14851
14852 test_184d() {
14853         check_swap_layouts_support
14854         [ -z "$(which getfattr 2>/dev/null)" ] &&
14855                 skip_env "no getfattr command"
14856
14857         local file1=$DIR/$tdir/$tfile-1
14858         local file2=$DIR/$tdir/$tfile-2
14859         local file3=$DIR/$tdir/$tfile-3
14860         local lovea1
14861         local lovea2
14862
14863         mkdir -p $DIR/$tdir
14864         touch $file1 || error "create $file1 failed"
14865         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14866                 error "create $file2 failed"
14867         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14868                 error "create $file3 failed"
14869         lovea1=$(get_layout_param $file1)
14870
14871         $LFS swap_layouts $file2 $file3 ||
14872                 error "swap $file2 $file3 layouts failed"
14873         $LFS swap_layouts $file1 $file2 ||
14874                 error "swap $file1 $file2 layouts failed"
14875
14876         lovea2=$(get_layout_param $file2)
14877         echo "$lovea1"
14878         echo "$lovea2"
14879         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
14880
14881         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14882         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
14883 }
14884 run_test 184d "allow stripeless layouts swap"
14885
14886 test_184e() {
14887         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
14888                 skip "Need MDS version at least 2.6.94"
14889         check_swap_layouts_support
14890         [ -z "$(which getfattr 2>/dev/null)" ] &&
14891                 skip_env "no getfattr command"
14892
14893         local file1=$DIR/$tdir/$tfile-1
14894         local file2=$DIR/$tdir/$tfile-2
14895         local file3=$DIR/$tdir/$tfile-3
14896         local lovea
14897
14898         mkdir -p $DIR/$tdir
14899         touch $file1 || error "create $file1 failed"
14900         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14901                 error "create $file2 failed"
14902         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14903                 error "create $file3 failed"
14904
14905         $LFS swap_layouts $file1 $file2 ||
14906                 error "swap $file1 $file2 layouts failed"
14907
14908         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14909         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
14910
14911         echo 123 > $file1 || error "Should be able to write into $file1"
14912
14913         $LFS swap_layouts $file1 $file3 ||
14914                 error "swap $file1 $file3 layouts failed"
14915
14916         echo 123 > $file1 || error "Should be able to write into $file1"
14917
14918         rm -rf $file1 $file2 $file3
14919 }
14920 run_test 184e "Recreate layout after stripeless layout swaps"
14921
14922 test_184f() {
14923         # Create a file with name longer than sizeof(struct stat) ==
14924         # 144 to see if we can get chars from the file name to appear
14925         # in the returned striping. Note that 'f' == 0x66.
14926         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
14927
14928         mkdir -p $DIR/$tdir
14929         mcreate $DIR/$tdir/$file
14930         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
14931                 error "IOC_MDC_GETFILEINFO returned garbage striping"
14932         fi
14933 }
14934 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
14935
14936 test_185() { # LU-2441
14937         # LU-3553 - no volatile file support in old servers
14938         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
14939                 skip "Need MDS version at least 2.3.60"
14940
14941         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14942         touch $DIR/$tdir/spoo
14943         local mtime1=$(stat -c "%Y" $DIR/$tdir)
14944         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
14945                 error "cannot create/write a volatile file"
14946         [ "$FILESET" == "" ] &&
14947         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
14948                 error "FID is still valid after close"
14949
14950         multiop_bg_pause $DIR/$tdir vVw4096_c
14951         local multi_pid=$!
14952
14953         local OLD_IFS=$IFS
14954         IFS=":"
14955         local fidv=($fid)
14956         IFS=$OLD_IFS
14957         # assume that the next FID for this client is sequential, since stdout
14958         # is unfortunately eaten by multiop_bg_pause
14959         local n=$((${fidv[1]} + 1))
14960         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
14961         if [ "$FILESET" == "" ]; then
14962                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
14963                         error "FID is missing before close"
14964         fi
14965         kill -USR1 $multi_pid
14966         # 1 second delay, so if mtime change we will see it
14967         sleep 1
14968         local mtime2=$(stat -c "%Y" $DIR/$tdir)
14969         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
14970 }
14971 run_test 185 "Volatile file support"
14972
14973 function create_check_volatile() {
14974         local idx=$1
14975         local tgt
14976
14977         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
14978         local PID=$!
14979         sleep 1
14980         local FID=$(cat /tmp/${tfile}.fid)
14981         [ "$FID" == "" ] && error "can't get FID for volatile"
14982         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
14983         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
14984         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
14985         kill -USR1 $PID
14986         wait
14987         sleep 1
14988         cancel_lru_locks mdc # flush opencache
14989         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
14990         return 0
14991 }
14992
14993 test_185a(){
14994         # LU-12516 - volatile creation via .lustre
14995         [[ $MDS1_VERSION -ge $(version_code 2.12.2) ]] ||
14996                 skip "Need MDS version at least 2.12.2"
14997
14998         create_check_volatile 0
14999         [ $MDSCOUNT -lt 2 ] && return 0
15000
15001         # DNE case
15002         create_check_volatile 1
15003
15004         return 0
15005 }
15006 run_test 185a "Volatile file creation in .lustre/fid/"
15007
15008 test_187a() {
15009         remote_mds_nodsh && skip "remote MDS with nodsh"
15010         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15011                 skip "Need MDS version at least 2.3.0"
15012
15013         local dir0=$DIR/$tdir/$testnum
15014         mkdir -p $dir0 || error "creating dir $dir0"
15015
15016         local file=$dir0/file1
15017         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15018         local dv1=$($LFS data_version $file)
15019         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15020         local dv2=$($LFS data_version $file)
15021         [[ $dv1 != $dv2 ]] ||
15022                 error "data version did not change on write $dv1 == $dv2"
15023
15024         # clean up
15025         rm -f $file1
15026 }
15027 run_test 187a "Test data version change"
15028
15029 test_187b() {
15030         remote_mds_nodsh && skip "remote MDS with nodsh"
15031         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15032                 skip "Need MDS version at least 2.3.0"
15033
15034         local dir0=$DIR/$tdir/$testnum
15035         mkdir -p $dir0 || error "creating dir $dir0"
15036
15037         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15038         [[ ${DV[0]} != ${DV[1]} ]] ||
15039                 error "data version did not change on write"\
15040                       " ${DV[0]} == ${DV[1]}"
15041
15042         # clean up
15043         rm -f $file1
15044 }
15045 run_test 187b "Test data version change on volatile file"
15046
15047 test_200() {
15048         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15049         remote_mgs_nodsh && skip "remote MGS with nodsh"
15050         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15051
15052         local POOL=${POOL:-cea1}
15053         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15054         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15055         # Pool OST targets
15056         local first_ost=0
15057         local last_ost=$(($OSTCOUNT - 1))
15058         local ost_step=2
15059         local ost_list=$(seq $first_ost $ost_step $last_ost)
15060         local ost_range="$first_ost $last_ost $ost_step"
15061         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15062         local file_dir=$POOL_ROOT/file_tst
15063         local subdir=$test_path/subdir
15064         local rc=0
15065
15066         while : ; do
15067                 # former test_200a test_200b
15068                 pool_add $POOL                          || { rc=$? ; break; }
15069                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
15070                 # former test_200c test_200d
15071                 mkdir -p $test_path
15072                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
15073                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
15074                 mkdir -p $subdir
15075                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
15076                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
15077                                                         || { rc=$? ; break; }
15078                 # former test_200e test_200f
15079                 local files=$((OSTCOUNT*3))
15080                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
15081                                                         || { rc=$? ; break; }
15082                 pool_create_files $POOL $file_dir $files "$ost_list" \
15083                                                         || { rc=$? ; break; }
15084                 # former test_200g test_200h
15085                 pool_lfs_df $POOL                       || { rc=$? ; break; }
15086                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
15087
15088                 # former test_201a test_201b test_201c
15089                 pool_remove_first_target $POOL          || { rc=$? ; break; }
15090
15091                 local f=$test_path/$tfile
15092                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
15093                 pool_remove $POOL $f                    || { rc=$? ; break; }
15094                 break
15095         done
15096
15097         destroy_test_pools
15098
15099         return $rc
15100 }
15101 run_test 200 "OST pools"
15102
15103 # usage: default_attr <count | size | offset>
15104 default_attr() {
15105         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
15106 }
15107
15108 # usage: check_default_stripe_attr
15109 check_default_stripe_attr() {
15110         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
15111         case $1 in
15112         --stripe-count|-c)
15113                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
15114         --stripe-size|-S)
15115                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
15116         --stripe-index|-i)
15117                 EXPECTED=-1;;
15118         *)
15119                 error "unknown getstripe attr '$1'"
15120         esac
15121
15122         [ $ACTUAL == $EXPECTED ] ||
15123                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
15124 }
15125
15126 test_204a() {
15127         test_mkdir $DIR/$tdir
15128         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15129
15130         check_default_stripe_attr --stripe-count
15131         check_default_stripe_attr --stripe-size
15132         check_default_stripe_attr --stripe-index
15133 }
15134 run_test 204a "Print default stripe attributes"
15135
15136 test_204b() {
15137         test_mkdir $DIR/$tdir
15138         $SETSTRIPE --stripe-count 1 $DIR/$tdir
15139
15140         check_default_stripe_attr --stripe-size
15141         check_default_stripe_attr --stripe-index
15142 }
15143 run_test 204b "Print default stripe size and offset"
15144
15145 test_204c() {
15146         test_mkdir $DIR/$tdir
15147         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
15148
15149         check_default_stripe_attr --stripe-count
15150         check_default_stripe_attr --stripe-index
15151 }
15152 run_test 204c "Print default stripe count and offset"
15153
15154 test_204d() {
15155         test_mkdir $DIR/$tdir
15156         $SETSTRIPE --stripe-index 0 $DIR/$tdir
15157
15158         check_default_stripe_attr --stripe-count
15159         check_default_stripe_attr --stripe-size
15160 }
15161 run_test 204d "Print default stripe count and size"
15162
15163 test_204e() {
15164         test_mkdir $DIR/$tdir
15165         $SETSTRIPE -d $DIR/$tdir
15166
15167         check_default_stripe_attr --stripe-count --raw
15168         check_default_stripe_attr --stripe-size --raw
15169         check_default_stripe_attr --stripe-index --raw
15170 }
15171 run_test 204e "Print raw stripe attributes"
15172
15173 test_204f() {
15174         test_mkdir $DIR/$tdir
15175         $SETSTRIPE --stripe-count 1 $DIR/$tdir
15176
15177         check_default_stripe_attr --stripe-size --raw
15178         check_default_stripe_attr --stripe-index --raw
15179 }
15180 run_test 204f "Print raw stripe size and offset"
15181
15182 test_204g() {
15183         test_mkdir $DIR/$tdir
15184         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
15185
15186         check_default_stripe_attr --stripe-count --raw
15187         check_default_stripe_attr --stripe-index --raw
15188 }
15189 run_test 204g "Print raw stripe count and offset"
15190
15191 test_204h() {
15192         test_mkdir $DIR/$tdir
15193         $SETSTRIPE --stripe-index 0 $DIR/$tdir
15194
15195         check_default_stripe_attr --stripe-count --raw
15196         check_default_stripe_attr --stripe-size --raw
15197 }
15198 run_test 204h "Print raw stripe count and size"
15199
15200 # Figure out which job scheduler is being used, if any,
15201 # or use a fake one
15202 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15203         JOBENV=SLURM_JOB_ID
15204 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15205         JOBENV=LSB_JOBID
15206 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15207         JOBENV=PBS_JOBID
15208 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15209         JOBENV=LOADL_STEP_ID
15210 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15211         JOBENV=JOB_ID
15212 else
15213         $LCTL list_param jobid_name > /dev/null 2>&1
15214         if [ $? -eq 0 ]; then
15215                 JOBENV=nodelocal
15216         else
15217                 JOBENV=FAKE_JOBID
15218         fi
15219 fi
15220 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15221
15222 verify_jobstats() {
15223         local cmd=($1)
15224         shift
15225         local facets="$@"
15226
15227 # we don't really need to clear the stats for this test to work, since each
15228 # command has a unique jobid, but it makes debugging easier if needed.
15229 #       for facet in $facets; do
15230 #               local dev=$(convert_facet2label $facet)
15231 #               # clear old jobstats
15232 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15233 #       done
15234
15235         # use a new JobID for each test, or we might see an old one
15236         [ "$JOBENV" = "FAKE_JOBID" ] &&
15237                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15238
15239         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15240
15241         [ "$JOBENV" = "nodelocal" ] && {
15242                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15243                 $LCTL set_param jobid_name=$FAKE_JOBID
15244                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15245         }
15246
15247         log "Test: ${cmd[*]}"
15248         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15249
15250         if [ $JOBENV = "FAKE_JOBID" ]; then
15251                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15252         else
15253                 ${cmd[*]}
15254         fi
15255
15256         # all files are created on OST0000
15257         for facet in $facets; do
15258                 local stats="*.$(convert_facet2label $facet).job_stats"
15259
15260                 # strip out libtool wrappers for in-tree executables
15261                 if [ $(do_facet $facet lctl get_param $stats |
15262                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15263                         do_facet $facet lctl get_param $stats
15264                         error "No jobstats for $JOBVAL found on $facet::$stats"
15265                 fi
15266         done
15267 }
15268
15269 jobstats_set() {
15270         local new_jobenv=$1
15271
15272         set_persistent_param_and_check client "jobid_var" \
15273                 "$FSNAME.sys.jobid_var" $new_jobenv
15274 }
15275
15276 test_205a() { # Job stats
15277         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15278         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15279                 skip "Need MDS version with at least 2.7.1"
15280         remote_mgs_nodsh && skip "remote MGS with nodsh"
15281         remote_mds_nodsh && skip "remote MDS with nodsh"
15282         remote_ost_nodsh && skip "remote OST with nodsh"
15283         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15284                 skip "Server doesn't support jobstats"
15285         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15286
15287         local old_jobenv=$($LCTL get_param -n jobid_var)
15288         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15289
15290         if [[ $PERM_CMD = *"set_param -P"* ]]; then
15291                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15292         else
15293                 stack_trap "do_facet mgs $PERM_CMD \
15294                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15295         fi
15296         changelog_register
15297
15298         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15299                                 mdt.*.job_cleanup_interval | head -n 1)
15300         local new_interval=5
15301         do_facet $SINGLEMDS \
15302                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15303         stack_trap "do_facet $SINGLEMDS \
15304                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15305         local start=$SECONDS
15306
15307         local cmd
15308         # mkdir
15309         cmd="mkdir $DIR/$tdir"
15310         verify_jobstats "$cmd" "$SINGLEMDS"
15311         # rmdir
15312         cmd="rmdir $DIR/$tdir"
15313         verify_jobstats "$cmd" "$SINGLEMDS"
15314         # mkdir on secondary MDT
15315         if [ $MDSCOUNT -gt 1 ]; then
15316                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15317                 verify_jobstats "$cmd" "mds2"
15318         fi
15319         # mknod
15320         cmd="mknod $DIR/$tfile c 1 3"
15321         verify_jobstats "$cmd" "$SINGLEMDS"
15322         # unlink
15323         cmd="rm -f $DIR/$tfile"
15324         verify_jobstats "$cmd" "$SINGLEMDS"
15325         # create all files on OST0000 so verify_jobstats can find OST stats
15326         # open & close
15327         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
15328         verify_jobstats "$cmd" "$SINGLEMDS"
15329         # setattr
15330         cmd="touch $DIR/$tfile"
15331         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15332         # write
15333         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15334         verify_jobstats "$cmd" "ost1"
15335         # read
15336         cancel_lru_locks osc
15337         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15338         verify_jobstats "$cmd" "ost1"
15339         # truncate
15340         cmd="$TRUNCATE $DIR/$tfile 0"
15341         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15342         # rename
15343         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15344         verify_jobstats "$cmd" "$SINGLEMDS"
15345         # jobstats expiry - sleep until old stats should be expired
15346         local left=$((new_interval + 5 - (SECONDS - start)))
15347         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15348                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15349                         "0" $left
15350         cmd="mkdir $DIR/$tdir.expire"
15351         verify_jobstats "$cmd" "$SINGLEMDS"
15352         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15353             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15354
15355         # Ensure that jobid are present in changelog (if supported by MDS)
15356         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15357                 changelog_dump | tail -10
15358                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15359                 [ $jobids -eq 9 ] ||
15360                         error "Wrong changelog jobid count $jobids != 9"
15361
15362                 # LU-5862
15363                 JOBENV="disable"
15364                 jobstats_set $JOBENV
15365                 touch $DIR/$tfile
15366                 changelog_dump | grep $tfile
15367                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15368                 [ $jobids -eq 0 ] ||
15369                         error "Unexpected jobids when jobid_var=$JOBENV"
15370         fi
15371
15372         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15373         JOBENV="JOBCOMPLEX"
15374         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15375
15376         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15377 }
15378 run_test 205a "Verify job stats"
15379
15380 # LU-13117
15381 test_205b() {
15382         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
15383         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
15384         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
15385                 grep job_id: | grep foolish &&
15386                         error "Unexpected jobid found"
15387         true
15388 }
15389 run_test 205b "Verify job stats jobid parsing"
15390
15391 # LU-1480, LU-1773 and LU-1657
15392 test_206() {
15393         mkdir -p $DIR/$tdir
15394         $SETSTRIPE -c -1 $DIR/$tdir
15395 #define OBD_FAIL_LOV_INIT 0x1403
15396         $LCTL set_param fail_loc=0xa0001403
15397         $LCTL set_param fail_val=1
15398         touch $DIR/$tdir/$tfile || true
15399 }
15400 run_test 206 "fail lov_init_raid0() doesn't lbug"
15401
15402 test_207a() {
15403         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15404         local fsz=`stat -c %s $DIR/$tfile`
15405         cancel_lru_locks mdc
15406
15407         # do not return layout in getattr intent
15408 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15409         $LCTL set_param fail_loc=0x170
15410         local sz=`stat -c %s $DIR/$tfile`
15411
15412         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15413
15414         rm -rf $DIR/$tfile
15415 }
15416 run_test 207a "can refresh layout at glimpse"
15417
15418 test_207b() {
15419         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15420         local cksum=`md5sum $DIR/$tfile`
15421         local fsz=`stat -c %s $DIR/$tfile`
15422         cancel_lru_locks mdc
15423         cancel_lru_locks osc
15424
15425         # do not return layout in getattr intent
15426 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15427         $LCTL set_param fail_loc=0x171
15428
15429         # it will refresh layout after the file is opened but before read issues
15430         echo checksum is "$cksum"
15431         echo "$cksum" |md5sum -c --quiet || error "file differs"
15432
15433         rm -rf $DIR/$tfile
15434 }
15435 run_test 207b "can refresh layout at open"
15436
15437 test_208() {
15438         # FIXME: in this test suite, only RD lease is used. This is okay
15439         # for now as only exclusive open is supported. After generic lease
15440         # is done, this test suite should be revised. - Jinshan
15441
15442         remote_mds_nodsh && skip "remote MDS with nodsh"
15443         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15444                 skip "Need MDS version at least 2.4.52"
15445
15446         echo "==== test 1: verify get lease work"
15447         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15448
15449         echo "==== test 2: verify lease can be broken by upcoming open"
15450         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15451         local PID=$!
15452         sleep 1
15453
15454         $MULTIOP $DIR/$tfile oO_RDONLY:c
15455         kill -USR1 $PID && wait $PID || error "break lease error"
15456
15457         echo "==== test 3: verify lease can't be granted if an open already exists"
15458         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15459         local PID=$!
15460         sleep 1
15461
15462         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15463         kill -USR1 $PID && wait $PID || error "open file error"
15464
15465         echo "==== test 4: lease can sustain over recovery"
15466         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15467         PID=$!
15468         sleep 1
15469
15470         fail mds1
15471
15472         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15473
15474         echo "==== test 5: lease broken can't be regained by replay"
15475         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15476         PID=$!
15477         sleep 1
15478
15479         # open file to break lease and then recovery
15480         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15481         fail mds1
15482
15483         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15484
15485         rm -f $DIR/$tfile
15486 }
15487 run_test 208 "Exclusive open"
15488
15489 test_209() {
15490         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15491                 skip_env "must have disp_stripe"
15492
15493         touch $DIR/$tfile
15494         sync; sleep 5; sync;
15495
15496         echo 3 > /proc/sys/vm/drop_caches
15497         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15498
15499         # open/close 500 times
15500         for i in $(seq 500); do
15501                 cat $DIR/$tfile
15502         done
15503
15504         echo 3 > /proc/sys/vm/drop_caches
15505         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15506
15507         echo "before: $req_before, after: $req_after"
15508         [ $((req_after - req_before)) -ge 300 ] &&
15509                 error "open/close requests are not freed"
15510         return 0
15511 }
15512 run_test 209 "read-only open/close requests should be freed promptly"
15513
15514 test_212() {
15515         size=`date +%s`
15516         size=$((size % 8192 + 1))
15517         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15518         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15519         rm -f $DIR/f212 $DIR/f212.xyz
15520 }
15521 run_test 212 "Sendfile test ============================================"
15522
15523 test_213() {
15524         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15525         cancel_lru_locks osc
15526         lctl set_param fail_loc=0x8000040f
15527         # generate a read lock
15528         cat $DIR/$tfile > /dev/null
15529         # write to the file, it will try to cancel the above read lock.
15530         cat /etc/hosts >> $DIR/$tfile
15531 }
15532 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15533
15534 test_214() { # for bug 20133
15535         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15536         for (( i=0; i < 340; i++ )) ; do
15537                 touch $DIR/$tdir/d214c/a$i
15538         done
15539
15540         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15541         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15542         ls $DIR/d214c || error "ls $DIR/d214c failed"
15543         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15544         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15545 }
15546 run_test 214 "hash-indexed directory test - bug 20133"
15547
15548 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15549 create_lnet_proc_files() {
15550         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15551 }
15552
15553 # counterpart of create_lnet_proc_files
15554 remove_lnet_proc_files() {
15555         rm -f $TMP/lnet_$1.sys
15556 }
15557
15558 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15559 # 3rd arg as regexp for body
15560 check_lnet_proc_stats() {
15561         local l=$(cat "$TMP/lnet_$1" |wc -l)
15562         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15563
15564         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15565 }
15566
15567 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15568 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15569 # optional and can be regexp for 2nd line (lnet.routes case)
15570 check_lnet_proc_entry() {
15571         local blp=2          # blp stands for 'position of 1st line of body'
15572         [ -z "$5" ] || blp=3 # lnet.routes case
15573
15574         local l=$(cat "$TMP/lnet_$1" |wc -l)
15575         # subtracting one from $blp because the body can be empty
15576         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15577
15578         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15579                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15580
15581         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15582                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15583
15584         # bail out if any unexpected line happened
15585         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15586         [ "$?" != 0 ] || error "$2 misformatted"
15587 }
15588
15589 test_215() { # for bugs 18102, 21079, 21517
15590         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15591
15592         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15593         local P='[1-9][0-9]*'           # positive numeric
15594         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15595         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15596         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15597         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15598
15599         local L1 # regexp for 1st line
15600         local L2 # regexp for 2nd line (optional)
15601         local BR # regexp for the rest (body)
15602
15603         # lnet.stats should look as 11 space-separated non-negative numerics
15604         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15605         create_lnet_proc_files "stats"
15606         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15607         remove_lnet_proc_files "stats"
15608
15609         # lnet.routes should look like this:
15610         # Routing disabled/enabled
15611         # net hops priority state router
15612         # where net is a string like tcp0, hops > 0, priority >= 0,
15613         # state is up/down,
15614         # router is a string like 192.168.1.1@tcp2
15615         L1="^Routing (disabled|enabled)$"
15616         L2="^net +hops +priority +state +router$"
15617         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15618         create_lnet_proc_files "routes"
15619         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15620         remove_lnet_proc_files "routes"
15621
15622         # lnet.routers should look like this:
15623         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15624         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15625         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15626         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15627         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
15628         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
15629         create_lnet_proc_files "routers"
15630         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15631         remove_lnet_proc_files "routers"
15632
15633         # lnet.peers should look like this:
15634         # nid refs state last max rtr min tx min queue
15635         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15636         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15637         # numeric (0 or >0 or <0), queue >= 0.
15638         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15639         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15640         create_lnet_proc_files "peers"
15641         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15642         remove_lnet_proc_files "peers"
15643
15644         # lnet.buffers  should look like this:
15645         # pages count credits min
15646         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15647         L1="^pages +count +credits +min$"
15648         BR="^ +$N +$N +$I +$I$"
15649         create_lnet_proc_files "buffers"
15650         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15651         remove_lnet_proc_files "buffers"
15652
15653         # lnet.nis should look like this:
15654         # nid status alive refs peer rtr max tx min
15655         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15656         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15657         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15658         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15659         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15660         create_lnet_proc_files "nis"
15661         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15662         remove_lnet_proc_files "nis"
15663
15664         # can we successfully write to lnet.stats?
15665         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15666 }
15667 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15668
15669 test_216() { # bug 20317
15670         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15671         remote_ost_nodsh && skip "remote OST with nodsh"
15672
15673         local node
15674         local facets=$(get_facets OST)
15675         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15676
15677         save_lustre_params client "osc.*.contention_seconds" > $p
15678         save_lustre_params $facets \
15679                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15680         save_lustre_params $facets \
15681                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15682         save_lustre_params $facets \
15683                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15684         clear_stats osc.*.osc_stats
15685
15686         # agressive lockless i/o settings
15687         do_nodes $(comma_list $(osts_nodes)) \
15688                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15689                         ldlm.namespaces.filter-*.contended_locks=0 \
15690                         ldlm.namespaces.filter-*.contention_seconds=60"
15691         lctl set_param -n osc.*.contention_seconds=60
15692
15693         $DIRECTIO write $DIR/$tfile 0 10 4096
15694         $CHECKSTAT -s 40960 $DIR/$tfile
15695
15696         # disable lockless i/o
15697         do_nodes $(comma_list $(osts_nodes)) \
15698                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15699                         ldlm.namespaces.filter-*.contended_locks=32 \
15700                         ldlm.namespaces.filter-*.contention_seconds=0"
15701         lctl set_param -n osc.*.contention_seconds=0
15702         clear_stats osc.*.osc_stats
15703
15704         dd if=/dev/zero of=$DIR/$tfile count=0
15705         $CHECKSTAT -s 0 $DIR/$tfile
15706
15707         restore_lustre_params <$p
15708         rm -f $p
15709         rm $DIR/$tfile
15710 }
15711 run_test 216 "check lockless direct write updates file size and kms correctly"
15712
15713 test_217() { # bug 22430
15714         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15715
15716         local node
15717         local nid
15718
15719         for node in $(nodes_list); do
15720                 nid=$(host_nids_address $node $NETTYPE)
15721                 if [[ $nid = *-* ]] ; then
15722                         echo "lctl ping $(h2nettype $nid)"
15723                         lctl ping $(h2nettype $nid)
15724                 else
15725                         echo "skipping $node (no hyphen detected)"
15726                 fi
15727         done
15728 }
15729 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
15730
15731 test_218() {
15732        # do directio so as not to populate the page cache
15733        log "creating a 10 Mb file"
15734        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15735        log "starting reads"
15736        dd if=$DIR/$tfile of=/dev/null bs=4096 &
15737        log "truncating the file"
15738        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15739        log "killing dd"
15740        kill %+ || true # reads might have finished
15741        echo "wait until dd is finished"
15742        wait
15743        log "removing the temporary file"
15744        rm -rf $DIR/$tfile || error "tmp file removal failed"
15745 }
15746 run_test 218 "parallel read and truncate should not deadlock"
15747
15748 test_219() {
15749         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15750
15751         # write one partial page
15752         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
15753         # set no grant so vvp_io_commit_write will do sync write
15754         $LCTL set_param fail_loc=0x411
15755         # write a full page at the end of file
15756         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
15757
15758         $LCTL set_param fail_loc=0
15759         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
15760         $LCTL set_param fail_loc=0x411
15761         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
15762
15763         # LU-4201
15764         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
15765         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
15766 }
15767 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
15768
15769 test_220() { #LU-325
15770         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15771         remote_ost_nodsh && skip "remote OST with nodsh"
15772         remote_mds_nodsh && skip "remote MDS with nodsh"
15773         remote_mgs_nodsh && skip "remote MGS with nodsh"
15774
15775         local OSTIDX=0
15776
15777         # create on MDT0000 so the last_id and next_id are correct
15778         mkdir $DIR/$tdir
15779         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
15780         OST=${OST%_UUID}
15781
15782         # on the mdt's osc
15783         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
15784         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
15785                         osp.$mdtosc_proc1.prealloc_last_id)
15786         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
15787                         osp.$mdtosc_proc1.prealloc_next_id)
15788
15789         $LFS df -i
15790
15791         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
15792         #define OBD_FAIL_OST_ENOINO              0x229
15793         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
15794         create_pool $FSNAME.$TESTNAME || return 1
15795         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
15796
15797         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
15798
15799         MDSOBJS=$((last_id - next_id))
15800         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
15801
15802         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
15803         echo "OST still has $count kbytes free"
15804
15805         echo "create $MDSOBJS files @next_id..."
15806         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
15807
15808         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15809                         osp.$mdtosc_proc1.prealloc_last_id)
15810         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15811                         osp.$mdtosc_proc1.prealloc_next_id)
15812
15813         echo "after creation, last_id=$last_id2, next_id=$next_id2"
15814         $LFS df -i
15815
15816         echo "cleanup..."
15817
15818         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
15819         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
15820
15821         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
15822                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
15823         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15824                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
15825         echo "unlink $MDSOBJS files @$next_id..."
15826         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
15827 }
15828 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
15829
15830 test_221() {
15831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15832
15833         dd if=`which date` of=$MOUNT/date oflag=sync
15834         chmod +x $MOUNT/date
15835
15836         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
15837         $LCTL set_param fail_loc=0x80001401
15838
15839         $MOUNT/date > /dev/null
15840         rm -f $MOUNT/date
15841 }
15842 run_test 221 "make sure fault and truncate race to not cause OOM"
15843
15844 test_222a () {
15845         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15846
15847         rm -rf $DIR/$tdir
15848         test_mkdir $DIR/$tdir
15849         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15850         createmany -o $DIR/$tdir/$tfile 10
15851         cancel_lru_locks mdc
15852         cancel_lru_locks osc
15853         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15854         $LCTL set_param fail_loc=0x31a
15855         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
15856         $LCTL set_param fail_loc=0
15857         rm -r $DIR/$tdir
15858 }
15859 run_test 222a "AGL for ls should not trigger CLIO lock failure"
15860
15861 test_222b () {
15862         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15863
15864         rm -rf $DIR/$tdir
15865         test_mkdir $DIR/$tdir
15866         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15867         createmany -o $DIR/$tdir/$tfile 10
15868         cancel_lru_locks mdc
15869         cancel_lru_locks osc
15870         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15871         $LCTL set_param fail_loc=0x31a
15872         rm -r $DIR/$tdir || error "AGL for rmdir failed"
15873         $LCTL set_param fail_loc=0
15874 }
15875 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
15876
15877 test_223 () {
15878         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15879
15880         rm -rf $DIR/$tdir
15881         test_mkdir $DIR/$tdir
15882         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15883         createmany -o $DIR/$tdir/$tfile 10
15884         cancel_lru_locks mdc
15885         cancel_lru_locks osc
15886         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
15887         $LCTL set_param fail_loc=0x31b
15888         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
15889         $LCTL set_param fail_loc=0
15890         rm -r $DIR/$tdir
15891 }
15892 run_test 223 "osc reenqueue if without AGL lock granted ======================="
15893
15894 test_224a() { # LU-1039, MRP-303
15895         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15896
15897         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
15898         $LCTL set_param fail_loc=0x508
15899         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
15900         $LCTL set_param fail_loc=0
15901         df $DIR
15902 }
15903 run_test 224a "Don't panic on bulk IO failure"
15904
15905 test_224b() { # LU-1039, MRP-303
15906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15907
15908         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
15909         cancel_lru_locks osc
15910         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
15911         $LCTL set_param fail_loc=0x515
15912         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
15913         $LCTL set_param fail_loc=0
15914         df $DIR
15915 }
15916 run_test 224b "Don't panic on bulk IO failure"
15917
15918 test_224c() { # LU-6441
15919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15920         remote_mds_nodsh && skip "remote MDS with nodsh"
15921
15922         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15923         save_writethrough $p
15924         set_cache writethrough on
15925
15926         local pages_per_rpc=$($LCTL get_param \
15927                                 osc.*.max_pages_per_rpc)
15928         local at_max=$($LCTL get_param -n at_max)
15929         local timeout=$($LCTL get_param -n timeout)
15930         local test_at="at_max"
15931         local param_at="$FSNAME.sys.at_max"
15932         local test_timeout="timeout"
15933         local param_timeout="$FSNAME.sys.timeout"
15934
15935         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
15936
15937         set_persistent_param_and_check client "$test_at" "$param_at" 0
15938         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
15939
15940         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
15941         do_facet ost1 $LCTL set_param fail_loc=0x520
15942         $LFS setstripe -c 1 -i 0 $DIR/$tfile
15943         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
15944         sync
15945         do_facet ost1 $LCTL set_param fail_loc=0
15946
15947         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
15948         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
15949                 $timeout
15950
15951         $LCTL set_param -n $pages_per_rpc
15952         restore_lustre_params < $p
15953         rm -f $p
15954 }
15955 run_test 224c "Don't hang if one of md lost during large bulk RPC"
15956
15957 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
15958 test_225a () {
15959         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15960         if [ -z ${MDSSURVEY} ]; then
15961                 skip_env "mds-survey not found"
15962         fi
15963         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15964                 skip "Need MDS version at least 2.2.51"
15965
15966         local mds=$(facet_host $SINGLEMDS)
15967         local target=$(do_nodes $mds 'lctl dl' |
15968                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15969
15970         local cmd1="file_count=1000 thrhi=4"
15971         local cmd2="dir_count=2 layer=mdd stripe_count=0"
15972         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15973         local cmd="$cmd1 $cmd2 $cmd3"
15974
15975         rm -f ${TMP}/mds_survey*
15976         echo + $cmd
15977         eval $cmd || error "mds-survey with zero-stripe failed"
15978         cat ${TMP}/mds_survey*
15979         rm -f ${TMP}/mds_survey*
15980 }
15981 run_test 225a "Metadata survey sanity with zero-stripe"
15982
15983 test_225b () {
15984         if [ -z ${MDSSURVEY} ]; then
15985                 skip_env "mds-survey not found"
15986         fi
15987         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15988                 skip "Need MDS version at least 2.2.51"
15989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15990         remote_mds_nodsh && skip "remote MDS with nodsh"
15991         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
15992                 skip_env "Need to mount OST to test"
15993         fi
15994
15995         local mds=$(facet_host $SINGLEMDS)
15996         local target=$(do_nodes $mds 'lctl dl' |
15997                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15998
15999         local cmd1="file_count=1000 thrhi=4"
16000         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16001         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16002         local cmd="$cmd1 $cmd2 $cmd3"
16003
16004         rm -f ${TMP}/mds_survey*
16005         echo + $cmd
16006         eval $cmd || error "mds-survey with stripe_count failed"
16007         cat ${TMP}/mds_survey*
16008         rm -f ${TMP}/mds_survey*
16009 }
16010 run_test 225b "Metadata survey sanity with stripe_count = 1"
16011
16012 mcreate_path2fid () {
16013         local mode=$1
16014         local major=$2
16015         local minor=$3
16016         local name=$4
16017         local desc=$5
16018         local path=$DIR/$tdir/$name
16019         local fid
16020         local rc
16021         local fid_path
16022
16023         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16024                 error "cannot create $desc"
16025
16026         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16027         rc=$?
16028         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16029
16030         fid_path=$($LFS fid2path $MOUNT $fid)
16031         rc=$?
16032         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16033
16034         [ "$path" == "$fid_path" ] ||
16035                 error "fid2path returned $fid_path, expected $path"
16036
16037         echo "pass with $path and $fid"
16038 }
16039
16040 test_226a () {
16041         rm -rf $DIR/$tdir
16042         mkdir -p $DIR/$tdir
16043
16044         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16045         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16046         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16047         mcreate_path2fid 0040666 0 0 dir "directory"
16048         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16049         mcreate_path2fid 0100666 0 0 file "regular file"
16050         mcreate_path2fid 0120666 0 0 link "symbolic link"
16051         mcreate_path2fid 0140666 0 0 sock "socket"
16052 }
16053 run_test 226a "call path2fid and fid2path on files of all type"
16054
16055 test_226b () {
16056         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16057
16058         local MDTIDX=1
16059
16060         rm -rf $DIR/$tdir
16061         mkdir -p $DIR/$tdir
16062         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16063                 error "create remote directory failed"
16064         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
16065         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
16066                                 "character special file (null)"
16067         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
16068                                 "character special file (no device)"
16069         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
16070         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
16071                                 "block special file (loop)"
16072         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
16073         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
16074         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
16075 }
16076 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
16077
16078 # LU-1299 Executing or running ldd on a truncated executable does not
16079 # cause an out-of-memory condition.
16080 test_227() {
16081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16082         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
16083
16084         dd if=$(which date) of=$MOUNT/date bs=1k count=1
16085         chmod +x $MOUNT/date
16086
16087         $MOUNT/date > /dev/null
16088         ldd $MOUNT/date > /dev/null
16089         rm -f $MOUNT/date
16090 }
16091 run_test 227 "running truncated executable does not cause OOM"
16092
16093 # LU-1512 try to reuse idle OI blocks
16094 test_228a() {
16095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16096         remote_mds_nodsh && skip "remote MDS with nodsh"
16097         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16098
16099         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16100         local myDIR=$DIR/$tdir
16101
16102         mkdir -p $myDIR
16103         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16104         $LCTL set_param fail_loc=0x80001002
16105         createmany -o $myDIR/t- 10000
16106         $LCTL set_param fail_loc=0
16107         # The guard is current the largest FID holder
16108         touch $myDIR/guard
16109         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16110                     tr -d '[')
16111         local IDX=$(($SEQ % 64))
16112
16113         do_facet $SINGLEMDS sync
16114         # Make sure journal flushed.
16115         sleep 6
16116         local blk1=$(do_facet $SINGLEMDS \
16117                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16118                      grep Blockcount | awk '{print $4}')
16119
16120         # Remove old files, some OI blocks will become idle.
16121         unlinkmany $myDIR/t- 10000
16122         # Create new files, idle OI blocks should be reused.
16123         createmany -o $myDIR/t- 2000
16124         do_facet $SINGLEMDS sync
16125         # Make sure journal flushed.
16126         sleep 6
16127         local blk2=$(do_facet $SINGLEMDS \
16128                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16129                      grep Blockcount | awk '{print $4}')
16130
16131         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16132 }
16133 run_test 228a "try to reuse idle OI blocks"
16134
16135 test_228b() {
16136         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16137         remote_mds_nodsh && skip "remote MDS with nodsh"
16138         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16139
16140         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16141         local myDIR=$DIR/$tdir
16142
16143         mkdir -p $myDIR
16144         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16145         $LCTL set_param fail_loc=0x80001002
16146         createmany -o $myDIR/t- 10000
16147         $LCTL set_param fail_loc=0
16148         # The guard is current the largest FID holder
16149         touch $myDIR/guard
16150         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16151                     tr -d '[')
16152         local IDX=$(($SEQ % 64))
16153
16154         do_facet $SINGLEMDS sync
16155         # Make sure journal flushed.
16156         sleep 6
16157         local blk1=$(do_facet $SINGLEMDS \
16158                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16159                      grep Blockcount | awk '{print $4}')
16160
16161         # Remove old files, some OI blocks will become idle.
16162         unlinkmany $myDIR/t- 10000
16163
16164         # stop the MDT
16165         stop $SINGLEMDS || error "Fail to stop MDT."
16166         # remount the MDT
16167         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16168
16169         df $MOUNT || error "Fail to df."
16170         # Create new files, idle OI blocks should be reused.
16171         createmany -o $myDIR/t- 2000
16172         do_facet $SINGLEMDS sync
16173         # Make sure journal flushed.
16174         sleep 6
16175         local blk2=$(do_facet $SINGLEMDS \
16176                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16177                      grep Blockcount | awk '{print $4}')
16178
16179         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16180 }
16181 run_test 228b "idle OI blocks can be reused after MDT restart"
16182
16183 #LU-1881
16184 test_228c() {
16185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16186         remote_mds_nodsh && skip "remote MDS with nodsh"
16187         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16188
16189         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16190         local myDIR=$DIR/$tdir
16191
16192         mkdir -p $myDIR
16193         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16194         $LCTL set_param fail_loc=0x80001002
16195         # 20000 files can guarantee there are index nodes in the OI file
16196         createmany -o $myDIR/t- 20000
16197         $LCTL set_param fail_loc=0
16198         # The guard is current the largest FID holder
16199         touch $myDIR/guard
16200         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16201                     tr -d '[')
16202         local IDX=$(($SEQ % 64))
16203
16204         do_facet $SINGLEMDS sync
16205         # Make sure journal flushed.
16206         sleep 6
16207         local blk1=$(do_facet $SINGLEMDS \
16208                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16209                      grep Blockcount | awk '{print $4}')
16210
16211         # Remove old files, some OI blocks will become idle.
16212         unlinkmany $myDIR/t- 20000
16213         rm -f $myDIR/guard
16214         # The OI file should become empty now
16215
16216         # Create new files, idle OI blocks should be reused.
16217         createmany -o $myDIR/t- 2000
16218         do_facet $SINGLEMDS sync
16219         # Make sure journal flushed.
16220         sleep 6
16221         local blk2=$(do_facet $SINGLEMDS \
16222                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16223                      grep Blockcount | awk '{print $4}')
16224
16225         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16226 }
16227 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16228
16229 test_229() { # LU-2482, LU-3448
16230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16231         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16232         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16233                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16234
16235         rm -f $DIR/$tfile
16236
16237         # Create a file with a released layout and stripe count 2.
16238         $MULTIOP $DIR/$tfile H2c ||
16239                 error "failed to create file with released layout"
16240
16241         $GETSTRIPE -v $DIR/$tfile
16242
16243         local pattern=$($GETSTRIPE -L $DIR/$tfile)
16244         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16245
16246         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
16247         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16248         stat $DIR/$tfile || error "failed to stat released file"
16249
16250         chown $RUNAS_ID $DIR/$tfile ||
16251                 error "chown $RUNAS_ID $DIR/$tfile failed"
16252
16253         chgrp $RUNAS_ID $DIR/$tfile ||
16254                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16255
16256         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16257         rm $DIR/$tfile || error "failed to remove released file"
16258 }
16259 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16260
16261 test_230a() {
16262         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16263         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16264         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16265                 skip "Need MDS version at least 2.11.52"
16266
16267         local MDTIDX=1
16268
16269         test_mkdir $DIR/$tdir
16270         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16271         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16272         [ $mdt_idx -ne 0 ] &&
16273                 error "create local directory on wrong MDT $mdt_idx"
16274
16275         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16276                         error "create remote directory failed"
16277         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16278         [ $mdt_idx -ne $MDTIDX ] &&
16279                 error "create remote directory on wrong MDT $mdt_idx"
16280
16281         createmany -o $DIR/$tdir/test_230/t- 10 ||
16282                 error "create files on remote directory failed"
16283         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16284         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16285         rm -r $DIR/$tdir || error "unlink remote directory failed"
16286 }
16287 run_test 230a "Create remote directory and files under the remote directory"
16288
16289 test_230b() {
16290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16291         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16292         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16293                 skip "Need MDS version at least 2.11.52"
16294
16295         local MDTIDX=1
16296         local mdt_index
16297         local i
16298         local file
16299         local pid
16300         local stripe_count
16301         local migrate_dir=$DIR/$tdir/migrate_dir
16302         local other_dir=$DIR/$tdir/other_dir
16303
16304         test_mkdir $DIR/$tdir
16305         test_mkdir -i0 -c1 $migrate_dir
16306         test_mkdir -i0 -c1 $other_dir
16307         for ((i=0; i<10; i++)); do
16308                 mkdir -p $migrate_dir/dir_${i}
16309                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16310                         error "create files under remote dir failed $i"
16311         done
16312
16313         cp /etc/passwd $migrate_dir/$tfile
16314         cp /etc/passwd $other_dir/$tfile
16315         chattr +SAD $migrate_dir
16316         chattr +SAD $migrate_dir/$tfile
16317
16318         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16319         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16320         local old_dir_mode=$(stat -c%f $migrate_dir)
16321         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16322
16323         mkdir -p $migrate_dir/dir_default_stripe2
16324         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
16325         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
16326
16327         mkdir -p $other_dir
16328         ln $migrate_dir/$tfile $other_dir/luna
16329         ln $migrate_dir/$tfile $migrate_dir/sofia
16330         ln $other_dir/$tfile $migrate_dir/david
16331         ln -s $migrate_dir/$tfile $other_dir/zachary
16332         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16333         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16334
16335         local len
16336         local lnktgt
16337
16338         # inline symlink
16339         for len in 58 59 60; do
16340                 lnktgt=$(str_repeat 'l' $len)
16341                 touch $migrate_dir/$lnktgt
16342                 ln -s $lnktgt $migrate_dir/${len}char_ln
16343         done
16344
16345         # PATH_MAX
16346         for len in 4094 4095; do
16347                 lnktgt=$(str_repeat 'l' $len)
16348                 ln -s $lnktgt $migrate_dir/${len}char_ln
16349         done
16350
16351         # NAME_MAX
16352         for len in 254 255; do
16353                 touch $migrate_dir/$(str_repeat 'l' $len)
16354         done
16355
16356         $LFS migrate -m $MDTIDX $migrate_dir ||
16357                 error "fails on migrating remote dir to MDT1"
16358
16359         echo "migratate to MDT1, then checking.."
16360         for ((i = 0; i < 10; i++)); do
16361                 for file in $(find $migrate_dir/dir_${i}); do
16362                         mdt_index=$($LFS getstripe -m $file)
16363                         # broken symlink getstripe will fail
16364                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16365                                 error "$file is not on MDT${MDTIDX}"
16366                 done
16367         done
16368
16369         # the multiple link file should still in MDT0
16370         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16371         [ $mdt_index == 0 ] ||
16372                 error "$file is not on MDT${MDTIDX}"
16373
16374         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16375         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16376                 error " expect $old_dir_flag get $new_dir_flag"
16377
16378         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16379         [ "$old_file_flag" = "$new_file_flag" ] ||
16380                 error " expect $old_file_flag get $new_file_flag"
16381
16382         local new_dir_mode=$(stat -c%f $migrate_dir)
16383         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16384                 error "expect mode $old_dir_mode get $new_dir_mode"
16385
16386         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16387         [ "$old_file_mode" = "$new_file_mode" ] ||
16388                 error "expect mode $old_file_mode get $new_file_mode"
16389
16390         diff /etc/passwd $migrate_dir/$tfile ||
16391                 error "$tfile different after migration"
16392
16393         diff /etc/passwd $other_dir/luna ||
16394                 error "luna different after migration"
16395
16396         diff /etc/passwd $migrate_dir/sofia ||
16397                 error "sofia different after migration"
16398
16399         diff /etc/passwd $migrate_dir/david ||
16400                 error "david different after migration"
16401
16402         diff /etc/passwd $other_dir/zachary ||
16403                 error "zachary different after migration"
16404
16405         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16406                 error "${tfile}_ln different after migration"
16407
16408         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16409                 error "${tfile}_ln_other different after migration"
16410
16411         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16412         [ $stripe_count = 2 ] ||
16413                 error "dir strpe_count $d != 2 after migration."
16414
16415         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16416         [ $stripe_count = 2 ] ||
16417                 error "file strpe_count $d != 2 after migration."
16418
16419         #migrate back to MDT0
16420         MDTIDX=0
16421
16422         $LFS migrate -m $MDTIDX $migrate_dir ||
16423                 error "fails on migrating remote dir to MDT0"
16424
16425         echo "migrate back to MDT0, checking.."
16426         for file in $(find $migrate_dir); do
16427                 mdt_index=$($LFS getstripe -m $file)
16428                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16429                         error "$file is not on MDT${MDTIDX}"
16430         done
16431
16432         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16433         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16434                 error " expect $old_dir_flag get $new_dir_flag"
16435
16436         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16437         [ "$old_file_flag" = "$new_file_flag" ] ||
16438                 error " expect $old_file_flag get $new_file_flag"
16439
16440         local new_dir_mode=$(stat -c%f $migrate_dir)
16441         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16442                 error "expect mode $old_dir_mode get $new_dir_mode"
16443
16444         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16445         [ "$old_file_mode" = "$new_file_mode" ] ||
16446                 error "expect mode $old_file_mode get $new_file_mode"
16447
16448         diff /etc/passwd ${migrate_dir}/$tfile ||
16449                 error "$tfile different after migration"
16450
16451         diff /etc/passwd ${other_dir}/luna ||
16452                 error "luna different after migration"
16453
16454         diff /etc/passwd ${migrate_dir}/sofia ||
16455                 error "sofia different after migration"
16456
16457         diff /etc/passwd ${other_dir}/zachary ||
16458                 error "zachary different after migration"
16459
16460         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16461                 error "${tfile}_ln different after migration"
16462
16463         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16464                 error "${tfile}_ln_other different after migration"
16465
16466         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16467         [ $stripe_count = 2 ] ||
16468                 error "dir strpe_count $d != 2 after migration."
16469
16470         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16471         [ $stripe_count = 2 ] ||
16472                 error "file strpe_count $d != 2 after migration."
16473
16474         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16475 }
16476 run_test 230b "migrate directory"
16477
16478 test_230c() {
16479         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16480         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16481         remote_mds_nodsh && skip "remote MDS with nodsh"
16482         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16483                 skip "Need MDS version at least 2.11.52"
16484
16485         local MDTIDX=1
16486         local total=3
16487         local mdt_index
16488         local file
16489         local migrate_dir=$DIR/$tdir/migrate_dir
16490
16491         #If migrating directory fails in the middle, all entries of
16492         #the directory is still accessiable.
16493         test_mkdir $DIR/$tdir
16494         test_mkdir -i0 -c1 $migrate_dir
16495         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16496         stat $migrate_dir
16497         createmany -o $migrate_dir/f $total ||
16498                 error "create files under ${migrate_dir} failed"
16499
16500         # fail after migrating top dir, and this will fail only once, so the
16501         # first sub file migration will fail (currently f3), others succeed.
16502         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16503         do_facet mds1 lctl set_param fail_loc=0x1801
16504         local t=$(ls $migrate_dir | wc -l)
16505         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16506                 error "migrate should fail"
16507         local u=$(ls $migrate_dir | wc -l)
16508         [ "$u" == "$t" ] || error "$u != $t during migration"
16509
16510         # add new dir/file should succeed
16511         mkdir $migrate_dir/dir ||
16512                 error "mkdir failed under migrating directory"
16513         touch $migrate_dir/file ||
16514                 error "create file failed under migrating directory"
16515
16516         # add file with existing name should fail
16517         for file in $migrate_dir/f*; do
16518                 stat $file > /dev/null || error "stat $file failed"
16519                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16520                         error "open(O_CREAT|O_EXCL) $file should fail"
16521                 $MULTIOP $file m && error "create $file should fail"
16522                 touch $DIR/$tdir/remote_dir/$tfile ||
16523                         error "touch $tfile failed"
16524                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16525                         error "link $file should fail"
16526                 mdt_index=$($LFS getstripe -m $file)
16527                 if [ $mdt_index == 0 ]; then
16528                         # file failed to migrate is not allowed to rename to
16529                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16530                                 error "rename to $file should fail"
16531                 else
16532                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16533                                 error "rename to $file failed"
16534                 fi
16535                 echo hello >> $file || error "write $file failed"
16536         done
16537
16538         # resume migration with different options should fail
16539         $LFS migrate -m 0 $migrate_dir &&
16540                 error "migrate -m 0 $migrate_dir should fail"
16541
16542         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16543                 error "migrate -c 2 $migrate_dir should fail"
16544
16545         # resume migration should succeed
16546         $LFS migrate -m $MDTIDX $migrate_dir ||
16547                 error "migrate $migrate_dir failed"
16548
16549         echo "Finish migration, then checking.."
16550         for file in $(find $migrate_dir); do
16551                 mdt_index=$($LFS getstripe -m $file)
16552                 [ $mdt_index == $MDTIDX ] ||
16553                         error "$file is not on MDT${MDTIDX}"
16554         done
16555
16556         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16557 }
16558 run_test 230c "check directory accessiblity if migration failed"
16559
16560 test_230d() {
16561         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16562         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16563         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16564                 skip "Need MDS version at least 2.11.52"
16565         # LU-11235
16566         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16567
16568         local migrate_dir=$DIR/$tdir/migrate_dir
16569         local old_index
16570         local new_index
16571         local old_count
16572         local new_count
16573         local new_hash
16574         local mdt_index
16575         local i
16576         local j
16577
16578         old_index=$((RANDOM % MDSCOUNT))
16579         old_count=$((MDSCOUNT - old_index))
16580         new_index=$((RANDOM % MDSCOUNT))
16581         new_count=$((MDSCOUNT - new_index))
16582         new_hash="all_char"
16583
16584         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16585         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16586
16587         test_mkdir $DIR/$tdir
16588         test_mkdir -i $old_index -c $old_count $migrate_dir
16589
16590         for ((i=0; i<100; i++)); do
16591                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16592                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16593                         error "create files under remote dir failed $i"
16594         done
16595
16596         echo -n "Migrate from MDT$old_index "
16597         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16598         echo -n "to MDT$new_index"
16599         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16600         echo
16601
16602         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16603         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16604                 error "migrate remote dir error"
16605
16606         echo "Finish migration, then checking.."
16607         for file in $(find $migrate_dir); do
16608                 mdt_index=$($LFS getstripe -m $file)
16609                 if [ $mdt_index -lt $new_index ] ||
16610                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16611                         error "$file is on MDT$mdt_index"
16612                 fi
16613         done
16614
16615         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16616 }
16617 run_test 230d "check migrate big directory"
16618
16619 test_230e() {
16620         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16621         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16622         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16623                 skip "Need MDS version at least 2.11.52"
16624
16625         local i
16626         local j
16627         local a_fid
16628         local b_fid
16629
16630         mkdir -p $DIR/$tdir
16631         mkdir $DIR/$tdir/migrate_dir
16632         mkdir $DIR/$tdir/other_dir
16633         touch $DIR/$tdir/migrate_dir/a
16634         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16635         ls $DIR/$tdir/other_dir
16636
16637         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16638                 error "migrate dir fails"
16639
16640         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16641         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16642
16643         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16644         [ $mdt_index == 0 ] || error "a is not on MDT0"
16645
16646         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16647                 error "migrate dir fails"
16648
16649         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16650         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16651
16652         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16653         [ $mdt_index == 1 ] || error "a is not on MDT1"
16654
16655         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16656         [ $mdt_index == 1 ] || error "b is not on MDT1"
16657
16658         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16659         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16660
16661         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16662
16663         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16664 }
16665 run_test 230e "migrate mulitple local link files"
16666
16667 test_230f() {
16668         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16669         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16670         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16671                 skip "Need MDS version at least 2.11.52"
16672
16673         local a_fid
16674         local ln_fid
16675
16676         mkdir -p $DIR/$tdir
16677         mkdir $DIR/$tdir/migrate_dir
16678         $LFS mkdir -i1 $DIR/$tdir/other_dir
16679         touch $DIR/$tdir/migrate_dir/a
16680         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16681         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16682         ls $DIR/$tdir/other_dir
16683
16684         # a should be migrated to MDT1, since no other links on MDT0
16685         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16686                 error "#1 migrate dir fails"
16687         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16688         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16689         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16690         [ $mdt_index == 1 ] || error "a is not on MDT1"
16691
16692         # a should stay on MDT1, because it is a mulitple link file
16693         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16694                 error "#2 migrate dir fails"
16695         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16696         [ $mdt_index == 1 ] || error "a is not on MDT1"
16697
16698         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16699                 error "#3 migrate dir fails"
16700
16701         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16702         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16703         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16704
16705         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16706         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16707
16708         # a should be migrated to MDT0, since no other links on MDT1
16709         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16710                 error "#4 migrate dir fails"
16711         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16712         [ $mdt_index == 0 ] || error "a is not on MDT0"
16713
16714         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16715 }
16716 run_test 230f "migrate mulitple remote link files"
16717
16718 test_230g() {
16719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16720         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16721         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16722                 skip "Need MDS version at least 2.11.52"
16723
16724         mkdir -p $DIR/$tdir/migrate_dir
16725
16726         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16727                 error "migrating dir to non-exist MDT succeeds"
16728         true
16729 }
16730 run_test 230g "migrate dir to non-exist MDT"
16731
16732 test_230h() {
16733         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16734         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16735         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16736                 skip "Need MDS version at least 2.11.52"
16737
16738         local mdt_index
16739
16740         mkdir -p $DIR/$tdir/migrate_dir
16741
16742         $LFS migrate -m1 $DIR &&
16743                 error "migrating mountpoint1 should fail"
16744
16745         $LFS migrate -m1 $DIR/$tdir/.. &&
16746                 error "migrating mountpoint2 should fail"
16747
16748         # same as mv
16749         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
16750                 error "migrating $tdir/migrate_dir/.. should fail"
16751
16752         true
16753 }
16754 run_test 230h "migrate .. and root"
16755
16756 test_230i() {
16757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16758         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16759         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16760                 skip "Need MDS version at least 2.11.52"
16761
16762         mkdir -p $DIR/$tdir/migrate_dir
16763
16764         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
16765                 error "migration fails with a tailing slash"
16766
16767         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
16768                 error "migration fails with two tailing slashes"
16769 }
16770 run_test 230i "lfs migrate -m tolerates trailing slashes"
16771
16772 test_230j() {
16773         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16774         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16775                 skip "Need MDS version at least 2.11.52"
16776
16777         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
16778         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
16779                 error "create $tfile failed"
16780         cat /etc/passwd > $DIR/$tdir/$tfile
16781
16782         $LFS migrate -m 1 $DIR/$tdir
16783
16784         cmp /etc/passwd $DIR/$tdir/$tfile ||
16785                 error "DoM file mismatch after migration"
16786 }
16787 run_test 230j "DoM file data not changed after dir migration"
16788
16789 test_230k() {
16790         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
16791         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16792                 skip "Need MDS version at least 2.11.56"
16793
16794         local total=20
16795         local files_on_starting_mdt=0
16796
16797         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
16798         $LFS getdirstripe $DIR/$tdir
16799         for i in $(seq $total); do
16800                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
16801                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16802                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16803         done
16804
16805         echo "$files_on_starting_mdt files on MDT0"
16806
16807         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
16808         $LFS getdirstripe $DIR/$tdir
16809
16810         files_on_starting_mdt=0
16811         for i in $(seq $total); do
16812                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16813                         error "file $tfile.$i mismatch after migration"
16814                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
16815                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16816         done
16817
16818         echo "$files_on_starting_mdt files on MDT1 after migration"
16819         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
16820
16821         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
16822         $LFS getdirstripe $DIR/$tdir
16823
16824         files_on_starting_mdt=0
16825         for i in $(seq $total); do
16826                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16827                         error "file $tfile.$i mismatch after 2nd migration"
16828                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16829                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16830         done
16831
16832         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
16833         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
16834
16835         true
16836 }
16837 run_test 230k "file data not changed after dir migration"
16838
16839 test_230l() {
16840         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16841         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16842                 skip "Need MDS version at least 2.11.56"
16843
16844         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
16845         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
16846                 error "create files under remote dir failed $i"
16847         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
16848 }
16849 run_test 230l "readdir between MDTs won't crash"
16850
16851 test_230m() {
16852         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16853         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16854                 skip "Need MDS version at least 2.11.56"
16855
16856         local MDTIDX=1
16857         local mig_dir=$DIR/$tdir/migrate_dir
16858         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16859         local shortstr="b"
16860         local val
16861
16862         echo "Creating files and dirs with xattrs"
16863         test_mkdir $DIR/$tdir
16864         test_mkdir -i0 -c1 $mig_dir
16865         mkdir $mig_dir/dir
16866         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
16867                 error "cannot set xattr attr1 on dir"
16868         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
16869                 error "cannot set xattr attr2 on dir"
16870         touch $mig_dir/dir/f0
16871         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
16872                 error "cannot set xattr attr1 on file"
16873         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
16874                 error "cannot set xattr attr2 on file"
16875         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16876         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16877         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
16878         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16879         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
16880         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16881         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
16882         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16883         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
16884
16885         echo "Migrating to MDT1"
16886         $LFS migrate -m $MDTIDX $mig_dir ||
16887                 error "fails on migrating dir to MDT1"
16888
16889         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16890         echo "Checking xattrs"
16891         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16892         [ "$val" = $longstr ] ||
16893                 error "expecting xattr1 $longstr on dir, found $val"
16894         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16895         [ "$val" = $shortstr ] ||
16896                 error "expecting xattr2 $shortstr on dir, found $val"
16897         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16898         [ "$val" = $longstr ] ||
16899                 error "expecting xattr1 $longstr on file, found $val"
16900         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16901         [ "$val" = $shortstr ] ||
16902                 error "expecting xattr2 $shortstr on file, found $val"
16903 }
16904 run_test 230m "xattrs not changed after dir migration"
16905
16906 test_230r() {
16907         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16908         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
16909         [[ $MDS1_VERSION -ge $(version_code 2.12.5) ]] ||
16910                 skip "Need MDS version at least 2.12.5"
16911
16912         # maximum amount of local locks:
16913         # parent striped dir - 2 locks
16914         # new stripe in parent to migrate to - 1 lock
16915         # source and target - 2 locks
16916         # Total 5 locks for regular file
16917         mkdir -p $DIR/$tdir
16918         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
16919         touch $DIR/$tdir/dir1/eee
16920
16921         # create 4 hardlink for 4 more locks
16922         # Total: 9 locks > RS_MAX_LOCKS (8)
16923         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
16924         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
16925         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
16926         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
16927         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
16928         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
16929         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
16930         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
16931
16932         cancel_lru_locks mdc
16933
16934         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
16935                 error "migrate dir fails"
16936
16937         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16938 }
16939 run_test 230r "migrate with too many local locks"
16940
16941 test_231a()
16942 {
16943         # For simplicity this test assumes that max_pages_per_rpc
16944         # is the same across all OSCs
16945         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
16946         local bulk_size=$((max_pages * PAGE_SIZE))
16947         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
16948                                        head -n 1)
16949
16950         mkdir -p $DIR/$tdir
16951         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
16952                 error "failed to set stripe with -S ${brw_size}M option"
16953
16954         # clear the OSC stats
16955         $LCTL set_param osc.*.stats=0 &>/dev/null
16956         stop_writeback
16957
16958         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
16959         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
16960                 oflag=direct &>/dev/null || error "dd failed"
16961
16962         sync; sleep 1; sync # just to be safe
16963         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
16964         if [ x$nrpcs != "x1" ]; then
16965                 $LCTL get_param osc.*.stats
16966                 error "found $nrpcs ost_write RPCs, not 1 as expected"
16967         fi
16968
16969         start_writeback
16970         # Drop the OSC cache, otherwise we will read from it
16971         cancel_lru_locks osc
16972
16973         # clear the OSC stats
16974         $LCTL set_param osc.*.stats=0 &>/dev/null
16975
16976         # Client reads $bulk_size.
16977         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
16978                 iflag=direct &>/dev/null || error "dd failed"
16979
16980         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
16981         if [ x$nrpcs != "x1" ]; then
16982                 $LCTL get_param osc.*.stats
16983                 error "found $nrpcs ost_read RPCs, not 1 as expected"
16984         fi
16985 }
16986 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
16987
16988 test_231b() {
16989         mkdir -p $DIR/$tdir
16990         local i
16991         for i in {0..1023}; do
16992                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
16993                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
16994                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
16995         done
16996         sync
16997 }
16998 run_test 231b "must not assert on fully utilized OST request buffer"
16999
17000 test_232a() {
17001         mkdir -p $DIR/$tdir
17002         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17003
17004         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17005         do_facet ost1 $LCTL set_param fail_loc=0x31c
17006
17007         # ignore dd failure
17008         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17009
17010         do_facet ost1 $LCTL set_param fail_loc=0
17011         umount_client $MOUNT || error "umount failed"
17012         mount_client $MOUNT || error "mount failed"
17013         stop ost1 || error "cannot stop ost1"
17014         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17015 }
17016 run_test 232a "failed lock should not block umount"
17017
17018 test_232b() {
17019         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17020                 skip "Need MDS version at least 2.10.58"
17021
17022         mkdir -p $DIR/$tdir
17023         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17024         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17025         sync
17026         cancel_lru_locks osc
17027
17028         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17029         do_facet ost1 $LCTL set_param fail_loc=0x31c
17030
17031         # ignore failure
17032         $LFS data_version $DIR/$tdir/$tfile || true
17033
17034         do_facet ost1 $LCTL set_param fail_loc=0
17035         umount_client $MOUNT || error "umount failed"
17036         mount_client $MOUNT || error "mount failed"
17037         stop ost1 || error "cannot stop ost1"
17038         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17039 }
17040 run_test 232b "failed data version lock should not block umount"
17041
17042 test_233a() {
17043         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17044                 skip "Need MDS version at least 2.3.64"
17045         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17046
17047         local fid=$($LFS path2fid $MOUNT)
17048
17049         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17050                 error "cannot access $MOUNT using its FID '$fid'"
17051 }
17052 run_test 233a "checking that OBF of the FS root succeeds"
17053
17054 test_233b() {
17055         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17056                 skip "Need MDS version at least 2.5.90"
17057         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17058
17059         local fid=$($LFS path2fid $MOUNT/.lustre)
17060
17061         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17062                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17063
17064         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17065         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17066                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17067 }
17068 run_test 233b "checking that OBF of the FS .lustre succeeds"
17069
17070 test_234() {
17071         local p="$TMP/sanityN-$TESTNAME.parameters"
17072         save_lustre_params client "llite.*.xattr_cache" > $p
17073         lctl set_param llite.*.xattr_cache 1 ||
17074                 skip_env "xattr cache is not supported"
17075
17076         mkdir -p $DIR/$tdir || error "mkdir failed"
17077         touch $DIR/$tdir/$tfile || error "touch failed"
17078         # OBD_FAIL_LLITE_XATTR_ENOMEM
17079         $LCTL set_param fail_loc=0x1405
17080         getfattr -n user.attr $DIR/$tdir/$tfile &&
17081                 error "getfattr should have failed with ENOMEM"
17082         $LCTL set_param fail_loc=0x0
17083         rm -rf $DIR/$tdir
17084
17085         restore_lustre_params < $p
17086         rm -f $p
17087 }
17088 run_test 234 "xattr cache should not crash on ENOMEM"
17089
17090 test_235() {
17091         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
17092                 skip "Need MDS version at least 2.4.52"
17093
17094         flock_deadlock $DIR/$tfile
17095         local RC=$?
17096         case $RC in
17097                 0)
17098                 ;;
17099                 124) error "process hangs on a deadlock"
17100                 ;;
17101                 *) error "error executing flock_deadlock $DIR/$tfile"
17102                 ;;
17103         esac
17104 }
17105 run_test 235 "LU-1715: flock deadlock detection does not work properly"
17106
17107 #LU-2935
17108 test_236() {
17109         check_swap_layouts_support
17110
17111         local ref1=/etc/passwd
17112         local ref2=/etc/group
17113         local file1=$DIR/$tdir/f1
17114         local file2=$DIR/$tdir/f2
17115
17116         test_mkdir -c1 $DIR/$tdir
17117         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
17118         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
17119         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
17120         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
17121         local fd=$(free_fd)
17122         local cmd="exec $fd<>$file2"
17123         eval $cmd
17124         rm $file2
17125         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
17126                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
17127         cmd="exec $fd>&-"
17128         eval $cmd
17129         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17130
17131         #cleanup
17132         rm -rf $DIR/$tdir
17133 }
17134 run_test 236 "Layout swap on open unlinked file"
17135
17136 # LU-4659 linkea consistency
17137 test_238() {
17138         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17139                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17140                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17141                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17142
17143         touch $DIR/$tfile
17144         ln $DIR/$tfile $DIR/$tfile.lnk
17145         touch $DIR/$tfile.new
17146         mv $DIR/$tfile.new $DIR/$tfile
17147         local fid1=$($LFS path2fid $DIR/$tfile)
17148         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17149         local path1=$($LFS fid2path $FSNAME "$fid1")
17150         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17151         local path2=$($LFS fid2path $FSNAME "$fid2")
17152         [ $tfile.lnk == $path2 ] ||
17153                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17154         rm -f $DIR/$tfile*
17155 }
17156 run_test 238 "Verify linkea consistency"
17157
17158 test_239A() { # was test_239
17159         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17160                 skip "Need MDS version at least 2.5.60"
17161
17162         local list=$(comma_list $(mdts_nodes))
17163
17164         mkdir -p $DIR/$tdir
17165         createmany -o $DIR/$tdir/f- 5000
17166         unlinkmany $DIR/$tdir/f- 5000
17167         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17168                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17169         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17170                         osp.*MDT*.sync_in_flight" | calc_sum)
17171         [ "$changes" -eq 0 ] || error "$changes not synced"
17172 }
17173 run_test 239A "osp_sync test"
17174
17175 test_239a() { #LU-5297
17176         remote_mds_nodsh && skip "remote MDS with nodsh"
17177
17178         touch $DIR/$tfile
17179         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17180         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17181         chgrp $RUNAS_GID $DIR/$tfile
17182         wait_delete_completed
17183 }
17184 run_test 239a "process invalid osp sync record correctly"
17185
17186 test_239b() { #LU-5297
17187         remote_mds_nodsh && skip "remote MDS with nodsh"
17188
17189         touch $DIR/$tfile1
17190         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17191         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17192         chgrp $RUNAS_GID $DIR/$tfile1
17193         wait_delete_completed
17194         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17195         touch $DIR/$tfile2
17196         chgrp $RUNAS_GID $DIR/$tfile2
17197         wait_delete_completed
17198 }
17199 run_test 239b "process osp sync record with ENOMEM error correctly"
17200
17201 test_240() {
17202         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17203         remote_mds_nodsh && skip "remote MDS with nodsh"
17204
17205         mkdir -p $DIR/$tdir
17206
17207         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17208                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17209         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17210                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17211
17212         umount_client $MOUNT || error "umount failed"
17213         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17214         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17215         mount_client $MOUNT || error "failed to mount client"
17216
17217         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17218         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17219 }
17220 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17221
17222 test_241_bio() {
17223         local count=$1
17224         local bsize=$2
17225
17226         for LOOP in $(seq $count); do
17227                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17228                 cancel_lru_locks $OSC || true
17229         done
17230 }
17231
17232 test_241_dio() {
17233         local count=$1
17234         local bsize=$2
17235
17236         for LOOP in $(seq $1); do
17237                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17238                         2>/dev/null
17239         done
17240 }
17241
17242 test_241a() { # was test_241
17243         local bsize=$PAGE_SIZE
17244
17245         (( bsize < 40960 )) && bsize=40960
17246         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17247         ls -la $DIR/$tfile
17248         cancel_lru_locks $OSC
17249         test_241_bio 1000 $bsize &
17250         PID=$!
17251         test_241_dio 1000 $bsize
17252         wait $PID
17253 }
17254 run_test 241a "bio vs dio"
17255
17256 test_241b() {
17257         local bsize=$PAGE_SIZE
17258
17259         (( bsize < 40960 )) && bsize=40960
17260         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17261         ls -la $DIR/$tfile
17262         test_241_dio 1000 $bsize &
17263         PID=$!
17264         test_241_dio 1000 $bsize
17265         wait $PID
17266 }
17267 run_test 241b "dio vs dio"
17268
17269 test_242() {
17270         remote_mds_nodsh && skip "remote MDS with nodsh"
17271
17272         mkdir -p $DIR/$tdir
17273         touch $DIR/$tdir/$tfile
17274
17275         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17276         do_facet mds1 lctl set_param fail_loc=0x105
17277         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17278
17279         do_facet mds1 lctl set_param fail_loc=0
17280         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17281 }
17282 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17283
17284 test_243()
17285 {
17286         test_mkdir $DIR/$tdir
17287         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17288 }
17289 run_test 243 "various group lock tests"
17290
17291 test_244()
17292 {
17293         test_mkdir $DIR/$tdir
17294         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17295         sendfile_grouplock $DIR/$tdir/$tfile || \
17296                 error "sendfile+grouplock failed"
17297         rm -rf $DIR/$tdir
17298 }
17299 run_test 244 "sendfile with group lock tests"
17300
17301 test_245() {
17302         local flagname="multi_mod_rpcs"
17303         local connect_data_name="max_mod_rpcs"
17304         local out
17305
17306         # check if multiple modify RPCs flag is set
17307         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17308                 grep "connect_flags:")
17309         echo "$out"
17310
17311         echo "$out" | grep -qw $flagname
17312         if [ $? -ne 0 ]; then
17313                 echo "connect flag $flagname is not set"
17314                 return
17315         fi
17316
17317         # check if multiple modify RPCs data is set
17318         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17319         echo "$out"
17320
17321         echo "$out" | grep -qw $connect_data_name ||
17322                 error "import should have connect data $connect_data_name"
17323 }
17324 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17325
17326 test_246() { # LU-7371
17327         remote_ost_nodsh && skip "remote OST with nodsh"
17328         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
17329                 skip "Need OST version >= 2.7.62"
17330
17331         do_facet ost1 $LCTL set_param fail_val=4095
17332 #define OBD_FAIL_OST_READ_SIZE          0x234
17333         do_facet ost1 $LCTL set_param fail_loc=0x234
17334         $LFS setstripe $DIR/$tfile -i 0 -c 1
17335         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
17336         cancel_lru_locks $FSNAME-OST0000
17337         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
17338 }
17339 run_test 246 "Read file of size 4095 should return right length"
17340
17341 cleanup_247() {
17342         local submount=$1
17343
17344         trap 0
17345         umount_client $submount
17346         rmdir $submount
17347 }
17348
17349 test_247a() {
17350         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17351                 grep -q subtree ||
17352                 skip_env "Fileset feature is not supported"
17353
17354         local submount=${MOUNT}_$tdir
17355
17356         mkdir $MOUNT/$tdir
17357         mkdir -p $submount || error "mkdir $submount failed"
17358         FILESET="$FILESET/$tdir" mount_client $submount ||
17359                 error "mount $submount failed"
17360         trap "cleanup_247 $submount" EXIT
17361         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17362         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17363                 error "read $MOUNT/$tdir/$tfile failed"
17364         cleanup_247 $submount
17365 }
17366 run_test 247a "mount subdir as fileset"
17367
17368 test_247b() {
17369         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17370                 skip_env "Fileset feature is not supported"
17371
17372         local submount=${MOUNT}_$tdir
17373
17374         rm -rf $MOUNT/$tdir
17375         mkdir -p $submount || error "mkdir $submount failed"
17376         SKIP_FILESET=1
17377         FILESET="$FILESET/$tdir" mount_client $submount &&
17378                 error "mount $submount should fail"
17379         rmdir $submount
17380 }
17381 run_test 247b "mount subdir that dose not exist"
17382
17383 test_247c() {
17384         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17385                 skip_env "Fileset feature is not supported"
17386
17387         local submount=${MOUNT}_$tdir
17388
17389         mkdir -p $MOUNT/$tdir/dir1
17390         mkdir -p $submount || error "mkdir $submount failed"
17391         trap "cleanup_247 $submount" EXIT
17392         FILESET="$FILESET/$tdir" mount_client $submount ||
17393                 error "mount $submount failed"
17394         local fid=$($LFS path2fid $MOUNT/)
17395         $LFS fid2path $submount $fid && error "fid2path should fail"
17396         cleanup_247 $submount
17397 }
17398 run_test 247c "running fid2path outside root"
17399
17400 test_247d() {
17401         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17402                 skip "Fileset feature is not supported"
17403
17404         local submount=${MOUNT}_$tdir
17405
17406         mkdir -p $MOUNT/$tdir/dir1
17407         mkdir -p $submount || error "mkdir $submount failed"
17408         FILESET="$FILESET/$tdir" mount_client $submount ||
17409                 error "mount $submount failed"
17410         trap "cleanup_247 $submount" EXIT
17411         local fid=$($LFS path2fid $submount/dir1)
17412         $LFS fid2path $submount $fid || error "fid2path should succeed"
17413         cleanup_247 $submount
17414 }
17415 run_test 247d "running fid2path inside root"
17416
17417 # LU-8037
17418 test_247e() {
17419         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17420                 grep -q subtree ||
17421                 skip "Fileset feature is not supported"
17422
17423         local submount=${MOUNT}_$tdir
17424
17425         mkdir $MOUNT/$tdir
17426         mkdir -p $submount || error "mkdir $submount failed"
17427         FILESET="$FILESET/.." mount_client $submount &&
17428                 error "mount $submount should fail"
17429         rmdir $submount
17430 }
17431 run_test 247e "mount .. as fileset"
17432
17433 test_247f() {
17434         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17435         [ $MDS1_VERSION -lt $(version_code 2.12.6) ] &&
17436                 skip "Need at least version 2.12.6"
17437         [ $CLIENT_VERSION -lt $(version_code 2.12.6) ] &&
17438                 skip "Need at least version 2.12.6"
17439         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17440                 grep -q subtree ||
17441                 skip "Fileset feature is not supported"
17442
17443         mkdir $DIR/$tdir || error "mkdir $tdir failed"
17444         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
17445                 error "mkdir remote failed"
17446         mkdir $DIR/$tdir/remote/subdir || error "mkdir remote/subdir failed"
17447         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
17448                 error "mkdir striped failed"
17449         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
17450
17451         local submount=${MOUNT}_$tdir
17452
17453         mkdir -p $submount || error "mkdir $submount failed"
17454         stack_trap "rmdir $submount"
17455
17456         local dir
17457         local stat
17458         local fileset=$FILESET
17459         local mdts=$(comma_list $(mdts_nodes))
17460
17461         stat=$(do_facet mds1 $LCTL get_param -n \
17462                 mdt.*MDT0000.enable_remote_subdir_mount)
17463         stack_trap "do_nodes $mdts $LCTL set_param \
17464                 mdt.*.enable_remote_subdir_mount=$stat"
17465
17466         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
17467         stack_trap "umount_client $submount"
17468         FILESET="$fileset/$tdir/remote" mount_client $submount &&
17469                 error "mount remote dir $dir should fail"
17470
17471         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
17472                 $tdir/striped/. ; do
17473                 FILESET="$fileset/$dir" mount_client $submount ||
17474                         error "mount $dir failed"
17475                 umount_client $submount
17476         done
17477
17478         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
17479         FILESET="$fileset/$tdir/remote" mount_client $submount ||
17480                 error "mount $tdir/remote failed"
17481 }
17482 run_test 247f "mount striped or remote directory as fileset"
17483
17484 test_247g() {
17485         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
17486         [ $CLIENT_VERSION -lt $(version_code 2.12.6) ] &&
17487                 skip "Need at least version 2.12.6"
17488
17489         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
17490                 error "mkdir $tdir failed"
17491         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
17492
17493         local submount=${MOUNT}_$tdir
17494
17495         mkdir -p $submount || error "mkdir $submount failed"
17496         stack_trap "rmdir $submount"
17497
17498         FILESET="$fileset/$tdir" mount_client $submount ||
17499                 error "mount $dir failed"
17500         stack_trap "umount $submount"
17501
17502         local mdts=$(comma_list $(mdts_nodes))
17503
17504         local nrpcs
17505
17506         stat $submount > /dev/null
17507         cancel_lru_locks $MDC
17508         stat $submount > /dev/null
17509         stat $submount/$tfile > /dev/null
17510         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
17511         stat $submount/$tfile > /dev/null
17512         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
17513                 awk '/getattr/ {sum += $2} END {print sum}')
17514
17515         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
17516 }
17517 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
17518
17519 test_248() {
17520         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17521         [ -z "$fast_read_sav" ] && skip "no fast read support"
17522
17523         # create a large file for fast read verification
17524         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17525
17526         # make sure the file is created correctly
17527         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17528                 { rm -f $DIR/$tfile; skip "file creation error"; }
17529
17530         echo "Test 1: verify that fast read is 4 times faster on cache read"
17531
17532         # small read with fast read enabled
17533         $LCTL set_param -n llite.*.fast_read=1
17534         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17535                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17536                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17537         # small read with fast read disabled
17538         $LCTL set_param -n llite.*.fast_read=0
17539         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17540                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17541                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17542
17543         # verify that fast read is 4 times faster for cache read
17544         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17545                 error_not_in_vm "fast read was not 4 times faster: " \
17546                            "$t_fast vs $t_slow"
17547
17548         echo "Test 2: verify the performance between big and small read"
17549         $LCTL set_param -n llite.*.fast_read=1
17550
17551         # 1k non-cache read
17552         cancel_lru_locks osc
17553         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17554                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17555                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17556
17557         # 1M non-cache read
17558         cancel_lru_locks osc
17559         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17560                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17561                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17562
17563         # verify that big IO is not 4 times faster than small IO
17564         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17565                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17566
17567         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17568         rm -f $DIR/$tfile
17569 }
17570 run_test 248 "fast read verification"
17571
17572 test_249() { # LU-7890
17573         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17574                 skip "Need at least version 2.8.54"
17575
17576         rm -f $DIR/$tfile
17577         $SETSTRIPE -c 1 $DIR/$tfile
17578         # Offset 2T == 4k * 512M
17579         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17580                 error "dd to 2T offset failed"
17581 }
17582 run_test 249 "Write above 2T file size"
17583
17584 test_250() {
17585         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
17586          && skip "no 16TB file size limit on ZFS"
17587
17588         $SETSTRIPE -c 1 $DIR/$tfile
17589         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17590         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17591         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17592         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17593                 conv=notrunc,fsync && error "append succeeded"
17594         return 0
17595 }
17596 run_test 250 "Write above 16T limit"
17597
17598 test_251() {
17599         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
17600
17601         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17602         #Skip once - writing the first stripe will succeed
17603         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17604         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17605                 error "short write happened"
17606
17607         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17608         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17609                 error "short read happened"
17610
17611         rm -f $DIR/$tfile
17612 }
17613 run_test 251 "Handling short read and write correctly"
17614
17615 test_252() {
17616         remote_mds_nodsh && skip "remote MDS with nodsh"
17617         remote_ost_nodsh && skip "remote OST with nodsh"
17618         if [ "$ost1_FSTYPE" != "ldiskfs" -o "$mds1_FSTYPE" != "ldiskfs" ]; then
17619                 skip_env "ldiskfs only test"
17620         fi
17621
17622         local tgt
17623         local dev
17624         local out
17625         local uuid
17626         local num
17627         local gen
17628
17629         # check lr_reader on OST0000
17630         tgt=ost1
17631         dev=$(facet_device $tgt)
17632         out=$(do_facet $tgt $LR_READER $dev)
17633         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17634         echo "$out"
17635         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17636         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17637                 error "Invalid uuid returned by $LR_READER on target $tgt"
17638         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17639
17640         # check lr_reader -c on MDT0000
17641         tgt=mds1
17642         dev=$(facet_device $tgt)
17643         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17644                 skip "$LR_READER does not support additional options"
17645         fi
17646         out=$(do_facet $tgt $LR_READER -c $dev)
17647         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17648         echo "$out"
17649         num=$(echo "$out" | grep -c "mdtlov")
17650         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17651                 error "Invalid number of mdtlov clients returned by $LR_READER"
17652         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17653
17654         # check lr_reader -cr on MDT0000
17655         out=$(do_facet $tgt $LR_READER -cr $dev)
17656         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17657         echo "$out"
17658         echo "$out" | grep -q "^reply_data:$" ||
17659                 error "$LR_READER should have returned 'reply_data' section"
17660         num=$(echo "$out" | grep -c "client_generation")
17661         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17662 }
17663 run_test 252 "check lr_reader tool"
17664
17665 test_253_fill_ost() {
17666         local size_mb #how many MB should we write to pass watermark
17667         local lwm=$3  #low watermark
17668         local free_10mb #10% of free space
17669
17670         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17671         size_mb=$((free_kb / 1024 - lwm))
17672         free_10mb=$((free_kb / 10240))
17673         #If 10% of free space cross low watermark use it
17674         if (( free_10mb > size_mb )); then
17675                 size_mb=$free_10mb
17676         else
17677                 #At least we need to store 1.1 of difference between
17678                 #free space and low watermark
17679                 size_mb=$((size_mb + size_mb / 10))
17680         fi
17681         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
17682                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
17683                          oflag=append conv=notrunc
17684         fi
17685
17686         sleep_maxage
17687
17688         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17689         echo "OST still has $((free_kb / 1024)) mbytes free"
17690 }
17691
17692 test_253() {
17693         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17694         remote_mds_nodsh && skip "remote MDS with nodsh"
17695         remote_mgs_nodsh && skip "remote MGS with nodsh"
17696
17697         local ostidx=0
17698         local rc=0
17699
17700         local ost_name=$($LFS osts |
17701                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
17702         # on the mdt's osc
17703         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17704         do_facet $SINGLEMDS $LCTL get_param -n \
17705                 osp.$mdtosc_proc1.reserved_mb_high ||
17706                 skip  "remote MDS does not support reserved_mb_high"
17707
17708         rm -rf $DIR/$tdir
17709         wait_mds_ost_sync
17710         wait_delete_completed
17711         mkdir $DIR/$tdir
17712
17713         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
17714                         osp.$mdtosc_proc1.reserved_mb_high)
17715         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
17716                         osp.$mdtosc_proc1.reserved_mb_low)
17717         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
17718
17719         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
17720         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
17721                 error "Adding $ost_name to pool failed"
17722
17723         # Wait for client to see a OST at pool
17724         wait_update $HOSTNAME "$LCTL get_param -n
17725                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
17726                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
17727                 error "Client can not see the pool"
17728         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17729                 error "Setstripe failed"
17730
17731         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
17732         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
17733         echo "OST still has $((blocks/1024)) mbytes free"
17734
17735         local new_lwm=$((blocks/1024-10))
17736         do_facet $SINGLEMDS $LCTL set_param \
17737                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
17738         do_facet $SINGLEMDS $LCTL set_param \
17739                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
17740
17741         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17742
17743         #First enospc could execute orphan deletion so repeat.
17744         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17745
17746         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17747                         osp.$mdtosc_proc1.prealloc_status)
17748         echo "prealloc_status $oa_status"
17749
17750         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
17751                 error "File creation should fail"
17752         #object allocation was stopped, but we still able to append files
17753         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
17754                 error "Append failed"
17755         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
17756
17757         wait_delete_completed
17758
17759         sleep_maxage
17760
17761         for i in $(seq 10 12); do
17762                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
17763                         error "File creation failed after rm";
17764         done
17765
17766         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17767                         osp.$mdtosc_proc1.prealloc_status)
17768         echo "prealloc_status $oa_status"
17769
17770         if (( oa_status != 0 )); then
17771                 error "Object allocation still disable after rm"
17772         fi
17773         do_facet $SINGLEMDS $LCTL set_param \
17774                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
17775         do_facet $SINGLEMDS $LCTL set_param \
17776                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
17777
17778
17779         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
17780                 error "Remove $ost_name from pool failed"
17781         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
17782                 error "Pool destroy fialed"
17783 }
17784 run_test 253 "Check object allocation limit"
17785
17786 test_254() {
17787         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17788         remote_mds_nodsh && skip "remote MDS with nodsh"
17789         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17790                 skip "MDS does not support changelog_size"
17791
17792         local cl_user
17793         local MDT0=$(facet_svc $SINGLEMDS)
17794
17795         changelog_register || error "changelog_register failed"
17796
17797         changelog_clear 0 || error "changelog_clear failed"
17798
17799         local size1=$(do_facet $SINGLEMDS \
17800                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17801         echo "Changelog size $size1"
17802
17803         rm -rf $DIR/$tdir
17804         $LFS mkdir -i 0 $DIR/$tdir
17805         # change something
17806         mkdir -p $DIR/$tdir/pics/2008/zachy
17807         touch $DIR/$tdir/pics/2008/zachy/timestamp
17808         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17809         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17810         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17811         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17812         rm $DIR/$tdir/pics/desktop.jpg
17813
17814         local size2=$(do_facet $SINGLEMDS \
17815                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17816         echo "Changelog size after work $size2"
17817
17818         (( $size2 > $size1 )) ||
17819                 error "new Changelog size=$size2 less than old size=$size1"
17820 }
17821 run_test 254 "Check changelog size"
17822
17823 ladvise_no_type()
17824 {
17825         local type=$1
17826         local file=$2
17827
17828         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
17829                 awk -F: '{print $2}' | grep $type > /dev/null
17830         if [ $? -ne 0 ]; then
17831                 return 0
17832         fi
17833         return 1
17834 }
17835
17836 ladvise_no_ioctl()
17837 {
17838         local file=$1
17839
17840         lfs ladvise -a willread $file > /dev/null 2>&1
17841         if [ $? -eq 0 ]; then
17842                 return 1
17843         fi
17844
17845         lfs ladvise -a willread $file 2>&1 |
17846                 grep "Inappropriate ioctl for device" > /dev/null
17847         if [ $? -eq 0 ]; then
17848                 return 0
17849         fi
17850         return 1
17851 }
17852
17853 percent() {
17854         bc <<<"scale=2; ($1 - $2) * 100 / $2"
17855 }
17856
17857 # run a random read IO workload
17858 # usage: random_read_iops <filename> <filesize> <iosize>
17859 random_read_iops() {
17860         local file=$1
17861         local fsize=$2
17862         local iosize=${3:-4096}
17863
17864         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
17865                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
17866 }
17867
17868 drop_file_oss_cache() {
17869         local file="$1"
17870         local nodes="$2"
17871
17872         $LFS ladvise -a dontneed $file 2>/dev/null ||
17873                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
17874 }
17875
17876 ladvise_willread_performance()
17877 {
17878         local repeat=10
17879         local average_origin=0
17880         local average_cache=0
17881         local average_ladvise=0
17882
17883         for ((i = 1; i <= $repeat; i++)); do
17884                 echo "Iter $i/$repeat: reading without willread hint"
17885                 cancel_lru_locks osc
17886                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17887                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
17888                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
17889                 average_origin=$(bc <<<"$average_origin + $speed_origin")
17890
17891                 cancel_lru_locks osc
17892                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
17893                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
17894                 average_cache=$(bc <<<"$average_cache + $speed_cache")
17895
17896                 cancel_lru_locks osc
17897                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17898                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
17899                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
17900                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
17901                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
17902         done
17903         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
17904         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
17905         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
17906
17907         speedup_cache=$(percent $average_cache $average_origin)
17908         speedup_ladvise=$(percent $average_ladvise $average_origin)
17909
17910         echo "Average uncached read: $average_origin"
17911         echo "Average speedup with OSS cached read: " \
17912                 "$average_cache = +$speedup_cache%"
17913         echo "Average speedup with ladvise willread: " \
17914                 "$average_ladvise = +$speedup_ladvise%"
17915
17916         local lowest_speedup=20
17917         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
17918                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
17919                         "got $average_cache%. Skipping ladvise willread check."
17920                 return 0
17921         fi
17922
17923         # the test won't work on ZFS until it supports 'ladvise dontneed', but
17924         # it is still good to run until then to exercise 'ladvise willread'
17925         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17926                 [ "$ost1_FSTYPE" = "zfs" ] &&
17927                 echo "osd-zfs does not support dontneed or drop_caches" &&
17928                 return 0
17929
17930         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
17931         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
17932                 error_not_in_vm "Speedup with willread is less than " \
17933                         "$lowest_speedup%, got $average_ladvise%"
17934 }
17935
17936 test_255a() {
17937         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17938                 skip "lustre < 2.8.54 does not support ladvise "
17939         remote_ost_nodsh && skip "remote OST with nodsh"
17940
17941         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
17942
17943         ladvise_no_type willread $DIR/$tfile &&
17944                 skip "willread ladvise is not supported"
17945
17946         ladvise_no_ioctl $DIR/$tfile &&
17947                 skip "ladvise ioctl is not supported"
17948
17949         local size_mb=100
17950         local size=$((size_mb * 1048576))
17951         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17952                 error "dd to $DIR/$tfile failed"
17953
17954         lfs ladvise -a willread $DIR/$tfile ||
17955                 error "Ladvise failed with no range argument"
17956
17957         lfs ladvise -a willread -s 0 $DIR/$tfile ||
17958                 error "Ladvise failed with no -l or -e argument"
17959
17960         lfs ladvise -a willread -e 1 $DIR/$tfile ||
17961                 error "Ladvise failed with only -e argument"
17962
17963         lfs ladvise -a willread -l 1 $DIR/$tfile ||
17964                 error "Ladvise failed with only -l argument"
17965
17966         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
17967                 error "End offset should not be smaller than start offset"
17968
17969         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
17970                 error "End offset should not be equal to start offset"
17971
17972         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
17973                 error "Ladvise failed with overflowing -s argument"
17974
17975         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
17976                 error "Ladvise failed with overflowing -e argument"
17977
17978         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
17979                 error "Ladvise failed with overflowing -l argument"
17980
17981         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
17982                 error "Ladvise succeeded with conflicting -l and -e arguments"
17983
17984         echo "Synchronous ladvise should wait"
17985         local delay=4
17986 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
17987         do_nodes $(comma_list $(osts_nodes)) \
17988                 $LCTL set_param fail_val=$delay fail_loc=0x237
17989
17990         local start_ts=$SECONDS
17991         lfs ladvise -a willread $DIR/$tfile ||
17992                 error "Ladvise failed with no range argument"
17993         local end_ts=$SECONDS
17994         local inteval_ts=$((end_ts - start_ts))
17995
17996         if [ $inteval_ts -lt $(($delay - 1)) ]; then
17997                 error "Synchronous advice didn't wait reply"
17998         fi
17999
18000         echo "Asynchronous ladvise shouldn't wait"
18001         local start_ts=$SECONDS
18002         lfs ladvise -a willread -b $DIR/$tfile ||
18003                 error "Ladvise failed with no range argument"
18004         local end_ts=$SECONDS
18005         local inteval_ts=$((end_ts - start_ts))
18006
18007         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18008                 error "Asynchronous advice blocked"
18009         fi
18010
18011         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18012         ladvise_willread_performance
18013 }
18014 run_test 255a "check 'lfs ladvise -a willread'"
18015
18016 facet_meminfo() {
18017         local facet=$1
18018         local info=$2
18019
18020         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18021 }
18022
18023 test_255b() {
18024         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18025                 skip "lustre < 2.8.54 does not support ladvise "
18026         remote_ost_nodsh && skip "remote OST with nodsh"
18027
18028         lfs setstripe -c 1 -i 0 $DIR/$tfile
18029
18030         ladvise_no_type dontneed $DIR/$tfile &&
18031                 skip "dontneed ladvise is not supported"
18032
18033         ladvise_no_ioctl $DIR/$tfile &&
18034                 skip "ladvise ioctl is not supported"
18035
18036         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18037                 [ "$ost1_FSTYPE" = "zfs" ] &&
18038                 skip "zfs-osd does not support 'ladvise dontneed'"
18039
18040         local size_mb=100
18041         local size=$((size_mb * 1048576))
18042         # In order to prevent disturbance of other processes, only check 3/4
18043         # of the memory usage
18044         local kibibytes=$((size_mb * 1024 * 3 / 4))
18045
18046         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18047                 error "dd to $DIR/$tfile failed"
18048
18049         #force write to complete before dropping OST cache & checking memory
18050         sync
18051
18052         local total=$(facet_meminfo ost1 MemTotal)
18053         echo "Total memory: $total KiB"
18054
18055         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18056         local before_read=$(facet_meminfo ost1 Cached)
18057         echo "Cache used before read: $before_read KiB"
18058
18059         lfs ladvise -a willread $DIR/$tfile ||
18060                 error "Ladvise willread failed"
18061         local after_read=$(facet_meminfo ost1 Cached)
18062         echo "Cache used after read: $after_read KiB"
18063
18064         lfs ladvise -a dontneed $DIR/$tfile ||
18065                 error "Ladvise dontneed again failed"
18066         local no_read=$(facet_meminfo ost1 Cached)
18067         echo "Cache used after dontneed ladvise: $no_read KiB"
18068
18069         if [ $total -lt $((before_read + kibibytes)) ]; then
18070                 echo "Memory is too small, abort checking"
18071                 return 0
18072         fi
18073
18074         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18075                 error "Ladvise willread should use more memory" \
18076                         "than $kibibytes KiB"
18077         fi
18078
18079         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18080                 error "Ladvise dontneed should release more memory" \
18081                         "than $kibibytes KiB"
18082         fi
18083 }
18084 run_test 255b "check 'lfs ladvise -a dontneed'"
18085
18086 test_255c() {
18087         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18088                 skip "lustre < 2.10.50 does not support lockahead"
18089
18090         local ost1_imp=$(get_osc_import_name client ost1)
18091         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
18092                          cut -d'.' -f2)
18093         local count
18094         local new_count
18095         local difference
18096         local i
18097         local rc
18098
18099         test_mkdir -p $DIR/$tdir
18100         $SETSTRIPE -i 0 $DIR/$tdir
18101
18102         #test 10 returns only success/failure
18103         i=10
18104         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18105         rc=$?
18106         if [ $rc -eq 255 ]; then
18107                 error "Ladvise test${i} failed, ${rc}"
18108         fi
18109
18110         #test 11 counts lock enqueue requests, all others count new locks
18111         i=11
18112         count=$(do_facet ost1 \
18113                 $LCTL get_param -n ost.OSS.ost.stats)
18114         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
18115
18116         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18117         rc=$?
18118         if [ $rc -eq 255 ]; then
18119                 error "Ladvise test${i} failed, ${rc}"
18120         fi
18121
18122         new_count=$(do_facet ost1 \
18123                 $LCTL get_param -n ost.OSS.ost.stats)
18124         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
18125                    awk '{ print $2 }')
18126
18127         difference="$((new_count - count))"
18128         if [ $difference -ne $rc ]; then
18129                 error "Ladvise test${i}, bad enqueue count, returned " \
18130                       "${rc}, actual ${difference}"
18131         fi
18132
18133         for i in $(seq 12 21); do
18134                 # If we do not do this, we run the risk of having too many
18135                 # locks and starting lock cancellation while we are checking
18136                 # lock counts.
18137                 cancel_lru_locks osc
18138
18139                 count=$($LCTL get_param -n \
18140                        ldlm.namespaces.$imp_name.lock_unused_count)
18141
18142                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18143                 rc=$?
18144                 if [ $rc -eq 255 ]; then
18145                         error "Ladvise test ${i} failed, ${rc}"
18146                 fi
18147
18148                 new_count=$($LCTL get_param -n \
18149                        ldlm.namespaces.$imp_name.lock_unused_count)
18150                 difference="$((new_count - count))"
18151
18152                 # Test 15 output is divided by 100 to map down to valid return
18153                 if [ $i -eq 15 ]; then
18154                         rc="$((rc * 100))"
18155                 fi
18156
18157                 if [ $difference -ne $rc ]; then
18158                         error "Ladvise test ${i}, bad lock count, returned " \
18159                               "${rc}, actual ${difference}"
18160                 fi
18161         done
18162
18163         #test 22 returns only success/failure
18164         i=22
18165         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18166         rc=$?
18167         if [ $rc -eq 255 ]; then
18168                 error "Ladvise test${i} failed, ${rc}"
18169         fi
18170 }
18171 run_test 255c "suite of ladvise lockahead tests"
18172
18173 test_256() {
18174         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18175         remote_mds_nodsh && skip "remote MDS with nodsh"
18176         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18177         changelog_users $SINGLEMDS | grep "^cl" &&
18178                 skip "active changelog user"
18179
18180         local cl_user
18181         local cat_sl
18182         local mdt_dev
18183
18184         mdt_dev=$(mdsdevname 1)
18185         echo $mdt_dev
18186
18187         changelog_register || error "changelog_register failed"
18188
18189         rm -rf $DIR/$tdir
18190         mkdir -p $DIR/$tdir
18191
18192         changelog_clear 0 || error "changelog_clear failed"
18193
18194         # change something
18195         touch $DIR/$tdir/{1..10}
18196
18197         # stop the MDT
18198         stop $SINGLEMDS || error "Fail to stop MDT"
18199
18200         # remount the MDT
18201
18202         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18203
18204         #after mount new plainllog is used
18205         touch $DIR/$tdir/{11..19}
18206         local tmpfile=$(mktemp -u $tfile.XXXXXX)
18207         cat_sl=$(do_facet $SINGLEMDS "sync; \
18208                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18209                  llog_reader $tmpfile | grep -c type=1064553b")
18210         do_facet $SINGLEMDS llog_reader $tmpfile
18211
18212         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18213
18214         changelog_clear 0 || error "changelog_clear failed"
18215
18216         cat_sl=$(do_facet $SINGLEMDS "sync; \
18217                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18218                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
18219
18220         if (( cat_sl == 2 )); then
18221                 error "Empty plain llog was not deleted from changelog catalog"
18222         elif (( cat_sl != 1 )); then
18223                 error "Active plain llog shouldn't be deleted from catalog"
18224         fi
18225 }
18226 run_test 256 "Check llog delete for empty and not full state"
18227
18228 test_257() {
18229         remote_mds_nodsh && skip "remote MDS with nodsh"
18230         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18231                 skip "Need MDS version at least 2.8.55"
18232
18233         test_mkdir $DIR/$tdir
18234
18235         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18236                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18237         stat $DIR/$tdir
18238
18239 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18240         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18241         local facet=mds$((mdtidx + 1))
18242         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18243         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18244
18245         stop $facet || error "stop MDS failed"
18246         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18247                 error "start MDS fail"
18248 }
18249 run_test 257 "xattr locks are not lost"
18250
18251 # Verify we take the i_mutex when security requires it
18252 test_258a() {
18253 #define OBD_FAIL_IMUTEX_SEC 0x141c
18254         $LCTL set_param fail_loc=0x141c
18255         touch $DIR/$tfile
18256         chmod u+s $DIR/$tfile
18257         chmod a+rwx $DIR/$tfile
18258         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18259         RC=$?
18260         if [ $RC -ne 0 ]; then
18261                 error "error, failed to take i_mutex, rc=$?"
18262         fi
18263         rm -f $DIR/$tfile
18264 }
18265 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18266
18267 # Verify we do NOT take the i_mutex in the normal case
18268 test_258b() {
18269 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18270         $LCTL set_param fail_loc=0x141d
18271         touch $DIR/$tfile
18272         chmod a+rwx $DIR
18273         chmod a+rw $DIR/$tfile
18274         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18275         RC=$?
18276         if [ $RC -ne 0 ]; then
18277                 error "error, took i_mutex unnecessarily, rc=$?"
18278         fi
18279         rm -f $DIR/$tfile
18280
18281 }
18282 run_test 258b "verify i_mutex security behavior"
18283
18284 test_259() {
18285         local file=$DIR/$tfile
18286         local before
18287         local after
18288
18289         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18290
18291         stack_trap "rm -f $file" EXIT
18292
18293         wait_delete_completed
18294         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18295         echo "before: $before"
18296
18297         $LFS setstripe -i 0 -c 1 $file
18298         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18299         sync_all_data
18300         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18301         echo "after write: $after"
18302
18303 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18304         do_facet ost1 $LCTL set_param fail_loc=0x2301
18305         $TRUNCATE $file 0
18306         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18307         echo "after truncate: $after"
18308
18309         stop ost1
18310         do_facet ost1 $LCTL set_param fail_loc=0
18311         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18312         sleep 2
18313         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18314         echo "after restart: $after"
18315         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18316                 error "missing truncate?"
18317
18318         return 0
18319 }
18320 run_test 259 "crash at delayed truncate"
18321
18322 test_260() {
18323 #define OBD_FAIL_MDC_CLOSE               0x806
18324         $LCTL set_param fail_loc=0x80000806
18325         touch $DIR/$tfile
18326
18327 }
18328 run_test 260 "Check mdc_close fail"
18329
18330 ### Data-on-MDT sanity tests ###
18331 test_270a() {
18332         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18333                 skip "Need MDS version at least 2.10.55 for DoM"
18334
18335         # create DoM file
18336         local dom=$DIR/$tdir/dom_file
18337         local tmp=$DIR/$tdir/tmp_file
18338
18339         mkdir -p $DIR/$tdir
18340
18341         # basic checks for DoM component creation
18342         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18343                 error "Can set MDT layout to non-first entry"
18344
18345         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18346                 error "Can define multiple entries as MDT layout"
18347
18348         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18349
18350         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18351         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18352         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18353
18354         local mdtidx=$($LFS getstripe -m $dom)
18355         local mdtname=MDT$(printf %04x $mdtidx)
18356         local facet=mds$((mdtidx + 1))
18357         local space_check=1
18358
18359         # Skip free space checks with ZFS
18360         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18361
18362         # write
18363         sync
18364         local size_tmp=$((65536 * 3))
18365         local mdtfree1=$(do_facet $facet \
18366                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18367
18368         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18369         # check also direct IO along write
18370         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18371         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18372         sync
18373         cmp $tmp $dom || error "file data is different"
18374         [ $(stat -c%s $dom) == $size_tmp ] ||
18375                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18376         if [ $space_check == 1 ]; then
18377                 local mdtfree2=$(do_facet $facet \
18378                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18379
18380                 # increase in usage from by $size_tmp
18381                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18382                         error "MDT free space wrong after write: " \
18383                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18384         fi
18385
18386         # truncate
18387         local size_dom=10000
18388
18389         $TRUNCATE $dom $size_dom
18390         [ $(stat -c%s $dom) == $size_dom ] ||
18391                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18392         if [ $space_check == 1 ]; then
18393                 mdtfree1=$(do_facet $facet \
18394                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18395                 # decrease in usage from $size_tmp to new $size_dom
18396                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18397                   $(((size_tmp - size_dom) / 1024)) ] ||
18398                         error "MDT free space is wrong after truncate: " \
18399                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18400         fi
18401
18402         # append
18403         cat $tmp >> $dom
18404         sync
18405         size_dom=$((size_dom + size_tmp))
18406         [ $(stat -c%s $dom) == $size_dom ] ||
18407                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18408         if [ $space_check == 1 ]; then
18409                 mdtfree2=$(do_facet $facet \
18410                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18411                 # increase in usage by $size_tmp from previous
18412                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18413                         error "MDT free space is wrong after append: " \
18414                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18415         fi
18416
18417         # delete
18418         rm $dom
18419         if [ $space_check == 1 ]; then
18420                 mdtfree1=$(do_facet $facet \
18421                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18422                 # decrease in usage by $size_dom from previous
18423                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18424                         error "MDT free space is wrong after removal: " \
18425                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18426         fi
18427
18428         # combined striping
18429         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18430                 error "Can't create DoM + OST striping"
18431
18432         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18433         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18434         # check also direct IO along write
18435         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18436         sync
18437         cmp $tmp $dom || error "file data is different"
18438         [ $(stat -c%s $dom) == $size_tmp ] ||
18439                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18440         rm $dom $tmp
18441
18442         return 0
18443 }
18444 run_test 270a "DoM: basic functionality tests"
18445
18446 test_270b() {
18447         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18448                 skip "Need MDS version at least 2.10.55"
18449
18450         local dom=$DIR/$tdir/dom_file
18451         local max_size=1048576
18452
18453         mkdir -p $DIR/$tdir
18454         $LFS setstripe -E $max_size -L mdt $dom
18455
18456         # truncate over the limit
18457         $TRUNCATE $dom $(($max_size + 1)) &&
18458                 error "successful truncate over the maximum size"
18459         # write over the limit
18460         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18461                 error "successful write over the maximum size"
18462         # append over the limit
18463         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18464         echo "12345" >> $dom && error "successful append over the maximum size"
18465         rm $dom
18466
18467         return 0
18468 }
18469 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18470
18471 test_270c() {
18472         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18473                 skip "Need MDS version at least 2.10.55"
18474
18475         mkdir -p $DIR/$tdir
18476         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18477
18478         # check files inherit DoM EA
18479         touch $DIR/$tdir/first
18480         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
18481                 error "bad pattern"
18482         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18483                 error "bad stripe count"
18484         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18485                 error "bad stripe size"
18486
18487         # check directory inherits DoM EA and uses it as default
18488         mkdir $DIR/$tdir/subdir
18489         touch $DIR/$tdir/subdir/second
18490         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18491                 error "bad pattern in sub-directory"
18492         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18493                 error "bad stripe count in sub-directory"
18494         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18495                 error "bad stripe size in sub-directory"
18496         return 0
18497 }
18498 run_test 270c "DoM: DoM EA inheritance tests"
18499
18500 test_270d() {
18501         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18502                 skip "Need MDS version at least 2.10.55"
18503
18504         mkdir -p $DIR/$tdir
18505         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18506
18507         # inherit default DoM striping
18508         mkdir $DIR/$tdir/subdir
18509         touch $DIR/$tdir/subdir/f1
18510
18511         # change default directory striping
18512         $LFS setstripe -c 1 $DIR/$tdir/subdir
18513         touch $DIR/$tdir/subdir/f2
18514         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18515                 error "wrong default striping in file 2"
18516         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18517                 error "bad pattern in file 2"
18518         return 0
18519 }
18520 run_test 270d "DoM: change striping from DoM to RAID0"
18521
18522 test_270e() {
18523         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18524                 skip "Need MDS version at least 2.10.55"
18525
18526         mkdir -p $DIR/$tdir/dom
18527         mkdir -p $DIR/$tdir/norm
18528         DOMFILES=20
18529         NORMFILES=10
18530         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18531         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18532
18533         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18534         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18535
18536         # find DoM files by layout
18537         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18538         [ $NUM -eq  $DOMFILES ] ||
18539                 error "lfs find -L: found $NUM, expected $DOMFILES"
18540         echo "Test 1: lfs find 20 DOM files by layout: OK"
18541
18542         # there should be 1 dir with default DOM striping
18543         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18544         [ $NUM -eq  1 ] ||
18545                 error "lfs find -L: found $NUM, expected 1 dir"
18546         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18547
18548         # find DoM files by stripe size
18549         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18550         [ $NUM -eq  $DOMFILES ] ||
18551                 error "lfs find -S: found $NUM, expected $DOMFILES"
18552         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18553
18554         # find files by stripe offset except DoM files
18555         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18556         [ $NUM -eq  $NORMFILES ] ||
18557                 error "lfs find -i: found $NUM, expected $NORMFILES"
18558         echo "Test 5: lfs find no DOM files by stripe index: OK"
18559         return 0
18560 }
18561 run_test 270e "DoM: lfs find with DoM files test"
18562
18563 test_270f() {
18564         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18565                 skip "Need MDS version at least 2.10.55"
18566
18567         local mdtname=${FSNAME}-MDT0000-mdtlov
18568         local dom=$DIR/$tdir/dom_file
18569         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18570                                                 lod.$mdtname.dom_stripesize)
18571         local dom_limit=131072
18572
18573         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18574         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18575                                                 lod.$mdtname.dom_stripesize)
18576         [ ${dom_limit} -eq ${dom_current} ] ||
18577                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18578
18579         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18580         $LFS setstripe -d $DIR/$tdir
18581         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18582                 error "Can't set directory default striping"
18583
18584         # exceed maximum stripe size
18585         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18586                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18587         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18588                 error "Able to create DoM component size more than LOD limit"
18589
18590         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18591         dom_current=$(do_facet mds1 $LCTL get_param -n \
18592                                                 lod.$mdtname.dom_stripesize)
18593         [ 0 -eq ${dom_current} ] ||
18594                 error "Can't set zero DoM stripe limit"
18595         rm $dom
18596
18597         # attempt to create DoM file on server with disabled DoM should
18598         # remove DoM entry from layout and be succeed
18599         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18600                 error "Can't create DoM file (DoM is disabled)"
18601         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18602                 error "File has DoM component while DoM is disabled"
18603         rm $dom
18604
18605         # attempt to create DoM file with only DoM stripe should return error
18606         $LFS setstripe -E $dom_limit -L mdt $dom &&
18607                 error "Able to create DoM-only file while DoM is disabled"
18608
18609         # too low values to be aligned with smallest stripe size 64K
18610         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18611         dom_current=$(do_facet mds1 $LCTL get_param -n \
18612                                                 lod.$mdtname.dom_stripesize)
18613         [ 30000 -eq ${dom_current} ] &&
18614                 error "Can set too small DoM stripe limit"
18615
18616         # 64K is a minimal stripe size in Lustre, expect limit of that size
18617         [ 65536 -eq ${dom_current} ] ||
18618                 error "Limit is not set to 64K but ${dom_current}"
18619
18620         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18621         dom_current=$(do_facet mds1 $LCTL get_param -n \
18622                                                 lod.$mdtname.dom_stripesize)
18623         echo $dom_current
18624         [ 2147483648 -eq ${dom_current} ] &&
18625                 error "Can set too large DoM stripe limit"
18626
18627         do_facet mds1 $LCTL set_param -n \
18628                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18629         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18630                 error "Can't create DoM component size after limit change"
18631         do_facet mds1 $LCTL set_param -n \
18632                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18633         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18634                 error "Can't create DoM file after limit decrease"
18635         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18636                 error "Can create big DoM component after limit decrease"
18637         touch ${dom}_def ||
18638                 error "Can't create file with old default layout"
18639
18640         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18641         return 0
18642 }
18643 run_test 270f "DoM: maximum DoM stripe size checks"
18644
18645 test_271a() {
18646         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18647                 skip "Need MDS version at least 2.10.55"
18648
18649         local dom=$DIR/$tdir/dom
18650
18651         mkdir -p $DIR/$tdir
18652
18653         $LFS setstripe -E 1024K -L mdt $dom
18654
18655         lctl set_param -n mdc.*.stats=clear
18656         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18657         cat $dom > /dev/null
18658         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18659         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18660         ls $dom
18661         rm -f $dom
18662 }
18663 run_test 271a "DoM: data is cached for read after write"
18664
18665 test_271b() {
18666         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18667                 skip "Need MDS version at least 2.10.55"
18668
18669         local dom=$DIR/$tdir/dom
18670
18671         mkdir -p $DIR/$tdir
18672
18673         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18674
18675         lctl set_param -n mdc.*.stats=clear
18676         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18677         cancel_lru_locks mdc
18678         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18679         # second stat to check size is cached on client
18680         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18681         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18682         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18683         rm -f $dom
18684 }
18685 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18686
18687 test_271ba() {
18688         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18689                 skip "Need MDS version at least 2.10.55"
18690
18691         local dom=$DIR/$tdir/dom
18692
18693         mkdir -p $DIR/$tdir
18694
18695         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18696
18697         lctl set_param -n mdc.*.stats=clear
18698         lctl set_param -n osc.*.stats=clear
18699         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18700         cancel_lru_locks mdc
18701         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18702         # second stat to check size is cached on client
18703         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18704         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18705         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18706         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18707         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18708         rm -f $dom
18709 }
18710 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18711
18712
18713 get_mdc_stats() {
18714         local mdtidx=$1
18715         local param=$2
18716         local mdt=MDT$(printf %04x $mdtidx)
18717
18718         if [ -z $param ]; then
18719                 lctl get_param -n mdc.*$mdt*.stats
18720         else
18721                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18722         fi
18723 }
18724
18725 test_271c() {
18726         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18727                 skip "Need MDS version at least 2.10.55"
18728
18729         local dom=$DIR/$tdir/dom
18730
18731         mkdir -p $DIR/$tdir
18732
18733         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18734
18735         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18736         local facet=mds$((mdtidx + 1))
18737
18738         cancel_lru_locks mdc
18739         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18740         createmany -o $dom 1000
18741         lctl set_param -n mdc.*.stats=clear
18742         smalliomany -w $dom 1000 200
18743         get_mdc_stats $mdtidx
18744         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18745         # Each file has 1 open, 1 IO enqueues, total 2000
18746         # but now we have also +1 getxattr for security.capability, total 3000
18747         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18748         unlinkmany $dom 1000
18749
18750         cancel_lru_locks mdc
18751         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18752         createmany -o $dom 1000
18753         lctl set_param -n mdc.*.stats=clear
18754         smalliomany -w $dom 1000 200
18755         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18756         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18757         # for OPEN and IO lock.
18758         [ $((enq - enq_2)) -ge 1000 ] ||
18759                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18760         unlinkmany $dom 1000
18761         return 0
18762 }
18763 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18764
18765 cleanup_271def_tests() {
18766         trap 0
18767         rm -f $1
18768 }
18769
18770 test_271d() {
18771         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18772                 skip "Need MDS version at least 2.10.57"
18773
18774         local dom=$DIR/$tdir/dom
18775         local tmp=$TMP/$tfile
18776         trap "cleanup_271def_tests $tmp" EXIT
18777
18778         mkdir -p $DIR/$tdir
18779
18780         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18781
18782         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18783
18784         cancel_lru_locks mdc
18785         dd if=/dev/urandom of=$tmp bs=1000 count=1
18786         dd if=$tmp of=$dom bs=1000 count=1
18787         cancel_lru_locks mdc
18788
18789         cat /etc/hosts >> $tmp
18790         lctl set_param -n mdc.*.stats=clear
18791
18792         # append data to the same file it should update local page
18793         echo "Append to the same page"
18794         cat /etc/hosts >> $dom
18795         local num=$(get_mdc_stats $mdtidx ost_read)
18796         local ra=$(get_mdc_stats $mdtidx req_active)
18797         local rw=$(get_mdc_stats $mdtidx req_waittime)
18798
18799         [ -z $num ] || error "$num READ RPC occured"
18800         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18801         echo "... DONE"
18802
18803         # compare content
18804         cmp $tmp $dom || error "file miscompare"
18805
18806         cancel_lru_locks mdc
18807         lctl set_param -n mdc.*.stats=clear
18808
18809         echo "Open and read file"
18810         cat $dom > /dev/null
18811         local num=$(get_mdc_stats $mdtidx ost_read)
18812         local ra=$(get_mdc_stats $mdtidx req_active)
18813         local rw=$(get_mdc_stats $mdtidx req_waittime)
18814
18815         [ -z $num ] || error "$num READ RPC occured"
18816         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18817         echo "... DONE"
18818
18819         # compare content
18820         cmp $tmp $dom || error "file miscompare"
18821
18822         return 0
18823 }
18824 run_test 271d "DoM: read on open (1K file in reply buffer)"
18825
18826 test_271f() {
18827         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18828                 skip "Need MDS version at least 2.10.57"
18829
18830         local dom=$DIR/$tdir/dom
18831         local tmp=$TMP/$tfile
18832         trap "cleanup_271def_tests $tmp" EXIT
18833
18834         mkdir -p $DIR/$tdir
18835
18836         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18837
18838         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18839
18840         cancel_lru_locks mdc
18841         dd if=/dev/urandom of=$tmp bs=200000 count=1
18842         dd if=$tmp of=$dom bs=200000 count=1
18843         cancel_lru_locks mdc
18844         cat /etc/hosts >> $tmp
18845         lctl set_param -n mdc.*.stats=clear
18846
18847         echo "Append to the same page"
18848         cat /etc/hosts >> $dom
18849         local num=$(get_mdc_stats $mdtidx ost_read)
18850         local ra=$(get_mdc_stats $mdtidx req_active)
18851         local rw=$(get_mdc_stats $mdtidx req_waittime)
18852
18853         [ -z $num ] || error "$num READ RPC occured"
18854         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18855         echo "... DONE"
18856
18857         # compare content
18858         cmp $tmp $dom || error "file miscompare"
18859
18860         cancel_lru_locks mdc
18861         lctl set_param -n mdc.*.stats=clear
18862
18863         echo "Open and read file"
18864         cat $dom > /dev/null
18865         local num=$(get_mdc_stats $mdtidx ost_read)
18866         local ra=$(get_mdc_stats $mdtidx req_active)
18867         local rw=$(get_mdc_stats $mdtidx req_waittime)
18868
18869         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
18870         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18871         echo "... DONE"
18872
18873         # compare content
18874         cmp $tmp $dom || error "file miscompare"
18875
18876         return 0
18877 }
18878 run_test 271f "DoM: read on open (200K file and read tail)"
18879
18880 test_271g() {
18881         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
18882                 skip "Skipping due to old client or server version"
18883
18884         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
18885         # to get layout
18886         $CHECKSTAT -t file $DIR1/$tfile
18887
18888         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
18889         MULTIOP_PID=$!
18890         sleep 1
18891         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
18892         $LCTL set_param fail_loc=0x80000314
18893         rm $DIR1/$tfile || error "Unlink fails"
18894         RC=$?
18895         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
18896         [ $RC -eq 0 ] || error "Failed write to stale object"
18897 }
18898 run_test 271g "Discard DoM data vs client flush race"
18899
18900 test_272a() {
18901         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18902                 skip "Need MDS version at least 2.11.50"
18903
18904         local dom=$DIR/$tdir/dom
18905         mkdir -p $DIR/$tdir
18906
18907         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
18908         dd if=/dev/urandom of=$dom bs=512K count=1 ||
18909                 error "failed to write data into $dom"
18910         local old_md5=$(md5sum $dom)
18911
18912         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
18913                 error "failed to migrate to the same DoM component"
18914
18915         [ $($LFS getstripe -c $dom) -eq 2 ] ||
18916                 error "layout was not changed silently"
18917
18918         local new_md5=$(md5sum $dom)
18919
18920         [ "$old_md5" != "$new_md5" ] &&
18921                 error "md5sum differ: $old_md5, $new_md5"
18922         return 0
18923 }
18924 run_test 272a "DoM migration: new layout with the same DOM component"
18925
18926 test_272b() {
18927         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18928                 skip "Need MDS version at least 2.11.50"
18929
18930         local dom=$DIR/$tdir/dom
18931         mkdir -p $DIR/$tdir
18932         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18933
18934         local mdtidx=$($LFS getstripe -m $dom)
18935         local mdtname=MDT$(printf %04x $mdtidx)
18936         local facet=mds$((mdtidx + 1))
18937
18938         local mdtfree1=$(do_facet $facet \
18939                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18940         dd if=/dev/urandom of=$dom bs=2M count=1 ||
18941                 error "failed to write data into $dom"
18942         local old_md5=$(md5sum $dom)
18943         cancel_lru_locks mdc
18944         local mdtfree1=$(do_facet $facet \
18945                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18946
18947         $LFS migrate -c2 $dom ||
18948                 error "failed to migrate to the new composite layout"
18949         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18950                 error "MDT stripe was not removed"
18951
18952         cancel_lru_locks mdc
18953         local new_md5=$(md5sum $dom)
18954         [ "$old_md5" != "$new_md5" ] &&
18955                 error "$old_md5 != $new_md5"
18956
18957         # Skip free space checks with ZFS
18958         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18959                 local mdtfree2=$(do_facet $facet \
18960                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18961                 [ $mdtfree2 -gt $mdtfree1 ] ||
18962                         error "MDT space is not freed after migration"
18963         fi
18964         return 0
18965 }
18966 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
18967
18968 test_272c() {
18969         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18970                 skip "Need MDS version at least 2.11.50"
18971
18972         local dom=$DIR/$tdir/$tfile
18973         mkdir -p $DIR/$tdir
18974         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18975
18976         local mdtidx=$($LFS getstripe -m $dom)
18977         local mdtname=MDT$(printf %04x $mdtidx)
18978         local facet=mds$((mdtidx + 1))
18979
18980         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
18981                 error "failed to write data into $dom"
18982         local old_md5=$(md5sum $dom)
18983         cancel_lru_locks mdc
18984         local mdtfree1=$(do_facet $facet \
18985                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18986
18987         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
18988                 error "failed to migrate to the new composite layout"
18989         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18990                 error "MDT stripe was not removed"
18991
18992         cancel_lru_locks mdc
18993         local new_md5=$(md5sum $dom)
18994         [ "$old_md5" != "$new_md5" ] &&
18995                 error "$old_md5 != $new_md5"
18996
18997         # Skip free space checks with ZFS
18998         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18999                 local mdtfree2=$(do_facet $facet \
19000                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19001                 [ $mdtfree2 -gt $mdtfree1 ] ||
19002                         error "MDS space is not freed after migration"
19003         fi
19004         return 0
19005 }
19006 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19007
19008 test_273a() {
19009         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19010                 skip "Need MDS version at least 2.11.50"
19011
19012         # Layout swap cannot be done if either file has DOM component,
19013         # this will never be supported, migration should be used instead
19014
19015         local dom=$DIR/$tdir/$tfile
19016         mkdir -p $DIR/$tdir
19017
19018         $LFS setstripe -c2 ${dom}_plain
19019         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
19020         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
19021                 error "can swap layout with DoM component"
19022         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
19023                 error "can swap layout with DoM component"
19024
19025         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
19026         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
19027                 error "can swap layout with DoM component"
19028         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
19029                 error "can swap layout with DoM component"
19030         return 0
19031 }
19032 run_test 273a "DoM: layout swapping should fail with DOM"
19033
19034 test_275() {
19035         remote_ost_nodsh && skip "remote OST with nodsh"
19036         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
19037                 skip "Need OST version >= 2.10.57"
19038
19039         local file=$DIR/$tfile
19040         local oss
19041
19042         oss=$(comma_list $(osts_nodes))
19043
19044         dd if=/dev/urandom of=$file bs=1M count=2 ||
19045                 error "failed to create a file"
19046         cancel_lru_locks osc
19047
19048         #lock 1
19049         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19050                 error "failed to read a file"
19051
19052 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
19053         $LCTL set_param fail_loc=0x8000031f
19054
19055         cancel_lru_locks osc &
19056         sleep 1
19057
19058 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
19059         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
19060         #IO takes another lock, but matches the PENDING one
19061         #and places it to the IO RPC
19062         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
19063                 error "failed to read a file with PENDING lock"
19064 }
19065 run_test 275 "Read on a canceled duplicate lock"
19066
19067 test_276() {
19068         remote_ost_nodsh && skip "remote OST with nodsh"
19069         local pid
19070
19071         do_facet ost1 "(while true; do \
19072                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
19073                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
19074         pid=$!
19075
19076         for LOOP in $(seq 20); do
19077                 stop ost1
19078                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
19079         done
19080         kill -9 $pid
19081         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
19082                 rm $TMP/sanity_276_pid"
19083 }
19084 run_test 276 "Race between mount and obd_statfs"
19085
19086 cleanup_test_300() {
19087         trap 0
19088         umask $SAVE_UMASK
19089 }
19090 test_striped_dir() {
19091         local mdt_index=$1
19092         local stripe_count
19093         local stripe_index
19094
19095         mkdir -p $DIR/$tdir
19096
19097         SAVE_UMASK=$(umask)
19098         trap cleanup_test_300 RETURN EXIT
19099
19100         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
19101                                                 $DIR/$tdir/striped_dir ||
19102                 error "set striped dir error"
19103
19104         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
19105         [ "$mode" = "755" ] || error "expect 755 got $mode"
19106
19107         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
19108                 error "getdirstripe failed"
19109         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
19110         if [ "$stripe_count" != "2" ]; then
19111                 error "1:stripe_count is $stripe_count, expect 2"
19112         fi
19113         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
19114         if [ "$stripe_count" != "2" ]; then
19115                 error "2:stripe_count is $stripe_count, expect 2"
19116         fi
19117
19118         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
19119         if [ "$stripe_index" != "$mdt_index" ]; then
19120                 error "stripe_index is $stripe_index, expect $mdt_index"
19121         fi
19122
19123         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19124                 error "nlink error after create striped dir"
19125
19126         mkdir $DIR/$tdir/striped_dir/a
19127         mkdir $DIR/$tdir/striped_dir/b
19128
19129         stat $DIR/$tdir/striped_dir/a ||
19130                 error "create dir under striped dir failed"
19131         stat $DIR/$tdir/striped_dir/b ||
19132                 error "create dir under striped dir failed"
19133
19134         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19135                 error "nlink error after mkdir"
19136
19137         rmdir $DIR/$tdir/striped_dir/a
19138         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19139                 error "nlink error after rmdir"
19140
19141         rmdir $DIR/$tdir/striped_dir/b
19142         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19143                 error "nlink error after rmdir"
19144
19145         chattr +i $DIR/$tdir/striped_dir
19146         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19147                 error "immutable flags not working under striped dir!"
19148         chattr -i $DIR/$tdir/striped_dir
19149
19150         rmdir $DIR/$tdir/striped_dir ||
19151                 error "rmdir striped dir error"
19152
19153         cleanup_test_300
19154
19155         true
19156 }
19157
19158 test_300a() {
19159         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19160                 skip "skipped for lustre < 2.7.0"
19161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19162         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19163
19164         test_striped_dir 0 || error "failed on striped dir on MDT0"
19165         test_striped_dir 1 || error "failed on striped dir on MDT0"
19166 }
19167 run_test 300a "basic striped dir sanity test"
19168
19169 test_300b() {
19170         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19171                 skip "skipped for lustre < 2.7.0"
19172         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19173         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19174
19175         local i
19176         local mtime1
19177         local mtime2
19178         local mtime3
19179
19180         test_mkdir $DIR/$tdir || error "mkdir fail"
19181         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19182                 error "set striped dir error"
19183         for i in {0..9}; do
19184                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19185                 sleep 1
19186                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19187                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19188                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19189                 sleep 1
19190                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19191                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19192                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19193         done
19194         true
19195 }
19196 run_test 300b "check ctime/mtime for striped dir"
19197
19198 test_300c() {
19199         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19200                 skip "skipped for lustre < 2.7.0"
19201         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19202         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19203
19204         local file_count
19205
19206         mkdir -p $DIR/$tdir
19207         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19208                 error "set striped dir error"
19209
19210         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19211                 error "chown striped dir failed"
19212
19213         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19214                 error "create 5k files failed"
19215
19216         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19217
19218         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19219
19220         rm -rf $DIR/$tdir
19221 }
19222 run_test 300c "chown && check ls under striped directory"
19223
19224 test_300d() {
19225         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19226                 skip "skipped for lustre < 2.7.0"
19227         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19228         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19229
19230         local stripe_count
19231         local file
19232
19233         mkdir -p $DIR/$tdir
19234         $SETSTRIPE -c 2 $DIR/$tdir
19235
19236         #local striped directory
19237         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19238                 error "set striped dir error"
19239         #look at the directories for debug purposes
19240         ls -l $DIR/$tdir
19241         $LFS getdirstripe $DIR/$tdir
19242         ls -l $DIR/$tdir/striped_dir
19243         $LFS getdirstripe $DIR/$tdir/striped_dir
19244         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19245                 error "create 10 files failed"
19246
19247         #remote striped directory
19248         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19249                 error "set striped dir error"
19250         #look at the directories for debug purposes
19251         ls -l $DIR/$tdir
19252         $LFS getdirstripe $DIR/$tdir
19253         ls -l $DIR/$tdir/remote_striped_dir
19254         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
19255         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19256                 error "create 10 files failed"
19257
19258         for file in $(find $DIR/$tdir); do
19259                 stripe_count=$($LFS getstripe -c $file)
19260                 [ $stripe_count -eq 2 ] ||
19261                         error "wrong stripe $stripe_count for $file"
19262         done
19263
19264         rm -rf $DIR/$tdir
19265 }
19266 run_test 300d "check default stripe under striped directory"
19267
19268 test_300e() {
19269         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19270                 skip "Need MDS version at least 2.7.55"
19271         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19272         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19273
19274         local stripe_count
19275         local file
19276
19277         mkdir -p $DIR/$tdir
19278
19279         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19280                 error "set striped dir error"
19281
19282         touch $DIR/$tdir/striped_dir/a
19283         touch $DIR/$tdir/striped_dir/b
19284         touch $DIR/$tdir/striped_dir/c
19285
19286         mkdir $DIR/$tdir/striped_dir/dir_a
19287         mkdir $DIR/$tdir/striped_dir/dir_b
19288         mkdir $DIR/$tdir/striped_dir/dir_c
19289
19290         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19291                 error "set striped adir under striped dir error"
19292
19293         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19294                 error "set striped bdir under striped dir error"
19295
19296         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19297                 error "set striped cdir under striped dir error"
19298
19299         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19300                 error "rename dir under striped dir fails"
19301
19302         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19303                 error "rename dir under different stripes fails"
19304
19305         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19306                 error "rename file under striped dir should succeed"
19307
19308         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19309                 error "rename dir under striped dir should succeed"
19310
19311         rm -rf $DIR/$tdir
19312 }
19313 run_test 300e "check rename under striped directory"
19314
19315 test_300f() {
19316         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19317         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19318         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19319                 skip "Need MDS version at least 2.7.55"
19320
19321         local stripe_count
19322         local file
19323
19324         rm -rf $DIR/$tdir
19325         mkdir -p $DIR/$tdir
19326
19327         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19328                 error "set striped dir error"
19329
19330         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19331                 error "set striped dir error"
19332
19333         touch $DIR/$tdir/striped_dir/a
19334         mkdir $DIR/$tdir/striped_dir/dir_a
19335         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19336                 error "create striped dir under striped dir fails"
19337
19338         touch $DIR/$tdir/striped_dir1/b
19339         mkdir $DIR/$tdir/striped_dir1/dir_b
19340         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19341                 error "create striped dir under striped dir fails"
19342
19343         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19344                 error "rename dir under different striped dir should fail"
19345
19346         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19347                 error "rename striped dir under diff striped dir should fail"
19348
19349         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19350                 error "rename file under diff striped dirs fails"
19351
19352         rm -rf $DIR/$tdir
19353 }
19354 run_test 300f "check rename cross striped directory"
19355
19356 test_300_check_default_striped_dir()
19357 {
19358         local dirname=$1
19359         local default_count=$2
19360         local default_index=$3
19361         local stripe_count
19362         local stripe_index
19363         local dir_stripe_index
19364         local dir
19365
19366         echo "checking $dirname $default_count $default_index"
19367         $LFS setdirstripe -D -c $default_count -i $default_index \
19368                                 -t all_char $DIR/$tdir/$dirname ||
19369                 error "set default stripe on striped dir error"
19370         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19371         [ $stripe_count -eq $default_count ] ||
19372                 error "expect $default_count get $stripe_count for $dirname"
19373
19374         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19375         [ $stripe_index -eq $default_index ] ||
19376                 error "expect $default_index get $stripe_index for $dirname"
19377
19378         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19379                                                 error "create dirs failed"
19380
19381         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19382         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19383         for dir in $(find $DIR/$tdir/$dirname/*); do
19384                 stripe_count=$($LFS getdirstripe -c $dir)
19385                 [ $stripe_count -eq $default_count ] ||
19386                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
19387                 error "stripe count $default_count != $stripe_count for $dir"
19388
19389                 stripe_index=$($LFS getdirstripe -i $dir)
19390                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
19391                         error "$stripe_index != $default_index for $dir"
19392
19393                 #check default stripe
19394                 stripe_count=$($LFS getdirstripe -D -c $dir)
19395                 [ $stripe_count -eq $default_count ] ||
19396                 error "default count $default_count != $stripe_count for $dir"
19397
19398                 stripe_index=$($LFS getdirstripe -D -i $dir)
19399                 [ $stripe_index -eq $default_index ] ||
19400                 error "default index $default_index != $stripe_index for $dir"
19401         done
19402         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19403 }
19404
19405 test_300g() {
19406         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19407         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19408                 skip "Need MDS version at least 2.7.55"
19409
19410         local dir
19411         local stripe_count
19412         local stripe_index
19413
19414         mkdir $DIR/$tdir
19415         mkdir $DIR/$tdir/normal_dir
19416
19417         #Checking when client cache stripe index
19418         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19419         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19420                 error "create striped_dir failed"
19421
19422         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19423                 error "create dir0 fails"
19424         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19425         [ $stripe_index -eq 0 ] ||
19426                 error "dir0 expect index 0 got $stripe_index"
19427
19428         mkdir $DIR/$tdir/striped_dir/dir1 ||
19429                 error "create dir1 fails"
19430         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19431         [ $stripe_index -eq 1 ] ||
19432                 error "dir1 expect index 1 got $stripe_index"
19433
19434         #check default stripe count/stripe index
19435         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19436         test_300_check_default_striped_dir normal_dir 1 0
19437         test_300_check_default_striped_dir normal_dir 2 1
19438         test_300_check_default_striped_dir normal_dir 2 -1
19439
19440         #delete default stripe information
19441         echo "delete default stripeEA"
19442         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19443                 error "set default stripe on striped dir error"
19444
19445         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19446         for dir in $(find $DIR/$tdir/normal_dir/*); do
19447                 stripe_count=$($LFS getdirstripe -c $dir)
19448                 [ $stripe_count -eq 0 ] ||
19449                         error "expect 1 get $stripe_count for $dir"
19450                 stripe_index=$($LFS getdirstripe -i $dir)
19451                 [ $stripe_index -eq 0 ] ||
19452                         error "expect 0 get $stripe_index for $dir"
19453         done
19454 }
19455 run_test 300g "check default striped directory for normal directory"
19456
19457 test_300h() {
19458         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19459         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19460                 skip "Need MDS version at least 2.7.55"
19461
19462         local dir
19463         local stripe_count
19464
19465         mkdir $DIR/$tdir
19466         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19467                 error "set striped dir error"
19468
19469         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19470         test_300_check_default_striped_dir striped_dir 1 0
19471         test_300_check_default_striped_dir striped_dir 2 1
19472         test_300_check_default_striped_dir striped_dir 2 -1
19473
19474         #delete default stripe information
19475         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19476                 error "set default stripe on striped dir error"
19477
19478         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19479         for dir in $(find $DIR/$tdir/striped_dir/*); do
19480                 stripe_count=$($LFS getdirstripe -c $dir)
19481                 [ $stripe_count -eq 0 ] ||
19482                         error "expect 1 get $stripe_count for $dir"
19483         done
19484 }
19485 run_test 300h "check default striped directory for striped directory"
19486
19487 test_300i() {
19488         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19489         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19490         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19491                 skip "Need MDS version at least 2.7.55"
19492
19493         local stripe_count
19494         local file
19495
19496         mkdir $DIR/$tdir
19497
19498         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19499                 error "set striped dir error"
19500
19501         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19502                 error "create files under striped dir failed"
19503
19504         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
19505                 error "set striped hashdir error"
19506
19507         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
19508                 error "create dir0 under hash dir failed"
19509         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
19510                 error "create dir1 under hash dir failed"
19511
19512         # unfortunately, we need to umount to clear dir layout cache for now
19513         # once we fully implement dir layout, we can drop this
19514         umount_client $MOUNT || error "umount failed"
19515         mount_client $MOUNT || error "mount failed"
19516
19517         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
19518         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
19519         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
19520
19521         #set the stripe to be unknown hash type
19522         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
19523         $LCTL set_param fail_loc=0x1901
19524         for ((i = 0; i < 10; i++)); do
19525                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
19526                         error "stat f-$i failed"
19527                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
19528         done
19529
19530         touch $DIR/$tdir/striped_dir/f0 &&
19531                 error "create under striped dir with unknown hash should fail"
19532
19533         $LCTL set_param fail_loc=0
19534
19535         umount_client $MOUNT || error "umount failed"
19536         mount_client $MOUNT || error "mount failed"
19537
19538         return 0
19539 }
19540 run_test 300i "client handle unknown hash type striped directory"
19541
19542 test_300j() {
19543         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19544         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19545         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19546                 skip "Need MDS version at least 2.7.55"
19547
19548         local stripe_count
19549         local file
19550
19551         mkdir $DIR/$tdir
19552
19553         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
19554         $LCTL set_param fail_loc=0x1702
19555         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19556                 error "set striped dir error"
19557
19558         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19559                 error "create files under striped dir failed"
19560
19561         $LCTL set_param fail_loc=0
19562
19563         rm -rf $DIR/$tdir || error "unlink striped dir fails"
19564
19565         return 0
19566 }
19567 run_test 300j "test large update record"
19568
19569 test_300k() {
19570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19571         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19572         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19573                 skip "Need MDS version at least 2.7.55"
19574
19575         local stripe_count
19576         local file
19577
19578         mkdir $DIR/$tdir
19579
19580         #define OBD_FAIL_LARGE_STRIPE   0x1703
19581         $LCTL set_param fail_loc=0x1703
19582         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
19583                 error "set striped dir error"
19584         $LCTL set_param fail_loc=0
19585
19586         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19587                 error "getstripeddir fails"
19588         rm -rf $DIR/$tdir/striped_dir ||
19589                 error "unlink striped dir fails"
19590
19591         return 0
19592 }
19593 run_test 300k "test large striped directory"
19594
19595 test_300l() {
19596         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19597         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19598         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19599                 skip "Need MDS version at least 2.7.55"
19600
19601         local stripe_index
19602
19603         test_mkdir -p $DIR/$tdir/striped_dir
19604         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
19605                         error "chown $RUNAS_ID failed"
19606         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19607                 error "set default striped dir failed"
19608
19609         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19610         $LCTL set_param fail_loc=0x80000158
19611         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19612
19613         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19614         [ $stripe_index -eq 1 ] ||
19615                 error "expect 1 get $stripe_index for $dir"
19616 }
19617 run_test 300l "non-root user to create dir under striped dir with stale layout"
19618
19619 test_300m() {
19620         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19621         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19622         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19623                 skip "Need MDS version at least 2.7.55"
19624
19625         mkdir -p $DIR/$tdir/striped_dir
19626         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19627                 error "set default stripes dir error"
19628
19629         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19630
19631         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19632         [ $stripe_count -eq 0 ] ||
19633                         error "expect 0 get $stripe_count for a"
19634
19635         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19636                 error "set default stripes dir error"
19637
19638         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19639
19640         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19641         [ $stripe_count -eq 0 ] ||
19642                         error "expect 0 get $stripe_count for b"
19643
19644         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19645                 error "set default stripes dir error"
19646
19647         mkdir $DIR/$tdir/striped_dir/c &&
19648                 error "default stripe_index is invalid, mkdir c should fails"
19649
19650         rm -rf $DIR/$tdir || error "rmdir fails"
19651 }
19652 run_test 300m "setstriped directory on single MDT FS"
19653
19654 cleanup_300n() {
19655         local list=$(comma_list $(mdts_nodes))
19656
19657         trap 0
19658         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19659 }
19660
19661 test_300n() {
19662         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19663         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19664         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19665                 skip "Need MDS version at least 2.7.55"
19666         remote_mds_nodsh && skip "remote MDS with nodsh"
19667
19668         local stripe_index
19669         local list=$(comma_list $(mdts_nodes))
19670
19671         trap cleanup_300n RETURN EXIT
19672         mkdir -p $DIR/$tdir
19673         chmod 777 $DIR/$tdir
19674         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19675                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19676                 error "create striped dir succeeds with gid=0"
19677
19678         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19679         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19680                 error "create striped dir fails with gid=-1"
19681
19682         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19683         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
19684                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19685                 error "set default striped dir succeeds with gid=0"
19686
19687
19688         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19689         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
19690                 error "set default striped dir fails with gid=-1"
19691
19692
19693         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19694         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
19695                                         error "create test_dir fails"
19696         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
19697                                         error "create test_dir1 fails"
19698         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
19699                                         error "create test_dir2 fails"
19700         cleanup_300n
19701 }
19702 run_test 300n "non-root user to create dir under striped dir with default EA"
19703
19704 test_300o() {
19705         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19706         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19707         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19708                 skip "Need MDS version at least 2.7.55"
19709
19710         local numfree1
19711         local numfree2
19712
19713         mkdir -p $DIR/$tdir
19714
19715         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
19716         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
19717         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
19718                 skip "not enough free inodes $numfree1 $numfree2"
19719         fi
19720
19721         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
19722         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
19723         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
19724                 skip "not enough free space $numfree1 $numfree2"
19725         fi
19726
19727         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
19728                 error "setdirstripe fails"
19729
19730         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
19731                 error "create dirs fails"
19732
19733         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
19734         ls $DIR/$tdir/striped_dir > /dev/null ||
19735                 error "ls striped dir fails"
19736         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
19737                 error "unlink big striped dir fails"
19738 }
19739 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
19740
19741 test_300p() {
19742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19743         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19744         remote_mds_nodsh && skip "remote MDS with nodsh"
19745
19746         mkdir -p $DIR/$tdir
19747
19748         #define OBD_FAIL_OUT_ENOSPC     0x1704
19749         do_facet mds2 lctl set_param fail_loc=0x80001704
19750         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
19751                  && error "create striped directory should fail"
19752
19753         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
19754
19755         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
19756         true
19757 }
19758 run_test 300p "create striped directory without space"
19759
19760 test_300q() {
19761         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19762         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19763
19764         local fd=$(free_fd)
19765         local cmd="exec $fd<$tdir"
19766         cd $DIR
19767         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
19768         eval $cmd
19769         cmd="exec $fd<&-"
19770         trap "eval $cmd" EXIT
19771         cd $tdir || error "cd $tdir fails"
19772         rmdir  ../$tdir || error "rmdir $tdir fails"
19773         mkdir local_dir && error "create dir succeeds"
19774         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
19775         eval $cmd
19776         return 0
19777 }
19778 run_test 300q "create remote directory under orphan directory"
19779
19780 test_300r() {
19781         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
19782                 skip "Need MDS version at least 2.7.55" && return
19783         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19784
19785         mkdir $DIR/$tdir
19786
19787         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
19788                 error "set striped dir error"
19789
19790         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19791                 error "getstripeddir fails"
19792
19793         local stripe_count
19794         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
19795                       awk '/lmv_stripe_count:/ { print $2 }')
19796
19797         [ $MDSCOUNT -ne $stripe_count ] &&
19798                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
19799
19800         rm -rf $DIR/$tdir/striped_dir ||
19801                 error "unlink striped dir fails"
19802 }
19803 run_test 300r "test -1 striped directory"
19804
19805 test_300s_helper() {
19806         local count=$1
19807
19808         local stripe_dir=$DIR/$tdir/striped_dir.$count
19809
19810         $LFS mkdir -c $count $stripe_dir ||
19811                 error "lfs mkdir -c error"
19812
19813         $LFS getdirstripe $stripe_dir ||
19814                 error "lfs getdirstripe fails"
19815
19816         local stripe_count
19817         stripe_count=$($LFS getdirstripe $stripe_dir |
19818                       awk '/lmv_stripe_count:/ { print $2 }')
19819
19820         [ $count -ne $stripe_count ] &&
19821                 error_noexit "bad stripe count $stripe_count expected $count"
19822
19823         local dupe_stripes
19824         dupe_stripes=$($LFS getdirstripe $stripe_dir |
19825                 awk '/0x/ {count[$1] += 1}; END {
19826                         for (idx in count) {
19827                                 if (count[idx]>1) {
19828                                         print "index " idx " count " count[idx]
19829                                 }
19830                         }
19831                 }')
19832
19833         if [[ -n "$dupe_stripes" ]] ; then
19834                 lfs getdirstripe $stripe_dir
19835                 error_noexit "Dupe MDT above: $dupe_stripes "
19836         fi
19837
19838         rm -rf $stripe_dir ||
19839                 error_noexit "unlink $stripe_dir fails"
19840 }
19841
19842 test_300s() {
19843         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19844                 skip "Need MDS version at least 2.7.55" && return
19845         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19846
19847         mkdir $DIR/$tdir
19848         for count in $(seq 2 $MDSCOUNT); do
19849                 test_300s_helper $count
19850         done
19851 }
19852 run_test 300s "test lfs mkdir -c without -i"
19853
19854
19855 prepare_remote_file() {
19856         mkdir $DIR/$tdir/src_dir ||
19857                 error "create remote source failed"
19858
19859         cp /etc/hosts $DIR/$tdir/src_dir/a ||
19860                  error "cp to remote source failed"
19861         touch $DIR/$tdir/src_dir/a
19862
19863         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
19864                 error "create remote target dir failed"
19865
19866         touch $DIR/$tdir/tgt_dir/b
19867
19868         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
19869                 error "rename dir cross MDT failed!"
19870
19871         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
19872                 error "src_child still exists after rename"
19873
19874         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
19875                 error "missing file(a) after rename"
19876
19877         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
19878                 error "diff after rename"
19879 }
19880
19881 test_310a() {
19882         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19883         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19884
19885         local remote_file=$DIR/$tdir/tgt_dir/b
19886
19887         mkdir -p $DIR/$tdir
19888
19889         prepare_remote_file || error "prepare remote file failed"
19890
19891         #open-unlink file
19892         $OPENUNLINK $remote_file $remote_file ||
19893                 error "openunlink $remote_file failed"
19894         $CHECKSTAT -a $remote_file || error "$remote_file exists"
19895 }
19896 run_test 310a "open unlink remote file"
19897
19898 test_310b() {
19899         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19901
19902         local remote_file=$DIR/$tdir/tgt_dir/b
19903
19904         mkdir -p $DIR/$tdir
19905
19906         prepare_remote_file || error "prepare remote file failed"
19907
19908         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19909         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
19910         $CHECKSTAT -t file $remote_file || error "check file failed"
19911 }
19912 run_test 310b "unlink remote file with multiple links while open"
19913
19914 test_310c() {
19915         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19916         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
19917
19918         local remote_file=$DIR/$tdir/tgt_dir/b
19919
19920         mkdir -p $DIR/$tdir
19921
19922         prepare_remote_file || error "prepare remote file failed"
19923
19924         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19925         multiop_bg_pause $remote_file O_uc ||
19926                         error "mulitop failed for remote file"
19927         MULTIPID=$!
19928         $MULTIOP $DIR/$tfile Ouc
19929         kill -USR1 $MULTIPID
19930         wait $MULTIPID
19931 }
19932 run_test 310c "open-unlink remote file with multiple links"
19933
19934 #LU-4825
19935 test_311() {
19936         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19937         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19938         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
19939                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
19940         remote_mds_nodsh && skip "remote MDS with nodsh"
19941
19942         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19943         local mdts=$(comma_list $(mdts_nodes))
19944
19945         mkdir -p $DIR/$tdir
19946         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
19947         createmany -o $DIR/$tdir/$tfile. 1000
19948
19949         # statfs data is not real time, let's just calculate it
19950         old_iused=$((old_iused + 1000))
19951
19952         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19953                         osp.*OST0000*MDT0000.create_count")
19954         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19955                                 osp.*OST0000*MDT0000.max_create_count")
19956         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
19957
19958         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
19959         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
19960         [ $index -ne 0 ] || error "$tfile stripe index is 0"
19961
19962         unlinkmany $DIR/$tdir/$tfile. 1000
19963
19964         do_nodes $mdts "$LCTL set_param -n \
19965                         osp.*OST0000*.max_create_count=$max_count"
19966         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
19967                 do_nodes $mdts "$LCTL set_param -n \
19968                                 osp.*OST0000*.create_count=$count"
19969         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
19970                         grep "=0" && error "create_count is zero"
19971
19972         local new_iused
19973         for i in $(seq 120); do
19974                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19975                 # system may be too busy to destroy all objs in time, use
19976                 # a somewhat small value to not fail autotest
19977                 [ $((old_iused - new_iused)) -gt 400 ] && break
19978                 sleep 1
19979         done
19980
19981         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
19982         [ $((old_iused - new_iused)) -gt 400 ] ||
19983                 error "objs not destroyed after unlink"
19984 }
19985 run_test 311 "disable OSP precreate, and unlink should destroy objs"
19986
19987 zfs_oid_to_objid()
19988 {
19989         local ost=$1
19990         local objid=$2
19991
19992         local vdevdir=$(dirname $(facet_vdevice $ost))
19993         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
19994         local zfs_zapid=$(do_facet $ost $cmd |
19995                           grep -w "/O/0/d$((objid%32))" -C 5 |
19996                           awk '/Object/{getline; print $1}')
19997         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
19998                           awk "/$objid = /"'{printf $3}')
19999
20000         echo $zfs_objid
20001 }
20002
20003 zfs_object_blksz() {
20004         local ost=$1
20005         local objid=$2
20006
20007         local vdevdir=$(dirname $(facet_vdevice $ost))
20008         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
20009         local blksz=$(do_facet $ost $cmd $objid |
20010                       awk '/dblk/{getline; printf $4}')
20011
20012         case "${blksz: -1}" in
20013                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
20014                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
20015                 *) ;;
20016         esac
20017
20018         echo $blksz
20019 }
20020
20021 test_312() { # LU-4856
20022         remote_ost_nodsh && skip "remote OST with nodsh"
20023         [ "$ost1_FSTYPE" = "zfs" ] ||
20024                 skip_env "the test only applies to zfs"
20025
20026         local max_blksz=$(do_facet ost1 \
20027                           $ZFS get -p recordsize $(facet_device ost1) |
20028                           awk '!/VALUE/{print $3}')
20029
20030         # to make life a little bit easier
20031         $LFS mkdir -c 1 -i 0 $DIR/$tdir
20032         $LFS setstripe -c 1 -i 0 $DIR/$tdir
20033
20034         local tf=$DIR/$tdir/$tfile
20035         touch $tf
20036         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20037
20038         # Get ZFS object id
20039         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20040         # block size change by sequential overwrite
20041         local bs
20042
20043         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
20044                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
20045
20046                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
20047                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
20048         done
20049         rm -f $tf
20050
20051         # block size change by sequential append write
20052         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
20053         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20054         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20055         local count
20056
20057         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
20058                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
20059                         oflag=sync conv=notrunc
20060
20061                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
20062                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
20063                         error "blksz error, actual $blksz, " \
20064                                 "expected: 2 * $count * $PAGE_SIZE"
20065         done
20066         rm -f $tf
20067
20068         # random write
20069         touch $tf
20070         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
20071         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
20072
20073         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
20074         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20075         [ $blksz -eq $PAGE_SIZE ] ||
20076                 error "blksz error: $blksz, expected: $PAGE_SIZE"
20077
20078         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
20079         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20080         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
20081
20082         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
20083         blksz=$(zfs_object_blksz ost1 $zfs_objid)
20084         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
20085 }
20086 run_test 312 "make sure ZFS adjusts its block size by write pattern"
20087
20088 test_313() {
20089         remote_ost_nodsh && skip "remote OST with nodsh"
20090
20091         local file=$DIR/$tfile
20092
20093         rm -f $file
20094         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
20095
20096         # define OBD_FAIL_TGT_RCVD_EIO           0x720
20097         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20098         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
20099                 error "write should failed"
20100         do_facet ost1 "$LCTL set_param fail_loc=0"
20101         rm -f $file
20102 }
20103 run_test 313 "io should fail after last_rcvd update fail"
20104
20105 test_314() {
20106         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20107
20108         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
20109         do_facet ost1 "$LCTL set_param fail_loc=0x720"
20110         rm -f $DIR/$tfile
20111         wait_delete_completed
20112         do_facet ost1 "$LCTL set_param fail_loc=0"
20113 }
20114 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
20115
20116 test_315() { # LU-618
20117         local file=$DIR/$tfile
20118         rm -f $file
20119
20120         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
20121                 error "multiop file write failed"
20122         $MULTIOP $file oO_RDONLY:r4063232_c &
20123         PID=$!
20124
20125         sleep 2
20126
20127         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20128         kill -USR1 $PID
20129
20130         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20131         rm -f $file
20132 }
20133 run_test 315 "read should be accounted"
20134
20135 test_316() {
20136         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20137         large_xattr_enabled || skip_env "ea_inode feature disabled"
20138
20139         rm -rf $DIR/$tdir/d
20140         mkdir -p $DIR/$tdir/d
20141         chown nobody $DIR/$tdir/d
20142         touch $DIR/$tdir/d/file
20143
20144         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
20145 }
20146 run_test 316 "lfs mv"
20147
20148 test_317() {
20149         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20150                 skip "Need MDS version at least 2.11.53"
20151         if [ "$ost1_FSTYPE" == "zfs" ]; then
20152                 skip "LU-10370: no implementation for ZFS"
20153         fi
20154
20155         local trunc_sz
20156         local grant_blk_size
20157
20158         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20159                         awk '/grant_block_size:/ { print $2; exit; }')
20160         #
20161         # Create File of size 5M. Truncate it to below size's and verify
20162         # blocks count.
20163         #
20164         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20165                 error "Create file $DIR/$tfile failed"
20166         stack_trap "rm -f $DIR/$tfile" EXIT
20167
20168         for trunc_sz in 2097152 4097 4000 509 0; do
20169                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20170                         error "truncate $tfile to $trunc_sz failed"
20171                 local sz=$(stat --format=%s $DIR/$tfile)
20172                 local blk=$(stat --format=%b $DIR/$tfile)
20173                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20174                                      grant_blk_size) * 8))
20175
20176                 if [[ $blk -ne $trunc_blk ]]; then
20177                         $(which stat) $DIR/$tfile
20178                         error "Expected Block $trunc_blk got $blk for $tfile"
20179                 fi
20180
20181                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20182                         error "Expected Size $trunc_sz got $sz for $tfile"
20183         done
20184
20185         #
20186         # sparse file test
20187         # Create file with a hole and write actual two blocks. Block count
20188         # must be 16.
20189         #
20190         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20191                 conv=fsync || error "Create file : $DIR/$tfile"
20192
20193         # Calculate the final truncate size.
20194         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20195
20196         #
20197         # truncate to size $trunc_sz bytes. Strip the last block
20198         # The block count must drop to 8
20199         #
20200         $TRUNCATE $DIR/$tfile $trunc_sz ||
20201                 error "truncate $tfile to $trunc_sz failed"
20202
20203         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20204         sz=$(stat --format=%s $DIR/$tfile)
20205         blk=$(stat --format=%b $DIR/$tfile)
20206
20207         if [[ $blk -ne $trunc_bsz ]]; then
20208                 $(which stat) $DIR/$tfile
20209                 error "Expected Block $trunc_bsz got $blk for $tfile"
20210         fi
20211
20212         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20213                 error "Expected Size $trunc_sz got $sz for $tfile"
20214 }
20215 run_test 317 "Verify blocks get correctly update after truncate"
20216
20217 test_fake_rw() {
20218         local read_write=$1
20219         if [ "$read_write" = "write" ]; then
20220                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20221         elif [ "$read_write" = "read" ]; then
20222                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20223         else
20224                 error "argument error"
20225         fi
20226
20227         # turn off debug for performance testing
20228         local saved_debug=$($LCTL get_param -n debug)
20229         $LCTL set_param debug=0
20230
20231         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
20232
20233         # get ost1 size - lustre-OST0000
20234         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20235         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20236         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20237
20238         if [ "$read_write" = "read" ]; then
20239                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20240         fi
20241
20242         local start_time=$(date +%s.%N)
20243         $dd_cmd bs=1M count=$blocks oflag=sync ||
20244                 error "real dd $read_write error"
20245         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20246
20247         if [ "$read_write" = "write" ]; then
20248                 rm -f $DIR/$tfile
20249         fi
20250
20251         # define OBD_FAIL_OST_FAKE_RW           0x238
20252         do_facet ost1 $LCTL set_param fail_loc=0x238
20253
20254         local start_time=$(date +%s.%N)
20255         $dd_cmd bs=1M count=$blocks oflag=sync ||
20256                 error "fake dd $read_write error"
20257         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20258
20259         if [ "$read_write" = "write" ]; then
20260                 # verify file size
20261                 cancel_lru_locks osc
20262                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20263                         error "$tfile size not $blocks MB"
20264         fi
20265         do_facet ost1 $LCTL set_param fail_loc=0
20266
20267         echo "fake $read_write $duration_fake vs. normal $read_write" \
20268                 "$duration in seconds"
20269         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20270                 error_not_in_vm "fake write is slower"
20271
20272         $LCTL set_param -n debug="$saved_debug"
20273         rm -f $DIR/$tfile
20274 }
20275 test_399a() { # LU-7655 for OST fake write
20276         remote_ost_nodsh && skip "remote OST with nodsh"
20277
20278         test_fake_rw write
20279 }
20280 run_test 399a "fake write should not be slower than normal write"
20281
20282 test_399b() { # LU-8726 for OST fake read
20283         remote_ost_nodsh && skip "remote OST with nodsh"
20284         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20285                 skip_env "ldiskfs only test"
20286         fi
20287
20288         test_fake_rw read
20289 }
20290 run_test 399b "fake read should not be slower than normal read"
20291
20292 test_400a() { # LU-1606, was conf-sanity test_74
20293         if ! which $CC > /dev/null 2>&1; then
20294                 skip_env "$CC is not installed"
20295         fi
20296
20297         local extra_flags=''
20298         local out=$TMP/$tfile
20299         local prefix=/usr/include/lustre
20300         local prog
20301
20302         # Oleg removes c files in his test rig so test if any c files exist
20303         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
20304                 skip_env "Needed c test files are missing"
20305
20306         if ! [[ -d $prefix ]]; then
20307                 # Assume we're running in tree and fixup the include path.
20308                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20309                 extra_flags+=" -L$LUSTRE/utils/.lib"
20310         fi
20311
20312         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20313                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
20314                         error "client api broken"
20315         done
20316         rm -f $out
20317 }
20318 run_test 400a "Lustre client api program can compile and link"
20319
20320 test_400b() { # LU-1606, LU-5011
20321         local header
20322         local out=$TMP/$tfile
20323         local prefix=/usr/include/linux/lustre
20324
20325         # We use a hard coded prefix so that this test will not fail
20326         # when run in tree. There are headers in lustre/include/lustre/
20327         # that are not packaged (like lustre_idl.h) and have more
20328         # complicated include dependencies (like config.h and lnet/types.h).
20329         # Since this test about correct packaging we just skip them when
20330         # they don't exist (see below) rather than try to fixup cppflags.
20331
20332         if ! which $CC > /dev/null 2>&1; then
20333                 skip_env "$CC is not installed"
20334         fi
20335
20336         for header in $prefix/*.h; do
20337                 if ! [[ -f "$header" ]]; then
20338                         continue
20339                 fi
20340
20341                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20342                         continue # lustre_ioctl.h is internal header
20343                 fi
20344
20345                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
20346                         error "cannot compile '$header'"
20347         done
20348         rm -f $out
20349 }
20350 run_test 400b "packaged headers can be compiled"
20351
20352 test_401a() { #LU-7437
20353         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20354         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20355
20356         #count the number of parameters by "list_param -R"
20357         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20358         #count the number of parameters by listing proc files
20359         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20360         echo "proc_dirs='$proc_dirs'"
20361         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20362         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20363                       sort -u | wc -l)
20364
20365         [ $params -eq $procs ] ||
20366                 error "found $params parameters vs. $procs proc files"
20367
20368         # test the list_param -D option only returns directories
20369         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20370         #count the number of parameters by listing proc directories
20371         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20372                 sort -u | wc -l)
20373
20374         [ $params -eq $procs ] ||
20375                 error "found $params parameters vs. $procs proc files"
20376 }
20377 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
20378
20379 test_401b() {
20380         local save=$($LCTL get_param -n jobid_var)
20381         local tmp=testing
20382
20383         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
20384                 error "no error returned when setting bad parameters"
20385
20386         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
20387         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
20388
20389         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
20390         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
20391         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
20392 }
20393 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
20394
20395 test_401c() {
20396         local jobid_var_old=$($LCTL get_param -n jobid_var)
20397         local jobid_var_new
20398
20399         $LCTL set_param jobid_var= &&
20400                 error "no error returned for 'set_param a='"
20401
20402         jobid_var_new=$($LCTL get_param -n jobid_var)
20403         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20404                 error "jobid_var was changed by setting without value"
20405
20406         $LCTL set_param jobid_var &&
20407                 error "no error returned for 'set_param a'"
20408
20409         jobid_var_new=$($LCTL get_param -n jobid_var)
20410         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20411                 error "jobid_var was changed by setting without value"
20412 }
20413 run_test 401c "Verify 'lctl set_param' without value fails in either format."
20414
20415 test_401d() {
20416         local jobid_var_old=$($LCTL get_param -n jobid_var)
20417         local jobid_var_new
20418         local new_value="foo=bar"
20419
20420         $LCTL set_param jobid_var=$new_value ||
20421                 error "'set_param a=b' did not accept a value containing '='"
20422
20423         jobid_var_new=$($LCTL get_param -n jobid_var)
20424         [[ "$jobid_var_new" == "$new_value" ]] ||
20425                 error "'set_param a=b' failed on a value containing '='"
20426
20427         # Reset the jobid_var to test the other format
20428         $LCTL set_param jobid_var=$jobid_var_old
20429         jobid_var_new=$($LCTL get_param -n jobid_var)
20430         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20431                 error "failed to reset jobid_var"
20432
20433         $LCTL set_param jobid_var $new_value ||
20434                 error "'set_param a b' did not accept a value containing '='"
20435
20436         jobid_var_new=$($LCTL get_param -n jobid_var)
20437         [[ "$jobid_var_new" == "$new_value" ]] ||
20438                 error "'set_param a b' failed on a value containing '='"
20439
20440         $LCTL set_param jobid_var $jobid_var_old
20441         jobid_var_new=$($LCTL get_param -n jobid_var)
20442         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20443                 error "failed to reset jobid_var"
20444 }
20445 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
20446
20447 test_402() {
20448         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
20449         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
20450                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
20451         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
20452                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
20453                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
20454         remote_mds_nodsh && skip "remote MDS with nodsh"
20455
20456         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
20457 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
20458         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
20459         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
20460                 echo "Touch failed - OK"
20461 }
20462 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
20463
20464 test_403() {
20465         local file1=$DIR/$tfile.1
20466         local file2=$DIR/$tfile.2
20467         local tfile=$TMP/$tfile
20468
20469         rm -f $file1 $file2 $tfile
20470
20471         touch $file1
20472         ln $file1 $file2
20473
20474         # 30 sec OBD_TIMEOUT in ll_getattr()
20475         # right before populating st_nlink
20476         $LCTL set_param fail_loc=0x80001409
20477         stat -c %h $file1 > $tfile &
20478
20479         # create an alias, drop all locks and reclaim the dentry
20480         < $file2
20481         cancel_lru_locks mdc
20482         cancel_lru_locks osc
20483         sysctl -w vm.drop_caches=2
20484
20485         wait
20486
20487         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
20488
20489         rm -f $tfile $file1 $file2
20490 }
20491 run_test 403 "i_nlink should not drop to zero due to aliasing"
20492
20493 test_404() { # LU-6601
20494         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
20495                 skip "Need server version newer than 2.8.52"
20496         remote_mds_nodsh && skip "remote MDS with nodsh"
20497
20498         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
20499                 awk '/osp .*-osc-MDT/ { print $4}')
20500
20501         local osp
20502         for osp in $mosps; do
20503                 echo "Deactivate: " $osp
20504                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
20505                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20506                         awk -vp=$osp '$4 == p { print $2 }')
20507                 [ $stat = IN ] || {
20508                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20509                         error "deactivate error"
20510                 }
20511                 echo "Activate: " $osp
20512                 do_facet $SINGLEMDS $LCTL --device %$osp activate
20513                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20514                         awk -vp=$osp '$4 == p { print $2 }')
20515                 [ $stat = UP ] || {
20516                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20517                         error "activate error"
20518                 }
20519         done
20520 }
20521 run_test 404 "validate manual {de}activated works properly for OSPs"
20522
20523 test_405() {
20524         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
20525                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
20526                         skip "Layout swap lock is not supported"
20527
20528         check_swap_layouts_support
20529
20530         test_mkdir $DIR/$tdir
20531         swap_lock_test -d $DIR/$tdir ||
20532                 error "One layout swap locked test failed"
20533 }
20534 run_test 405 "Various layout swap lock tests"
20535
20536 test_406() {
20537         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20538         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20539         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
20540         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20541         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
20542                 skip "Need MDS version at least 2.8.50"
20543
20544         local def_stripe_size=$($LFS getstripe -S $MOUNT)
20545         local test_pool=$TESTNAME
20546
20547         pool_add $test_pool || error "pool_add failed"
20548         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
20549                 error "pool_add_targets failed"
20550
20551         save_layout_restore_at_exit $MOUNT
20552
20553         # parent set default stripe count only, child will stripe from both
20554         # parent and fs default
20555         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
20556                 error "setstripe $MOUNT failed"
20557         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
20558         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
20559         for i in $(seq 10); do
20560                 local f=$DIR/$tdir/$tfile.$i
20561                 touch $f || error "touch failed"
20562                 local count=$($LFS getstripe -c $f)
20563                 [ $count -eq $OSTCOUNT ] ||
20564                         error "$f stripe count $count != $OSTCOUNT"
20565                 local offset=$($LFS getstripe -i $f)
20566                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
20567                 local size=$($LFS getstripe -S $f)
20568                 [ $size -eq $((def_stripe_size * 2)) ] ||
20569                         error "$f stripe size $size != $((def_stripe_size * 2))"
20570                 local pool=$($LFS getstripe -p $f)
20571                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
20572         done
20573
20574         # change fs default striping, delete parent default striping, now child
20575         # will stripe from new fs default striping only
20576         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
20577                 error "change $MOUNT default stripe failed"
20578         $LFS setstripe -c 0 $DIR/$tdir ||
20579                 error "delete $tdir default stripe failed"
20580         for i in $(seq 11 20); do
20581                 local f=$DIR/$tdir/$tfile.$i
20582                 touch $f || error "touch $f failed"
20583                 local count=$($LFS getstripe -c $f)
20584                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
20585                 local offset=$($LFS getstripe -i $f)
20586                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
20587                 local size=$($LFS getstripe -S $f)
20588                 [ $size -eq $def_stripe_size ] ||
20589                         error "$f stripe size $size != $def_stripe_size"
20590                 local pool=$($LFS getstripe -p $f)
20591                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
20592         done
20593
20594         unlinkmany $DIR/$tdir/$tfile. 1 20
20595
20596         local f=$DIR/$tdir/$tfile
20597         pool_remove_all_targets $test_pool $f
20598         pool_remove $test_pool $f
20599 }
20600 run_test 406 "DNE support fs default striping"
20601
20602 test_407() {
20603         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20604         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
20605                 skip "Need MDS version at least 2.8.55"
20606         remote_mds_nodsh && skip "remote MDS with nodsh"
20607
20608         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
20609                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
20610         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
20611                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
20612         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
20613
20614         #define OBD_FAIL_DT_TXN_STOP    0x2019
20615         for idx in $(seq $MDSCOUNT); do
20616                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
20617         done
20618         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
20619         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
20620                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
20621         true
20622 }
20623 run_test 407 "transaction fail should cause operation fail"
20624
20625 test_408() {
20626         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
20627
20628         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
20629         lctl set_param fail_loc=0x8000040a
20630         # let ll_prepare_partial_page() fail
20631         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
20632
20633         rm -f $DIR/$tfile
20634
20635         # create at least 100 unused inodes so that
20636         # shrink_icache_memory(0) should not return 0
20637         touch $DIR/$tfile-{0..100}
20638         rm -f $DIR/$tfile-{0..100}
20639         sync
20640
20641         echo 2 > /proc/sys/vm/drop_caches
20642 }
20643 run_test 408 "drop_caches should not hang due to page leaks"
20644
20645 test_409()
20646 {
20647         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20648         check_mount_and_prep
20649
20650         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
20651         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
20652         touch $DIR/$tdir/guard || error "(2) Fail to create"
20653
20654         local PREFIX=$(str_repeat 'A' 128)
20655         echo "Create 1K hard links start at $(date)"
20656         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20657                 error "(3) Fail to hard link"
20658
20659         echo "Links count should be right although linkEA overflow"
20660         stat $DIR/$tdir/guard || error "(4) Fail to stat"
20661         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
20662         [ $linkcount -eq 1001 ] ||
20663                 error "(5) Unexpected hard links count: $linkcount"
20664
20665         echo "List all links start at $(date)"
20666         ls -l $DIR/$tdir/foo > /dev/null ||
20667                 error "(6) Fail to list $DIR/$tdir/foo"
20668
20669         echo "Unlink hard links start at $(date)"
20670         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20671                 error "(7) Fail to unlink"
20672 }
20673 run_test 409 "Large amount of cross-MDTs hard links on the same file"
20674
20675 test_410()
20676 {
20677         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
20678                 skip "Need client version at least 2.9.59"
20679
20680         # Create a file, and stat it from the kernel
20681         local testfile=$DIR/$tfile
20682         touch $testfile
20683
20684         local run_id=$RANDOM
20685         local my_ino=$(stat --format "%i" $testfile)
20686
20687         # Try to insert the module. This will always fail as the
20688         # module is designed to not be inserted.
20689         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
20690             &> /dev/null
20691
20692         # Anything but success is a test failure
20693         dmesg | grep -q \
20694             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
20695             error "no inode match"
20696 }
20697 run_test 410 "Test inode number returned from kernel thread"
20698
20699 cleanup_test411_cgroup() {
20700         trap 0
20701         rmdir "$1"
20702 }
20703
20704 test_411() {
20705         local cg_basedir=/sys/fs/cgroup/memory
20706         # LU-9966
20707         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
20708                 skip "no setup for cgroup"
20709
20710         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
20711                 error "test file creation failed"
20712         cancel_lru_locks osc
20713
20714         # Create a very small memory cgroup to force a slab allocation error
20715         local cgdir=$cg_basedir/osc_slab_alloc
20716         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
20717         trap "cleanup_test411_cgroup $cgdir" EXIT
20718         echo 2M > $cgdir/memory.kmem.limit_in_bytes
20719         echo 1M > $cgdir/memory.limit_in_bytes
20720
20721         # Should not LBUG, just be killed by oom-killer
20722         # dd will return 0 even allocation failure in some environment.
20723         # So don't check return value
20724         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
20725         cleanup_test411_cgroup $cgdir
20726
20727         return 0
20728 }
20729 run_test 411 "Slab allocation error with cgroup does not LBUG"
20730
20731 test_412() {
20732         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20733         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20734                 skip "Need server version at least 2.10.55"
20735         fi
20736
20737         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
20738                 error "mkdir failed"
20739         $LFS getdirstripe $DIR/$tdir
20740         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20741         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
20742                 error "expect $((MDSCOUT - 1)) get $stripe_index"
20743         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
20744         [ $stripe_count -eq 2 ] ||
20745                 error "expect 2 get $stripe_count"
20746 }
20747 run_test 412 "mkdir on specific MDTs"
20748
20749 test_413() {
20750         [ $MDSCOUNT -lt 2 ] &&
20751                 skip "We need at least 2 MDTs for this test"
20752
20753         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20754                 skip "Need server version at least 2.10.55"
20755         fi
20756
20757         mkdir $DIR/$tdir || error "mkdir failed"
20758
20759         # find MDT that is the most full
20760         local max=$($LFS df | grep MDT |
20761                 awk 'BEGIN { a=0 }
20762                         { sub("%", "", $5)
20763                           if (0+$5 >= a)
20764                           {
20765                                 a = $5
20766                                 b = $6
20767                           }
20768                         }
20769                      END { split(b, c, ":")
20770                            sub("]", "", c[2])
20771                            print c[2]
20772                          }')
20773
20774         for i in $(seq $((MDSCOUNT - 1))); do
20775                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
20776                         error "mkdir d$i failed"
20777                 $LFS getdirstripe $DIR/$tdir/d$i
20778                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
20779                 [ $stripe_index -ne $max ] ||
20780                         error "don't expect $max"
20781         done
20782 }
20783 run_test 413 "mkdir on less full MDTs"
20784
20785 test_414() {
20786 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
20787         $LCTL set_param fail_loc=0x80000521
20788         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
20789         rm -f $DIR/$tfile
20790 }
20791 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
20792
20793 test_415() {
20794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20795         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
20796                 skip "Need server version at least 2.11.52"
20797
20798         # LU-11102
20799         local total
20800         local setattr_pid
20801         local start_time
20802         local end_time
20803         local duration
20804
20805         total=500
20806         # this test may be slow on ZFS
20807         [ "$mds1_FSTYPE" == "zfs" ] && total=100
20808
20809         # though this test is designed for striped directory, let's test normal
20810         # directory too since lock is always saved as CoS lock.
20811         test_mkdir $DIR/$tdir || error "mkdir $tdir"
20812         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
20813
20814         (
20815                 while true; do
20816                         touch $DIR/$tdir
20817                 done
20818         ) &
20819         setattr_pid=$!
20820
20821         start_time=$(date +%s)
20822         for i in $(seq $total); do
20823                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
20824                         > /dev/null
20825         done
20826         end_time=$(date +%s)
20827         duration=$((end_time - start_time))
20828
20829         kill -9 $setattr_pid
20830
20831         echo "rename $total files took $duration sec"
20832         [ $duration -lt 100 ] || error "rename took $duration sec"
20833 }
20834 run_test 415 "lock revoke is not missing"
20835
20836 test_416() {
20837         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
20838                 skip "Need server version at least 2.11.55"
20839
20840         # define OBD_FAIL_OSD_TXN_START    0x19a
20841         do_facet mds1 lctl set_param fail_loc=0x19a
20842
20843         lfs mkdir -c $MDSCOUNT $DIR/$tdir
20844
20845         true
20846 }
20847 run_test 416 "transaction start failure won't cause system hung"
20848
20849 cleanup_417() {
20850         trap 0
20851         do_nodes $(comma_list $(mdts_nodes)) \
20852                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
20853         do_nodes $(comma_list $(mdts_nodes)) \
20854                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
20855         do_nodes $(comma_list $(mdts_nodes)) \
20856                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
20857 }
20858
20859 test_417() {
20860         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20861         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
20862                 skip "Need MDS version at least 2.11.56"
20863
20864         trap cleanup_417 RETURN EXIT
20865
20866         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
20867         do_nodes $(comma_list $(mdts_nodes)) \
20868                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
20869         $LFS migrate -m 0 $DIR/$tdir.1 &&
20870                 error "migrate dir $tdir.1 should fail"
20871
20872         do_nodes $(comma_list $(mdts_nodes)) \
20873                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
20874         $LFS mkdir -i 1 $DIR/$tdir.2 &&
20875                 error "create remote dir $tdir.2 should fail"
20876
20877         do_nodes $(comma_list $(mdts_nodes)) \
20878                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
20879         $LFS mkdir -c 2 $DIR/$tdir.3 &&
20880                 error "create striped dir $tdir.3 should fail"
20881         true
20882 }
20883 run_test 417 "disable remote dir, striped dir and dir migration"
20884
20885 # Checks that the outputs of df [-i] and lfs df [-i] match
20886 #
20887 # usage: check_lfs_df <blocks | inodes> <mountpoint>
20888 check_lfs_df() {
20889         local dir=$2
20890         local inodes
20891         local df_out
20892         local lfs_df_out
20893         local count
20894         local passed=false
20895
20896         # blocks or inodes
20897         [ "$1" == "blocks" ] && inodes= || inodes="-i"
20898
20899         for count in {1..100}; do
20900                 cancel_lru_locks
20901                 sync; sleep 2
20902
20903                 # read the lines of interest
20904                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
20905                         error "df $inodes $dir | tail -n +2 failed"
20906                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
20907                         error "lfs df $inodes $dir | grep summary: failed"
20908
20909                 # skip first substrings of each output as they are different
20910                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
20911                 # compare the remaining fields of the two outputs
20912                 passed=true
20913                 for i in {1..5}; do
20914                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
20915                 done
20916                 $passed && break
20917         done
20918
20919         if ! $passed; then
20920                 df -P $inodes $dir
20921                 echo
20922                 lfs df $inodes $dir
20923                 error "df and lfs df $1 output mismatch: "      \
20924                       "df ${inodes}: ${df_out[*]}, "            \
20925                       "lfs df ${inodes}: ${lfs_df_out[*]}"
20926         fi
20927 }
20928
20929 test_418() {
20930         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20931
20932         local dir=$DIR/$tdir
20933         local numfiles=$((RANDOM % 4096 + 2))
20934         local numblocks=$((RANDOM % 256 + 1))
20935
20936         wait_delete_completed
20937         test_mkdir $dir
20938
20939         # check block output
20940         check_lfs_df blocks $dir
20941         # check inode output
20942         check_lfs_df inodes $dir
20943
20944         # create a single file and retest
20945         echo "Creating a single file and testing"
20946         createmany -o $dir/$tfile- 1 &>/dev/null ||
20947                 error "creating 1 file in $dir failed"
20948         check_lfs_df blocks $dir
20949         check_lfs_df inodes $dir
20950
20951         # create a random number of files
20952         echo "Creating $((numfiles - 1)) files and testing"
20953         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
20954                 error "creating $((numfiles - 1)) files in $dir failed"
20955
20956         # write a random number of blocks to the first test file
20957         echo "Writing $numblocks 4K blocks and testing"
20958         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
20959                 count=$numblocks &>/dev/null ||
20960                 error "dd to $dir/${tfile}-0 failed"
20961
20962         # retest
20963         check_lfs_df blocks $dir
20964         check_lfs_df inodes $dir
20965
20966         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
20967                 error "unlinking $numfiles files in $dir failed"
20968 }
20969 run_test 418 "df and lfs df outputs match"
20970
20971 test_419()
20972 {
20973         local dir=$DIR/$tdir
20974
20975         mkdir -p $dir
20976         touch $dir/file
20977
20978         cancel_lru_locks mdc
20979
20980         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
20981         $LCTL set_param fail_loc=0x1410
20982         cat $dir/file
20983         $LCTL set_param fail_loc=0
20984         rm -rf $dir
20985 }
20986 run_test 419 "Verify open file by name doesn't crash kernel"
20987
20988 test_420()
20989 {
20990         [[ $MDS1_VERSION -ge $(version_code 2.12.1) ]] ||
20991                 skip "Need MDS version at least 2.12.1"
20992
20993         local SAVE_UMASK=$(umask)
20994         local dir=$DIR/$tdir
20995         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
20996
20997         mkdir -p $dir
20998         umask 0000
20999         mkdir -m03777 $dir/testdir
21000         ls -dn $dir/testdir
21001         # Need to remove trailing '.' when SELinux is enabled
21002         local dirperms=$(ls -dn $dir/testdir |
21003                          awk '{ sub(/\.$/, "", $1); print $1}')
21004         [ $dirperms == "drwxrwsrwt" ] ||
21005                 error "incorrect perms on $dir/testdir"
21006
21007         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
21008                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
21009         ls -n $dir/testdir/testfile
21010         local fileperms=$(ls -n $dir/testdir/testfile |
21011                           awk '{ sub(/\.$/, "", $1); print $1}')
21012         [ $fileperms == "-rwxr-xr-x" ] ||
21013                 error "incorrect perms on $dir/testdir/testfile"
21014
21015         umask $SAVE_UMASK
21016 }
21017 run_test 420 "clear SGID bit on non-directories for non-members"
21018
21019 test_421a() {
21020         local cnt
21021         local fid1
21022         local fid2
21023
21024         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21025                 skip "Need MDS version at least 2.12.2"
21026
21027         test_mkdir $DIR/$tdir
21028         createmany -o $DIR/$tdir/f 3
21029         cnt=$(ls -1 $DIR/$tdir | wc -l)
21030         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21031
21032         fid1=$(lfs path2fid $DIR/$tdir/f1)
21033         fid2=$(lfs path2fid $DIR/$tdir/f2)
21034         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
21035
21036         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
21037         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
21038
21039         cnt=$(ls -1 $DIR/$tdir | wc -l)
21040         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21041
21042         rm -f $DIR/$tdir/f3 || error "can't remove f3"
21043         createmany -o $DIR/$tdir/f 3
21044         cnt=$(ls -1 $DIR/$tdir | wc -l)
21045         [ $cnt != 3 ] && error "unexpected #files: $cnt"
21046
21047         fid1=$(lfs path2fid $DIR/$tdir/f1)
21048         fid2=$(lfs path2fid $DIR/$tdir/f2)
21049         echo "remove using fsname $FSNAME"
21050         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
21051
21052         cnt=$(ls -1 $DIR/$tdir | wc -l)
21053         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
21054 }
21055 run_test 421a "simple rm by fid"
21056
21057 test_421b() {
21058         local cnt
21059         local FID1
21060         local FID2
21061
21062         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21063                 skip "Need MDS version at least 2.12.2"
21064
21065         test_mkdir $DIR/$tdir
21066         createmany -o $DIR/$tdir/f 3
21067         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
21068         MULTIPID=$!
21069
21070         FID1=$(lfs path2fid $DIR/$tdir/f1)
21071         FID2=$(lfs path2fid $DIR/$tdir/f2)
21072         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
21073
21074         kill -USR1 $MULTIPID
21075         wait
21076
21077         cnt=$(ls $DIR/$tdir | wc -l)
21078         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
21079 }
21080 run_test 421b "rm by fid on open file"
21081
21082 test_421c() {
21083         local cnt
21084         local FIDS
21085
21086         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21087                 skip "Need MDS version at least 2.12.2"
21088
21089         test_mkdir $DIR/$tdir
21090         createmany -o $DIR/$tdir/f 3
21091         touch $DIR/$tdir/$tfile
21092         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
21093         cnt=$(ls -1 $DIR/$tdir | wc -l)
21094         [ $cnt != 184 ] && error "unexpected #files: $cnt"
21095
21096         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
21097         $LFS rmfid $DIR $FID1 || error "rmfid failed"
21098
21099         cnt=$(ls $DIR/$tdir | wc -l)
21100         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
21101 }
21102 run_test 421c "rm by fid against hardlinked files"
21103
21104 test_421d() {
21105         local cnt
21106         local FIDS
21107
21108         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21109                 skip "Need MDS version at least 2.12.2"
21110
21111         test_mkdir $DIR/$tdir
21112         createmany -o $DIR/$tdir/f 4097
21113         cnt=$(ls -1 $DIR/$tdir | wc -l)
21114         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
21115
21116         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
21117         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21118
21119         cnt=$(ls $DIR/$tdir | wc -l)
21120         rm -rf $DIR/$tdir
21121         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21122 }
21123 run_test 421d "rmfid en masse"
21124
21125 test_421e() {
21126         local cnt
21127         local FID
21128
21129         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21130         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21131                 skip "Need MDS version at least 2.12.2"
21132
21133         mkdir -p $DIR/$tdir
21134         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21135         createmany -o $DIR/$tdir/striped_dir/f 512
21136         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21137         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21138
21139         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21140                 sed "s/[/][^:]*://g")
21141         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21142
21143         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21144         rm -rf $DIR/$tdir
21145         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21146 }
21147 run_test 421e "rmfid in DNE"
21148
21149 test_421f() {
21150         local cnt
21151         local FID
21152
21153         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21154                 skip "Need MDS version at least 2.12.2"
21155
21156         test_mkdir $DIR/$tdir
21157         touch $DIR/$tdir/f
21158         cnt=$(ls -1 $DIR/$tdir | wc -l)
21159         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21160
21161         FID=$(lfs path2fid $DIR/$tdir/f)
21162         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21163         # rmfid should fail
21164         cnt=$(ls -1 $DIR/$tdir | wc -l)
21165         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21166
21167         chmod a+rw $DIR/$tdir
21168         ls -la $DIR/$tdir
21169         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
21170         # rmfid should fail
21171         cnt=$(ls -1 $DIR/$tdir | wc -l)
21172         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
21173
21174         rm -f $DIR/$tdir/f
21175         $RUNAS touch $DIR/$tdir/f
21176         FID=$(lfs path2fid $DIR/$tdir/f)
21177         echo "rmfid as root"
21178         $LFS rmfid $DIR $FID || error "rmfid as root failed"
21179         cnt=$(ls -1 $DIR/$tdir | wc -l)
21180         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
21181
21182         rm -f $DIR/$tdir/f
21183         $RUNAS touch $DIR/$tdir/f
21184         cnt=$(ls -1 $DIR/$tdir | wc -l)
21185         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
21186         FID=$(lfs path2fid $DIR/$tdir/f)
21187         # rmfid w/o user_fid2path mount option should fail
21188         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
21189         cnt=$(ls -1 $DIR/$tdir | wc -l)
21190         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
21191
21192         umount_client $MOUNT || error "failed to umount client"
21193         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
21194                 error "failed to mount client'"
21195
21196         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
21197         # rmfid should succeed
21198         cnt=$(ls -1 $DIR/$tdir | wc -l)
21199         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
21200
21201         # rmfid shouldn't allow to remove files due to dir's permission
21202         chmod a+rwx $DIR/$tdir
21203         touch $DIR/$tdir/f
21204         ls -la $DIR/$tdir
21205         FID=$(lfs path2fid $DIR/$tdir/f)
21206         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
21207
21208         umount_client $MOUNT || error "failed to umount client"
21209         mount_client $MOUNT "$MOUNT_OPTS" ||
21210                 error "failed to mount client'"
21211
21212 }
21213 run_test 421f "rmfid checks permissions"
21214
21215 test_421g() {
21216         local cnt
21217         local FIDS
21218
21219         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21220         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21221                 skip "Need MDS version at least 2.12.2"
21222
21223         mkdir -p $DIR/$tdir
21224         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21225         createmany -o $DIR/$tdir/striped_dir/f 512
21226         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21227         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21228
21229         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21230                 sed "s/[/][^:]*://g")
21231
21232         rm -f $DIR/$tdir/striped_dir/f1*
21233         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21234         removed=$((512 - cnt))
21235
21236         # few files have been just removed, so we expect
21237         # rmfid to fail on their fids
21238         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
21239         [ $removed != $errors ] && error "$errors != $removed"
21240
21241         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21242         rm -rf $DIR/$tdir
21243         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21244 }
21245 run_test 421g "rmfid to return errors properly"
21246
21247 stat_test() {
21248     df -h $MOUNT &
21249     df -h $MOUNT &
21250     df -h $MOUNT &
21251     df -h $MOUNT &
21252     df -h $MOUNT &
21253     df -h $MOUNT &
21254 }
21255
21256 test_423() {
21257     local _stats
21258     # ensure statfs cache is expired
21259     sleep 2;
21260
21261     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
21262     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
21263
21264     return 0
21265 }
21266 run_test 423 "statfs should return a right data"
21267
21268 prep_801() {
21269         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21270         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21271                 skip "Need server version at least 2.9.55"
21272
21273         start_full_debug_logging
21274 }
21275
21276 post_801() {
21277         stop_full_debug_logging
21278 }
21279
21280 barrier_stat() {
21281         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21282                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21283                            awk '/The barrier for/ { print $7 }')
21284                 echo $st
21285         else
21286                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
21287                 echo \'$st\'
21288         fi
21289 }
21290
21291 barrier_expired() {
21292         local expired
21293
21294         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21295                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21296                           awk '/will be expired/ { print $7 }')
21297         else
21298                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
21299         fi
21300
21301         echo $expired
21302 }
21303
21304 test_801a() {
21305         prep_801
21306
21307         echo "Start barrier_freeze at: $(date)"
21308         #define OBD_FAIL_BARRIER_DELAY          0x2202
21309         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21310         # Do not reduce barrier time - See LU-11873
21311         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
21312
21313         sleep 2
21314         local b_status=$(barrier_stat)
21315         echo "Got barrier status at: $(date)"
21316         [ "$b_status" = "'freezing_p1'" ] ||
21317                 error "(1) unexpected barrier status $b_status"
21318
21319         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21320         wait
21321         b_status=$(barrier_stat)
21322         [ "$b_status" = "'frozen'" ] ||
21323                 error "(2) unexpected barrier status $b_status"
21324
21325         local expired=$(barrier_expired)
21326         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
21327         sleep $((expired + 3))
21328
21329         b_status=$(barrier_stat)
21330         [ "$b_status" = "'expired'" ] ||
21331                 error "(3) unexpected barrier status $b_status"
21332
21333         # Do not reduce barrier time - See LU-11873
21334         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
21335                 error "(4) fail to freeze barrier"
21336
21337         b_status=$(barrier_stat)
21338         [ "$b_status" = "'frozen'" ] ||
21339                 error "(5) unexpected barrier status $b_status"
21340
21341         echo "Start barrier_thaw at: $(date)"
21342         #define OBD_FAIL_BARRIER_DELAY          0x2202
21343         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21344         do_facet mgs $LCTL barrier_thaw $FSNAME &
21345
21346         sleep 2
21347         b_status=$(barrier_stat)
21348         echo "Got barrier status at: $(date)"
21349         [ "$b_status" = "'thawing'" ] ||
21350                 error "(6) unexpected barrier status $b_status"
21351
21352         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21353         wait
21354         b_status=$(barrier_stat)
21355         [ "$b_status" = "'thawed'" ] ||
21356                 error "(7) unexpected barrier status $b_status"
21357
21358         #define OBD_FAIL_BARRIER_FAILURE        0x2203
21359         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
21360         do_facet mgs $LCTL barrier_freeze $FSNAME
21361
21362         b_status=$(barrier_stat)
21363         [ "$b_status" = "'failed'" ] ||
21364                 error "(8) unexpected barrier status $b_status"
21365
21366         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21367         do_facet mgs $LCTL barrier_thaw $FSNAME
21368
21369         post_801
21370 }
21371 run_test 801a "write barrier user interfaces and stat machine"
21372
21373 test_801b() {
21374         prep_801
21375
21376         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21377         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
21378         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
21379         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
21380         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
21381
21382         cancel_lru_locks mdc
21383
21384         # 180 seconds should be long enough
21385         do_facet mgs $LCTL barrier_freeze $FSNAME 180
21386
21387         local b_status=$(barrier_stat)
21388         [ "$b_status" = "'frozen'" ] ||
21389                 error "(6) unexpected barrier status $b_status"
21390
21391         mkdir $DIR/$tdir/d0/d10 &
21392         mkdir_pid=$!
21393
21394         touch $DIR/$tdir/d1/f13 &
21395         touch_pid=$!
21396
21397         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
21398         ln_pid=$!
21399
21400         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
21401         mv_pid=$!
21402
21403         rm -f $DIR/$tdir/d4/f12 &
21404         rm_pid=$!
21405
21406         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
21407
21408         # To guarantee taht the 'stat' is not blocked
21409         b_status=$(barrier_stat)
21410         [ "$b_status" = "'frozen'" ] ||
21411                 error "(8) unexpected barrier status $b_status"
21412
21413         # let above commands to run at background
21414         sleep 5
21415
21416         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
21417         ps -p $touch_pid || error "(10) touch should be blocked"
21418         ps -p $ln_pid || error "(11) link should be blocked"
21419         ps -p $mv_pid || error "(12) rename should be blocked"
21420         ps -p $rm_pid || error "(13) unlink should be blocked"
21421
21422         b_status=$(barrier_stat)
21423         [ "$b_status" = "'frozen'" ] ||
21424                 error "(14) unexpected barrier status $b_status"
21425
21426         do_facet mgs $LCTL barrier_thaw $FSNAME
21427         b_status=$(barrier_stat)
21428         [ "$b_status" = "'thawed'" ] ||
21429                 error "(15) unexpected barrier status $b_status"
21430
21431         wait $mkdir_pid || error "(16) mkdir should succeed"
21432         wait $touch_pid || error "(17) touch should succeed"
21433         wait $ln_pid || error "(18) link should succeed"
21434         wait $mv_pid || error "(19) rename should succeed"
21435         wait $rm_pid || error "(20) unlink should succeed"
21436
21437         post_801
21438 }
21439 run_test 801b "modification will be blocked by write barrier"
21440
21441 test_801c() {
21442         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21443
21444         prep_801
21445
21446         stop mds2 || error "(1) Fail to stop mds2"
21447
21448         do_facet mgs $LCTL barrier_freeze $FSNAME 30
21449
21450         local b_status=$(barrier_stat)
21451         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
21452                 do_facet mgs $LCTL barrier_thaw $FSNAME
21453                 error "(2) unexpected barrier status $b_status"
21454         }
21455
21456         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21457                 error "(3) Fail to rescan barrier bitmap"
21458
21459         # Do not reduce barrier time - See LU-11873
21460         do_facet mgs $LCTL barrier_freeze $FSNAME 20
21461
21462         b_status=$(barrier_stat)
21463         [ "$b_status" = "'frozen'" ] ||
21464                 error "(4) unexpected barrier status $b_status"
21465
21466         do_facet mgs $LCTL barrier_thaw $FSNAME
21467         b_status=$(barrier_stat)
21468         [ "$b_status" = "'thawed'" ] ||
21469                 error "(5) unexpected barrier status $b_status"
21470
21471         local devname=$(mdsdevname 2)
21472
21473         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
21474
21475         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21476                 error "(7) Fail to rescan barrier bitmap"
21477
21478         post_801
21479 }
21480 run_test 801c "rescan barrier bitmap"
21481
21482 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
21483 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
21484 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
21485 saved_MOUNT_OPTS=$MOUNT_OPTS
21486
21487 cleanup_802() {
21488         trap 0
21489
21490         stopall
21491         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
21492         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
21493         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
21494         MOUNT_OPTS=$saved_MOUNT_OPTS
21495         setupall
21496 }
21497
21498 test_802() {
21499         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
21500         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21501         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21502                 skip "Need server version at least 2.9.55"
21503
21504         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
21505
21506         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21507
21508         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
21509                 error "(2) Fail to copy"
21510
21511         trap cleanup_802 EXIT
21512
21513         # sync by force before remount as readonly
21514         sync; sync_all_data; sleep 3; sync_all_data
21515
21516         stopall
21517
21518         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
21519         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
21520         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
21521
21522         echo "Mount the server as read only"
21523         setupall server_only || error "(3) Fail to start servers"
21524
21525         echo "Mount client without ro should fail"
21526         mount_client $MOUNT &&
21527                 error "(4) Mount client without 'ro' should fail"
21528
21529         echo "Mount client with ro should succeed"
21530         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
21531         mount_client $MOUNT ||
21532                 error "(5) Mount client with 'ro' should succeed"
21533
21534         echo "Modify should be refused"
21535         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
21536
21537         echo "Read should be allowed"
21538         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
21539                 error "(7) Read should succeed under ro mode"
21540
21541         cleanup_802
21542 }
21543 run_test 802 "simulate readonly device"
21544
21545 test_803a() {
21546         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21547         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21548                 skip "MDS needs to be newer than 2.10.54"
21549
21550         mkdir -p $DIR/$tdir
21551         # Create some objects on all MDTs to trigger related logs objects
21552         for idx in $(seq $MDSCOUNT); do
21553                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
21554                         $DIR/$tdir/dir${idx} ||
21555                         error "Fail to create $DIR/$tdir/dir${idx}"
21556         done
21557
21558         sync; sleep 3
21559         wait_delete_completed # ensure old test cleanups are finished
21560         echo "before create:"
21561         $LFS df -i $MOUNT
21562         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21563
21564         for i in {1..10}; do
21565                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
21566                         error "Fail to create $DIR/$tdir/foo$i"
21567         done
21568
21569         sync; sleep 3
21570         echo "after create:"
21571         $LFS df -i $MOUNT
21572         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21573
21574         # allow for an llog to be cleaned up during the test
21575         [ $after_used -ge $((before_used + 10 - 1)) ] ||
21576                 error "before ($before_used) + 10 > after ($after_used)"
21577
21578         for i in {1..10}; do
21579                 rm -rf $DIR/$tdir/foo$i ||
21580                         error "Fail to remove $DIR/$tdir/foo$i"
21581         done
21582
21583         sleep 3 # avoid MDT return cached statfs
21584         wait_delete_completed
21585         echo "after unlink:"
21586         $LFS df -i $MOUNT
21587         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21588
21589         # allow for an llog to be created during the test
21590         [ $after_used -le $((before_used + 1)) ] ||
21591                 error "after ($after_used) > before ($before_used) + 1"
21592 }
21593 run_test 803a "verify agent object for remote object"
21594
21595 test_803b() {
21596         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21597         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
21598                 skip "MDS needs to be newer than 2.13.56"
21599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21600
21601         for i in $(seq 0 $((MDSCOUNT - 1))); do
21602                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
21603         done
21604
21605         local before=0
21606         local after=0
21607
21608         local tmp
21609
21610         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
21611         for i in $(seq 0 $((MDSCOUNT - 1))); do
21612                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
21613                         awk '/getattr/ { print $2 }')
21614                 before=$((before + tmp))
21615         done
21616         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
21617         for i in $(seq 0 $((MDSCOUNT - 1))); do
21618                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
21619                         awk '/getattr/ { print $2 }')
21620                 after=$((after + tmp))
21621         done
21622
21623         [ $before -eq $after ] || error "getattr count $before != $after"
21624 }
21625 run_test 803b "remote object can getattr from cache"
21626
21627 test_804() {
21628         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21629         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21630                 skip "MDS needs to be newer than 2.10.54"
21631         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
21632
21633         mkdir -p $DIR/$tdir
21634         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
21635                 error "Fail to create $DIR/$tdir/dir0"
21636
21637         local fid=$($LFS path2fid $DIR/$tdir/dir0)
21638         local dev=$(mdsdevname 2)
21639
21640         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21641                 grep ${fid} || error "NOT found agent entry for dir0"
21642
21643         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
21644                 error "Fail to create $DIR/$tdir/dir1"
21645
21646         touch $DIR/$tdir/dir1/foo0 ||
21647                 error "Fail to create $DIR/$tdir/dir1/foo0"
21648         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
21649         local rc=0
21650
21651         for idx in $(seq $MDSCOUNT); do
21652                 dev=$(mdsdevname $idx)
21653                 do_facet mds${idx} \
21654                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21655                         grep ${fid} && rc=$idx
21656         done
21657
21658         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
21659                 error "Fail to rename foo0 to foo1"
21660         if [ $rc -eq 0 ]; then
21661                 for idx in $(seq $MDSCOUNT); do
21662                         dev=$(mdsdevname $idx)
21663                         do_facet mds${idx} \
21664                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21665                         grep ${fid} && rc=$idx
21666                 done
21667         fi
21668
21669         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
21670                 error "Fail to rename foo1 to foo2"
21671         if [ $rc -eq 0 ]; then
21672                 for idx in $(seq $MDSCOUNT); do
21673                         dev=$(mdsdevname $idx)
21674                         do_facet mds${idx} \
21675                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21676                         grep ${fid} && rc=$idx
21677                 done
21678         fi
21679
21680         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
21681
21682         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
21683                 error "Fail to link to $DIR/$tdir/dir1/foo2"
21684         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
21685                 error "Fail to rename foo2 to foo0"
21686         unlink $DIR/$tdir/dir1/foo0 ||
21687                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
21688         rm -rf $DIR/$tdir/dir0 ||
21689                 error "Fail to rm $DIR/$tdir/dir0"
21690
21691         for idx in $(seq $MDSCOUNT); do
21692                 dev=$(mdsdevname $idx)
21693                 rc=0
21694
21695                 stop mds${idx}
21696                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
21697                         rc=$?
21698                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
21699                         error "mount mds$idx failed"
21700                 df $MOUNT > /dev/null 2>&1
21701
21702                 # e2fsck should not return error
21703                 [ $rc -eq 0 ] ||
21704                         error "e2fsck detected error on MDT${idx}: rc=$rc"
21705         done
21706 }
21707 run_test 804 "verify agent entry for remote entry"
21708
21709 cleanup_805() {
21710         do_facet $SINGLEMDS zfs set quota=$old $fsset
21711         unlinkmany $DIR/$tdir/f- 1000000
21712         trap 0
21713 }
21714
21715 test_805() {
21716         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
21717         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
21718         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
21719                 skip "netfree not implemented before 0.7"
21720         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
21721                 skip "Need MDS version at least 2.10.57"
21722
21723         local fsset
21724         local freekb
21725         local usedkb
21726         local old
21727         local quota
21728         local pref="osd-zfs.lustre-MDT0000."
21729
21730         # limit available space on MDS dataset to meet nospace issue
21731         # quickly. then ZFS 0.7.2 can use reserved space if asked
21732         # properly (using netfree flag in osd_declare_destroy()
21733         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
21734         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
21735                 gawk '{print $3}')
21736         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
21737         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
21738         let "usedkb=usedkb-freekb"
21739         let "freekb=freekb/2"
21740         if let "freekb > 5000"; then
21741                 let "freekb=5000"
21742         fi
21743         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
21744         trap cleanup_805 EXIT
21745         mkdir $DIR/$tdir
21746         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
21747         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
21748         rm -rf $DIR/$tdir || error "not able to remove"
21749         do_facet $SINGLEMDS zfs set quota=$old $fsset
21750         trap 0
21751 }
21752 run_test 805 "ZFS can remove from full fs"
21753
21754 # Size-on-MDS test
21755 check_lsom_data()
21756 {
21757         local file=$1
21758         local size=$($LFS getsom -s $file)
21759         local expect=$(stat -c %s $file)
21760
21761         [[ $size == $expect ]] ||
21762                 error "$file expected size: $expect, got: $size"
21763
21764         local blocks=$($LFS getsom -b $file)
21765         expect=$(stat -c %b $file)
21766         [[ $blocks == $expect ]] ||
21767                 error "$file expected blocks: $expect, got: $blocks"
21768 }
21769
21770 check_lsom_size()
21771 {
21772         local size=$($LFS getsom -s $1)
21773         local expect=$2
21774
21775         [[ $size == $expect ]] ||
21776                 error "$file expected size: $expect, got: $size"
21777 }
21778
21779 test_806() {
21780         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21781                 skip "Need MDS version at least 2.11.52"
21782
21783         local bs=1048576
21784
21785         touch $DIR/$tfile || error "touch $tfile failed"
21786
21787         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21788         save_lustre_params client "llite.*.xattr_cache" > $save
21789         lctl set_param llite.*.xattr_cache=0
21790         stack_trap "restore_lustre_params < $save" EXIT
21791
21792         # single-threaded write
21793         echo "Test SOM for single-threaded write"
21794         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
21795                 error "write $tfile failed"
21796         check_lsom_size $DIR/$tfile $bs
21797
21798         local num=32
21799         local size=$(($num * $bs))
21800         local offset=0
21801         local i
21802
21803         echo "Test SOM for single client multi-threaded($num) write"
21804         $TRUNCATE $DIR/$tfile 0
21805         for ((i = 0; i < $num; i++)); do
21806                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21807                 local pids[$i]=$!
21808                 offset=$((offset + $bs))
21809         done
21810         for (( i=0; i < $num; i++ )); do
21811                 wait ${pids[$i]}
21812         done
21813         check_lsom_size $DIR/$tfile $size
21814
21815         $TRUNCATE $DIR/$tfile 0
21816         for ((i = 0; i < $num; i++)); do
21817                 offset=$((offset - $bs))
21818                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21819                 local pids[$i]=$!
21820         done
21821         for (( i=0; i < $num; i++ )); do
21822                 wait ${pids[$i]}
21823         done
21824         check_lsom_size $DIR/$tfile $size
21825
21826         # multi-client writes
21827         num=$(get_node_count ${CLIENTS//,/ })
21828         size=$(($num * $bs))
21829         offset=0
21830         i=0
21831
21832         echo "Test SOM for multi-client ($num) writes"
21833         $TRUNCATE $DIR/$tfile 0
21834         for client in ${CLIENTS//,/ }; do
21835                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21836                 local pids[$i]=$!
21837                 i=$((i + 1))
21838                 offset=$((offset + $bs))
21839         done
21840         for (( i=0; i < $num; i++ )); do
21841                 wait ${pids[$i]}
21842         done
21843         check_lsom_size $DIR/$tfile $offset
21844
21845         i=0
21846         $TRUNCATE $DIR/$tfile 0
21847         for client in ${CLIENTS//,/ }; do
21848                 offset=$((offset - $bs))
21849                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21850                 local pids[$i]=$!
21851                 i=$((i + 1))
21852         done
21853         for (( i=0; i < $num; i++ )); do
21854                 wait ${pids[$i]}
21855         done
21856         check_lsom_size $DIR/$tfile $size
21857
21858         # verify truncate
21859         echo "Test SOM for truncate"
21860         $TRUNCATE $DIR/$tfile 1048576
21861         check_lsom_size $DIR/$tfile 1048576
21862         $TRUNCATE $DIR/$tfile 1234
21863         check_lsom_size $DIR/$tfile 1234
21864
21865         # verify SOM blocks count
21866         echo "Verify SOM block count"
21867         $TRUNCATE $DIR/$tfile 0
21868         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
21869                 error "failed to write file $tfile"
21870         check_lsom_data $DIR/$tfile
21871 }
21872 run_test 806 "Verify Lazy Size on MDS"
21873
21874 test_807() {
21875         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21876                 skip "Need MDS version at least 2.11.52"
21877
21878         # Registration step
21879         changelog_register || error "changelog_register failed"
21880         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
21881         changelog_users $SINGLEMDS | grep -q $cl_user ||
21882                 error "User $cl_user not found in changelog_users"
21883
21884         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21885         save_lustre_params client "llite.*.xattr_cache" > $save
21886         lctl set_param llite.*.xattr_cache=0
21887         stack_trap "restore_lustre_params < $save" EXIT
21888
21889         rm -rf $DIR/$tdir || error "rm $tdir failed"
21890         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
21891         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
21892         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
21893         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
21894                 error "truncate $tdir/trunc failed"
21895
21896         local bs=1048576
21897         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
21898                 error "write $tfile failed"
21899
21900         # multi-client wirtes
21901         local num=$(get_node_count ${CLIENTS//,/ })
21902         local offset=0
21903         local i=0
21904
21905         echo "Test SOM for multi-client ($num) writes"
21906         touch $DIR/$tfile || error "touch $tfile failed"
21907         $TRUNCATE $DIR/$tfile 0
21908         for client in ${CLIENTS//,/ }; do
21909                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21910                 local pids[$i]=$!
21911                 i=$((i + 1))
21912                 offset=$((offset + $bs))
21913         done
21914         for (( i=0; i < $num; i++ )); do
21915                 wait ${pids[$i]}
21916         done
21917
21918         sleep 5
21919         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
21920         check_lsom_data $DIR/$tdir/trunc
21921         check_lsom_data $DIR/$tdir/single_dd
21922         check_lsom_data $DIR/$tfile
21923
21924         rm -rf $DIR/$tdir
21925         # Deregistration step
21926         changelog_deregister || error "changelog_deregister failed"
21927 }
21928 run_test 807 "verify LSOM syncing tool"
21929
21930 check_som_nologged()
21931 {
21932         local lines=$($LFS changelog $FSNAME-MDT0000 |
21933                 grep 'x=trusted.som' | wc -l)
21934         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
21935 }
21936
21937 test_808() {
21938         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
21939                 skip "Need MDS version at least 2.11.55"
21940
21941         # Registration step
21942         changelog_register || error "changelog_register failed"
21943
21944         touch $DIR/$tfile || error "touch $tfile failed"
21945         check_som_nologged
21946
21947         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
21948                 error "write $tfile failed"
21949         check_som_nologged
21950
21951         $TRUNCATE $DIR/$tfile 1234
21952         check_som_nologged
21953
21954         $TRUNCATE $DIR/$tfile 1048576
21955         check_som_nologged
21956
21957         # Deregistration step
21958         changelog_deregister || error "changelog_deregister failed"
21959 }
21960 run_test 808 "Check trusted.som xattr not logged in Changelogs"
21961
21962 check_som_nodata()
21963 {
21964         $LFS getsom $1
21965         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
21966 }
21967
21968 test_809() {
21969         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
21970                 skip "Need MDS version at least 2.11.56"
21971
21972         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
21973                 error "failed to create DoM-only file $DIR/$tfile"
21974         touch $DIR/$tfile || error "touch $tfile failed"
21975         check_som_nodata $DIR/$tfile
21976
21977         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
21978                 error "write $tfile failed"
21979         check_som_nodata $DIR/$tfile
21980
21981         $TRUNCATE $DIR/$tfile 1234
21982         check_som_nodata $DIR/$tfile
21983
21984         $TRUNCATE $DIR/$tfile 4097
21985         check_som_nodata $DIR/$file
21986 }
21987 run_test 809 "Verify no SOM xattr store for DoM-only files"
21988
21989 test_810() {
21990         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21991         $GSS && skip_env "could not run with gss"
21992
21993         set_checksums 1
21994         stack_trap "set_checksums $ORIG_CSUM" EXIT
21995         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
21996
21997         local csum
21998         local before
21999         local after
22000         for csum in $CKSUM_TYPES; do
22001                 #define OBD_FAIL_OSC_NO_GRANT   0x411
22002                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
22003                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
22004                         eval set -- $i
22005                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
22006                         before=$(md5sum $DIR/$tfile)
22007                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
22008                         after=$(md5sum $DIR/$tfile)
22009                         [ "$before" == "$after" ] ||
22010                                 error "$csum: $before != $after bs=$1 seek=$2"
22011                 done
22012         done
22013 }
22014 run_test 810 "partial page writes on ZFS (LU-11663)"
22015
22016 test_812() {
22017         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
22018                 skip "OST < 2.12.51 doesn't support this fail_loc"
22019         [ "$SHARED_KEY" = true ] &&
22020                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22021
22022         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22023         # ensure ost1 is connected
22024         stat $DIR/$tfile >/dev/null || error "can't stat"
22025         wait_osc_import_state client ost1 FULL
22026         # no locks, no reqs to let the connection idle
22027         cancel_lru_locks osc
22028
22029         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
22030 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
22031         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
22032         wait_osc_import_state client ost1 CONNECTING
22033         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
22034
22035         stat $DIR/$tfile >/dev/null || error "can't stat file"
22036 }
22037 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
22038
22039 test_814()
22040 {
22041         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
22042         echo -n y >> $DIR/$tfile
22043         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
22044         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
22045 }
22046 run_test 814 "sparse cp works as expected (LU-12361)"
22047
22048 test_815()
22049 {
22050         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
22051         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
22052 }
22053 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
22054
22055 test_816() {
22056         local ost1_imp=$(get_osc_import_name client ost1)
22057         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
22058                          cut -d'.' -f2)
22059
22060         [ "$SHARED_KEY" = true ] &&
22061                 skip "OSC connections never go IDLE with Shared-Keys enabled"
22062
22063         $LFS setstripe -c 1 -i 0 $DIR/$tfile
22064         # ensure ost1 is connected
22065         stat $DIR/$tfile >/dev/null || error "can't stat"
22066         wait_osc_import_state client ost1 FULL
22067         # no locks, no reqs to let the connection idle
22068         cancel_lru_locks osc
22069         lru_resize_disable osc
22070         local before
22071         local now
22072         before=$($LCTL get_param -n \
22073                  ldlm.namespaces.$imp_name.lru_size)
22074
22075         wait_osc_import_state client ost1 IDLE
22076         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
22077         now=$($LCTL get_param -n \
22078               ldlm.namespaces.$imp_name.lru_size)
22079         [ $before == $now ] || error "lru_size changed $before != $now"
22080 }
22081 run_test 816 "do not reset lru_resize on idle reconnect"
22082
22083 cleanup_817() {
22084         umount $tmpdir
22085         exportfs -u localhost:$DIR/nfsexp
22086         rm -rf $DIR/nfsexp
22087 }
22088
22089 test_817() {
22090         systemctl restart nfs-server.service || skip "failed to restart nfsd"
22091
22092         mkdir -p $DIR/nfsexp
22093         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
22094                 error "failed to export nfs"
22095
22096         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
22097         stack_trap cleanup_817 EXIT
22098
22099         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
22100                 error "failed to mount nfs to $tmpdir"
22101
22102         cp /bin/true $tmpdir
22103         $DIR/nfsexp/true || error "failed to execute 'true' command"
22104 }
22105 run_test 817 "nfsd won't cache write lock for exec file"
22106
22107 test_819a() {
22108         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22109         cancel_lru_locks osc
22110         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22111         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22112         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
22113         rm -f $TDIR/$tfile
22114 }
22115 run_test 819a "too big niobuf in read"
22116
22117 test_819b() {
22118         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
22119         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
22120         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22121         cancel_lru_locks osc
22122         sleep 1
22123         rm -f $TDIR/$tfile
22124 }
22125 run_test 819b "too big niobuf in write"
22126
22127 test_818() {
22128         mkdir $DIR/$tdir
22129         $LFS setstripe -c1 -i0 $DIR/$tfile
22130         $LFS setstripe -c1 -i1 $DIR/$tfile
22131         stop $SINGLEMDS
22132         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
22133         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
22134         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
22135                 error "start $SINGLEMDS failed"
22136         rm -rf $DIR/$tdir
22137 }
22138 run_test 818 "unlink with failed llog"
22139
22140 #
22141 # tests that do cleanup/setup should be run at the end
22142 #
22143
22144 test_900() {
22145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22146         local ls
22147
22148         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
22149         $LCTL set_param fail_loc=0x903
22150
22151         cancel_lru_locks MGC
22152
22153         FAIL_ON_ERROR=true cleanup
22154         FAIL_ON_ERROR=true setup
22155 }
22156 run_test 900 "umount should not race with any mgc requeue thread"
22157
22158 # LU-13377
22159 test_902() {
22160         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
22161                 skip "client does not have LU-13377 fix"
22162         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
22163         $LCTL set_param fail_loc=0x1415
22164         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22165         cancel_lru_locks osc
22166         rm -f $DIR/$tfile
22167 }
22168 run_test 902 "test short write doesn't hang lustre"
22169
22170 complete $SECONDS
22171 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
22172 check_and_cleanup_lustre
22173 if [ "$I_MOUNTED" != "yes" ]; then
22174         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
22175 fi
22176 exit_status