Whamcloud - gitweb
LU-14413 test: test for overstriping for sanity 27M
[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.14.0 until they are fixed
61 if [ $LINUX_VERSION_CODE -ge $(version_code 4.14.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.52) ] ||
6854                 skip "Need MDS version at least 2.12.52"
6855         [ $MDSCOUNT -le 2 ] && skip "Need >= 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 # bug 1414 - set/get directories' stripe info
7243 test_65a() {
7244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7245
7246         test_mkdir $DIR/$tdir
7247         touch $DIR/$tdir/f1
7248         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7249 }
7250 run_test 65a "directory with no stripe info"
7251
7252 test_65b() {
7253         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7254
7255         test_mkdir $DIR/$tdir
7256         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7257
7258         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7259                                                 error "setstripe"
7260         touch $DIR/$tdir/f2
7261         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7262 }
7263 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7264
7265 test_65c() {
7266         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7267         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7268
7269         test_mkdir $DIR/$tdir
7270         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
7271
7272         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7273                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7274         touch $DIR/$tdir/f3
7275         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7276 }
7277 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7278
7279 test_65d() {
7280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7281
7282         test_mkdir $DIR/$tdir
7283         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
7284         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7285
7286         if [[ $STRIPECOUNT -le 0 ]]; then
7287                 sc=1
7288         elif [[ $STRIPECOUNT -gt 2000 ]]; then
7289 #LOV_MAX_STRIPE_COUNT is 2000
7290                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
7291         else
7292                 sc=$(($STRIPECOUNT - 1))
7293         fi
7294         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7295         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7296         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7297                 error "lverify failed"
7298 }
7299 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7300
7301 test_65e() {
7302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7303
7304         test_mkdir $DIR/$tdir
7305
7306         $SETSTRIPE $DIR/$tdir || error "setstripe"
7307         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
7308                                         error "no stripe info failed"
7309         touch $DIR/$tdir/f6
7310         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7311 }
7312 run_test 65e "directory setstripe defaults"
7313
7314 test_65f() {
7315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7316
7317         test_mkdir $DIR/${tdir}f
7318         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
7319 }
7320 run_test 65f "dir setstripe permission (should return error) ==="
7321
7322 test_65g() {
7323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7324
7325         test_mkdir $DIR/$tdir
7326         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7327
7328         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7329                 error "setstripe -S failed"
7330         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7331         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7332                 error "delete default stripe failed"
7333 }
7334 run_test 65g "directory setstripe -d"
7335
7336 test_65h() {
7337         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7338
7339         test_mkdir $DIR/$tdir
7340         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7341
7342         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7343                 error "setstripe -S failed"
7344         test_mkdir $DIR/$tdir/dd1
7345         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
7346                 error "stripe info inherit failed"
7347 }
7348 run_test 65h "directory stripe info inherit ===================="
7349
7350 test_65i() {
7351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7352
7353         save_layout_restore_at_exit $MOUNT
7354
7355         # bug6367: set non-default striping on root directory
7356         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7357
7358         # bug12836: getstripe on -1 default directory striping
7359         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7360
7361         # bug12836: getstripe -v on -1 default directory striping
7362         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7363
7364         # bug12836: new find on -1 default directory striping
7365         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7366 }
7367 run_test 65i "various tests to set root directory striping"
7368
7369 test_65j() { # bug6367
7370         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7371
7372         sync; sleep 1
7373
7374         # if we aren't already remounting for each test, do so for this test
7375         if [ "$I_MOUNTED" = "yes" ]; then
7376                 cleanup || error "failed to unmount"
7377                 setup
7378         fi
7379
7380         save_layout_restore_at_exit $MOUNT
7381
7382         $SETSTRIPE -d $MOUNT || error "setstripe failed"
7383 }
7384 run_test 65j "set default striping on root directory (bug 6367)="
7385
7386 cleanup_65k() {
7387         rm -rf $DIR/$tdir
7388         wait_delete_completed
7389         do_facet $SINGLEMDS "lctl set_param -n \
7390                 osp.$ost*MDT0000.max_create_count=$max_count"
7391         do_facet $SINGLEMDS "lctl set_param -n \
7392                 osp.$ost*MDT0000.create_count=$count"
7393         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7394         echo $INACTIVE_OSC "is Activate"
7395
7396         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7397 }
7398
7399 test_65k() { # bug11679
7400         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7401         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7402         remote_mds_nodsh && skip "remote MDS with nodsh"
7403
7404         local disable_precreate=true
7405         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7406                 disable_precreate=false
7407
7408         echo "Check OST status: "
7409         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7410                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7411
7412         for OSC in $MDS_OSCS; do
7413                 echo $OSC "is active"
7414                 do_facet $SINGLEMDS lctl --device %$OSC activate
7415         done
7416
7417         for INACTIVE_OSC in $MDS_OSCS; do
7418                 local ost=$(osc_to_ost $INACTIVE_OSC)
7419                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7420                                lov.*md*.target_obd |
7421                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7422
7423                 mkdir -p $DIR/$tdir
7424                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
7425                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7426
7427                 echo "Deactivate: " $INACTIVE_OSC
7428                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7429
7430                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7431                               osp.$ost*MDT0000.create_count")
7432                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7433                                   osp.$ost*MDT0000.max_create_count")
7434                 $disable_precreate &&
7435                         do_facet $SINGLEMDS "lctl set_param -n \
7436                                 osp.$ost*MDT0000.max_create_count=0"
7437
7438                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7439                         [ -f $DIR/$tdir/$idx ] && continue
7440                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
7441                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
7442                                 { cleanup_65k;
7443                                   error "setstripe $idx should succeed"; }
7444                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7445                 done
7446                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7447                 rmdir $DIR/$tdir
7448
7449                 do_facet $SINGLEMDS "lctl set_param -n \
7450                         osp.$ost*MDT0000.max_create_count=$max_count"
7451                 do_facet $SINGLEMDS "lctl set_param -n \
7452                         osp.$ost*MDT0000.create_count=$count"
7453                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7454                 echo $INACTIVE_OSC "is Activate"
7455
7456                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7457         done
7458 }
7459 run_test 65k "validate manual striping works properly with deactivated OSCs"
7460
7461 test_65l() { # bug 12836
7462         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7463
7464         test_mkdir -p $DIR/$tdir/test_dir
7465         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
7466         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7467 }
7468 run_test 65l "lfs find on -1 stripe dir ========================"
7469
7470 test_65m() {
7471         local layout=$(save_layout $MOUNT)
7472         $RUNAS $SETSTRIPE -c 2 $MOUNT && {
7473                 restore_layout $MOUNT $layout
7474                 error "setstripe should fail by non-root users"
7475         }
7476         true
7477 }
7478 run_test 65m "normal user can't set filesystem default stripe"
7479
7480 test_65n() {
7481         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.0) ]] ||
7482                 skip "Need MDS version at least 2.12.0"
7483         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7484
7485         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7486         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7487         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7488
7489         save_layout_restore_at_exit $MOUNT
7490
7491         # new subdirectory under root directory should not inherit
7492         # the default layout from root
7493         local dir1=$MOUNT/$tdir-1
7494         mkdir $dir1 || error "mkdir $dir1 failed"
7495         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7496                 error "$dir1 shouldn't have LOV EA"
7497
7498         # delete the default layout on root directory
7499         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7500
7501         local dir2=$MOUNT/$tdir-2
7502         mkdir $dir2 || error "mkdir $dir2 failed"
7503         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7504                 error "$dir2 shouldn't have LOV EA"
7505
7506         # set a new striping pattern on root directory
7507         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7508         local new_def_stripe_size=$((def_stripe_size * 2))
7509         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7510                 error "set stripe size on $MOUNT failed"
7511
7512         # new file created in $dir2 should inherit the new stripe size from
7513         # the filesystem default
7514         local file2=$dir2/$tfile-2
7515         touch $file2 || error "touch $file2 failed"
7516
7517         local file2_stripe_size=$($LFS getstripe -S $file2)
7518         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7519                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7520
7521         local dir3=$MOUNT/$tdir-3
7522         mkdir $dir3 || error "mkdir $dir3 failed"
7523         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
7524         # the root layout, which is the actual default layout that will be used
7525         # when new files are created in $dir3.
7526         local dir3_layout=$(get_layout_param $dir3)
7527         local root_dir_layout=$(get_layout_param $MOUNT)
7528         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
7529                 error "$dir3 should show the default layout from $MOUNT"
7530
7531         # set OST pool on root directory
7532         local pool=$TESTNAME
7533         pool_add $pool || error "add $pool failed"
7534         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7535                 error "add targets to $pool failed"
7536
7537         $LFS setstripe -p $pool $MOUNT ||
7538                 error "set OST pool on $MOUNT failed"
7539
7540         # new file created in $dir3 should inherit the pool from
7541         # the filesystem default
7542         local file3=$dir3/$tfile-3
7543         touch $file3 || error "touch $file3 failed"
7544
7545         local file3_pool=$($LFS getstripe -p $file3)
7546         [[ "$file3_pool" = "$pool" ]] ||
7547                 error "$file3 didn't inherit OST pool $pool"
7548
7549         local dir4=$MOUNT/$tdir-4
7550         mkdir $dir4 || error "mkdir $dir4 failed"
7551         local dir4_layout=$(get_layout_param $dir4)
7552         root_dir_layout=$(get_layout_param $MOUNT)
7553         echo "$LFS getstripe -d $dir4"
7554         $LFS getstripe -d $dir4
7555         echo "$LFS getstripe -d $MOUNT"
7556         $LFS getstripe -d $MOUNT
7557         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
7558                 error "$dir4 should show the default layout from $MOUNT"
7559
7560         # new file created in $dir4 should inherit the pool from
7561         # the filesystem default
7562         local file4=$dir4/$tfile-4
7563         touch $file4 || error "touch $file4 failed"
7564
7565         local file4_pool=$($LFS getstripe -p $file4)
7566         [[ "$file4_pool" = "$pool" ]] ||
7567                 error "$file4 didn't inherit OST pool $pool"
7568
7569         # new subdirectory under non-root directory should inherit
7570         # the default layout from its parent directory
7571         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7572                 error "set directory layout on $dir4 failed"
7573
7574         local dir5=$dir4/$tdir-5
7575         mkdir $dir5 || error "mkdir $dir5 failed"
7576
7577         dir4_layout=$(get_layout_param $dir4)
7578         local dir5_layout=$(get_layout_param $dir5)
7579         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7580                 error "$dir5 should inherit the default layout from $dir4"
7581
7582         # though subdir under ROOT doesn't inherit default layout, but
7583         # its sub dir/file should be created with default layout.
7584         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
7585         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
7586         [[ $MDS1_VERSION -ge $(version_code 2.12.3) &&
7587            $MDS1_VERSION -lt $(version_code 2.12.50) ]] ||
7588                 skip "Need MDS version at least 2.12.3 or 2.12.59"
7589
7590         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
7591         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
7592         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
7593
7594         if [ $default_lmv_hash == "none" ]; then
7595                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
7596         else
7597                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
7598                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
7599         fi
7600
7601         $LFS setdirstripe -D -c 2 $MOUNT ||
7602                 error "setdirstripe -D -c 2 failed"
7603         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
7604         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
7605         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
7606 }
7607 run_test 65n "don't inherit default layout from root for new subdirectories"
7608
7609 # bug 2543 - update blocks count on client
7610 test_66() {
7611         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7612
7613         COUNT=${COUNT:-8}
7614         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7615         sync; sync_all_data; sync; sync_all_data
7616         cancel_lru_locks osc
7617         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7618         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7619 }
7620 run_test 66 "update inode blocks count on client ==============="
7621
7622 meminfo() {
7623         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7624 }
7625
7626 swap_used() {
7627         swapon -s | awk '($1 == "'$1'") { print $4 }'
7628 }
7629
7630 # bug5265, obdfilter oa2dentry return -ENOENT
7631 # #define OBD_FAIL_SRV_ENOENT 0x217
7632 test_69() {
7633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7634         remote_ost_nodsh && skip "remote OST with nodsh"
7635
7636         f="$DIR/$tfile"
7637         $SETSTRIPE -c 1 -i 0 $f
7638
7639         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7640
7641         do_facet ost1 lctl set_param fail_loc=0x217
7642         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7643         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7644
7645         do_facet ost1 lctl set_param fail_loc=0
7646         $DIRECTIO write $f 0 2 || error "write error"
7647
7648         cancel_lru_locks osc
7649         $DIRECTIO read $f 0 1 || error "read error"
7650
7651         do_facet ost1 lctl set_param fail_loc=0x217
7652         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7653
7654         do_facet ost1 lctl set_param fail_loc=0
7655         rm -f $f
7656 }
7657 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
7658
7659 test_71() {
7660         test_mkdir $DIR/$tdir
7661         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
7662         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
7663 }
7664 run_test 71 "Running dbench on lustre (don't segment fault) ===="
7665
7666 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
7667         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7668         [ "$RUNAS_ID" = "$UID" ] &&
7669                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7670         # Check that testing environment is properly set up. Skip if not
7671         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
7672                 skip_env "User $RUNAS_ID does not exist - skipping"
7673
7674         touch $DIR/$tfile
7675         chmod 777 $DIR/$tfile
7676         chmod ug+s $DIR/$tfile
7677         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
7678                 error "$RUNAS dd $DIR/$tfile failed"
7679         # See if we are still setuid/sgid
7680         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7681                 error "S/gid is not dropped on write"
7682         # Now test that MDS is updated too
7683         cancel_lru_locks mdc
7684         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7685                 error "S/gid is not dropped on MDS"
7686         rm -f $DIR/$tfile
7687 }
7688 run_test 72a "Test that remove suid works properly (bug5695) ===="
7689
7690 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7691         local perm
7692
7693         [ "$RUNAS_ID" = "$UID" ] &&
7694                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7695         [ "$RUNAS_ID" -eq 0 ] &&
7696                 skip_env "RUNAS_ID = 0 -- skipping"
7697         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7698         # Check that testing environment is properly set up. Skip if not
7699         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7700                 skip_env "User $RUNAS_ID does not exist - skipping"
7701
7702         touch $DIR/${tfile}-f{g,u}
7703         test_mkdir $DIR/${tfile}-dg
7704         test_mkdir $DIR/${tfile}-du
7705         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7706         chmod g+s $DIR/${tfile}-{f,d}g
7707         chmod u+s $DIR/${tfile}-{f,d}u
7708         for perm in 777 2777 4777; do
7709                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7710                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7711                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7712                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7713         done
7714         true
7715 }
7716 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7717
7718 # bug 3462 - multiple simultaneous MDC requests
7719 test_73() {
7720         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7721
7722         test_mkdir $DIR/d73-1
7723         test_mkdir $DIR/d73-2
7724         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7725         pid1=$!
7726
7727         lctl set_param fail_loc=0x80000129
7728         $MULTIOP $DIR/d73-1/f73-2 Oc &
7729         sleep 1
7730         lctl set_param fail_loc=0
7731
7732         $MULTIOP $DIR/d73-2/f73-3 Oc &
7733         pid3=$!
7734
7735         kill -USR1 $pid1
7736         wait $pid1 || return 1
7737
7738         sleep 25
7739
7740         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7741         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7742         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7743
7744         rm -rf $DIR/d73-*
7745 }
7746 run_test 73 "multiple MDC requests (should not deadlock)"
7747
7748 test_74a() { # bug 6149, 6184
7749         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7750
7751         touch $DIR/f74a
7752         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7753         #
7754         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7755         # will spin in a tight reconnection loop
7756         $LCTL set_param fail_loc=0x8000030e
7757         # get any lock that won't be difficult - lookup works.
7758         ls $DIR/f74a
7759         $LCTL set_param fail_loc=0
7760         rm -f $DIR/f74a
7761         true
7762 }
7763 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
7764
7765 test_74b() { # bug 13310
7766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7767
7768         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7769         #
7770         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7771         # will spin in a tight reconnection loop
7772         $LCTL set_param fail_loc=0x8000030e
7773         # get a "difficult" lock
7774         touch $DIR/f74b
7775         $LCTL set_param fail_loc=0
7776         rm -f $DIR/f74b
7777         true
7778 }
7779 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
7780
7781 test_74c() {
7782         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7783
7784         #define OBD_FAIL_LDLM_NEW_LOCK
7785         $LCTL set_param fail_loc=0x319
7786         touch $DIR/$tfile && error "touch successful"
7787         $LCTL set_param fail_loc=0
7788         true
7789 }
7790 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
7791
7792 num_inodes() {
7793         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
7794 }
7795
7796 test_76() { # Now for bug 20433, added originally in bug 1443
7797         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7798
7799         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
7800
7801         cancel_lru_locks osc
7802         BEFORE_INODES=$(num_inodes)
7803         echo "before inodes: $BEFORE_INODES"
7804         local COUNT=1000
7805         [ "$SLOW" = "no" ] && COUNT=100
7806         for i in $(seq $COUNT); do
7807                 touch $DIR/$tfile
7808                 rm -f $DIR/$tfile
7809         done
7810         cancel_lru_locks osc
7811         AFTER_INODES=$(num_inodes)
7812         echo "after inodes: $AFTER_INODES"
7813         local wait=0
7814         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
7815                 sleep 2
7816                 AFTER_INODES=$(num_inodes)
7817                 wait=$((wait+2))
7818                 echo "wait $wait seconds inodes: $AFTER_INODES"
7819                 if [ $wait -gt 30 ]; then
7820                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
7821                 fi
7822         done
7823 }
7824 run_test 76 "confirm clients recycle inodes properly ===="
7825
7826
7827 export ORIG_CSUM=""
7828 set_checksums()
7829 {
7830         # Note: in sptlrpc modes which enable its own bulk checksum, the
7831         # original crc32_le bulk checksum will be automatically disabled,
7832         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
7833         # will be checked by sptlrpc code against sptlrpc bulk checksum.
7834         # In this case set_checksums() will not be no-op, because sptlrpc
7835         # bulk checksum will be enabled all through the test.
7836
7837         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
7838         lctl set_param -n osc.*.checksums $1
7839         return 0
7840 }
7841
7842 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7843                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
7844 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7845                              tr -d [] | head -n1)}
7846 set_checksum_type()
7847 {
7848         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
7849         log "set checksum type to $1"
7850         return 0
7851 }
7852 F77_TMP=$TMP/f77-temp
7853 F77SZ=8
7854 setup_f77() {
7855         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
7856                 error "error writing to $F77_TMP"
7857 }
7858
7859 test_77a() { # bug 10889
7860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7861         $GSS && skip_env "could not run with gss"
7862
7863         [ ! -f $F77_TMP ] && setup_f77
7864         set_checksums 1
7865         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
7866         set_checksums 0
7867         rm -f $DIR/$tfile
7868 }
7869 run_test 77a "normal checksum read/write operation"
7870
7871 test_77b() { # bug 10889
7872         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7873         $GSS && skip_env "could not run with gss"
7874
7875         [ ! -f $F77_TMP ] && setup_f77
7876         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7877         $LCTL set_param fail_loc=0x80000409
7878         set_checksums 1
7879
7880         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7881                 error "dd error: $?"
7882         $LCTL set_param fail_loc=0
7883
7884         for algo in $CKSUM_TYPES; do
7885                 cancel_lru_locks osc
7886                 set_checksum_type $algo
7887                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7888                 $LCTL set_param fail_loc=0x80000408
7889                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7890                 $LCTL set_param fail_loc=0
7891         done
7892         set_checksums 0
7893         set_checksum_type $ORIG_CSUM_TYPE
7894         rm -f $DIR/$tfile
7895 }
7896 run_test 77b "checksum error on client write, read"
7897
7898 cleanup_77c() {
7899         trap 0
7900         set_checksums 0
7901         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7902         $check_ost &&
7903                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7904         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7905         $check_ost && [ -n "$ost_file_prefix" ] &&
7906                 do_facet ost1 rm -f ${ost_file_prefix}\*
7907 }
7908
7909 test_77c() {
7910         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7911         $GSS && skip_env "could not run with gss"
7912         remote_ost_nodsh && skip "remote OST with nodsh"
7913
7914         local bad1
7915         local osc_file_prefix
7916         local osc_file
7917         local check_ost=false
7918         local ost_file_prefix
7919         local ost_file
7920         local orig_cksum
7921         local dump_cksum
7922         local fid
7923
7924         # ensure corruption will occur on first OSS/OST
7925         $LFS setstripe -i 0 $DIR/$tfile
7926
7927         [ ! -f $F77_TMP ] && setup_f77
7928         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7929                 error "dd write error: $?"
7930         fid=$($LFS path2fid $DIR/$tfile)
7931
7932         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
7933         then
7934                 check_ost=true
7935                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7936                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7937         else
7938                 echo "OSS do not support bulk pages dump upon error"
7939         fi
7940
7941         osc_file_prefix=$($LCTL get_param -n debug_path)
7942         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7943
7944         trap cleanup_77c EXIT
7945
7946         set_checksums 1
7947         # enable bulk pages dump upon error on Client
7948         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7949         # enable bulk pages dump upon error on OSS
7950         $check_ost &&
7951                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7952
7953         # flush Client cache to allow next read to reach OSS
7954         cancel_lru_locks osc
7955
7956         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7957         $LCTL set_param fail_loc=0x80000408
7958         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7959         $LCTL set_param fail_loc=0
7960
7961         rm -f $DIR/$tfile
7962
7963         # check cksum dump on Client
7964         osc_file=$(ls ${osc_file_prefix}*)
7965         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7966         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7967         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7968         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7969         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7970                      cksum)
7971         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7972         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7973                 error "dump content does not match on Client"
7974
7975         $check_ost || skip "No need to check cksum dump on OSS"
7976
7977         # check cksum dump on OSS
7978         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7979         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7980         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7981         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7982         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7983                 error "dump content does not match on OSS"
7984
7985         cleanup_77c
7986 }
7987 run_test 77c "checksum error on client read with debug"
7988
7989 test_77d() { # bug 10889
7990         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7991         $GSS && skip_env "could not run with gss"
7992
7993         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7994         $LCTL set_param fail_loc=0x80000409
7995         set_checksums 1
7996         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7997                 error "direct write: rc=$?"
7998         $LCTL set_param fail_loc=0
7999         set_checksums 0
8000
8001         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8002         $LCTL set_param fail_loc=0x80000408
8003         set_checksums 1
8004         cancel_lru_locks osc
8005         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8006                 error "direct read: rc=$?"
8007         $LCTL set_param fail_loc=0
8008         set_checksums 0
8009 }
8010 run_test 77d "checksum error on OST direct write, read"
8011
8012 test_77f() { # bug 10889
8013         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8014         $GSS && skip_env "could not run with gss"
8015
8016         set_checksums 1
8017         for algo in $CKSUM_TYPES; do
8018                 cancel_lru_locks osc
8019                 set_checksum_type $algo
8020                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8021                 $LCTL set_param fail_loc=0x409
8022                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8023                         error "direct write succeeded"
8024                 $LCTL set_param fail_loc=0
8025         done
8026         set_checksum_type $ORIG_CSUM_TYPE
8027         set_checksums 0
8028 }
8029 run_test 77f "repeat checksum error on write (expect error)"
8030
8031 test_77g() { # bug 10889
8032         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8033         $GSS && skip_env "could not run with gss"
8034         remote_ost_nodsh && skip "remote OST with nodsh"
8035
8036         [ ! -f $F77_TMP ] && setup_f77
8037
8038         local file=$DIR/$tfile
8039         stack_trap "rm -f $file" EXIT
8040
8041         $SETSTRIPE -c 1 -i 0 $file
8042         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8043         do_facet ost1 lctl set_param fail_loc=0x8000021a
8044         set_checksums 1
8045         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8046                 error "write error: rc=$?"
8047         do_facet ost1 lctl set_param fail_loc=0
8048         set_checksums 0
8049
8050         cancel_lru_locks osc
8051         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8052         do_facet ost1 lctl set_param fail_loc=0x8000021b
8053         set_checksums 1
8054         cmp $F77_TMP $file || error "file compare failed"
8055         do_facet ost1 lctl set_param fail_loc=0
8056         set_checksums 0
8057 }
8058 run_test 77g "checksum error on OST write, read"
8059
8060 test_77k() { # LU-10906
8061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8062         $GSS && skip_env "could not run with gss"
8063
8064         local cksum_param="osc.$FSNAME*.checksums"
8065         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8066         local checksum
8067         local i
8068
8069         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8070         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8071         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8072                 EXIT
8073
8074         for i in 0 1; do
8075                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8076                         error "failed to set checksum=$i on MGS"
8077                 wait_update $HOSTNAME "$get_checksum" $i
8078                 #remount
8079                 echo "remount client, checksum should be $i"
8080                 remount_client $MOUNT || "failed to remount client"
8081                 checksum=$(eval $get_checksum)
8082                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8083         done
8084
8085         for opt in "checksum" "nochecksum"; do
8086                 #remount with mount option
8087                 echo "remount client with option $opt, checksum should be $i"
8088                 umount_client $MOUNT || "failed to umount client"
8089                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8090                         "failed to mount client with option '$opt'"
8091                 checksum=$(eval $get_checksum)
8092                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8093                 i=$((i - 1))
8094         done
8095
8096         remount_client $MOUNT || "failed to remount client"
8097 }
8098 run_test 77k "enable/disable checksum correctly"
8099
8100 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8101 rm -f $F77_TMP
8102 unset F77_TMP
8103
8104 cleanup_test_78() {
8105         trap 0
8106         rm -f $DIR/$tfile
8107 }
8108
8109 test_78() { # bug 10901
8110         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8111         remote_ost || skip_env "local OST"
8112
8113         NSEQ=5
8114         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8115         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8116         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8117         echo "MemTotal: $MEMTOTAL"
8118
8119         # reserve 256MB of memory for the kernel and other running processes,
8120         # and then take 1/2 of the remaining memory for the read/write buffers.
8121         if [ $MEMTOTAL -gt 512 ] ;then
8122                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8123         else
8124                 # for those poor memory-starved high-end clusters...
8125                 MEMTOTAL=$((MEMTOTAL / 2))
8126         fi
8127         echo "Mem to use for directio: $MEMTOTAL"
8128
8129         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8130         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8131         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8132         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8133                 head -n1)
8134         echo "Smallest OST: $SMALLESTOST"
8135         [[ $SMALLESTOST -lt 10240 ]] &&
8136                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8137
8138         trap cleanup_test_78 EXIT
8139
8140         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8141                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8142
8143         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8144         echo "File size: $F78SIZE"
8145         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8146         for i in $(seq 1 $NSEQ); do
8147                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8148                 echo directIO rdwr round $i of $NSEQ
8149                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8150         done
8151
8152         cleanup_test_78
8153 }
8154 run_test 78 "handle large O_DIRECT writes correctly ============"
8155
8156 test_79() { # bug 12743
8157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8158
8159         wait_delete_completed
8160
8161         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8162         BKFREE=$(calc_osc_kbytes kbytesfree)
8163         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8164
8165         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8166         DFTOTAL=`echo $STRING | cut -d, -f1`
8167         DFUSED=`echo $STRING  | cut -d, -f2`
8168         DFAVAIL=`echo $STRING | cut -d, -f3`
8169         DFFREE=$(($DFTOTAL - $DFUSED))
8170
8171         ALLOWANCE=$((64 * $OSTCOUNT))
8172
8173         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8174            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8175                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8176         fi
8177         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8178            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8179                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8180         fi
8181         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8182            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8183                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8184         fi
8185 }
8186 run_test 79 "df report consistency check ======================="
8187
8188 test_80() { # bug 10718
8189         remote_ost_nodsh && skip "remote OST with nodsh"
8190         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8191
8192         # relax strong synchronous semantics for slow backends like ZFS
8193         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8194                 local soc="obdfilter.*.sync_lock_cancel"
8195                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8196
8197                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8198                 if [ -z "$save" ]; then
8199                         soc="obdfilter.*.sync_on_lock_cancel"
8200                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8201                 fi
8202
8203                 if [ "$save" != "never" ]; then
8204                         local hosts=$(comma_list $(osts_nodes))
8205
8206                         do_nodes $hosts $LCTL set_param $soc=never
8207                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8208                 fi
8209         fi
8210
8211         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8212         sync; sleep 1; sync
8213         local before=$(date +%s)
8214         cancel_lru_locks osc
8215         local after=$(date +%s)
8216         local diff=$((after - before))
8217         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8218
8219         rm -f $DIR/$tfile
8220 }
8221 run_test 80 "Page eviction is equally fast at high offsets too"
8222
8223 test_81a() { # LU-456
8224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8225         remote_ost_nodsh && skip "remote OST with nodsh"
8226
8227         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8228         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8229         do_facet ost1 lctl set_param fail_loc=0x80000228
8230
8231         # write should trigger a retry and success
8232         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8233         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8234         RC=$?
8235         if [ $RC -ne 0 ] ; then
8236                 error "write should success, but failed for $RC"
8237         fi
8238 }
8239 run_test 81a "OST should retry write when get -ENOSPC ==============="
8240
8241 test_81b() { # LU-456
8242         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8243         remote_ost_nodsh && skip "remote OST with nodsh"
8244
8245         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8246         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8247         do_facet ost1 lctl set_param fail_loc=0x228
8248
8249         # write should retry several times and return -ENOSPC finally
8250         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8251         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8252         RC=$?
8253         ENOSPC=28
8254         if [ $RC -ne $ENOSPC ] ; then
8255                 error "dd should fail for -ENOSPC, but succeed."
8256         fi
8257 }
8258 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8259
8260 test_82() { # LU-1031
8261         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8262         local gid1=14091995
8263         local gid2=16022000
8264
8265         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8266         local MULTIPID1=$!
8267         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8268         local MULTIPID2=$!
8269         kill -USR1 $MULTIPID2
8270         sleep 2
8271         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8272                 error "First grouplock does not block second one"
8273         else
8274                 echo "Second grouplock blocks first one"
8275         fi
8276         kill -USR1 $MULTIPID1
8277         wait $MULTIPID1
8278         wait $MULTIPID2
8279 }
8280 run_test 82 "Basic grouplock test"
8281
8282 test_99() {
8283         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8284
8285         test_mkdir $DIR/$tdir.cvsroot
8286         chown $RUNAS_ID $DIR/$tdir.cvsroot
8287
8288         cd $TMP
8289         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8290
8291         cd /etc/init.d
8292         # some versions of cvs import exit(1) when asked to import links or
8293         # files they can't read.  ignore those files.
8294         local toignore=$(find . -type l -printf '-I %f\n' -o \
8295                          ! -perm /4 -printf '-I %f\n')
8296         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8297                 $tdir.reposname vtag rtag
8298
8299         cd $DIR
8300         test_mkdir $DIR/$tdir.reposname
8301         chown $RUNAS_ID $DIR/$tdir.reposname
8302         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8303
8304         cd $DIR/$tdir.reposname
8305         $RUNAS touch foo99
8306         $RUNAS cvs add -m 'addmsg' foo99
8307         $RUNAS cvs update
8308         $RUNAS cvs commit -m 'nomsg' foo99
8309         rm -fr $DIR/$tdir.cvsroot
8310 }
8311 run_test 99 "cvs strange file/directory operations"
8312
8313 test_100() {
8314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8315         [[ "$NETTYPE" =~ tcp ]] ||
8316                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8317         remote_ost_nodsh && skip "remote OST with nodsh"
8318         remote_mds_nodsh && skip "remote MDS with nodsh"
8319         remote_servers ||
8320                 skip "useless for local single node setup"
8321
8322         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8323                 [ "$PROT" != "tcp" ] && continue
8324                 RPORT=$(echo $REMOTE | cut -d: -f2)
8325                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8326
8327                 rc=0
8328                 LPORT=`echo $LOCAL | cut -d: -f2`
8329                 if [ $LPORT -ge 1024 ]; then
8330                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8331                         netstat -tna
8332                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8333                 fi
8334         done
8335         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8336 }
8337 run_test 100 "check local port using privileged port ==========="
8338
8339 function get_named_value()
8340 {
8341     local tag
8342
8343     tag=$1
8344     while read ;do
8345         line=$REPLY
8346         case $line in
8347         $tag*)
8348             echo $line | sed "s/^$tag[ ]*//"
8349             break
8350             ;;
8351         esac
8352     done
8353 }
8354
8355 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8356                    awk '/^max_cached_mb/ { print $2 }')
8357
8358 cleanup_101a() {
8359         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8360         trap 0
8361 }
8362
8363 test_101a() {
8364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8365         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
8366
8367         local s
8368         local discard
8369         local nreads=10000
8370         local cache_limit=32
8371
8372         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8373         trap cleanup_101a EXIT
8374         $LCTL set_param -n llite.*.read_ahead_stats 0
8375         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8376
8377         #
8378         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8379         #
8380         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8381         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8382
8383         discard=0
8384         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8385                 get_named_value 'read but discarded' | cut -d" " -f1); do
8386                         discard=$(($discard + $s))
8387         done
8388         cleanup_101a
8389
8390         if [[ $(($discard * 10)) -gt $nreads ]]; then
8391                 $LCTL get_param osc.*-osc*.rpc_stats
8392                 $LCTL get_param llite.*.read_ahead_stats
8393                 error "too many ($discard) discarded pages"
8394         fi
8395         rm -f $DIR/$tfile || true
8396 }
8397 run_test 101a "check read-ahead for random reads"
8398
8399 setup_test101bc() {
8400         test_mkdir $DIR/$tdir
8401         local STRIPE_SIZE=$1
8402         local FILE_LENGTH=$2
8403         STRIPE_OFFSET=0
8404
8405         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
8406
8407         local list=$(comma_list $(osts_nodes))
8408         set_osd_param $list '' read_cache_enable 0
8409         set_osd_param $list '' writethrough_cache_enable 0
8410
8411         trap cleanup_test101bc EXIT
8412         # prepare the read-ahead file
8413         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8414
8415         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
8416                                 count=$FILE_SIZE_MB 2> /dev/null
8417
8418 }
8419
8420 cleanup_test101bc() {
8421         trap 0
8422         rm -rf $DIR/$tdir
8423         rm -f $DIR/$tfile
8424
8425         local list=$(comma_list $(osts_nodes))
8426         set_osd_param $list '' read_cache_enable 1
8427         set_osd_param $list '' writethrough_cache_enable 1
8428 }
8429
8430 calc_total() {
8431         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8432 }
8433
8434 ra_check_101() {
8435         local READ_SIZE=$1
8436         local STRIPE_SIZE=$2
8437         local FILE_LENGTH=$3
8438         local RA_INC=1048576
8439         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8440         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8441                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8442         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8443                         get_named_value 'read but discarded' |
8444                         cut -d" " -f1 | calc_total)
8445         if [[ $DISCARD -gt $discard_limit ]]; then
8446                 $LCTL get_param llite.*.read_ahead_stats
8447                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8448         else
8449                 echo "Read-ahead success for size ${READ_SIZE}"
8450         fi
8451 }
8452
8453 test_101b() {
8454         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8455         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8456
8457         local STRIPE_SIZE=1048576
8458         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8459
8460         if [ $SLOW == "yes" ]; then
8461                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8462         else
8463                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8464         fi
8465
8466         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8467
8468         # prepare the read-ahead file
8469         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8470         cancel_lru_locks osc
8471         for BIDX in 2 4 8 16 32 64 128 256
8472         do
8473                 local BSIZE=$((BIDX*4096))
8474                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8475                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8476                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8477                 $LCTL set_param -n llite.*.read_ahead_stats 0
8478                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8479                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8480                 cancel_lru_locks osc
8481                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8482         done
8483         cleanup_test101bc
8484         true
8485 }
8486 run_test 101b "check stride-io mode read-ahead ================="
8487
8488 test_101c() {
8489         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8490
8491         local STRIPE_SIZE=1048576
8492         local FILE_LENGTH=$((STRIPE_SIZE*100))
8493         local nreads=10000
8494         local rsize=65536
8495         local osc_rpc_stats
8496
8497         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8498
8499         cancel_lru_locks osc
8500         $LCTL set_param osc.*.rpc_stats 0
8501         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
8502         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8503                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8504                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8505                 local size
8506
8507                 if [ $lines -le 20 ]; then
8508                         continue
8509                 fi
8510                 for size in 1 2 4 8; do
8511                         local rpc=$(echo "$stats" |
8512                                     awk '($1 == "'$size':") {print $2; exit; }')
8513                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
8514                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
8515                 done
8516                 echo "$osc_rpc_stats check passed!"
8517         done
8518         cleanup_test101bc
8519         true
8520 }
8521 run_test 101c "check stripe_size aligned read-ahead ================="
8522
8523 set_read_ahead() {
8524         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8525         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8526 }
8527
8528 test_101d() {
8529         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8530
8531         local file=$DIR/$tfile
8532         local sz_MB=${FILESIZE_101d:-500}
8533         local ra_MB=${READAHEAD_MB:-40}
8534
8535         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8536         [ $free_MB -lt $sz_MB ] &&
8537                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8538
8539         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8540         $SETSTRIPE -c -1 $file || error "setstripe failed"
8541
8542         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8543         echo Cancel LRU locks on lustre client to flush the client cache
8544         cancel_lru_locks osc
8545
8546         echo Disable read-ahead
8547         local old_READAHEAD=$(set_read_ahead 0)
8548
8549         echo Reading the test file $file with read-ahead disabled
8550         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8551
8552         echo Cancel LRU locks on lustre client to flush the client cache
8553         cancel_lru_locks osc
8554         echo Enable read-ahead with ${ra_MB}MB
8555         set_read_ahead $ra_MB
8556
8557         echo Reading the test file $file with read-ahead enabled
8558         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8559
8560         echo "read-ahead disabled time read $raOFF"
8561         echo "read-ahead enabled  time read $raON"
8562
8563         set_read_ahead $old_READAHEAD
8564         rm -f $file
8565         wait_delete_completed
8566
8567         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
8568                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8569 }
8570 run_test 101d "file read with and without read-ahead enabled"
8571
8572 test_101e() {
8573         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8574
8575         local file=$DIR/$tfile
8576         local size_KB=500  #KB
8577         local count=100
8578         local bsize=1024
8579
8580         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8581         local need_KB=$((count * size_KB))
8582         [[ $free_KB -le $need_KB ]] &&
8583                 skip_env "Need free space $need_KB, have $free_KB"
8584
8585         echo "Creating $count ${size_KB}K test files"
8586         for ((i = 0; i < $count; i++)); do
8587                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8588         done
8589
8590         echo "Cancel LRU locks on lustre client to flush the client cache"
8591         cancel_lru_locks $OSC
8592
8593         echo "Reset readahead stats"
8594         $LCTL set_param -n llite.*.read_ahead_stats 0
8595
8596         for ((i = 0; i < $count; i++)); do
8597                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8598         done
8599
8600         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8601                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8602
8603         for ((i = 0; i < $count; i++)); do
8604                 rm -rf $file.$i 2>/dev/null
8605         done
8606
8607         #10000 means 20% reads are missing in readahead
8608         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8609 }
8610 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
8611
8612 test_101f() {
8613         which iozone || skip_env "no iozone installed"
8614
8615         local old_debug=$($LCTL get_param debug)
8616         old_debug=${old_debug#*=}
8617         $LCTL set_param debug="reada mmap"
8618
8619         # create a test file
8620         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
8621
8622         echo Cancel LRU locks on lustre client to flush the client cache
8623         cancel_lru_locks osc
8624
8625         echo Reset readahead stats
8626         $LCTL set_param -n llite.*.read_ahead_stats 0
8627
8628         echo mmap read the file with small block size
8629         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
8630                 > /dev/null 2>&1
8631
8632         echo checking missing pages
8633         $LCTL get_param llite.*.read_ahead_stats
8634         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8635                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
8636
8637         $LCTL set_param debug="$old_debug"
8638         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
8639         rm -f $DIR/$tfile
8640 }
8641 run_test 101f "check mmap read performance"
8642
8643 test_101g_brw_size_test() {
8644         local mb=$1
8645         local pages=$((mb * 1048576 / PAGE_SIZE))
8646         local file=$DIR/$tfile
8647
8648         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
8649                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
8650         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
8651                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
8652                         return 2
8653         done
8654
8655         stack_trap "rm -f $file" EXIT
8656         $LCTL set_param -n osc.*.rpc_stats=0
8657
8658         # 10 RPCs should be enough for the test
8659         local count=10
8660         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
8661                 { error "dd write ${mb} MB blocks failed"; return 3; }
8662         cancel_lru_locks osc
8663         dd of=/dev/null if=$file bs=${mb}M count=$count ||
8664                 { error "dd write ${mb} MB blocks failed"; return 4; }
8665
8666         # calculate number of full-sized read and write RPCs
8667         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
8668                 sed -n '/pages per rpc/,/^$/p' |
8669                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
8670                 END { print reads,writes }'))
8671         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
8672                 return 5
8673         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
8674                 return 6
8675
8676         return 0
8677 }
8678
8679 test_101g() {
8680         remote_ost_nodsh && skip "remote OST with nodsh"
8681
8682         local rpcs
8683         local osts=$(get_facets OST)
8684         local list=$(comma_list $(osts_nodes))
8685         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
8686         local brw_size="obdfilter.*.brw_size"
8687
8688         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8689
8690         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
8691         if [ $OST1_VERSION -ge $(version_code 2.8.52) -o \
8692              \( $OST1_VERSION -ge $(version_code 2.7.17) -a \
8693                 $OST1_VERSION -lt $(version_code 2.7.50) \) ] &&
8694            [ $CLIENT_VERSION -ge $(version_code 2.8.52) -o \
8695              \( $CLIENT_VERSION -ge $(version_code 2.7.17) -a \
8696                 $CLIENT_VERSION -lt $(version_code 2.7.50) \) ]; then
8697                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] && suffix="M"
8698                 if [[ $orig_mb -lt 16 ]]; then
8699                         save_lustre_params $osts "$brw_size" > $p
8700                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8701                                 error "set 16MB RPC size failed"
8702
8703                         echo "remount client to enable new RPC size"
8704                         remount_client $MOUNT || error "remount_client failed"
8705                 fi
8706
8707                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8708                 # should be able to set brw_size=12, but no rpc_stats for that
8709                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
8710         fi
8711
8712         test_101g_brw_size_test 4 || error "4MB RPC test failed"
8713
8714         if [[ $orig_mb -lt 16 ]]; then
8715                 restore_lustre_params < $p
8716                 remount_client $MOUNT || error "remount_client restore failed"
8717         fi
8718
8719         rm -f $p $DIR/$tfile
8720 }
8721 run_test 101g "Big bulk(4/16 MiB) readahead"
8722
8723 setup_test102() {
8724         test_mkdir $DIR/$tdir
8725         chown $RUNAS_ID $DIR/$tdir
8726         STRIPE_SIZE=65536
8727         STRIPE_OFFSET=1
8728         STRIPE_COUNT=$OSTCOUNT
8729         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
8730
8731         trap cleanup_test102 EXIT
8732         cd $DIR
8733         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
8734         cd $DIR/$tdir
8735         for num in 1 2 3 4; do
8736                 for count in $(seq 1 $STRIPE_COUNT); do
8737                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
8738                                 local size=`expr $STRIPE_SIZE \* $num`
8739                                 local file=file"$num-$idx-$count"
8740                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
8741                         done
8742                 done
8743         done
8744
8745         cd $DIR
8746         $1 tar cf $TMP/f102.tar $tdir --xattrs
8747 }
8748
8749 cleanup_test102() {
8750         trap 0
8751         rm -f $TMP/f102.tar
8752         rm -rf $DIR/d0.sanity/d102
8753 }
8754
8755 test_102a() {
8756         [ "$UID" != 0 ] && skip "must run as root"
8757         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
8758                 skip_env "must have user_xattr"
8759
8760         [ -z "$(which setfattr 2>/dev/null)" ] &&
8761                 skip_env "could not find setfattr"
8762
8763         local testfile=$DIR/$tfile
8764
8765         touch $testfile
8766         echo "set/get xattr..."
8767         setfattr -n trusted.name1 -v value1 $testfile ||
8768                 error "setfattr -n trusted.name1=value1 $testfile failed"
8769         getfattr -n trusted.name1 $testfile 2> /dev/null |
8770           grep "trusted.name1=.value1" ||
8771                 error "$testfile missing trusted.name1=value1"
8772
8773         setfattr -n user.author1 -v author1 $testfile ||
8774                 error "setfattr -n user.author1=author1 $testfile failed"
8775         getfattr -n user.author1 $testfile 2> /dev/null |
8776           grep "user.author1=.author1" ||
8777                 error "$testfile missing trusted.author1=author1"
8778
8779         echo "listxattr..."
8780         setfattr -n trusted.name2 -v value2 $testfile ||
8781                 error "$testfile unable to set trusted.name2"
8782         setfattr -n trusted.name3 -v value3 $testfile ||
8783                 error "$testfile unable to set trusted.name3"
8784         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
8785             grep "trusted.name" | wc -l) -eq 3 ] ||
8786                 error "$testfile missing 3 trusted.name xattrs"
8787
8788         setfattr -n user.author2 -v author2 $testfile ||
8789                 error "$testfile unable to set user.author2"
8790         setfattr -n user.author3 -v author3 $testfile ||
8791                 error "$testfile unable to set user.author3"
8792         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
8793             grep "user.author" | wc -l) -eq 3 ] ||
8794                 error "$testfile missing 3 user.author xattrs"
8795
8796         echo "remove xattr..."
8797         setfattr -x trusted.name1 $testfile ||
8798                 error "$testfile error deleting trusted.name1"
8799         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
8800                 error "$testfile did not delete trusted.name1 xattr"
8801
8802         setfattr -x user.author1 $testfile ||
8803                 error "$testfile error deleting user.author1"
8804         echo "set lustre special xattr ..."
8805         $LFS setstripe -c1 $testfile
8806         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
8807                 awk -F "=" '/trusted.lov/ { print $2 }' )
8808         setfattr -n "trusted.lov" -v $lovea $testfile ||
8809                 error "$testfile doesn't ignore setting trusted.lov again"
8810         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
8811                 error "$testfile allow setting invalid trusted.lov"
8812         rm -f $testfile
8813 }
8814 run_test 102a "user xattr test =================================="
8815
8816 check_102b_layout() {
8817         local layout="$*"
8818         local testfile=$DIR/$tfile
8819
8820         echo "test layout '$layout'"
8821         $LFS setstripe $layout $testfile || error "setstripe failed"
8822         $LFS getstripe -y $testfile
8823
8824         echo "get/set/list trusted.lov xattr ..." # b=10930
8825         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
8826         [[ "$value" =~ "trusted.lov" ]] ||
8827                 error "can't get trusted.lov from $testfile"
8828         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
8829                 error "getstripe failed"
8830
8831         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
8832
8833         value=$(cut -d= -f2 <<<$value)
8834         # LU-13168: truncated xattr should fail if short lov_user_md header
8835         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
8836                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
8837         for len in $lens; do
8838                 echo "setfattr $len $testfile.2"
8839                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
8840                         [ $len -lt 66 ] && error "short xattr len=$len worked"
8841         done
8842         local stripe_size=$($LFS getstripe -S $testfile.2)
8843         local stripe_count=$($LFS getstripe -c $testfile.2)
8844         [[ $stripe_size -eq 65536 ]] ||
8845                 error "stripe size $stripe_size != 65536"
8846         [[ $stripe_count -eq $stripe_count_orig ]] ||
8847                 error "stripe count $stripe_count != $stripe_count_orig"
8848         rm $testfile $testfile.2
8849 }
8850
8851 test_102b() {
8852         [ -z "$(which setfattr 2>/dev/null)" ] &&
8853                 skip_env "could not find setfattr"
8854         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8855
8856         # check plain layout
8857         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
8858
8859         # and also check composite layout
8860         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
8861
8862 }
8863 run_test 102b "getfattr/setfattr for trusted.lov EAs"
8864
8865 test_102c() {
8866         [ -z "$(which setfattr 2>/dev/null)" ] &&
8867                 skip_env "could not find setfattr"
8868         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8869
8870         # b10930: get/set/list lustre.lov xattr
8871         echo "get/set/list lustre.lov xattr ..."
8872         test_mkdir $DIR/$tdir
8873         chown $RUNAS_ID $DIR/$tdir
8874         local testfile=$DIR/$tdir/$tfile
8875         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8876                 error "setstripe failed"
8877         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
8878                 error "getstripe failed"
8879         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
8880         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8881
8882         local testfile2=${testfile}2
8883         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8884                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8885
8886         $RUNAS $MCREATE $testfile2
8887         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8888         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
8889         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
8890         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8891         [ $stripe_count -eq $STRIPECOUNT ] ||
8892                 error "stripe count $stripe_count != $STRIPECOUNT"
8893 }
8894 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8895
8896 compare_stripe_info1() {
8897         local stripe_index_all_zero=true
8898
8899         for num in 1 2 3 4; do
8900                 for count in $(seq 1 $STRIPE_COUNT); do
8901                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8902                                 local size=$((STRIPE_SIZE * num))
8903                                 local file=file"$num-$offset-$count"
8904                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8905                                 [[ $stripe_size -ne $size ]] &&
8906                                     error "$file: size $stripe_size != $size"
8907                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8908                                 # allow fewer stripes to be created, ORI-601
8909                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8910                                     error "$file: count $stripe_count != $count"
8911                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8912                                 [[ $stripe_index -ne 0 ]] &&
8913                                         stripe_index_all_zero=false
8914                         done
8915                 done
8916         done
8917         $stripe_index_all_zero &&
8918                 error "all files are being extracted starting from OST index 0"
8919         return 0
8920 }
8921
8922 have_xattrs_include() {
8923         tar --help | grep -q xattrs-include &&
8924                 echo --xattrs-include="lustre.*"
8925 }
8926
8927 test_102d() {
8928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8929         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8930
8931         XINC=$(have_xattrs_include)
8932         setup_test102
8933         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8934         cd $DIR/$tdir/$tdir
8935         compare_stripe_info1
8936 }
8937 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8938
8939 test_102f() {
8940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8941         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8942
8943         XINC=$(have_xattrs_include)
8944         setup_test102
8945         test_mkdir $DIR/$tdir.restore
8946         cd $DIR
8947         tar cf - --xattrs $tdir | tar xf - \
8948                 -C $DIR/$tdir.restore --xattrs $XINC
8949         cd $DIR/$tdir.restore/$tdir
8950         compare_stripe_info1
8951 }
8952 run_test 102f "tar copy files, not keep osts"
8953
8954 grow_xattr() {
8955         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8956                 skip "must have user_xattr"
8957         [ -z "$(which setfattr 2>/dev/null)" ] &&
8958                 skip_env "could not find setfattr"
8959         [ -z "$(which getfattr 2>/dev/null)" ] &&
8960                 skip_env "could not find getfattr"
8961
8962         local xsize=${1:-1024}  # in bytes
8963         local file=$DIR/$tfile
8964         local value="$(generate_string $xsize)"
8965         local xbig=trusted.big
8966
8967         touch $file
8968         log "save $xbig on $file"
8969         setfattr -n $xbig -v $value $file ||
8970                 error "saving $xbig on $file failed"
8971
8972         local orig=$(get_xattr_value $xbig $file)
8973         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8974
8975         local xsml=trusted.sml
8976         log "save $xsml on $file"
8977         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8978
8979         local new=$(get_xattr_value $xbig $file)
8980         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8981
8982         log "grow $xsml on $file"
8983         setfattr -n $xsml -v "$value" $file ||
8984                 error "growing $xsml on $file failed"
8985
8986         new=$(get_xattr_value $xbig $file)
8987         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8988         log "$xbig still valid after growing $xsml"
8989
8990         rm -f $file
8991 }
8992
8993 test_102h() { # bug 15777
8994         grow_xattr 1024
8995 }
8996 run_test 102h "grow xattr from inside inode to external block"
8997
8998 test_102ha() {
8999         large_xattr_enabled || skip_env "ea_inode feature disabled"
9000
9001         grow_xattr $(max_xattr_size)
9002 }
9003 run_test 102ha "grow xattr from inside inode to external inode"
9004
9005 test_102i() { # bug 17038
9006         [ -z "$(which getfattr 2>/dev/null)" ] &&
9007                 skip "could not find getfattr"
9008
9009         touch $DIR/$tfile
9010         ln -s $DIR/$tfile $DIR/${tfile}link
9011         getfattr -n trusted.lov $DIR/$tfile ||
9012                 error "lgetxattr on $DIR/$tfile failed"
9013         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9014                 grep -i "no such attr" ||
9015                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9016         rm -f $DIR/$tfile $DIR/${tfile}link
9017 }
9018 run_test 102i "lgetxattr test on symbolic link ============"
9019
9020 test_102j() {
9021         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9022         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9023
9024         XINC=$(have_xattrs_include)
9025         setup_test102 "$RUNAS"
9026         chown $RUNAS_ID $DIR/$tdir
9027         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9028         cd $DIR/$tdir/$tdir
9029         compare_stripe_info1 "$RUNAS"
9030 }
9031 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9032
9033 test_102k() {
9034         [ -z "$(which setfattr 2>/dev/null)" ] &&
9035                 skip "could not find setfattr"
9036
9037         touch $DIR/$tfile
9038         # b22187 just check that does not crash for regular file.
9039         setfattr -n trusted.lov $DIR/$tfile
9040         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9041         local test_kdir=$DIR/$tdir
9042         test_mkdir $test_kdir
9043         local default_size=$($LFS getstripe -S $test_kdir)
9044         local default_count=$($LFS getstripe -c $test_kdir)
9045         local default_offset=$($LFS getstripe -i $test_kdir)
9046         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9047                 error 'dir setstripe failed'
9048         setfattr -n trusted.lov $test_kdir
9049         local stripe_size=$($LFS getstripe -S $test_kdir)
9050         local stripe_count=$($LFS getstripe -c $test_kdir)
9051         local stripe_offset=$($LFS getstripe -i $test_kdir)
9052         [ $stripe_size -eq $default_size ] ||
9053                 error "stripe size $stripe_size != $default_size"
9054         [ $stripe_count -eq $default_count ] ||
9055                 error "stripe count $stripe_count != $default_count"
9056         [ $stripe_offset -eq $default_offset ] ||
9057                 error "stripe offset $stripe_offset != $default_offset"
9058         rm -rf $DIR/$tfile $test_kdir
9059 }
9060 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9061
9062 test_102l() {
9063         [ -z "$(which getfattr 2>/dev/null)" ] &&
9064                 skip "could not find getfattr"
9065
9066         # LU-532 trusted. xattr is invisible to non-root
9067         local testfile=$DIR/$tfile
9068
9069         touch $testfile
9070
9071         echo "listxattr as user..."
9072         chown $RUNAS_ID $testfile
9073         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9074             grep -q "trusted" &&
9075                 error "$testfile trusted xattrs are user visible"
9076
9077         return 0;
9078 }
9079 run_test 102l "listxattr size test =================================="
9080
9081 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9082         local path=$DIR/$tfile
9083         touch $path
9084
9085         listxattr_size_check $path || error "listattr_size_check $path failed"
9086 }
9087 run_test 102m "Ensure listxattr fails on small bufffer ========"
9088
9089 cleanup_test102
9090
9091 getxattr() { # getxattr path name
9092         # Return the base64 encoding of the value of xattr name on path.
9093         local path=$1
9094         local name=$2
9095
9096         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9097         # file: $path
9098         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9099         #
9100         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9101
9102         getfattr --absolute-names --encoding=base64 --name=$name $path |
9103                 awk -F= -v name=$name '$1 == name {
9104                         print substr($0, index($0, "=") + 1);
9105         }'
9106 }
9107
9108 test_102n() { # LU-4101 mdt: protect internal xattrs
9109         [ -z "$(which setfattr 2>/dev/null)" ] &&
9110                 skip "could not find setfattr"
9111         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9112         then
9113                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9114         fi
9115
9116         local file0=$DIR/$tfile.0
9117         local file1=$DIR/$tfile.1
9118         local xattr0=$TMP/$tfile.0
9119         local xattr1=$TMP/$tfile.1
9120         local namelist="lov lma lmv link fid version som hsm"
9121         local name
9122         local value
9123
9124         rm -rf $file0 $file1 $xattr0 $xattr1
9125         touch $file0 $file1
9126
9127         # Get 'before' xattrs of $file1.
9128         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9129
9130         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9131                 namelist+=" lfsck_namespace"
9132         for name in $namelist; do
9133                 # Try to copy xattr from $file0 to $file1.
9134                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9135
9136                 setfattr --name=trusted.$name --value="$value" $file1 ||
9137                         error "setxattr 'trusted.$name' failed"
9138
9139                 # Try to set a garbage xattr.
9140                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9141
9142                 if [[ x$name == "xlov" ]]; then
9143                         setfattr --name=trusted.lov --value="$value" $file1 &&
9144                         error "setxattr invalid 'trusted.lov' success"
9145                 else
9146                         setfattr --name=trusted.$name --value="$value" $file1 ||
9147                                 error "setxattr invalid 'trusted.$name' failed"
9148                 fi
9149
9150                 # Try to remove the xattr from $file1. We don't care if this
9151                 # appears to succeed or fail, we just don't want there to be
9152                 # any changes or crashes.
9153                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9154         done
9155
9156         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9157         then
9158                 name="lfsck_ns"
9159                 # Try to copy xattr from $file0 to $file1.
9160                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9161
9162                 setfattr --name=trusted.$name --value="$value" $file1 ||
9163                         error "setxattr 'trusted.$name' failed"
9164
9165                 # Try to set a garbage xattr.
9166                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9167
9168                 setfattr --name=trusted.$name --value="$value" $file1 ||
9169                         error "setxattr 'trusted.$name' failed"
9170
9171                 # Try to remove the xattr from $file1. We don't care if this
9172                 # appears to succeed or fail, we just don't want there to be
9173                 # any changes or crashes.
9174                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9175         fi
9176
9177         # Get 'after' xattrs of file1.
9178         getfattr --absolute-names --dump --match=- $file1 > $xattr1
9179
9180         if ! diff $xattr0 $xattr1; then
9181                 error "before and after xattrs of '$file1' differ"
9182         fi
9183
9184         rm -rf $file0 $file1 $xattr0 $xattr1
9185
9186         return 0
9187 }
9188 run_test 102n "silently ignore setxattr on internal trusted xattrs"
9189
9190 test_102p() { # LU-4703 setxattr did not check ownership
9191         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
9192                 skip "MDS needs to be at least 2.5.56"
9193
9194         local testfile=$DIR/$tfile
9195
9196         touch $testfile
9197
9198         echo "setfacl as user..."
9199         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
9200         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
9201
9202         echo "setfattr as user..."
9203         setfacl -m "u:$RUNAS_ID:---" $testfile
9204         $RUNAS setfattr -x system.posix_acl_access $testfile
9205         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
9206 }
9207 run_test 102p "check setxattr(2) correctly fails without permission"
9208
9209 test_102q() {
9210         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
9211                 skip "MDS needs to be at least 2.6.92"
9212
9213         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
9214 }
9215 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
9216
9217 test_102r() {
9218         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
9219                 skip "MDS needs to be at least 2.6.93"
9220
9221         touch $DIR/$tfile || error "touch"
9222         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
9223         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
9224         rm $DIR/$tfile || error "rm"
9225
9226         #normal directory
9227         mkdir -p $DIR/$tdir || error "mkdir"
9228         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9229         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9230         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9231                 error "$testfile error deleting user.author1"
9232         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9233                 grep "user.$(basename $tdir)" &&
9234                 error "$tdir did not delete user.$(basename $tdir)"
9235         rmdir $DIR/$tdir || error "rmdir"
9236
9237         #striped directory
9238         test_mkdir $DIR/$tdir
9239         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
9240         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
9241         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
9242                 error "$testfile error deleting user.author1"
9243         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
9244                 grep "user.$(basename $tdir)" &&
9245                 error "$tdir did not delete user.$(basename $tdir)"
9246         rmdir $DIR/$tdir || error "rm striped dir"
9247 }
9248 run_test 102r "set EAs with empty values"
9249
9250 test_102s() {
9251         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9252                 skip "MDS needs to be at least 2.11.52"
9253
9254         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9255
9256         save_lustre_params client "llite.*.xattr_cache" > $save
9257
9258         for cache in 0 1; do
9259                 lctl set_param llite.*.xattr_cache=$cache
9260
9261                 rm -f $DIR/$tfile
9262                 touch $DIR/$tfile || error "touch"
9263                 for prefix in lustre security system trusted user; do
9264                         # Note getxattr() may fail with 'Operation not
9265                         # supported' or 'No such attribute' depending
9266                         # on prefix and cache.
9267                         getfattr -n $prefix.n102s $DIR/$tfile &&
9268                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9269                 done
9270         done
9271
9272         restore_lustre_params < $save
9273 }
9274 run_test 102s "getting nonexistent xattrs should fail"
9275
9276 test_102t() {
9277         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9278                 skip "MDS needs to be at least 2.11.52"
9279
9280         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9281
9282         save_lustre_params client "llite.*.xattr_cache" > $save
9283
9284         for cache in 0 1; do
9285                 lctl set_param llite.*.xattr_cache=$cache
9286
9287                 for buf_size in 0 256; do
9288                         rm -f $DIR/$tfile
9289                         touch $DIR/$tfile || error "touch"
9290                         setfattr -n user.multiop $DIR/$tfile
9291                         $MULTIOP $DIR/$tfile oa$buf_size ||
9292                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9293                 done
9294         done
9295
9296         restore_lustre_params < $save
9297 }
9298 run_test 102t "zero length xattr values handled correctly"
9299
9300 run_acl_subtest()
9301 {
9302     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9303     return $?
9304 }
9305
9306 test_103a() {
9307         [ "$UID" != 0 ] && skip "must run as root"
9308         $GSS && skip_env "could not run under gss"
9309         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9310                 skip_env "must have acl enabled"
9311         [ -z "$(which setfacl 2>/dev/null)" ] &&
9312                 skip_env "could not find setfacl"
9313         remote_mds_nodsh && skip "remote MDS with nodsh"
9314
9315         gpasswd -a daemon bin                           # LU-5641
9316         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9317
9318         declare -a identity_old
9319
9320         for num in $(seq $MDSCOUNT); do
9321                 switch_identity $num true || identity_old[$num]=$?
9322         done
9323
9324         SAVE_UMASK=$(umask)
9325         umask 0022
9326         mkdir -p $DIR/$tdir
9327         cd $DIR/$tdir
9328
9329         echo "performing cp ..."
9330         run_acl_subtest cp || error "run_acl_subtest cp failed"
9331         echo "performing getfacl-noacl..."
9332         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9333         echo "performing misc..."
9334         run_acl_subtest misc || error  "misc test failed"
9335         echo "performing permissions..."
9336         run_acl_subtest permissions || error "permissions failed"
9337         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9338         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9339                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9340                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9341         then
9342                 echo "performing permissions xattr..."
9343                 run_acl_subtest permissions_xattr ||
9344                         error "permissions_xattr failed"
9345         fi
9346         echo "performing setfacl..."
9347         run_acl_subtest setfacl || error  "setfacl test failed"
9348
9349         # inheritance test got from HP
9350         echo "performing inheritance..."
9351         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9352         chmod +x make-tree || error "chmod +x failed"
9353         run_acl_subtest inheritance || error "inheritance test failed"
9354         rm -f make-tree
9355
9356         echo "LU-974 ignore umask when acl is enabled..."
9357         run_acl_subtest 974 || error "LU-974 umask test failed"
9358         if [ $MDSCOUNT -ge 2 ]; then
9359                 run_acl_subtest 974_remote ||
9360                         error "LU-974 umask test failed under remote dir"
9361         fi
9362
9363         echo "LU-2561 newly created file is same size as directory..."
9364         if [ "$mds1_FSTYPE" != "zfs" ]; then
9365                 run_acl_subtest 2561 || error "LU-2561 test failed"
9366         else
9367                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9368         fi
9369
9370         run_acl_subtest 4924 || error "LU-4924 test failed"
9371
9372         cd $SAVE_PWD
9373         umask $SAVE_UMASK
9374
9375         for num in $(seq $MDSCOUNT); do
9376                 if [ "${identity_old[$num]}" = 1 ]; then
9377                         switch_identity $num false || identity_old[$num]=$?
9378                 fi
9379         done
9380 }
9381 run_test 103a "acl test"
9382
9383 test_103b() {
9384         declare -a pids
9385         local U
9386
9387         for U in {0..511}; do
9388                 {
9389                 local O=$(printf "%04o" $U)
9390
9391                 umask $(printf "%04o" $((511 ^ $O)))
9392                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9393                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9394
9395                 (( $S == ($O & 0666) )) ||
9396                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9397
9398                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9399                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9400                 (( $S == ($O & 0666) )) ||
9401                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9402
9403                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9404                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9405                 (( $S == ($O & 0666) )) ||
9406                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9407                 rm -f $DIR/$tfile.[smp]$0
9408                 } &
9409                 local pid=$!
9410
9411                 # limit the concurrently running threads to 64. LU-11878
9412                 local idx=$((U % 64))
9413                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9414                 pids[idx]=$pid
9415         done
9416         wait
9417 }
9418 run_test 103b "umask lfs setstripe"
9419
9420 test_103c() {
9421         mkdir -p $DIR/$tdir
9422         cp -rp $DIR/$tdir $DIR/$tdir.bak
9423
9424         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9425                 error "$DIR/$tdir shouldn't contain default ACL"
9426         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9427                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9428         true
9429 }
9430 run_test 103c "'cp -rp' won't set empty acl"
9431
9432 test_104a() {
9433         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9434
9435         touch $DIR/$tfile
9436         lfs df || error "lfs df failed"
9437         lfs df -ih || error "lfs df -ih failed"
9438         lfs df -h $DIR || error "lfs df -h $DIR failed"
9439         lfs df -i $DIR || error "lfs df -i $DIR failed"
9440         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9441         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9442
9443         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9444         lctl --device %$OSC deactivate
9445         lfs df || error "lfs df with deactivated OSC failed"
9446         lctl --device %$OSC activate
9447         # wait the osc back to normal
9448         wait_osc_import_ready client ost
9449
9450         lfs df || error "lfs df with reactivated OSC failed"
9451         rm -f $DIR/$tfile
9452 }
9453 run_test 104a "lfs df [-ih] [path] test ========================="
9454
9455 test_104b() {
9456         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9457         [ $RUNAS_ID -eq $UID ] &&
9458                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9459
9460         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9461                         grep "Permission denied" | wc -l)))
9462         if [ $denied_cnt -ne 0 ]; then
9463                 error "lfs check servers test failed"
9464         fi
9465 }
9466 run_test 104b "$RUNAS lfs check servers test ===================="
9467
9468 #
9469 # Verify $1 is within range of $2.
9470 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
9471 # $1 is <= 2% of $2. Else Fail.
9472 #
9473 value_in_range() {
9474         # Strip all units (M, G, T)
9475         actual=$(echo $1 | tr -d A-Z)
9476         expect=$(echo $2 | tr -d A-Z)
9477
9478         expect_lo=$(($expect * 98 / 100)) # 2% below
9479         expect_hi=$(($expect * 102 / 100)) # 2% above
9480
9481         # permit 2% drift above and below
9482         (( $actual >= $expect_lo && $actual <= $expect_hi ))
9483 }
9484
9485 test_104c() {
9486         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9487         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
9488
9489         local ost_param="osd-zfs.$FSNAME-OST0000."
9490         local mdt_param="osd-zfs.$FSNAME-MDT0000."
9491         local ofacets=$(get_facets OST)
9492         local mfacets=$(get_facets MDS)
9493         local saved_ost_blocks=
9494         local saved_mdt_blocks=
9495
9496         echo "Before recordsize change"
9497         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
9498         df=($(df -h | grep "/mnt/lustre"$))
9499
9500         # For checking.
9501         echo "lfs output : ${lfs_df[*]}"
9502         echo "df  output : ${df[*]}"
9503
9504         for facet in ${ofacets//,/ }; do
9505                 if [ -z $saved_ost_blocks ]; then
9506                         saved_ost_blocks=$(do_facet $facet \
9507                                 lctl get_param -n $ost_param.blocksize)
9508                         echo "OST Blocksize: $saved_ost_blocks"
9509                 fi
9510                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
9511                 do_facet $facet zfs set recordsize=32768 $ost
9512         done
9513
9514         # BS too small. Sufficient for functional testing.
9515         for facet in ${mfacets//,/ }; do
9516                 if [ -z $saved_mdt_blocks ]; then
9517                         saved_mdt_blocks=$(do_facet $facet \
9518                                 lctl get_param -n $mdt_param.blocksize)
9519                         echo "MDT Blocksize: $saved_mdt_blocks"
9520                 fi
9521                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
9522                 do_facet $facet zfs set recordsize=32768 $mdt
9523         done
9524
9525         # Give new values chance to reflect change
9526         sleep 2
9527
9528         echo "After recordsize change"
9529         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
9530         df_after=($(df -h | grep "/mnt/lustre"$))
9531
9532         # For checking.
9533         echo "lfs output : ${lfs_df_after[*]}"
9534         echo "df  output : ${df_after[*]}"
9535
9536         # Verify lfs df
9537         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
9538                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
9539         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
9540                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
9541         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
9542                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
9543
9544         # Verify df
9545         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
9546                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
9547         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
9548                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
9549         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
9550                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
9551
9552         # Restore MDT recordize back to original
9553         for facet in ${mfacets//,/ }; do
9554                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
9555                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
9556         done
9557
9558         # Restore OST recordize back to original
9559         for facet in ${ofacets//,/ }; do
9560                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
9561                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
9562         done
9563
9564         return 0
9565 }
9566 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
9567
9568 test_105a() {
9569         # doesn't work on 2.4 kernels
9570         touch $DIR/$tfile
9571         if $(flock_is_enabled); then
9572                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9573         else
9574                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9575         fi
9576         rm -f $DIR/$tfile
9577 }
9578 run_test 105a "flock when mounted without -o flock test ========"
9579
9580 test_105b() {
9581         touch $DIR/$tfile
9582         if $(flock_is_enabled); then
9583                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9584         else
9585                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9586         fi
9587         rm -f $DIR/$tfile
9588 }
9589 run_test 105b "fcntl when mounted without -o flock test ========"
9590
9591 test_105c() {
9592         touch $DIR/$tfile
9593         if $(flock_is_enabled); then
9594                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9595         else
9596                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9597         fi
9598         rm -f $DIR/$tfile
9599 }
9600 run_test 105c "lockf when mounted without -o flock test"
9601
9602 test_105d() { # bug 15924
9603         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9604
9605         test_mkdir $DIR/$tdir
9606         flock_is_enabled || skip_env "mount w/o flock enabled"
9607         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9608         $LCTL set_param fail_loc=0x80000315
9609         flocks_test 2 $DIR/$tdir
9610 }
9611 run_test 105d "flock race (should not freeze) ========"
9612
9613 test_105e() { # bug 22660 && 22040
9614         flock_is_enabled || skip_env "mount w/o flock enabled"
9615
9616         touch $DIR/$tfile
9617         flocks_test 3 $DIR/$tfile
9618 }
9619 run_test 105e "Two conflicting flocks from same process"
9620
9621 test_106() { #bug 10921
9622         test_mkdir $DIR/$tdir
9623         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9624         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9625 }
9626 run_test 106 "attempt exec of dir followed by chown of that dir"
9627
9628 test_107() {
9629         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9630
9631         CDIR=`pwd`
9632         local file=core
9633
9634         cd $DIR
9635         rm -f $file
9636
9637         local save_pattern=$(sysctl -n kernel.core_pattern)
9638         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9639         sysctl -w kernel.core_pattern=$file
9640         sysctl -w kernel.core_uses_pid=0
9641
9642         ulimit -c unlimited
9643         sleep 60 &
9644         SLEEPPID=$!
9645
9646         sleep 1
9647
9648         kill -s 11 $SLEEPPID
9649         wait $SLEEPPID
9650         if [ -e $file ]; then
9651                 size=`stat -c%s $file`
9652                 [ $size -eq 0 ] && error "Fail to create core file $file"
9653         else
9654                 error "Fail to create core file $file"
9655         fi
9656         rm -f $file
9657         sysctl -w kernel.core_pattern=$save_pattern
9658         sysctl -w kernel.core_uses_pid=$save_uses_pid
9659         cd $CDIR
9660 }
9661 run_test 107 "Coredump on SIG"
9662
9663 test_110() {
9664         test_mkdir $DIR/$tdir
9665         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9666         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9667                 error "mkdir with 256 char should fail, but did not"
9668         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9669                 error "create with 255 char failed"
9670         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9671                 error "create with 256 char should fail, but did not"
9672
9673         ls -l $DIR/$tdir
9674         rm -rf $DIR/$tdir
9675 }
9676 run_test 110 "filename length checking"
9677
9678 #
9679 # Purpose: To verify dynamic thread (OSS) creation.
9680 #
9681 test_115() {
9682         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9683         remote_ost_nodsh && skip "remote OST with nodsh"
9684
9685         # Lustre does not stop service threads once they are started.
9686         # Reset number of running threads to default.
9687         stopall
9688         setupall
9689
9690         local OSTIO_pre
9691         local save_params="$TMP/sanity-$TESTNAME.parameters"
9692
9693         # Get ll_ost_io count before I/O
9694         OSTIO_pre=$(do_facet ost1 \
9695                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
9696         # Exit if lustre is not running (ll_ost_io not running).
9697         [ -z "$OSTIO_pre" ] && error "no OSS threads"
9698
9699         echo "Starting with $OSTIO_pre threads"
9700         local thread_max=$((OSTIO_pre * 2))
9701         local rpc_in_flight=$((thread_max * 2))
9702         # Number of I/O Process proposed to be started.
9703         local nfiles
9704         local facets=$(get_facets OST)
9705
9706         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
9707         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
9708
9709         # Set in_flight to $rpc_in_flight
9710         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
9711                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
9712         nfiles=${rpc_in_flight}
9713         # Set ost thread_max to $thread_max
9714         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
9715
9716         # 5 Minutes should be sufficient for max number of OSS
9717         # threads(thread_max) to be created.
9718         local timeout=300
9719
9720         # Start I/O.
9721         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
9722         test_mkdir $DIR/$tdir
9723         for i in $(seq $nfiles); do
9724                 local file=$DIR/$tdir/${tfile}-$i
9725                 $LFS setstripe -c -1 -i 0 $file
9726                 ($WTL $file $timeout)&
9727         done
9728
9729         # I/O Started - Wait for thread_started to reach thread_max or report
9730         # error if thread_started is more than thread_max.
9731         echo "Waiting for thread_started to reach thread_max"
9732         local thread_started=0
9733         local end_time=$((SECONDS + timeout))
9734
9735         while [ $SECONDS -le $end_time ] ; do
9736                 echo -n "."
9737                 # Get ost i/o thread_started count.
9738                 thread_started=$(do_facet ost1 \
9739                         "$LCTL get_param \
9740                         ost.OSS.ost_io.threads_started | cut -d= -f2")
9741                 # Break out if thread_started is equal/greater than thread_max
9742                 if [[ $thread_started -ge $thread_max ]]; then
9743                         echo ll_ost_io thread_started $thread_started, \
9744                                 equal/greater than thread_max $thread_max
9745                         break
9746                 fi
9747                 sleep 1
9748         done
9749
9750         # Cleanup - We have the numbers, Kill i/o jobs if running.
9751         jobcount=($(jobs -p))
9752         for i in $(seq 0 $((${#jobcount[@]}-1)))
9753         do
9754                 kill -9 ${jobcount[$i]}
9755                 if [ $? -ne 0 ] ; then
9756                         echo Warning: \
9757                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
9758                 fi
9759         done
9760
9761         # Cleanup files left by WTL binary.
9762         for i in $(seq $nfiles); do
9763                 local file=$DIR/$tdir/${tfile}-$i
9764                 rm -rf $file
9765                 if [ $? -ne 0 ] ; then
9766                         echo "Warning: Failed to delete file $file"
9767                 fi
9768         done
9769
9770         restore_lustre_params <$save_params
9771         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
9772
9773         # Error out if no new thread has started or Thread started is greater
9774         # than thread max.
9775         if [[ $thread_started -le $OSTIO_pre ||
9776                         $thread_started -gt $thread_max ]]; then
9777                 error "ll_ost_io: thread_started $thread_started" \
9778                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
9779                       "No new thread started or thread started greater " \
9780                       "than thread_max."
9781         fi
9782 }
9783 run_test 115 "verify dynamic thread creation===================="
9784
9785 free_min_max () {
9786         wait_delete_completed
9787         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
9788         echo "OST kbytes available: ${AVAIL[@]}"
9789         MAXV=${AVAIL[0]}
9790         MAXI=0
9791         MINV=${AVAIL[0]}
9792         MINI=0
9793         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
9794                 #echo OST $i: ${AVAIL[i]}kb
9795                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
9796                         MAXV=${AVAIL[i]}
9797                         MAXI=$i
9798                 fi
9799                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
9800                         MINV=${AVAIL[i]}
9801                         MINI=$i
9802                 fi
9803         done
9804         echo "Min free space: OST $MINI: $MINV"
9805         echo "Max free space: OST $MAXI: $MAXV"
9806 }
9807
9808 test_116a() { # was previously test_116()
9809         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9810         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9811         remote_mds_nodsh && skip "remote MDS with nodsh"
9812
9813         echo -n "Free space priority "
9814         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
9815                 head -n1
9816         declare -a AVAIL
9817         free_min_max
9818
9819         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
9820         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
9821         trap simple_cleanup_common EXIT
9822
9823         # Check if we need to generate uneven OSTs
9824         test_mkdir -p $DIR/$tdir/OST${MINI}
9825         local FILL=$((MINV / 4))
9826         local DIFF=$((MAXV - MINV))
9827         local DIFF2=$((DIFF * 100 / MINV))
9828
9829         local threshold=$(do_facet $SINGLEMDS \
9830                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
9831         threshold=${threshold%%%}
9832         echo -n "Check for uneven OSTs: "
9833         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
9834
9835         if [[ $DIFF2 -gt $threshold ]]; then
9836                 echo "ok"
9837                 echo "Don't need to fill OST$MINI"
9838         else
9839                 # generate uneven OSTs. Write 2% over the QOS threshold value
9840                 echo "no"
9841                 DIFF=$((threshold - DIFF2 + 2))
9842                 DIFF2=$((MINV * DIFF / 100))
9843                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
9844                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
9845                         error "setstripe failed"
9846                 DIFF=$((DIFF2 / 2048))
9847                 i=0
9848                 while [ $i -lt $DIFF ]; do
9849                         i=$((i + 1))
9850                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
9851                                 bs=2M count=1 2>/dev/null
9852                         echo -n .
9853                 done
9854                 echo .
9855                 sync
9856                 sleep_maxage
9857                 free_min_max
9858         fi
9859
9860         DIFF=$((MAXV - MINV))
9861         DIFF2=$((DIFF * 100 / MINV))
9862         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
9863         if [ $DIFF2 -gt $threshold ]; then
9864                 echo "ok"
9865         else
9866                 echo "failed - QOS mode won't be used"
9867                 simple_cleanup_common
9868                 skip "QOS imbalance criteria not met"
9869         fi
9870
9871         MINI1=$MINI
9872         MINV1=$MINV
9873         MAXI1=$MAXI
9874         MAXV1=$MAXV
9875
9876         # now fill using QOS
9877         $SETSTRIPE -c 1 $DIR/$tdir
9878         FILL=$((FILL / 200))
9879         if [ $FILL -gt 600 ]; then
9880                 FILL=600
9881         fi
9882         echo "writing $FILL files to QOS-assigned OSTs"
9883         i=0
9884         while [ $i -lt $FILL ]; do
9885                 i=$((i + 1))
9886                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
9887                         count=1 2>/dev/null
9888                 echo -n .
9889         done
9890         echo "wrote $i 200k files"
9891         sync
9892         sleep_maxage
9893
9894         echo "Note: free space may not be updated, so measurements might be off"
9895         free_min_max
9896         DIFF2=$((MAXV - MINV))
9897         echo "free space delta: orig $DIFF final $DIFF2"
9898         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
9899         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
9900         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
9901         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
9902         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
9903         if [[ $DIFF -gt 0 ]]; then
9904                 FILL=$((DIFF2 * 100 / DIFF - 100))
9905                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
9906         fi
9907
9908         # Figure out which files were written where
9909         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9910                awk '/'$MINI1': / {print $2; exit}')
9911         echo $UUID
9912         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9913         echo "$MINC files created on smaller OST $MINI1"
9914         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9915                awk '/'$MAXI1': / {print $2; exit}')
9916         echo $UUID
9917         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9918         echo "$MAXC files created on larger OST $MAXI1"
9919         if [[ $MINC -gt 0 ]]; then
9920                 FILL=$((MAXC * 100 / MINC - 100))
9921                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
9922         fi
9923         [[ $MAXC -gt $MINC ]] ||
9924                 error_ignore LU-9 "stripe QOS didn't balance free space"
9925         simple_cleanup_common
9926 }
9927 run_test 116a "stripe QOS: free space balance ==================="
9928
9929 test_116b() { # LU-2093
9930         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9931         remote_mds_nodsh && skip "remote MDS with nodsh"
9932
9933 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
9934         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
9935                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
9936         [ -z "$old_rr" ] && skip "no QOS"
9937         do_facet $SINGLEMDS lctl set_param \
9938                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
9939         mkdir -p $DIR/$tdir
9940         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
9941         createmany -o $DIR/$tdir/f- 20 || error "can't create"
9942         do_facet $SINGLEMDS lctl set_param fail_loc=0
9943         rm -rf $DIR/$tdir
9944         do_facet $SINGLEMDS lctl set_param \
9945                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
9946 }
9947 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
9948
9949 test_117() # bug 10891
9950 {
9951         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9952
9953         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
9954         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
9955         lctl set_param fail_loc=0x21e
9956         > $DIR/$tfile || error "truncate failed"
9957         lctl set_param fail_loc=0
9958         echo "Truncate succeeded."
9959         rm -f $DIR/$tfile
9960 }
9961 run_test 117 "verify osd extend =========="
9962
9963 NO_SLOW_RESENDCOUNT=4
9964 export OLD_RESENDCOUNT=""
9965 set_resend_count () {
9966         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
9967         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
9968         lctl set_param -n $PROC_RESENDCOUNT $1
9969         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
9970 }
9971
9972 # for reduce test_118* time (b=14842)
9973 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9974
9975 # Reset async IO behavior after error case
9976 reset_async() {
9977         FILE=$DIR/reset_async
9978
9979         # Ensure all OSCs are cleared
9980         $SETSTRIPE -c -1 $FILE
9981         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
9982         sync
9983         rm $FILE
9984 }
9985
9986 test_118a() #bug 11710
9987 {
9988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9989
9990         reset_async
9991
9992         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9993         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9994         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9995
9996         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9997                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9998                 return 1;
9999         fi
10000         rm -f $DIR/$tfile
10001 }
10002 run_test 118a "verify O_SYNC works =========="
10003
10004 test_118b()
10005 {
10006         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10007         remote_ost_nodsh && skip "remote OST with nodsh"
10008
10009         reset_async
10010
10011         #define OBD_FAIL_SRV_ENOENT 0x217
10012         set_nodes_failloc "$(osts_nodes)" 0x217
10013         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10014         RC=$?
10015         set_nodes_failloc "$(osts_nodes)" 0
10016         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10017         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10018                     grep -c writeback)
10019
10020         if [[ $RC -eq 0 ]]; then
10021                 error "Must return error due to dropped pages, rc=$RC"
10022                 return 1;
10023         fi
10024
10025         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10026                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10027                 return 1;
10028         fi
10029
10030         echo "Dirty pages not leaked on ENOENT"
10031
10032         # Due to the above error the OSC will issue all RPCs syncronously
10033         # until a subsequent RPC completes successfully without error.
10034         $MULTIOP $DIR/$tfile Ow4096yc
10035         rm -f $DIR/$tfile
10036
10037         return 0
10038 }
10039 run_test 118b "Reclaim dirty pages on fatal error =========="
10040
10041 test_118c()
10042 {
10043         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10044
10045         # for 118c, restore the original resend count, LU-1940
10046         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10047                                 set_resend_count $OLD_RESENDCOUNT
10048         remote_ost_nodsh && skip "remote OST with nodsh"
10049
10050         reset_async
10051
10052         #define OBD_FAIL_OST_EROFS               0x216
10053         set_nodes_failloc "$(osts_nodes)" 0x216
10054
10055         # multiop should block due to fsync until pages are written
10056         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10057         MULTIPID=$!
10058         sleep 1
10059
10060         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10061                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10062         fi
10063
10064         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10065                     grep -c writeback)
10066         if [[ $WRITEBACK -eq 0 ]]; then
10067                 error "No page in writeback, writeback=$WRITEBACK"
10068         fi
10069
10070         set_nodes_failloc "$(osts_nodes)" 0
10071         wait $MULTIPID
10072         RC=$?
10073         if [[ $RC -ne 0 ]]; then
10074                 error "Multiop fsync failed, rc=$RC"
10075         fi
10076
10077         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10078         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10079                     grep -c writeback)
10080         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10081                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10082         fi
10083
10084         rm -f $DIR/$tfile
10085         echo "Dirty pages flushed via fsync on EROFS"
10086         return 0
10087 }
10088 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10089
10090 # continue to use small resend count to reduce test_118* time (b=14842)
10091 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10092
10093 test_118d()
10094 {
10095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10096         remote_ost_nodsh && skip "remote OST with nodsh"
10097
10098         reset_async
10099
10100         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10101         set_nodes_failloc "$(osts_nodes)" 0x214
10102         # multiop should block due to fsync until pages are written
10103         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10104         MULTIPID=$!
10105         sleep 1
10106
10107         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10108                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10109         fi
10110
10111         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10112                     grep -c writeback)
10113         if [[ $WRITEBACK -eq 0 ]]; then
10114                 error "No page in writeback, writeback=$WRITEBACK"
10115         fi
10116
10117         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10118         set_nodes_failloc "$(osts_nodes)" 0
10119
10120         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10121         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10122                     grep -c writeback)
10123         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10124                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10125         fi
10126
10127         rm -f $DIR/$tfile
10128         echo "Dirty pages gaurenteed flushed via fsync"
10129         return 0
10130 }
10131 run_test 118d "Fsync validation inject a delay of the bulk =========="
10132
10133 test_118f() {
10134         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10135
10136         reset_async
10137
10138         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10139         lctl set_param fail_loc=0x8000040a
10140
10141         # Should simulate EINVAL error which is fatal
10142         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10143         RC=$?
10144         if [[ $RC -eq 0 ]]; then
10145                 error "Must return error due to dropped pages, rc=$RC"
10146         fi
10147
10148         lctl set_param fail_loc=0x0
10149
10150         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10151         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10152         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10153                     grep -c writeback)
10154         if [[ $LOCKED -ne 0 ]]; then
10155                 error "Locked pages remain in cache, locked=$LOCKED"
10156         fi
10157
10158         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10159                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10160         fi
10161
10162         rm -f $DIR/$tfile
10163         echo "No pages locked after fsync"
10164
10165         reset_async
10166         return 0
10167 }
10168 run_test 118f "Simulate unrecoverable OSC side error =========="
10169
10170 test_118g() {
10171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10172
10173         reset_async
10174
10175         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10176         lctl set_param fail_loc=0x406
10177
10178         # simulate local -ENOMEM
10179         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10180         RC=$?
10181
10182         lctl set_param fail_loc=0
10183         if [[ $RC -eq 0 ]]; then
10184                 error "Must return error due to dropped pages, rc=$RC"
10185         fi
10186
10187         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10188         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10189         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10190                         grep -c writeback)
10191         if [[ $LOCKED -ne 0 ]]; then
10192                 error "Locked pages remain in cache, locked=$LOCKED"
10193         fi
10194
10195         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10196                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10197         fi
10198
10199         rm -f $DIR/$tfile
10200         echo "No pages locked after fsync"
10201
10202         reset_async
10203         return 0
10204 }
10205 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10206
10207 test_118h() {
10208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10209         remote_ost_nodsh && skip "remote OST with nodsh"
10210
10211         reset_async
10212
10213         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10214         set_nodes_failloc "$(osts_nodes)" 0x20e
10215         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10216         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10217         RC=$?
10218
10219         set_nodes_failloc "$(osts_nodes)" 0
10220         if [[ $RC -eq 0 ]]; then
10221                 error "Must return error due to dropped pages, rc=$RC"
10222         fi
10223
10224         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10225         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10226         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10227                     grep -c writeback)
10228         if [[ $LOCKED -ne 0 ]]; then
10229                 error "Locked pages remain in cache, locked=$LOCKED"
10230         fi
10231
10232         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10233                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10234         fi
10235
10236         rm -f $DIR/$tfile
10237         echo "No pages locked after fsync"
10238
10239         return 0
10240 }
10241 run_test 118h "Verify timeout in handling recoverables errors  =========="
10242
10243 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10244
10245 test_118i() {
10246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10247         remote_ost_nodsh && skip "remote OST with nodsh"
10248
10249         reset_async
10250
10251         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10252         set_nodes_failloc "$(osts_nodes)" 0x20e
10253
10254         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10255         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10256         PID=$!
10257         sleep 5
10258         set_nodes_failloc "$(osts_nodes)" 0
10259
10260         wait $PID
10261         RC=$?
10262         if [[ $RC -ne 0 ]]; then
10263                 error "got error, but should be not, rc=$RC"
10264         fi
10265
10266         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10267         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10268         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10269         if [[ $LOCKED -ne 0 ]]; then
10270                 error "Locked pages remain in cache, locked=$LOCKED"
10271         fi
10272
10273         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10274                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10275         fi
10276
10277         rm -f $DIR/$tfile
10278         echo "No pages locked after fsync"
10279
10280         return 0
10281 }
10282 run_test 118i "Fix error before timeout in recoverable error  =========="
10283
10284 [ "$SLOW" = "no" ] && set_resend_count 4
10285
10286 test_118j() {
10287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10288         remote_ost_nodsh && skip "remote OST with nodsh"
10289
10290         reset_async
10291
10292         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
10293         set_nodes_failloc "$(osts_nodes)" 0x220
10294
10295         # return -EIO from OST
10296         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10297         RC=$?
10298         set_nodes_failloc "$(osts_nodes)" 0x0
10299         if [[ $RC -eq 0 ]]; then
10300                 error "Must return error due to dropped pages, rc=$RC"
10301         fi
10302
10303         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10304         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10305         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10306         if [[ $LOCKED -ne 0 ]]; then
10307                 error "Locked pages remain in cache, locked=$LOCKED"
10308         fi
10309
10310         # in recoverable error on OST we want resend and stay until it finished
10311         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10312                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10313         fi
10314
10315         rm -f $DIR/$tfile
10316         echo "No pages locked after fsync"
10317
10318         return 0
10319 }
10320 run_test 118j "Simulate unrecoverable OST side error =========="
10321
10322 test_118k()
10323 {
10324         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10325         remote_ost_nodsh && skip "remote OSTs with nodsh"
10326
10327         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10328         set_nodes_failloc "$(osts_nodes)" 0x20e
10329         test_mkdir $DIR/$tdir
10330
10331         for ((i=0;i<10;i++)); do
10332                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
10333                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
10334                 SLEEPPID=$!
10335                 sleep 0.500s
10336                 kill $SLEEPPID
10337                 wait $SLEEPPID
10338         done
10339
10340         set_nodes_failloc "$(osts_nodes)" 0
10341         rm -rf $DIR/$tdir
10342 }
10343 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
10344
10345 test_118l() # LU-646
10346 {
10347         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10348
10349         test_mkdir $DIR/$tdir
10350         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10351         rm -rf $DIR/$tdir
10352 }
10353 run_test 118l "fsync dir"
10354
10355 test_118m() # LU-3066
10356 {
10357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10358
10359         test_mkdir $DIR/$tdir
10360         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10361         rm -rf $DIR/$tdir
10362 }
10363 run_test 118m "fdatasync dir ========="
10364
10365 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10366
10367 test_118n()
10368 {
10369         local begin
10370         local end
10371
10372         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10373         remote_ost_nodsh && skip "remote OSTs with nodsh"
10374
10375         # Sleep to avoid a cached response.
10376         #define OBD_STATFS_CACHE_SECONDS 1
10377         sleep 2
10378
10379         # Inject a 10 second delay in the OST_STATFS handler.
10380         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10381         set_nodes_failloc "$(osts_nodes)" 0x242
10382
10383         begin=$SECONDS
10384         stat --file-system $MOUNT > /dev/null
10385         end=$SECONDS
10386
10387         set_nodes_failloc "$(osts_nodes)" 0
10388
10389         if ((end - begin > 20)); then
10390             error "statfs took $((end - begin)) seconds, expected 10"
10391         fi
10392 }
10393 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10394
10395 test_119a() # bug 11737
10396 {
10397         BSIZE=$((512 * 1024))
10398         directio write $DIR/$tfile 0 1 $BSIZE
10399         # We ask to read two blocks, which is more than a file size.
10400         # directio will indicate an error when requested and actual
10401         # sizes aren't equeal (a normal situation in this case) and
10402         # print actual read amount.
10403         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10404         if [ "$NOB" != "$BSIZE" ]; then
10405                 error "read $NOB bytes instead of $BSIZE"
10406         fi
10407         rm -f $DIR/$tfile
10408 }
10409 run_test 119a "Short directIO read must return actual read amount"
10410
10411 test_119b() # bug 11737
10412 {
10413         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10414
10415         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
10416         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10417         sync
10418         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10419                 error "direct read failed"
10420         rm -f $DIR/$tfile
10421 }
10422 run_test 119b "Sparse directIO read must return actual read amount"
10423
10424 test_119c() # bug 13099
10425 {
10426         BSIZE=1048576
10427         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10428         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10429         rm -f $DIR/$tfile
10430 }
10431 run_test 119c "Testing for direct read hitting hole"
10432
10433 test_119d() # bug 15950
10434 {
10435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10436
10437         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10438         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10439         BSIZE=1048576
10440         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10441         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10442         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10443         lctl set_param fail_loc=0x40d
10444         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10445         pid_dio=$!
10446         sleep 1
10447         cat $DIR/$tfile > /dev/null &
10448         lctl set_param fail_loc=0
10449         pid_reads=$!
10450         wait $pid_dio
10451         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10452         sleep 2
10453         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10454         error "the read rpcs have not completed in 2s"
10455         rm -f $DIR/$tfile
10456         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10457 }
10458 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10459
10460 test_120a() {
10461         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10462         remote_mds_nodsh && skip "remote MDS with nodsh"
10463         test_mkdir $DIR/$tdir
10464         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10465                 skip_env "no early lock cancel on server"
10466
10467         lru_resize_disable mdc
10468         lru_resize_disable osc
10469         cancel_lru_locks mdc
10470         # asynchronous object destroy at MDT could cause bl ast to client
10471         cancel_lru_locks osc
10472
10473         stat $DIR/$tdir > /dev/null
10474         can1=$(do_facet $SINGLEMDS \
10475                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10476                awk '/ldlm_cancel/ {print $2}')
10477         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10478                awk '/ldlm_bl_callback/ {print $2}')
10479         test_mkdir -c1 $DIR/$tdir/d1
10480         can2=$(do_facet $SINGLEMDS \
10481                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10482                awk '/ldlm_cancel/ {print $2}')
10483         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10484                awk '/ldlm_bl_callback/ {print $2}')
10485         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10486         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10487         lru_resize_enable mdc
10488         lru_resize_enable osc
10489 }
10490 run_test 120a "Early Lock Cancel: mkdir test"
10491
10492 test_120b() {
10493         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10494         remote_mds_nodsh && skip "remote MDS with nodsh"
10495         test_mkdir $DIR/$tdir
10496         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10497                 skip_env "no early lock cancel on server"
10498
10499         lru_resize_disable mdc
10500         lru_resize_disable osc
10501         cancel_lru_locks mdc
10502         stat $DIR/$tdir > /dev/null
10503         can1=$(do_facet $SINGLEMDS \
10504                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10505                awk '/ldlm_cancel/ {print $2}')
10506         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10507                awk '/ldlm_bl_callback/ {print $2}')
10508         touch $DIR/$tdir/f1
10509         can2=$(do_facet $SINGLEMDS \
10510                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10511                awk '/ldlm_cancel/ {print $2}')
10512         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10513                awk '/ldlm_bl_callback/ {print $2}')
10514         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10515         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10516         lru_resize_enable mdc
10517         lru_resize_enable osc
10518 }
10519 run_test 120b "Early Lock Cancel: create test"
10520
10521 test_120c() {
10522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10523         remote_mds_nodsh && skip "remote MDS with nodsh"
10524         test_mkdir -c1 $DIR/$tdir
10525         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10526                 skip "no early lock cancel on server"
10527
10528         lru_resize_disable mdc
10529         lru_resize_disable osc
10530         test_mkdir -c1 $DIR/$tdir/d1
10531         test_mkdir -c1 $DIR/$tdir/d2
10532         touch $DIR/$tdir/d1/f1
10533         cancel_lru_locks mdc
10534         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10535         can1=$(do_facet $SINGLEMDS \
10536                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10537                awk '/ldlm_cancel/ {print $2}')
10538         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10539                awk '/ldlm_bl_callback/ {print $2}')
10540         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10541         can2=$(do_facet $SINGLEMDS \
10542                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10543                awk '/ldlm_cancel/ {print $2}')
10544         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10545                awk '/ldlm_bl_callback/ {print $2}')
10546         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10547         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10548         lru_resize_enable mdc
10549         lru_resize_enable osc
10550 }
10551 run_test 120c "Early Lock Cancel: link test"
10552
10553 test_120d() {
10554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10555         remote_mds_nodsh && skip "remote MDS with nodsh"
10556         test_mkdir -c1 $DIR/$tdir
10557         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10558                 skip_env "no early lock cancel on server"
10559
10560         lru_resize_disable mdc
10561         lru_resize_disable osc
10562         touch $DIR/$tdir
10563         cancel_lru_locks mdc
10564         stat $DIR/$tdir > /dev/null
10565         can1=$(do_facet $SINGLEMDS \
10566                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10567                awk '/ldlm_cancel/ {print $2}')
10568         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10569                awk '/ldlm_bl_callback/ {print $2}')
10570         chmod a+x $DIR/$tdir
10571         can2=$(do_facet $SINGLEMDS \
10572                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10573                awk '/ldlm_cancel/ {print $2}')
10574         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10575                awk '/ldlm_bl_callback/ {print $2}')
10576         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10577         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10578         lru_resize_enable mdc
10579         lru_resize_enable osc
10580 }
10581 run_test 120d "Early Lock Cancel: setattr test"
10582
10583 test_120e() {
10584         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10585         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10586                 skip_env "no early lock cancel on server"
10587         remote_mds_nodsh && skip "remote MDS with nodsh"
10588
10589         local dlmtrace_set=false
10590
10591         test_mkdir -c1 $DIR/$tdir
10592         lru_resize_disable mdc
10593         lru_resize_disable osc
10594         ! $LCTL get_param debug | grep -q dlmtrace &&
10595                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10596         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10597         cancel_lru_locks mdc
10598         cancel_lru_locks osc
10599         dd if=$DIR/$tdir/f1 of=/dev/null
10600         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10601         # XXX client can not do early lock cancel of OST lock
10602         # during unlink (LU-4206), so cancel osc lock now.
10603         sleep 2
10604         cancel_lru_locks osc
10605         can1=$(do_facet $SINGLEMDS \
10606                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10607                awk '/ldlm_cancel/ {print $2}')
10608         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10609                awk '/ldlm_bl_callback/ {print $2}')
10610         unlink $DIR/$tdir/f1
10611         sleep 5
10612         can2=$(do_facet $SINGLEMDS \
10613                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10614                awk '/ldlm_cancel/ {print $2}')
10615         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10616                awk '/ldlm_bl_callback/ {print $2}')
10617         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10618                 $LCTL dk $TMP/cancel.debug.txt
10619         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10620                 $LCTL dk $TMP/blocking.debug.txt
10621         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10622         lru_resize_enable mdc
10623         lru_resize_enable osc
10624 }
10625 run_test 120e "Early Lock Cancel: unlink test"
10626
10627 test_120f() {
10628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10629         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10630                 skip_env "no early lock cancel on server"
10631         remote_mds_nodsh && skip "remote MDS with nodsh"
10632
10633         test_mkdir -c1 $DIR/$tdir
10634         lru_resize_disable mdc
10635         lru_resize_disable osc
10636         test_mkdir -c1 $DIR/$tdir/d1
10637         test_mkdir -c1 $DIR/$tdir/d2
10638         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10639         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10640         cancel_lru_locks mdc
10641         cancel_lru_locks osc
10642         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10643         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10644         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10645         # XXX client can not do early lock cancel of OST lock
10646         # during rename (LU-4206), so cancel osc lock now.
10647         sleep 2
10648         cancel_lru_locks osc
10649         can1=$(do_facet $SINGLEMDS \
10650                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10651                awk '/ldlm_cancel/ {print $2}')
10652         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10653                awk '/ldlm_bl_callback/ {print $2}')
10654         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10655         sleep 5
10656         can2=$(do_facet $SINGLEMDS \
10657                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10658                awk '/ldlm_cancel/ {print $2}')
10659         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10660                awk '/ldlm_bl_callback/ {print $2}')
10661         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10662         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10663         lru_resize_enable mdc
10664         lru_resize_enable osc
10665 }
10666 run_test 120f "Early Lock Cancel: rename test"
10667
10668 test_120g() {
10669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10670         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10671                 skip_env "no early lock cancel on server"
10672         remote_mds_nodsh && skip "remote MDS with nodsh"
10673
10674         lru_resize_disable mdc
10675         lru_resize_disable osc
10676         count=10000
10677         echo create $count files
10678         test_mkdir $DIR/$tdir
10679         cancel_lru_locks mdc
10680         cancel_lru_locks osc
10681         t0=$(date +%s)
10682
10683         can0=$(do_facet $SINGLEMDS \
10684                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10685                awk '/ldlm_cancel/ {print $2}')
10686         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10687                awk '/ldlm_bl_callback/ {print $2}')
10688         createmany -o $DIR/$tdir/f $count
10689         sync
10690         can1=$(do_facet $SINGLEMDS \
10691                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10692                awk '/ldlm_cancel/ {print $2}')
10693         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10694                awk '/ldlm_bl_callback/ {print $2}')
10695         t1=$(date +%s)
10696         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
10697         echo rm $count files
10698         rm -r $DIR/$tdir
10699         sync
10700         can2=$(do_facet $SINGLEMDS \
10701                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10702                awk '/ldlm_cancel/ {print $2}')
10703         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10704                awk '/ldlm_bl_callback/ {print $2}')
10705         t2=$(date +%s)
10706         echo total: $count removes in $((t2-t1))
10707         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
10708         sleep 2
10709         # wait for commitment of removal
10710         lru_resize_enable mdc
10711         lru_resize_enable osc
10712 }
10713 run_test 120g "Early Lock Cancel: performance test"
10714
10715 test_121() { #bug #10589
10716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10717
10718         rm -rf $DIR/$tfile
10719         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
10720 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
10721         lctl set_param fail_loc=0x310
10722         cancel_lru_locks osc > /dev/null
10723         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
10724         lctl set_param fail_loc=0
10725         [[ $reads -eq $writes ]] ||
10726                 error "read $reads blocks, must be $writes blocks"
10727 }
10728 run_test 121 "read cancel race ========="
10729
10730 test_123a() { # was test 123, statahead(bug 11401)
10731         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10732
10733         SLOWOK=0
10734         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
10735                 log "testing UP system. Performance may be lower than expected."
10736                 SLOWOK=1
10737         fi
10738
10739         rm -rf $DIR/$tdir
10740         test_mkdir $DIR/$tdir
10741         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
10742         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
10743         MULT=10
10744         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
10745                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
10746
10747                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10748                 lctl set_param -n llite.*.statahead_max 0
10749                 lctl get_param llite.*.statahead_max
10750                 cancel_lru_locks mdc
10751                 cancel_lru_locks osc
10752                 stime=`date +%s`
10753                 time ls -l $DIR/$tdir | wc -l
10754                 etime=`date +%s`
10755                 delta=$((etime - stime))
10756                 log "ls $i files without statahead: $delta sec"
10757                 lctl set_param llite.*.statahead_max=$max
10758
10759                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10760                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
10761                 cancel_lru_locks mdc
10762                 cancel_lru_locks osc
10763                 stime=`date +%s`
10764                 time ls -l $DIR/$tdir | wc -l
10765                 etime=`date +%s`
10766                 delta_sa=$((etime - stime))
10767                 log "ls $i files with statahead: $delta_sa sec"
10768                 lctl get_param -n llite.*.statahead_stats
10769                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10770
10771                 [[ $swrong -lt $ewrong ]] &&
10772                         log "statahead was stopped, maybe too many locks held!"
10773                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
10774
10775                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10776                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10777                     lctl set_param -n llite.*.statahead_max 0
10778                     lctl get_param llite.*.statahead_max
10779                     cancel_lru_locks mdc
10780                     cancel_lru_locks osc
10781                     stime=`date +%s`
10782                     time ls -l $DIR/$tdir | wc -l
10783                     etime=`date +%s`
10784                     delta=$((etime - stime))
10785                     log "ls $i files again without statahead: $delta sec"
10786                     lctl set_param llite.*.statahead_max=$max
10787                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10788                         if [  $SLOWOK -eq 0 ]; then
10789                                 error "ls $i files is slower with statahead!"
10790                         else
10791                                 log "ls $i files is slower with statahead!"
10792                         fi
10793                         break
10794                     fi
10795                 fi
10796
10797                 [ $delta -gt 20 ] && break
10798                 [ $delta -gt 8 ] && MULT=$((50 / delta))
10799                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
10800         done
10801         log "ls done"
10802
10803         stime=`date +%s`
10804         rm -r $DIR/$tdir
10805         sync
10806         etime=`date +%s`
10807         delta=$((etime - stime))
10808         log "rm -r $DIR/$tdir/: $delta seconds"
10809         log "rm done"
10810         lctl get_param -n llite.*.statahead_stats
10811 }
10812 run_test 123a "verify statahead work"
10813
10814 test_123b () { # statahead(bug 15027)
10815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10816
10817         test_mkdir $DIR/$tdir
10818         createmany -o $DIR/$tdir/$tfile-%d 1000
10819
10820         cancel_lru_locks mdc
10821         cancel_lru_locks osc
10822
10823 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
10824         lctl set_param fail_loc=0x80000803
10825         ls -lR $DIR/$tdir > /dev/null
10826         log "ls done"
10827         lctl set_param fail_loc=0x0
10828         lctl get_param -n llite.*.statahead_stats
10829         rm -r $DIR/$tdir
10830         sync
10831
10832 }
10833 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
10834
10835 test_123c() {
10836         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
10837
10838         test_mkdir -i 0 -c 1 $DIR/$tdir.0
10839         test_mkdir -i 1 -c 1 $DIR/$tdir.1
10840         touch $DIR/$tdir.1/{1..3}
10841         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
10842
10843         remount_client $MOUNT
10844
10845         $MULTIOP $DIR/$tdir.0 Q
10846
10847         # let statahead to complete
10848         ls -l $DIR/$tdir.0 > /dev/null
10849
10850         testid=$(echo $TESTNAME | tr '_' ' ')
10851         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
10852                 error "statahead warning" || true
10853 }
10854 run_test 123c "Can not initialize inode warning on DNE statahead"
10855
10856 test_124a() {
10857         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10858         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10859                 skip_env "no lru resize on server"
10860
10861         local NR=2000
10862
10863         test_mkdir $DIR/$tdir
10864
10865         log "create $NR files at $DIR/$tdir"
10866         createmany -o $DIR/$tdir/f $NR ||
10867                 error "failed to create $NR files in $DIR/$tdir"
10868
10869         cancel_lru_locks mdc
10870         ls -l $DIR/$tdir > /dev/null
10871
10872         local NSDIR=""
10873         local LRU_SIZE=0
10874         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
10875                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
10876                 LRU_SIZE=$($LCTL get_param -n $PARAM)
10877                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
10878                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
10879                         log "NSDIR=$NSDIR"
10880                         log "NS=$(basename $NSDIR)"
10881                         break
10882                 fi
10883         done
10884
10885         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
10886                 skip "Not enough cached locks created!"
10887         fi
10888         log "LRU=$LRU_SIZE"
10889
10890         local SLEEP=30
10891
10892         # We know that lru resize allows one client to hold $LIMIT locks
10893         # for 10h. After that locks begin to be killed by client.
10894         local MAX_HRS=10
10895         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
10896         log "LIMIT=$LIMIT"
10897         if [ $LIMIT -lt $LRU_SIZE ]; then
10898                 skip "Limit is too small $LIMIT"
10899         fi
10900
10901         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
10902         # killing locks. Some time was spent for creating locks. This means
10903         # that up to the moment of sleep finish we must have killed some of
10904         # them (10-100 locks). This depends on how fast ther were created.
10905         # Many of them were touched in almost the same moment and thus will
10906         # be killed in groups.
10907         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
10908
10909         # Use $LRU_SIZE_B here to take into account real number of locks
10910         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
10911         local LRU_SIZE_B=$LRU_SIZE
10912         log "LVF=$LVF"
10913         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
10914         log "OLD_LVF=$OLD_LVF"
10915         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
10916
10917         # Let's make sure that we really have some margin. Client checks
10918         # cached locks every 10 sec.
10919         SLEEP=$((SLEEP+20))
10920         log "Sleep ${SLEEP} sec"
10921         local SEC=0
10922         while ((SEC<$SLEEP)); do
10923                 echo -n "..."
10924                 sleep 5
10925                 SEC=$((SEC+5))
10926                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
10927                 echo -n "$LRU_SIZE"
10928         done
10929         echo ""
10930         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
10931         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
10932
10933         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
10934                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
10935                 unlinkmany $DIR/$tdir/f $NR
10936                 return
10937         }
10938
10939         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
10940         log "unlink $NR files at $DIR/$tdir"
10941         unlinkmany $DIR/$tdir/f $NR
10942 }
10943 run_test 124a "lru resize ======================================="
10944
10945 get_max_pool_limit()
10946 {
10947         local limit=$($LCTL get_param \
10948                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
10949         local max=0
10950         for l in $limit; do
10951                 if [[ $l -gt $max ]]; then
10952                         max=$l
10953                 fi
10954         done
10955         echo $max
10956 }
10957
10958 test_124b() {
10959         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10960         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10961                 skip_env "no lru resize on server"
10962
10963         LIMIT=$(get_max_pool_limit)
10964
10965         NR=$(($(default_lru_size)*20))
10966         if [[ $NR -gt $LIMIT ]]; then
10967                 log "Limit lock number by $LIMIT locks"
10968                 NR=$LIMIT
10969         fi
10970
10971         IFree=$(mdsrate_inodes_available)
10972         if [ $IFree -lt $NR ]; then
10973                 log "Limit lock number by $IFree inodes"
10974                 NR=$IFree
10975         fi
10976
10977         lru_resize_disable mdc
10978         test_mkdir -p $DIR/$tdir/disable_lru_resize
10979
10980         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
10981         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
10982         cancel_lru_locks mdc
10983         stime=`date +%s`
10984         PID=""
10985         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10986         PID="$PID $!"
10987         sleep 2
10988         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10989         PID="$PID $!"
10990         sleep 2
10991         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10992         PID="$PID $!"
10993         wait $PID
10994         etime=`date +%s`
10995         nolruresize_delta=$((etime-stime))
10996         log "ls -la time: $nolruresize_delta seconds"
10997         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10998         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
10999
11000         lru_resize_enable mdc
11001         test_mkdir -p $DIR/$tdir/enable_lru_resize
11002
11003         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11004         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11005         cancel_lru_locks mdc
11006         stime=`date +%s`
11007         PID=""
11008         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11009         PID="$PID $!"
11010         sleep 2
11011         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11012         PID="$PID $!"
11013         sleep 2
11014         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11015         PID="$PID $!"
11016         wait $PID
11017         etime=`date +%s`
11018         lruresize_delta=$((etime-stime))
11019         log "ls -la time: $lruresize_delta seconds"
11020         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11021
11022         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11023                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11024         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11025                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11026         else
11027                 log "lru resize performs the same with no lru resize"
11028         fi
11029         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11030 }
11031 run_test 124b "lru resize (performance test) ======================="
11032
11033 test_124c() {
11034         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11035         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11036                 skip_env "no lru resize on server"
11037
11038         # cache ununsed locks on client
11039         local nr=100
11040         cancel_lru_locks mdc
11041         test_mkdir $DIR/$tdir
11042         createmany -o $DIR/$tdir/f $nr ||
11043                 error "failed to create $nr files in $DIR/$tdir"
11044         ls -l $DIR/$tdir > /dev/null
11045
11046         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11047         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11048         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11049         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11050         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11051
11052         # set lru_max_age to 1 sec
11053         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11054         echo "sleep $((recalc_p * 2)) seconds..."
11055         sleep $((recalc_p * 2))
11056
11057         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11058         # restore lru_max_age
11059         $LCTL set_param -n $nsdir.lru_max_age $max_age
11060         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11061         unlinkmany $DIR/$tdir/f $nr
11062 }
11063 run_test 124c "LRUR cancel very aged locks"
11064
11065 test_124d() {
11066         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11067         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11068                 skip_env "no lru resize on server"
11069
11070         # cache ununsed locks on client
11071         local nr=100
11072
11073         lru_resize_disable mdc
11074         stack_trap "lru_resize_enable mdc" EXIT
11075
11076         cancel_lru_locks mdc
11077
11078         # asynchronous object destroy at MDT could cause bl ast to client
11079         test_mkdir $DIR/$tdir
11080         createmany -o $DIR/$tdir/f $nr ||
11081                 error "failed to create $nr files in $DIR/$tdir"
11082         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11083
11084         ls -l $DIR/$tdir > /dev/null
11085
11086         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11087         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11088         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11089         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11090
11091         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11092
11093         # set lru_max_age to 1 sec
11094         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11095         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11096
11097         echo "sleep $((recalc_p * 2)) seconds..."
11098         sleep $((recalc_p * 2))
11099
11100         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11101
11102         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11103 }
11104 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11105
11106 test_125() { # 13358
11107         $LCTL get_param -n llite.*.client_type | grep -q local ||
11108                 skip "must run as local client"
11109         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11110                 skip_env "must have acl enabled"
11111         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11112
11113         test_mkdir $DIR/$tdir
11114         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11115         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11116         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11117 }
11118 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11119
11120 test_126() { # bug 12829/13455
11121         $GSS && skip_env "must run as gss disabled"
11122         $LCTL get_param -n llite.*.client_type | grep -q local ||
11123                 skip "must run as local client"
11124         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11125
11126         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11127         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11128         rm -f $DIR/$tfile
11129         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11130 }
11131 run_test 126 "check that the fsgid provided by the client is taken into account"
11132
11133 test_127a() { # bug 15521
11134         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11135
11136         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11137         $LCTL set_param osc.*.stats=0
11138         FSIZE=$((2048 * 1024))
11139         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
11140         cancel_lru_locks osc
11141         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
11142
11143         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
11144         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
11145                 echo "got $COUNT $NAME"
11146                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
11147                 eval $NAME=$COUNT || error "Wrong proc format"
11148
11149                 case $NAME in
11150                         read_bytes|write_bytes)
11151                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
11152                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
11153                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
11154                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
11155                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
11156                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
11157                                 error "sumsquare is too small: $SUMSQ"
11158                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
11159                                 error "sumsquare is too big: $SUMSQ"
11160                         ;;
11161                         *) ;;
11162                 esac
11163         done < $DIR/${tfile}.tmp
11164
11165         #check that we actually got some stats
11166         [ "$read_bytes" ] || error "Missing read_bytes stats"
11167         [ "$write_bytes" ] || error "Missing write_bytes stats"
11168         [ "$read_bytes" != 0 ] || error "no read done"
11169         [ "$write_bytes" != 0 ] || error "no write done"
11170 }
11171 run_test 127a "verify the client stats are sane"
11172
11173 test_127b() { # bug LU-333
11174         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11175         local name count samp unit min max sum sumsq
11176
11177         $LCTL set_param llite.*.stats=0
11178
11179         # perform 2 reads and writes so MAX is different from SUM.
11180         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11181         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11182         cancel_lru_locks osc
11183         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11184         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11185
11186         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11187         while read name count samp unit min max sum sumsq; do
11188                 echo "got $count $name"
11189                 eval $name=$count || error "Wrong proc format"
11190
11191                 case $name in
11192                 read_bytes)
11193                         [ $count -ne 2 ] && error "count is not 2: $count"
11194                         [ $min -ne $PAGE_SIZE ] &&
11195                                 error "min is not $PAGE_SIZE: $min"
11196                         [ $max -ne $PAGE_SIZE ] &&
11197                                 error "max is incorrect: $max"
11198                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11199                                 error "sum is wrong: $sum"
11200                         ;;
11201                 write_bytes)
11202                         [ $count -ne 2 ] && error "count is not 2: $count"
11203                         [ $min -ne $PAGE_SIZE ] &&
11204                                 error "min is not $PAGE_SIZE: $min"
11205                         [ $max -ne $PAGE_SIZE ] &&
11206                                 error "max is incorrect: $max"
11207                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
11208                                 error "sum is wrong: $sum"
11209                         ;;
11210                 *) ;;
11211                 esac
11212         done < $TMP/$tfile.tmp
11213
11214         #check that we actually got some stats
11215         [ "$read_bytes" ] || error "Missing read_bytes stats"
11216         [ "$write_bytes" ] || error "Missing write_bytes stats"
11217         [ "$read_bytes" != 0 ] || error "no read done"
11218         [ "$write_bytes" != 0 ] || error "no write done"
11219
11220         rm -f $TMP/${tfile}.tmp
11221 }
11222 run_test 127b "verify the llite client stats are sane"
11223
11224 test_127c() { # LU-12394
11225         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11226         local size
11227         local bsize
11228         local reads
11229         local writes
11230         local count
11231
11232         $LCTL set_param llite.*.extents_stats=1
11233         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
11234
11235         # Use two stripes so there is enough space in default config
11236         $LFS setstripe -c 2 $DIR/$tfile
11237
11238         # Extent stats start at 0-4K and go in power of two buckets
11239         # LL_HIST_START = 12 --> 2^12 = 4K
11240         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
11241         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
11242         # small configs
11243         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
11244                 do
11245                 # Write and read, 2x each, second time at a non-zero offset
11246                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
11247                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
11248                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
11249                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
11250                 rm -f $DIR/$tfile
11251         done
11252
11253         $LCTL get_param llite.*.extents_stats
11254
11255         count=2
11256         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
11257                 do
11258                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
11259                                 grep -m 1 $bsize)
11260                 reads=$(echo $bucket | awk '{print $5}')
11261                 writes=$(echo $bucket | awk '{print $9}')
11262                 [ "$reads" -eq $count ] ||
11263                         error "$reads reads in < $bsize bucket, expect $count"
11264                 [ "$writes" -eq $count ] ||
11265                         error "$writes writes in < $bsize bucket, expect $count"
11266         done
11267
11268         # Test mmap write and read
11269         $LCTL set_param llite.*.extents_stats=c
11270         size=512
11271         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
11272         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
11273         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
11274
11275         $LCTL get_param llite.*.extents_stats
11276
11277         count=$(((size*1024) / PAGE_SIZE))
11278
11279         bsize=$((2 * PAGE_SIZE / 1024))K
11280
11281         bucket=$($LCTL get_param -n llite.*.extents_stats |
11282                         grep -m 1 $bsize)
11283         reads=$(echo $bucket | awk '{print $5}')
11284         writes=$(echo $bucket | awk '{print $9}')
11285         # mmap writes fault in the page first, creating an additonal read
11286         [ "$reads" -eq $((2 * count)) ] ||
11287                 error "$reads reads in < $bsize bucket, expect $count"
11288         [ "$writes" -eq $count ] ||
11289                 error "$writes writes in < $bsize bucket, expect $count"
11290 }
11291 run_test 127c "test llite extent stats with regular & mmap i/o"
11292
11293 test_128() { # bug 15212
11294         touch $DIR/$tfile
11295         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
11296                 find $DIR/$tfile
11297                 find $DIR/$tfile
11298         EOF
11299
11300         result=$(grep error $TMP/$tfile.log)
11301         rm -f $DIR/$tfile $TMP/$tfile.log
11302         [ -z "$result" ] ||
11303                 error "consecutive find's under interactive lfs failed"
11304 }
11305 run_test 128 "interactive lfs for 2 consecutive find's"
11306
11307 set_dir_limits () {
11308         local mntdev
11309         local canondev
11310         local node
11311
11312         local ldproc=/proc/fs/ldiskfs
11313         local facets=$(get_facets MDS)
11314
11315         for facet in ${facets//,/ }; do
11316                 canondev=$(ldiskfs_canon \
11317                            *.$(convert_facet2label $facet).mntdev $facet)
11318                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
11319                         ldproc=/sys/fs/ldiskfs
11320                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
11321                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
11322         done
11323 }
11324
11325 check_mds_dmesg() {
11326         local facets=$(get_facets MDS)
11327         for facet in ${facets//,/ }; do
11328                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
11329         done
11330         return 1
11331 }
11332
11333 test_129() {
11334         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11335         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
11336                 skip "Need MDS version with at least 2.5.56"
11337         if [ "$mds1_FSTYPE" != ldiskfs ]; then
11338                 skip_env "ldiskfs only test"
11339         fi
11340         remote_mds_nodsh && skip "remote MDS with nodsh"
11341
11342         local ENOSPC=28
11343         local EFBIG=27
11344         local has_warning=false
11345
11346         rm -rf $DIR/$tdir
11347         mkdir -p $DIR/$tdir
11348
11349         # block size of mds1
11350         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11351         set_dir_limits $maxsize $maxsize
11352         local dirsize=$(stat -c%s "$DIR/$tdir")
11353         local nfiles=0
11354         while [[ $dirsize -le $maxsize ]]; do
11355                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11356                 rc=$?
11357                 if ! $has_warning; then
11358                         check_mds_dmesg '"is approaching"' && has_warning=true
11359                 fi
11360                 # check two errors:
11361                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11362                 # EFBIG for previous versions included in ldiskfs series
11363                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
11364                         set_dir_limits 0 0
11365                         echo "return code $rc received as expected"
11366
11367                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11368                                 error_exit "create failed w/o dir size limit"
11369
11370                         check_mds_dmesg '"has reached"' ||
11371                                 error_exit "reached message should be output"
11372
11373                         [ $has_warning = "false" ] &&
11374                                 error_exit "warning message should be output"
11375
11376                         dirsize=$(stat -c%s "$DIR/$tdir")
11377
11378                         [[ $dirsize -ge $maxsize ]] && return 0
11379                         error_exit "current dir size $dirsize, " \
11380                                    "previous limit $maxsize"
11381                 elif [ $rc -ne 0 ]; then
11382                         set_dir_limits 0 0
11383                         error_exit "return $rc received instead of expected " \
11384                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11385                 fi
11386                 nfiles=$((nfiles + 1))
11387                 dirsize=$(stat -c%s "$DIR/$tdir")
11388         done
11389
11390         set_dir_limits 0 0
11391         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11392 }
11393 run_test 129 "test directory size limit ========================"
11394
11395 OLDIFS="$IFS"
11396 cleanup_130() {
11397         trap 0
11398         IFS="$OLDIFS"
11399 }
11400
11401 test_130a() {
11402         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11403         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11404
11405         trap cleanup_130 EXIT RETURN
11406
11407         local fm_file=$DIR/$tfile
11408         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11409         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11410                 error "dd failed for $fm_file"
11411
11412         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11413         filefrag -ves $fm_file
11414         RC=$?
11415         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11416                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11417         [ $RC != 0 ] && error "filefrag $fm_file failed"
11418
11419         filefrag_op=$(filefrag -ve -k $fm_file |
11420                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11421         lun=$($GETSTRIPE -i $fm_file)
11422
11423         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11424         IFS=$'\n'
11425         tot_len=0
11426         for line in $filefrag_op
11427         do
11428                 frag_lun=`echo $line | cut -d: -f5`
11429                 ext_len=`echo $line | cut -d: -f4`
11430                 if (( $frag_lun != $lun )); then
11431                         cleanup_130
11432                         error "FIEMAP on 1-stripe file($fm_file) failed"
11433                         return
11434                 fi
11435                 (( tot_len += ext_len ))
11436         done
11437
11438         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11439                 cleanup_130
11440                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11441                 return
11442         fi
11443
11444         cleanup_130
11445
11446         echo "FIEMAP on single striped file succeeded"
11447 }
11448 run_test 130a "FIEMAP (1-stripe file)"
11449
11450 test_130b() {
11451         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11452
11453         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11454         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11455
11456         trap cleanup_130 EXIT RETURN
11457
11458         local fm_file=$DIR/$tfile
11459         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11460                         error "setstripe on $fm_file"
11461         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11462                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11463
11464         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11465                 error "dd failed on $fm_file"
11466
11467         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11468         filefrag_op=$(filefrag -ve -k $fm_file |
11469                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11470
11471         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11472                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11473
11474         IFS=$'\n'
11475         tot_len=0
11476         num_luns=1
11477         for line in $filefrag_op
11478         do
11479                 frag_lun=$(echo $line | cut -d: -f5 |
11480                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11481                 ext_len=$(echo $line | cut -d: -f4)
11482                 if (( $frag_lun != $last_lun )); then
11483                         if (( tot_len != 1024 )); then
11484                                 cleanup_130
11485                                 error "FIEMAP on $fm_file failed; returned " \
11486                                 "len $tot_len for OST $last_lun instead of 1024"
11487                                 return
11488                         else
11489                                 (( num_luns += 1 ))
11490                                 tot_len=0
11491                         fi
11492                 fi
11493                 (( tot_len += ext_len ))
11494                 last_lun=$frag_lun
11495         done
11496         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11497                 cleanup_130
11498                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11499                         "luns or wrong len for OST $last_lun"
11500                 return
11501         fi
11502
11503         cleanup_130
11504
11505         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11506 }
11507 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11508
11509 test_130c() {
11510         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11511
11512         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11513         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11514
11515         trap cleanup_130 EXIT RETURN
11516
11517         local fm_file=$DIR/$tfile
11518         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11519         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11520                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11521
11522         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11523                         error "dd failed on $fm_file"
11524
11525         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11526         filefrag_op=$(filefrag -ve -k $fm_file |
11527                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11528
11529         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11530                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11531
11532         IFS=$'\n'
11533         tot_len=0
11534         num_luns=1
11535         for line in $filefrag_op
11536         do
11537                 frag_lun=$(echo $line | cut -d: -f5 |
11538                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11539                 ext_len=$(echo $line | cut -d: -f4)
11540                 if (( $frag_lun != $last_lun )); then
11541                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11542                         if (( logical != 512 )); then
11543                                 cleanup_130
11544                                 error "FIEMAP on $fm_file failed; returned " \
11545                                 "logical start for lun $logical instead of 512"
11546                                 return
11547                         fi
11548                         if (( tot_len != 512 )); then
11549                                 cleanup_130
11550                                 error "FIEMAP on $fm_file failed; returned " \
11551                                 "len $tot_len for OST $last_lun instead of 1024"
11552                                 return
11553                         else
11554                                 (( num_luns += 1 ))
11555                                 tot_len=0
11556                         fi
11557                 fi
11558                 (( tot_len += ext_len ))
11559                 last_lun=$frag_lun
11560         done
11561         if (( num_luns != 2 || tot_len != 512 )); then
11562                 cleanup_130
11563                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11564                         "luns or wrong len for OST $last_lun"
11565                 return
11566         fi
11567
11568         cleanup_130
11569
11570         echo "FIEMAP on 2-stripe file with hole succeeded"
11571 }
11572 run_test 130c "FIEMAP (2-stripe file with hole)"
11573
11574 test_130d() {
11575         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11576
11577         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11578         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11579
11580         trap cleanup_130 EXIT RETURN
11581
11582         local fm_file=$DIR/$tfile
11583         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11584                         error "setstripe on $fm_file"
11585         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11586                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11587
11588         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
11589         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11590                 error "dd failed on $fm_file"
11591
11592         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11593         filefrag_op=$(filefrag -ve -k $fm_file |
11594                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11595
11596         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11597                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11598
11599         IFS=$'\n'
11600         tot_len=0
11601         num_luns=1
11602         for line in $filefrag_op
11603         do
11604                 frag_lun=$(echo $line | cut -d: -f5 |
11605                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11606                 ext_len=$(echo $line | cut -d: -f4)
11607                 if (( $frag_lun != $last_lun )); then
11608                         if (( tot_len != 1024 )); then
11609                                 cleanup_130
11610                                 error "FIEMAP on $fm_file failed; returned " \
11611                                 "len $tot_len for OST $last_lun instead of 1024"
11612                                 return
11613                         else
11614                                 (( num_luns += 1 ))
11615                                 tot_len=0
11616                         fi
11617                 fi
11618                 (( tot_len += ext_len ))
11619                 last_lun=$frag_lun
11620         done
11621         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11622                 cleanup_130
11623                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11624                         "luns or wrong len for OST $last_lun"
11625                 return
11626         fi
11627
11628         cleanup_130
11629
11630         echo "FIEMAP on N-stripe file succeeded"
11631 }
11632 run_test 130d "FIEMAP (N-stripe file)"
11633
11634 test_130e() {
11635         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11636
11637         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11638         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11639
11640         trap cleanup_130 EXIT RETURN
11641
11642         local fm_file=$DIR/$tfile
11643         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11644         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11645                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11646
11647         NUM_BLKS=512
11648         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11649         for ((i = 0; i < $NUM_BLKS; i++))
11650         do
11651                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11652         done
11653
11654         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11655         filefrag_op=$(filefrag -ve -k $fm_file |
11656                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11657
11658         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11659                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11660
11661         IFS=$'\n'
11662         tot_len=0
11663         num_luns=1
11664         for line in $filefrag_op
11665         do
11666                 frag_lun=$(echo $line | cut -d: -f5 |
11667                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11668                 ext_len=$(echo $line | cut -d: -f4)
11669                 if (( $frag_lun != $last_lun )); then
11670                         if (( tot_len != $EXPECTED_LEN )); then
11671                                 cleanup_130
11672                                 error "FIEMAP on $fm_file failed; returned " \
11673                                 "len $tot_len for OST $last_lun instead " \
11674                                 "of $EXPECTED_LEN"
11675                                 return
11676                         else
11677                                 (( num_luns += 1 ))
11678                                 tot_len=0
11679                         fi
11680                 fi
11681                 (( tot_len += ext_len ))
11682                 last_lun=$frag_lun
11683         done
11684         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11685                 cleanup_130
11686                 error "FIEMAP on $fm_file failed; returned wrong number " \
11687                         "of luns or wrong len for OST $last_lun"
11688                 return
11689         fi
11690
11691         cleanup_130
11692
11693         echo "FIEMAP with continuation calls succeeded"
11694 }
11695 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11696
11697 test_130f() {
11698         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11699         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11700
11701         local fm_file=$DIR/$tfile
11702         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
11703                 error "multiop create with lov_delay_create on $fm_file"
11704
11705         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11706         filefrag_extents=$(filefrag -vek $fm_file |
11707                            awk '/extents? found/ { print $2 }')
11708         if [[ "$filefrag_extents" != "0" ]]; then
11709                 error "FIEMAP on $fm_file failed; " \
11710                       "returned $filefrag_extents expected 0"
11711         fi
11712
11713         rm -f $fm_file
11714 }
11715 run_test 130f "FIEMAP (unstriped file)"
11716
11717 # Test for writev/readv
11718 test_131a() {
11719         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
11720                 error "writev test failed"
11721         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
11722                 error "readv failed"
11723         rm -f $DIR/$tfile
11724 }
11725 run_test 131a "test iov's crossing stripe boundary for writev/readv"
11726
11727 test_131b() {
11728         local fsize=$((524288 + 1048576 + 1572864))
11729         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
11730                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11731                         error "append writev test failed"
11732
11733         ((fsize += 1572864 + 1048576))
11734         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
11735                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11736                         error "append writev test failed"
11737         rm -f $DIR/$tfile
11738 }
11739 run_test 131b "test append writev"
11740
11741 test_131c() {
11742         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
11743         error "NOT PASS"
11744 }
11745 run_test 131c "test read/write on file w/o objects"
11746
11747 test_131d() {
11748         rwv -f $DIR/$tfile -w -n 1 1572864
11749         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
11750         if [ "$NOB" != 1572864 ]; then
11751                 error "Short read filed: read $NOB bytes instead of 1572864"
11752         fi
11753         rm -f $DIR/$tfile
11754 }
11755 run_test 131d "test short read"
11756
11757 test_131e() {
11758         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
11759         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
11760         error "read hitting hole failed"
11761         rm -f $DIR/$tfile
11762 }
11763 run_test 131e "test read hitting hole"
11764
11765 check_stats() {
11766         local facet=$1
11767         local op=$2
11768         local want=${3:-0}
11769         local res
11770
11771         case $facet in
11772         mds*) res=$(do_facet $facet \
11773                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
11774                  ;;
11775         ost*) res=$(do_facet $facet \
11776                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
11777                  ;;
11778         *) error "Wrong facet '$facet'" ;;
11779         esac
11780         [ "$res" ] || error "The counter for $op on $facet was not incremented"
11781         # if the argument $3 is zero, it means any stat increment is ok.
11782         if [[ $want -gt 0 ]]; then
11783                 local count=$(echo $res | awk '{ print $2 }')
11784                 [[ $count -ne $want ]] &&
11785                         error "The $op counter on $facet is $count, not $want"
11786         fi
11787 }
11788
11789 test_133a() {
11790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11791         remote_ost_nodsh && skip "remote OST with nodsh"
11792         remote_mds_nodsh && skip "remote MDS with nodsh"
11793         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11794                 skip_env "MDS doesn't support rename stats"
11795
11796         local testdir=$DIR/${tdir}/stats_testdir
11797
11798         mkdir -p $DIR/${tdir}
11799
11800         # clear stats.
11801         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11802         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11803
11804         # verify mdt stats first.
11805         mkdir ${testdir} || error "mkdir failed"
11806         check_stats $SINGLEMDS "mkdir" 1
11807         touch ${testdir}/${tfile} || error "touch failed"
11808         check_stats $SINGLEMDS "open" 1
11809         check_stats $SINGLEMDS "close" 1
11810         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
11811                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
11812                 check_stats $SINGLEMDS "mknod" 2
11813         }
11814         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
11815         check_stats $SINGLEMDS "unlink" 1
11816         rm -f ${testdir}/${tfile} || error "file remove failed"
11817         check_stats $SINGLEMDS "unlink" 2
11818
11819         # remove working dir and check mdt stats again.
11820         rmdir ${testdir} || error "rmdir failed"
11821         check_stats $SINGLEMDS "rmdir" 1
11822
11823         local testdir1=$DIR/${tdir}/stats_testdir1
11824         mkdir -p ${testdir}
11825         mkdir -p ${testdir1}
11826         touch ${testdir1}/test1
11827         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
11828         check_stats $SINGLEMDS "crossdir_rename" 1
11829
11830         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
11831         check_stats $SINGLEMDS "samedir_rename" 1
11832
11833         rm -rf $DIR/${tdir}
11834 }
11835 run_test 133a "Verifying MDT stats ========================================"
11836
11837 test_133b() {
11838         local res
11839
11840         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11841         remote_ost_nodsh && skip "remote OST with nodsh"
11842         remote_mds_nodsh && skip "remote MDS with nodsh"
11843
11844         local testdir=$DIR/${tdir}/stats_testdir
11845
11846         mkdir -p ${testdir} || error "mkdir failed"
11847         touch ${testdir}/${tfile} || error "touch failed"
11848         cancel_lru_locks mdc
11849
11850         # clear stats.
11851         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11852         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11853
11854         # extra mdt stats verification.
11855         chmod 444 ${testdir}/${tfile} || error "chmod failed"
11856         check_stats $SINGLEMDS "setattr" 1
11857         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11858         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
11859         then            # LU-1740
11860                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
11861                 check_stats $SINGLEMDS "getattr" 1
11862         fi
11863         rm -rf $DIR/${tdir}
11864
11865         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
11866         # so the check below is not reliable
11867         [ $MDSCOUNT -eq 1 ] || return 0
11868
11869         # Sleep to avoid a cached response.
11870         #define OBD_STATFS_CACHE_SECONDS 1
11871         sleep 2
11872         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11873         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11874         $LFS df || error "lfs failed"
11875         check_stats $SINGLEMDS "statfs" 1
11876
11877         # check aggregated statfs (LU-10018)
11878         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
11879                 return 0
11880         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
11881                 return 0
11882         sleep 2
11883         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11884         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11885         df $DIR
11886         check_stats $SINGLEMDS "statfs" 1
11887
11888         # We want to check that the client didn't send OST_STATFS to
11889         # ost1 but the MDT also uses OST_STATFS for precreate. So some
11890         # extra care is needed here.
11891         if remote_mds; then
11892                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
11893                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
11894
11895                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
11896                 [ "$res" ] && error "OST got STATFS"
11897         fi
11898
11899         return 0
11900 }
11901 run_test 133b "Verifying extra MDT stats =================================="
11902
11903 test_133c() {
11904         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11905         remote_ost_nodsh && skip "remote OST with nodsh"
11906         remote_mds_nodsh && skip "remote MDS with nodsh"
11907
11908         local testdir=$DIR/$tdir/stats_testdir
11909
11910         test_mkdir -p $testdir
11911
11912         # verify obdfilter stats.
11913         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
11914         sync
11915         cancel_lru_locks osc
11916         wait_delete_completed
11917
11918         # clear stats.
11919         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11920         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11921
11922         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
11923                 error "dd failed"
11924         sync
11925         cancel_lru_locks osc
11926         check_stats ost1 "write" 1
11927
11928         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
11929         check_stats ost1 "read" 1
11930
11931         > $testdir/$tfile || error "truncate failed"
11932         check_stats ost1 "punch" 1
11933
11934         rm -f $testdir/$tfile || error "file remove failed"
11935         wait_delete_completed
11936         check_stats ost1 "destroy" 1
11937
11938         rm -rf $DIR/$tdir
11939 }
11940 run_test 133c "Verifying OST stats ========================================"
11941
11942 order_2() {
11943         local value=$1
11944         local orig=$value
11945         local order=1
11946
11947         while [ $value -ge 2 ]; do
11948                 order=$((order*2))
11949                 value=$((value/2))
11950         done
11951
11952         if [ $orig -gt $order ]; then
11953                 order=$((order*2))
11954         fi
11955         echo $order
11956 }
11957
11958 size_in_KMGT() {
11959     local value=$1
11960     local size=('K' 'M' 'G' 'T');
11961     local i=0
11962     local size_string=$value
11963
11964     while [ $value -ge 1024 ]; do
11965         if [ $i -gt 3 ]; then
11966             #T is the biggest unit we get here, if that is bigger,
11967             #just return XXXT
11968             size_string=${value}T
11969             break
11970         fi
11971         value=$((value >> 10))
11972         if [ $value -lt 1024 ]; then
11973             size_string=${value}${size[$i]}
11974             break
11975         fi
11976         i=$((i + 1))
11977     done
11978
11979     echo $size_string
11980 }
11981
11982 get_rename_size() {
11983         local size=$1
11984         local context=${2:-.}
11985         local sample=$(do_facet $SINGLEMDS $LCTL \
11986                 get_param mdt.$FSNAME-MDT0000.rename_stats |
11987                 grep -A1 $context |
11988                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
11989         echo $sample
11990 }
11991
11992 test_133d() {
11993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11994         remote_ost_nodsh && skip "remote OST with nodsh"
11995         remote_mds_nodsh && skip "remote MDS with nodsh"
11996         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11997                 skip_env "MDS doesn't support rename stats"
11998
11999         local testdir1=$DIR/${tdir}/stats_testdir1
12000         local testdir2=$DIR/${tdir}/stats_testdir2
12001         mkdir -p $DIR/${tdir}
12002
12003         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12004
12005         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12006         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12007
12008         createmany -o $testdir1/test 512 || error "createmany failed"
12009
12010         # check samedir rename size
12011         mv ${testdir1}/test0 ${testdir1}/test_0
12012
12013         local testdir1_size=$(ls -l $DIR/${tdir} |
12014                 awk '/stats_testdir1/ {print $5}')
12015         local testdir2_size=$(ls -l $DIR/${tdir} |
12016                 awk '/stats_testdir2/ {print $5}')
12017
12018         testdir1_size=$(order_2 $testdir1_size)
12019         testdir2_size=$(order_2 $testdir2_size)
12020
12021         testdir1_size=$(size_in_KMGT $testdir1_size)
12022         testdir2_size=$(size_in_KMGT $testdir2_size)
12023
12024         echo "source rename dir size: ${testdir1_size}"
12025         echo "target rename dir size: ${testdir2_size}"
12026
12027         local cmd="do_facet $SINGLEMDS $LCTL "
12028         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12029
12030         eval $cmd || error "$cmd failed"
12031         local samedir=$($cmd | grep 'same_dir')
12032         local same_sample=$(get_rename_size $testdir1_size)
12033         [ -z "$samedir" ] && error "samedir_rename_size count error"
12034         [[ $same_sample -eq 1 ]] ||
12035                 error "samedir_rename_size error $same_sample"
12036         echo "Check same dir rename stats success"
12037
12038         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12039
12040         # check crossdir rename size
12041         mv ${testdir1}/test_0 ${testdir2}/test_0
12042
12043         testdir1_size=$(ls -l $DIR/${tdir} |
12044                 awk '/stats_testdir1/ {print $5}')
12045         testdir2_size=$(ls -l $DIR/${tdir} |
12046                 awk '/stats_testdir2/ {print $5}')
12047
12048         testdir1_size=$(order_2 $testdir1_size)
12049         testdir2_size=$(order_2 $testdir2_size)
12050
12051         testdir1_size=$(size_in_KMGT $testdir1_size)
12052         testdir2_size=$(size_in_KMGT $testdir2_size)
12053
12054         echo "source rename dir size: ${testdir1_size}"
12055         echo "target rename dir size: ${testdir2_size}"
12056
12057         eval $cmd || error "$cmd failed"
12058         local crossdir=$($cmd | grep 'crossdir')
12059         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12060         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12061         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12062         [[ $src_sample -eq 1 ]] ||
12063                 error "crossdir_rename_size error $src_sample"
12064         [[ $tgt_sample -eq 1 ]] ||
12065                 error "crossdir_rename_size error $tgt_sample"
12066         echo "Check cross dir rename stats success"
12067         rm -rf $DIR/${tdir}
12068 }
12069 run_test 133d "Verifying rename_stats ========================================"
12070
12071 test_133e() {
12072         remote_mds_nodsh && skip "remote MDS with nodsh"
12073         remote_ost_nodsh && skip "remote OST with nodsh"
12074         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12075
12076         local testdir=$DIR/${tdir}/stats_testdir
12077         local ctr f0 f1 bs=32768 count=42 sum
12078
12079         mkdir -p ${testdir} || error "mkdir failed"
12080
12081         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
12082
12083         for ctr in {write,read}_bytes; do
12084                 sync
12085                 cancel_lru_locks osc
12086
12087                 do_facet ost1 $LCTL set_param -n \
12088                         "obdfilter.*.exports.clear=clear"
12089
12090                 if [ $ctr = write_bytes ]; then
12091                         f0=/dev/zero
12092                         f1=${testdir}/${tfile}
12093                 else
12094                         f0=${testdir}/${tfile}
12095                         f1=/dev/null
12096                 fi
12097
12098                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12099                         error "dd failed"
12100                 sync
12101                 cancel_lru_locks osc
12102
12103                 sum=$(do_facet ost1 $LCTL get_param \
12104                         "obdfilter.*.exports.*.stats" |
12105                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12106                                 $1 == ctr { sum += $7 }
12107                                 END { printf("%0.0f", sum) }')
12108
12109                 if ((sum != bs * count)); then
12110                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12111                 fi
12112         done
12113
12114         rm -rf $DIR/${tdir}
12115 }
12116 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12117
12118 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12119
12120 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12121 # not honor the -ignore_readdir_race option correctly. So we call
12122 # error_ignore() rather than error() in these cases. See LU-11152.
12123 error_133() {
12124         if (find --version; do_facet mds1 find --version) |
12125                 grep -q '\b4\.5\.1[1-4]\b'; then
12126                 error_ignore LU-11152 "$@"
12127         else
12128                 error "$@"
12129         fi
12130 }
12131
12132 test_133f() {
12133         # First without trusting modes.
12134         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12135         echo "proc_dirs='$proc_dirs'"
12136         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12137         find $proc_dirs -exec cat '{}' \; &> /dev/null
12138
12139         # Second verifying readability.
12140         $LCTL get_param -R '*' &> /dev/null
12141
12142         # Verifing writability with badarea_io.
12143         find $proc_dirs \
12144                 -ignore_readdir_race \
12145                 -type f \
12146                 -not -name force_lbug \
12147                 -not -name changelog_mask \
12148                 -exec badarea_io '{}' \; ||
12149                         error_133 "find $proc_dirs failed"
12150 }
12151 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12152
12153 test_133g() {
12154         remote_mds_nodsh && skip "remote MDS with nodsh"
12155         remote_ost_nodsh && skip "remote OST with nodsh"
12156
12157         # eventually, this can also be replaced with "lctl get_param -R",
12158         # but not until that option is always available on the server
12159         local facet
12160         for facet in mds1 ost1; do
12161                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
12162                         skip_noexit "Too old lustre on $facet"
12163                 local facet_proc_dirs=$(do_facet $facet \
12164                                         \\\ls -d $proc_regexp 2>/dev/null)
12165                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12166                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12167                 do_facet $facet find $facet_proc_dirs \
12168                         ! -name req_history \
12169                         -exec cat '{}' \\\; &> /dev/null
12170
12171                 do_facet $facet find $facet_proc_dirs \
12172                         ! -name req_history \
12173                         -type f \
12174                         -exec cat '{}' \\\; &> /dev/null ||
12175                                 error "proc file read failed"
12176
12177                 do_facet $facet find $facet_proc_dirs \
12178                         -ignore_readdir_race \
12179                         -type f \
12180                         -not -name force_lbug \
12181                         -not -name changelog_mask \
12182                         -exec badarea_io '{}' \\\; ||
12183                                 error_133 "$facet find $facet_proc_dirs failed"
12184         done
12185
12186         # remount the FS in case writes/reads /proc break the FS
12187         cleanup || error "failed to unmount"
12188         setup || error "failed to setup"
12189         true
12190 }
12191 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12192
12193 test_133h() {
12194         remote_mds_nodsh && skip "remote MDS with nodsh"
12195         remote_ost_nodsh && skip "remote OST with nodsh"
12196         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12197                 skip "Need MDS version at least 2.9.54"
12198
12199         local facet
12200
12201         for facet in client mds1 ost1; do
12202                 local facet_proc_dirs=$(do_facet $facet \
12203                                         \\\ls -d $proc_regexp 2> /dev/null)
12204                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12205                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12206                 # Get the list of files that are missing the terminating newline
12207                 local missing=($(do_facet $facet \
12208                         find ${facet_proc_dirs} -type f \|              \
12209                                 while read F\; do                       \
12210                                         awk -v FS='\v' -v RS='\v\v'     \
12211                                         "'END { if(NR>0 &&              \
12212                                         \\\$NF !~ /.*\\\n\$/)           \
12213                                                 print FILENAME}'"       \
12214                                         '\$F'\;                         \
12215                                 done 2>/dev/null))
12216                 [ ${#missing[*]} -eq 0 ] ||
12217                         error "files do not end with newline: ${missing[*]}"
12218         done
12219 }
12220 run_test 133h "Proc files should end with newlines"
12221
12222 test_134a() {
12223         remote_mds_nodsh && skip "remote MDS with nodsh"
12224         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12225                 skip "Need MDS version at least 2.7.54"
12226
12227         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12228         cancel_lru_locks mdc
12229
12230         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12231         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12232         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12233
12234         local nr=1000
12235         createmany -o $DIR/$tdir/f $nr ||
12236                 error "failed to create $nr files in $DIR/$tdir"
12237         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12238
12239         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12240         do_facet mds1 $LCTL set_param fail_loc=0x327
12241         do_facet mds1 $LCTL set_param fail_val=500
12242         touch $DIR/$tdir/m
12243
12244         echo "sleep 10 seconds ..."
12245         sleep 10
12246         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12247
12248         do_facet mds1 $LCTL set_param fail_loc=0
12249         do_facet mds1 $LCTL set_param fail_val=0
12250         [ $lck_cnt -lt $unused ] ||
12251                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12252
12253         rm $DIR/$tdir/m
12254         unlinkmany $DIR/$tdir/f $nr
12255 }
12256 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
12257
12258 test_134b() {
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 low_wm=$(do_facet mds1 $LCTL get_param -n \
12267                         ldlm.lock_reclaim_threshold_mb)
12268         # disable reclaim temporarily
12269         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
12270
12271         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
12272         do_facet mds1 $LCTL set_param fail_loc=0x328
12273         do_facet mds1 $LCTL set_param fail_val=500
12274
12275         $LCTL set_param debug=+trace
12276
12277         local nr=600
12278         createmany -o $DIR/$tdir/f $nr &
12279         local create_pid=$!
12280
12281         echo "Sleep $TIMEOUT seconds ..."
12282         sleep $TIMEOUT
12283         if ! ps -p $create_pid  > /dev/null 2>&1; then
12284                 do_facet mds1 $LCTL set_param fail_loc=0
12285                 do_facet mds1 $LCTL set_param fail_val=0
12286                 do_facet mds1 $LCTL set_param \
12287                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
12288                 error "createmany finished incorrectly!"
12289         fi
12290         do_facet mds1 $LCTL set_param fail_loc=0
12291         do_facet mds1 $LCTL set_param fail_val=0
12292         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
12293         wait $create_pid || return 1
12294
12295         unlinkmany $DIR/$tdir/f $nr
12296 }
12297 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
12298
12299 test_140() { #bug-17379
12300         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12301
12302         test_mkdir $DIR/$tdir
12303         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
12304         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
12305
12306         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
12307         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
12308         local i=0
12309         while i=$((i + 1)); do
12310                 test_mkdir $i
12311                 cd $i || error "Changing to $i"
12312                 ln -s ../stat stat || error "Creating stat symlink"
12313                 # Read the symlink until ELOOP present,
12314                 # not LBUGing the system is considered success,
12315                 # we didn't overrun the stack.
12316                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
12317                 if [ $ret -ne 0 ]; then
12318                         if [ $ret -eq 40 ]; then
12319                                 break  # -ELOOP
12320                         else
12321                                 error "Open stat symlink"
12322                                         return
12323                         fi
12324                 fi
12325         done
12326         i=$((i - 1))
12327         echo "The symlink depth = $i"
12328         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
12329                                         error "Invalid symlink depth"
12330
12331         # Test recursive symlink
12332         ln -s symlink_self symlink_self
12333         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
12334         echo "open symlink_self returns $ret"
12335         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
12336 }
12337 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
12338
12339 test_150() {
12340         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12341
12342         local TF="$TMP/$tfile"
12343
12344         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12345         cp $TF $DIR/$tfile
12346         cancel_lru_locks $OSC
12347         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12348         remount_client $MOUNT
12349         df -P $MOUNT
12350         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12351
12352         $TRUNCATE $TF 6000
12353         $TRUNCATE $DIR/$tfile 6000
12354         cancel_lru_locks $OSC
12355         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12356
12357         echo "12345" >>$TF
12358         echo "12345" >>$DIR/$tfile
12359         cancel_lru_locks $OSC
12360         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12361
12362         echo "12345" >>$TF
12363         echo "12345" >>$DIR/$tfile
12364         cancel_lru_locks $OSC
12365         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12366
12367         rm -f $TF
12368         true
12369 }
12370 run_test 150 "truncate/append tests"
12371
12372 #LU-2902 roc_hit was not able to read all values from lproc
12373 function roc_hit_init() {
12374         local list=$(comma_list $(osts_nodes))
12375         local dir=$DIR/$tdir-check
12376         local file=$dir/$tfile
12377         local BEFORE
12378         local AFTER
12379         local idx
12380
12381         test_mkdir $dir
12382         #use setstripe to do a write to every ost
12383         for i in $(seq 0 $((OSTCOUNT-1))); do
12384                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
12385                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12386                 idx=$(printf %04x $i)
12387                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12388                         awk '$1 == "cache_access" {sum += $7}
12389                                 END { printf("%0.0f", sum) }')
12390
12391                 cancel_lru_locks osc
12392                 cat $file >/dev/null
12393
12394                 AFTER=$(get_osd_param $list *OST*$idx stats |
12395                         awk '$1 == "cache_access" {sum += $7}
12396                                 END { printf("%0.0f", sum) }')
12397
12398                 echo BEFORE:$BEFORE AFTER:$AFTER
12399                 if ! let "AFTER - BEFORE == 4"; then
12400                         rm -rf $dir
12401                         error "roc_hit is not safe to use"
12402                 fi
12403                 rm $file
12404         done
12405
12406         rm -rf $dir
12407 }
12408
12409 function roc_hit() {
12410         local list=$(comma_list $(osts_nodes))
12411         echo $(get_osd_param $list '' stats |
12412                 awk '$1 == "cache_hit" {sum += $7}
12413                         END { printf("%0.0f", sum) }')
12414 }
12415
12416 function set_cache() {
12417         local on=1
12418
12419         if [ "$2" == "off" ]; then
12420                 on=0;
12421         fi
12422         local list=$(comma_list $(osts_nodes))
12423         set_osd_param $list '' $1_cache_enable $on
12424
12425         cancel_lru_locks osc
12426 }
12427
12428 test_151() {
12429         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12430         remote_ost_nodsh && skip "remote OST with nodsh"
12431
12432         local CPAGES=3
12433         local list=$(comma_list $(osts_nodes))
12434
12435         # check whether obdfilter is cache capable at all
12436         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12437                 skip "not cache-capable obdfilter"
12438         fi
12439
12440         # check cache is enabled on all obdfilters
12441         if get_osd_param $list '' read_cache_enable | grep 0; then
12442                 skip "oss cache is disabled"
12443         fi
12444
12445         set_osd_param $list '' writethrough_cache_enable 1
12446
12447         # check write cache is enabled on all obdfilters
12448         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12449                 skip "oss write cache is NOT enabled"
12450         fi
12451
12452         roc_hit_init
12453
12454         #define OBD_FAIL_OBD_NO_LRU  0x609
12455         do_nodes $list $LCTL set_param fail_loc=0x609
12456
12457         # pages should be in the case right after write
12458         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12459                 error "dd failed"
12460
12461         local BEFORE=$(roc_hit)
12462         cancel_lru_locks osc
12463         cat $DIR/$tfile >/dev/null
12464         local AFTER=$(roc_hit)
12465
12466         do_nodes $list $LCTL set_param fail_loc=0
12467
12468         if ! let "AFTER - BEFORE == CPAGES"; then
12469                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12470         fi
12471
12472         # the following read invalidates the cache
12473         cancel_lru_locks osc
12474         set_osd_param $list '' read_cache_enable 0
12475         cat $DIR/$tfile >/dev/null
12476
12477         # now data shouldn't be found in the cache
12478         BEFORE=$(roc_hit)
12479         cancel_lru_locks osc
12480         cat $DIR/$tfile >/dev/null
12481         AFTER=$(roc_hit)
12482         if let "AFTER - BEFORE != 0"; then
12483                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12484         fi
12485
12486         set_osd_param $list '' read_cache_enable 1
12487         rm -f $DIR/$tfile
12488 }
12489 run_test 151 "test cache on oss and controls ==============================="
12490
12491 test_152() {
12492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12493
12494         local TF="$TMP/$tfile"
12495
12496         # simulate ENOMEM during write
12497 #define OBD_FAIL_OST_NOMEM      0x226
12498         lctl set_param fail_loc=0x80000226
12499         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12500         cp $TF $DIR/$tfile
12501         sync || error "sync failed"
12502         lctl set_param fail_loc=0
12503
12504         # discard client's cache
12505         cancel_lru_locks osc
12506
12507         # simulate ENOMEM during read
12508         lctl set_param fail_loc=0x80000226
12509         cmp $TF $DIR/$tfile || error "cmp failed"
12510         lctl set_param fail_loc=0
12511
12512         rm -f $TF
12513 }
12514 run_test 152 "test read/write with enomem ============================"
12515
12516 test_153() {
12517         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12518 }
12519 run_test 153 "test if fdatasync does not crash ======================="
12520
12521 dot_lustre_fid_permission_check() {
12522         local fid=$1
12523         local ffid=$MOUNT/.lustre/fid/$fid
12524         local test_dir=$2
12525
12526         echo "stat fid $fid"
12527         stat $ffid > /dev/null || error "stat $ffid failed."
12528         echo "touch fid $fid"
12529         touch $ffid || error "touch $ffid failed."
12530         echo "write to fid $fid"
12531         cat /etc/hosts > $ffid || error "write $ffid failed."
12532         echo "read fid $fid"
12533         diff /etc/hosts $ffid || error "read $ffid failed."
12534         echo "append write to fid $fid"
12535         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12536         echo "rename fid $fid"
12537         mv $ffid $test_dir/$tfile.1 &&
12538                 error "rename $ffid to $tfile.1 should fail."
12539         touch $test_dir/$tfile.1
12540         mv $test_dir/$tfile.1 $ffid &&
12541                 error "rename $tfile.1 to $ffid should fail."
12542         rm -f $test_dir/$tfile.1
12543         echo "truncate fid $fid"
12544         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12545         echo "link fid $fid"
12546         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12547         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12548                 echo "setfacl fid $fid"
12549                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12550                 echo "getfacl fid $fid"
12551                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12552         fi
12553         echo "unlink fid $fid"
12554         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12555         echo "mknod fid $fid"
12556         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12557
12558         fid=[0xf00000400:0x1:0x0]
12559         ffid=$MOUNT/.lustre/fid/$fid
12560
12561         echo "stat non-exist fid $fid"
12562         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12563         echo "write to non-exist fid $fid"
12564         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12565         echo "link new fid $fid"
12566         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12567
12568         mkdir -p $test_dir/$tdir
12569         touch $test_dir/$tdir/$tfile
12570         fid=$($LFS path2fid $test_dir/$tdir)
12571         rc=$?
12572         [ $rc -ne 0 ] &&
12573                 error "error: could not get fid for $test_dir/$dir/$tfile."
12574
12575         ffid=$MOUNT/.lustre/fid/$fid
12576
12577         echo "ls $fid"
12578         ls $ffid > /dev/null || error "ls $ffid failed."
12579         echo "touch $fid/$tfile.1"
12580         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12581
12582         echo "touch $MOUNT/.lustre/fid/$tfile"
12583         touch $MOUNT/.lustre/fid/$tfile && \
12584                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12585
12586         echo "setxattr to $MOUNT/.lustre/fid"
12587         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12588
12589         echo "listxattr for $MOUNT/.lustre/fid"
12590         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12591
12592         echo "delxattr from $MOUNT/.lustre/fid"
12593         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12594
12595         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12596         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12597                 error "touch invalid fid should fail."
12598
12599         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12600         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12601                 error "touch non-normal fid should fail."
12602
12603         echo "rename $tdir to $MOUNT/.lustre/fid"
12604         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12605                 error "rename to $MOUNT/.lustre/fid should fail."
12606
12607         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
12608         then            # LU-3547
12609                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
12610                 local new_obf_mode=777
12611
12612                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
12613                 chmod $new_obf_mode $DIR/.lustre/fid ||
12614                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
12615
12616                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
12617                 [ $obf_mode -eq $new_obf_mode ] ||
12618                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12619
12620                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12621                 chmod $old_obf_mode $DIR/.lustre/fid ||
12622                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12623         fi
12624
12625         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12626         fid=$($LFS path2fid $test_dir/$tfile-2)
12627
12628         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12629         then # LU-5424
12630                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12631                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12632                         error "create lov data thru .lustre failed"
12633         fi
12634         echo "cp /etc/passwd $test_dir/$tfile-2"
12635         cp /etc/passwd $test_dir/$tfile-2 ||
12636                 error "copy to $test_dir/$tfile-2 failed."
12637         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12638         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12639                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12640
12641         rm -rf $test_dir/tfile.lnk
12642         rm -rf $test_dir/$tfile-2
12643 }
12644
12645 test_154A() {
12646         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12647                 skip "Need MDS version at least 2.4.1"
12648
12649         local tf=$DIR/$tfile
12650         touch $tf
12651
12652         local fid=$($LFS path2fid $tf)
12653         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12654
12655         # check that we get the same pathname back
12656         local found=$($LFS fid2path $MOUNT "$fid")
12657         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12658         [ "$found" == "$tf" ] ||
12659                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12660 }
12661 run_test 154A "lfs path2fid and fid2path basic checks"
12662
12663 test_154B() {
12664         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12665                 skip "Need MDS version at least 2.4.1"
12666
12667         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12668         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12669         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12670         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12671
12672         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12673         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12674
12675         # check that we get the same pathname
12676         echo "PFID: $PFID, name: $name"
12677         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12678         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12679         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12680                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12681
12682         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12683 }
12684 run_test 154B "verify the ll_decode_linkea tool"
12685
12686 test_154a() {
12687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12688         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12689         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12690                 skip "Need MDS version at least 2.2.51"
12691         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12692
12693         cp /etc/hosts $DIR/$tfile
12694
12695         fid=$($LFS path2fid $DIR/$tfile)
12696         rc=$?
12697         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12698
12699         dot_lustre_fid_permission_check "$fid" $DIR ||
12700                 error "dot lustre permission check $fid failed"
12701
12702         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
12703
12704         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
12705
12706         touch $MOUNT/.lustre/file &&
12707                 error "creation is not allowed under .lustre"
12708
12709         mkdir $MOUNT/.lustre/dir &&
12710                 error "mkdir is not allowed under .lustre"
12711
12712         rm -rf $DIR/$tfile
12713 }
12714 run_test 154a "Open-by-FID"
12715
12716 test_154b() {
12717         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12718         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12719         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12720         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12721                 skip "Need MDS version at least 2.2.51"
12722
12723         local remote_dir=$DIR/$tdir/remote_dir
12724         local MDTIDX=1
12725         local rc=0
12726
12727         mkdir -p $DIR/$tdir
12728         $LFS mkdir -i $MDTIDX $remote_dir ||
12729                 error "create remote directory failed"
12730
12731         cp /etc/hosts $remote_dir/$tfile
12732
12733         fid=$($LFS path2fid $remote_dir/$tfile)
12734         rc=$?
12735         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
12736
12737         dot_lustre_fid_permission_check "$fid" $remote_dir ||
12738                 error "dot lustre permission check $fid failed"
12739         rm -rf $DIR/$tdir
12740 }
12741 run_test 154b "Open-by-FID for remote directory"
12742
12743 test_154c() {
12744         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12745                 skip "Need MDS version at least 2.4.1"
12746
12747         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
12748         local FID1=$($LFS path2fid $DIR/$tfile.1)
12749         local FID2=$($LFS path2fid $DIR/$tfile.2)
12750         local FID3=$($LFS path2fid $DIR/$tfile.3)
12751
12752         local N=1
12753         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
12754                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
12755                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
12756                 local want=FID$N
12757                 [ "$FID" = "${!want}" ] ||
12758                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
12759                 N=$((N + 1))
12760         done
12761
12762         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
12763         do
12764                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
12765                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
12766                 N=$((N + 1))
12767         done
12768 }
12769 run_test 154c "lfs path2fid and fid2path multiple arguments"
12770
12771 test_154d() {
12772         remote_mds_nodsh && skip "remote MDS with nodsh"
12773         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
12774                 skip "Need MDS version at least 2.5.53"
12775
12776         if remote_mds; then
12777                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
12778         else
12779                 nid="0@lo"
12780         fi
12781         local proc_ofile="mdt.*.exports.'$nid'.open_files"
12782         local fd
12783         local cmd
12784
12785         rm -f $DIR/$tfile
12786         touch $DIR/$tfile
12787
12788         local fid=$($LFS path2fid $DIR/$tfile)
12789         # Open the file
12790         fd=$(free_fd)
12791         cmd="exec $fd<$DIR/$tfile"
12792         eval $cmd
12793         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
12794         echo "$fid_list" | grep "$fid"
12795         rc=$?
12796
12797         cmd="exec $fd>/dev/null"
12798         eval $cmd
12799         if [ $rc -ne 0 ]; then
12800                 error "FID $fid not found in open files list $fid_list"
12801         fi
12802 }
12803 run_test 154d "Verify open file fid"
12804
12805 test_154e()
12806 {
12807         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
12808                 skip "Need MDS version at least 2.6.50"
12809
12810         if ls -a $MOUNT | grep -q '^\.lustre$'; then
12811                 error ".lustre returned by readdir"
12812         fi
12813 }
12814 run_test 154e ".lustre is not returned by readdir"
12815
12816 test_154f() {
12817         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12818
12819         # create parent directory on a single MDT to avoid cross-MDT hardlinks
12820         test_mkdir -p -c1 $DIR/$tdir/d
12821         # test dirs inherit from its stripe
12822         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
12823         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
12824         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
12825         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
12826         touch $DIR/f
12827
12828         # get fid of parents
12829         local FID0=$($LFS path2fid $DIR/$tdir/d)
12830         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
12831         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
12832         local FID3=$($LFS path2fid $DIR)
12833
12834         # check that path2fid --parents returns expected <parent_fid>/name
12835         # 1) test for a directory (single parent)
12836         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
12837         [ "$parent" == "$FID0/foo1" ] ||
12838                 error "expected parent: $FID0/foo1, got: $parent"
12839
12840         # 2) test for a file with nlink > 1 (multiple parents)
12841         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
12842         echo "$parent" | grep -F "$FID1/$tfile" ||
12843                 error "$FID1/$tfile not returned in parent list"
12844         echo "$parent" | grep -F "$FID2/link" ||
12845                 error "$FID2/link not returned in parent list"
12846
12847         # 3) get parent by fid
12848         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
12849         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12850         echo "$parent" | grep -F "$FID1/$tfile" ||
12851                 error "$FID1/$tfile not returned in parent list (by fid)"
12852         echo "$parent" | grep -F "$FID2/link" ||
12853                 error "$FID2/link not returned in parent list (by fid)"
12854
12855         # 4) test for entry in root directory
12856         parent=$($LFS path2fid --parents $DIR/f)
12857         echo "$parent" | grep -F "$FID3/f" ||
12858                 error "$FID3/f not returned in parent list"
12859
12860         # 5) test it on root directory
12861         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
12862                 error "$MOUNT should not have parents"
12863
12864         # enable xattr caching and check that linkea is correctly updated
12865         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12866         save_lustre_params client "llite.*.xattr_cache" > $save
12867         lctl set_param llite.*.xattr_cache 1
12868
12869         # 6.1) linkea update on rename
12870         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
12871
12872         # get parents by fid
12873         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12874         # foo1 should no longer be returned in parent list
12875         echo "$parent" | grep -F "$FID1" &&
12876                 error "$FID1 should no longer be in parent list"
12877         # the new path should appear
12878         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
12879                 error "$FID2/$tfile.moved is not in parent list"
12880
12881         # 6.2) linkea update on unlink
12882         rm -f $DIR/$tdir/d/foo2/link
12883         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12884         # foo2/link should no longer be returned in parent list
12885         echo "$parent" | grep -F "$FID2/link" &&
12886                 error "$FID2/link should no longer be in parent list"
12887         true
12888
12889         rm -f $DIR/f
12890         restore_lustre_params < $save
12891         rm -f $save
12892 }
12893 run_test 154f "get parent fids by reading link ea"
12894
12895 test_154g()
12896 {
12897         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12898         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
12899            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
12900                 skip "Need MDS version at least 2.6.92"
12901
12902         mkdir -p $DIR/$tdir
12903         llapi_fid_test -d $DIR/$tdir
12904 }
12905 run_test 154g "various llapi FID tests"
12906
12907 test_155_small_load() {
12908     local temp=$TMP/$tfile
12909     local file=$DIR/$tfile
12910
12911     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
12912         error "dd of=$temp bs=6096 count=1 failed"
12913     cp $temp $file
12914     cancel_lru_locks $OSC
12915     cmp $temp $file || error "$temp $file differ"
12916
12917     $TRUNCATE $temp 6000
12918     $TRUNCATE $file 6000
12919     cmp $temp $file || error "$temp $file differ (truncate1)"
12920
12921     echo "12345" >>$temp
12922     echo "12345" >>$file
12923     cmp $temp $file || error "$temp $file differ (append1)"
12924
12925     echo "12345" >>$temp
12926     echo "12345" >>$file
12927     cmp $temp $file || error "$temp $file differ (append2)"
12928
12929     rm -f $temp $file
12930     true
12931 }
12932
12933 test_155_big_load() {
12934         remote_ost_nodsh && skip "remote OST with nodsh"
12935
12936         local temp=$TMP/$tfile
12937         local file=$DIR/$tfile
12938
12939         free_min_max
12940         local cache_size=$(do_facet ost$((MAXI+1)) \
12941                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
12942         local large_file_size=$((cache_size * 2))
12943
12944         echo "OSS cache size: $cache_size KB"
12945         echo "Large file size: $large_file_size KB"
12946
12947         [ $MAXV -le $large_file_size ] &&
12948                 skip_env "max available OST size needs > $large_file_size KB"
12949
12950         $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
12951
12952         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
12953                 error "dd of=$temp bs=$large_file_size count=1k failed"
12954         cp $temp $file
12955         ls -lh $temp $file
12956         cancel_lru_locks osc
12957         cmp $temp $file || error "$temp $file differ"
12958
12959         rm -f $temp $file
12960         true
12961 }
12962
12963 save_writethrough() {
12964         local facets=$(get_facets OST)
12965
12966         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
12967 }
12968
12969 test_155a() {
12970         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12971
12972         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12973
12974         save_writethrough $p
12975
12976         set_cache read on
12977         set_cache writethrough on
12978         test_155_small_load
12979         restore_lustre_params < $p
12980         rm -f $p
12981 }
12982 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
12983
12984 test_155b() {
12985         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12986
12987         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12988
12989         save_writethrough $p
12990
12991         set_cache read on
12992         set_cache writethrough off
12993         test_155_small_load
12994         restore_lustre_params < $p
12995         rm -f $p
12996 }
12997 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
12998
12999 test_155c() {
13000         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13001
13002         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13003
13004         save_writethrough $p
13005
13006         set_cache read off
13007         set_cache writethrough on
13008         test_155_small_load
13009         restore_lustre_params < $p
13010         rm -f $p
13011 }
13012 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13013
13014 test_155d() {
13015         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13016
13017         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13018
13019         save_writethrough $p
13020
13021         set_cache read off
13022         set_cache writethrough off
13023         test_155_small_load
13024         restore_lustre_params < $p
13025         rm -f $p
13026 }
13027 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13028
13029 test_155e() {
13030         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13031
13032         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13033
13034         save_writethrough $p
13035
13036         set_cache read on
13037         set_cache writethrough on
13038         test_155_big_load
13039         restore_lustre_params < $p
13040         rm -f $p
13041 }
13042 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13043
13044 test_155f() {
13045         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13046
13047         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13048
13049         save_writethrough $p
13050
13051         set_cache read on
13052         set_cache writethrough off
13053         test_155_big_load
13054         restore_lustre_params < $p
13055         rm -f $p
13056 }
13057 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13058
13059 test_155g() {
13060         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13061
13062         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13063
13064         save_writethrough $p
13065
13066         set_cache read off
13067         set_cache writethrough on
13068         test_155_big_load
13069         restore_lustre_params < $p
13070         rm -f $p
13071 }
13072 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13073
13074 test_155h() {
13075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13076
13077         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13078
13079         save_writethrough $p
13080
13081         set_cache read off
13082         set_cache writethrough off
13083         test_155_big_load
13084         restore_lustre_params < $p
13085         rm -f $p
13086 }
13087 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13088
13089 test_156() {
13090         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13091         remote_ost_nodsh && skip "remote OST with nodsh"
13092         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13093                 skip "stats not implemented on old servers"
13094         [ "$ost1_FSTYPE" = "zfs" ] &&
13095                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13096
13097         local CPAGES=3
13098         local BEFORE
13099         local AFTER
13100         local file="$DIR/$tfile"
13101         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13102
13103         save_writethrough $p
13104         roc_hit_init
13105
13106         log "Turn on read and write cache"
13107         set_cache read on
13108         set_cache writethrough on
13109
13110         log "Write data and read it back."
13111         log "Read should be satisfied from the cache."
13112         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13113         BEFORE=$(roc_hit)
13114         cancel_lru_locks osc
13115         cat $file >/dev/null
13116         AFTER=$(roc_hit)
13117         if ! let "AFTER - BEFORE == CPAGES"; then
13118                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13119         else
13120                 log "cache hits:: before: $BEFORE, after: $AFTER"
13121         fi
13122
13123         log "Read again; it should be satisfied from the cache."
13124         BEFORE=$AFTER
13125         cancel_lru_locks osc
13126         cat $file >/dev/null
13127         AFTER=$(roc_hit)
13128         if ! let "AFTER - BEFORE == CPAGES"; then
13129                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13130         else
13131                 log "cache hits:: before: $BEFORE, after: $AFTER"
13132         fi
13133
13134         log "Turn off the read cache and turn on the write cache"
13135         set_cache read off
13136         set_cache writethrough on
13137
13138         log "Read again; it should be satisfied from the cache."
13139         BEFORE=$(roc_hit)
13140         cancel_lru_locks osc
13141         cat $file >/dev/null
13142         AFTER=$(roc_hit)
13143         if ! let "AFTER - BEFORE == CPAGES"; then
13144                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13145         else
13146                 log "cache hits:: before: $BEFORE, after: $AFTER"
13147         fi
13148
13149         log "Read again; it should not be satisfied from the cache."
13150         BEFORE=$AFTER
13151         cancel_lru_locks osc
13152         cat $file >/dev/null
13153         AFTER=$(roc_hit)
13154         if ! let "AFTER - BEFORE == 0"; then
13155                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13156         else
13157                 log "cache hits:: before: $BEFORE, after: $AFTER"
13158         fi
13159
13160         log "Write data and read it back."
13161         log "Read should be satisfied from the cache."
13162         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13163         BEFORE=$(roc_hit)
13164         cancel_lru_locks osc
13165         cat $file >/dev/null
13166         AFTER=$(roc_hit)
13167         if ! let "AFTER - BEFORE == CPAGES"; then
13168                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13169         else
13170                 log "cache hits:: before: $BEFORE, after: $AFTER"
13171         fi
13172
13173         log "Read again; it should not be satisfied from the cache."
13174         BEFORE=$AFTER
13175         cancel_lru_locks osc
13176         cat $file >/dev/null
13177         AFTER=$(roc_hit)
13178         if ! let "AFTER - BEFORE == 0"; then
13179                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13180         else
13181                 log "cache hits:: before: $BEFORE, after: $AFTER"
13182         fi
13183
13184         log "Turn off read and write cache"
13185         set_cache read off
13186         set_cache writethrough off
13187
13188         log "Write data and read it back"
13189         log "It should not be satisfied from the cache."
13190         rm -f $file
13191         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13192         cancel_lru_locks osc
13193         BEFORE=$(roc_hit)
13194         cat $file >/dev/null
13195         AFTER=$(roc_hit)
13196         if ! let "AFTER - BEFORE == 0"; then
13197                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13198         else
13199                 log "cache hits:: before: $BEFORE, after: $AFTER"
13200         fi
13201
13202         log "Turn on the read cache and turn off the write cache"
13203         set_cache read on
13204         set_cache writethrough off
13205
13206         log "Write data and read it back"
13207         log "It should not be satisfied from the cache."
13208         rm -f $file
13209         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13210         BEFORE=$(roc_hit)
13211         cancel_lru_locks osc
13212         cat $file >/dev/null
13213         AFTER=$(roc_hit)
13214         if ! let "AFTER - BEFORE == 0"; then
13215                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
13216         else
13217                 log "cache hits:: before: $BEFORE, after: $AFTER"
13218         fi
13219
13220         log "Read again; it should be satisfied from the cache."
13221         BEFORE=$(roc_hit)
13222         cancel_lru_locks osc
13223         cat $file >/dev/null
13224         AFTER=$(roc_hit)
13225         if ! let "AFTER - BEFORE == CPAGES"; then
13226                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13227         else
13228                 log "cache hits:: before: $BEFORE, after: $AFTER"
13229         fi
13230
13231         restore_lustre_params < $p
13232         rm -f $p $file
13233 }
13234 run_test 156 "Verification of tunables"
13235
13236 test_160a() {
13237         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13238         remote_mds_nodsh && skip "remote MDS with nodsh"
13239         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13240                 skip "Need MDS version at least 2.2.0"
13241
13242         changelog_register || error "changelog_register failed"
13243         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13244         changelog_users $SINGLEMDS | grep -q $cl_user ||
13245                 error "User $cl_user not found in changelog_users"
13246
13247         # change something
13248         test_mkdir -p $DIR/$tdir/pics/2008/zachy
13249         changelog_clear 0 || error "changelog_clear failed"
13250         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
13251         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
13252         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
13253         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
13254         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
13255         rm $DIR/$tdir/pics/desktop.jpg
13256
13257         changelog_dump | tail -10
13258
13259         echo "verifying changelog mask"
13260         changelog_chmask "-MKDIR"
13261         changelog_chmask "-CLOSE"
13262
13263         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
13264         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
13265
13266         changelog_chmask "+MKDIR"
13267         changelog_chmask "+CLOSE"
13268
13269         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
13270         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
13271
13272         changelog_dump | tail -10
13273         MKDIRS=$(changelog_dump | grep -c "MKDIR")
13274         CLOSES=$(changelog_dump | grep -c "CLOSE")
13275         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
13276         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
13277
13278         # verify contents
13279         echo "verifying target fid"
13280         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
13281         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
13282         [ "$fidc" == "$fidf" ] ||
13283                 error "changelog '$tfile' fid $fidc != file fid $fidf"
13284         echo "verifying parent fid"
13285         # The FID returned from the Changelog may be the directory shard on
13286         # a different MDT, and not the FID returned by path2fid on the parent.
13287         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
13288         # since this is what will matter when recreating this file in the tree.
13289         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
13290         local pathp=$($LFS fid2path $MOUNT "$fidp")
13291         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
13292                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
13293
13294         echo "getting records for $cl_user"
13295         changelog_users $SINGLEMDS
13296         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
13297         local nclr=3
13298         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
13299                 error "changelog_clear failed"
13300         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
13301         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
13302         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
13303                 error "user index expect $user_rec1 + $nclr != $user_rec2"
13304
13305         local min0_rec=$(changelog_users $SINGLEMDS |
13306                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
13307         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
13308                           awk '{ print $1; exit; }')
13309
13310         changelog_dump | tail -n 5
13311         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
13312         [ $first_rec == $((min0_rec + 1)) ] ||
13313                 error "first index should be $min0_rec + 1 not $first_rec"
13314
13315         # LU-3446 changelog index reset on MDT restart
13316         local cur_rec1=$(changelog_users $SINGLEMDS |
13317                          awk '/^current.index:/ { print $NF }')
13318         changelog_clear 0 ||
13319                 error "clear all changelog records for $cl_user failed"
13320         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
13321         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
13322                 error "Fail to start $SINGLEMDS"
13323         local cur_rec2=$(changelog_users $SINGLEMDS |
13324                          awk '/^current.index:/ { print $NF }')
13325         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
13326         [ $cur_rec1 == $cur_rec2 ] ||
13327                 error "current index should be $cur_rec1 not $cur_rec2"
13328
13329         echo "verifying users from this test are deregistered"
13330         changelog_deregister || error "changelog_deregister failed"
13331         changelog_users $SINGLEMDS | grep -q $cl_user &&
13332                 error "User '$cl_user' still in changelog_users"
13333
13334         # lctl get_param -n mdd.*.changelog_users
13335         # current index: 144
13336         # ID    index (idle seconds)
13337         # cl3   144 (2)
13338         if ! changelog_users $SINGLEMDS | grep "^cl"; then
13339                 # this is the normal case where all users were deregistered
13340                 # make sure no new records are added when no users are present
13341                 local last_rec1=$(changelog_users $SINGLEMDS |
13342                                   awk '/^current.index:/ { print $NF }')
13343                 touch $DIR/$tdir/chloe
13344                 local last_rec2=$(changelog_users $SINGLEMDS |
13345                                   awk '/^current.index:/ { print $NF }')
13346                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13347                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13348         else
13349                 # any changelog users must be leftovers from a previous test
13350                 changelog_users $SINGLEMDS
13351                 echo "other changelog users; can't verify off"
13352         fi
13353 }
13354 run_test 160a "changelog sanity"
13355
13356 test_160b() { # LU-3587
13357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13358         remote_mds_nodsh && skip "remote MDS with nodsh"
13359         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13360                 skip "Need MDS version at least 2.2.0"
13361
13362         changelog_register || error "changelog_register failed"
13363         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13364         changelog_users $SINGLEMDS | grep -q $cl_user ||
13365                 error "User '$cl_user' not found in changelog_users"
13366
13367         local longname1=$(str_repeat a 255)
13368         local longname2=$(str_repeat b 255)
13369
13370         cd $DIR
13371         echo "creating very long named file"
13372         touch $longname1 || error "create of '$longname1' failed"
13373         echo "renaming very long named file"
13374         mv $longname1 $longname2
13375
13376         changelog_dump | grep RENME | tail -n 5
13377         rm -f $longname2
13378 }
13379 run_test 160b "Verify that very long rename doesn't crash in changelog"
13380
13381 test_160c() {
13382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13383         remote_mds_nodsh && skip "remote MDS with nodsh"
13384
13385         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13386                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13387                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13388                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13389
13390         local rc=0
13391
13392         # Registration step
13393         changelog_register || error "changelog_register failed"
13394
13395         rm -rf $DIR/$tdir
13396         mkdir -p $DIR/$tdir
13397         $MCREATE $DIR/$tdir/foo_160c
13398         changelog_chmask "-TRUNC"
13399         $TRUNCATE $DIR/$tdir/foo_160c 200
13400         changelog_chmask "+TRUNC"
13401         $TRUNCATE $DIR/$tdir/foo_160c 199
13402         changelog_dump | tail -n 5
13403         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13404         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13405 }
13406 run_test 160c "verify that changelog log catch the truncate event"
13407
13408 test_160d() {
13409         remote_mds_nodsh && skip "remote MDS with nodsh"
13410         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13411         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13412         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13413                 skip "Need MDS version at least 2.7.60"
13414
13415         # Registration step
13416         changelog_register || error "changelog_register failed"
13417
13418         mkdir -p $DIR/$tdir/migrate_dir
13419         changelog_clear 0 || error "changelog_clear failed"
13420
13421         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13422         changelog_dump | tail -n 5
13423         local migrates=$(changelog_dump | grep -c "MIGRT")
13424         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13425 }
13426 run_test 160d "verify that changelog log catch the migrate event"
13427
13428 test_160e() {
13429         remote_mds_nodsh && skip "remote MDS with nodsh"
13430
13431         # Create a user
13432         changelog_register || error "changelog_register failed"
13433
13434         # Delete a future user (expect fail)
13435         local MDT0=$(facet_svc $SINGLEMDS)
13436         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13437         local rc=$?
13438
13439         if [ $rc -eq 0 ]; then
13440                 error "Deleted non-existant user cl77"
13441         elif [ $rc -ne 2 ]; then
13442                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13443         fi
13444
13445         # Clear to a bad index (1 billion should be safe)
13446         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13447         rc=$?
13448
13449         if [ $rc -eq 0 ]; then
13450                 error "Successfully cleared to invalid CL index"
13451         elif [ $rc -ne 22 ]; then
13452                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13453         fi
13454 }
13455 run_test 160e "changelog negative testing (should return errors)"
13456
13457 test_160f() {
13458         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13459         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13460                 skip "Need MDS version at least 2.10.56"
13461
13462         local mdts=$(comma_list $(mdts_nodes))
13463
13464         # Create a user
13465         changelog_register || error "first changelog_register failed"
13466         changelog_register || error "second changelog_register failed"
13467         local cl_users
13468         declare -A cl_user1
13469         declare -A cl_user2
13470         local user_rec1
13471         local user_rec2
13472         local i
13473
13474         # generate some changelog records to accumulate on each MDT
13475         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13476         log "$(date +%s): creating first files"
13477         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13478                 error "create $DIR/$tdir/$tfile failed"
13479
13480         # check changelogs have been generated
13481         local start=$SECONDS
13482         local idle_time=$((MDSCOUNT * 5 + 5))
13483         local nbcl=$(changelog_dump | wc -l)
13484         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13485
13486         for param in "changelog_max_idle_time=$idle_time" \
13487                      "changelog_gc=1" \
13488                      "changelog_min_gc_interval=2" \
13489                      "changelog_min_free_cat_entries=3"; do
13490                 local MDT0=$(facet_svc $SINGLEMDS)
13491                 local var="${param%=*}"
13492                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13493
13494                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13495                 do_nodes $mdts $LCTL set_param mdd.*.$param
13496         done
13497
13498         # force cl_user2 to be idle (1st part), but also cancel the
13499         # cl_user1 records so that it is not evicted later in the test.
13500         local sleep1=$((idle_time / 2))
13501         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
13502         sleep $sleep1
13503
13504         # simulate changelog catalog almost full
13505         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13506         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13507
13508         for i in $(seq $MDSCOUNT); do
13509                 cl_users=(${CL_USERS[mds$i]})
13510                 cl_user1[mds$i]="${cl_users[0]}"
13511                 cl_user2[mds$i]="${cl_users[1]}"
13512
13513                 [ -n "${cl_user1[mds$i]}" ] ||
13514                         error "mds$i: no user registered"
13515                 [ -n "${cl_user2[mds$i]}" ] ||
13516                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13517
13518                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13519                 [ -n "$user_rec1" ] ||
13520                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13521                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13522                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13523                 [ -n "$user_rec2" ] ||
13524                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13525                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13526                      "$user_rec1 + 2 == $user_rec2"
13527                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13528                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13529                               "$user_rec1 + 2, but is $user_rec2"
13530                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13531                 [ -n "$user_rec2" ] ||
13532                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13533                 [ $user_rec1 == $user_rec2 ] ||
13534                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13535                               "$user_rec1, but is $user_rec2"
13536         done
13537
13538         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
13539         local sleep2=$((idle_time - (SECONDS - start) + 1))
13540         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
13541         sleep $sleep2
13542
13543         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
13544         # cl_user1 should be OK because it recently processed records.
13545         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
13546         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
13547                 error "create $DIR/$tdir/${tfile}b failed"
13548
13549         # ensure gc thread is done
13550         for i in $(mdts_nodes); do
13551                 wait_update $i \
13552                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13553                         error "$i: GC-thread not done"
13554         done
13555
13556         local first_rec
13557         for i in $(seq $MDSCOUNT); do
13558                 # check cl_user1 still registered
13559                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13560                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13561                 # check cl_user2 unregistered
13562                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13563                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13564
13565                 # check changelogs are present and starting at $user_rec1 + 1
13566                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13567                 [ -n "$user_rec1" ] ||
13568                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13569                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13570                             awk '{ print $1; exit; }')
13571
13572                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13573                 [ $((user_rec1 + 1)) == $first_rec ] ||
13574                         error "mds$i: first index should be $user_rec1 + 1, " \
13575                               "but is $first_rec"
13576         done
13577 }
13578 run_test 160f "changelog garbage collect (timestamped users)"
13579
13580 test_160g() {
13581         remote_mds_nodsh && skip "remote MDS with nodsh"
13582         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13583                 skip "Need MDS version at least 2.10.56"
13584
13585         local mdts=$(comma_list $(mdts_nodes))
13586
13587         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
13588         do_nodes $mdts $LCTL set_param fail_loc=0x1314
13589
13590         # Create a user
13591         changelog_register || error "first changelog_register failed"
13592         changelog_register || error "second changelog_register failed"
13593         local cl_users
13594         declare -A cl_user1
13595         declare -A cl_user2
13596         local user_rec1
13597         local user_rec2
13598         local i
13599
13600         # generate some changelog records to accumulate on each MDT
13601         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13602         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13603                 error "create $DIR/$tdir/$tfile failed"
13604
13605         # check changelogs have been generated
13606         local nbcl=$(changelog_dump | wc -l)
13607         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13608
13609         # reduce the max_idle_indexes value to make sure we exceed it
13610         max_ndx=$((nbcl / 2 - 1))
13611
13612         for param in "changelog_max_idle_indexes=$max_ndx" \
13613                      "changelog_gc=1" \
13614                      "changelog_min_gc_interval=2" \
13615                      "changelog_min_free_cat_entries=3"; do
13616                 local MDT0=$(facet_svc $SINGLEMDS)
13617                 local var="${param%=*}"
13618                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13619
13620                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13621                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
13622                         error "unable to set mdd.*.$param"
13623         done
13624
13625         # simulate changelog catalog almost full
13626         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13627         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13628
13629         for i in $(seq $MDSCOUNT); do
13630                 cl_users=(${CL_USERS[mds$i]})
13631                 cl_user1[mds$i]="${cl_users[0]}"
13632                 cl_user2[mds$i]="${cl_users[1]}"
13633
13634                 [ -n "${cl_user1[mds$i]}" ] ||
13635                         error "mds$i: no user registered"
13636                 [ -n "${cl_user2[mds$i]}" ] ||
13637                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13638
13639                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13640                 [ -n "$user_rec1" ] ||
13641                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13642                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13643                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13644                 [ -n "$user_rec2" ] ||
13645                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13646                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13647                      "$user_rec1 + 2 == $user_rec2"
13648                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13649                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13650                               "$user_rec1 + 2, but is $user_rec2"
13651                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13652                 [ -n "$user_rec2" ] ||
13653                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13654                 [ $user_rec1 == $user_rec2 ] ||
13655                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13656                               "$user_rec1, but is $user_rec2"
13657         done
13658
13659         # ensure we are past the previous changelog_min_gc_interval set above
13660         sleep 2
13661
13662         # generate one more changelog to trigger fail_loc
13663         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13664                 error "create $DIR/$tdir/${tfile}bis failed"
13665
13666         # ensure gc thread is done
13667         for i in $(mdts_nodes); do
13668                 wait_update $i \
13669                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13670                         error "$i: GC-thread not done"
13671         done
13672
13673         local first_rec
13674         for i in $(seq $MDSCOUNT); do
13675                 # check cl_user1 still registered
13676                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13677                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13678                 # check cl_user2 unregistered
13679                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13680                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13681
13682                 # check changelogs are present and starting at $user_rec1 + 1
13683                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13684                 [ -n "$user_rec1" ] ||
13685                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13686                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13687                             awk '{ print $1; exit; }')
13688
13689                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13690                 [ $((user_rec1 + 1)) == $first_rec ] ||
13691                         error "mds$i: first index should be $user_rec1 + 1, " \
13692                               "but is $first_rec"
13693         done
13694 }
13695 run_test 160g "changelog garbage collect (old users)"
13696
13697 test_160h() {
13698         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13699         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13700                 skip "Need MDS version at least 2.10.56"
13701
13702         local mdts=$(comma_list $(mdts_nodes))
13703
13704         # Create a user
13705         changelog_register || error "first changelog_register failed"
13706         changelog_register || error "second changelog_register failed"
13707         local cl_users
13708         declare -A cl_user1
13709         declare -A cl_user2
13710         local user_rec1
13711         local user_rec2
13712         local i
13713
13714         # generate some changelog records to accumulate on each MDT
13715         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13716         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13717                 error "create $DIR/$tdir/$tfile failed"
13718
13719         # check changelogs have been generated
13720         local nbcl=$(changelog_dump | wc -l)
13721         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13722
13723         for param in "changelog_max_idle_time=10" \
13724                      "changelog_gc=1" \
13725                      "changelog_min_gc_interval=2"; do
13726                 local MDT0=$(facet_svc $SINGLEMDS)
13727                 local var="${param%=*}"
13728                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13729
13730                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13731                 do_nodes $mdts $LCTL set_param mdd.*.$param
13732         done
13733
13734         # force cl_user2 to be idle (1st part)
13735         sleep 9
13736
13737         for i in $(seq $MDSCOUNT); do
13738                 cl_users=(${CL_USERS[mds$i]})
13739                 cl_user1[mds$i]="${cl_users[0]}"
13740                 cl_user2[mds$i]="${cl_users[1]}"
13741
13742                 [ -n "${cl_user1[mds$i]}" ] ||
13743                         error "mds$i: no user registered"
13744                 [ -n "${cl_user2[mds$i]}" ] ||
13745                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13746
13747                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13748                 [ -n "$user_rec1" ] ||
13749                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13750                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13751                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13752                 [ -n "$user_rec2" ] ||
13753                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13754                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13755                      "$user_rec1 + 2 == $user_rec2"
13756                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13757                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13758                               "$user_rec1 + 2, but is $user_rec2"
13759                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13760                 [ -n "$user_rec2" ] ||
13761                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13762                 [ $user_rec1 == $user_rec2 ] ||
13763                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13764                               "$user_rec1, but is $user_rec2"
13765         done
13766
13767         # force cl_user2 to be idle (2nd part) and to reach
13768         # changelog_max_idle_time
13769         sleep 2
13770
13771         # force each GC-thread start and block then
13772         # one per MDT/MDD, set fail_val accordingly
13773         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
13774         do_nodes $mdts $LCTL set_param fail_loc=0x1316
13775
13776         # generate more changelogs to trigger fail_loc
13777         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13778                 error "create $DIR/$tdir/${tfile}bis failed"
13779
13780         # stop MDT to stop GC-thread, should be done in back-ground as it will
13781         # block waiting for the thread to be released and exit
13782         declare -A stop_pids
13783         for i in $(seq $MDSCOUNT); do
13784                 stop mds$i &
13785                 stop_pids[mds$i]=$!
13786         done
13787
13788         for i in $(mdts_nodes); do
13789                 local facet
13790                 local nb=0
13791                 local facets=$(facets_up_on_host $i)
13792
13793                 for facet in ${facets//,/ }; do
13794                         if [[ $facet == mds* ]]; then
13795                                 nb=$((nb + 1))
13796                         fi
13797                 done
13798                 # ensure each MDS's gc threads are still present and all in "R"
13799                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
13800                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
13801                         error "$i: expected $nb GC-thread"
13802                 wait_update $i \
13803                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
13804                         "R" 20 ||
13805                         error "$i: GC-thread not found in R-state"
13806                 # check umounts of each MDT on MDS have reached kthread_stop()
13807                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
13808                         error "$i: expected $nb umount"
13809                 wait_update $i \
13810                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
13811                         error "$i: umount not found in D-state"
13812         done
13813
13814         # release all GC-threads
13815         do_nodes $mdts $LCTL set_param fail_loc=0
13816
13817         # wait for MDT stop to complete
13818         for i in $(seq $MDSCOUNT); do
13819                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
13820         done
13821
13822         # XXX
13823         # may try to check if any orphan changelog records are present
13824         # via ldiskfs/zfs and llog_reader...
13825
13826         # re-start/mount MDTs
13827         for i in $(seq $MDSCOUNT); do
13828                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
13829                         error "Fail to start mds$i"
13830         done
13831
13832         local first_rec
13833         for i in $(seq $MDSCOUNT); do
13834                 # check cl_user1 still registered
13835                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13836                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13837                 # check cl_user2 unregistered
13838                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13839                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13840
13841                 # check changelogs are present and starting at $user_rec1 + 1
13842                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13843                 [ -n "$user_rec1" ] ||
13844                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13845                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13846                             awk '{ print $1; exit; }')
13847
13848                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13849                 [ $((user_rec1 + 1)) == $first_rec ] ||
13850                         error "mds$i: first index should be $user_rec1 + 1, " \
13851                               "but is $first_rec"
13852         done
13853 }
13854 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
13855               "during mount"
13856
13857 test_160i() {
13858
13859         local mdts=$(comma_list $(mdts_nodes))
13860
13861         changelog_register || error "first changelog_register failed"
13862
13863         # generate some changelog records to accumulate on each MDT
13864         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13865         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13866                 error "create $DIR/$tdir/$tfile failed"
13867
13868         # check changelogs have been generated
13869         local nbcl=$(changelog_dump | wc -l)
13870         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13871
13872         # simulate race between register and unregister
13873         # XXX as fail_loc is set per-MDS, with DNE configs the race
13874         # simulation will only occur for one MDT per MDS and for the
13875         # others the normal race scenario will take place
13876         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
13877         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
13878         do_nodes $mdts $LCTL set_param fail_val=1
13879
13880         # unregister 1st user
13881         changelog_deregister &
13882         local pid1=$!
13883         # wait some time for deregister work to reach race rdv
13884         sleep 2
13885         # register 2nd user
13886         changelog_register || error "2nd user register failed"
13887
13888         wait $pid1 || error "1st user deregister failed"
13889
13890         local i
13891         local last_rec
13892         declare -A LAST_REC
13893         for i in $(seq $MDSCOUNT); do
13894                 if changelog_users mds$i | grep "^cl"; then
13895                         # make sure new records are added with one user present
13896                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
13897                                           awk '/^current.index:/ { print $NF }')
13898                 else
13899                         error "mds$i has no user registered"
13900                 fi
13901         done
13902
13903         # generate more changelog records to accumulate on each MDT
13904         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13905                 error "create $DIR/$tdir/${tfile}bis failed"
13906
13907         for i in $(seq $MDSCOUNT); do
13908                 last_rec=$(changelog_users $SINGLEMDS |
13909                            awk '/^current.index:/ { print $NF }')
13910                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
13911                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
13912                         error "changelogs are off on mds$i"
13913         done
13914 }
13915 run_test 160i "changelog user register/unregister race"
13916
13917 test_160k() {
13918         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13919         remote_mds_nodsh && skip "remote MDS with nodsh"
13920
13921         mkdir -p $DIR/$tdir/1/1
13922
13923         changelog_register || error "changelog_register failed"
13924         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13925
13926         changelog_users $SINGLEMDS | grep -q $cl_user ||
13927                 error "User '$cl_user' not found in changelog_users"
13928 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
13929         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
13930         rmdir $DIR/$tdir/1/1 & sleep 1
13931         mkdir $DIR/$tdir/2
13932         touch $DIR/$tdir/2/2
13933         rm -rf $DIR/$tdir/2
13934
13935         wait
13936         sleep 4
13937
13938         changelog_dump | grep rmdir || error "rmdir not recorded"
13939
13940         rm -rf $DIR/$tdir
13941         changelog_deregister
13942 }
13943 run_test 160k "Verify that changelog records are not lost"
13944
13945 test_160j() {
13946         remote_mds_nodsh && skip "remote MDS with nodsh"
13947         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
13948                 skip "Need MDS version at least 2.12.56"
13949
13950         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
13951
13952         changelog_register || error "first changelog_register failed"
13953
13954         # generate some changelog
13955         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13956         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13957                 error "create $DIR/$tdir/${tfile}bis failed"
13958
13959         # open the changelog device
13960         exec 3>/dev/changelog-$FSNAME-MDT0000
13961         exec 4</dev/changelog-$FSNAME-MDT0000
13962
13963         # umount the first lustre mount
13964         umount $MOUNT
13965
13966         # read changelog
13967         cat <&4 >/dev/null || error "read changelog failed"
13968
13969         # clear changelog
13970         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13971         changelog_users $SINGLEMDS | grep -q $cl_user ||
13972                 error "User $cl_user not found in changelog_users"
13973
13974         printf 'clear:'$cl_user':0' >&3
13975
13976         # close
13977         exec 3>&-
13978         exec 4<&-
13979
13980         # cleanup
13981         changelog_deregister || error "changelog_deregister failed"
13982
13983         umount $MOUNT2
13984         mount_client $MOUNT || error "mount_client on $MOUNT failed"
13985 }
13986 run_test 160j "client can be umounted  while its chanangelog is being used"
13987
13988 test_161a() {
13989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13990
13991         test_mkdir -c1 $DIR/$tdir
13992         cp /etc/hosts $DIR/$tdir/$tfile
13993         test_mkdir -c1 $DIR/$tdir/foo1
13994         test_mkdir -c1 $DIR/$tdir/foo2
13995         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
13996         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
13997         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
13998         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
13999         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14000         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14001                 $LFS fid2path $DIR $FID
14002                 error "bad link ea"
14003         fi
14004         # middle
14005         rm $DIR/$tdir/foo2/zachary
14006         # last
14007         rm $DIR/$tdir/foo2/thor
14008         # first
14009         rm $DIR/$tdir/$tfile
14010         # rename
14011         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14012         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14013                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14014         rm $DIR/$tdir/foo2/maggie
14015
14016         # overflow the EA
14017         local longname=$tfile.avg_len_is_thirty_two_
14018         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14019                 error_noexit 'failed to unlink many hardlinks'" EXIT
14020         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14021                 error "failed to hardlink many files"
14022         links=$($LFS fid2path $DIR $FID | wc -l)
14023         echo -n "${links}/1000 links in link EA"
14024         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14025 }
14026 run_test 161a "link ea sanity"
14027
14028 test_161b() {
14029         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14030         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14031
14032         local MDTIDX=1
14033         local remote_dir=$DIR/$tdir/remote_dir
14034
14035         mkdir -p $DIR/$tdir
14036         $LFS mkdir -i $MDTIDX $remote_dir ||
14037                 error "create remote directory failed"
14038
14039         cp /etc/hosts $remote_dir/$tfile
14040         mkdir -p $remote_dir/foo1
14041         mkdir -p $remote_dir/foo2
14042         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14043         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14044         ln $remote_dir/$tfile $remote_dir/foo1/luna
14045         ln $remote_dir/$tfile $remote_dir/foo2/thor
14046
14047         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14048                      tr -d ']')
14049         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14050                 $LFS fid2path $DIR $FID
14051                 error "bad link ea"
14052         fi
14053         # middle
14054         rm $remote_dir/foo2/zachary
14055         # last
14056         rm $remote_dir/foo2/thor
14057         # first
14058         rm $remote_dir/$tfile
14059         # rename
14060         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14061         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14062         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14063                 $LFS fid2path $DIR $FID
14064                 error "bad link rename"
14065         fi
14066         rm $remote_dir/foo2/maggie
14067
14068         # overflow the EA
14069         local longname=filename_avg_len_is_thirty_two_
14070         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14071                 error "failed to hardlink many files"
14072         links=$($LFS fid2path $DIR $FID | wc -l)
14073         echo -n "${links}/1000 links in link EA"
14074         [[ ${links} -gt 60 ]] ||
14075                 error "expected at least 60 links in link EA"
14076         unlinkmany $remote_dir/foo2/$longname 1000 ||
14077         error "failed to unlink many hardlinks"
14078 }
14079 run_test 161b "link ea sanity under remote directory"
14080
14081 test_161c() {
14082         remote_mds_nodsh && skip "remote MDS with nodsh"
14083         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14084         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14085                 skip "Need MDS version at least 2.1.5"
14086
14087         # define CLF_RENAME_LAST 0x0001
14088         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14089         changelog_register || error "changelog_register failed"
14090
14091         rm -rf $DIR/$tdir
14092         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14093         touch $DIR/$tdir/foo_161c
14094         touch $DIR/$tdir/bar_161c
14095         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14096         changelog_dump | grep RENME | tail -n 5
14097         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14098         changelog_clear 0 || error "changelog_clear failed"
14099         if [ x$flags != "x0x1" ]; then
14100                 error "flag $flags is not 0x1"
14101         fi
14102
14103         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14104         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14105         touch $DIR/$tdir/foo_161c
14106         touch $DIR/$tdir/bar_161c
14107         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14108         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14109         changelog_dump | grep RENME | tail -n 5
14110         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14111         changelog_clear 0 || error "changelog_clear failed"
14112         if [ x$flags != "x0x0" ]; then
14113                 error "flag $flags is not 0x0"
14114         fi
14115         echo "rename overwrite a target having nlink > 1," \
14116                 "changelog record has flags of $flags"
14117
14118         # rename doesn't overwrite a target (changelog flag 0x0)
14119         touch $DIR/$tdir/foo_161c
14120         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14121         changelog_dump | grep RENME | tail -n 5
14122         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14123         changelog_clear 0 || error "changelog_clear failed"
14124         if [ x$flags != "x0x0" ]; then
14125                 error "flag $flags is not 0x0"
14126         fi
14127         echo "rename doesn't overwrite a target," \
14128                 "changelog record has flags of $flags"
14129
14130         # define CLF_UNLINK_LAST 0x0001
14131         # unlink a file having nlink = 1 (changelog flag 0x1)
14132         rm -f $DIR/$tdir/foo2_161c
14133         changelog_dump | grep UNLNK | tail -n 5
14134         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14135         changelog_clear 0 || error "changelog_clear failed"
14136         if [ x$flags != "x0x1" ]; then
14137                 error "flag $flags is not 0x1"
14138         fi
14139         echo "unlink a file having nlink = 1," \
14140                 "changelog record has flags of $flags"
14141
14142         # unlink a file having nlink > 1 (changelog flag 0x0)
14143         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14144         rm -f $DIR/$tdir/foobar_161c
14145         changelog_dump | grep UNLNK | tail -n 5
14146         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14147         changelog_clear 0 || error "changelog_clear failed"
14148         if [ x$flags != "x0x0" ]; then
14149                 error "flag $flags is not 0x0"
14150         fi
14151         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14152 }
14153 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14154
14155 test_161d() {
14156         remote_mds_nodsh && skip "remote MDS with nodsh"
14157
14158         local pid
14159         local fid
14160
14161         changelog_register || error "changelog_register failed"
14162
14163         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
14164         # interfer with $MOUNT/.lustre/fid/ access
14165         mkdir $DIR/$tdir
14166         [[ $? -eq 0 ]] || error "mkdir failed"
14167
14168         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
14169         $LCTL set_param fail_loc=0x8000140c
14170         # 5s pause
14171         $LCTL set_param fail_val=5
14172
14173         # create file
14174         echo foofoo > $DIR/$tdir/$tfile &
14175         pid=$!
14176
14177         # wait for create to be delayed
14178         sleep 2
14179
14180         ps -p $pid
14181         [[ $? -eq 0 ]] || error "create should be blocked"
14182
14183         local tempfile=$(mktemp)
14184         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
14185         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
14186         # some delay may occur during ChangeLog publishing and file read just
14187         # above, that could allow file write to happen finally
14188         [[ -s $tempfile ]] && echo "file should be empty"
14189
14190         $LCTL set_param fail_loc=0
14191
14192         wait $pid
14193         [[ $? -eq 0 ]] || error "create failed"
14194 }
14195 run_test 161d "create with concurrent .lustre/fid access"
14196
14197 check_path() {
14198         local expected="$1"
14199         shift
14200         local fid="$2"
14201
14202         local path
14203         path=$($LFS fid2path "$@")
14204         local rc=$?
14205
14206         if [ $rc -ne 0 ]; then
14207                 error "path looked up of '$expected' failed: rc=$rc"
14208         elif [ "$path" != "$expected" ]; then
14209                 error "path looked up '$path' instead of '$expected'"
14210         else
14211                 echo "FID '$fid' resolves to path '$path' as expected"
14212         fi
14213 }
14214
14215 test_162a() { # was test_162
14216         test_mkdir -p -c1 $DIR/$tdir/d2
14217         touch $DIR/$tdir/d2/$tfile
14218         touch $DIR/$tdir/d2/x1
14219         touch $DIR/$tdir/d2/x2
14220         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
14221         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
14222         # regular file
14223         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
14224         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
14225
14226         # softlink
14227         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
14228         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
14229         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
14230
14231         # softlink to wrong file
14232         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
14233         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
14234         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
14235
14236         # hardlink
14237         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
14238         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
14239         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
14240         # fid2path dir/fsname should both work
14241         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
14242         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
14243
14244         # hardlink count: check that there are 2 links
14245         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
14246         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
14247
14248         # hardlink indexing: remove the first link
14249         rm $DIR/$tdir/d2/p/q/r/hlink
14250         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
14251 }
14252 run_test 162a "path lookup sanity"
14253
14254 test_162b() {
14255         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14256         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14257
14258         mkdir $DIR/$tdir
14259         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
14260                                 error "create striped dir failed"
14261
14262         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
14263                                         tail -n 1 | awk '{print $2}')
14264         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
14265
14266         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
14267         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
14268
14269         # regular file
14270         for ((i=0;i<5;i++)); do
14271                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
14272                         error "get fid for f$i failed"
14273                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
14274
14275                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
14276                         error "get fid for d$i failed"
14277                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
14278         done
14279
14280         return 0
14281 }
14282 run_test 162b "striped directory path lookup sanity"
14283
14284 # LU-4239: Verify fid2path works with paths 100 or more directories deep
14285 test_162c() {
14286         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
14287                 skip "Need MDS version at least 2.7.51"
14288
14289         local lpath=$tdir.local
14290         local rpath=$tdir.remote
14291
14292         test_mkdir $DIR/$lpath
14293         test_mkdir $DIR/$rpath
14294
14295         for ((i = 0; i <= 101; i++)); do
14296                 lpath="$lpath/$i"
14297                 mkdir $DIR/$lpath
14298                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
14299                         error "get fid for local directory $DIR/$lpath failed"
14300                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
14301
14302                 rpath="$rpath/$i"
14303                 test_mkdir $DIR/$rpath
14304                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
14305                         error "get fid for remote directory $DIR/$rpath failed"
14306                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
14307         done
14308
14309         return 0
14310 }
14311 run_test 162c "fid2path works with paths 100 or more directories deep"
14312
14313 test_169() {
14314         # do directio so as not to populate the page cache
14315         log "creating a 10 Mb file"
14316         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
14317         log "starting reads"
14318         dd if=$DIR/$tfile of=/dev/null bs=4096 &
14319         log "truncating the file"
14320         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
14321         log "killing dd"
14322         kill %+ || true # reads might have finished
14323         echo "wait until dd is finished"
14324         wait
14325         log "removing the temporary file"
14326         rm -rf $DIR/$tfile || error "tmp file removal failed"
14327 }
14328 run_test 169 "parallel read and truncate should not deadlock"
14329
14330 test_170() {
14331         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14332
14333         $LCTL clear     # bug 18514
14334         $LCTL debug_daemon start $TMP/${tfile}_log_good
14335         touch $DIR/$tfile
14336         $LCTL debug_daemon stop
14337         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
14338                 error "sed failed to read log_good"
14339
14340         $LCTL debug_daemon start $TMP/${tfile}_log_good
14341         rm -rf $DIR/$tfile
14342         $LCTL debug_daemon stop
14343
14344         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
14345                error "lctl df log_bad failed"
14346
14347         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14348         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14349
14350         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
14351         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
14352
14353         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
14354                 error "bad_line good_line1 good_line2 are empty"
14355
14356         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14357         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14358         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14359
14360         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14361         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14362         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14363
14364         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14365                 error "bad_line_new good_line_new are empty"
14366
14367         local expected_good=$((good_line1 + good_line2*2))
14368
14369         rm -f $TMP/${tfile}*
14370         # LU-231, short malformed line may not be counted into bad lines
14371         if [ $bad_line -ne $bad_line_new ] &&
14372                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14373                 error "expected $bad_line bad lines, but got $bad_line_new"
14374                 return 1
14375         fi
14376
14377         if [ $expected_good -ne $good_line_new ]; then
14378                 error "expected $expected_good good lines, but got $good_line_new"
14379                 return 2
14380         fi
14381         true
14382 }
14383 run_test 170 "test lctl df to handle corrupted log ====================="
14384
14385 test_171() { # bug20592
14386         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14387
14388         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14389         $LCTL set_param fail_loc=0x50e
14390         $LCTL set_param fail_val=3000
14391         multiop_bg_pause $DIR/$tfile O_s || true
14392         local MULTIPID=$!
14393         kill -USR1 $MULTIPID
14394         # cause log dump
14395         sleep 3
14396         wait $MULTIPID
14397         if dmesg | grep "recursive fault"; then
14398                 error "caught a recursive fault"
14399         fi
14400         $LCTL set_param fail_loc=0
14401         true
14402 }
14403 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14404
14405 # it would be good to share it with obdfilter-survey/iokit-libecho code
14406 setup_obdecho_osc () {
14407         local rc=0
14408         local ost_nid=$1
14409         local obdfilter_name=$2
14410         echo "Creating new osc for $obdfilter_name on $ost_nid"
14411         # make sure we can find loopback nid
14412         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14413
14414         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14415                            ${obdfilter_name}_osc_UUID || rc=2; }
14416         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14417                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14418         return $rc
14419 }
14420
14421 cleanup_obdecho_osc () {
14422         local obdfilter_name=$1
14423         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14424         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14425         return 0
14426 }
14427
14428 obdecho_test() {
14429         local OBD=$1
14430         local node=$2
14431         local pages=${3:-64}
14432         local rc=0
14433         local id
14434
14435         local count=10
14436         local obd_size=$(get_obd_size $node $OBD)
14437         local page_size=$(get_page_size $node)
14438         if [[ -n "$obd_size" ]]; then
14439                 local new_count=$((obd_size / (pages * page_size / 1024)))
14440                 [[ $new_count -ge $count ]] || count=$new_count
14441         fi
14442
14443         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14444         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14445                            rc=2; }
14446         if [ $rc -eq 0 ]; then
14447             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14448             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14449         fi
14450         echo "New object id is $id"
14451         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14452                            rc=4; }
14453         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14454                            "test_brw $count w v $pages $id" || rc=4; }
14455         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14456                            rc=4; }
14457         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
14458                                         "cleanup" || rc=5; }
14459         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
14460                                         "detach" || rc=6; }
14461         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14462         return $rc
14463 }
14464
14465 test_180a() {
14466         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14467
14468         if ! module_loaded obdecho; then
14469                 load_module obdecho/obdecho &&
14470                         stack_trap "rmmod obdecho" EXIT ||
14471                         error "unable to load obdecho on client"
14472         fi
14473
14474         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14475         local host=$($LCTL get_param -n osc.$osc.import |
14476                      awk '/current_connection:/ { print $2 }' )
14477         local target=$($LCTL get_param -n osc.$osc.import |
14478                        awk '/target:/ { print $2 }' )
14479         target=${target%_UUID}
14480
14481         if [ -n "$target" ]; then
14482                 setup_obdecho_osc $host $target &&
14483                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14484                         { error "obdecho setup failed with $?"; return; }
14485
14486                 obdecho_test ${target}_osc client ||
14487                         error "obdecho_test failed on ${target}_osc"
14488         else
14489                 $LCTL get_param osc.$osc.import
14490                 error "there is no osc.$osc.import target"
14491         fi
14492 }
14493 run_test 180a "test obdecho on osc"
14494
14495 test_180b() {
14496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14497         remote_ost_nodsh && skip "remote OST with nodsh"
14498
14499         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14500                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14501                 error "failed to load module obdecho"
14502
14503         local target=$(do_facet ost1 $LCTL dl |
14504                        awk '/obdfilter/ { print $4; exit; }')
14505
14506         if [ -n "$target" ]; then
14507                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14508         else
14509                 do_facet ost1 $LCTL dl
14510                 error "there is no obdfilter target on ost1"
14511         fi
14512 }
14513 run_test 180b "test obdecho directly on obdfilter"
14514
14515 test_180c() { # LU-2598
14516         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14517         remote_ost_nodsh && skip "remote OST with nodsh"
14518         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14519                 skip "Need MDS version at least 2.4.0"
14520
14521         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14522                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14523                 error "failed to load module obdecho"
14524
14525         local target=$(do_facet ost1 $LCTL dl |
14526                        awk '/obdfilter/ { print $4; exit; }')
14527
14528         if [ -n "$target" ]; then
14529                 local pages=16384 # 64MB bulk I/O RPC size
14530
14531                 obdecho_test "$target" ost1 "$pages" ||
14532                         error "obdecho_test with pages=$pages failed with $?"
14533         else
14534                 do_facet ost1 $LCTL dl
14535                 error "there is no obdfilter target on ost1"
14536         fi
14537 }
14538 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14539
14540 test_181() { # bug 22177
14541         test_mkdir $DIR/$tdir
14542         # create enough files to index the directory
14543         createmany -o $DIR/$tdir/foobar 4000
14544         # print attributes for debug purpose
14545         lsattr -d .
14546         # open dir
14547         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14548         MULTIPID=$!
14549         # remove the files & current working dir
14550         unlinkmany $DIR/$tdir/foobar 4000
14551         rmdir $DIR/$tdir
14552         kill -USR1 $MULTIPID
14553         wait $MULTIPID
14554         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14555         return 0
14556 }
14557 run_test 181 "Test open-unlinked dir ========================"
14558
14559 test_182() {
14560         local fcount=1000
14561         local tcount=10
14562
14563         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14564
14565         $LCTL set_param mdc.*.rpc_stats=clear
14566
14567         for (( i = 0; i < $tcount; i++ )) ; do
14568                 mkdir $DIR/$tdir/$i
14569         done
14570
14571         for (( i = 0; i < $tcount; i++ )) ; do
14572                 createmany -o $DIR/$tdir/$i/f- $fcount &
14573         done
14574         wait
14575
14576         for (( i = 0; i < $tcount; i++ )) ; do
14577                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14578         done
14579         wait
14580
14581         $LCTL get_param mdc.*.rpc_stats
14582
14583         rm -rf $DIR/$tdir
14584 }
14585 run_test 182 "Test parallel modify metadata operations ================"
14586
14587 test_183() { # LU-2275
14588         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14589         remote_mds_nodsh && skip "remote MDS with nodsh"
14590         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
14591                 skip "Need MDS version at least 2.3.56"
14592
14593         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14594         echo aaa > $DIR/$tdir/$tfile
14595
14596 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
14597         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
14598
14599         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
14600         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
14601
14602         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14603
14604         # Flush negative dentry cache
14605         touch $DIR/$tdir/$tfile
14606
14607         # We are not checking for any leaked references here, they'll
14608         # become evident next time we do cleanup with module unload.
14609         rm -rf $DIR/$tdir
14610 }
14611 run_test 183 "No crash or request leak in case of strange dispositions ========"
14612
14613 # test suite 184 is for LU-2016, LU-2017
14614 test_184a() {
14615         check_swap_layouts_support
14616
14617         dir0=$DIR/$tdir/$testnum
14618         test_mkdir -p -c1 $dir0
14619         ref1=/etc/passwd
14620         ref2=/etc/group
14621         file1=$dir0/f1
14622         file2=$dir0/f2
14623         $SETSTRIPE -c1 $file1
14624         cp $ref1 $file1
14625         $SETSTRIPE -c2 $file2
14626         cp $ref2 $file2
14627         gen1=$($GETSTRIPE -g $file1)
14628         gen2=$($GETSTRIPE -g $file2)
14629
14630         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
14631         gen=$($GETSTRIPE -g $file1)
14632         [[ $gen1 != $gen ]] ||
14633                 "Layout generation on $file1 does not change"
14634         gen=$($GETSTRIPE -g $file2)
14635         [[ $gen2 != $gen ]] ||
14636                 "Layout generation on $file2 does not change"
14637
14638         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
14639         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14640
14641         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
14642 }
14643 run_test 184a "Basic layout swap"
14644
14645 test_184b() {
14646         check_swap_layouts_support
14647
14648         dir0=$DIR/$tdir/$testnum
14649         mkdir -p $dir0 || error "creating dir $dir0"
14650         file1=$dir0/f1
14651         file2=$dir0/f2
14652         file3=$dir0/f3
14653         dir1=$dir0/d1
14654         dir2=$dir0/d2
14655         mkdir $dir1 $dir2
14656         $SETSTRIPE -c1 $file1
14657         $SETSTRIPE -c2 $file2
14658         $SETSTRIPE -c1 $file3
14659         chown $RUNAS_ID $file3
14660         gen1=$($GETSTRIPE -g $file1)
14661         gen2=$($GETSTRIPE -g $file2)
14662
14663         $LFS swap_layouts $dir1 $dir2 &&
14664                 error "swap of directories layouts should fail"
14665         $LFS swap_layouts $dir1 $file1 &&
14666                 error "swap of directory and file layouts should fail"
14667         $RUNAS $LFS swap_layouts $file1 $file2 &&
14668                 error "swap of file we cannot write should fail"
14669         $LFS swap_layouts $file1 $file3 &&
14670                 error "swap of file with different owner should fail"
14671         /bin/true # to clear error code
14672 }
14673 run_test 184b "Forbidden layout swap (will generate errors)"
14674
14675 test_184c() {
14676         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
14677         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
14678         check_swap_layouts_support
14679
14680         local dir0=$DIR/$tdir/$testnum
14681         mkdir -p $dir0 || error "creating dir $dir0"
14682
14683         local ref1=$dir0/ref1
14684         local ref2=$dir0/ref2
14685         local file1=$dir0/file1
14686         local file2=$dir0/file2
14687         # create a file large enough for the concurrent test
14688         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
14689         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
14690         echo "ref file size: ref1($(stat -c %s $ref1))," \
14691              "ref2($(stat -c %s $ref2))"
14692
14693         cp $ref2 $file2
14694         dd if=$ref1 of=$file1 bs=16k &
14695         local DD_PID=$!
14696
14697         # Make sure dd starts to copy file
14698         while [ ! -f $file1 ]; do sleep 0.1; done
14699
14700         $LFS swap_layouts $file1 $file2
14701         local rc=$?
14702         wait $DD_PID
14703         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
14704         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
14705
14706         # how many bytes copied before swapping layout
14707         local copied=$(stat -c %s $file2)
14708         local remaining=$(stat -c %s $ref1)
14709         remaining=$((remaining - copied))
14710         echo "Copied $copied bytes before swapping layout..."
14711
14712         cmp -n $copied $file1 $ref2 | grep differ &&
14713                 error "Content mismatch [0, $copied) of ref2 and file1"
14714         cmp -n $copied $file2 $ref1 ||
14715                 error "Content mismatch [0, $copied) of ref1 and file2"
14716         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
14717                 error "Content mismatch [$copied, EOF) of ref1 and file1"
14718
14719         # clean up
14720         rm -f $ref1 $ref2 $file1 $file2
14721 }
14722 run_test 184c "Concurrent write and layout swap"
14723
14724 test_184d() {
14725         check_swap_layouts_support
14726         [ -z "$(which getfattr 2>/dev/null)" ] &&
14727                 skip_env "no getfattr command"
14728
14729         local file1=$DIR/$tdir/$tfile-1
14730         local file2=$DIR/$tdir/$tfile-2
14731         local file3=$DIR/$tdir/$tfile-3
14732         local lovea1
14733         local lovea2
14734
14735         mkdir -p $DIR/$tdir
14736         touch $file1 || error "create $file1 failed"
14737         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14738                 error "create $file2 failed"
14739         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14740                 error "create $file3 failed"
14741         lovea1=$(get_layout_param $file1)
14742
14743         $LFS swap_layouts $file2 $file3 ||
14744                 error "swap $file2 $file3 layouts failed"
14745         $LFS swap_layouts $file1 $file2 ||
14746                 error "swap $file1 $file2 layouts failed"
14747
14748         lovea2=$(get_layout_param $file2)
14749         echo "$lovea1"
14750         echo "$lovea2"
14751         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
14752
14753         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14754         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
14755 }
14756 run_test 184d "allow stripeless layouts swap"
14757
14758 test_184e() {
14759         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
14760                 skip "Need MDS version at least 2.6.94"
14761         check_swap_layouts_support
14762         [ -z "$(which getfattr 2>/dev/null)" ] &&
14763                 skip_env "no getfattr command"
14764
14765         local file1=$DIR/$tdir/$tfile-1
14766         local file2=$DIR/$tdir/$tfile-2
14767         local file3=$DIR/$tdir/$tfile-3
14768         local lovea
14769
14770         mkdir -p $DIR/$tdir
14771         touch $file1 || error "create $file1 failed"
14772         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14773                 error "create $file2 failed"
14774         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14775                 error "create $file3 failed"
14776
14777         $LFS swap_layouts $file1 $file2 ||
14778                 error "swap $file1 $file2 layouts failed"
14779
14780         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14781         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
14782
14783         echo 123 > $file1 || error "Should be able to write into $file1"
14784
14785         $LFS swap_layouts $file1 $file3 ||
14786                 error "swap $file1 $file3 layouts failed"
14787
14788         echo 123 > $file1 || error "Should be able to write into $file1"
14789
14790         rm -rf $file1 $file2 $file3
14791 }
14792 run_test 184e "Recreate layout after stripeless layout swaps"
14793
14794 test_184f() {
14795         # Create a file with name longer than sizeof(struct stat) ==
14796         # 144 to see if we can get chars from the file name to appear
14797         # in the returned striping. Note that 'f' == 0x66.
14798         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
14799
14800         mkdir -p $DIR/$tdir
14801         mcreate $DIR/$tdir/$file
14802         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
14803                 error "IOC_MDC_GETFILEINFO returned garbage striping"
14804         fi
14805 }
14806 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
14807
14808 test_185() { # LU-2441
14809         # LU-3553 - no volatile file support in old servers
14810         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
14811                 skip "Need MDS version at least 2.3.60"
14812
14813         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14814         touch $DIR/$tdir/spoo
14815         local mtime1=$(stat -c "%Y" $DIR/$tdir)
14816         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
14817                 error "cannot create/write a volatile file"
14818         [ "$FILESET" == "" ] &&
14819         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
14820                 error "FID is still valid after close"
14821
14822         multiop_bg_pause $DIR/$tdir vVw4096_c
14823         local multi_pid=$!
14824
14825         local OLD_IFS=$IFS
14826         IFS=":"
14827         local fidv=($fid)
14828         IFS=$OLD_IFS
14829         # assume that the next FID for this client is sequential, since stdout
14830         # is unfortunately eaten by multiop_bg_pause
14831         local n=$((${fidv[1]} + 1))
14832         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
14833         if [ "$FILESET" == "" ]; then
14834                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
14835                         error "FID is missing before close"
14836         fi
14837         kill -USR1 $multi_pid
14838         # 1 second delay, so if mtime change we will see it
14839         sleep 1
14840         local mtime2=$(stat -c "%Y" $DIR/$tdir)
14841         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
14842 }
14843 run_test 185 "Volatile file support"
14844
14845 function create_check_volatile() {
14846         local idx=$1
14847         local tgt
14848
14849         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
14850         local PID=$!
14851         sleep 1
14852         local FID=$(cat /tmp/${tfile}.fid)
14853         [ "$FID" == "" ] && error "can't get FID for volatile"
14854         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
14855         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
14856         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
14857         kill -USR1 $PID
14858         wait
14859         sleep 1
14860         cancel_lru_locks mdc # flush opencache
14861         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
14862         return 0
14863 }
14864
14865 test_185a(){
14866         # LU-12516 - volatile creation via .lustre
14867         [[ $MDS1_VERSION -ge $(version_code 2.12.2) ]] ||
14868                 skip "Need MDS version at least 2.12.2"
14869
14870         create_check_volatile 0
14871         [ $MDSCOUNT -lt 2 ] && return 0
14872
14873         # DNE case
14874         create_check_volatile 1
14875
14876         return 0
14877 }
14878 run_test 185a "Volatile file creation in .lustre/fid/"
14879
14880 test_187a() {
14881         remote_mds_nodsh && skip "remote MDS with nodsh"
14882         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14883                 skip "Need MDS version at least 2.3.0"
14884
14885         local dir0=$DIR/$tdir/$testnum
14886         mkdir -p $dir0 || error "creating dir $dir0"
14887
14888         local file=$dir0/file1
14889         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
14890         local dv1=$($LFS data_version $file)
14891         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
14892         local dv2=$($LFS data_version $file)
14893         [[ $dv1 != $dv2 ]] ||
14894                 error "data version did not change on write $dv1 == $dv2"
14895
14896         # clean up
14897         rm -f $file1
14898 }
14899 run_test 187a "Test data version change"
14900
14901 test_187b() {
14902         remote_mds_nodsh && skip "remote MDS with nodsh"
14903         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14904                 skip "Need MDS version at least 2.3.0"
14905
14906         local dir0=$DIR/$tdir/$testnum
14907         mkdir -p $dir0 || error "creating dir $dir0"
14908
14909         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
14910         [[ ${DV[0]} != ${DV[1]} ]] ||
14911                 error "data version did not change on write"\
14912                       " ${DV[0]} == ${DV[1]}"
14913
14914         # clean up
14915         rm -f $file1
14916 }
14917 run_test 187b "Test data version change on volatile file"
14918
14919 test_200() {
14920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14921         remote_mgs_nodsh && skip "remote MGS with nodsh"
14922         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14923
14924         local POOL=${POOL:-cea1}
14925         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
14926         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
14927         # Pool OST targets
14928         local first_ost=0
14929         local last_ost=$(($OSTCOUNT - 1))
14930         local ost_step=2
14931         local ost_list=$(seq $first_ost $ost_step $last_ost)
14932         local ost_range="$first_ost $last_ost $ost_step"
14933         local test_path=$POOL_ROOT/$POOL_DIR_NAME
14934         local file_dir=$POOL_ROOT/file_tst
14935         local subdir=$test_path/subdir
14936         local rc=0
14937
14938         while : ; do
14939                 # former test_200a test_200b
14940                 pool_add $POOL                          || { rc=$? ; break; }
14941                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
14942                 # former test_200c test_200d
14943                 mkdir -p $test_path
14944                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
14945                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
14946                 mkdir -p $subdir
14947                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
14948                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
14949                                                         || { rc=$? ; break; }
14950                 # former test_200e test_200f
14951                 local files=$((OSTCOUNT*3))
14952                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
14953                                                         || { rc=$? ; break; }
14954                 pool_create_files $POOL $file_dir $files "$ost_list" \
14955                                                         || { rc=$? ; break; }
14956                 # former test_200g test_200h
14957                 pool_lfs_df $POOL                       || { rc=$? ; break; }
14958                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
14959
14960                 # former test_201a test_201b test_201c
14961                 pool_remove_first_target $POOL          || { rc=$? ; break; }
14962
14963                 local f=$test_path/$tfile
14964                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
14965                 pool_remove $POOL $f                    || { rc=$? ; break; }
14966                 break
14967         done
14968
14969         destroy_test_pools
14970
14971         return $rc
14972 }
14973 run_test 200 "OST pools"
14974
14975 # usage: default_attr <count | size | offset>
14976 default_attr() {
14977         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
14978 }
14979
14980 # usage: check_default_stripe_attr
14981 check_default_stripe_attr() {
14982         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
14983         case $1 in
14984         --stripe-count|-c)
14985                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
14986         --stripe-size|-S)
14987                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
14988         --stripe-index|-i)
14989                 EXPECTED=-1;;
14990         *)
14991                 error "unknown getstripe attr '$1'"
14992         esac
14993
14994         [ $ACTUAL == $EXPECTED ] ||
14995                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
14996 }
14997
14998 test_204a() {
14999         test_mkdir $DIR/$tdir
15000         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
15001
15002         check_default_stripe_attr --stripe-count
15003         check_default_stripe_attr --stripe-size
15004         check_default_stripe_attr --stripe-index
15005 }
15006 run_test 204a "Print default stripe attributes"
15007
15008 test_204b() {
15009         test_mkdir $DIR/$tdir
15010         $SETSTRIPE --stripe-count 1 $DIR/$tdir
15011
15012         check_default_stripe_attr --stripe-size
15013         check_default_stripe_attr --stripe-index
15014 }
15015 run_test 204b "Print default stripe size and offset"
15016
15017 test_204c() {
15018         test_mkdir $DIR/$tdir
15019         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
15020
15021         check_default_stripe_attr --stripe-count
15022         check_default_stripe_attr --stripe-index
15023 }
15024 run_test 204c "Print default stripe count and offset"
15025
15026 test_204d() {
15027         test_mkdir $DIR/$tdir
15028         $SETSTRIPE --stripe-index 0 $DIR/$tdir
15029
15030         check_default_stripe_attr --stripe-count
15031         check_default_stripe_attr --stripe-size
15032 }
15033 run_test 204d "Print default stripe count and size"
15034
15035 test_204e() {
15036         test_mkdir $DIR/$tdir
15037         $SETSTRIPE -d $DIR/$tdir
15038
15039         check_default_stripe_attr --stripe-count --raw
15040         check_default_stripe_attr --stripe-size --raw
15041         check_default_stripe_attr --stripe-index --raw
15042 }
15043 run_test 204e "Print raw stripe attributes"
15044
15045 test_204f() {
15046         test_mkdir $DIR/$tdir
15047         $SETSTRIPE --stripe-count 1 $DIR/$tdir
15048
15049         check_default_stripe_attr --stripe-size --raw
15050         check_default_stripe_attr --stripe-index --raw
15051 }
15052 run_test 204f "Print raw stripe size and offset"
15053
15054 test_204g() {
15055         test_mkdir $DIR/$tdir
15056         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
15057
15058         check_default_stripe_attr --stripe-count --raw
15059         check_default_stripe_attr --stripe-index --raw
15060 }
15061 run_test 204g "Print raw stripe count and offset"
15062
15063 test_204h() {
15064         test_mkdir $DIR/$tdir
15065         $SETSTRIPE --stripe-index 0 $DIR/$tdir
15066
15067         check_default_stripe_attr --stripe-count --raw
15068         check_default_stripe_attr --stripe-size --raw
15069 }
15070 run_test 204h "Print raw stripe count and size"
15071
15072 # Figure out which job scheduler is being used, if any,
15073 # or use a fake one
15074 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
15075         JOBENV=SLURM_JOB_ID
15076 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
15077         JOBENV=LSB_JOBID
15078 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
15079         JOBENV=PBS_JOBID
15080 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
15081         JOBENV=LOADL_STEP_ID
15082 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
15083         JOBENV=JOB_ID
15084 else
15085         $LCTL list_param jobid_name > /dev/null 2>&1
15086         if [ $? -eq 0 ]; then
15087                 JOBENV=nodelocal
15088         else
15089                 JOBENV=FAKE_JOBID
15090         fi
15091 fi
15092 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
15093
15094 verify_jobstats() {
15095         local cmd=($1)
15096         shift
15097         local facets="$@"
15098
15099 # we don't really need to clear the stats for this test to work, since each
15100 # command has a unique jobid, but it makes debugging easier if needed.
15101 #       for facet in $facets; do
15102 #               local dev=$(convert_facet2label $facet)
15103 #               # clear old jobstats
15104 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
15105 #       done
15106
15107         # use a new JobID for each test, or we might see an old one
15108         [ "$JOBENV" = "FAKE_JOBID" ] &&
15109                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
15110
15111         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
15112
15113         [ "$JOBENV" = "nodelocal" ] && {
15114                 FAKE_JOBID=id.$testnum.%e.$RANDOM
15115                 $LCTL set_param jobid_name=$FAKE_JOBID
15116                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
15117         }
15118
15119         log "Test: ${cmd[*]}"
15120         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
15121
15122         if [ $JOBENV = "FAKE_JOBID" ]; then
15123                 FAKE_JOBID=$JOBVAL ${cmd[*]}
15124         else
15125                 ${cmd[*]}
15126         fi
15127
15128         # all files are created on OST0000
15129         for facet in $facets; do
15130                 local stats="*.$(convert_facet2label $facet).job_stats"
15131
15132                 # strip out libtool wrappers for in-tree executables
15133                 if [ $(do_facet $facet lctl get_param $stats |
15134                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
15135                         do_facet $facet lctl get_param $stats
15136                         error "No jobstats for $JOBVAL found on $facet::$stats"
15137                 fi
15138         done
15139 }
15140
15141 jobstats_set() {
15142         local new_jobenv=$1
15143
15144         set_persistent_param_and_check client "jobid_var" \
15145                 "$FSNAME.sys.jobid_var" $new_jobenv
15146 }
15147
15148 test_205a() { # Job stats
15149         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15150         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
15151                 skip "Need MDS version with at least 2.7.1"
15152         remote_mgs_nodsh && skip "remote MGS with nodsh"
15153         remote_mds_nodsh && skip "remote MDS with nodsh"
15154         remote_ost_nodsh && skip "remote OST with nodsh"
15155         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
15156                 skip "Server doesn't support jobstats"
15157         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
15158
15159         local old_jobenv=$($LCTL get_param -n jobid_var)
15160         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
15161
15162         if [[ $PERM_CMD = *"set_param -P"* ]]; then
15163                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
15164         else
15165                 stack_trap "do_facet mgs $PERM_CMD \
15166                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
15167         fi
15168         changelog_register
15169
15170         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
15171                                 mdt.*.job_cleanup_interval | head -n 1)
15172         local new_interval=5
15173         do_facet $SINGLEMDS \
15174                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
15175         stack_trap "do_facet $SINGLEMDS \
15176                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
15177         local start=$SECONDS
15178
15179         local cmd
15180         # mkdir
15181         cmd="mkdir $DIR/$tdir"
15182         verify_jobstats "$cmd" "$SINGLEMDS"
15183         # rmdir
15184         cmd="rmdir $DIR/$tdir"
15185         verify_jobstats "$cmd" "$SINGLEMDS"
15186         # mkdir on secondary MDT
15187         if [ $MDSCOUNT -gt 1 ]; then
15188                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
15189                 verify_jobstats "$cmd" "mds2"
15190         fi
15191         # mknod
15192         cmd="mknod $DIR/$tfile c 1 3"
15193         verify_jobstats "$cmd" "$SINGLEMDS"
15194         # unlink
15195         cmd="rm -f $DIR/$tfile"
15196         verify_jobstats "$cmd" "$SINGLEMDS"
15197         # create all files on OST0000 so verify_jobstats can find OST stats
15198         # open & close
15199         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
15200         verify_jobstats "$cmd" "$SINGLEMDS"
15201         # setattr
15202         cmd="touch $DIR/$tfile"
15203         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15204         # write
15205         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
15206         verify_jobstats "$cmd" "ost1"
15207         # read
15208         cancel_lru_locks osc
15209         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
15210         verify_jobstats "$cmd" "ost1"
15211         # truncate
15212         cmd="$TRUNCATE $DIR/$tfile 0"
15213         verify_jobstats "$cmd" "$SINGLEMDS ost1"
15214         # rename
15215         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
15216         verify_jobstats "$cmd" "$SINGLEMDS"
15217         # jobstats expiry - sleep until old stats should be expired
15218         local left=$((new_interval + 5 - (SECONDS - start)))
15219         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
15220                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
15221                         "0" $left
15222         cmd="mkdir $DIR/$tdir.expire"
15223         verify_jobstats "$cmd" "$SINGLEMDS"
15224         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
15225             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
15226
15227         # Ensure that jobid are present in changelog (if supported by MDS)
15228         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
15229                 changelog_dump | tail -10
15230                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
15231                 [ $jobids -eq 9 ] ||
15232                         error "Wrong changelog jobid count $jobids != 9"
15233
15234                 # LU-5862
15235                 JOBENV="disable"
15236                 jobstats_set $JOBENV
15237                 touch $DIR/$tfile
15238                 changelog_dump | grep $tfile
15239                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
15240                 [ $jobids -eq 0 ] ||
15241                         error "Unexpected jobids when jobid_var=$JOBENV"
15242         fi
15243
15244         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
15245         JOBENV="JOBCOMPLEX"
15246         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
15247
15248         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
15249 }
15250 run_test 205a "Verify job stats"
15251
15252 # LU-13117
15253 test_205b() {
15254         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
15255         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
15256         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
15257                 grep job_id: | grep foolish &&
15258                         error "Unexpected jobid found"
15259         true
15260 }
15261 run_test 205b "Verify job stats jobid parsing"
15262
15263 # LU-1480, LU-1773 and LU-1657
15264 test_206() {
15265         mkdir -p $DIR/$tdir
15266         $SETSTRIPE -c -1 $DIR/$tdir
15267 #define OBD_FAIL_LOV_INIT 0x1403
15268         $LCTL set_param fail_loc=0xa0001403
15269         $LCTL set_param fail_val=1
15270         touch $DIR/$tdir/$tfile || true
15271 }
15272 run_test 206 "fail lov_init_raid0() doesn't lbug"
15273
15274 test_207a() {
15275         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15276         local fsz=`stat -c %s $DIR/$tfile`
15277         cancel_lru_locks mdc
15278
15279         # do not return layout in getattr intent
15280 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
15281         $LCTL set_param fail_loc=0x170
15282         local sz=`stat -c %s $DIR/$tfile`
15283
15284         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
15285
15286         rm -rf $DIR/$tfile
15287 }
15288 run_test 207a "can refresh layout at glimpse"
15289
15290 test_207b() {
15291         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
15292         local cksum=`md5sum $DIR/$tfile`
15293         local fsz=`stat -c %s $DIR/$tfile`
15294         cancel_lru_locks mdc
15295         cancel_lru_locks osc
15296
15297         # do not return layout in getattr intent
15298 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
15299         $LCTL set_param fail_loc=0x171
15300
15301         # it will refresh layout after the file is opened but before read issues
15302         echo checksum is "$cksum"
15303         echo "$cksum" |md5sum -c --quiet || error "file differs"
15304
15305         rm -rf $DIR/$tfile
15306 }
15307 run_test 207b "can refresh layout at open"
15308
15309 test_208() {
15310         # FIXME: in this test suite, only RD lease is used. This is okay
15311         # for now as only exclusive open is supported. After generic lease
15312         # is done, this test suite should be revised. - Jinshan
15313
15314         remote_mds_nodsh && skip "remote MDS with nodsh"
15315         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
15316                 skip "Need MDS version at least 2.4.52"
15317
15318         echo "==== test 1: verify get lease work"
15319         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
15320
15321         echo "==== test 2: verify lease can be broken by upcoming open"
15322         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15323         local PID=$!
15324         sleep 1
15325
15326         $MULTIOP $DIR/$tfile oO_RDONLY:c
15327         kill -USR1 $PID && wait $PID || error "break lease error"
15328
15329         echo "==== test 3: verify lease can't be granted if an open already exists"
15330         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
15331         local PID=$!
15332         sleep 1
15333
15334         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
15335         kill -USR1 $PID && wait $PID || error "open file error"
15336
15337         echo "==== test 4: lease can sustain over recovery"
15338         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
15339         PID=$!
15340         sleep 1
15341
15342         fail mds1
15343
15344         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
15345
15346         echo "==== test 5: lease broken can't be regained by replay"
15347         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
15348         PID=$!
15349         sleep 1
15350
15351         # open file to break lease and then recovery
15352         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
15353         fail mds1
15354
15355         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15356
15357         rm -f $DIR/$tfile
15358 }
15359 run_test 208 "Exclusive open"
15360
15361 test_209() {
15362         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15363                 skip_env "must have disp_stripe"
15364
15365         touch $DIR/$tfile
15366         sync; sleep 5; sync;
15367
15368         echo 3 > /proc/sys/vm/drop_caches
15369         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15370
15371         # open/close 500 times
15372         for i in $(seq 500); do
15373                 cat $DIR/$tfile
15374         done
15375
15376         echo 3 > /proc/sys/vm/drop_caches
15377         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15378
15379         echo "before: $req_before, after: $req_after"
15380         [ $((req_after - req_before)) -ge 300 ] &&
15381                 error "open/close requests are not freed"
15382         return 0
15383 }
15384 run_test 209 "read-only open/close requests should be freed promptly"
15385
15386 test_212() {
15387         size=`date +%s`
15388         size=$((size % 8192 + 1))
15389         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15390         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15391         rm -f $DIR/f212 $DIR/f212.xyz
15392 }
15393 run_test 212 "Sendfile test ============================================"
15394
15395 test_213() {
15396         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15397         cancel_lru_locks osc
15398         lctl set_param fail_loc=0x8000040f
15399         # generate a read lock
15400         cat $DIR/$tfile > /dev/null
15401         # write to the file, it will try to cancel the above read lock.
15402         cat /etc/hosts >> $DIR/$tfile
15403 }
15404 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15405
15406 test_214() { # for bug 20133
15407         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15408         for (( i=0; i < 340; i++ )) ; do
15409                 touch $DIR/$tdir/d214c/a$i
15410         done
15411
15412         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15413         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15414         ls $DIR/d214c || error "ls $DIR/d214c failed"
15415         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15416         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15417 }
15418 run_test 214 "hash-indexed directory test - bug 20133"
15419
15420 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15421 create_lnet_proc_files() {
15422         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15423 }
15424
15425 # counterpart of create_lnet_proc_files
15426 remove_lnet_proc_files() {
15427         rm -f $TMP/lnet_$1.sys
15428 }
15429
15430 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15431 # 3rd arg as regexp for body
15432 check_lnet_proc_stats() {
15433         local l=$(cat "$TMP/lnet_$1" |wc -l)
15434         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15435
15436         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15437 }
15438
15439 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15440 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15441 # optional and can be regexp for 2nd line (lnet.routes case)
15442 check_lnet_proc_entry() {
15443         local blp=2          # blp stands for 'position of 1st line of body'
15444         [ -z "$5" ] || blp=3 # lnet.routes case
15445
15446         local l=$(cat "$TMP/lnet_$1" |wc -l)
15447         # subtracting one from $blp because the body can be empty
15448         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15449
15450         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15451                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15452
15453         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15454                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15455
15456         # bail out if any unexpected line happened
15457         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15458         [ "$?" != 0 ] || error "$2 misformatted"
15459 }
15460
15461 test_215() { # for bugs 18102, 21079, 21517
15462         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15463
15464         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15465         local P='[1-9][0-9]*'           # positive numeric
15466         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15467         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15468         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15469         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15470
15471         local L1 # regexp for 1st line
15472         local L2 # regexp for 2nd line (optional)
15473         local BR # regexp for the rest (body)
15474
15475         # lnet.stats should look as 11 space-separated non-negative numerics
15476         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15477         create_lnet_proc_files "stats"
15478         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15479         remove_lnet_proc_files "stats"
15480
15481         # lnet.routes should look like this:
15482         # Routing disabled/enabled
15483         # net hops priority state router
15484         # where net is a string like tcp0, hops > 0, priority >= 0,
15485         # state is up/down,
15486         # router is a string like 192.168.1.1@tcp2
15487         L1="^Routing (disabled|enabled)$"
15488         L2="^net +hops +priority +state +router$"
15489         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15490         create_lnet_proc_files "routes"
15491         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15492         remove_lnet_proc_files "routes"
15493
15494         # lnet.routers should look like this:
15495         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15496         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15497         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15498         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15499         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
15500         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
15501         create_lnet_proc_files "routers"
15502         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15503         remove_lnet_proc_files "routers"
15504
15505         # lnet.peers should look like this:
15506         # nid refs state last max rtr min tx min queue
15507         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15508         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15509         # numeric (0 or >0 or <0), queue >= 0.
15510         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15511         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15512         create_lnet_proc_files "peers"
15513         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15514         remove_lnet_proc_files "peers"
15515
15516         # lnet.buffers  should look like this:
15517         # pages count credits min
15518         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15519         L1="^pages +count +credits +min$"
15520         BR="^ +$N +$N +$I +$I$"
15521         create_lnet_proc_files "buffers"
15522         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15523         remove_lnet_proc_files "buffers"
15524
15525         # lnet.nis should look like this:
15526         # nid status alive refs peer rtr max tx min
15527         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15528         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15529         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15530         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15531         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15532         create_lnet_proc_files "nis"
15533         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15534         remove_lnet_proc_files "nis"
15535
15536         # can we successfully write to lnet.stats?
15537         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15538 }
15539 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15540
15541 test_216() { # bug 20317
15542         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15543         remote_ost_nodsh && skip "remote OST with nodsh"
15544
15545         local node
15546         local facets=$(get_facets OST)
15547         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15548
15549         save_lustre_params client "osc.*.contention_seconds" > $p
15550         save_lustre_params $facets \
15551                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15552         save_lustre_params $facets \
15553                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15554         save_lustre_params $facets \
15555                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15556         clear_stats osc.*.osc_stats
15557
15558         # agressive lockless i/o settings
15559         do_nodes $(comma_list $(osts_nodes)) \
15560                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15561                         ldlm.namespaces.filter-*.contended_locks=0 \
15562                         ldlm.namespaces.filter-*.contention_seconds=60"
15563         lctl set_param -n osc.*.contention_seconds=60
15564
15565         $DIRECTIO write $DIR/$tfile 0 10 4096
15566         $CHECKSTAT -s 40960 $DIR/$tfile
15567
15568         # disable lockless i/o
15569         do_nodes $(comma_list $(osts_nodes)) \
15570                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15571                         ldlm.namespaces.filter-*.contended_locks=32 \
15572                         ldlm.namespaces.filter-*.contention_seconds=0"
15573         lctl set_param -n osc.*.contention_seconds=0
15574         clear_stats osc.*.osc_stats
15575
15576         dd if=/dev/zero of=$DIR/$tfile count=0
15577         $CHECKSTAT -s 0 $DIR/$tfile
15578
15579         restore_lustre_params <$p
15580         rm -f $p
15581         rm $DIR/$tfile
15582 }
15583 run_test 216 "check lockless direct write updates file size and kms correctly"
15584
15585 test_217() { # bug 22430
15586         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15587
15588         local node
15589         local nid
15590
15591         for node in $(nodes_list); do
15592                 nid=$(host_nids_address $node $NETTYPE)
15593                 if [[ $nid = *-* ]] ; then
15594                         echo "lctl ping $(h2nettype $nid)"
15595                         lctl ping $(h2nettype $nid)
15596                 else
15597                         echo "skipping $node (no hyphen detected)"
15598                 fi
15599         done
15600 }
15601 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
15602
15603 test_218() {
15604        # do directio so as not to populate the page cache
15605        log "creating a 10 Mb file"
15606        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15607        log "starting reads"
15608        dd if=$DIR/$tfile of=/dev/null bs=4096 &
15609        log "truncating the file"
15610        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15611        log "killing dd"
15612        kill %+ || true # reads might have finished
15613        echo "wait until dd is finished"
15614        wait
15615        log "removing the temporary file"
15616        rm -rf $DIR/$tfile || error "tmp file removal failed"
15617 }
15618 run_test 218 "parallel read and truncate should not deadlock"
15619
15620 test_219() {
15621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15622
15623         # write one partial page
15624         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
15625         # set no grant so vvp_io_commit_write will do sync write
15626         $LCTL set_param fail_loc=0x411
15627         # write a full page at the end of file
15628         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
15629
15630         $LCTL set_param fail_loc=0
15631         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
15632         $LCTL set_param fail_loc=0x411
15633         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
15634
15635         # LU-4201
15636         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
15637         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
15638 }
15639 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
15640
15641 test_220() { #LU-325
15642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15643         remote_ost_nodsh && skip "remote OST with nodsh"
15644         remote_mds_nodsh && skip "remote MDS with nodsh"
15645         remote_mgs_nodsh && skip "remote MGS with nodsh"
15646
15647         local OSTIDX=0
15648
15649         # create on MDT0000 so the last_id and next_id are correct
15650         mkdir $DIR/$tdir
15651         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
15652         OST=${OST%_UUID}
15653
15654         # on the mdt's osc
15655         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
15656         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
15657                         osp.$mdtosc_proc1.prealloc_last_id)
15658         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
15659                         osp.$mdtosc_proc1.prealloc_next_id)
15660
15661         $LFS df -i
15662
15663         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
15664         #define OBD_FAIL_OST_ENOINO              0x229
15665         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
15666         create_pool $FSNAME.$TESTNAME || return 1
15667         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
15668
15669         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
15670
15671         MDSOBJS=$((last_id - next_id))
15672         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
15673
15674         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
15675         echo "OST still has $count kbytes free"
15676
15677         echo "create $MDSOBJS files @next_id..."
15678         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
15679
15680         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15681                         osp.$mdtosc_proc1.prealloc_last_id)
15682         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15683                         osp.$mdtosc_proc1.prealloc_next_id)
15684
15685         echo "after creation, last_id=$last_id2, next_id=$next_id2"
15686         $LFS df -i
15687
15688         echo "cleanup..."
15689
15690         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
15691         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
15692
15693         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
15694                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
15695         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15696                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
15697         echo "unlink $MDSOBJS files @$next_id..."
15698         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
15699 }
15700 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
15701
15702 test_221() {
15703         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15704
15705         dd if=`which date` of=$MOUNT/date oflag=sync
15706         chmod +x $MOUNT/date
15707
15708         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
15709         $LCTL set_param fail_loc=0x80001401
15710
15711         $MOUNT/date > /dev/null
15712         rm -f $MOUNT/date
15713 }
15714 run_test 221 "make sure fault and truncate race to not cause OOM"
15715
15716 test_222a () {
15717         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15718
15719         rm -rf $DIR/$tdir
15720         test_mkdir $DIR/$tdir
15721         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15722         createmany -o $DIR/$tdir/$tfile 10
15723         cancel_lru_locks mdc
15724         cancel_lru_locks osc
15725         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15726         $LCTL set_param fail_loc=0x31a
15727         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
15728         $LCTL set_param fail_loc=0
15729         rm -r $DIR/$tdir
15730 }
15731 run_test 222a "AGL for ls should not trigger CLIO lock failure"
15732
15733 test_222b () {
15734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15735
15736         rm -rf $DIR/$tdir
15737         test_mkdir $DIR/$tdir
15738         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15739         createmany -o $DIR/$tdir/$tfile 10
15740         cancel_lru_locks mdc
15741         cancel_lru_locks osc
15742         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15743         $LCTL set_param fail_loc=0x31a
15744         rm -r $DIR/$tdir || error "AGL for rmdir failed"
15745         $LCTL set_param fail_loc=0
15746 }
15747 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
15748
15749 test_223 () {
15750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15751
15752         rm -rf $DIR/$tdir
15753         test_mkdir $DIR/$tdir
15754         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15755         createmany -o $DIR/$tdir/$tfile 10
15756         cancel_lru_locks mdc
15757         cancel_lru_locks osc
15758         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
15759         $LCTL set_param fail_loc=0x31b
15760         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
15761         $LCTL set_param fail_loc=0
15762         rm -r $DIR/$tdir
15763 }
15764 run_test 223 "osc reenqueue if without AGL lock granted ======================="
15765
15766 test_224a() { # LU-1039, MRP-303
15767         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15768
15769         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
15770         $LCTL set_param fail_loc=0x508
15771         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
15772         $LCTL set_param fail_loc=0
15773         df $DIR
15774 }
15775 run_test 224a "Don't panic on bulk IO failure"
15776
15777 test_224b() { # LU-1039, MRP-303
15778         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15779
15780         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
15781         cancel_lru_locks osc
15782         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
15783         $LCTL set_param fail_loc=0x515
15784         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
15785         $LCTL set_param fail_loc=0
15786         df $DIR
15787 }
15788 run_test 224b "Don't panic on bulk IO failure"
15789
15790 test_224c() { # LU-6441
15791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15792         remote_mds_nodsh && skip "remote MDS with nodsh"
15793
15794         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15795         save_writethrough $p
15796         set_cache writethrough on
15797
15798         local pages_per_rpc=$($LCTL get_param \
15799                                 osc.*.max_pages_per_rpc)
15800         local at_max=$($LCTL get_param -n at_max)
15801         local timeout=$($LCTL get_param -n timeout)
15802         local test_at="at_max"
15803         local param_at="$FSNAME.sys.at_max"
15804         local test_timeout="timeout"
15805         local param_timeout="$FSNAME.sys.timeout"
15806
15807         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
15808
15809         set_persistent_param_and_check client "$test_at" "$param_at" 0
15810         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
15811
15812         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
15813         do_facet ost1 $LCTL set_param fail_loc=0x520
15814         $LFS setstripe -c 1 -i 0 $DIR/$tfile
15815         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
15816         sync
15817         do_facet ost1 $LCTL set_param fail_loc=0
15818
15819         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
15820         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
15821                 $timeout
15822
15823         $LCTL set_param -n $pages_per_rpc
15824         restore_lustre_params < $p
15825         rm -f $p
15826 }
15827 run_test 224c "Don't hang if one of md lost during large bulk RPC"
15828
15829 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
15830 test_225a () {
15831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15832         if [ -z ${MDSSURVEY} ]; then
15833                 skip_env "mds-survey not found"
15834         fi
15835         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15836                 skip "Need MDS version at least 2.2.51"
15837
15838         local mds=$(facet_host $SINGLEMDS)
15839         local target=$(do_nodes $mds 'lctl dl' |
15840                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15841
15842         local cmd1="file_count=1000 thrhi=4"
15843         local cmd2="dir_count=2 layer=mdd stripe_count=0"
15844         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15845         local cmd="$cmd1 $cmd2 $cmd3"
15846
15847         rm -f ${TMP}/mds_survey*
15848         echo + $cmd
15849         eval $cmd || error "mds-survey with zero-stripe failed"
15850         cat ${TMP}/mds_survey*
15851         rm -f ${TMP}/mds_survey*
15852 }
15853 run_test 225a "Metadata survey sanity with zero-stripe"
15854
15855 test_225b () {
15856         if [ -z ${MDSSURVEY} ]; then
15857                 skip_env "mds-survey not found"
15858         fi
15859         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15860                 skip "Need MDS version at least 2.2.51"
15861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15862         remote_mds_nodsh && skip "remote MDS with nodsh"
15863         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
15864                 skip_env "Need to mount OST to test"
15865         fi
15866
15867         local mds=$(facet_host $SINGLEMDS)
15868         local target=$(do_nodes $mds 'lctl dl' |
15869                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15870
15871         local cmd1="file_count=1000 thrhi=4"
15872         local cmd2="dir_count=2 layer=mdd stripe_count=1"
15873         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15874         local cmd="$cmd1 $cmd2 $cmd3"
15875
15876         rm -f ${TMP}/mds_survey*
15877         echo + $cmd
15878         eval $cmd || error "mds-survey with stripe_count failed"
15879         cat ${TMP}/mds_survey*
15880         rm -f ${TMP}/mds_survey*
15881 }
15882 run_test 225b "Metadata survey sanity with stripe_count = 1"
15883
15884 mcreate_path2fid () {
15885         local mode=$1
15886         local major=$2
15887         local minor=$3
15888         local name=$4
15889         local desc=$5
15890         local path=$DIR/$tdir/$name
15891         local fid
15892         local rc
15893         local fid_path
15894
15895         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
15896                 error "cannot create $desc"
15897
15898         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
15899         rc=$?
15900         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
15901
15902         fid_path=$($LFS fid2path $MOUNT $fid)
15903         rc=$?
15904         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
15905
15906         [ "$path" == "$fid_path" ] ||
15907                 error "fid2path returned $fid_path, expected $path"
15908
15909         echo "pass with $path and $fid"
15910 }
15911
15912 test_226a () {
15913         rm -rf $DIR/$tdir
15914         mkdir -p $DIR/$tdir
15915
15916         mcreate_path2fid 0010666 0 0 fifo "FIFO"
15917         mcreate_path2fid 0020666 1 3 null "character special file (null)"
15918         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
15919         mcreate_path2fid 0040666 0 0 dir "directory"
15920         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
15921         mcreate_path2fid 0100666 0 0 file "regular file"
15922         mcreate_path2fid 0120666 0 0 link "symbolic link"
15923         mcreate_path2fid 0140666 0 0 sock "socket"
15924 }
15925 run_test 226a "call path2fid and fid2path on files of all type"
15926
15927 test_226b () {
15928         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15929
15930         local MDTIDX=1
15931
15932         rm -rf $DIR/$tdir
15933         mkdir -p $DIR/$tdir
15934         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
15935                 error "create remote directory failed"
15936         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
15937         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
15938                                 "character special file (null)"
15939         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
15940                                 "character special file (no device)"
15941         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
15942         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
15943                                 "block special file (loop)"
15944         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
15945         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
15946         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
15947 }
15948 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
15949
15950 # LU-1299 Executing or running ldd on a truncated executable does not
15951 # cause an out-of-memory condition.
15952 test_227() {
15953         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15954         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
15955
15956         dd if=$(which date) of=$MOUNT/date bs=1k count=1
15957         chmod +x $MOUNT/date
15958
15959         $MOUNT/date > /dev/null
15960         ldd $MOUNT/date > /dev/null
15961         rm -f $MOUNT/date
15962 }
15963 run_test 227 "running truncated executable does not cause OOM"
15964
15965 # LU-1512 try to reuse idle OI blocks
15966 test_228a() {
15967         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15968         remote_mds_nodsh && skip "remote MDS with nodsh"
15969         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15970
15971         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15972         local myDIR=$DIR/$tdir
15973
15974         mkdir -p $myDIR
15975         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15976         $LCTL set_param fail_loc=0x80001002
15977         createmany -o $myDIR/t- 10000
15978         $LCTL set_param fail_loc=0
15979         # The guard is current the largest FID holder
15980         touch $myDIR/guard
15981         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15982                     tr -d '[')
15983         local IDX=$(($SEQ % 64))
15984
15985         do_facet $SINGLEMDS sync
15986         # Make sure journal flushed.
15987         sleep 6
15988         local blk1=$(do_facet $SINGLEMDS \
15989                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15990                      grep Blockcount | awk '{print $4}')
15991
15992         # Remove old files, some OI blocks will become idle.
15993         unlinkmany $myDIR/t- 10000
15994         # Create new files, idle OI blocks should be reused.
15995         createmany -o $myDIR/t- 2000
15996         do_facet $SINGLEMDS sync
15997         # Make sure journal flushed.
15998         sleep 6
15999         local blk2=$(do_facet $SINGLEMDS \
16000                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16001                      grep Blockcount | awk '{print $4}')
16002
16003         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16004 }
16005 run_test 228a "try to reuse idle OI blocks"
16006
16007 test_228b() {
16008         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16009         remote_mds_nodsh && skip "remote MDS with nodsh"
16010         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16011
16012         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16013         local myDIR=$DIR/$tdir
16014
16015         mkdir -p $myDIR
16016         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16017         $LCTL set_param fail_loc=0x80001002
16018         createmany -o $myDIR/t- 10000
16019         $LCTL set_param fail_loc=0
16020         # The guard is current the largest FID holder
16021         touch $myDIR/guard
16022         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16023                     tr -d '[')
16024         local IDX=$(($SEQ % 64))
16025
16026         do_facet $SINGLEMDS sync
16027         # Make sure journal flushed.
16028         sleep 6
16029         local blk1=$(do_facet $SINGLEMDS \
16030                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16031                      grep Blockcount | awk '{print $4}')
16032
16033         # Remove old files, some OI blocks will become idle.
16034         unlinkmany $myDIR/t- 10000
16035
16036         # stop the MDT
16037         stop $SINGLEMDS || error "Fail to stop MDT."
16038         # remount the MDT
16039         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
16040
16041         df $MOUNT || error "Fail to df."
16042         # Create new files, idle OI blocks should be reused.
16043         createmany -o $myDIR/t- 2000
16044         do_facet $SINGLEMDS sync
16045         # Make sure journal flushed.
16046         sleep 6
16047         local blk2=$(do_facet $SINGLEMDS \
16048                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16049                      grep Blockcount | awk '{print $4}')
16050
16051         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16052 }
16053 run_test 228b "idle OI blocks can be reused after MDT restart"
16054
16055 #LU-1881
16056 test_228c() {
16057         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16058         remote_mds_nodsh && skip "remote MDS with nodsh"
16059         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
16060
16061         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
16062         local myDIR=$DIR/$tdir
16063
16064         mkdir -p $myDIR
16065         #define OBD_FAIL_SEQ_EXHAUST             0x1002
16066         $LCTL set_param fail_loc=0x80001002
16067         # 20000 files can guarantee there are index nodes in the OI file
16068         createmany -o $myDIR/t- 20000
16069         $LCTL set_param fail_loc=0
16070         # The guard is current the largest FID holder
16071         touch $myDIR/guard
16072         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
16073                     tr -d '[')
16074         local IDX=$(($SEQ % 64))
16075
16076         do_facet $SINGLEMDS sync
16077         # Make sure journal flushed.
16078         sleep 6
16079         local blk1=$(do_facet $SINGLEMDS \
16080                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16081                      grep Blockcount | awk '{print $4}')
16082
16083         # Remove old files, some OI blocks will become idle.
16084         unlinkmany $myDIR/t- 20000
16085         rm -f $myDIR/guard
16086         # The OI file should become empty now
16087
16088         # Create new files, idle OI blocks should be reused.
16089         createmany -o $myDIR/t- 2000
16090         do_facet $SINGLEMDS sync
16091         # Make sure journal flushed.
16092         sleep 6
16093         local blk2=$(do_facet $SINGLEMDS \
16094                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
16095                      grep Blockcount | awk '{print $4}')
16096
16097         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
16098 }
16099 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
16100
16101 test_229() { # LU-2482, LU-3448
16102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16103         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
16104         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
16105                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
16106
16107         rm -f $DIR/$tfile
16108
16109         # Create a file with a released layout and stripe count 2.
16110         $MULTIOP $DIR/$tfile H2c ||
16111                 error "failed to create file with released layout"
16112
16113         $GETSTRIPE -v $DIR/$tfile
16114
16115         local pattern=$($GETSTRIPE -L $DIR/$tfile)
16116         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
16117
16118         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
16119         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
16120         stat $DIR/$tfile || error "failed to stat released file"
16121
16122         chown $RUNAS_ID $DIR/$tfile ||
16123                 error "chown $RUNAS_ID $DIR/$tfile failed"
16124
16125         chgrp $RUNAS_ID $DIR/$tfile ||
16126                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
16127
16128         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
16129         rm $DIR/$tfile || error "failed to remove released file"
16130 }
16131 run_test 229 "getstripe/stat/rm/attr changes work on released files"
16132
16133 test_230a() {
16134         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16135         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16136         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16137                 skip "Need MDS version at least 2.11.52"
16138
16139         local MDTIDX=1
16140
16141         test_mkdir $DIR/$tdir
16142         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
16143         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
16144         [ $mdt_idx -ne 0 ] &&
16145                 error "create local directory on wrong MDT $mdt_idx"
16146
16147         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
16148                         error "create remote directory failed"
16149         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
16150         [ $mdt_idx -ne $MDTIDX ] &&
16151                 error "create remote directory on wrong MDT $mdt_idx"
16152
16153         createmany -o $DIR/$tdir/test_230/t- 10 ||
16154                 error "create files on remote directory failed"
16155         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
16156         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
16157         rm -r $DIR/$tdir || error "unlink remote directory failed"
16158 }
16159 run_test 230a "Create remote directory and files under the remote directory"
16160
16161 test_230b() {
16162         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16163         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16164         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16165                 skip "Need MDS version at least 2.11.52"
16166
16167         local MDTIDX=1
16168         local mdt_index
16169         local i
16170         local file
16171         local pid
16172         local stripe_count
16173         local migrate_dir=$DIR/$tdir/migrate_dir
16174         local other_dir=$DIR/$tdir/other_dir
16175
16176         test_mkdir $DIR/$tdir
16177         test_mkdir -i0 -c1 $migrate_dir
16178         test_mkdir -i0 -c1 $other_dir
16179         for ((i=0; i<10; i++)); do
16180                 mkdir -p $migrate_dir/dir_${i}
16181                 createmany -o $migrate_dir/dir_${i}/f 10 ||
16182                         error "create files under remote dir failed $i"
16183         done
16184
16185         cp /etc/passwd $migrate_dir/$tfile
16186         cp /etc/passwd $other_dir/$tfile
16187         chattr +SAD $migrate_dir
16188         chattr +SAD $migrate_dir/$tfile
16189
16190         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16191         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16192         local old_dir_mode=$(stat -c%f $migrate_dir)
16193         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
16194
16195         mkdir -p $migrate_dir/dir_default_stripe2
16196         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
16197         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
16198
16199         mkdir -p $other_dir
16200         ln $migrate_dir/$tfile $other_dir/luna
16201         ln $migrate_dir/$tfile $migrate_dir/sofia
16202         ln $other_dir/$tfile $migrate_dir/david
16203         ln -s $migrate_dir/$tfile $other_dir/zachary
16204         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
16205         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
16206
16207         local len
16208         local lnktgt
16209
16210         # inline symlink
16211         for len in 58 59 60; do
16212                 lnktgt=$(str_repeat 'l' $len)
16213                 touch $migrate_dir/$lnktgt
16214                 ln -s $lnktgt $migrate_dir/${len}char_ln
16215         done
16216
16217         # PATH_MAX
16218         for len in 4094 4095; do
16219                 lnktgt=$(str_repeat 'l' $len)
16220                 ln -s $lnktgt $migrate_dir/${len}char_ln
16221         done
16222
16223         # NAME_MAX
16224         for len in 254 255; do
16225                 touch $migrate_dir/$(str_repeat 'l' $len)
16226         done
16227
16228         $LFS migrate -m $MDTIDX $migrate_dir ||
16229                 error "fails on migrating remote dir to MDT1"
16230
16231         echo "migratate to MDT1, then checking.."
16232         for ((i = 0; i < 10; i++)); do
16233                 for file in $(find $migrate_dir/dir_${i}); do
16234                         mdt_index=$($LFS getstripe -m $file)
16235                         # broken symlink getstripe will fail
16236                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16237                                 error "$file is not on MDT${MDTIDX}"
16238                 done
16239         done
16240
16241         # the multiple link file should still in MDT0
16242         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
16243         [ $mdt_index == 0 ] ||
16244                 error "$file is not on MDT${MDTIDX}"
16245
16246         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16247         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16248                 error " expect $old_dir_flag get $new_dir_flag"
16249
16250         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16251         [ "$old_file_flag" = "$new_file_flag" ] ||
16252                 error " expect $old_file_flag get $new_file_flag"
16253
16254         local new_dir_mode=$(stat -c%f $migrate_dir)
16255         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16256                 error "expect mode $old_dir_mode get $new_dir_mode"
16257
16258         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16259         [ "$old_file_mode" = "$new_file_mode" ] ||
16260                 error "expect mode $old_file_mode get $new_file_mode"
16261
16262         diff /etc/passwd $migrate_dir/$tfile ||
16263                 error "$tfile different after migration"
16264
16265         diff /etc/passwd $other_dir/luna ||
16266                 error "luna different after migration"
16267
16268         diff /etc/passwd $migrate_dir/sofia ||
16269                 error "sofia different after migration"
16270
16271         diff /etc/passwd $migrate_dir/david ||
16272                 error "david different after migration"
16273
16274         diff /etc/passwd $other_dir/zachary ||
16275                 error "zachary different after migration"
16276
16277         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16278                 error "${tfile}_ln different after migration"
16279
16280         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16281                 error "${tfile}_ln_other different after migration"
16282
16283         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
16284         [ $stripe_count = 2 ] ||
16285                 error "dir strpe_count $d != 2 after migration."
16286
16287         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
16288         [ $stripe_count = 2 ] ||
16289                 error "file strpe_count $d != 2 after migration."
16290
16291         #migrate back to MDT0
16292         MDTIDX=0
16293
16294         $LFS migrate -m $MDTIDX $migrate_dir ||
16295                 error "fails on migrating remote dir to MDT0"
16296
16297         echo "migrate back to MDT0, checking.."
16298         for file in $(find $migrate_dir); do
16299                 mdt_index=$($LFS getstripe -m $file)
16300                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
16301                         error "$file is not on MDT${MDTIDX}"
16302         done
16303
16304         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
16305         [ "$old_dir_flag" = "$new_dir_flag" ] ||
16306                 error " expect $old_dir_flag get $new_dir_flag"
16307
16308         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
16309         [ "$old_file_flag" = "$new_file_flag" ] ||
16310                 error " expect $old_file_flag get $new_file_flag"
16311
16312         local new_dir_mode=$(stat -c%f $migrate_dir)
16313         [ "$old_dir_mode" = "$new_dir_mode" ] ||
16314                 error "expect mode $old_dir_mode get $new_dir_mode"
16315
16316         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
16317         [ "$old_file_mode" = "$new_file_mode" ] ||
16318                 error "expect mode $old_file_mode get $new_file_mode"
16319
16320         diff /etc/passwd ${migrate_dir}/$tfile ||
16321                 error "$tfile different after migration"
16322
16323         diff /etc/passwd ${other_dir}/luna ||
16324                 error "luna different after migration"
16325
16326         diff /etc/passwd ${migrate_dir}/sofia ||
16327                 error "sofia different after migration"
16328
16329         diff /etc/passwd ${other_dir}/zachary ||
16330                 error "zachary different after migration"
16331
16332         diff /etc/passwd $migrate_dir/${tfile}_ln ||
16333                 error "${tfile}_ln different after migration"
16334
16335         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
16336                 error "${tfile}_ln_other different after migration"
16337
16338         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
16339         [ $stripe_count = 2 ] ||
16340                 error "dir strpe_count $d != 2 after migration."
16341
16342         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
16343         [ $stripe_count = 2 ] ||
16344                 error "file strpe_count $d != 2 after migration."
16345
16346         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16347 }
16348 run_test 230b "migrate directory"
16349
16350 test_230c() {
16351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16352         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16353         remote_mds_nodsh && skip "remote MDS with nodsh"
16354         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16355                 skip "Need MDS version at least 2.11.52"
16356
16357         local MDTIDX=1
16358         local total=3
16359         local mdt_index
16360         local file
16361         local migrate_dir=$DIR/$tdir/migrate_dir
16362
16363         #If migrating directory fails in the middle, all entries of
16364         #the directory is still accessiable.
16365         test_mkdir $DIR/$tdir
16366         test_mkdir -i0 -c1 $migrate_dir
16367         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16368         stat $migrate_dir
16369         createmany -o $migrate_dir/f $total ||
16370                 error "create files under ${migrate_dir} failed"
16371
16372         # fail after migrating top dir, and this will fail only once, so the
16373         # first sub file migration will fail (currently f3), others succeed.
16374         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16375         do_facet mds1 lctl set_param fail_loc=0x1801
16376         local t=$(ls $migrate_dir | wc -l)
16377         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16378                 error "migrate should fail"
16379         local u=$(ls $migrate_dir | wc -l)
16380         [ "$u" == "$t" ] || error "$u != $t during migration"
16381
16382         # add new dir/file should succeed
16383         mkdir $migrate_dir/dir ||
16384                 error "mkdir failed under migrating directory"
16385         touch $migrate_dir/file ||
16386                 error "create file failed under migrating directory"
16387
16388         # add file with existing name should fail
16389         for file in $migrate_dir/f*; do
16390                 stat $file > /dev/null || error "stat $file failed"
16391                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16392                         error "open(O_CREAT|O_EXCL) $file should fail"
16393                 $MULTIOP $file m && error "create $file should fail"
16394                 touch $DIR/$tdir/remote_dir/$tfile ||
16395                         error "touch $tfile failed"
16396                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16397                         error "link $file should fail"
16398                 mdt_index=$($LFS getstripe -m $file)
16399                 if [ $mdt_index == 0 ]; then
16400                         # file failed to migrate is not allowed to rename to
16401                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16402                                 error "rename to $file should fail"
16403                 else
16404                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16405                                 error "rename to $file failed"
16406                 fi
16407                 echo hello >> $file || error "write $file failed"
16408         done
16409
16410         # resume migration with different options should fail
16411         $LFS migrate -m 0 $migrate_dir &&
16412                 error "migrate -m 0 $migrate_dir should fail"
16413
16414         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16415                 error "migrate -c 2 $migrate_dir should fail"
16416
16417         # resume migration should succeed
16418         $LFS migrate -m $MDTIDX $migrate_dir ||
16419                 error "migrate $migrate_dir failed"
16420
16421         echo "Finish migration, then checking.."
16422         for file in $(find $migrate_dir); do
16423                 mdt_index=$($LFS getstripe -m $file)
16424                 [ $mdt_index == $MDTIDX ] ||
16425                         error "$file is not on MDT${MDTIDX}"
16426         done
16427
16428         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16429 }
16430 run_test 230c "check directory accessiblity if migration failed"
16431
16432 test_230d() {
16433         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16434         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16435         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16436                 skip "Need MDS version at least 2.11.52"
16437         # LU-11235
16438         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16439
16440         local migrate_dir=$DIR/$tdir/migrate_dir
16441         local old_index
16442         local new_index
16443         local old_count
16444         local new_count
16445         local new_hash
16446         local mdt_index
16447         local i
16448         local j
16449
16450         old_index=$((RANDOM % MDSCOUNT))
16451         old_count=$((MDSCOUNT - old_index))
16452         new_index=$((RANDOM % MDSCOUNT))
16453         new_count=$((MDSCOUNT - new_index))
16454         new_hash="all_char"
16455
16456         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16457         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16458
16459         test_mkdir $DIR/$tdir
16460         test_mkdir -i $old_index -c $old_count $migrate_dir
16461
16462         for ((i=0; i<100; i++)); do
16463                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16464                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16465                         error "create files under remote dir failed $i"
16466         done
16467
16468         echo -n "Migrate from MDT$old_index "
16469         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16470         echo -n "to MDT$new_index"
16471         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16472         echo
16473
16474         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16475         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16476                 error "migrate remote dir error"
16477
16478         echo "Finish migration, then checking.."
16479         for file in $(find $migrate_dir); do
16480                 mdt_index=$($LFS getstripe -m $file)
16481                 if [ $mdt_index -lt $new_index ] ||
16482                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16483                         error "$file is on MDT$mdt_index"
16484                 fi
16485         done
16486
16487         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16488 }
16489 run_test 230d "check migrate big directory"
16490
16491 test_230e() {
16492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16493         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16494         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16495                 skip "Need MDS version at least 2.11.52"
16496
16497         local i
16498         local j
16499         local a_fid
16500         local b_fid
16501
16502         mkdir -p $DIR/$tdir
16503         mkdir $DIR/$tdir/migrate_dir
16504         mkdir $DIR/$tdir/other_dir
16505         touch $DIR/$tdir/migrate_dir/a
16506         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16507         ls $DIR/$tdir/other_dir
16508
16509         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16510                 error "migrate dir fails"
16511
16512         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16513         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16514
16515         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16516         [ $mdt_index == 0 ] || error "a is not on MDT0"
16517
16518         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16519                 error "migrate dir fails"
16520
16521         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16522         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16523
16524         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16525         [ $mdt_index == 1 ] || error "a is not on MDT1"
16526
16527         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16528         [ $mdt_index == 1 ] || error "b is not on MDT1"
16529
16530         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16531         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16532
16533         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16534
16535         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16536 }
16537 run_test 230e "migrate mulitple local link files"
16538
16539 test_230f() {
16540         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16541         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16542         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16543                 skip "Need MDS version at least 2.11.52"
16544
16545         local a_fid
16546         local ln_fid
16547
16548         mkdir -p $DIR/$tdir
16549         mkdir $DIR/$tdir/migrate_dir
16550         $LFS mkdir -i1 $DIR/$tdir/other_dir
16551         touch $DIR/$tdir/migrate_dir/a
16552         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16553         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16554         ls $DIR/$tdir/other_dir
16555
16556         # a should be migrated to MDT1, since no other links on MDT0
16557         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16558                 error "#1 migrate dir fails"
16559         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16560         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16561         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16562         [ $mdt_index == 1 ] || error "a is not on MDT1"
16563
16564         # a should stay on MDT1, because it is a mulitple link file
16565         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16566                 error "#2 migrate dir fails"
16567         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16568         [ $mdt_index == 1 ] || error "a is not on MDT1"
16569
16570         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16571                 error "#3 migrate dir fails"
16572
16573         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16574         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16575         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16576
16577         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16578         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16579
16580         # a should be migrated to MDT0, since no other links on MDT1
16581         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16582                 error "#4 migrate dir fails"
16583         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16584         [ $mdt_index == 0 ] || error "a is not on MDT0"
16585
16586         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16587 }
16588 run_test 230f "migrate mulitple remote link files"
16589
16590 test_230g() {
16591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16592         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16593         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16594                 skip "Need MDS version at least 2.11.52"
16595
16596         mkdir -p $DIR/$tdir/migrate_dir
16597
16598         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16599                 error "migrating dir to non-exist MDT succeeds"
16600         true
16601 }
16602 run_test 230g "migrate dir to non-exist MDT"
16603
16604 test_230h() {
16605         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16606         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16607         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16608                 skip "Need MDS version at least 2.11.52"
16609
16610         local mdt_index
16611
16612         mkdir -p $DIR/$tdir/migrate_dir
16613
16614         $LFS migrate -m1 $DIR &&
16615                 error "migrating mountpoint1 should fail"
16616
16617         $LFS migrate -m1 $DIR/$tdir/.. &&
16618                 error "migrating mountpoint2 should fail"
16619
16620         # same as mv
16621         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
16622                 error "migrating $tdir/migrate_dir/.. should fail"
16623
16624         true
16625 }
16626 run_test 230h "migrate .. and root"
16627
16628 test_230i() {
16629         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16630         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16631         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16632                 skip "Need MDS version at least 2.11.52"
16633
16634         mkdir -p $DIR/$tdir/migrate_dir
16635
16636         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
16637                 error "migration fails with a tailing slash"
16638
16639         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
16640                 error "migration fails with two tailing slashes"
16641 }
16642 run_test 230i "lfs migrate -m tolerates trailing slashes"
16643
16644 test_230j() {
16645         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16646         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16647                 skip "Need MDS version at least 2.11.52"
16648
16649         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
16650         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
16651                 error "create $tfile failed"
16652         cat /etc/passwd > $DIR/$tdir/$tfile
16653
16654         $LFS migrate -m 1 $DIR/$tdir
16655
16656         cmp /etc/passwd $DIR/$tdir/$tfile ||
16657                 error "DoM file mismatch after migration"
16658 }
16659 run_test 230j "DoM file data not changed after dir migration"
16660
16661 test_230k() {
16662         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
16663         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16664                 skip "Need MDS version at least 2.11.56"
16665
16666         local total=20
16667         local files_on_starting_mdt=0
16668
16669         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
16670         $LFS getdirstripe $DIR/$tdir
16671         for i in $(seq $total); do
16672                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
16673                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16674                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16675         done
16676
16677         echo "$files_on_starting_mdt files on MDT0"
16678
16679         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
16680         $LFS getdirstripe $DIR/$tdir
16681
16682         files_on_starting_mdt=0
16683         for i in $(seq $total); do
16684                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16685                         error "file $tfile.$i mismatch after migration"
16686                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
16687                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16688         done
16689
16690         echo "$files_on_starting_mdt files on MDT1 after migration"
16691         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
16692
16693         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
16694         $LFS getdirstripe $DIR/$tdir
16695
16696         files_on_starting_mdt=0
16697         for i in $(seq $total); do
16698                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16699                         error "file $tfile.$i mismatch after 2nd migration"
16700                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16701                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16702         done
16703
16704         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
16705         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
16706
16707         true
16708 }
16709 run_test 230k "file data not changed after dir migration"
16710
16711 test_230l() {
16712         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16713         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16714                 skip "Need MDS version at least 2.11.56"
16715
16716         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
16717         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
16718                 error "create files under remote dir failed $i"
16719         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
16720 }
16721 run_test 230l "readdir between MDTs won't crash"
16722
16723 test_230m() {
16724         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16725         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16726                 skip "Need MDS version at least 2.11.56"
16727
16728         local MDTIDX=1
16729         local mig_dir=$DIR/$tdir/migrate_dir
16730         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16731         local shortstr="b"
16732         local val
16733
16734         echo "Creating files and dirs with xattrs"
16735         test_mkdir $DIR/$tdir
16736         test_mkdir -i0 -c1 $mig_dir
16737         mkdir $mig_dir/dir
16738         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
16739                 error "cannot set xattr attr1 on dir"
16740         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
16741                 error "cannot set xattr attr2 on dir"
16742         touch $mig_dir/dir/f0
16743         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
16744                 error "cannot set xattr attr1 on file"
16745         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
16746                 error "cannot set xattr attr2 on file"
16747         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16748         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16749         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
16750         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16751         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
16752         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16753         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
16754         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16755         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
16756
16757         echo "Migrating to MDT1"
16758         $LFS migrate -m $MDTIDX $mig_dir ||
16759                 error "fails on migrating dir to MDT1"
16760
16761         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16762         echo "Checking xattrs"
16763         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16764         [ "$val" = $longstr ] ||
16765                 error "expecting xattr1 $longstr on dir, found $val"
16766         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16767         [ "$val" = $shortstr ] ||
16768                 error "expecting xattr2 $shortstr on dir, found $val"
16769         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16770         [ "$val" = $longstr ] ||
16771                 error "expecting xattr1 $longstr on file, found $val"
16772         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16773         [ "$val" = $shortstr ] ||
16774                 error "expecting xattr2 $shortstr on file, found $val"
16775 }
16776 run_test 230m "xattrs not changed after dir migration"
16777
16778 test_230r() {
16779         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16780         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
16781         [[ $MDS1_VERSION -ge $(version_code 2.12.5) ]] ||
16782                 skip "Need MDS version at least 2.12.5"
16783
16784         # maximum amount of local locks:
16785         # parent striped dir - 2 locks
16786         # new stripe in parent to migrate to - 1 lock
16787         # source and target - 2 locks
16788         # Total 5 locks for regular file
16789         mkdir -p $DIR/$tdir
16790         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
16791         touch $DIR/$tdir/dir1/eee
16792
16793         # create 4 hardlink for 4 more locks
16794         # Total: 9 locks > RS_MAX_LOCKS (8)
16795         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
16796         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
16797         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
16798         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
16799         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
16800         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
16801         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
16802         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
16803
16804         cancel_lru_locks mdc
16805
16806         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
16807                 error "migrate dir fails"
16808
16809         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16810 }
16811 run_test 230r "migrate with too many local locks"
16812
16813 test_231a()
16814 {
16815         # For simplicity this test assumes that max_pages_per_rpc
16816         # is the same across all OSCs
16817         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
16818         local bulk_size=$((max_pages * PAGE_SIZE))
16819         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
16820                                        head -n 1)
16821
16822         mkdir -p $DIR/$tdir
16823         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
16824                 error "failed to set stripe with -S ${brw_size}M option"
16825
16826         # clear the OSC stats
16827         $LCTL set_param osc.*.stats=0 &>/dev/null
16828         stop_writeback
16829
16830         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
16831         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
16832                 oflag=direct &>/dev/null || error "dd failed"
16833
16834         sync; sleep 1; sync # just to be safe
16835         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
16836         if [ x$nrpcs != "x1" ]; then
16837                 $LCTL get_param osc.*.stats
16838                 error "found $nrpcs ost_write RPCs, not 1 as expected"
16839         fi
16840
16841         start_writeback
16842         # Drop the OSC cache, otherwise we will read from it
16843         cancel_lru_locks osc
16844
16845         # clear the OSC stats
16846         $LCTL set_param osc.*.stats=0 &>/dev/null
16847
16848         # Client reads $bulk_size.
16849         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
16850                 iflag=direct &>/dev/null || error "dd failed"
16851
16852         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
16853         if [ x$nrpcs != "x1" ]; then
16854                 $LCTL get_param osc.*.stats
16855                 error "found $nrpcs ost_read RPCs, not 1 as expected"
16856         fi
16857 }
16858 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
16859
16860 test_231b() {
16861         mkdir -p $DIR/$tdir
16862         local i
16863         for i in {0..1023}; do
16864                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
16865                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
16866                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
16867         done
16868         sync
16869 }
16870 run_test 231b "must not assert on fully utilized OST request buffer"
16871
16872 test_232a() {
16873         mkdir -p $DIR/$tdir
16874         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16875
16876         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16877         do_facet ost1 $LCTL set_param fail_loc=0x31c
16878
16879         # ignore dd failure
16880         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
16881
16882         do_facet ost1 $LCTL set_param fail_loc=0
16883         umount_client $MOUNT || error "umount failed"
16884         mount_client $MOUNT || error "mount failed"
16885         stop ost1 || error "cannot stop ost1"
16886         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16887 }
16888 run_test 232a "failed lock should not block umount"
16889
16890 test_232b() {
16891         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
16892                 skip "Need MDS version at least 2.10.58"
16893
16894         mkdir -p $DIR/$tdir
16895         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16896         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
16897         sync
16898         cancel_lru_locks osc
16899
16900         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16901         do_facet ost1 $LCTL set_param fail_loc=0x31c
16902
16903         # ignore failure
16904         $LFS data_version $DIR/$tdir/$tfile || true
16905
16906         do_facet ost1 $LCTL set_param fail_loc=0
16907         umount_client $MOUNT || error "umount failed"
16908         mount_client $MOUNT || error "mount failed"
16909         stop ost1 || error "cannot stop ost1"
16910         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16911 }
16912 run_test 232b "failed data version lock should not block umount"
16913
16914 test_233a() {
16915         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
16916                 skip "Need MDS version at least 2.3.64"
16917         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16918
16919         local fid=$($LFS path2fid $MOUNT)
16920
16921         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16922                 error "cannot access $MOUNT using its FID '$fid'"
16923 }
16924 run_test 233a "checking that OBF of the FS root succeeds"
16925
16926 test_233b() {
16927         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
16928                 skip "Need MDS version at least 2.5.90"
16929         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16930
16931         local fid=$($LFS path2fid $MOUNT/.lustre)
16932
16933         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16934                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
16935
16936         fid=$($LFS path2fid $MOUNT/.lustre/fid)
16937         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16938                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
16939 }
16940 run_test 233b "checking that OBF of the FS .lustre succeeds"
16941
16942 test_234() {
16943         local p="$TMP/sanityN-$TESTNAME.parameters"
16944         save_lustre_params client "llite.*.xattr_cache" > $p
16945         lctl set_param llite.*.xattr_cache 1 ||
16946                 skip_env "xattr cache is not supported"
16947
16948         mkdir -p $DIR/$tdir || error "mkdir failed"
16949         touch $DIR/$tdir/$tfile || error "touch failed"
16950         # OBD_FAIL_LLITE_XATTR_ENOMEM
16951         $LCTL set_param fail_loc=0x1405
16952         getfattr -n user.attr $DIR/$tdir/$tfile &&
16953                 error "getfattr should have failed with ENOMEM"
16954         $LCTL set_param fail_loc=0x0
16955         rm -rf $DIR/$tdir
16956
16957         restore_lustre_params < $p
16958         rm -f $p
16959 }
16960 run_test 234 "xattr cache should not crash on ENOMEM"
16961
16962 test_235() {
16963         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
16964                 skip "Need MDS version at least 2.4.52"
16965
16966         flock_deadlock $DIR/$tfile
16967         local RC=$?
16968         case $RC in
16969                 0)
16970                 ;;
16971                 124) error "process hangs on a deadlock"
16972                 ;;
16973                 *) error "error executing flock_deadlock $DIR/$tfile"
16974                 ;;
16975         esac
16976 }
16977 run_test 235 "LU-1715: flock deadlock detection does not work properly"
16978
16979 #LU-2935
16980 test_236() {
16981         check_swap_layouts_support
16982
16983         local ref1=/etc/passwd
16984         local ref2=/etc/group
16985         local file1=$DIR/$tdir/f1
16986         local file2=$DIR/$tdir/f2
16987
16988         test_mkdir -c1 $DIR/$tdir
16989         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
16990         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
16991         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
16992         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
16993         local fd=$(free_fd)
16994         local cmd="exec $fd<>$file2"
16995         eval $cmd
16996         rm $file2
16997         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
16998                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
16999         cmd="exec $fd>&-"
17000         eval $cmd
17001         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17002
17003         #cleanup
17004         rm -rf $DIR/$tdir
17005 }
17006 run_test 236 "Layout swap on open unlinked file"
17007
17008 # LU-4659 linkea consistency
17009 test_238() {
17010         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
17011                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
17012                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
17013                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
17014
17015         touch $DIR/$tfile
17016         ln $DIR/$tfile $DIR/$tfile.lnk
17017         touch $DIR/$tfile.new
17018         mv $DIR/$tfile.new $DIR/$tfile
17019         local fid1=$($LFS path2fid $DIR/$tfile)
17020         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
17021         local path1=$($LFS fid2path $FSNAME "$fid1")
17022         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
17023         local path2=$($LFS fid2path $FSNAME "$fid2")
17024         [ $tfile.lnk == $path2 ] ||
17025                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
17026         rm -f $DIR/$tfile*
17027 }
17028 run_test 238 "Verify linkea consistency"
17029
17030 test_239A() { # was test_239
17031         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
17032                 skip "Need MDS version at least 2.5.60"
17033
17034         local list=$(comma_list $(mdts_nodes))
17035
17036         mkdir -p $DIR/$tdir
17037         createmany -o $DIR/$tdir/f- 5000
17038         unlinkmany $DIR/$tdir/f- 5000
17039         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
17040                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
17041         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
17042                         osp.*MDT*.sync_in_flight" | calc_sum)
17043         [ "$changes" -eq 0 ] || error "$changes not synced"
17044 }
17045 run_test 239A "osp_sync test"
17046
17047 test_239a() { #LU-5297
17048         remote_mds_nodsh && skip "remote MDS with nodsh"
17049
17050         touch $DIR/$tfile
17051         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
17052         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
17053         chgrp $RUNAS_GID $DIR/$tfile
17054         wait_delete_completed
17055 }
17056 run_test 239a "process invalid osp sync record correctly"
17057
17058 test_239b() { #LU-5297
17059         remote_mds_nodsh && skip "remote MDS with nodsh"
17060
17061         touch $DIR/$tfile1
17062         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
17063         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
17064         chgrp $RUNAS_GID $DIR/$tfile1
17065         wait_delete_completed
17066         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17067         touch $DIR/$tfile2
17068         chgrp $RUNAS_GID $DIR/$tfile2
17069         wait_delete_completed
17070 }
17071 run_test 239b "process osp sync record with ENOMEM error correctly"
17072
17073 test_240() {
17074         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17075         remote_mds_nodsh && skip "remote MDS with nodsh"
17076
17077         mkdir -p $DIR/$tdir
17078
17079         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
17080                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
17081         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
17082                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
17083
17084         umount_client $MOUNT || error "umount failed"
17085         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
17086         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
17087         mount_client $MOUNT || error "failed to mount client"
17088
17089         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
17090         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
17091 }
17092 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
17093
17094 test_241_bio() {
17095         local count=$1
17096         local bsize=$2
17097
17098         for LOOP in $(seq $count); do
17099                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
17100                 cancel_lru_locks $OSC || true
17101         done
17102 }
17103
17104 test_241_dio() {
17105         local count=$1
17106         local bsize=$2
17107
17108         for LOOP in $(seq $1); do
17109                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
17110                         2>/dev/null
17111         done
17112 }
17113
17114 test_241a() { # was test_241
17115         local bsize=$PAGE_SIZE
17116
17117         (( bsize < 40960 )) && bsize=40960
17118         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17119         ls -la $DIR/$tfile
17120         cancel_lru_locks $OSC
17121         test_241_bio 1000 $bsize &
17122         PID=$!
17123         test_241_dio 1000 $bsize
17124         wait $PID
17125 }
17126 run_test 241a "bio vs dio"
17127
17128 test_241b() {
17129         local bsize=$PAGE_SIZE
17130
17131         (( bsize < 40960 )) && bsize=40960
17132         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
17133         ls -la $DIR/$tfile
17134         test_241_dio 1000 $bsize &
17135         PID=$!
17136         test_241_dio 1000 $bsize
17137         wait $PID
17138 }
17139 run_test 241b "dio vs dio"
17140
17141 test_242() {
17142         remote_mds_nodsh && skip "remote MDS with nodsh"
17143
17144         mkdir -p $DIR/$tdir
17145         touch $DIR/$tdir/$tfile
17146
17147         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
17148         do_facet mds1 lctl set_param fail_loc=0x105
17149         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
17150
17151         do_facet mds1 lctl set_param fail_loc=0
17152         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
17153 }
17154 run_test 242 "mdt_readpage failure should not cause directory unreadable"
17155
17156 test_243()
17157 {
17158         test_mkdir $DIR/$tdir
17159         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
17160 }
17161 run_test 243 "various group lock tests"
17162
17163 test_244()
17164 {
17165         test_mkdir $DIR/$tdir
17166         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
17167         sendfile_grouplock $DIR/$tdir/$tfile || \
17168                 error "sendfile+grouplock failed"
17169         rm -rf $DIR/$tdir
17170 }
17171 run_test 244 "sendfile with group lock tests"
17172
17173 test_245() {
17174         local flagname="multi_mod_rpcs"
17175         local connect_data_name="max_mod_rpcs"
17176         local out
17177
17178         # check if multiple modify RPCs flag is set
17179         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
17180                 grep "connect_flags:")
17181         echo "$out"
17182
17183         echo "$out" | grep -qw $flagname
17184         if [ $? -ne 0 ]; then
17185                 echo "connect flag $flagname is not set"
17186                 return
17187         fi
17188
17189         # check if multiple modify RPCs data is set
17190         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
17191         echo "$out"
17192
17193         echo "$out" | grep -qw $connect_data_name ||
17194                 error "import should have connect data $connect_data_name"
17195 }
17196 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
17197
17198 test_246() { # LU-7371
17199         remote_ost_nodsh && skip "remote OST with nodsh"
17200         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
17201                 skip "Need OST version >= 2.7.62"
17202
17203         do_facet ost1 $LCTL set_param fail_val=4095
17204 #define OBD_FAIL_OST_READ_SIZE          0x234
17205         do_facet ost1 $LCTL set_param fail_loc=0x234
17206         $LFS setstripe $DIR/$tfile -i 0 -c 1
17207         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
17208         cancel_lru_locks $FSNAME-OST0000
17209         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
17210 }
17211 run_test 246 "Read file of size 4095 should return right length"
17212
17213 cleanup_247() {
17214         local submount=$1
17215
17216         trap 0
17217         umount_client $submount
17218         rmdir $submount
17219 }
17220
17221 test_247a() {
17222         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17223                 grep -q subtree ||
17224                 skip_env "Fileset feature is not supported"
17225
17226         local submount=${MOUNT}_$tdir
17227
17228         mkdir $MOUNT/$tdir
17229         mkdir -p $submount || error "mkdir $submount failed"
17230         FILESET="$FILESET/$tdir" mount_client $submount ||
17231                 error "mount $submount failed"
17232         trap "cleanup_247 $submount" EXIT
17233         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
17234         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
17235                 error "read $MOUNT/$tdir/$tfile failed"
17236         cleanup_247 $submount
17237 }
17238 run_test 247a "mount subdir as fileset"
17239
17240 test_247b() {
17241         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17242                 skip_env "Fileset feature is not supported"
17243
17244         local submount=${MOUNT}_$tdir
17245
17246         rm -rf $MOUNT/$tdir
17247         mkdir -p $submount || error "mkdir $submount failed"
17248         SKIP_FILESET=1
17249         FILESET="$FILESET/$tdir" mount_client $submount &&
17250                 error "mount $submount should fail"
17251         rmdir $submount
17252 }
17253 run_test 247b "mount subdir that dose not exist"
17254
17255 test_247c() {
17256         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17257                 skip_env "Fileset feature is not supported"
17258
17259         local submount=${MOUNT}_$tdir
17260
17261         mkdir -p $MOUNT/$tdir/dir1
17262         mkdir -p $submount || error "mkdir $submount failed"
17263         trap "cleanup_247 $submount" EXIT
17264         FILESET="$FILESET/$tdir" mount_client $submount ||
17265                 error "mount $submount failed"
17266         local fid=$($LFS path2fid $MOUNT/)
17267         $LFS fid2path $submount $fid && error "fid2path should fail"
17268         cleanup_247 $submount
17269 }
17270 run_test 247c "running fid2path outside root"
17271
17272 test_247d() {
17273         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
17274                 skip "Fileset feature is not supported"
17275
17276         local submount=${MOUNT}_$tdir
17277
17278         mkdir -p $MOUNT/$tdir/dir1
17279         mkdir -p $submount || error "mkdir $submount failed"
17280         FILESET="$FILESET/$tdir" mount_client $submount ||
17281                 error "mount $submount failed"
17282         trap "cleanup_247 $submount" EXIT
17283         local fid=$($LFS path2fid $submount/dir1)
17284         $LFS fid2path $submount $fid || error "fid2path should succeed"
17285         cleanup_247 $submount
17286 }
17287 run_test 247d "running fid2path inside root"
17288
17289 # LU-8037
17290 test_247e() {
17291         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17292                 grep -q subtree ||
17293                 skip "Fileset feature is not supported"
17294
17295         local submount=${MOUNT}_$tdir
17296
17297         mkdir $MOUNT/$tdir
17298         mkdir -p $submount || error "mkdir $submount failed"
17299         FILESET="$FILESET/.." mount_client $submount &&
17300                 error "mount $submount should fail"
17301         rmdir $submount
17302 }
17303 run_test 247e "mount .. as fileset"
17304
17305 test_247f() {
17306         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17307         [ $MDS1_VERSION -lt $(version_code 2.12.6) ] &&
17308                 skip "Need at least version 2.12.6"
17309         [ $CLIENT_VERSION -lt $(version_code 2.12.6) ] &&
17310                 skip "Need at least version 2.12.6"
17311         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
17312                 grep -q subtree ||
17313                 skip "Fileset feature is not supported"
17314
17315         mkdir $DIR/$tdir || error "mkdir $tdir failed"
17316         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
17317                 error "mkdir remote failed"
17318         mkdir $DIR/$tdir/remote/subdir || error "mkdir remote/subdir failed"
17319         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
17320                 error "mkdir striped failed"
17321         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
17322
17323         local submount=${MOUNT}_$tdir
17324
17325         mkdir -p $submount || error "mkdir $submount failed"
17326         stack_trap "rmdir $submount"
17327
17328         local dir
17329         local stat
17330         local fileset=$FILESET
17331         local mdts=$(comma_list $(mdts_nodes))
17332
17333         stat=$(do_facet mds1 $LCTL get_param -n \
17334                 mdt.*MDT0000.enable_remote_subdir_mount)
17335         stack_trap "do_nodes $mdts $LCTL set_param \
17336                 mdt.*.enable_remote_subdir_mount=$stat"
17337
17338         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
17339         stack_trap "umount_client $submount"
17340         FILESET="$fileset/$tdir/remote" mount_client $submount &&
17341                 error "mount remote dir $dir should fail"
17342
17343         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
17344                 $tdir/striped/. ; do
17345                 FILESET="$fileset/$dir" mount_client $submount ||
17346                         error "mount $dir failed"
17347                 umount_client $submount
17348         done
17349
17350         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
17351         FILESET="$fileset/$tdir/remote" mount_client $submount ||
17352                 error "mount $tdir/remote failed"
17353 }
17354 run_test 247f "mount striped or remote directory as fileset"
17355
17356 test_247g() {
17357         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
17358         [ $CLIENT_VERSION -lt $(version_code 2.12.6) ] &&
17359                 skip "Need at least version 2.12.6"
17360
17361         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
17362                 error "mkdir $tdir failed"
17363         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
17364
17365         local submount=${MOUNT}_$tdir
17366
17367         mkdir -p $submount || error "mkdir $submount failed"
17368         stack_trap "rmdir $submount"
17369
17370         FILESET="$fileset/$tdir" mount_client $submount ||
17371                 error "mount $dir failed"
17372         stack_trap "umount $submount"
17373
17374         local mdts=$(comma_list $(mdts_nodes))
17375
17376         local nrpcs
17377
17378         stat $submount > /dev/null
17379         cancel_lru_locks $MDC
17380         stat $submount > /dev/null
17381         stat $submount/$tfile > /dev/null
17382         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
17383         stat $submount/$tfile > /dev/null
17384         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
17385                 awk '/getattr/ {sum += $2} END {print sum}')
17386
17387         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
17388 }
17389 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
17390
17391 test_248() {
17392         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
17393         [ -z "$fast_read_sav" ] && skip "no fast read support"
17394
17395         # create a large file for fast read verification
17396         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
17397
17398         # make sure the file is created correctly
17399         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
17400                 { rm -f $DIR/$tfile; skip "file creation error"; }
17401
17402         echo "Test 1: verify that fast read is 4 times faster on cache read"
17403
17404         # small read with fast read enabled
17405         $LCTL set_param -n llite.*.fast_read=1
17406         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17407                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17408                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17409         # small read with fast read disabled
17410         $LCTL set_param -n llite.*.fast_read=0
17411         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
17412                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17413                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17414
17415         # verify that fast read is 4 times faster for cache read
17416         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
17417                 error_not_in_vm "fast read was not 4 times faster: " \
17418                            "$t_fast vs $t_slow"
17419
17420         echo "Test 2: verify the performance between big and small read"
17421         $LCTL set_param -n llite.*.fast_read=1
17422
17423         # 1k non-cache read
17424         cancel_lru_locks osc
17425         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17426                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17427                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17428
17429         # 1M non-cache read
17430         cancel_lru_locks osc
17431         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
17432                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
17433                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
17434
17435         # verify that big IO is not 4 times faster than small IO
17436         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
17437                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
17438
17439         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
17440         rm -f $DIR/$tfile
17441 }
17442 run_test 248 "fast read verification"
17443
17444 test_249() { # LU-7890
17445         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
17446                 skip "Need at least version 2.8.54"
17447
17448         rm -f $DIR/$tfile
17449         $SETSTRIPE -c 1 $DIR/$tfile
17450         # Offset 2T == 4k * 512M
17451         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
17452                 error "dd to 2T offset failed"
17453 }
17454 run_test 249 "Write above 2T file size"
17455
17456 test_250() {
17457         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
17458          && skip "no 16TB file size limit on ZFS"
17459
17460         $SETSTRIPE -c 1 $DIR/$tfile
17461         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
17462         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
17463         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
17464         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
17465                 conv=notrunc,fsync && error "append succeeded"
17466         return 0
17467 }
17468 run_test 250 "Write above 16T limit"
17469
17470 test_251() {
17471         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
17472
17473         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
17474         #Skip once - writing the first stripe will succeed
17475         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17476         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17477                 error "short write happened"
17478
17479         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17480         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17481                 error "short read happened"
17482
17483         rm -f $DIR/$tfile
17484 }
17485 run_test 251 "Handling short read and write correctly"
17486
17487 test_252() {
17488         remote_mds_nodsh && skip "remote MDS with nodsh"
17489         remote_ost_nodsh && skip "remote OST with nodsh"
17490         if [ "$ost1_FSTYPE" != "ldiskfs" -o "$mds1_FSTYPE" != "ldiskfs" ]; then
17491                 skip_env "ldiskfs only test"
17492         fi
17493
17494         local tgt
17495         local dev
17496         local out
17497         local uuid
17498         local num
17499         local gen
17500
17501         # check lr_reader on OST0000
17502         tgt=ost1
17503         dev=$(facet_device $tgt)
17504         out=$(do_facet $tgt $LR_READER $dev)
17505         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17506         echo "$out"
17507         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17508         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17509                 error "Invalid uuid returned by $LR_READER on target $tgt"
17510         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17511
17512         # check lr_reader -c on MDT0000
17513         tgt=mds1
17514         dev=$(facet_device $tgt)
17515         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17516                 skip "$LR_READER does not support additional options"
17517         fi
17518         out=$(do_facet $tgt $LR_READER -c $dev)
17519         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17520         echo "$out"
17521         num=$(echo "$out" | grep -c "mdtlov")
17522         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17523                 error "Invalid number of mdtlov clients returned by $LR_READER"
17524         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17525
17526         # check lr_reader -cr on MDT0000
17527         out=$(do_facet $tgt $LR_READER -cr $dev)
17528         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17529         echo "$out"
17530         echo "$out" | grep -q "^reply_data:$" ||
17531                 error "$LR_READER should have returned 'reply_data' section"
17532         num=$(echo "$out" | grep -c "client_generation")
17533         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17534 }
17535 run_test 252 "check lr_reader tool"
17536
17537 test_253_fill_ost() {
17538         local size_mb #how many MB should we write to pass watermark
17539         local lwm=$3  #low watermark
17540         local free_10mb #10% of free space
17541
17542         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17543         size_mb=$((free_kb / 1024 - lwm))
17544         free_10mb=$((free_kb / 10240))
17545         #If 10% of free space cross low watermark use it
17546         if (( free_10mb > size_mb )); then
17547                 size_mb=$free_10mb
17548         else
17549                 #At least we need to store 1.1 of difference between
17550                 #free space and low watermark
17551                 size_mb=$((size_mb + size_mb / 10))
17552         fi
17553         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
17554                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
17555                          oflag=append conv=notrunc
17556         fi
17557
17558         sleep_maxage
17559
17560         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17561         echo "OST still has $((free_kb / 1024)) mbytes free"
17562 }
17563
17564 test_253() {
17565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17566         remote_mds_nodsh && skip "remote MDS with nodsh"
17567         remote_mgs_nodsh && skip "remote MGS with nodsh"
17568
17569         local ostidx=0
17570         local rc=0
17571
17572         local ost_name=$($LFS osts |
17573                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
17574         # on the mdt's osc
17575         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17576         do_facet $SINGLEMDS $LCTL get_param -n \
17577                 osp.$mdtosc_proc1.reserved_mb_high ||
17578                 skip  "remote MDS does not support reserved_mb_high"
17579
17580         rm -rf $DIR/$tdir
17581         wait_mds_ost_sync
17582         wait_delete_completed
17583         mkdir $DIR/$tdir
17584
17585         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
17586                         osp.$mdtosc_proc1.reserved_mb_high)
17587         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
17588                         osp.$mdtosc_proc1.reserved_mb_low)
17589         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
17590
17591         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
17592         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
17593                 error "Adding $ost_name to pool failed"
17594
17595         # Wait for client to see a OST at pool
17596         wait_update $HOSTNAME "$LCTL get_param -n
17597                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
17598                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
17599                 error "Client can not see the pool"
17600         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17601                 error "Setstripe failed"
17602
17603         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
17604         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
17605         echo "OST still has $((blocks/1024)) mbytes free"
17606
17607         local new_lwm=$((blocks/1024-10))
17608         do_facet $SINGLEMDS $LCTL set_param \
17609                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
17610         do_facet $SINGLEMDS $LCTL set_param \
17611                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
17612
17613         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17614
17615         #First enospc could execute orphan deletion so repeat.
17616         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17617
17618         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17619                         osp.$mdtosc_proc1.prealloc_status)
17620         echo "prealloc_status $oa_status"
17621
17622         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
17623                 error "File creation should fail"
17624         #object allocation was stopped, but we still able to append files
17625         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
17626                 error "Append failed"
17627         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
17628
17629         wait_delete_completed
17630
17631         sleep_maxage
17632
17633         for i in $(seq 10 12); do
17634                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
17635                         error "File creation failed after rm";
17636         done
17637
17638         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17639                         osp.$mdtosc_proc1.prealloc_status)
17640         echo "prealloc_status $oa_status"
17641
17642         if (( oa_status != 0 )); then
17643                 error "Object allocation still disable after rm"
17644         fi
17645         do_facet $SINGLEMDS $LCTL set_param \
17646                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
17647         do_facet $SINGLEMDS $LCTL set_param \
17648                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
17649
17650
17651         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
17652                 error "Remove $ost_name from pool failed"
17653         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
17654                 error "Pool destroy fialed"
17655 }
17656 run_test 253 "Check object allocation limit"
17657
17658 test_254() {
17659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17660         remote_mds_nodsh && skip "remote MDS with nodsh"
17661         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17662                 skip "MDS does not support changelog_size"
17663
17664         local cl_user
17665         local MDT0=$(facet_svc $SINGLEMDS)
17666
17667         changelog_register || error "changelog_register failed"
17668
17669         changelog_clear 0 || error "changelog_clear failed"
17670
17671         local size1=$(do_facet $SINGLEMDS \
17672                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17673         echo "Changelog size $size1"
17674
17675         rm -rf $DIR/$tdir
17676         $LFS mkdir -i 0 $DIR/$tdir
17677         # change something
17678         mkdir -p $DIR/$tdir/pics/2008/zachy
17679         touch $DIR/$tdir/pics/2008/zachy/timestamp
17680         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17681         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17682         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17683         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17684         rm $DIR/$tdir/pics/desktop.jpg
17685
17686         local size2=$(do_facet $SINGLEMDS \
17687                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17688         echo "Changelog size after work $size2"
17689
17690         (( $size2 > $size1 )) ||
17691                 error "new Changelog size=$size2 less than old size=$size1"
17692 }
17693 run_test 254 "Check changelog size"
17694
17695 ladvise_no_type()
17696 {
17697         local type=$1
17698         local file=$2
17699
17700         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
17701                 awk -F: '{print $2}' | grep $type > /dev/null
17702         if [ $? -ne 0 ]; then
17703                 return 0
17704         fi
17705         return 1
17706 }
17707
17708 ladvise_no_ioctl()
17709 {
17710         local file=$1
17711
17712         lfs ladvise -a willread $file > /dev/null 2>&1
17713         if [ $? -eq 0 ]; then
17714                 return 1
17715         fi
17716
17717         lfs ladvise -a willread $file 2>&1 |
17718                 grep "Inappropriate ioctl for device" > /dev/null
17719         if [ $? -eq 0 ]; then
17720                 return 0
17721         fi
17722         return 1
17723 }
17724
17725 percent() {
17726         bc <<<"scale=2; ($1 - $2) * 100 / $2"
17727 }
17728
17729 # run a random read IO workload
17730 # usage: random_read_iops <filename> <filesize> <iosize>
17731 random_read_iops() {
17732         local file=$1
17733         local fsize=$2
17734         local iosize=${3:-4096}
17735
17736         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
17737                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
17738 }
17739
17740 drop_file_oss_cache() {
17741         local file="$1"
17742         local nodes="$2"
17743
17744         $LFS ladvise -a dontneed $file 2>/dev/null ||
17745                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
17746 }
17747
17748 ladvise_willread_performance()
17749 {
17750         local repeat=10
17751         local average_origin=0
17752         local average_cache=0
17753         local average_ladvise=0
17754
17755         for ((i = 1; i <= $repeat; i++)); do
17756                 echo "Iter $i/$repeat: reading without willread hint"
17757                 cancel_lru_locks osc
17758                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17759                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
17760                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
17761                 average_origin=$(bc <<<"$average_origin + $speed_origin")
17762
17763                 cancel_lru_locks osc
17764                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
17765                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
17766                 average_cache=$(bc <<<"$average_cache + $speed_cache")
17767
17768                 cancel_lru_locks osc
17769                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17770                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
17771                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
17772                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
17773                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
17774         done
17775         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
17776         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
17777         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
17778
17779         speedup_cache=$(percent $average_cache $average_origin)
17780         speedup_ladvise=$(percent $average_ladvise $average_origin)
17781
17782         echo "Average uncached read: $average_origin"
17783         echo "Average speedup with OSS cached read: " \
17784                 "$average_cache = +$speedup_cache%"
17785         echo "Average speedup with ladvise willread: " \
17786                 "$average_ladvise = +$speedup_ladvise%"
17787
17788         local lowest_speedup=20
17789         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
17790                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
17791                         "got $average_cache%. Skipping ladvise willread check."
17792                 return 0
17793         fi
17794
17795         # the test won't work on ZFS until it supports 'ladvise dontneed', but
17796         # it is still good to run until then to exercise 'ladvise willread'
17797         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17798                 [ "$ost1_FSTYPE" = "zfs" ] &&
17799                 echo "osd-zfs does not support dontneed or drop_caches" &&
17800                 return 0
17801
17802         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
17803         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
17804                 error_not_in_vm "Speedup with willread is less than " \
17805                         "$lowest_speedup%, got $average_ladvise%"
17806 }
17807
17808 test_255a() {
17809         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17810                 skip "lustre < 2.8.54 does not support ladvise "
17811         remote_ost_nodsh && skip "remote OST with nodsh"
17812
17813         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
17814
17815         ladvise_no_type willread $DIR/$tfile &&
17816                 skip "willread ladvise is not supported"
17817
17818         ladvise_no_ioctl $DIR/$tfile &&
17819                 skip "ladvise ioctl is not supported"
17820
17821         local size_mb=100
17822         local size=$((size_mb * 1048576))
17823         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17824                 error "dd to $DIR/$tfile failed"
17825
17826         lfs ladvise -a willread $DIR/$tfile ||
17827                 error "Ladvise failed with no range argument"
17828
17829         lfs ladvise -a willread -s 0 $DIR/$tfile ||
17830                 error "Ladvise failed with no -l or -e argument"
17831
17832         lfs ladvise -a willread -e 1 $DIR/$tfile ||
17833                 error "Ladvise failed with only -e argument"
17834
17835         lfs ladvise -a willread -l 1 $DIR/$tfile ||
17836                 error "Ladvise failed with only -l argument"
17837
17838         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
17839                 error "End offset should not be smaller than start offset"
17840
17841         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
17842                 error "End offset should not be equal to start offset"
17843
17844         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
17845                 error "Ladvise failed with overflowing -s argument"
17846
17847         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
17848                 error "Ladvise failed with overflowing -e argument"
17849
17850         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
17851                 error "Ladvise failed with overflowing -l argument"
17852
17853         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
17854                 error "Ladvise succeeded with conflicting -l and -e arguments"
17855
17856         echo "Synchronous ladvise should wait"
17857         local delay=4
17858 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
17859         do_nodes $(comma_list $(osts_nodes)) \
17860                 $LCTL set_param fail_val=$delay fail_loc=0x237
17861
17862         local start_ts=$SECONDS
17863         lfs ladvise -a willread $DIR/$tfile ||
17864                 error "Ladvise failed with no range argument"
17865         local end_ts=$SECONDS
17866         local inteval_ts=$((end_ts - start_ts))
17867
17868         if [ $inteval_ts -lt $(($delay - 1)) ]; then
17869                 error "Synchronous advice didn't wait reply"
17870         fi
17871
17872         echo "Asynchronous ladvise shouldn't wait"
17873         local start_ts=$SECONDS
17874         lfs ladvise -a willread -b $DIR/$tfile ||
17875                 error "Ladvise failed with no range argument"
17876         local end_ts=$SECONDS
17877         local inteval_ts=$((end_ts - start_ts))
17878
17879         if [ $inteval_ts -gt $(($delay / 2)) ]; then
17880                 error "Asynchronous advice blocked"
17881         fi
17882
17883         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
17884         ladvise_willread_performance
17885 }
17886 run_test 255a "check 'lfs ladvise -a willread'"
17887
17888 facet_meminfo() {
17889         local facet=$1
17890         local info=$2
17891
17892         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
17893 }
17894
17895 test_255b() {
17896         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17897                 skip "lustre < 2.8.54 does not support ladvise "
17898         remote_ost_nodsh && skip "remote OST with nodsh"
17899
17900         lfs setstripe -c 1 -i 0 $DIR/$tfile
17901
17902         ladvise_no_type dontneed $DIR/$tfile &&
17903                 skip "dontneed ladvise is not supported"
17904
17905         ladvise_no_ioctl $DIR/$tfile &&
17906                 skip "ladvise ioctl is not supported"
17907
17908         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17909                 [ "$ost1_FSTYPE" = "zfs" ] &&
17910                 skip "zfs-osd does not support 'ladvise dontneed'"
17911
17912         local size_mb=100
17913         local size=$((size_mb * 1048576))
17914         # In order to prevent disturbance of other processes, only check 3/4
17915         # of the memory usage
17916         local kibibytes=$((size_mb * 1024 * 3 / 4))
17917
17918         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17919                 error "dd to $DIR/$tfile failed"
17920
17921         #force write to complete before dropping OST cache & checking memory
17922         sync
17923
17924         local total=$(facet_meminfo ost1 MemTotal)
17925         echo "Total memory: $total KiB"
17926
17927         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
17928         local before_read=$(facet_meminfo ost1 Cached)
17929         echo "Cache used before read: $before_read KiB"
17930
17931         lfs ladvise -a willread $DIR/$tfile ||
17932                 error "Ladvise willread failed"
17933         local after_read=$(facet_meminfo ost1 Cached)
17934         echo "Cache used after read: $after_read KiB"
17935
17936         lfs ladvise -a dontneed $DIR/$tfile ||
17937                 error "Ladvise dontneed again failed"
17938         local no_read=$(facet_meminfo ost1 Cached)
17939         echo "Cache used after dontneed ladvise: $no_read KiB"
17940
17941         if [ $total -lt $((before_read + kibibytes)) ]; then
17942                 echo "Memory is too small, abort checking"
17943                 return 0
17944         fi
17945
17946         if [ $((before_read + kibibytes)) -gt $after_read ]; then
17947                 error "Ladvise willread should use more memory" \
17948                         "than $kibibytes KiB"
17949         fi
17950
17951         if [ $((no_read + kibibytes)) -gt $after_read ]; then
17952                 error "Ladvise dontneed should release more memory" \
17953                         "than $kibibytes KiB"
17954         fi
17955 }
17956 run_test 255b "check 'lfs ladvise -a dontneed'"
17957
17958 test_255c() {
17959         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
17960                 skip "lustre < 2.10.50 does not support lockahead"
17961
17962         local ost1_imp=$(get_osc_import_name client ost1)
17963         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
17964                          cut -d'.' -f2)
17965         local count
17966         local new_count
17967         local difference
17968         local i
17969         local rc
17970
17971         test_mkdir -p $DIR/$tdir
17972         $SETSTRIPE -i 0 $DIR/$tdir
17973
17974         #test 10 returns only success/failure
17975         i=10
17976         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17977         rc=$?
17978         if [ $rc -eq 255 ]; then
17979                 error "Ladvise test${i} failed, ${rc}"
17980         fi
17981
17982         #test 11 counts lock enqueue requests, all others count new locks
17983         i=11
17984         count=$(do_facet ost1 \
17985                 $LCTL get_param -n ost.OSS.ost.stats)
17986         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
17987
17988         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17989         rc=$?
17990         if [ $rc -eq 255 ]; then
17991                 error "Ladvise test${i} failed, ${rc}"
17992         fi
17993
17994         new_count=$(do_facet ost1 \
17995                 $LCTL get_param -n ost.OSS.ost.stats)
17996         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
17997                    awk '{ print $2 }')
17998
17999         difference="$((new_count - count))"
18000         if [ $difference -ne $rc ]; then
18001                 error "Ladvise test${i}, bad enqueue count, returned " \
18002                       "${rc}, actual ${difference}"
18003         fi
18004
18005         for i in $(seq 12 21); do
18006                 # If we do not do this, we run the risk of having too many
18007                 # locks and starting lock cancellation while we are checking
18008                 # lock counts.
18009                 cancel_lru_locks osc
18010
18011                 count=$($LCTL get_param -n \
18012                        ldlm.namespaces.$imp_name.lock_unused_count)
18013
18014                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
18015                 rc=$?
18016                 if [ $rc -eq 255 ]; then
18017                         error "Ladvise test ${i} failed, ${rc}"
18018                 fi
18019
18020                 new_count=$($LCTL get_param -n \
18021                        ldlm.namespaces.$imp_name.lock_unused_count)
18022                 difference="$((new_count - count))"
18023
18024                 # Test 15 output is divided by 100 to map down to valid return
18025                 if [ $i -eq 15 ]; then
18026                         rc="$((rc * 100))"
18027                 fi
18028
18029                 if [ $difference -ne $rc ]; then
18030                         error "Ladvise test ${i}, bad lock count, returned " \
18031                               "${rc}, actual ${difference}"
18032                 fi
18033         done
18034
18035         #test 22 returns only success/failure
18036         i=22
18037         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18038         rc=$?
18039         if [ $rc -eq 255 ]; then
18040                 error "Ladvise test${i} failed, ${rc}"
18041         fi
18042 }
18043 run_test 255c "suite of ladvise lockahead tests"
18044
18045 test_256() {
18046         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18047         remote_mds_nodsh && skip "remote MDS with nodsh"
18048         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18049         changelog_users $SINGLEMDS | grep "^cl" &&
18050                 skip "active changelog user"
18051
18052         local cl_user
18053         local cat_sl
18054         local mdt_dev
18055
18056         mdt_dev=$(mdsdevname 1)
18057         echo $mdt_dev
18058
18059         changelog_register || error "changelog_register failed"
18060
18061         rm -rf $DIR/$tdir
18062         mkdir -p $DIR/$tdir
18063
18064         changelog_clear 0 || error "changelog_clear failed"
18065
18066         # change something
18067         touch $DIR/$tdir/{1..10}
18068
18069         # stop the MDT
18070         stop $SINGLEMDS || error "Fail to stop MDT"
18071
18072         # remount the MDT
18073
18074         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
18075
18076         #after mount new plainllog is used
18077         touch $DIR/$tdir/{11..19}
18078         local tmpfile=$(mktemp -u $tfile.XXXXXX)
18079         cat_sl=$(do_facet $SINGLEMDS "sync; \
18080                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18081                  llog_reader $tmpfile | grep -c type=1064553b")
18082         do_facet $SINGLEMDS llog_reader $tmpfile
18083
18084         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
18085
18086         changelog_clear 0 || error "changelog_clear failed"
18087
18088         cat_sl=$(do_facet $SINGLEMDS "sync; \
18089                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
18090                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
18091
18092         if (( cat_sl == 2 )); then
18093                 error "Empty plain llog was not deleted from changelog catalog"
18094         elif (( cat_sl != 1 )); then
18095                 error "Active plain llog shouldn't be deleted from catalog"
18096         fi
18097 }
18098 run_test 256 "Check llog delete for empty and not full state"
18099
18100 test_257() {
18101         remote_mds_nodsh && skip "remote MDS with nodsh"
18102         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
18103                 skip "Need MDS version at least 2.8.55"
18104
18105         test_mkdir $DIR/$tdir
18106
18107         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
18108                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
18109         stat $DIR/$tdir
18110
18111 #define OBD_FAIL_MDS_XATTR_REP                  0x161
18112         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18113         local facet=mds$((mdtidx + 1))
18114         set_nodes_failloc $(facet_active_host $facet) 0x80000161
18115         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
18116
18117         stop $facet || error "stop MDS failed"
18118         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
18119                 error "start MDS fail"
18120 }
18121 run_test 257 "xattr locks are not lost"
18122
18123 # Verify we take the i_mutex when security requires it
18124 test_258a() {
18125 #define OBD_FAIL_IMUTEX_SEC 0x141c
18126         $LCTL set_param fail_loc=0x141c
18127         touch $DIR/$tfile
18128         chmod u+s $DIR/$tfile
18129         chmod a+rwx $DIR/$tfile
18130         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18131         RC=$?
18132         if [ $RC -ne 0 ]; then
18133                 error "error, failed to take i_mutex, rc=$?"
18134         fi
18135         rm -f $DIR/$tfile
18136 }
18137 run_test 258a "verify i_mutex security behavior when suid attributes is set"
18138
18139 # Verify we do NOT take the i_mutex in the normal case
18140 test_258b() {
18141 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
18142         $LCTL set_param fail_loc=0x141d
18143         touch $DIR/$tfile
18144         chmod a+rwx $DIR
18145         chmod a+rw $DIR/$tfile
18146         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
18147         RC=$?
18148         if [ $RC -ne 0 ]; then
18149                 error "error, took i_mutex unnecessarily, rc=$?"
18150         fi
18151         rm -f $DIR/$tfile
18152
18153 }
18154 run_test 258b "verify i_mutex security behavior"
18155
18156 test_259() {
18157         local file=$DIR/$tfile
18158         local before
18159         local after
18160
18161         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
18162
18163         stack_trap "rm -f $file" EXIT
18164
18165         wait_delete_completed
18166         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18167         echo "before: $before"
18168
18169         $LFS setstripe -i 0 -c 1 $file
18170         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
18171         sync_all_data
18172         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18173         echo "after write: $after"
18174
18175 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
18176         do_facet ost1 $LCTL set_param fail_loc=0x2301
18177         $TRUNCATE $file 0
18178         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18179         echo "after truncate: $after"
18180
18181         stop ost1
18182         do_facet ost1 $LCTL set_param fail_loc=0
18183         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18184         sleep 2
18185         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
18186         echo "after restart: $after"
18187         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
18188                 error "missing truncate?"
18189
18190         return 0
18191 }
18192 run_test 259 "crash at delayed truncate"
18193
18194 test_260() {
18195 #define OBD_FAIL_MDC_CLOSE               0x806
18196         $LCTL set_param fail_loc=0x80000806
18197         touch $DIR/$tfile
18198
18199 }
18200 run_test 260 "Check mdc_close fail"
18201
18202 ### Data-on-MDT sanity tests ###
18203 test_270a() {
18204         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18205                 skip "Need MDS version at least 2.10.55 for DoM"
18206
18207         # create DoM file
18208         local dom=$DIR/$tdir/dom_file
18209         local tmp=$DIR/$tdir/tmp_file
18210
18211         mkdir -p $DIR/$tdir
18212
18213         # basic checks for DoM component creation
18214         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
18215                 error "Can set MDT layout to non-first entry"
18216
18217         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
18218                 error "Can define multiple entries as MDT layout"
18219
18220         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
18221
18222         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
18223         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
18224         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
18225
18226         local mdtidx=$($LFS getstripe -m $dom)
18227         local mdtname=MDT$(printf %04x $mdtidx)
18228         local facet=mds$((mdtidx + 1))
18229         local space_check=1
18230
18231         # Skip free space checks with ZFS
18232         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
18233
18234         # write
18235         sync
18236         local size_tmp=$((65536 * 3))
18237         local mdtfree1=$(do_facet $facet \
18238                          lctl get_param -n osd*.*$mdtname.kbytesfree)
18239
18240         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18241         # check also direct IO along write
18242         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
18243         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18244         sync
18245         cmp $tmp $dom || error "file data is different"
18246         [ $(stat -c%s $dom) == $size_tmp ] ||
18247                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18248         if [ $space_check == 1 ]; then
18249                 local mdtfree2=$(do_facet $facet \
18250                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
18251
18252                 # increase in usage from by $size_tmp
18253                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18254                         error "MDT free space wrong after write: " \
18255                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18256         fi
18257
18258         # truncate
18259         local size_dom=10000
18260
18261         $TRUNCATE $dom $size_dom
18262         [ $(stat -c%s $dom) == $size_dom ] ||
18263                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
18264         if [ $space_check == 1 ]; then
18265                 mdtfree1=$(do_facet $facet \
18266                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18267                 # decrease in usage from $size_tmp to new $size_dom
18268                 [ $(($mdtfree1 - $mdtfree2)) -ge \
18269                   $(((size_tmp - size_dom) / 1024)) ] ||
18270                         error "MDT free space is wrong after truncate: " \
18271                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
18272         fi
18273
18274         # append
18275         cat $tmp >> $dom
18276         sync
18277         size_dom=$((size_dom + size_tmp))
18278         [ $(stat -c%s $dom) == $size_dom ] ||
18279                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
18280         if [ $space_check == 1 ]; then
18281                 mdtfree2=$(do_facet $facet \
18282                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18283                 # increase in usage by $size_tmp from previous
18284                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
18285                         error "MDT free space is wrong after append: " \
18286                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
18287         fi
18288
18289         # delete
18290         rm $dom
18291         if [ $space_check == 1 ]; then
18292                 mdtfree1=$(do_facet $facet \
18293                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18294                 # decrease in usage by $size_dom from previous
18295                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
18296                         error "MDT free space is wrong after removal: " \
18297                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
18298         fi
18299
18300         # combined striping
18301         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
18302                 error "Can't create DoM + OST striping"
18303
18304         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
18305         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
18306         # check also direct IO along write
18307         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
18308         sync
18309         cmp $tmp $dom || error "file data is different"
18310         [ $(stat -c%s $dom) == $size_tmp ] ||
18311                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
18312         rm $dom $tmp
18313
18314         return 0
18315 }
18316 run_test 270a "DoM: basic functionality tests"
18317
18318 test_270b() {
18319         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18320                 skip "Need MDS version at least 2.10.55"
18321
18322         local dom=$DIR/$tdir/dom_file
18323         local max_size=1048576
18324
18325         mkdir -p $DIR/$tdir
18326         $LFS setstripe -E $max_size -L mdt $dom
18327
18328         # truncate over the limit
18329         $TRUNCATE $dom $(($max_size + 1)) &&
18330                 error "successful truncate over the maximum size"
18331         # write over the limit
18332         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
18333                 error "successful write over the maximum size"
18334         # append over the limit
18335         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
18336         echo "12345" >> $dom && error "successful append over the maximum size"
18337         rm $dom
18338
18339         return 0
18340 }
18341 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
18342
18343 test_270c() {
18344         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18345                 skip "Need MDS version at least 2.10.55"
18346
18347         mkdir -p $DIR/$tdir
18348         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18349
18350         # check files inherit DoM EA
18351         touch $DIR/$tdir/first
18352         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
18353                 error "bad pattern"
18354         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
18355                 error "bad stripe count"
18356         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
18357                 error "bad stripe size"
18358
18359         # check directory inherits DoM EA and uses it as default
18360         mkdir $DIR/$tdir/subdir
18361         touch $DIR/$tdir/subdir/second
18362         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
18363                 error "bad pattern in sub-directory"
18364         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
18365                 error "bad stripe count in sub-directory"
18366         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
18367                 error "bad stripe size in sub-directory"
18368         return 0
18369 }
18370 run_test 270c "DoM: DoM EA inheritance tests"
18371
18372 test_270d() {
18373         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18374                 skip "Need MDS version at least 2.10.55"
18375
18376         mkdir -p $DIR/$tdir
18377         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18378
18379         # inherit default DoM striping
18380         mkdir $DIR/$tdir/subdir
18381         touch $DIR/$tdir/subdir/f1
18382
18383         # change default directory striping
18384         $LFS setstripe -c 1 $DIR/$tdir/subdir
18385         touch $DIR/$tdir/subdir/f2
18386         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
18387                 error "wrong default striping in file 2"
18388         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
18389                 error "bad pattern in file 2"
18390         return 0
18391 }
18392 run_test 270d "DoM: change striping from DoM to RAID0"
18393
18394 test_270e() {
18395         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18396                 skip "Need MDS version at least 2.10.55"
18397
18398         mkdir -p $DIR/$tdir/dom
18399         mkdir -p $DIR/$tdir/norm
18400         DOMFILES=20
18401         NORMFILES=10
18402         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
18403         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
18404
18405         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
18406         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
18407
18408         # find DoM files by layout
18409         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
18410         [ $NUM -eq  $DOMFILES ] ||
18411                 error "lfs find -L: found $NUM, expected $DOMFILES"
18412         echo "Test 1: lfs find 20 DOM files by layout: OK"
18413
18414         # there should be 1 dir with default DOM striping
18415         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
18416         [ $NUM -eq  1 ] ||
18417                 error "lfs find -L: found $NUM, expected 1 dir"
18418         echo "Test 2: lfs find 1 DOM dir by layout: OK"
18419
18420         # find DoM files by stripe size
18421         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
18422         [ $NUM -eq  $DOMFILES ] ||
18423                 error "lfs find -S: found $NUM, expected $DOMFILES"
18424         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
18425
18426         # find files by stripe offset except DoM files
18427         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
18428         [ $NUM -eq  $NORMFILES ] ||
18429                 error "lfs find -i: found $NUM, expected $NORMFILES"
18430         echo "Test 5: lfs find no DOM files by stripe index: OK"
18431         return 0
18432 }
18433 run_test 270e "DoM: lfs find with DoM files test"
18434
18435 test_270f() {
18436         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18437                 skip "Need MDS version at least 2.10.55"
18438
18439         local mdtname=${FSNAME}-MDT0000-mdtlov
18440         local dom=$DIR/$tdir/dom_file
18441         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
18442                                                 lod.$mdtname.dom_stripesize)
18443         local dom_limit=131072
18444
18445         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
18446         local dom_current=$(do_facet mds1 $LCTL get_param -n \
18447                                                 lod.$mdtname.dom_stripesize)
18448         [ ${dom_limit} -eq ${dom_current} ] ||
18449                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
18450
18451         $LFS mkdir -i 0 -c 1 $DIR/$tdir
18452         $LFS setstripe -d $DIR/$tdir
18453         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
18454                 error "Can't set directory default striping"
18455
18456         # exceed maximum stripe size
18457         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18458                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
18459         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
18460                 error "Able to create DoM component size more than LOD limit"
18461
18462         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
18463         dom_current=$(do_facet mds1 $LCTL get_param -n \
18464                                                 lod.$mdtname.dom_stripesize)
18465         [ 0 -eq ${dom_current} ] ||
18466                 error "Can't set zero DoM stripe limit"
18467         rm $dom
18468
18469         # attempt to create DoM file on server with disabled DoM should
18470         # remove DoM entry from layout and be succeed
18471         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
18472                 error "Can't create DoM file (DoM is disabled)"
18473         [ $($LFS getstripe -L $dom) == "mdt" ] &&
18474                 error "File has DoM component while DoM is disabled"
18475         rm $dom
18476
18477         # attempt to create DoM file with only DoM stripe should return error
18478         $LFS setstripe -E $dom_limit -L mdt $dom &&
18479                 error "Able to create DoM-only file while DoM is disabled"
18480
18481         # too low values to be aligned with smallest stripe size 64K
18482         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18483         dom_current=$(do_facet mds1 $LCTL get_param -n \
18484                                                 lod.$mdtname.dom_stripesize)
18485         [ 30000 -eq ${dom_current} ] &&
18486                 error "Can set too small DoM stripe limit"
18487
18488         # 64K is a minimal stripe size in Lustre, expect limit of that size
18489         [ 65536 -eq ${dom_current} ] ||
18490                 error "Limit is not set to 64K but ${dom_current}"
18491
18492         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18493         dom_current=$(do_facet mds1 $LCTL get_param -n \
18494                                                 lod.$mdtname.dom_stripesize)
18495         echo $dom_current
18496         [ 2147483648 -eq ${dom_current} ] &&
18497                 error "Can set too large DoM stripe limit"
18498
18499         do_facet mds1 $LCTL set_param -n \
18500                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18501         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18502                 error "Can't create DoM component size after limit change"
18503         do_facet mds1 $LCTL set_param -n \
18504                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18505         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18506                 error "Can't create DoM file after limit decrease"
18507         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18508                 error "Can create big DoM component after limit decrease"
18509         touch ${dom}_def ||
18510                 error "Can't create file with old default layout"
18511
18512         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18513         return 0
18514 }
18515 run_test 270f "DoM: maximum DoM stripe size checks"
18516
18517 test_271a() {
18518         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18519                 skip "Need MDS version at least 2.10.55"
18520
18521         local dom=$DIR/$tdir/dom
18522
18523         mkdir -p $DIR/$tdir
18524
18525         $LFS setstripe -E 1024K -L mdt $dom
18526
18527         lctl set_param -n mdc.*.stats=clear
18528         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18529         cat $dom > /dev/null
18530         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18531         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18532         ls $dom
18533         rm -f $dom
18534 }
18535 run_test 271a "DoM: data is cached for read after write"
18536
18537 test_271b() {
18538         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18539                 skip "Need MDS version at least 2.10.55"
18540
18541         local dom=$DIR/$tdir/dom
18542
18543         mkdir -p $DIR/$tdir
18544
18545         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18546
18547         lctl set_param -n mdc.*.stats=clear
18548         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18549         cancel_lru_locks mdc
18550         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18551         # second stat to check size is cached on client
18552         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18553         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18554         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18555         rm -f $dom
18556 }
18557 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18558
18559 test_271ba() {
18560         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18561                 skip "Need MDS version at least 2.10.55"
18562
18563         local dom=$DIR/$tdir/dom
18564
18565         mkdir -p $DIR/$tdir
18566
18567         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18568
18569         lctl set_param -n mdc.*.stats=clear
18570         lctl set_param -n osc.*.stats=clear
18571         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18572         cancel_lru_locks mdc
18573         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18574         # second stat to check size is cached on client
18575         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18576         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18577         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18578         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18579         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18580         rm -f $dom
18581 }
18582 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18583
18584
18585 get_mdc_stats() {
18586         local mdtidx=$1
18587         local param=$2
18588         local mdt=MDT$(printf %04x $mdtidx)
18589
18590         if [ -z $param ]; then
18591                 lctl get_param -n mdc.*$mdt*.stats
18592         else
18593                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18594         fi
18595 }
18596
18597 test_271c() {
18598         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18599                 skip "Need MDS version at least 2.10.55"
18600
18601         local dom=$DIR/$tdir/dom
18602
18603         mkdir -p $DIR/$tdir
18604
18605         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18606
18607         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18608         local facet=mds$((mdtidx + 1))
18609
18610         cancel_lru_locks mdc
18611         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18612         createmany -o $dom 1000
18613         lctl set_param -n mdc.*.stats=clear
18614         smalliomany -w $dom 1000 200
18615         get_mdc_stats $mdtidx
18616         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18617         # Each file has 1 open, 1 IO enqueues, total 2000
18618         # but now we have also +1 getxattr for security.capability, total 3000
18619         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18620         unlinkmany $dom 1000
18621
18622         cancel_lru_locks mdc
18623         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18624         createmany -o $dom 1000
18625         lctl set_param -n mdc.*.stats=clear
18626         smalliomany -w $dom 1000 200
18627         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18628         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18629         # for OPEN and IO lock.
18630         [ $((enq - enq_2)) -ge 1000 ] ||
18631                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18632         unlinkmany $dom 1000
18633         return 0
18634 }
18635 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18636
18637 cleanup_271def_tests() {
18638         trap 0
18639         rm -f $1
18640 }
18641
18642 test_271d() {
18643         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18644                 skip "Need MDS version at least 2.10.57"
18645
18646         local dom=$DIR/$tdir/dom
18647         local tmp=$TMP/$tfile
18648         trap "cleanup_271def_tests $tmp" EXIT
18649
18650         mkdir -p $DIR/$tdir
18651
18652         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18653
18654         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18655
18656         cancel_lru_locks mdc
18657         dd if=/dev/urandom of=$tmp bs=1000 count=1
18658         dd if=$tmp of=$dom bs=1000 count=1
18659         cancel_lru_locks mdc
18660
18661         cat /etc/hosts >> $tmp
18662         lctl set_param -n mdc.*.stats=clear
18663
18664         # append data to the same file it should update local page
18665         echo "Append to the same page"
18666         cat /etc/hosts >> $dom
18667         local num=$(get_mdc_stats $mdtidx ost_read)
18668         local ra=$(get_mdc_stats $mdtidx req_active)
18669         local rw=$(get_mdc_stats $mdtidx req_waittime)
18670
18671         [ -z $num ] || error "$num READ RPC occured"
18672         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18673         echo "... DONE"
18674
18675         # compare content
18676         cmp $tmp $dom || error "file miscompare"
18677
18678         cancel_lru_locks mdc
18679         lctl set_param -n mdc.*.stats=clear
18680
18681         echo "Open and read file"
18682         cat $dom > /dev/null
18683         local num=$(get_mdc_stats $mdtidx ost_read)
18684         local ra=$(get_mdc_stats $mdtidx req_active)
18685         local rw=$(get_mdc_stats $mdtidx req_waittime)
18686
18687         [ -z $num ] || error "$num READ RPC occured"
18688         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18689         echo "... DONE"
18690
18691         # compare content
18692         cmp $tmp $dom || error "file miscompare"
18693
18694         return 0
18695 }
18696 run_test 271d "DoM: read on open (1K file in reply buffer)"
18697
18698 test_271f() {
18699         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18700                 skip "Need MDS version at least 2.10.57"
18701
18702         local dom=$DIR/$tdir/dom
18703         local tmp=$TMP/$tfile
18704         trap "cleanup_271def_tests $tmp" EXIT
18705
18706         mkdir -p $DIR/$tdir
18707
18708         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18709
18710         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18711
18712         cancel_lru_locks mdc
18713         dd if=/dev/urandom of=$tmp bs=200000 count=1
18714         dd if=$tmp of=$dom bs=200000 count=1
18715         cancel_lru_locks mdc
18716         cat /etc/hosts >> $tmp
18717         lctl set_param -n mdc.*.stats=clear
18718
18719         echo "Append to the same page"
18720         cat /etc/hosts >> $dom
18721         local num=$(get_mdc_stats $mdtidx ost_read)
18722         local ra=$(get_mdc_stats $mdtidx req_active)
18723         local rw=$(get_mdc_stats $mdtidx req_waittime)
18724
18725         [ -z $num ] || error "$num READ RPC occured"
18726         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18727         echo "... DONE"
18728
18729         # compare content
18730         cmp $tmp $dom || error "file miscompare"
18731
18732         cancel_lru_locks mdc
18733         lctl set_param -n mdc.*.stats=clear
18734
18735         echo "Open and read file"
18736         cat $dom > /dev/null
18737         local num=$(get_mdc_stats $mdtidx ost_read)
18738         local ra=$(get_mdc_stats $mdtidx req_active)
18739         local rw=$(get_mdc_stats $mdtidx req_waittime)
18740
18741         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
18742         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18743         echo "... DONE"
18744
18745         # compare content
18746         cmp $tmp $dom || error "file miscompare"
18747
18748         return 0
18749 }
18750 run_test 271f "DoM: read on open (200K file and read tail)"
18751
18752 test_271g() {
18753         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
18754                 skip "Skipping due to old client or server version"
18755
18756         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
18757         # to get layout
18758         $CHECKSTAT -t file $DIR1/$tfile
18759
18760         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
18761         MULTIOP_PID=$!
18762         sleep 1
18763         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
18764         $LCTL set_param fail_loc=0x80000314
18765         rm $DIR1/$tfile || error "Unlink fails"
18766         RC=$?
18767         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
18768         [ $RC -eq 0 ] || error "Failed write to stale object"
18769 }
18770 run_test 271g "Discard DoM data vs client flush race"
18771
18772 test_272a() {
18773         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18774                 skip "Need MDS version at least 2.11.50"
18775
18776         local dom=$DIR/$tdir/dom
18777         mkdir -p $DIR/$tdir
18778
18779         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
18780         dd if=/dev/urandom of=$dom bs=512K count=1 ||
18781                 error "failed to write data into $dom"
18782         local old_md5=$(md5sum $dom)
18783
18784         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
18785                 error "failed to migrate to the same DoM component"
18786
18787         [ $($LFS getstripe -c $dom) -eq 2 ] ||
18788                 error "layout was not changed silently"
18789
18790         local new_md5=$(md5sum $dom)
18791
18792         [ "$old_md5" != "$new_md5" ] &&
18793                 error "md5sum differ: $old_md5, $new_md5"
18794         return 0
18795 }
18796 run_test 272a "DoM migration: new layout with the same DOM component"
18797
18798 test_272b() {
18799         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18800                 skip "Need MDS version at least 2.11.50"
18801
18802         local dom=$DIR/$tdir/dom
18803         mkdir -p $DIR/$tdir
18804         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18805
18806         local mdtidx=$($LFS getstripe -m $dom)
18807         local mdtname=MDT$(printf %04x $mdtidx)
18808         local facet=mds$((mdtidx + 1))
18809
18810         local mdtfree1=$(do_facet $facet \
18811                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18812         dd if=/dev/urandom of=$dom bs=2M count=1 ||
18813                 error "failed to write data into $dom"
18814         local old_md5=$(md5sum $dom)
18815         cancel_lru_locks mdc
18816         local mdtfree1=$(do_facet $facet \
18817                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18818
18819         $LFS migrate -c2 $dom ||
18820                 error "failed to migrate to the new composite layout"
18821         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18822                 error "MDT stripe was not removed"
18823
18824         cancel_lru_locks mdc
18825         local new_md5=$(md5sum $dom)
18826         [ "$old_md5" != "$new_md5" ] &&
18827                 error "$old_md5 != $new_md5"
18828
18829         # Skip free space checks with ZFS
18830         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18831                 local mdtfree2=$(do_facet $facet \
18832                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18833                 [ $mdtfree2 -gt $mdtfree1 ] ||
18834                         error "MDT space is not freed after migration"
18835         fi
18836         return 0
18837 }
18838 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
18839
18840 test_272c() {
18841         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18842                 skip "Need MDS version at least 2.11.50"
18843
18844         local dom=$DIR/$tdir/$tfile
18845         mkdir -p $DIR/$tdir
18846         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18847
18848         local mdtidx=$($LFS getstripe -m $dom)
18849         local mdtname=MDT$(printf %04x $mdtidx)
18850         local facet=mds$((mdtidx + 1))
18851
18852         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
18853                 error "failed to write data into $dom"
18854         local old_md5=$(md5sum $dom)
18855         cancel_lru_locks mdc
18856         local mdtfree1=$(do_facet $facet \
18857                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18858
18859         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
18860                 error "failed to migrate to the new composite layout"
18861         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18862                 error "MDT stripe was not removed"
18863
18864         cancel_lru_locks mdc
18865         local new_md5=$(md5sum $dom)
18866         [ "$old_md5" != "$new_md5" ] &&
18867                 error "$old_md5 != $new_md5"
18868
18869         # Skip free space checks with ZFS
18870         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18871                 local mdtfree2=$(do_facet $facet \
18872                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18873                 [ $mdtfree2 -gt $mdtfree1 ] ||
18874                         error "MDS space is not freed after migration"
18875         fi
18876         return 0
18877 }
18878 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
18879
18880 test_273a() {
18881         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18882                 skip "Need MDS version at least 2.11.50"
18883
18884         # Layout swap cannot be done if either file has DOM component,
18885         # this will never be supported, migration should be used instead
18886
18887         local dom=$DIR/$tdir/$tfile
18888         mkdir -p $DIR/$tdir
18889
18890         $LFS setstripe -c2 ${dom}_plain
18891         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
18892         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
18893                 error "can swap layout with DoM component"
18894         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
18895                 error "can swap layout with DoM component"
18896
18897         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
18898         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
18899                 error "can swap layout with DoM component"
18900         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
18901                 error "can swap layout with DoM component"
18902         return 0
18903 }
18904 run_test 273a "DoM: layout swapping should fail with DOM"
18905
18906 test_275() {
18907         remote_ost_nodsh && skip "remote OST with nodsh"
18908         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
18909                 skip "Need OST version >= 2.10.57"
18910
18911         local file=$DIR/$tfile
18912         local oss
18913
18914         oss=$(comma_list $(osts_nodes))
18915
18916         dd if=/dev/urandom of=$file bs=1M count=2 ||
18917                 error "failed to create a file"
18918         cancel_lru_locks osc
18919
18920         #lock 1
18921         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18922                 error "failed to read a file"
18923
18924 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
18925         $LCTL set_param fail_loc=0x8000031f
18926
18927         cancel_lru_locks osc &
18928         sleep 1
18929
18930 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
18931         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
18932         #IO takes another lock, but matches the PENDING one
18933         #and places it to the IO RPC
18934         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18935                 error "failed to read a file with PENDING lock"
18936 }
18937 run_test 275 "Read on a canceled duplicate lock"
18938
18939 test_276() {
18940         remote_ost_nodsh && skip "remote OST with nodsh"
18941         local pid
18942
18943         do_facet ost1 "(while true; do \
18944                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
18945                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
18946         pid=$!
18947
18948         for LOOP in $(seq 20); do
18949                 stop ost1
18950                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
18951         done
18952         kill -9 $pid
18953         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
18954                 rm $TMP/sanity_276_pid"
18955 }
18956 run_test 276 "Race between mount and obd_statfs"
18957
18958 cleanup_test_300() {
18959         trap 0
18960         umask $SAVE_UMASK
18961 }
18962 test_striped_dir() {
18963         local mdt_index=$1
18964         local stripe_count
18965         local stripe_index
18966
18967         mkdir -p $DIR/$tdir
18968
18969         SAVE_UMASK=$(umask)
18970         trap cleanup_test_300 RETURN EXIT
18971
18972         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
18973                                                 $DIR/$tdir/striped_dir ||
18974                 error "set striped dir error"
18975
18976         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
18977         [ "$mode" = "755" ] || error "expect 755 got $mode"
18978
18979         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
18980                 error "getdirstripe failed"
18981         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
18982         if [ "$stripe_count" != "2" ]; then
18983                 error "1:stripe_count is $stripe_count, expect 2"
18984         fi
18985         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
18986         if [ "$stripe_count" != "2" ]; then
18987                 error "2:stripe_count is $stripe_count, expect 2"
18988         fi
18989
18990         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
18991         if [ "$stripe_index" != "$mdt_index" ]; then
18992                 error "stripe_index is $stripe_index, expect $mdt_index"
18993         fi
18994
18995         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18996                 error "nlink error after create striped dir"
18997
18998         mkdir $DIR/$tdir/striped_dir/a
18999         mkdir $DIR/$tdir/striped_dir/b
19000
19001         stat $DIR/$tdir/striped_dir/a ||
19002                 error "create dir under striped dir failed"
19003         stat $DIR/$tdir/striped_dir/b ||
19004                 error "create dir under striped dir failed"
19005
19006         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
19007                 error "nlink error after mkdir"
19008
19009         rmdir $DIR/$tdir/striped_dir/a
19010         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
19011                 error "nlink error after rmdir"
19012
19013         rmdir $DIR/$tdir/striped_dir/b
19014         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
19015                 error "nlink error after rmdir"
19016
19017         chattr +i $DIR/$tdir/striped_dir
19018         createmany -o $DIR/$tdir/striped_dir/f 10 &&
19019                 error "immutable flags not working under striped dir!"
19020         chattr -i $DIR/$tdir/striped_dir
19021
19022         rmdir $DIR/$tdir/striped_dir ||
19023                 error "rmdir striped dir error"
19024
19025         cleanup_test_300
19026
19027         true
19028 }
19029
19030 test_300a() {
19031         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19032                 skip "skipped for lustre < 2.7.0"
19033         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19034         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19035
19036         test_striped_dir 0 || error "failed on striped dir on MDT0"
19037         test_striped_dir 1 || error "failed on striped dir on MDT0"
19038 }
19039 run_test 300a "basic striped dir sanity test"
19040
19041 test_300b() {
19042         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19043                 skip "skipped for lustre < 2.7.0"
19044         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19045         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19046
19047         local i
19048         local mtime1
19049         local mtime2
19050         local mtime3
19051
19052         test_mkdir $DIR/$tdir || error "mkdir fail"
19053         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19054                 error "set striped dir error"
19055         for i in {0..9}; do
19056                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
19057                 sleep 1
19058                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
19059                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
19060                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
19061                 sleep 1
19062                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
19063                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
19064                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
19065         done
19066         true
19067 }
19068 run_test 300b "check ctime/mtime for striped dir"
19069
19070 test_300c() {
19071         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19072                 skip "skipped for lustre < 2.7.0"
19073         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19074         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19075
19076         local file_count
19077
19078         mkdir -p $DIR/$tdir
19079         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
19080                 error "set striped dir error"
19081
19082         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
19083                 error "chown striped dir failed"
19084
19085         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
19086                 error "create 5k files failed"
19087
19088         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
19089
19090         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
19091
19092         rm -rf $DIR/$tdir
19093 }
19094 run_test 300c "chown && check ls under striped directory"
19095
19096 test_300d() {
19097         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
19098                 skip "skipped for lustre < 2.7.0"
19099         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19100         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19101
19102         local stripe_count
19103         local file
19104
19105         mkdir -p $DIR/$tdir
19106         $SETSTRIPE -c 2 $DIR/$tdir
19107
19108         #local striped directory
19109         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19110                 error "set striped dir error"
19111         #look at the directories for debug purposes
19112         ls -l $DIR/$tdir
19113         $LFS getdirstripe $DIR/$tdir
19114         ls -l $DIR/$tdir/striped_dir
19115         $LFS getdirstripe $DIR/$tdir/striped_dir
19116         createmany -o $DIR/$tdir/striped_dir/f 10 ||
19117                 error "create 10 files failed"
19118
19119         #remote striped directory
19120         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
19121                 error "set striped dir error"
19122         #look at the directories for debug purposes
19123         ls -l $DIR/$tdir
19124         $LFS getdirstripe $DIR/$tdir
19125         ls -l $DIR/$tdir/remote_striped_dir
19126         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
19127         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
19128                 error "create 10 files failed"
19129
19130         for file in $(find $DIR/$tdir); do
19131                 stripe_count=$($LFS getstripe -c $file)
19132                 [ $stripe_count -eq 2 ] ||
19133                         error "wrong stripe $stripe_count for $file"
19134         done
19135
19136         rm -rf $DIR/$tdir
19137 }
19138 run_test 300d "check default stripe under striped directory"
19139
19140 test_300e() {
19141         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19142                 skip "Need MDS version at least 2.7.55"
19143         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19144         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19145
19146         local stripe_count
19147         local file
19148
19149         mkdir -p $DIR/$tdir
19150
19151         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19152                 error "set striped dir error"
19153
19154         touch $DIR/$tdir/striped_dir/a
19155         touch $DIR/$tdir/striped_dir/b
19156         touch $DIR/$tdir/striped_dir/c
19157
19158         mkdir $DIR/$tdir/striped_dir/dir_a
19159         mkdir $DIR/$tdir/striped_dir/dir_b
19160         mkdir $DIR/$tdir/striped_dir/dir_c
19161
19162         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
19163                 error "set striped adir under striped dir error"
19164
19165         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
19166                 error "set striped bdir under striped dir error"
19167
19168         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
19169                 error "set striped cdir under striped dir error"
19170
19171         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
19172                 error "rename dir under striped dir fails"
19173
19174         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
19175                 error "rename dir under different stripes fails"
19176
19177         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
19178                 error "rename file under striped dir should succeed"
19179
19180         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
19181                 error "rename dir under striped dir should succeed"
19182
19183         rm -rf $DIR/$tdir
19184 }
19185 run_test 300e "check rename under striped directory"
19186
19187 test_300f() {
19188         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19189         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19190         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19191                 skip "Need MDS version at least 2.7.55"
19192
19193         local stripe_count
19194         local file
19195
19196         rm -rf $DIR/$tdir
19197         mkdir -p $DIR/$tdir
19198
19199         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
19200                 error "set striped dir error"
19201
19202         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
19203                 error "set striped dir error"
19204
19205         touch $DIR/$tdir/striped_dir/a
19206         mkdir $DIR/$tdir/striped_dir/dir_a
19207         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
19208                 error "create striped dir under striped dir fails"
19209
19210         touch $DIR/$tdir/striped_dir1/b
19211         mkdir $DIR/$tdir/striped_dir1/dir_b
19212         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
19213                 error "create striped dir under striped dir fails"
19214
19215         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
19216                 error "rename dir under different striped dir should fail"
19217
19218         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
19219                 error "rename striped dir under diff striped dir should fail"
19220
19221         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
19222                 error "rename file under diff striped dirs fails"
19223
19224         rm -rf $DIR/$tdir
19225 }
19226 run_test 300f "check rename cross striped directory"
19227
19228 test_300_check_default_striped_dir()
19229 {
19230         local dirname=$1
19231         local default_count=$2
19232         local default_index=$3
19233         local stripe_count
19234         local stripe_index
19235         local dir_stripe_index
19236         local dir
19237
19238         echo "checking $dirname $default_count $default_index"
19239         $LFS setdirstripe -D -c $default_count -i $default_index \
19240                                 -t all_char $DIR/$tdir/$dirname ||
19241                 error "set default stripe on striped dir error"
19242         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
19243         [ $stripe_count -eq $default_count ] ||
19244                 error "expect $default_count get $stripe_count for $dirname"
19245
19246         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
19247         [ $stripe_index -eq $default_index ] ||
19248                 error "expect $default_index get $stripe_index for $dirname"
19249
19250         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
19251                                                 error "create dirs failed"
19252
19253         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
19254         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
19255         for dir in $(find $DIR/$tdir/$dirname/*); do
19256                 stripe_count=$($LFS getdirstripe -c $dir)
19257                 [ $stripe_count -eq $default_count ] ||
19258                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
19259                 error "stripe count $default_count != $stripe_count for $dir"
19260
19261                 stripe_index=$($LFS getdirstripe -i $dir)
19262                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
19263                         error "$stripe_index != $default_index for $dir"
19264
19265                 #check default stripe
19266                 stripe_count=$($LFS getdirstripe -D -c $dir)
19267                 [ $stripe_count -eq $default_count ] ||
19268                 error "default count $default_count != $stripe_count for $dir"
19269
19270                 stripe_index=$($LFS getdirstripe -D -i $dir)
19271                 [ $stripe_index -eq $default_index ] ||
19272                 error "default index $default_index != $stripe_index for $dir"
19273         done
19274         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
19275 }
19276
19277 test_300g() {
19278         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19279         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19280                 skip "Need MDS version at least 2.7.55"
19281
19282         local dir
19283         local stripe_count
19284         local stripe_index
19285
19286         mkdir $DIR/$tdir
19287         mkdir $DIR/$tdir/normal_dir
19288
19289         #Checking when client cache stripe index
19290         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
19291         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
19292                 error "create striped_dir failed"
19293
19294         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
19295                 error "create dir0 fails"
19296         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
19297         [ $stripe_index -eq 0 ] ||
19298                 error "dir0 expect index 0 got $stripe_index"
19299
19300         mkdir $DIR/$tdir/striped_dir/dir1 ||
19301                 error "create dir1 fails"
19302         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
19303         [ $stripe_index -eq 1 ] ||
19304                 error "dir1 expect index 1 got $stripe_index"
19305
19306         #check default stripe count/stripe index
19307         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
19308         test_300_check_default_striped_dir normal_dir 1 0
19309         test_300_check_default_striped_dir normal_dir 2 1
19310         test_300_check_default_striped_dir normal_dir 2 -1
19311
19312         #delete default stripe information
19313         echo "delete default stripeEA"
19314         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
19315                 error "set default stripe on striped dir error"
19316
19317         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
19318         for dir in $(find $DIR/$tdir/normal_dir/*); do
19319                 stripe_count=$($LFS getdirstripe -c $dir)
19320                 [ $stripe_count -eq 0 ] ||
19321                         error "expect 1 get $stripe_count for $dir"
19322                 stripe_index=$($LFS getdirstripe -i $dir)
19323                 [ $stripe_index -eq 0 ] ||
19324                         error "expect 0 get $stripe_index for $dir"
19325         done
19326 }
19327 run_test 300g "check default striped directory for normal directory"
19328
19329 test_300h() {
19330         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19331         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19332                 skip "Need MDS version at least 2.7.55"
19333
19334         local dir
19335         local stripe_count
19336
19337         mkdir $DIR/$tdir
19338         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19339                 error "set striped dir error"
19340
19341         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
19342         test_300_check_default_striped_dir striped_dir 1 0
19343         test_300_check_default_striped_dir striped_dir 2 1
19344         test_300_check_default_striped_dir striped_dir 2 -1
19345
19346         #delete default stripe information
19347         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
19348                 error "set default stripe on striped dir error"
19349
19350         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
19351         for dir in $(find $DIR/$tdir/striped_dir/*); do
19352                 stripe_count=$($LFS getdirstripe -c $dir)
19353                 [ $stripe_count -eq 0 ] ||
19354                         error "expect 1 get $stripe_count for $dir"
19355         done
19356 }
19357 run_test 300h "check default striped directory for striped directory"
19358
19359 test_300i() {
19360         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19361         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19362         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19363                 skip "Need MDS version at least 2.7.55"
19364
19365         local stripe_count
19366         local file
19367
19368         mkdir $DIR/$tdir
19369
19370         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19371                 error "set striped dir error"
19372
19373         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19374                 error "create files under striped dir failed"
19375
19376         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
19377                 error "set striped hashdir error"
19378
19379         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
19380                 error "create dir0 under hash dir failed"
19381         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
19382                 error "create dir1 under hash dir failed"
19383
19384         # unfortunately, we need to umount to clear dir layout cache for now
19385         # once we fully implement dir layout, we can drop this
19386         umount_client $MOUNT || error "umount failed"
19387         mount_client $MOUNT || error "mount failed"
19388
19389         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
19390         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
19391         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
19392
19393         #set the stripe to be unknown hash type
19394         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
19395         $LCTL set_param fail_loc=0x1901
19396         for ((i = 0; i < 10; i++)); do
19397                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
19398                         error "stat f-$i failed"
19399                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
19400         done
19401
19402         touch $DIR/$tdir/striped_dir/f0 &&
19403                 error "create under striped dir with unknown hash should fail"
19404
19405         $LCTL set_param fail_loc=0
19406
19407         umount_client $MOUNT || error "umount failed"
19408         mount_client $MOUNT || error "mount failed"
19409
19410         return 0
19411 }
19412 run_test 300i "client handle unknown hash type striped directory"
19413
19414 test_300j() {
19415         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19417         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19418                 skip "Need MDS version at least 2.7.55"
19419
19420         local stripe_count
19421         local file
19422
19423         mkdir $DIR/$tdir
19424
19425         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
19426         $LCTL set_param fail_loc=0x1702
19427         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
19428                 error "set striped dir error"
19429
19430         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
19431                 error "create files under striped dir failed"
19432
19433         $LCTL set_param fail_loc=0
19434
19435         rm -rf $DIR/$tdir || error "unlink striped dir fails"
19436
19437         return 0
19438 }
19439 run_test 300j "test large update record"
19440
19441 test_300k() {
19442         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19443         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19444         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19445                 skip "Need MDS version at least 2.7.55"
19446
19447         local stripe_count
19448         local file
19449
19450         mkdir $DIR/$tdir
19451
19452         #define OBD_FAIL_LARGE_STRIPE   0x1703
19453         $LCTL set_param fail_loc=0x1703
19454         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
19455                 error "set striped dir error"
19456         $LCTL set_param fail_loc=0
19457
19458         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19459                 error "getstripeddir fails"
19460         rm -rf $DIR/$tdir/striped_dir ||
19461                 error "unlink striped dir fails"
19462
19463         return 0
19464 }
19465 run_test 300k "test large striped directory"
19466
19467 test_300l() {
19468         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19469         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19470         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19471                 skip "Need MDS version at least 2.7.55"
19472
19473         local stripe_index
19474
19475         test_mkdir -p $DIR/$tdir/striped_dir
19476         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
19477                         error "chown $RUNAS_ID failed"
19478         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19479                 error "set default striped dir failed"
19480
19481         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19482         $LCTL set_param fail_loc=0x80000158
19483         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19484
19485         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19486         [ $stripe_index -eq 1 ] ||
19487                 error "expect 1 get $stripe_index for $dir"
19488 }
19489 run_test 300l "non-root user to create dir under striped dir with stale layout"
19490
19491 test_300m() {
19492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19493         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19494         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19495                 skip "Need MDS version at least 2.7.55"
19496
19497         mkdir -p $DIR/$tdir/striped_dir
19498         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19499                 error "set default stripes dir error"
19500
19501         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19502
19503         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19504         [ $stripe_count -eq 0 ] ||
19505                         error "expect 0 get $stripe_count for a"
19506
19507         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19508                 error "set default stripes dir error"
19509
19510         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19511
19512         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19513         [ $stripe_count -eq 0 ] ||
19514                         error "expect 0 get $stripe_count for b"
19515
19516         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19517                 error "set default stripes dir error"
19518
19519         mkdir $DIR/$tdir/striped_dir/c &&
19520                 error "default stripe_index is invalid, mkdir c should fails"
19521
19522         rm -rf $DIR/$tdir || error "rmdir fails"
19523 }
19524 run_test 300m "setstriped directory on single MDT FS"
19525
19526 cleanup_300n() {
19527         local list=$(comma_list $(mdts_nodes))
19528
19529         trap 0
19530         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19531 }
19532
19533 test_300n() {
19534         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19535         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19536         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19537                 skip "Need MDS version at least 2.7.55"
19538         remote_mds_nodsh && skip "remote MDS with nodsh"
19539
19540         local stripe_index
19541         local list=$(comma_list $(mdts_nodes))
19542
19543         trap cleanup_300n RETURN EXIT
19544         mkdir -p $DIR/$tdir
19545         chmod 777 $DIR/$tdir
19546         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19547                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19548                 error "create striped dir succeeds with gid=0"
19549
19550         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19551         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19552                 error "create striped dir fails with gid=-1"
19553
19554         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19555         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
19556                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19557                 error "set default striped dir succeeds with gid=0"
19558
19559
19560         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19561         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
19562                 error "set default striped dir fails with gid=-1"
19563
19564
19565         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19566         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
19567                                         error "create test_dir fails"
19568         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
19569                                         error "create test_dir1 fails"
19570         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
19571                                         error "create test_dir2 fails"
19572         cleanup_300n
19573 }
19574 run_test 300n "non-root user to create dir under striped dir with default EA"
19575
19576 test_300o() {
19577         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19578         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19579         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19580                 skip "Need MDS version at least 2.7.55"
19581
19582         local numfree1
19583         local numfree2
19584
19585         mkdir -p $DIR/$tdir
19586
19587         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
19588         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
19589         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
19590                 skip "not enough free inodes $numfree1 $numfree2"
19591         fi
19592
19593         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
19594         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
19595         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
19596                 skip "not enough free space $numfree1 $numfree2"
19597         fi
19598
19599         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
19600                 error "setdirstripe fails"
19601
19602         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
19603                 error "create dirs fails"
19604
19605         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
19606         ls $DIR/$tdir/striped_dir > /dev/null ||
19607                 error "ls striped dir fails"
19608         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
19609                 error "unlink big striped dir fails"
19610 }
19611 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
19612
19613 test_300p() {
19614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19615         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19616         remote_mds_nodsh && skip "remote MDS with nodsh"
19617
19618         mkdir -p $DIR/$tdir
19619
19620         #define OBD_FAIL_OUT_ENOSPC     0x1704
19621         do_facet mds2 lctl set_param fail_loc=0x80001704
19622         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
19623                  && error "create striped directory should fail"
19624
19625         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
19626
19627         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
19628         true
19629 }
19630 run_test 300p "create striped directory without space"
19631
19632 test_300q() {
19633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19634         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19635
19636         local fd=$(free_fd)
19637         local cmd="exec $fd<$tdir"
19638         cd $DIR
19639         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
19640         eval $cmd
19641         cmd="exec $fd<&-"
19642         trap "eval $cmd" EXIT
19643         cd $tdir || error "cd $tdir fails"
19644         rmdir  ../$tdir || error "rmdir $tdir fails"
19645         mkdir local_dir && error "create dir succeeds"
19646         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
19647         eval $cmd
19648         return 0
19649 }
19650 run_test 300q "create remote directory under orphan directory"
19651
19652 test_300r() {
19653         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
19654                 skip "Need MDS version at least 2.7.55" && return
19655         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19656
19657         mkdir $DIR/$tdir
19658
19659         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
19660                 error "set striped dir error"
19661
19662         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19663                 error "getstripeddir fails"
19664
19665         local stripe_count
19666         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
19667                       awk '/lmv_stripe_count:/ { print $2 }')
19668
19669         [ $MDSCOUNT -ne $stripe_count ] &&
19670                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
19671
19672         rm -rf $DIR/$tdir/striped_dir ||
19673                 error "unlink striped dir fails"
19674 }
19675 run_test 300r "test -1 striped directory"
19676
19677 test_300s_helper() {
19678         local count=$1
19679
19680         local stripe_dir=$DIR/$tdir/striped_dir.$count
19681
19682         $LFS mkdir -c $count $stripe_dir ||
19683                 error "lfs mkdir -c error"
19684
19685         $LFS getdirstripe $stripe_dir ||
19686                 error "lfs getdirstripe fails"
19687
19688         local stripe_count
19689         stripe_count=$($LFS getdirstripe $stripe_dir |
19690                       awk '/lmv_stripe_count:/ { print $2 }')
19691
19692         [ $count -ne $stripe_count ] &&
19693                 error_noexit "bad stripe count $stripe_count expected $count"
19694
19695         local dupe_stripes
19696         dupe_stripes=$($LFS getdirstripe $stripe_dir |
19697                 awk '/0x/ {count[$1] += 1}; END {
19698                         for (idx in count) {
19699                                 if (count[idx]>1) {
19700                                         print "index " idx " count " count[idx]
19701                                 }
19702                         }
19703                 }')
19704
19705         if [[ -n "$dupe_stripes" ]] ; then
19706                 lfs getdirstripe $stripe_dir
19707                 error_noexit "Dupe MDT above: $dupe_stripes "
19708         fi
19709
19710         rm -rf $stripe_dir ||
19711                 error_noexit "unlink $stripe_dir fails"
19712 }
19713
19714 test_300s() {
19715         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19716                 skip "Need MDS version at least 2.7.55" && return
19717         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19718
19719         mkdir $DIR/$tdir
19720         for count in $(seq 2 $MDSCOUNT); do
19721                 test_300s_helper $count
19722         done
19723 }
19724 run_test 300s "test lfs mkdir -c without -i"
19725
19726
19727 prepare_remote_file() {
19728         mkdir $DIR/$tdir/src_dir ||
19729                 error "create remote source failed"
19730
19731         cp /etc/hosts $DIR/$tdir/src_dir/a ||
19732                  error "cp to remote source failed"
19733         touch $DIR/$tdir/src_dir/a
19734
19735         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
19736                 error "create remote target dir failed"
19737
19738         touch $DIR/$tdir/tgt_dir/b
19739
19740         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
19741                 error "rename dir cross MDT failed!"
19742
19743         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
19744                 error "src_child still exists after rename"
19745
19746         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
19747                 error "missing file(a) after rename"
19748
19749         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
19750                 error "diff after rename"
19751 }
19752
19753 test_310a() {
19754         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19755         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19756
19757         local remote_file=$DIR/$tdir/tgt_dir/b
19758
19759         mkdir -p $DIR/$tdir
19760
19761         prepare_remote_file || error "prepare remote file failed"
19762
19763         #open-unlink file
19764         $OPENUNLINK $remote_file $remote_file ||
19765                 error "openunlink $remote_file failed"
19766         $CHECKSTAT -a $remote_file || error "$remote_file exists"
19767 }
19768 run_test 310a "open unlink remote file"
19769
19770 test_310b() {
19771         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19772         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19773
19774         local remote_file=$DIR/$tdir/tgt_dir/b
19775
19776         mkdir -p $DIR/$tdir
19777
19778         prepare_remote_file || error "prepare remote file failed"
19779
19780         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19781         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
19782         $CHECKSTAT -t file $remote_file || error "check file failed"
19783 }
19784 run_test 310b "unlink remote file with multiple links while open"
19785
19786 test_310c() {
19787         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19788         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
19789
19790         local remote_file=$DIR/$tdir/tgt_dir/b
19791
19792         mkdir -p $DIR/$tdir
19793
19794         prepare_remote_file || error "prepare remote file failed"
19795
19796         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19797         multiop_bg_pause $remote_file O_uc ||
19798                         error "mulitop failed for remote file"
19799         MULTIPID=$!
19800         $MULTIOP $DIR/$tfile Ouc
19801         kill -USR1 $MULTIPID
19802         wait $MULTIPID
19803 }
19804 run_test 310c "open-unlink remote file with multiple links"
19805
19806 #LU-4825
19807 test_311() {
19808         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19809         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19810         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
19811                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
19812         remote_mds_nodsh && skip "remote MDS with nodsh"
19813
19814         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19815         local mdts=$(comma_list $(mdts_nodes))
19816
19817         mkdir -p $DIR/$tdir
19818         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
19819         createmany -o $DIR/$tdir/$tfile. 1000
19820
19821         # statfs data is not real time, let's just calculate it
19822         old_iused=$((old_iused + 1000))
19823
19824         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19825                         osp.*OST0000*MDT0000.create_count")
19826         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19827                                 osp.*OST0000*MDT0000.max_create_count")
19828         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
19829
19830         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
19831         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
19832         [ $index -ne 0 ] || error "$tfile stripe index is 0"
19833
19834         unlinkmany $DIR/$tdir/$tfile. 1000
19835
19836         do_nodes $mdts "$LCTL set_param -n \
19837                         osp.*OST0000*.max_create_count=$max_count"
19838         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
19839                 do_nodes $mdts "$LCTL set_param -n \
19840                                 osp.*OST0000*.create_count=$count"
19841         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
19842                         grep "=0" && error "create_count is zero"
19843
19844         local new_iused
19845         for i in $(seq 120); do
19846                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19847                 # system may be too busy to destroy all objs in time, use
19848                 # a somewhat small value to not fail autotest
19849                 [ $((old_iused - new_iused)) -gt 400 ] && break
19850                 sleep 1
19851         done
19852
19853         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
19854         [ $((old_iused - new_iused)) -gt 400 ] ||
19855                 error "objs not destroyed after unlink"
19856 }
19857 run_test 311 "disable OSP precreate, and unlink should destroy objs"
19858
19859 zfs_oid_to_objid()
19860 {
19861         local ost=$1
19862         local objid=$2
19863
19864         local vdevdir=$(dirname $(facet_vdevice $ost))
19865         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
19866         local zfs_zapid=$(do_facet $ost $cmd |
19867                           grep -w "/O/0/d$((objid%32))" -C 5 |
19868                           awk '/Object/{getline; print $1}')
19869         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
19870                           awk "/$objid = /"'{printf $3}')
19871
19872         echo $zfs_objid
19873 }
19874
19875 zfs_object_blksz() {
19876         local ost=$1
19877         local objid=$2
19878
19879         local vdevdir=$(dirname $(facet_vdevice $ost))
19880         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
19881         local blksz=$(do_facet $ost $cmd $objid |
19882                       awk '/dblk/{getline; printf $4}')
19883
19884         case "${blksz: -1}" in
19885                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
19886                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
19887                 *) ;;
19888         esac
19889
19890         echo $blksz
19891 }
19892
19893 test_312() { # LU-4856
19894         remote_ost_nodsh && skip "remote OST with nodsh"
19895         [ "$ost1_FSTYPE" = "zfs" ] ||
19896                 skip_env "the test only applies to zfs"
19897
19898         local max_blksz=$(do_facet ost1 \
19899                           $ZFS get -p recordsize $(facet_device ost1) |
19900                           awk '!/VALUE/{print $3}')
19901
19902         # to make life a little bit easier
19903         $LFS mkdir -c 1 -i 0 $DIR/$tdir
19904         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19905
19906         local tf=$DIR/$tdir/$tfile
19907         touch $tf
19908         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19909
19910         # Get ZFS object id
19911         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19912         # block size change by sequential overwrite
19913         local bs
19914
19915         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
19916                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
19917
19918                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
19919                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
19920         done
19921         rm -f $tf
19922
19923         # block size change by sequential append write
19924         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
19925         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19926         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19927         local count
19928
19929         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
19930                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
19931                         oflag=sync conv=notrunc
19932
19933                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
19934                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
19935                         error "blksz error, actual $blksz, " \
19936                                 "expected: 2 * $count * $PAGE_SIZE"
19937         done
19938         rm -f $tf
19939
19940         # random write
19941         touch $tf
19942         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19943         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19944
19945         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
19946         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19947         [ $blksz -eq $PAGE_SIZE ] ||
19948                 error "blksz error: $blksz, expected: $PAGE_SIZE"
19949
19950         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
19951         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19952         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
19953
19954         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
19955         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19956         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
19957 }
19958 run_test 312 "make sure ZFS adjusts its block size by write pattern"
19959
19960 test_313() {
19961         remote_ost_nodsh && skip "remote OST with nodsh"
19962
19963         local file=$DIR/$tfile
19964
19965         rm -f $file
19966         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
19967
19968         # define OBD_FAIL_TGT_RCVD_EIO           0x720
19969         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19970         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
19971                 error "write should failed"
19972         do_facet ost1 "$LCTL set_param fail_loc=0"
19973         rm -f $file
19974 }
19975 run_test 313 "io should fail after last_rcvd update fail"
19976
19977 test_314() {
19978         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19979
19980         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
19981         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19982         rm -f $DIR/$tfile
19983         wait_delete_completed
19984         do_facet ost1 "$LCTL set_param fail_loc=0"
19985 }
19986 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
19987
19988 test_315() { # LU-618
19989         local file=$DIR/$tfile
19990         rm -f $file
19991
19992         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
19993                 error "multiop file write failed"
19994         $MULTIOP $file oO_RDONLY:r4063232_c &
19995         PID=$!
19996
19997         sleep 2
19998
19999         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
20000         kill -USR1 $PID
20001
20002         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
20003         rm -f $file
20004 }
20005 run_test 315 "read should be accounted"
20006
20007 test_316() {
20008         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20009         large_xattr_enabled || skip_env "ea_inode feature disabled"
20010
20011         rm -rf $DIR/$tdir/d
20012         mkdir -p $DIR/$tdir/d
20013         chown nobody $DIR/$tdir/d
20014         touch $DIR/$tdir/d/file
20015
20016         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
20017 }
20018 run_test 316 "lfs mv"
20019
20020 test_317() {
20021         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
20022                 skip "Need MDS version at least 2.11.53"
20023         if [ "$ost1_FSTYPE" == "zfs" ]; then
20024                 skip "LU-10370: no implementation for ZFS"
20025         fi
20026
20027         local trunc_sz
20028         local grant_blk_size
20029
20030         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
20031                         awk '/grant_block_size:/ { print $2; exit; }')
20032         #
20033         # Create File of size 5M. Truncate it to below size's and verify
20034         # blocks count.
20035         #
20036         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
20037                 error "Create file $DIR/$tfile failed"
20038         stack_trap "rm -f $DIR/$tfile" EXIT
20039
20040         for trunc_sz in 2097152 4097 4000 509 0; do
20041                 $TRUNCATE $DIR/$tfile $trunc_sz ||
20042                         error "truncate $tfile to $trunc_sz failed"
20043                 local sz=$(stat --format=%s $DIR/$tfile)
20044                 local blk=$(stat --format=%b $DIR/$tfile)
20045                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
20046                                      grant_blk_size) * 8))
20047
20048                 if [[ $blk -ne $trunc_blk ]]; then
20049                         $(which stat) $DIR/$tfile
20050                         error "Expected Block $trunc_blk got $blk for $tfile"
20051                 fi
20052
20053                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20054                         error "Expected Size $trunc_sz got $sz for $tfile"
20055         done
20056
20057         #
20058         # sparse file test
20059         # Create file with a hole and write actual two blocks. Block count
20060         # must be 16.
20061         #
20062         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
20063                 conv=fsync || error "Create file : $DIR/$tfile"
20064
20065         # Calculate the final truncate size.
20066         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
20067
20068         #
20069         # truncate to size $trunc_sz bytes. Strip the last block
20070         # The block count must drop to 8
20071         #
20072         $TRUNCATE $DIR/$tfile $trunc_sz ||
20073                 error "truncate $tfile to $trunc_sz failed"
20074
20075         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
20076         sz=$(stat --format=%s $DIR/$tfile)
20077         blk=$(stat --format=%b $DIR/$tfile)
20078
20079         if [[ $blk -ne $trunc_bsz ]]; then
20080                 $(which stat) $DIR/$tfile
20081                 error "Expected Block $trunc_bsz got $blk for $tfile"
20082         fi
20083
20084         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
20085                 error "Expected Size $trunc_sz got $sz for $tfile"
20086 }
20087 run_test 317 "Verify blocks get correctly update after truncate"
20088
20089 test_fake_rw() {
20090         local read_write=$1
20091         if [ "$read_write" = "write" ]; then
20092                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
20093         elif [ "$read_write" = "read" ]; then
20094                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
20095         else
20096                 error "argument error"
20097         fi
20098
20099         # turn off debug for performance testing
20100         local saved_debug=$($LCTL get_param -n debug)
20101         $LCTL set_param debug=0
20102
20103         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
20104
20105         # get ost1 size - lustre-OST0000
20106         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
20107         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
20108         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
20109
20110         if [ "$read_write" = "read" ]; then
20111                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
20112         fi
20113
20114         local start_time=$(date +%s.%N)
20115         $dd_cmd bs=1M count=$blocks oflag=sync ||
20116                 error "real dd $read_write error"
20117         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
20118
20119         if [ "$read_write" = "write" ]; then
20120                 rm -f $DIR/$tfile
20121         fi
20122
20123         # define OBD_FAIL_OST_FAKE_RW           0x238
20124         do_facet ost1 $LCTL set_param fail_loc=0x238
20125
20126         local start_time=$(date +%s.%N)
20127         $dd_cmd bs=1M count=$blocks oflag=sync ||
20128                 error "fake dd $read_write error"
20129         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
20130
20131         if [ "$read_write" = "write" ]; then
20132                 # verify file size
20133                 cancel_lru_locks osc
20134                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
20135                         error "$tfile size not $blocks MB"
20136         fi
20137         do_facet ost1 $LCTL set_param fail_loc=0
20138
20139         echo "fake $read_write $duration_fake vs. normal $read_write" \
20140                 "$duration in seconds"
20141         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
20142                 error_not_in_vm "fake write is slower"
20143
20144         $LCTL set_param -n debug="$saved_debug"
20145         rm -f $DIR/$tfile
20146 }
20147 test_399a() { # LU-7655 for OST fake write
20148         remote_ost_nodsh && skip "remote OST with nodsh"
20149
20150         test_fake_rw write
20151 }
20152 run_test 399a "fake write should not be slower than normal write"
20153
20154 test_399b() { # LU-8726 for OST fake read
20155         remote_ost_nodsh && skip "remote OST with nodsh"
20156         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
20157                 skip_env "ldiskfs only test"
20158         fi
20159
20160         test_fake_rw read
20161 }
20162 run_test 399b "fake read should not be slower than normal read"
20163
20164 test_400a() { # LU-1606, was conf-sanity test_74
20165         if ! which $CC > /dev/null 2>&1; then
20166                 skip_env "$CC is not installed"
20167         fi
20168
20169         local extra_flags=''
20170         local out=$TMP/$tfile
20171         local prefix=/usr/include/lustre
20172         local prog
20173
20174         # Oleg removes c files in his test rig so test if any c files exist
20175         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
20176                 skip_env "Needed c test files are missing"
20177
20178         if ! [[ -d $prefix ]]; then
20179                 # Assume we're running in tree and fixup the include path.
20180                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
20181                 extra_flags+=" -L$LUSTRE/utils/.lib"
20182         fi
20183
20184         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
20185                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
20186                         error "client api broken"
20187         done
20188         rm -f $out
20189 }
20190 run_test 400a "Lustre client api program can compile and link"
20191
20192 test_400b() { # LU-1606, LU-5011
20193         local header
20194         local out=$TMP/$tfile
20195         local prefix=/usr/include/linux/lustre
20196
20197         # We use a hard coded prefix so that this test will not fail
20198         # when run in tree. There are headers in lustre/include/lustre/
20199         # that are not packaged (like lustre_idl.h) and have more
20200         # complicated include dependencies (like config.h and lnet/types.h).
20201         # Since this test about correct packaging we just skip them when
20202         # they don't exist (see below) rather than try to fixup cppflags.
20203
20204         if ! which $CC > /dev/null 2>&1; then
20205                 skip_env "$CC is not installed"
20206         fi
20207
20208         for header in $prefix/*.h; do
20209                 if ! [[ -f "$header" ]]; then
20210                         continue
20211                 fi
20212
20213                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
20214                         continue # lustre_ioctl.h is internal header
20215                 fi
20216
20217                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
20218                         error "cannot compile '$header'"
20219         done
20220         rm -f $out
20221 }
20222 run_test 400b "packaged headers can be compiled"
20223
20224 test_401a() { #LU-7437
20225         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
20226         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
20227
20228         #count the number of parameters by "list_param -R"
20229         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
20230         #count the number of parameters by listing proc files
20231         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
20232         echo "proc_dirs='$proc_dirs'"
20233         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
20234         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
20235                       sort -u | wc -l)
20236
20237         [ $params -eq $procs ] ||
20238                 error "found $params parameters vs. $procs proc files"
20239
20240         # test the list_param -D option only returns directories
20241         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
20242         #count the number of parameters by listing proc directories
20243         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
20244                 sort -u | wc -l)
20245
20246         [ $params -eq $procs ] ||
20247                 error "found $params parameters vs. $procs proc files"
20248 }
20249 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
20250
20251 test_401b() {
20252         local save=$($LCTL get_param -n jobid_var)
20253         local tmp=testing
20254
20255         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
20256                 error "no error returned when setting bad parameters"
20257
20258         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
20259         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
20260
20261         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
20262         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
20263         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
20264 }
20265 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
20266
20267 test_401c() {
20268         local jobid_var_old=$($LCTL get_param -n jobid_var)
20269         local jobid_var_new
20270
20271         $LCTL set_param jobid_var= &&
20272                 error "no error returned for 'set_param a='"
20273
20274         jobid_var_new=$($LCTL get_param -n jobid_var)
20275         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20276                 error "jobid_var was changed by setting without value"
20277
20278         $LCTL set_param jobid_var &&
20279                 error "no error returned for 'set_param a'"
20280
20281         jobid_var_new=$($LCTL get_param -n jobid_var)
20282         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
20283                 error "jobid_var was changed by setting without value"
20284 }
20285 run_test 401c "Verify 'lctl set_param' without value fails in either format."
20286
20287 test_401d() {
20288         local jobid_var_old=$($LCTL get_param -n jobid_var)
20289         local jobid_var_new
20290         local new_value="foo=bar"
20291
20292         $LCTL set_param jobid_var=$new_value ||
20293                 error "'set_param a=b' did not accept a value containing '='"
20294
20295         jobid_var_new=$($LCTL get_param -n jobid_var)
20296         [[ "$jobid_var_new" == "$new_value" ]] ||
20297                 error "'set_param a=b' failed on a value containing '='"
20298
20299         # Reset the jobid_var to test the other format
20300         $LCTL set_param jobid_var=$jobid_var_old
20301         jobid_var_new=$($LCTL get_param -n jobid_var)
20302         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20303                 error "failed to reset jobid_var"
20304
20305         $LCTL set_param jobid_var $new_value ||
20306                 error "'set_param a b' did not accept a value containing '='"
20307
20308         jobid_var_new=$($LCTL get_param -n jobid_var)
20309         [[ "$jobid_var_new" == "$new_value" ]] ||
20310                 error "'set_param a b' failed on a value containing '='"
20311
20312         $LCTL set_param jobid_var $jobid_var_old
20313         jobid_var_new=$($LCTL get_param -n jobid_var)
20314         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
20315                 error "failed to reset jobid_var"
20316 }
20317 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
20318
20319 test_402() {
20320         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
20321         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
20322                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
20323         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
20324                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
20325                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
20326         remote_mds_nodsh && skip "remote MDS with nodsh"
20327
20328         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
20329 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
20330         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
20331         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
20332                 echo "Touch failed - OK"
20333 }
20334 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
20335
20336 test_403() {
20337         local file1=$DIR/$tfile.1
20338         local file2=$DIR/$tfile.2
20339         local tfile=$TMP/$tfile
20340
20341         rm -f $file1 $file2 $tfile
20342
20343         touch $file1
20344         ln $file1 $file2
20345
20346         # 30 sec OBD_TIMEOUT in ll_getattr()
20347         # right before populating st_nlink
20348         $LCTL set_param fail_loc=0x80001409
20349         stat -c %h $file1 > $tfile &
20350
20351         # create an alias, drop all locks and reclaim the dentry
20352         < $file2
20353         cancel_lru_locks mdc
20354         cancel_lru_locks osc
20355         sysctl -w vm.drop_caches=2
20356
20357         wait
20358
20359         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
20360
20361         rm -f $tfile $file1 $file2
20362 }
20363 run_test 403 "i_nlink should not drop to zero due to aliasing"
20364
20365 test_404() { # LU-6601
20366         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
20367                 skip "Need server version newer than 2.8.52"
20368         remote_mds_nodsh && skip "remote MDS with nodsh"
20369
20370         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
20371                 awk '/osp .*-osc-MDT/ { print $4}')
20372
20373         local osp
20374         for osp in $mosps; do
20375                 echo "Deactivate: " $osp
20376                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
20377                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20378                         awk -vp=$osp '$4 == p { print $2 }')
20379                 [ $stat = IN ] || {
20380                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20381                         error "deactivate error"
20382                 }
20383                 echo "Activate: " $osp
20384                 do_facet $SINGLEMDS $LCTL --device %$osp activate
20385                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
20386                         awk -vp=$osp '$4 == p { print $2 }')
20387                 [ $stat = UP ] || {
20388                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
20389                         error "activate error"
20390                 }
20391         done
20392 }
20393 run_test 404 "validate manual {de}activated works properly for OSPs"
20394
20395 test_405() {
20396         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
20397                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
20398                         skip "Layout swap lock is not supported"
20399
20400         check_swap_layouts_support
20401
20402         test_mkdir $DIR/$tdir
20403         swap_lock_test -d $DIR/$tdir ||
20404                 error "One layout swap locked test failed"
20405 }
20406 run_test 405 "Various layout swap lock tests"
20407
20408 test_406() {
20409         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20410         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20411         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
20412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20413         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
20414                 skip "Need MDS version at least 2.8.50"
20415
20416         local def_stripe_size=$($LFS getstripe -S $MOUNT)
20417         local test_pool=$TESTNAME
20418
20419         pool_add $test_pool || error "pool_add failed"
20420         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
20421                 error "pool_add_targets failed"
20422
20423         save_layout_restore_at_exit $MOUNT
20424
20425         # parent set default stripe count only, child will stripe from both
20426         # parent and fs default
20427         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
20428                 error "setstripe $MOUNT failed"
20429         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
20430         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
20431         for i in $(seq 10); do
20432                 local f=$DIR/$tdir/$tfile.$i
20433                 touch $f || error "touch failed"
20434                 local count=$($LFS getstripe -c $f)
20435                 [ $count -eq $OSTCOUNT ] ||
20436                         error "$f stripe count $count != $OSTCOUNT"
20437                 local offset=$($LFS getstripe -i $f)
20438                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
20439                 local size=$($LFS getstripe -S $f)
20440                 [ $size -eq $((def_stripe_size * 2)) ] ||
20441                         error "$f stripe size $size != $((def_stripe_size * 2))"
20442                 local pool=$($LFS getstripe -p $f)
20443                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
20444         done
20445
20446         # change fs default striping, delete parent default striping, now child
20447         # will stripe from new fs default striping only
20448         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
20449                 error "change $MOUNT default stripe failed"
20450         $LFS setstripe -c 0 $DIR/$tdir ||
20451                 error "delete $tdir default stripe failed"
20452         for i in $(seq 11 20); do
20453                 local f=$DIR/$tdir/$tfile.$i
20454                 touch $f || error "touch $f failed"
20455                 local count=$($LFS getstripe -c $f)
20456                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
20457                 local offset=$($LFS getstripe -i $f)
20458                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
20459                 local size=$($LFS getstripe -S $f)
20460                 [ $size -eq $def_stripe_size ] ||
20461                         error "$f stripe size $size != $def_stripe_size"
20462                 local pool=$($LFS getstripe -p $f)
20463                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
20464         done
20465
20466         unlinkmany $DIR/$tdir/$tfile. 1 20
20467
20468         local f=$DIR/$tdir/$tfile
20469         pool_remove_all_targets $test_pool $f
20470         pool_remove $test_pool $f
20471 }
20472 run_test 406 "DNE support fs default striping"
20473
20474 test_407() {
20475         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20476         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
20477                 skip "Need MDS version at least 2.8.55"
20478         remote_mds_nodsh && skip "remote MDS with nodsh"
20479
20480         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
20481                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
20482         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
20483                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
20484         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
20485
20486         #define OBD_FAIL_DT_TXN_STOP    0x2019
20487         for idx in $(seq $MDSCOUNT); do
20488                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
20489         done
20490         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
20491         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
20492                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
20493         true
20494 }
20495 run_test 407 "transaction fail should cause operation fail"
20496
20497 test_408() {
20498         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
20499
20500         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
20501         lctl set_param fail_loc=0x8000040a
20502         # let ll_prepare_partial_page() fail
20503         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
20504
20505         rm -f $DIR/$tfile
20506
20507         # create at least 100 unused inodes so that
20508         # shrink_icache_memory(0) should not return 0
20509         touch $DIR/$tfile-{0..100}
20510         rm -f $DIR/$tfile-{0..100}
20511         sync
20512
20513         echo 2 > /proc/sys/vm/drop_caches
20514 }
20515 run_test 408 "drop_caches should not hang due to page leaks"
20516
20517 test_409()
20518 {
20519         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20520         check_mount_and_prep
20521
20522         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
20523         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
20524         touch $DIR/$tdir/guard || error "(2) Fail to create"
20525
20526         local PREFIX=$(str_repeat 'A' 128)
20527         echo "Create 1K hard links start at $(date)"
20528         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20529                 error "(3) Fail to hard link"
20530
20531         echo "Links count should be right although linkEA overflow"
20532         stat $DIR/$tdir/guard || error "(4) Fail to stat"
20533         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
20534         [ $linkcount -eq 1001 ] ||
20535                 error "(5) Unexpected hard links count: $linkcount"
20536
20537         echo "List all links start at $(date)"
20538         ls -l $DIR/$tdir/foo > /dev/null ||
20539                 error "(6) Fail to list $DIR/$tdir/foo"
20540
20541         echo "Unlink hard links start at $(date)"
20542         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20543                 error "(7) Fail to unlink"
20544 }
20545 run_test 409 "Large amount of cross-MDTs hard links on the same file"
20546
20547 test_410()
20548 {
20549         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
20550                 skip "Need client version at least 2.9.59"
20551
20552         # Create a file, and stat it from the kernel
20553         local testfile=$DIR/$tfile
20554         touch $testfile
20555
20556         local run_id=$RANDOM
20557         local my_ino=$(stat --format "%i" $testfile)
20558
20559         # Try to insert the module. This will always fail as the
20560         # module is designed to not be inserted.
20561         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
20562             &> /dev/null
20563
20564         # Anything but success is a test failure
20565         dmesg | grep -q \
20566             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
20567             error "no inode match"
20568 }
20569 run_test 410 "Test inode number returned from kernel thread"
20570
20571 cleanup_test411_cgroup() {
20572         trap 0
20573         rmdir "$1"
20574 }
20575
20576 test_411() {
20577         local cg_basedir=/sys/fs/cgroup/memory
20578         # LU-9966
20579         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
20580                 skip "no setup for cgroup"
20581
20582         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
20583                 error "test file creation failed"
20584         cancel_lru_locks osc
20585
20586         # Create a very small memory cgroup to force a slab allocation error
20587         local cgdir=$cg_basedir/osc_slab_alloc
20588         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
20589         trap "cleanup_test411_cgroup $cgdir" EXIT
20590         echo 2M > $cgdir/memory.kmem.limit_in_bytes
20591         echo 1M > $cgdir/memory.limit_in_bytes
20592
20593         # Should not LBUG, just be killed by oom-killer
20594         # dd will return 0 even allocation failure in some environment.
20595         # So don't check return value
20596         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
20597         cleanup_test411_cgroup $cgdir
20598
20599         return 0
20600 }
20601 run_test 411 "Slab allocation error with cgroup does not LBUG"
20602
20603 test_412() {
20604         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20605         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20606                 skip "Need server version at least 2.10.55"
20607         fi
20608
20609         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
20610                 error "mkdir failed"
20611         $LFS getdirstripe $DIR/$tdir
20612         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20613         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
20614                 error "expect $((MDSCOUT - 1)) get $stripe_index"
20615         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
20616         [ $stripe_count -eq 2 ] ||
20617                 error "expect 2 get $stripe_count"
20618 }
20619 run_test 412 "mkdir on specific MDTs"
20620
20621 test_413() {
20622         [ $MDSCOUNT -lt 2 ] &&
20623                 skip "We need at least 2 MDTs for this test"
20624
20625         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20626                 skip "Need server version at least 2.10.55"
20627         fi
20628
20629         mkdir $DIR/$tdir || error "mkdir failed"
20630
20631         # find MDT that is the most full
20632         local max=$($LFS df | grep MDT |
20633                 awk 'BEGIN { a=0 }
20634                         { sub("%", "", $5)
20635                           if (0+$5 >= a)
20636                           {
20637                                 a = $5
20638                                 b = $6
20639                           }
20640                         }
20641                      END { split(b, c, ":")
20642                            sub("]", "", c[2])
20643                            print c[2]
20644                          }')
20645
20646         for i in $(seq $((MDSCOUNT - 1))); do
20647                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
20648                         error "mkdir d$i failed"
20649                 $LFS getdirstripe $DIR/$tdir/d$i
20650                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
20651                 [ $stripe_index -ne $max ] ||
20652                         error "don't expect $max"
20653         done
20654 }
20655 run_test 413 "mkdir on less full MDTs"
20656
20657 test_414() {
20658 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
20659         $LCTL set_param fail_loc=0x80000521
20660         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
20661         rm -f $DIR/$tfile
20662 }
20663 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
20664
20665 test_415() {
20666         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20667         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
20668                 skip "Need server version at least 2.11.52"
20669
20670         # LU-11102
20671         local total
20672         local setattr_pid
20673         local start_time
20674         local end_time
20675         local duration
20676
20677         total=500
20678         # this test may be slow on ZFS
20679         [ "$mds1_FSTYPE" == "zfs" ] && total=100
20680
20681         # though this test is designed for striped directory, let's test normal
20682         # directory too since lock is always saved as CoS lock.
20683         test_mkdir $DIR/$tdir || error "mkdir $tdir"
20684         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
20685
20686         (
20687                 while true; do
20688                         touch $DIR/$tdir
20689                 done
20690         ) &
20691         setattr_pid=$!
20692
20693         start_time=$(date +%s)
20694         for i in $(seq $total); do
20695                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
20696                         > /dev/null
20697         done
20698         end_time=$(date +%s)
20699         duration=$((end_time - start_time))
20700
20701         kill -9 $setattr_pid
20702
20703         echo "rename $total files took $duration sec"
20704         [ $duration -lt 100 ] || error "rename took $duration sec"
20705 }
20706 run_test 415 "lock revoke is not missing"
20707
20708 test_416() {
20709         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
20710                 skip "Need server version at least 2.11.55"
20711
20712         # define OBD_FAIL_OSD_TXN_START    0x19a
20713         do_facet mds1 lctl set_param fail_loc=0x19a
20714
20715         lfs mkdir -c $MDSCOUNT $DIR/$tdir
20716
20717         true
20718 }
20719 run_test 416 "transaction start failure won't cause system hung"
20720
20721 cleanup_417() {
20722         trap 0
20723         do_nodes $(comma_list $(mdts_nodes)) \
20724                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
20725         do_nodes $(comma_list $(mdts_nodes)) \
20726                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
20727         do_nodes $(comma_list $(mdts_nodes)) \
20728                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
20729 }
20730
20731 test_417() {
20732         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20733         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
20734                 skip "Need MDS version at least 2.11.56"
20735
20736         trap cleanup_417 RETURN EXIT
20737
20738         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
20739         do_nodes $(comma_list $(mdts_nodes)) \
20740                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
20741         $LFS migrate -m 0 $DIR/$tdir.1 &&
20742                 error "migrate dir $tdir.1 should fail"
20743
20744         do_nodes $(comma_list $(mdts_nodes)) \
20745                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
20746         $LFS mkdir -i 1 $DIR/$tdir.2 &&
20747                 error "create remote dir $tdir.2 should fail"
20748
20749         do_nodes $(comma_list $(mdts_nodes)) \
20750                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
20751         $LFS mkdir -c 2 $DIR/$tdir.3 &&
20752                 error "create striped dir $tdir.3 should fail"
20753         true
20754 }
20755 run_test 417 "disable remote dir, striped dir and dir migration"
20756
20757 # Checks that the outputs of df [-i] and lfs df [-i] match
20758 #
20759 # usage: check_lfs_df <blocks | inodes> <mountpoint>
20760 check_lfs_df() {
20761         local dir=$2
20762         local inodes
20763         local df_out
20764         local lfs_df_out
20765         local count
20766         local passed=false
20767
20768         # blocks or inodes
20769         [ "$1" == "blocks" ] && inodes= || inodes="-i"
20770
20771         for count in {1..100}; do
20772                 cancel_lru_locks
20773                 sync; sleep 2
20774
20775                 # read the lines of interest
20776                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
20777                         error "df $inodes $dir | tail -n +2 failed"
20778                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
20779                         error "lfs df $inodes $dir | grep summary: failed"
20780
20781                 # skip first substrings of each output as they are different
20782                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
20783                 # compare the remaining fields of the two outputs
20784                 passed=true
20785                 for i in {1..5}; do
20786                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
20787                 done
20788                 $passed && break
20789         done
20790
20791         if ! $passed; then
20792                 df -P $inodes $dir
20793                 echo
20794                 lfs df $inodes $dir
20795                 error "df and lfs df $1 output mismatch: "      \
20796                       "df ${inodes}: ${df_out[*]}, "            \
20797                       "lfs df ${inodes}: ${lfs_df_out[*]}"
20798         fi
20799 }
20800
20801 test_418() {
20802         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20803
20804         local dir=$DIR/$tdir
20805         local numfiles=$((RANDOM % 4096 + 2))
20806         local numblocks=$((RANDOM % 256 + 1))
20807
20808         wait_delete_completed
20809         test_mkdir $dir
20810
20811         # check block output
20812         check_lfs_df blocks $dir
20813         # check inode output
20814         check_lfs_df inodes $dir
20815
20816         # create a single file and retest
20817         echo "Creating a single file and testing"
20818         createmany -o $dir/$tfile- 1 &>/dev/null ||
20819                 error "creating 1 file in $dir failed"
20820         check_lfs_df blocks $dir
20821         check_lfs_df inodes $dir
20822
20823         # create a random number of files
20824         echo "Creating $((numfiles - 1)) files and testing"
20825         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
20826                 error "creating $((numfiles - 1)) files in $dir failed"
20827
20828         # write a random number of blocks to the first test file
20829         echo "Writing $numblocks 4K blocks and testing"
20830         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
20831                 count=$numblocks &>/dev/null ||
20832                 error "dd to $dir/${tfile}-0 failed"
20833
20834         # retest
20835         check_lfs_df blocks $dir
20836         check_lfs_df inodes $dir
20837
20838         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
20839                 error "unlinking $numfiles files in $dir failed"
20840 }
20841 run_test 418 "df and lfs df outputs match"
20842
20843 test_419()
20844 {
20845         local dir=$DIR/$tdir
20846
20847         mkdir -p $dir
20848         touch $dir/file
20849
20850         cancel_lru_locks mdc
20851
20852         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
20853         $LCTL set_param fail_loc=0x1410
20854         cat $dir/file
20855         $LCTL set_param fail_loc=0
20856         rm -rf $dir
20857 }
20858 run_test 419 "Verify open file by name doesn't crash kernel"
20859
20860 test_420()
20861 {
20862         [[ $MDS1_VERSION -ge $(version_code 2.12.1) ]] ||
20863                 skip "Need MDS version at least 2.12.1"
20864
20865         local SAVE_UMASK=$(umask)
20866         local dir=$DIR/$tdir
20867         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
20868
20869         mkdir -p $dir
20870         umask 0000
20871         mkdir -m03777 $dir/testdir
20872         ls -dn $dir/testdir
20873         # Need to remove trailing '.' when SELinux is enabled
20874         local dirperms=$(ls -dn $dir/testdir |
20875                          awk '{ sub(/\.$/, "", $1); print $1}')
20876         [ $dirperms == "drwxrwsrwt" ] ||
20877                 error "incorrect perms on $dir/testdir"
20878
20879         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
20880                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
20881         ls -n $dir/testdir/testfile
20882         local fileperms=$(ls -n $dir/testdir/testfile |
20883                           awk '{ sub(/\.$/, "", $1); print $1}')
20884         [ $fileperms == "-rwxr-xr-x" ] ||
20885                 error "incorrect perms on $dir/testdir/testfile"
20886
20887         umask $SAVE_UMASK
20888 }
20889 run_test 420 "clear SGID bit on non-directories for non-members"
20890
20891 test_421a() {
20892         local cnt
20893         local fid1
20894         local fid2
20895
20896         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20897                 skip "Need MDS version at least 2.12.2"
20898
20899         test_mkdir $DIR/$tdir
20900         createmany -o $DIR/$tdir/f 3
20901         cnt=$(ls -1 $DIR/$tdir | wc -l)
20902         [ $cnt != 3 ] && error "unexpected #files: $cnt"
20903
20904         fid1=$(lfs path2fid $DIR/$tdir/f1)
20905         fid2=$(lfs path2fid $DIR/$tdir/f2)
20906         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
20907
20908         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
20909         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
20910
20911         cnt=$(ls -1 $DIR/$tdir | wc -l)
20912         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
20913
20914         rm -f $DIR/$tdir/f3 || error "can't remove f3"
20915         createmany -o $DIR/$tdir/f 3
20916         cnt=$(ls -1 $DIR/$tdir | wc -l)
20917         [ $cnt != 3 ] && error "unexpected #files: $cnt"
20918
20919         fid1=$(lfs path2fid $DIR/$tdir/f1)
20920         fid2=$(lfs path2fid $DIR/$tdir/f2)
20921         echo "remove using fsname $FSNAME"
20922         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
20923
20924         cnt=$(ls -1 $DIR/$tdir | wc -l)
20925         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
20926 }
20927 run_test 421a "simple rm by fid"
20928
20929 test_421b() {
20930         local cnt
20931         local FID1
20932         local FID2
20933
20934         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20935                 skip "Need MDS version at least 2.12.2"
20936
20937         test_mkdir $DIR/$tdir
20938         createmany -o $DIR/$tdir/f 3
20939         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
20940         MULTIPID=$!
20941
20942         FID1=$(lfs path2fid $DIR/$tdir/f1)
20943         FID2=$(lfs path2fid $DIR/$tdir/f2)
20944         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
20945
20946         kill -USR1 $MULTIPID
20947         wait
20948
20949         cnt=$(ls $DIR/$tdir | wc -l)
20950         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
20951 }
20952 run_test 421b "rm by fid on open file"
20953
20954 test_421c() {
20955         local cnt
20956         local FIDS
20957
20958         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20959                 skip "Need MDS version at least 2.12.2"
20960
20961         test_mkdir $DIR/$tdir
20962         createmany -o $DIR/$tdir/f 3
20963         touch $DIR/$tdir/$tfile
20964         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
20965         cnt=$(ls -1 $DIR/$tdir | wc -l)
20966         [ $cnt != 184 ] && error "unexpected #files: $cnt"
20967
20968         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
20969         $LFS rmfid $DIR $FID1 || error "rmfid failed"
20970
20971         cnt=$(ls $DIR/$tdir | wc -l)
20972         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
20973 }
20974 run_test 421c "rm by fid against hardlinked files"
20975
20976 test_421d() {
20977         local cnt
20978         local FIDS
20979
20980         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20981                 skip "Need MDS version at least 2.12.2"
20982
20983         test_mkdir $DIR/$tdir
20984         createmany -o $DIR/$tdir/f 4097
20985         cnt=$(ls -1 $DIR/$tdir | wc -l)
20986         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
20987
20988         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
20989         $LFS rmfid $DIR $FIDS || error "rmfid failed"
20990
20991         cnt=$(ls $DIR/$tdir | wc -l)
20992         rm -rf $DIR/$tdir
20993         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
20994 }
20995 run_test 421d "rmfid en masse"
20996
20997 test_421e() {
20998         local cnt
20999         local FID
21000
21001         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21002         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21003                 skip "Need MDS version at least 2.12.2"
21004
21005         mkdir -p $DIR/$tdir
21006         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21007         createmany -o $DIR/$tdir/striped_dir/f 512
21008         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21009         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21010
21011         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21012                 sed "s/[/][^:]*://g")
21013         $LFS rmfid $DIR $FIDS || error "rmfid failed"
21014
21015         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21016         rm -rf $DIR/$tdir
21017         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21018 }
21019 run_test 421e "rmfid in DNE"
21020
21021 test_421f() {
21022         local cnt
21023         local FID
21024
21025         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21026                 skip "Need MDS version at least 2.12.2"
21027
21028         test_mkdir $DIR/$tdir
21029         touch $DIR/$tdir/f
21030         cnt=$(ls -1 $DIR/$tdir | wc -l)
21031         [ $cnt != 1 ] && error "unexpected #files: $cnt"
21032
21033         FID=$(lfs path2fid $DIR/$tdir/f)
21034         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
21035         # rmfid should fail
21036         cnt=$(ls -1 $DIR/$tdir | wc -l)
21037         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
21038
21039         chmod a+rw $DIR/$tdir
21040         ls -la $DIR/$tdir
21041         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
21042         # rmfid should fail
21043         cnt=$(ls -1 $DIR/$tdir | wc -l)
21044         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
21045
21046         rm -f $DIR/$tdir/f
21047         $RUNAS touch $DIR/$tdir/f
21048         FID=$(lfs path2fid $DIR/$tdir/f)
21049         echo "rmfid as root"
21050         $LFS rmfid $DIR $FID || error "rmfid as root failed"
21051         cnt=$(ls -1 $DIR/$tdir | wc -l)
21052         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
21053
21054         rm -f $DIR/$tdir/f
21055         $RUNAS touch $DIR/$tdir/f
21056         cnt=$(ls -1 $DIR/$tdir | wc -l)
21057         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
21058         FID=$(lfs path2fid $DIR/$tdir/f)
21059         # rmfid w/o user_fid2path mount option should fail
21060         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
21061         cnt=$(ls -1 $DIR/$tdir | wc -l)
21062         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
21063
21064         umount_client $MOUNT || "failed to umount client"
21065         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
21066                 "failed to mount client'"
21067
21068         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
21069         # rmfid should succeed
21070         cnt=$(ls -1 $DIR/$tdir | wc -l)
21071         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
21072
21073         # rmfid shouldn't allow to remove files due to dir's permission
21074         chmod a+rwx $DIR/$tdir
21075         touch $DIR/$tdir/f
21076         ls -la $DIR/$tdir
21077         FID=$(lfs path2fid $DIR/$tdir/f)
21078         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
21079
21080         umount_client $MOUNT || "failed to umount client"
21081         mount_client $MOUNT "$MOUNT_OPTS" ||
21082                 "failed to mount client'"
21083
21084 }
21085 run_test 421f "rmfid checks permissions"
21086
21087 test_421g() {
21088         local cnt
21089         local FIDS
21090
21091         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21092         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
21093                 skip "Need MDS version at least 2.12.2"
21094
21095         mkdir -p $DIR/$tdir
21096         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
21097         createmany -o $DIR/$tdir/striped_dir/f 512
21098         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21099         [ $cnt != 512 ] && error "unexpected #files: $cnt"
21100
21101         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
21102                 sed "s/[/][^:]*://g")
21103
21104         rm -f $DIR/$tdir/striped_dir/f1*
21105         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
21106         removed=$((512 - cnt))
21107
21108         # few files have been just removed, so we expect
21109         # rmfid to fail on their fids
21110         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
21111         [ $removed != $errors ] && error "$errors != $removed"
21112
21113         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
21114         rm -rf $DIR/$tdir
21115         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
21116 }
21117 run_test 421g "rmfid to return errors properly"
21118
21119 stat_test() {
21120     df -h $MOUNT &
21121     df -h $MOUNT &
21122     df -h $MOUNT &
21123     df -h $MOUNT &
21124     df -h $MOUNT &
21125     df -h $MOUNT &
21126 }
21127
21128 test_423() {
21129     local _stats
21130     # ensure statfs cache is expired
21131     sleep 2;
21132
21133     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
21134     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
21135
21136     return 0
21137 }
21138 run_test 423 "statfs should return a right data"
21139
21140 prep_801() {
21141         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21142         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21143                 skip "Need server version at least 2.9.55"
21144
21145         start_full_debug_logging
21146 }
21147
21148 post_801() {
21149         stop_full_debug_logging
21150 }
21151
21152 barrier_stat() {
21153         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21154                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21155                            awk '/The barrier for/ { print $7 }')
21156                 echo $st
21157         else
21158                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
21159                 echo \'$st\'
21160         fi
21161 }
21162
21163 barrier_expired() {
21164         local expired
21165
21166         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
21167                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
21168                           awk '/will be expired/ { print $7 }')
21169         else
21170                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
21171         fi
21172
21173         echo $expired
21174 }
21175
21176 test_801a() {
21177         prep_801
21178
21179         echo "Start barrier_freeze at: $(date)"
21180         #define OBD_FAIL_BARRIER_DELAY          0x2202
21181         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21182         # Do not reduce barrier time - See LU-11873
21183         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
21184
21185         sleep 2
21186         local b_status=$(barrier_stat)
21187         echo "Got barrier status at: $(date)"
21188         [ "$b_status" = "'freezing_p1'" ] ||
21189                 error "(1) unexpected barrier status $b_status"
21190
21191         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21192         wait
21193         b_status=$(barrier_stat)
21194         [ "$b_status" = "'frozen'" ] ||
21195                 error "(2) unexpected barrier status $b_status"
21196
21197         local expired=$(barrier_expired)
21198         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
21199         sleep $((expired + 3))
21200
21201         b_status=$(barrier_stat)
21202         [ "$b_status" = "'expired'" ] ||
21203                 error "(3) unexpected barrier status $b_status"
21204
21205         # Do not reduce barrier time - See LU-11873
21206         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
21207                 error "(4) fail to freeze barrier"
21208
21209         b_status=$(barrier_stat)
21210         [ "$b_status" = "'frozen'" ] ||
21211                 error "(5) unexpected barrier status $b_status"
21212
21213         echo "Start barrier_thaw at: $(date)"
21214         #define OBD_FAIL_BARRIER_DELAY          0x2202
21215         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
21216         do_facet mgs $LCTL barrier_thaw $FSNAME &
21217
21218         sleep 2
21219         b_status=$(barrier_stat)
21220         echo "Got barrier status at: $(date)"
21221         [ "$b_status" = "'thawing'" ] ||
21222                 error "(6) unexpected barrier status $b_status"
21223
21224         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
21225         wait
21226         b_status=$(barrier_stat)
21227         [ "$b_status" = "'thawed'" ] ||
21228                 error "(7) unexpected barrier status $b_status"
21229
21230         #define OBD_FAIL_BARRIER_FAILURE        0x2203
21231         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
21232         do_facet mgs $LCTL barrier_freeze $FSNAME
21233
21234         b_status=$(barrier_stat)
21235         [ "$b_status" = "'failed'" ] ||
21236                 error "(8) unexpected barrier status $b_status"
21237
21238         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21239         do_facet mgs $LCTL barrier_thaw $FSNAME
21240
21241         post_801
21242 }
21243 run_test 801a "write barrier user interfaces and stat machine"
21244
21245 test_801b() {
21246         prep_801
21247
21248         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21249         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
21250         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
21251         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
21252         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
21253
21254         cancel_lru_locks mdc
21255
21256         # 180 seconds should be long enough
21257         do_facet mgs $LCTL barrier_freeze $FSNAME 180
21258
21259         local b_status=$(barrier_stat)
21260         [ "$b_status" = "'frozen'" ] ||
21261                 error "(6) unexpected barrier status $b_status"
21262
21263         mkdir $DIR/$tdir/d0/d10 &
21264         mkdir_pid=$!
21265
21266         touch $DIR/$tdir/d1/f13 &
21267         touch_pid=$!
21268
21269         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
21270         ln_pid=$!
21271
21272         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
21273         mv_pid=$!
21274
21275         rm -f $DIR/$tdir/d4/f12 &
21276         rm_pid=$!
21277
21278         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
21279
21280         # To guarantee taht the 'stat' is not blocked
21281         b_status=$(barrier_stat)
21282         [ "$b_status" = "'frozen'" ] ||
21283                 error "(8) unexpected barrier status $b_status"
21284
21285         # let above commands to run at background
21286         sleep 5
21287
21288         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
21289         ps -p $touch_pid || error "(10) touch should be blocked"
21290         ps -p $ln_pid || error "(11) link should be blocked"
21291         ps -p $mv_pid || error "(12) rename should be blocked"
21292         ps -p $rm_pid || error "(13) unlink should be blocked"
21293
21294         b_status=$(barrier_stat)
21295         [ "$b_status" = "'frozen'" ] ||
21296                 error "(14) unexpected barrier status $b_status"
21297
21298         do_facet mgs $LCTL barrier_thaw $FSNAME
21299         b_status=$(barrier_stat)
21300         [ "$b_status" = "'thawed'" ] ||
21301                 error "(15) unexpected barrier status $b_status"
21302
21303         wait $mkdir_pid || error "(16) mkdir should succeed"
21304         wait $touch_pid || error "(17) touch should succeed"
21305         wait $ln_pid || error "(18) link should succeed"
21306         wait $mv_pid || error "(19) rename should succeed"
21307         wait $rm_pid || error "(20) unlink should succeed"
21308
21309         post_801
21310 }
21311 run_test 801b "modification will be blocked by write barrier"
21312
21313 test_801c() {
21314         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21315
21316         prep_801
21317
21318         stop mds2 || error "(1) Fail to stop mds2"
21319
21320         do_facet mgs $LCTL barrier_freeze $FSNAME 30
21321
21322         local b_status=$(barrier_stat)
21323         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
21324                 do_facet mgs $LCTL barrier_thaw $FSNAME
21325                 error "(2) unexpected barrier status $b_status"
21326         }
21327
21328         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21329                 error "(3) Fail to rescan barrier bitmap"
21330
21331         # Do not reduce barrier time - See LU-11873
21332         do_facet mgs $LCTL barrier_freeze $FSNAME 20
21333
21334         b_status=$(barrier_stat)
21335         [ "$b_status" = "'frozen'" ] ||
21336                 error "(4) unexpected barrier status $b_status"
21337
21338         do_facet mgs $LCTL barrier_thaw $FSNAME
21339         b_status=$(barrier_stat)
21340         [ "$b_status" = "'thawed'" ] ||
21341                 error "(5) unexpected barrier status $b_status"
21342
21343         local devname=$(mdsdevname 2)
21344
21345         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
21346
21347         do_facet mgs $LCTL barrier_rescan $FSNAME ||
21348                 error "(7) Fail to rescan barrier bitmap"
21349
21350         post_801
21351 }
21352 run_test 801c "rescan barrier bitmap"
21353
21354 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
21355 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
21356 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
21357 saved_MOUNT_OPTS=$MOUNT_OPTS
21358
21359 cleanup_802() {
21360         trap 0
21361
21362         stopall
21363         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
21364         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
21365         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
21366         MOUNT_OPTS=$saved_MOUNT_OPTS
21367         setupall
21368 }
21369
21370 test_802() {
21371         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
21372         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
21373         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
21374                 skip "Need server version at least 2.9.55"
21375
21376         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
21377
21378         mkdir $DIR/$tdir || error "(1) fail to mkdir"
21379
21380         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
21381                 error "(2) Fail to copy"
21382
21383         trap cleanup_802 EXIT
21384
21385         # sync by force before remount as readonly
21386         sync; sync_all_data; sleep 3; sync_all_data
21387
21388         stopall
21389
21390         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
21391         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
21392         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
21393
21394         echo "Mount the server as read only"
21395         setupall server_only || error "(3) Fail to start servers"
21396
21397         echo "Mount client without ro should fail"
21398         mount_client $MOUNT &&
21399                 error "(4) Mount client without 'ro' should fail"
21400
21401         echo "Mount client with ro should succeed"
21402         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
21403         mount_client $MOUNT ||
21404                 error "(5) Mount client with 'ro' should succeed"
21405
21406         echo "Modify should be refused"
21407         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
21408
21409         echo "Read should be allowed"
21410         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
21411                 error "(7) Read should succeed under ro mode"
21412
21413         cleanup_802
21414 }
21415 run_test 802 "simulate readonly device"
21416
21417 test_803a() {
21418         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21419         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21420                 skip "MDS needs to be newer than 2.10.54"
21421
21422         mkdir -p $DIR/$tdir
21423         # Create some objects on all MDTs to trigger related logs objects
21424         for idx in $(seq $MDSCOUNT); do
21425                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
21426                         $DIR/$tdir/dir${idx} ||
21427                         error "Fail to create $DIR/$tdir/dir${idx}"
21428         done
21429
21430         sync; sleep 3
21431         wait_delete_completed # ensure old test cleanups are finished
21432         echo "before create:"
21433         $LFS df -i $MOUNT
21434         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21435
21436         for i in {1..10}; do
21437                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
21438                         error "Fail to create $DIR/$tdir/foo$i"
21439         done
21440
21441         sync; sleep 3
21442         echo "after create:"
21443         $LFS df -i $MOUNT
21444         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21445
21446         # allow for an llog to be cleaned up during the test
21447         [ $after_used -ge $((before_used + 10 - 1)) ] ||
21448                 error "before ($before_used) + 10 > after ($after_used)"
21449
21450         for i in {1..10}; do
21451                 rm -rf $DIR/$tdir/foo$i ||
21452                         error "Fail to remove $DIR/$tdir/foo$i"
21453         done
21454
21455         sleep 3 # avoid MDT return cached statfs
21456         wait_delete_completed
21457         echo "after unlink:"
21458         $LFS df -i $MOUNT
21459         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
21460
21461         # allow for an llog to be created during the test
21462         [ $after_used -le $((before_used + 1)) ] ||
21463                 error "after ($after_used) > before ($before_used) + 1"
21464 }
21465 run_test 803a "verify agent object for remote object"
21466
21467 test_803b() {
21468         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21469         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
21470                 skip "MDS needs to be newer than 2.13.56"
21471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21472
21473         for i in $(seq 0 $((MDSCOUNT - 1))); do
21474                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
21475         done
21476
21477         local before=0
21478         local after=0
21479
21480         local tmp
21481
21482         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
21483         for i in $(seq 0 $((MDSCOUNT - 1))); do
21484                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
21485                         awk '/getattr/ { print $2 }')
21486                 before=$((before + tmp))
21487         done
21488         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
21489         for i in $(seq 0 $((MDSCOUNT - 1))); do
21490                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
21491                         awk '/getattr/ { print $2 }')
21492                 after=$((after + tmp))
21493         done
21494
21495         [ $before -eq $after ] || error "getattr count $before != $after"
21496 }
21497 run_test 803b "remote object can getattr from cache"
21498
21499 test_804() {
21500         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
21501         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
21502                 skip "MDS needs to be newer than 2.10.54"
21503         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
21504
21505         mkdir -p $DIR/$tdir
21506         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
21507                 error "Fail to create $DIR/$tdir/dir0"
21508
21509         local fid=$($LFS path2fid $DIR/$tdir/dir0)
21510         local dev=$(mdsdevname 2)
21511
21512         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21513                 grep ${fid} || error "NOT found agent entry for dir0"
21514
21515         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
21516                 error "Fail to create $DIR/$tdir/dir1"
21517
21518         touch $DIR/$tdir/dir1/foo0 ||
21519                 error "Fail to create $DIR/$tdir/dir1/foo0"
21520         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
21521         local rc=0
21522
21523         for idx in $(seq $MDSCOUNT); do
21524                 dev=$(mdsdevname $idx)
21525                 do_facet mds${idx} \
21526                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21527                         grep ${fid} && rc=$idx
21528         done
21529
21530         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
21531                 error "Fail to rename foo0 to foo1"
21532         if [ $rc -eq 0 ]; then
21533                 for idx in $(seq $MDSCOUNT); do
21534                         dev=$(mdsdevname $idx)
21535                         do_facet mds${idx} \
21536                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21537                         grep ${fid} && rc=$idx
21538                 done
21539         fi
21540
21541         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
21542                 error "Fail to rename foo1 to foo2"
21543         if [ $rc -eq 0 ]; then
21544                 for idx in $(seq $MDSCOUNT); do
21545                         dev=$(mdsdevname $idx)
21546                         do_facet mds${idx} \
21547                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
21548                         grep ${fid} && rc=$idx
21549                 done
21550         fi
21551
21552         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
21553
21554         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
21555                 error "Fail to link to $DIR/$tdir/dir1/foo2"
21556         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
21557                 error "Fail to rename foo2 to foo0"
21558         unlink $DIR/$tdir/dir1/foo0 ||
21559                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
21560         rm -rf $DIR/$tdir/dir0 ||
21561                 error "Fail to rm $DIR/$tdir/dir0"
21562
21563         for idx in $(seq $MDSCOUNT); do
21564                 dev=$(mdsdevname $idx)
21565                 rc=0
21566
21567                 stop mds${idx}
21568                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
21569                         rc=$?
21570                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
21571                         error "mount mds$idx failed"
21572                 df $MOUNT > /dev/null 2>&1
21573
21574                 # e2fsck should not return error
21575                 [ $rc -eq 0 ] ||
21576                         error "e2fsck detected error on MDT${idx}: rc=$rc"
21577         done
21578 }
21579 run_test 804 "verify agent entry for remote entry"
21580
21581 cleanup_805() {
21582         do_facet $SINGLEMDS zfs set quota=$old $fsset
21583         unlinkmany $DIR/$tdir/f- 1000000
21584         trap 0
21585 }
21586
21587 test_805() {
21588         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
21589         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
21590         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
21591                 skip "netfree not implemented before 0.7"
21592         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
21593                 skip "Need MDS version at least 2.10.57"
21594
21595         local fsset
21596         local freekb
21597         local usedkb
21598         local old
21599         local quota
21600         local pref="osd-zfs.lustre-MDT0000."
21601
21602         # limit available space on MDS dataset to meet nospace issue
21603         # quickly. then ZFS 0.7.2 can use reserved space if asked
21604         # properly (using netfree flag in osd_declare_destroy()
21605         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
21606         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
21607                 gawk '{print $3}')
21608         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
21609         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
21610         let "usedkb=usedkb-freekb"
21611         let "freekb=freekb/2"
21612         if let "freekb > 5000"; then
21613                 let "freekb=5000"
21614         fi
21615         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
21616         trap cleanup_805 EXIT
21617         mkdir $DIR/$tdir
21618         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
21619         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
21620         rm -rf $DIR/$tdir || error "not able to remove"
21621         do_facet $SINGLEMDS zfs set quota=$old $fsset
21622         trap 0
21623 }
21624 run_test 805 "ZFS can remove from full fs"
21625
21626 # Size-on-MDS test
21627 check_lsom_data()
21628 {
21629         local file=$1
21630         local size=$($LFS getsom -s $file)
21631         local expect=$(stat -c %s $file)
21632
21633         [[ $size == $expect ]] ||
21634                 error "$file expected size: $expect, got: $size"
21635
21636         local blocks=$($LFS getsom -b $file)
21637         expect=$(stat -c %b $file)
21638         [[ $blocks == $expect ]] ||
21639                 error "$file expected blocks: $expect, got: $blocks"
21640 }
21641
21642 check_lsom_size()
21643 {
21644         local size=$($LFS getsom -s $1)
21645         local expect=$2
21646
21647         [[ $size == $expect ]] ||
21648                 error "$file expected size: $expect, got: $size"
21649 }
21650
21651 test_806() {
21652         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21653                 skip "Need MDS version at least 2.11.52"
21654
21655         local bs=1048576
21656
21657         touch $DIR/$tfile || error "touch $tfile failed"
21658
21659         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21660         save_lustre_params client "llite.*.xattr_cache" > $save
21661         lctl set_param llite.*.xattr_cache=0
21662         stack_trap "restore_lustre_params < $save" EXIT
21663
21664         # single-threaded write
21665         echo "Test SOM for single-threaded write"
21666         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
21667                 error "write $tfile failed"
21668         check_lsom_size $DIR/$tfile $bs
21669
21670         local num=32
21671         local size=$(($num * $bs))
21672         local offset=0
21673         local i
21674
21675         echo "Test SOM for single client multi-threaded($num) write"
21676         $TRUNCATE $DIR/$tfile 0
21677         for ((i = 0; i < $num; i++)); do
21678                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21679                 local pids[$i]=$!
21680                 offset=$((offset + $bs))
21681         done
21682         for (( i=0; i < $num; i++ )); do
21683                 wait ${pids[$i]}
21684         done
21685         check_lsom_size $DIR/$tfile $size
21686
21687         $TRUNCATE $DIR/$tfile 0
21688         for ((i = 0; i < $num; i++)); do
21689                 offset=$((offset - $bs))
21690                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21691                 local pids[$i]=$!
21692         done
21693         for (( i=0; i < $num; i++ )); do
21694                 wait ${pids[$i]}
21695         done
21696         check_lsom_size $DIR/$tfile $size
21697
21698         # multi-client writes
21699         num=$(get_node_count ${CLIENTS//,/ })
21700         size=$(($num * $bs))
21701         offset=0
21702         i=0
21703
21704         echo "Test SOM for multi-client ($num) writes"
21705         $TRUNCATE $DIR/$tfile 0
21706         for client in ${CLIENTS//,/ }; do
21707                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21708                 local pids[$i]=$!
21709                 i=$((i + 1))
21710                 offset=$((offset + $bs))
21711         done
21712         for (( i=0; i < $num; i++ )); do
21713                 wait ${pids[$i]}
21714         done
21715         check_lsom_size $DIR/$tfile $offset
21716
21717         i=0
21718         $TRUNCATE $DIR/$tfile 0
21719         for client in ${CLIENTS//,/ }; do
21720                 offset=$((offset - $bs))
21721                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21722                 local pids[$i]=$!
21723                 i=$((i + 1))
21724         done
21725         for (( i=0; i < $num; i++ )); do
21726                 wait ${pids[$i]}
21727         done
21728         check_lsom_size $DIR/$tfile $size
21729
21730         # verify truncate
21731         echo "Test SOM for truncate"
21732         $TRUNCATE $DIR/$tfile 1048576
21733         check_lsom_size $DIR/$tfile 1048576
21734         $TRUNCATE $DIR/$tfile 1234
21735         check_lsom_size $DIR/$tfile 1234
21736
21737         # verify SOM blocks count
21738         echo "Verify SOM block count"
21739         $TRUNCATE $DIR/$tfile 0
21740         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
21741                 error "failed to write file $tfile"
21742         check_lsom_data $DIR/$tfile
21743 }
21744 run_test 806 "Verify Lazy Size on MDS"
21745
21746 test_807() {
21747         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21748                 skip "Need MDS version at least 2.11.52"
21749
21750         # Registration step
21751         changelog_register || error "changelog_register failed"
21752         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
21753         changelog_users $SINGLEMDS | grep -q $cl_user ||
21754                 error "User $cl_user not found in changelog_users"
21755
21756         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21757         save_lustre_params client "llite.*.xattr_cache" > $save
21758         lctl set_param llite.*.xattr_cache=0
21759         stack_trap "restore_lustre_params < $save" EXIT
21760
21761         rm -rf $DIR/$tdir || error "rm $tdir failed"
21762         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
21763         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
21764         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
21765         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
21766                 error "truncate $tdir/trunc failed"
21767
21768         local bs=1048576
21769         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
21770                 error "write $tfile failed"
21771
21772         # multi-client wirtes
21773         local num=$(get_node_count ${CLIENTS//,/ })
21774         local offset=0
21775         local i=0
21776
21777         echo "Test SOM for multi-client ($num) writes"
21778         touch $DIR/$tfile || error "touch $tfile failed"
21779         $TRUNCATE $DIR/$tfile 0
21780         for client in ${CLIENTS//,/ }; do
21781                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21782                 local pids[$i]=$!
21783                 i=$((i + 1))
21784                 offset=$((offset + $bs))
21785         done
21786         for (( i=0; i < $num; i++ )); do
21787                 wait ${pids[$i]}
21788         done
21789
21790         sleep 5
21791         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
21792         check_lsom_data $DIR/$tdir/trunc
21793         check_lsom_data $DIR/$tdir/single_dd
21794         check_lsom_data $DIR/$tfile
21795
21796         rm -rf $DIR/$tdir
21797         # Deregistration step
21798         changelog_deregister || error "changelog_deregister failed"
21799 }
21800 run_test 807 "verify LSOM syncing tool"
21801
21802 check_som_nologged()
21803 {
21804         local lines=$($LFS changelog $FSNAME-MDT0000 |
21805                 grep 'x=trusted.som' | wc -l)
21806         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
21807 }
21808
21809 test_808() {
21810         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
21811                 skip "Need MDS version at least 2.11.55"
21812
21813         # Registration step
21814         changelog_register || error "changelog_register failed"
21815
21816         touch $DIR/$tfile || error "touch $tfile failed"
21817         check_som_nologged
21818
21819         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
21820                 error "write $tfile failed"
21821         check_som_nologged
21822
21823         $TRUNCATE $DIR/$tfile 1234
21824         check_som_nologged
21825
21826         $TRUNCATE $DIR/$tfile 1048576
21827         check_som_nologged
21828
21829         # Deregistration step
21830         changelog_deregister || error "changelog_deregister failed"
21831 }
21832 run_test 808 "Check trusted.som xattr not logged in Changelogs"
21833
21834 check_som_nodata()
21835 {
21836         $LFS getsom $1
21837         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
21838 }
21839
21840 test_809() {
21841         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
21842                 skip "Need MDS version at least 2.11.56"
21843
21844         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
21845                 error "failed to create DoM-only file $DIR/$tfile"
21846         touch $DIR/$tfile || error "touch $tfile failed"
21847         check_som_nodata $DIR/$tfile
21848
21849         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
21850                 error "write $tfile failed"
21851         check_som_nodata $DIR/$tfile
21852
21853         $TRUNCATE $DIR/$tfile 1234
21854         check_som_nodata $DIR/$tfile
21855
21856         $TRUNCATE $DIR/$tfile 4097
21857         check_som_nodata $DIR/$file
21858 }
21859 run_test 809 "Verify no SOM xattr store for DoM-only files"
21860
21861 test_810() {
21862         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21863         $GSS && skip_env "could not run with gss"
21864
21865         set_checksums 1
21866         stack_trap "set_checksums $ORIG_CSUM" EXIT
21867         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
21868
21869         local csum
21870         local before
21871         local after
21872         for csum in $CKSUM_TYPES; do
21873                 #define OBD_FAIL_OSC_NO_GRANT   0x411
21874                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
21875                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
21876                         eval set -- $i
21877                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
21878                         before=$(md5sum $DIR/$tfile)
21879                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
21880                         after=$(md5sum $DIR/$tfile)
21881                         [ "$before" == "$after" ] ||
21882                                 error "$csum: $before != $after bs=$1 seek=$2"
21883                 done
21884         done
21885 }
21886 run_test 810 "partial page writes on ZFS (LU-11663)"
21887
21888 test_812() {
21889         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
21890                 skip "OST < 2.12.51 doesn't support this fail_loc"
21891         [ "$SHARED_KEY" = true ] &&
21892                 skip "OSC connections never go IDLE with Shared-Keys enabled"
21893
21894         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21895         # ensure ost1 is connected
21896         stat $DIR/$tfile >/dev/null || error "can't stat"
21897         wait_osc_import_state client ost1 FULL
21898         # no locks, no reqs to let the connection idle
21899         cancel_lru_locks osc
21900
21901         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
21902 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
21903         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
21904         wait_osc_import_state client ost1 CONNECTING
21905         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
21906
21907         stat $DIR/$tfile >/dev/null || error "can't stat file"
21908 }
21909 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
21910
21911 test_814()
21912 {
21913         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
21914         echo -n y >> $DIR/$tfile
21915         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
21916         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
21917 }
21918 run_test 814 "sparse cp works as expected (LU-12361)"
21919
21920 test_815()
21921 {
21922         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
21923         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
21924 }
21925 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
21926
21927 test_816() {
21928         local ost1_imp=$(get_osc_import_name client ost1)
21929         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
21930                          cut -d'.' -f2)
21931
21932         [ "$SHARED_KEY" = true ] &&
21933                 skip "OSC connections never go IDLE with Shared-Keys enabled"
21934
21935         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21936         # ensure ost1 is connected
21937         stat $DIR/$tfile >/dev/null || error "can't stat"
21938         wait_osc_import_state client ost1 FULL
21939         # no locks, no reqs to let the connection idle
21940         cancel_lru_locks osc
21941         lru_resize_disable osc
21942         local before
21943         local now
21944         before=$($LCTL get_param -n \
21945                  ldlm.namespaces.$imp_name.lru_size)
21946
21947         wait_osc_import_state client ost1 IDLE
21948         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
21949         now=$($LCTL get_param -n \
21950               ldlm.namespaces.$imp_name.lru_size)
21951         [ $before == $now ] || error "lru_size changed $before != $now"
21952 }
21953 run_test 816 "do not reset lru_resize on idle reconnect"
21954
21955 cleanup_817() {
21956         umount $tmpdir
21957         exportfs -u localhost:$DIR/nfsexp
21958         rm -rf $DIR/nfsexp
21959 }
21960
21961 test_817() {
21962         systemctl restart nfs-server.service || skip "failed to restart nfsd"
21963
21964         mkdir -p $DIR/nfsexp
21965         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
21966                 error "failed to export nfs"
21967
21968         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
21969         stack_trap cleanup_817 EXIT
21970
21971         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
21972                 error "failed to mount nfs to $tmpdir"
21973
21974         cp /bin/true $tmpdir
21975         $DIR/nfsexp/true || error "failed to execute 'true' command"
21976 }
21977 run_test 817 "nfsd won't cache write lock for exec file"
21978
21979 test_819a() {
21980         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21981         cancel_lru_locks osc
21982         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
21983         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
21984         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
21985         rm -f $TDIR/$tfile
21986 }
21987 run_test 819a "too big niobuf in read"
21988
21989 test_819b() {
21990         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
21991         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
21992         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21993         cancel_lru_locks osc
21994         sleep 1
21995         rm -f $TDIR/$tfile
21996 }
21997 run_test 819b "too big niobuf in write"
21998
21999 test_818() {
22000         mkdir $DIR/$tdir
22001         $LFS setstripe -c1 -i0 $DIR/$tfile
22002         $LFS setstripe -c1 -i1 $DIR/$tfile
22003         stop $SINGLEMDS
22004         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
22005         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
22006         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
22007                 error "start $SINGLEMDS failed"
22008         rm -rf $DIR/$tdir
22009 }
22010 run_test 818 "unlink with failed llog"
22011
22012 #
22013 # tests that do cleanup/setup should be run at the end
22014 #
22015
22016 test_900() {
22017         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22018         local ls
22019
22020         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
22021         $LCTL set_param fail_loc=0x903
22022
22023         cancel_lru_locks MGC
22024
22025         FAIL_ON_ERROR=true cleanup
22026         FAIL_ON_ERROR=true setup
22027 }
22028 run_test 900 "umount should not race with any mgc requeue thread"
22029
22030 # LU-13377
22031 test_902() {
22032         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
22033                 skip "client does not have LU-13377 fix"
22034         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
22035         $LCTL set_param fail_loc=0x1415
22036         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22037         cancel_lru_locks osc
22038         rm -f $DIR/$tfile
22039 }
22040 run_test 902 "test short write doesn't hang lustre"
22041
22042 complete $SECONDS
22043 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
22044 check_and_cleanup_lustre
22045 if [ "$I_MOUNTED" != "yes" ]; then
22046         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
22047 fi
22048 exit_status