Whamcloud - gitweb
3b76782baca8e082194be12392792640b28c06c6
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 # -*- tab-width: 8; indent-tabs-mode: t; -*-
3 #
4 # Run select tests by setting ONLY, or as arguments to the script.
5 # Skip specific tests by setting EXCEPT.
6 #
7 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
8 set -e
9
10 ONLY=${ONLY:-"$*"}
11 # bug number for skipped test: LU-9693 LU-6493 LU-9693 LU-11058
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c     77k"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054 ..
16 ALWAYS_EXCEPT="  407     253     312     $ALWAYS_EXCEPT"
17
18 if $SHARED_KEY; then
19 # bug number for skipped tests: LU-9795 (all below)
20         ALWAYS_EXCEPT="$ALWAYS_EXCEPT   17n     60a     133g    300f"
21 fi
22
23 if [[ $(uname -m) = aarch64 ]]; then
24         # bug number:    LU-11596 (all below)
25         ALWAYS_EXCEPT+=" 42d 42e 63a 63b 64a 64b 64c"
26         # bug number:    LU-11671 LU-11665 LU-11594 LU-11667 LU-11729
27         ALWAYS_EXCEPT+=" 45       101c     103a     317      810"
28 fi
29
30 # Check Grants after these tests
31 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
32 SRCDIR=$(cd $(dirname $0); echo $PWD)
33 export PATH=$PATH:/sbin
34
35 TMP=${TMP:-/tmp}
36 OSC=${OSC:-"osc"}
37
38 CC=${CC:-cc}
39 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
40 CREATETEST=${CREATETEST:-createtest}
41 LFS=${LFS:-lfs}
42 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
43 LCTL=${LCTL:-lctl}
44 OPENFILE=${OPENFILE:-openfile}
45 OPENUNLINK=${OPENUNLINK:-openunlink}
46 export MULTIOP=${MULTIOP:-multiop}
47 READS=${READS:-"reads"}
48 MUNLINK=${MUNLINK:-munlink}
49 SOCKETSERVER=${SOCKETSERVER:-socketserver}
50 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
51 MEMHOG=${MEMHOG:-memhog}
52 DIRECTIO=${DIRECTIO:-directio}
53 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
54 DEF_STRIPE_COUNT=-1
55 CHECK_GRANT=${CHECK_GRANT:-"yes"}
56 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
57 export PARALLEL=${PARALLEL:-"no"}
58
59 export NAME=${NAME:-local}
60
61 SAVE_PWD=$PWD
62
63 CLEANUP=${CLEANUP:-:}
64 SETUP=${SETUP:-:}
65 TRACE=${TRACE:-""}
66 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
67 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
68 . $LUSTRE/tests/test-framework.sh
69 init_test_env $@
70 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
71 get_lustre_env
72 init_logging
73
74 # skip nfs tests on kernels >= 4.14.0 until they are fixed
75 if [ $LINUX_VERSION_CODE -ge $(version_code 4.14.0) ];then
76         # bug number:   LU-12661
77         ALWAYS_EXCEPT+=" 817"
78 fi
79
80 #                                  5          12          (min)"
81 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
82
83 if [ "$mds1_FSTYPE" = "zfs" ]; then
84         # bug number for skipped test:
85         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
86         #                                               13    (min)"
87         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
88 fi
89
90 # Get the SLES distro version
91 #
92 # Returns a version string that should only be used in comparing
93 # strings returned by version_code()
94 sles_version_code()
95 {
96         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
97
98         # All SuSE Linux versions have one decimal. version_code expects two
99         local sles_version=$version.0
100         version_code $sles_version
101 }
102
103 # Check if we are running on Ubuntu or SLES so we can make decisions on
104 # what tests to run
105 if [ -r /etc/SuSE-release ]; then
106         sles_version=$(sles_version_code)
107         [ $sles_version -lt $(version_code 11.4.0) ] &&
108                 # bug number for skipped test: LU-4341
109                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
110         [ $sles_version -lt $(version_code 12.0.0) ] &&
111                 # bug number for skipped test: LU-3703
112                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
113 elif [ -r /etc/os-release ]; then
114         if grep -qi ubuntu /etc/os-release; then
115                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
116                                                 -e 's/^VERSION=//p' \
117                                                 /etc/os-release |
118                                                 awk '{ print $1 }'))
119
120                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
121                         # bug number for skipped test:
122                         #                LU-10334 LU-10366
123                         ALWAYS_EXCEPT+=" 103a     410"
124                 fi
125         fi
126 fi
127
128 FAIL_ON_ERROR=false
129
130 cleanup() {
131         echo -n "cln.."
132         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
133         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
134 }
135 setup() {
136         echo -n "mnt.."
137         load_modules
138         setupall || exit 10
139         echo "done"
140 }
141
142 check_swap_layouts_support()
143 {
144         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
145                 skip "Does not support layout lock."
146 }
147
148 check_and_setup_lustre
149 DIR=${DIR:-$MOUNT}
150 assert_DIR
151
152 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
153
154 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
155 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
156 rm -rf $DIR/[Rdfs][0-9]*
157
158 # $RUNAS_ID may get set incorrectly somewhere else
159 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
160         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
161
162 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
163
164 build_test_filter
165
166 if [ "${ONLY}" = "MOUNT" ] ; then
167         echo "Lustre is up, please go on"
168         exit
169 fi
170
171 echo "preparing for tests involving mounts"
172 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
173 touch $EXT2_DEV
174 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
175 echo # add a newline after mke2fs.
176
177 umask 077
178
179 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
180 lctl set_param debug=-1 2> /dev/null || true
181 test_0a() {
182         touch $DIR/$tfile
183         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
184         rm $DIR/$tfile
185         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
186 }
187 run_test 0a "touch; rm ====================="
188
189 test_0b() {
190         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
191         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
192 }
193 run_test 0b "chmod 0755 $DIR ============================="
194
195 test_0c() {
196         $LCTL get_param mdc.*.import | grep "state: FULL" ||
197                 error "import not FULL"
198         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
199                 error "bad target"
200 }
201 run_test 0c "check import proc"
202
203 test_0d() { # LU-3397
204         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
205                 skip "proc exports not supported before 2.10.57"
206
207         local mgs_exp="mgs.MGS.exports"
208         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
209         local exp_client_nid
210         local exp_client_version
211         local exp_val
212         local imp_val
213         local temp_imp=$DIR/$tfile.import
214         local temp_exp=$DIR/$tfile.export
215
216         # save mgc import file to $temp_imp
217         $LCTL get_param mgc.*.import | tee $temp_imp
218         # Check if client uuid is found in MGS export
219         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
220                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
221                         $client_uuid ] &&
222                         break;
223         done
224         # save mgs export file to $temp_exp
225         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
226
227         # Compare the value of field "connect_flags"
228         imp_val=$(grep "connect_flags" $temp_imp)
229         exp_val=$(grep "connect_flags" $temp_exp)
230         [ "$exp_val" == "$imp_val" ] ||
231                 error "export flags '$exp_val' != import flags '$imp_val'"
232
233         # Compare the value of client version
234         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
235         exp_val=$(version_code $exp_client_version)
236         imp_val=$CLIENT_VERSION
237         [ "$exp_val" == "$imp_val" ] ||
238                 error "export client version '$exp_val' != '$imp_val'"
239 }
240 run_test 0d "check export proc ============================="
241
242 test_1() {
243         test_mkdir $DIR/$tdir
244         test_mkdir $DIR/$tdir/d2
245         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
246         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
247         rmdir $DIR/$tdir/d2
248         rmdir $DIR/$tdir
249         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
250 }
251 run_test 1 "mkdir; remkdir; rmdir"
252
253 test_2() {
254         test_mkdir $DIR/$tdir
255         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
256         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
257         rm -r $DIR/$tdir
258         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
259 }
260 run_test 2 "mkdir; touch; rmdir; check file"
261
262 test_3() {
263         test_mkdir $DIR/$tdir
264         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
265         touch $DIR/$tdir/$tfile
266         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
267         rm -r $DIR/$tdir
268         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
269 }
270 run_test 3 "mkdir; touch; rmdir; check dir"
271
272 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
273 test_4() {
274         test_mkdir -i 1 $DIR/$tdir
275
276         touch $DIR/$tdir/$tfile ||
277                 error "Create file under remote directory failed"
278
279         rmdir $DIR/$tdir &&
280                 error "Expect error removing in-use dir $DIR/$tdir"
281
282         test -d $DIR/$tdir || error "Remote directory disappeared"
283
284         rm -rf $DIR/$tdir || error "remove remote dir error"
285 }
286 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
287
288 test_5() {
289         test_mkdir $DIR/$tdir
290         test_mkdir $DIR/$tdir/d2
291         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
292         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
293         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
294 }
295 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
296
297 test_6a() {
298         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
299         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
300         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
301                 error "$tfile does not have perm 0666 or UID $UID"
302         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
303         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
304                 error "$tfile should be 0666 and owned by UID $UID"
305 }
306 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
307
308 test_6c() {
309         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
310
311         touch $DIR/$tfile
312         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
313         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
314                 error "$tfile should be owned by UID $RUNAS_ID"
315         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
316         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
317                 error "$tfile should be owned by UID $RUNAS_ID"
318 }
319 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
320
321 test_6e() {
322         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
323
324         touch $DIR/$tfile
325         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
326         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
327                 error "$tfile should be owned by GID $UID"
328         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
329         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
330                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
331 }
332 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
333
334 test_6g() {
335         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
336
337         test_mkdir $DIR/$tdir
338         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
339         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
340         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
341         test_mkdir $DIR/$tdir/d/subdir
342         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
343                 error "$tdir/d/subdir should be GID $RUNAS_GID"
344         if [[ $MDSCOUNT -gt 1 ]]; then
345                 # check remote dir sgid inherite
346                 $LFS mkdir -i 0 $DIR/$tdir.local ||
347                         error "mkdir $tdir.local failed"
348                 chmod g+s $DIR/$tdir.local ||
349                         error "chmod $tdir.local failed"
350                 chgrp $RUNAS_GID $DIR/$tdir.local ||
351                         error "chgrp $tdir.local failed"
352                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
353                         error "mkdir $tdir.remote failed"
354                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
355                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
356                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
357                         error "$tdir.remote should be mode 02755"
358         fi
359 }
360 run_test 6g "verify new dir in sgid dir inherits group"
361
362 test_6h() { # bug 7331
363         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
364
365         touch $DIR/$tfile || error "touch failed"
366         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
367         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
368                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
369         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
370                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
371 }
372 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
373
374 test_7a() {
375         test_mkdir $DIR/$tdir
376         $MCREATE $DIR/$tdir/$tfile
377         chmod 0666 $DIR/$tdir/$tfile
378         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
379                 error "$tdir/$tfile should be mode 0666"
380 }
381 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
382
383 test_7b() {
384         if [ ! -d $DIR/$tdir ]; then
385                 test_mkdir $DIR/$tdir
386         fi
387         $MCREATE $DIR/$tdir/$tfile
388         echo -n foo > $DIR/$tdir/$tfile
389         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
390         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
391 }
392 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
393
394 test_8() {
395         test_mkdir $DIR/$tdir
396         touch $DIR/$tdir/$tfile
397         chmod 0666 $DIR/$tdir/$tfile
398         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
399                 error "$tfile mode not 0666"
400 }
401 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
402
403 test_9() {
404         test_mkdir $DIR/$tdir
405         test_mkdir $DIR/$tdir/d2
406         test_mkdir $DIR/$tdir/d2/d3
407         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
408 }
409 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
410
411 test_10() {
412         test_mkdir $DIR/$tdir
413         test_mkdir $DIR/$tdir/d2
414         touch $DIR/$tdir/d2/$tfile
415         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
416                 error "$tdir/d2/$tfile not a file"
417 }
418 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
419
420 test_11() {
421         test_mkdir $DIR/$tdir
422         test_mkdir $DIR/$tdir/d2
423         chmod 0666 $DIR/$tdir/d2
424         chmod 0705 $DIR/$tdir/d2
425         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
426                 error "$tdir/d2 mode not 0705"
427 }
428 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
429
430 test_12() {
431         test_mkdir $DIR/$tdir
432         touch $DIR/$tdir/$tfile
433         chmod 0666 $DIR/$tdir/$tfile
434         chmod 0654 $DIR/$tdir/$tfile
435         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
436                 error "$tdir/d2 mode not 0654"
437 }
438 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
439
440 test_13() {
441         test_mkdir $DIR/$tdir
442         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
443         >  $DIR/$tdir/$tfile
444         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
445                 error "$tdir/$tfile size not 0 after truncate"
446 }
447 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
448
449 test_14() {
450         test_mkdir $DIR/$tdir
451         touch $DIR/$tdir/$tfile
452         rm $DIR/$tdir/$tfile
453         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
454 }
455 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
456
457 test_15() {
458         test_mkdir $DIR/$tdir
459         touch $DIR/$tdir/$tfile
460         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
461         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
462                 error "$tdir/${tfile_2} not a file after rename"
463         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
464 }
465 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
466
467 test_16() {
468         test_mkdir $DIR/$tdir
469         touch $DIR/$tdir/$tfile
470         rm -rf $DIR/$tdir/$tfile
471         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
472 }
473 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
474
475 test_17a() {
476         test_mkdir $DIR/$tdir
477         touch $DIR/$tdir/$tfile
478         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
479         ls -l $DIR/$tdir
480         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
481                 error "$tdir/l-exist not a symlink"
482         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
483                 error "$tdir/l-exist not referencing a file"
484         rm -f $DIR/$tdir/l-exist
485         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
486 }
487 run_test 17a "symlinks: create, remove (real)"
488
489 test_17b() {
490         test_mkdir $DIR/$tdir
491         ln -s no-such-file $DIR/$tdir/l-dangle
492         ls -l $DIR/$tdir
493         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
494                 error "$tdir/l-dangle not referencing no-such-file"
495         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
496                 error "$tdir/l-dangle not referencing non-existent file"
497         rm -f $DIR/$tdir/l-dangle
498         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
499 }
500 run_test 17b "symlinks: create, remove (dangling)"
501
502 test_17c() { # bug 3440 - don't save failed open RPC for replay
503         test_mkdir $DIR/$tdir
504         ln -s foo $DIR/$tdir/$tfile
505         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
506 }
507 run_test 17c "symlinks: open dangling (should return error)"
508
509 test_17d() {
510         test_mkdir $DIR/$tdir
511         ln -s foo $DIR/$tdir/$tfile
512         touch $DIR/$tdir/$tfile || error "creating to new symlink"
513 }
514 run_test 17d "symlinks: create dangling"
515
516 test_17e() {
517         test_mkdir $DIR/$tdir
518         local foo=$DIR/$tdir/$tfile
519         ln -s $foo $foo || error "create symlink failed"
520         ls -l $foo || error "ls -l failed"
521         ls $foo && error "ls not failed" || true
522 }
523 run_test 17e "symlinks: create recursive symlink (should return error)"
524
525 test_17f() {
526         test_mkdir $DIR/$tdir
527         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
528         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
529         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
530         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
531         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
532         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
533         ls -l  $DIR/$tdir
534 }
535 run_test 17f "symlinks: long and very long symlink name"
536
537 # str_repeat(S, N) generate a string that is string S repeated N times
538 str_repeat() {
539         local s=$1
540         local n=$2
541         local ret=''
542         while [ $((n -= 1)) -ge 0 ]; do
543                 ret=$ret$s
544         done
545         echo $ret
546 }
547
548 # Long symlinks and LU-2241
549 test_17g() {
550         test_mkdir $DIR/$tdir
551         local TESTS="59 60 61 4094 4095"
552
553         # Fix for inode size boundary in 2.1.4
554         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
555                 TESTS="4094 4095"
556
557         # Patch not applied to 2.2 or 2.3 branches
558         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
559         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
560                 TESTS="4094 4095"
561
562         # skip long symlink name for rhel6.5.
563         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
564         grep -q '6.5' /etc/redhat-release &>/dev/null &&
565                 TESTS="59 60 61 4062 4063"
566
567         for i in $TESTS; do
568                 local SYMNAME=$(str_repeat 'x' $i)
569                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
570                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
571         done
572 }
573 run_test 17g "symlinks: really long symlink name and inode boundaries"
574
575 test_17h() { #bug 17378
576         [ $PARALLEL == "yes" ] && skip "skip parallel run"
577         remote_mds_nodsh && skip "remote MDS with nodsh"
578
579         local mdt_idx
580
581         test_mkdir $DIR/$tdir
582         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
583         $LFS setstripe -c -1 $DIR/$tdir
584         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
585         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
586         touch $DIR/$tdir/$tfile || true
587 }
588 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
589
590 test_17i() { #bug 20018
591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
592         remote_mds_nodsh && skip "remote MDS with nodsh"
593
594         local foo=$DIR/$tdir/$tfile
595         local mdt_idx
596
597         test_mkdir -c1 $DIR/$tdir
598         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
599         ln -s $foo $foo || error "create symlink failed"
600 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
601         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
602         ls -l $foo && error "error not detected"
603         return 0
604 }
605 run_test 17i "don't panic on short symlink (should return error)"
606
607 test_17k() { #bug 22301
608         [ $PARALLEL == "yes" ] && skip "skip parallel run"
609         [[ -z "$(which rsync 2>/dev/null)" ]] &&
610                 skip "no rsync command"
611         rsync --help | grep -q xattr ||
612                 skip_env "$(rsync --version | head -n1) does not support xattrs"
613         test_mkdir $DIR/$tdir
614         test_mkdir $DIR/$tdir.new
615         touch $DIR/$tdir/$tfile
616         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
617         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
618                 error "rsync failed with xattrs enabled"
619 }
620 run_test 17k "symlinks: rsync with xattrs enabled"
621
622 test_17l() { # LU-279
623         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
624                 skip "no getfattr command"
625
626         test_mkdir $DIR/$tdir
627         touch $DIR/$tdir/$tfile
628         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
629         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
630                 # -h to not follow symlinks. -m '' to list all the xattrs.
631                 # grep to remove first line: '# file: $path'.
632                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
633                 do
634                         lgetxattr_size_check $path $xattr ||
635                                 error "lgetxattr_size_check $path $xattr failed"
636                 done
637         done
638 }
639 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
640
641 # LU-1540
642 test_17m() {
643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
644         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
645         remote_mds_nodsh && skip "remote MDS with nodsh"
646         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
647         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
648                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
649
650         local short_sym="0123456789"
651         local wdir=$DIR/$tdir
652         local i
653
654         test_mkdir $wdir
655         long_sym=$short_sym
656         # create a long symlink file
657         for ((i = 0; i < 4; ++i)); do
658                 long_sym=${long_sym}${long_sym}
659         done
660
661         echo "create 512 short and long symlink files under $wdir"
662         for ((i = 0; i < 256; ++i)); do
663                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
664                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
665         done
666
667         echo "erase them"
668         rm -f $wdir/*
669         sync
670         wait_delete_completed
671
672         echo "recreate the 512 symlink files with a shorter string"
673         for ((i = 0; i < 512; ++i)); do
674                 # rewrite the symlink file with a shorter string
675                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
676                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
677         done
678
679         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
680         local devname=$(mdsdevname $mds_index)
681
682         echo "stop and checking mds${mds_index}:"
683         # e2fsck should not return error
684         stop mds${mds_index}
685         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
686         rc=$?
687
688         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
689                 error "start mds${mds_index} failed"
690         df $MOUNT > /dev/null 2>&1
691         [ $rc -eq 0 ] ||
692                 error "e2fsck detected error for short/long symlink: rc=$rc"
693         rm -f $wdir/*
694 }
695 run_test 17m "run e2fsck against MDT which contains short/long symlink"
696
697 check_fs_consistency_17n() {
698         local mdt_index
699         local rc=0
700
701         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
702         # so it only check MDT1/MDT2 instead of all of MDTs.
703         for mdt_index in 1 2; do
704                 local devname=$(mdsdevname $mdt_index)
705                 # e2fsck should not return error
706                 stop mds${mdt_index}
707                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
708                         rc=$((rc + $?))
709
710                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
711                         error "mount mds$mdt_index failed"
712                 df $MOUNT > /dev/null 2>&1
713         done
714         return $rc
715 }
716
717 test_17n() {
718         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
720         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
721         remote_mds_nodsh && skip "remote MDS with nodsh"
722         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
723         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
724                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
725
726         local i
727
728         test_mkdir $DIR/$tdir
729         for ((i=0; i<10; i++)); do
730                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
731                         error "create remote dir error $i"
732                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
733                         error "create files under remote dir failed $i"
734         done
735
736         check_fs_consistency_17n ||
737                 error "e2fsck report error after create files under remote dir"
738
739         for ((i = 0; i < 10; i++)); do
740                 rm -rf $DIR/$tdir/remote_dir_${i} ||
741                         error "destroy remote dir error $i"
742         done
743
744         check_fs_consistency_17n ||
745                 error "e2fsck report error after unlink files under remote dir"
746
747         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
748                 skip "lustre < 2.4.50 does not support migrate mv"
749
750         for ((i = 0; i < 10; i++)); do
751                 mkdir -p $DIR/$tdir/remote_dir_${i}
752                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
753                         error "create files under remote dir failed $i"
754                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
755                         error "migrate remote dir error $i"
756         done
757         check_fs_consistency_17n || error "e2fsck report error after migration"
758
759         for ((i = 0; i < 10; i++)); do
760                 rm -rf $DIR/$tdir/remote_dir_${i} ||
761                         error "destroy remote dir error $i"
762         done
763
764         check_fs_consistency_17n || error "e2fsck report error after unlink"
765 }
766 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
767
768 test_17o() {
769         remote_mds_nodsh && skip "remote MDS with nodsh"
770         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
771                 skip "Need MDS version at least 2.3.64"
772
773         local wdir=$DIR/${tdir}o
774         local mdt_index
775         local rc=0
776
777         test_mkdir $wdir
778         touch $wdir/$tfile
779         mdt_index=$($LFS getstripe -m $wdir/$tfile)
780         mdt_index=$((mdt_index + 1))
781
782         cancel_lru_locks mdc
783         #fail mds will wait the failover finish then set
784         #following fail_loc to avoid interfer the recovery process.
785         fail mds${mdt_index}
786
787         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
788         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
789         ls -l $wdir/$tfile && rc=1
790         do_facet mds${mdt_index} lctl set_param fail_loc=0
791         [[ $rc -eq 0 ]] || error "stat file should fail"
792 }
793 run_test 17o "stat file with incompat LMA feature"
794
795 test_18() {
796         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
797         ls $DIR || error "Failed to ls $DIR: $?"
798 }
799 run_test 18 "touch .../f ; ls ... =============================="
800
801 test_19a() {
802         touch $DIR/$tfile
803         ls -l $DIR
804         rm $DIR/$tfile
805         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
806 }
807 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
808
809 test_19b() {
810         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
811 }
812 run_test 19b "ls -l .../f19 (should return error) =============="
813
814 test_19c() {
815         [ $RUNAS_ID -eq $UID ] &&
816                 skip_env "RUNAS_ID = UID = $UID -- skipping"
817
818         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
819 }
820 run_test 19c "$RUNAS touch .../f19 (should return error) =="
821
822 test_19d() {
823         cat $DIR/f19 && error || true
824 }
825 run_test 19d "cat .../f19 (should return error) =============="
826
827 test_20() {
828         touch $DIR/$tfile
829         rm $DIR/$tfile
830         touch $DIR/$tfile
831         rm $DIR/$tfile
832         touch $DIR/$tfile
833         rm $DIR/$tfile
834         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
835 }
836 run_test 20 "touch .../f ; ls -l ..."
837
838 test_21() {
839         test_mkdir $DIR/$tdir
840         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
841         ln -s dangle $DIR/$tdir/link
842         echo foo >> $DIR/$tdir/link
843         cat $DIR/$tdir/dangle
844         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
845         $CHECKSTAT -f -t file $DIR/$tdir/link ||
846                 error "$tdir/link not linked to a file"
847 }
848 run_test 21 "write to dangling link"
849
850 test_22() {
851         local wdir=$DIR/$tdir
852         test_mkdir $wdir
853         chown $RUNAS_ID:$RUNAS_GID $wdir
854         (cd $wdir || error "cd $wdir failed";
855                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
856                 $RUNAS tar xf -)
857         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
858         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
859         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
860                 error "checkstat -u failed"
861 }
862 run_test 22 "unpack tar archive as non-root user"
863
864 # was test_23
865 test_23a() {
866         test_mkdir $DIR/$tdir
867         local file=$DIR/$tdir/$tfile
868
869         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
870         openfile -f O_CREAT:O_EXCL $file &&
871                 error "$file recreate succeeded" || true
872 }
873 run_test 23a "O_CREAT|O_EXCL in subdir"
874
875 test_23b() { # bug 18988
876         test_mkdir $DIR/$tdir
877         local file=$DIR/$tdir/$tfile
878
879         rm -f $file
880         echo foo > $file || error "write filed"
881         echo bar >> $file || error "append filed"
882         $CHECKSTAT -s 8 $file || error "wrong size"
883         rm $file
884 }
885 run_test 23b "O_APPEND check"
886
887 # LU-9409, size with O_APPEND and tiny writes
888 test_23c() {
889         local file=$DIR/$tfile
890
891         # single dd
892         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
893         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
894         rm -f $file
895
896         # racing tiny writes
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
898         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
899         wait
900         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
901         rm -f $file
902
903         #racing tiny & normal writes
904         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
905         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
906         wait
907         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
908         rm -f $file
909
910         #racing tiny & normal writes 2, ugly numbers
911         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
912         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
913         wait
914         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
915         rm -f $file
916 }
917 run_test 23c "O_APPEND size checks for tiny writes"
918
919 # LU-11069 file offset is correct after appending writes
920 test_23d() {
921         local file=$DIR/$tfile
922         local offset
923
924         echo CentaurHauls > $file
925         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
926         if ((offset != 26)); then
927                 error "wrong offset, expected 26, got '$offset'"
928         fi
929 }
930 run_test 23d "file offset is correct after appending writes"
931
932 # rename sanity
933 test_24a() {
934         echo '-- same directory rename'
935         test_mkdir $DIR/$tdir
936         touch $DIR/$tdir/$tfile.1
937         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
938         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
939 }
940 run_test 24a "rename file to non-existent target"
941
942 test_24b() {
943         test_mkdir $DIR/$tdir
944         touch $DIR/$tdir/$tfile.{1,2}
945         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
946         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
947         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
948 }
949 run_test 24b "rename file to existing target"
950
951 test_24c() {
952         test_mkdir $DIR/$tdir
953         test_mkdir $DIR/$tdir/d$testnum.1
954         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
955         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
956         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
957 }
958 run_test 24c "rename directory to non-existent target"
959
960 test_24d() {
961         test_mkdir -c1 $DIR/$tdir
962         test_mkdir -c1 $DIR/$tdir/d$testnum.1
963         test_mkdir -c1 $DIR/$tdir/d$testnum.2
964         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
965         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
966         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
967 }
968 run_test 24d "rename directory to existing target"
969
970 test_24e() {
971         echo '-- cross directory renames --'
972         test_mkdir $DIR/R5a
973         test_mkdir $DIR/R5b
974         touch $DIR/R5a/f
975         mv $DIR/R5a/f $DIR/R5b/g
976         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
977         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
978 }
979 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
980
981 test_24f() {
982         test_mkdir $DIR/R6a
983         test_mkdir $DIR/R6b
984         touch $DIR/R6a/f $DIR/R6b/g
985         mv $DIR/R6a/f $DIR/R6b/g
986         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
987         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
988 }
989 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
990
991 test_24g() {
992         test_mkdir $DIR/R7a
993         test_mkdir $DIR/R7b
994         test_mkdir $DIR/R7a/d
995         mv $DIR/R7a/d $DIR/R7b/e
996         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
997         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
998 }
999 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
1000
1001 test_24h() {
1002         test_mkdir -c1 $DIR/R8a
1003         test_mkdir -c1 $DIR/R8b
1004         test_mkdir -c1 $DIR/R8a/d
1005         test_mkdir -c1 $DIR/R8b/e
1006         mrename $DIR/R8a/d $DIR/R8b/e
1007         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1008         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1009 }
1010 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1011
1012 test_24i() {
1013         echo "-- rename error cases"
1014         test_mkdir $DIR/R9
1015         test_mkdir $DIR/R9/a
1016         touch $DIR/R9/f
1017         mrename $DIR/R9/f $DIR/R9/a
1018         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1019         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1020         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1021 }
1022 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1023
1024 test_24j() {
1025         test_mkdir $DIR/R10
1026         mrename $DIR/R10/f $DIR/R10/g
1027         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1028         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1029         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1030 }
1031 run_test 24j "source does not exist ============================"
1032
1033 test_24k() {
1034         test_mkdir $DIR/R11a
1035         test_mkdir $DIR/R11a/d
1036         touch $DIR/R11a/f
1037         mv $DIR/R11a/f $DIR/R11a/d
1038         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1039         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1040 }
1041 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1042
1043 # bug 2429 - rename foo foo foo creates invalid file
1044 test_24l() {
1045         f="$DIR/f24l"
1046         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1047 }
1048 run_test 24l "Renaming a file to itself ========================"
1049
1050 test_24m() {
1051         f="$DIR/f24m"
1052         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1053         # on ext3 this does not remove either the source or target files
1054         # though the "expected" operation would be to remove the source
1055         $CHECKSTAT -t file ${f} || error "${f} missing"
1056         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1057 }
1058 run_test 24m "Renaming a file to a hard link to itself ========="
1059
1060 test_24n() {
1061     f="$DIR/f24n"
1062     # this stats the old file after it was renamed, so it should fail
1063     touch ${f}
1064     $CHECKSTAT ${f} || error "${f} missing"
1065     mv ${f} ${f}.rename
1066     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1067     $CHECKSTAT -a ${f} || error "${f} exists"
1068 }
1069 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1070
1071 test_24o() {
1072         test_mkdir $DIR/$tdir
1073         rename_many -s random -v -n 10 $DIR/$tdir
1074 }
1075 run_test 24o "rename of files during htree split"
1076
1077 test_24p() {
1078         test_mkdir $DIR/R12a
1079         test_mkdir $DIR/R12b
1080         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1081         mrename $DIR/R12a $DIR/R12b
1082         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1083         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1084         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1085         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1086 }
1087 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1088
1089 cleanup_multiop_pause() {
1090         trap 0
1091         kill -USR1 $MULTIPID
1092 }
1093
1094 test_24q() {
1095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1096
1097         test_mkdir $DIR/R13a
1098         test_mkdir $DIR/R13b
1099         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1100         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1101         MULTIPID=$!
1102
1103         trap cleanup_multiop_pause EXIT
1104         mrename $DIR/R13a $DIR/R13b
1105         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1106         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1107         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1108         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1109         cleanup_multiop_pause
1110         wait $MULTIPID || error "multiop close failed"
1111 }
1112 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1113
1114 test_24r() { #bug 3789
1115         test_mkdir $DIR/R14a
1116         test_mkdir $DIR/R14a/b
1117         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1118         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1119         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1120 }
1121 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1122
1123 test_24s() {
1124         test_mkdir $DIR/R15a
1125         test_mkdir $DIR/R15a/b
1126         test_mkdir $DIR/R15a/b/c
1127         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1128         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1129         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1130 }
1131 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1132 test_24t() {
1133         test_mkdir $DIR/R16a
1134         test_mkdir $DIR/R16a/b
1135         test_mkdir $DIR/R16a/b/c
1136         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1137         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1138         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1139 }
1140 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1141
1142 test_24u() { # bug12192
1143         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1144         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1145 }
1146 run_test 24u "create stripe file"
1147
1148 simple_cleanup_common() {
1149         local rc=0
1150         trap 0
1151         [ -z "$DIR" -o -z "$tdir" ] && return 0
1152
1153         local start=$SECONDS
1154         rm -rf $DIR/$tdir
1155         rc=$?
1156         wait_delete_completed
1157         echo "cleanup time $((SECONDS - start))"
1158         return $rc
1159 }
1160
1161 max_pages_per_rpc() {
1162         local mdtname="$(printf "MDT%04x" ${1:-0})"
1163         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1164 }
1165
1166 test_24v() {
1167         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1168
1169         local nrfiles=${COUNT:-100000}
1170         local fname="$DIR/$tdir/$tfile"
1171
1172         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1173         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1174
1175         test_mkdir "$(dirname $fname)"
1176         # assume MDT0000 has the fewest inodes
1177         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1178         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1179         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1180
1181         trap simple_cleanup_common EXIT
1182
1183         createmany -m "$fname" $nrfiles
1184
1185         cancel_lru_locks mdc
1186         lctl set_param mdc.*.stats clear
1187
1188         # was previously test_24D: LU-6101
1189         # readdir() returns correct number of entries after cursor reload
1190         local num_ls=$(ls $DIR/$tdir | wc -l)
1191         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1192         local num_all=$(ls -a $DIR/$tdir | wc -l)
1193         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1194              $num_all -ne $((nrfiles + 2)) ]; then
1195                 error "Expected $nrfiles files, got $num_ls " \
1196                         "($num_uniq unique $num_all .&..)"
1197         fi
1198         # LU-5 large readdir
1199         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1200         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1201         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1202         # take into account of overhead in lu_dirpage header and end mark in
1203         # each page, plus one in rpc_num calculation.
1204         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1205         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1206         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1207         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1208         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1209         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1210         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1211         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1212                 error "large readdir doesn't take effect: " \
1213                       "$mds_readpage should be about $rpc_max"
1214
1215         simple_cleanup_common
1216 }
1217 run_test 24v "list large directory (test hash collision, b=17560)"
1218
1219 test_24w() { # bug21506
1220         SZ1=234852
1221         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1222         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1223         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1224         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1225         [[ "$SZ1" -eq "$SZ2" ]] ||
1226                 error "Error reading at the end of the file $tfile"
1227 }
1228 run_test 24w "Reading a file larger than 4Gb"
1229
1230 test_24x() {
1231         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1232         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1233         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1234                 skip "Need MDS version at least 2.7.56"
1235
1236         local MDTIDX=1
1237         local remote_dir=$DIR/$tdir/remote_dir
1238
1239         test_mkdir $DIR/$tdir
1240         $LFS mkdir -i $MDTIDX $remote_dir ||
1241                 error "create remote directory failed"
1242
1243         test_mkdir $DIR/$tdir/src_dir
1244         touch $DIR/$tdir/src_file
1245         test_mkdir $remote_dir/tgt_dir
1246         touch $remote_dir/tgt_file
1247
1248         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1249                 error "rename dir cross MDT failed!"
1250
1251         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1252                 error "rename file cross MDT failed!"
1253
1254         touch $DIR/$tdir/ln_file
1255         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1256                 error "ln file cross MDT failed"
1257
1258         rm -rf $DIR/$tdir || error "Can not delete directories"
1259 }
1260 run_test 24x "cross MDT rename/link"
1261
1262 test_24y() {
1263         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1264         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1265
1266         local remote_dir=$DIR/$tdir/remote_dir
1267         local mdtidx=1
1268
1269         test_mkdir $DIR/$tdir
1270         $LFS mkdir -i $mdtidx $remote_dir ||
1271                 error "create remote directory failed"
1272
1273         test_mkdir $remote_dir/src_dir
1274         touch $remote_dir/src_file
1275         test_mkdir $remote_dir/tgt_dir
1276         touch $remote_dir/tgt_file
1277
1278         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1279                 error "rename subdir in the same remote dir failed!"
1280
1281         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1282                 error "rename files in the same remote dir failed!"
1283
1284         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1285                 error "link files in the same remote dir failed!"
1286
1287         rm -rf $DIR/$tdir || error "Can not delete directories"
1288 }
1289 run_test 24y "rename/link on the same dir should succeed"
1290
1291 test_24z() {
1292         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1293         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1294                 skip "Need MDS version at least 2.12.51"
1295
1296         local index
1297
1298         for index in 0 1; do
1299                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1300                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1301         done
1302
1303         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1304
1305         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1306         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1307
1308         local mdts=$(comma_list $(mdts_nodes))
1309
1310         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1311         stack_trap "do_nodes $mdts $LCTL \
1312                 set_param mdt.*.enable_remote_rename=1" EXIT
1313
1314         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1315
1316         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1317         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1318 }
1319 run_test 24z "cross-MDT rename is done as cp"
1320
1321 test_24A() { # LU-3182
1322         local NFILES=5000
1323
1324         rm -rf $DIR/$tdir
1325         test_mkdir $DIR/$tdir
1326         trap simple_cleanup_common EXIT
1327         createmany -m $DIR/$tdir/$tfile $NFILES
1328         local t=$(ls $DIR/$tdir | wc -l)
1329         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1330         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1331         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1332                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1333         fi
1334
1335         simple_cleanup_common || error "Can not delete directories"
1336 }
1337 run_test 24A "readdir() returns correct number of entries."
1338
1339 test_24B() { # LU-4805
1340         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1341
1342         local count
1343
1344         test_mkdir $DIR/$tdir
1345         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1346                 error "create striped dir failed"
1347
1348         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1349         [ $count -eq 2 ] || error "Expected 2, got $count"
1350
1351         touch $DIR/$tdir/striped_dir/a
1352
1353         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1354         [ $count -eq 3 ] || error "Expected 3, got $count"
1355
1356         touch $DIR/$tdir/striped_dir/.f
1357
1358         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1359         [ $count -eq 4 ] || error "Expected 4, got $count"
1360
1361         rm -rf $DIR/$tdir || error "Can not delete directories"
1362 }
1363 run_test 24B "readdir for striped dir return correct number of entries"
1364
1365 test_24C() {
1366         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1367
1368         mkdir $DIR/$tdir
1369         mkdir $DIR/$tdir/d0
1370         mkdir $DIR/$tdir/d1
1371
1372         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1373                 error "create striped dir failed"
1374
1375         cd $DIR/$tdir/d0/striped_dir
1376
1377         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1378         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1379         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1380
1381         [ "$d0_ino" = "$parent_ino" ] ||
1382                 error ".. wrong, expect $d0_ino, get $parent_ino"
1383
1384         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1385                 error "mv striped dir failed"
1386
1387         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1388
1389         [ "$d1_ino" = "$parent_ino" ] ||
1390                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1391 }
1392 run_test 24C "check .. in striped dir"
1393
1394 test_24E() {
1395         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1397
1398         mkdir -p $DIR/$tdir
1399         mkdir $DIR/$tdir/src_dir
1400         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1401                 error "create remote source failed"
1402
1403         touch $DIR/$tdir/src_dir/src_child/a
1404
1405         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1406                 error "create remote target dir failed"
1407
1408         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1409                 error "create remote target child failed"
1410
1411         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1412                 error "rename dir cross MDT failed!"
1413
1414         find $DIR/$tdir
1415
1416         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1417                 error "src_child still exists after rename"
1418
1419         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1420                 error "missing file(a) after rename"
1421
1422         rm -rf $DIR/$tdir || error "Can not delete directories"
1423 }
1424 run_test 24E "cross MDT rename/link"
1425
1426 test_24F () {
1427         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1428
1429         local repeats=1000
1430         [ "$SLOW" = "no" ] && repeats=100
1431
1432         mkdir -p $DIR/$tdir
1433
1434         echo "$repeats repeats"
1435         for ((i = 0; i < repeats; i++)); do
1436                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1437                 touch $DIR/$tdir/test/a || error "touch fails"
1438                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1439                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1440         done
1441
1442         true
1443 }
1444 run_test 24F "hash order vs readdir (LU-11330)"
1445
1446 test_25a() {
1447         echo '== symlink sanity ============================================='
1448
1449         test_mkdir $DIR/d25
1450         ln -s d25 $DIR/s25
1451         touch $DIR/s25/foo ||
1452                 error "File creation in symlinked directory failed"
1453 }
1454 run_test 25a "create file in symlinked directory ==============="
1455
1456 test_25b() {
1457         [ ! -d $DIR/d25 ] && test_25a
1458         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1459 }
1460 run_test 25b "lookup file in symlinked directory ==============="
1461
1462 test_26a() {
1463         test_mkdir $DIR/d26
1464         test_mkdir $DIR/d26/d26-2
1465         ln -s d26/d26-2 $DIR/s26
1466         touch $DIR/s26/foo || error "File creation failed"
1467 }
1468 run_test 26a "multiple component symlink ======================="
1469
1470 test_26b() {
1471         test_mkdir -p $DIR/$tdir/d26-2
1472         ln -s $tdir/d26-2/foo $DIR/s26-2
1473         touch $DIR/s26-2 || error "File creation failed"
1474 }
1475 run_test 26b "multiple component symlink at end of lookup ======"
1476
1477 test_26c() {
1478         test_mkdir $DIR/d26.2
1479         touch $DIR/d26.2/foo
1480         ln -s d26.2 $DIR/s26.2-1
1481         ln -s s26.2-1 $DIR/s26.2-2
1482         ln -s s26.2-2 $DIR/s26.2-3
1483         chmod 0666 $DIR/s26.2-3/foo
1484 }
1485 run_test 26c "chain of symlinks"
1486
1487 # recursive symlinks (bug 439)
1488 test_26d() {
1489         ln -s d26-3/foo $DIR/d26-3
1490 }
1491 run_test 26d "create multiple component recursive symlink"
1492
1493 test_26e() {
1494         [ ! -h $DIR/d26-3 ] && test_26d
1495         rm $DIR/d26-3
1496 }
1497 run_test 26e "unlink multiple component recursive symlink"
1498
1499 # recursive symlinks (bug 7022)
1500 test_26f() {
1501         test_mkdir $DIR/$tdir
1502         test_mkdir $DIR/$tdir/$tfile
1503         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1504         test_mkdir -p lndir/bar1
1505         test_mkdir $DIR/$tdir/$tfile/$tfile
1506         cd $tfile                || error "cd $tfile failed"
1507         ln -s .. dotdot          || error "ln dotdot failed"
1508         ln -s dotdot/lndir lndir || error "ln lndir failed"
1509         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1510         output=`ls $tfile/$tfile/lndir/bar1`
1511         [ "$output" = bar1 ] && error "unexpected output"
1512         rm -r $tfile             || error "rm $tfile failed"
1513         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1514 }
1515 run_test 26f "rm -r of a directory which has recursive symlink"
1516
1517 test_27a() {
1518         test_mkdir $DIR/$tdir
1519         $LFS getstripe $DIR/$tdir
1520         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1521         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1522         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1523 }
1524 run_test 27a "one stripe file"
1525
1526 test_27b() {
1527         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1528
1529         test_mkdir $DIR/$tdir
1530         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1531         $LFS getstripe -c $DIR/$tdir/$tfile
1532         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1533                 error "two-stripe file doesn't have two stripes"
1534
1535         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1536 }
1537 run_test 27b "create and write to two stripe file"
1538
1539 test_27d() {
1540         test_mkdir $DIR/$tdir
1541         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1542                 error "setstripe failed"
1543         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1544         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1545 }
1546 run_test 27d "create file with default settings"
1547
1548 test_27e() {
1549         # LU-5839 adds check for existed layout before setting it
1550         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1551                 skip "Need MDS version at least 2.7.56"
1552
1553         test_mkdir $DIR/$tdir
1554         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1555         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1556         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1557 }
1558 run_test 27e "setstripe existing file (should return error)"
1559
1560 test_27f() {
1561         test_mkdir $DIR/$tdir
1562         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1563                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1564         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1565                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1566         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1567         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1568 }
1569 run_test 27f "setstripe with bad stripe size (should return error)"
1570
1571 test_27g() {
1572         test_mkdir $DIR/$tdir
1573         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1574         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1575                 error "$DIR/$tdir/$tfile has object"
1576 }
1577 run_test 27g "$LFS getstripe with no objects"
1578
1579 test_27ga() {
1580         test_mkdir $DIR/$tdir
1581         touch $DIR/$tdir/$tfile || error "touch failed"
1582         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1583         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1584         local rc=$?
1585         (( rc == 2 )) || error "getstripe did not return ENOENT"
1586 }
1587 run_test 27ga "$LFS getstripe with missing file (should return error)"
1588
1589 test_27i() {
1590         test_mkdir $DIR/$tdir
1591         touch $DIR/$tdir/$tfile || error "touch failed"
1592         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1593                 error "missing objects"
1594 }
1595 run_test 27i "$LFS getstripe with some objects"
1596
1597 test_27j() {
1598         test_mkdir $DIR/$tdir
1599         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1600                 error "setstripe failed" || true
1601 }
1602 run_test 27j "setstripe with bad stripe offset (should return error)"
1603
1604 test_27k() { # bug 2844
1605         test_mkdir $DIR/$tdir
1606         local file=$DIR/$tdir/$tfile
1607         local ll_max_blksize=$((4 * 1024 * 1024))
1608         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1609         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1610         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1611         dd if=/dev/zero of=$file bs=4k count=1
1612         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1613         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1614 }
1615 run_test 27k "limit i_blksize for broken user apps"
1616
1617 test_27l() {
1618         mcreate $DIR/$tfile || error "creating file"
1619         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1620                 error "setstripe should have failed" || true
1621 }
1622 run_test 27l "check setstripe permissions (should return error)"
1623
1624 test_27m() {
1625         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1626
1627         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1628                    head -n1)
1629         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1630                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1631         fi
1632         trap simple_cleanup_common EXIT
1633         test_mkdir $DIR/$tdir
1634         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1635         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1636                 error "dd should fill OST0"
1637         i=2
1638         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1639                 i=$((i + 1))
1640                 [ $i -gt 256 ] && break
1641         done
1642         i=$((i + 1))
1643         touch $DIR/$tdir/$tfile.$i
1644         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1645             awk '{print $1}'| grep -w "0") ] &&
1646                 error "OST0 was full but new created file still use it"
1647         i=$((i + 1))
1648         touch $DIR/$tdir/$tfile.$i
1649         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1650             awk '{print $1}'| grep -w "0") ] &&
1651                 error "OST0 was full but new created file still use it"
1652         simple_cleanup_common
1653 }
1654 run_test 27m "create file while OST0 was full"
1655
1656 sleep_maxage() {
1657         local delay=$(do_facet $SINGLEMDS lctl get_param -n lo[vd].*.qos_maxage |
1658                       awk '{ print $1 * 2; exit; }')
1659         sleep $delay
1660 }
1661
1662 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1663 # if the OST isn't full anymore.
1664 reset_enospc() {
1665         local OSTIDX=${1:-""}
1666
1667         local list=$(comma_list $(osts_nodes))
1668         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1669
1670         do_nodes $list lctl set_param fail_loc=0
1671         sync    # initiate all OST_DESTROYs from MDS to OST
1672         sleep_maxage
1673 }
1674
1675 exhaust_precreations() {
1676         local OSTIDX=$1
1677         local FAILLOC=$2
1678         local FAILIDX=${3:-$OSTIDX}
1679         local ofacet=ost$((OSTIDX + 1))
1680
1681         test_mkdir -p -c1 $DIR/$tdir
1682         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1683         local mfacet=mds$((mdtidx + 1))
1684         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1685
1686         local OST=$(ostname_from_index $OSTIDX)
1687
1688         # on the mdt's osc
1689         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1690         local last_id=$(do_facet $mfacet lctl get_param -n \
1691                         osp.$mdtosc_proc1.prealloc_last_id)
1692         local next_id=$(do_facet $mfacet lctl get_param -n \
1693                         osp.$mdtosc_proc1.prealloc_next_id)
1694
1695         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1696         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1697
1698         test_mkdir -p $DIR/$tdir/${OST}
1699         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1700 #define OBD_FAIL_OST_ENOSPC              0x215
1701         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1702         echo "Creating to objid $last_id on ost $OST..."
1703         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1704         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1705         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1706         sleep_maxage
1707 }
1708
1709 exhaust_all_precreations() {
1710         local i
1711         for (( i=0; i < OSTCOUNT; i++ )) ; do
1712                 exhaust_precreations $i $1 -1
1713         done
1714 }
1715
1716 test_27n() {
1717         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1718         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1719         remote_mds_nodsh && skip "remote MDS with nodsh"
1720         remote_ost_nodsh && skip "remote OST with nodsh"
1721
1722         reset_enospc
1723         rm -f $DIR/$tdir/$tfile
1724         exhaust_precreations 0 0x80000215
1725         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1726         touch $DIR/$tdir/$tfile || error "touch failed"
1727         $LFS getstripe $DIR/$tdir/$tfile
1728         reset_enospc
1729 }
1730 run_test 27n "create file with some full OSTs"
1731
1732 test_27o() {
1733         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1735         remote_mds_nodsh && skip "remote MDS with nodsh"
1736         remote_ost_nodsh && skip "remote OST with nodsh"
1737
1738         reset_enospc
1739         rm -f $DIR/$tdir/$tfile
1740         exhaust_all_precreations 0x215
1741
1742         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1743
1744         reset_enospc
1745         rm -rf $DIR/$tdir/*
1746 }
1747 run_test 27o "create file with all full OSTs (should error)"
1748
1749 test_27p() {
1750         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1751         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1752         remote_mds_nodsh && skip "remote MDS with nodsh"
1753         remote_ost_nodsh && skip "remote OST with nodsh"
1754
1755         reset_enospc
1756         rm -f $DIR/$tdir/$tfile
1757         test_mkdir $DIR/$tdir
1758
1759         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1760         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1761         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1762
1763         exhaust_precreations 0 0x80000215
1764         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1765         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1766         $LFS getstripe $DIR/$tdir/$tfile
1767
1768         reset_enospc
1769 }
1770 run_test 27p "append to a truncated file with some full OSTs"
1771
1772 test_27q() {
1773         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1775         remote_mds_nodsh && skip "remote MDS with nodsh"
1776         remote_ost_nodsh && skip "remote OST with nodsh"
1777
1778         reset_enospc
1779         rm -f $DIR/$tdir/$tfile
1780
1781         test_mkdir $DIR/$tdir
1782         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1783         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1784                 error "truncate $DIR/$tdir/$tfile failed"
1785         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1786
1787         exhaust_all_precreations 0x215
1788
1789         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1790         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1791
1792         reset_enospc
1793 }
1794 run_test 27q "append to truncated file with all OSTs full (should error)"
1795
1796 test_27r() {
1797         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1798         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1799         remote_mds_nodsh && skip "remote MDS with nodsh"
1800         remote_ost_nodsh && skip "remote OST with nodsh"
1801
1802         reset_enospc
1803         rm -f $DIR/$tdir/$tfile
1804         exhaust_precreations 0 0x80000215
1805
1806         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1807
1808         reset_enospc
1809 }
1810 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1811
1812 test_27s() { # bug 10725
1813         test_mkdir $DIR/$tdir
1814         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1815         local stripe_count=0
1816         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1817         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1818                 error "stripe width >= 2^32 succeeded" || true
1819
1820 }
1821 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1822
1823 test_27t() { # bug 10864
1824         WDIR=$(pwd)
1825         WLFS=$(which lfs)
1826         cd $DIR
1827         touch $tfile
1828         $WLFS getstripe $tfile
1829         cd $WDIR
1830 }
1831 run_test 27t "check that utils parse path correctly"
1832
1833 test_27u() { # bug 4900
1834         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1835         remote_mds_nodsh && skip "remote MDS with nodsh"
1836
1837         local index
1838         local list=$(comma_list $(mdts_nodes))
1839
1840 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1841         do_nodes $list $LCTL set_param fail_loc=0x139
1842         test_mkdir -p $DIR/$tdir
1843         trap simple_cleanup_common EXIT
1844         createmany -o $DIR/$tdir/t- 1000
1845         do_nodes $list $LCTL set_param fail_loc=0
1846
1847         TLOG=$TMP/$tfile.getstripe
1848         $LFS getstripe $DIR/$tdir > $TLOG
1849         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1850         unlinkmany $DIR/$tdir/t- 1000
1851         trap 0
1852         [[ $OBJS -gt 0 ]] &&
1853                 error "$OBJS objects created on OST-0. See $TLOG" ||
1854                 rm -f $TLOG
1855 }
1856 run_test 27u "skip object creation on OSC w/o objects"
1857
1858 test_27v() { # bug 4900
1859         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1860         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1861         remote_mds_nodsh && skip "remote MDS with nodsh"
1862         remote_ost_nodsh && skip "remote OST with nodsh"
1863
1864         exhaust_all_precreations 0x215
1865         reset_enospc
1866
1867         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1868
1869         touch $DIR/$tdir/$tfile
1870         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1871         # all except ost1
1872         for (( i=1; i < OSTCOUNT; i++ )); do
1873                 do_facet ost$i lctl set_param fail_loc=0x705
1874         done
1875         local START=`date +%s`
1876         createmany -o $DIR/$tdir/$tfile 32
1877
1878         local FINISH=`date +%s`
1879         local TIMEOUT=`lctl get_param -n timeout`
1880         local PROCESS=$((FINISH - START))
1881         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1882                error "$FINISH - $START >= $TIMEOUT / 2"
1883         sleep $((TIMEOUT / 2 - PROCESS))
1884         reset_enospc
1885 }
1886 run_test 27v "skip object creation on slow OST"
1887
1888 test_27w() { # bug 10997
1889         test_mkdir $DIR/$tdir
1890         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1891         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1892                 error "stripe size $size != 65536" || true
1893         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1894                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1895 }
1896 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1897
1898 test_27wa() {
1899         [[ $OSTCOUNT -lt 2 ]] &&
1900                 skip_env "skipping multiple stripe count/offset test"
1901
1902         test_mkdir $DIR/$tdir
1903         for i in $(seq 1 $OSTCOUNT); do
1904                 offset=$((i - 1))
1905                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1906                         error "setstripe -c $i -i $offset failed"
1907                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1908                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1909                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1910                 [ $index -ne $offset ] &&
1911                         error "stripe offset $index != $offset" || true
1912         done
1913 }
1914 run_test 27wa "check $LFS setstripe -c -i options"
1915
1916 test_27x() {
1917         remote_ost_nodsh && skip "remote OST with nodsh"
1918         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1920
1921         OFFSET=$(($OSTCOUNT - 1))
1922         OSTIDX=0
1923         local OST=$(ostname_from_index $OSTIDX)
1924
1925         test_mkdir $DIR/$tdir
1926         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1927         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1928         sleep_maxage
1929         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1930         for i in $(seq 0 $OFFSET); do
1931                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1932                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1933                 error "OST0 was degraded but new created file still use it"
1934         done
1935         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1936 }
1937 run_test 27x "create files while OST0 is degraded"
1938
1939 test_27y() {
1940         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1941         remote_mds_nodsh && skip "remote MDS with nodsh"
1942         remote_ost_nodsh && skip "remote OST with nodsh"
1943         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1944
1945         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1946         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1947                 osp.$mdtosc.prealloc_last_id)
1948         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1949                 osp.$mdtosc.prealloc_next_id)
1950         local fcount=$((last_id - next_id))
1951         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
1952         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1953
1954         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1955                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1956         local OST_DEACTIVE_IDX=-1
1957         local OSC
1958         local OSTIDX
1959         local OST
1960
1961         for OSC in $MDS_OSCS; do
1962                 OST=$(osc_to_ost $OSC)
1963                 OSTIDX=$(index_from_ostuuid $OST)
1964                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1965                         OST_DEACTIVE_IDX=$OSTIDX
1966                 fi
1967                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1968                         echo $OSC "is Deactivated:"
1969                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1970                 fi
1971         done
1972
1973         OSTIDX=$(index_from_ostuuid $OST)
1974         test_mkdir $DIR/$tdir
1975         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1976
1977         for OSC in $MDS_OSCS; do
1978                 OST=$(osc_to_ost $OSC)
1979                 OSTIDX=$(index_from_ostuuid $OST)
1980                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1981                         echo $OST "is degraded:"
1982                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1983                                                 obdfilter.$OST.degraded=1
1984                 fi
1985         done
1986
1987         sleep_maxage
1988         createmany -o $DIR/$tdir/$tfile $fcount
1989
1990         for OSC in $MDS_OSCS; do
1991                 OST=$(osc_to_ost $OSC)
1992                 OSTIDX=$(index_from_ostuuid $OST)
1993                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1994                         echo $OST "is recovered from degraded:"
1995                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1996                                                 obdfilter.$OST.degraded=0
1997                 else
1998                         do_facet $SINGLEMDS lctl --device %$OSC activate
1999                 fi
2000         done
2001
2002         # all osp devices get activated, hence -1 stripe count restored
2003         local stripe_count=0
2004
2005         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2006         # devices get activated.
2007         sleep_maxage
2008         $LFS setstripe -c -1 $DIR/$tfile
2009         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2010         rm -f $DIR/$tfile
2011         [ $stripe_count -ne $OSTCOUNT ] &&
2012                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2013         return 0
2014 }
2015 run_test 27y "create files while OST0 is degraded and the rest inactive"
2016
2017 check_seq_oid()
2018 {
2019         log "check file $1"
2020
2021         lmm_count=$($GETSTRIPE -c $1)
2022         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
2023         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
2024
2025         local old_ifs="$IFS"
2026         IFS=$'[:]'
2027         fid=($($LFS path2fid $1))
2028         IFS="$old_ifs"
2029
2030         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2031         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2032
2033         # compare lmm_seq and lu_fid->f_seq
2034         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2035         # compare lmm_object_id and lu_fid->oid
2036         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2037
2038         # check the trusted.fid attribute of the OST objects of the file
2039         local have_obdidx=false
2040         local stripe_nr=0
2041         $GETSTRIPE $1 | while read obdidx oid hex seq; do
2042                 # skip lines up to and including "obdidx"
2043                 [ -z "$obdidx" ] && break
2044                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2045                 $have_obdidx || continue
2046
2047                 local ost=$((obdidx + 1))
2048                 local dev=$(ostdevname $ost)
2049                 local oid_hex
2050
2051                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2052
2053                 seq=$(echo $seq | sed -e "s/^0x//g")
2054                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2055                         oid_hex=$(echo $oid)
2056                 else
2057                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2058                 fi
2059                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2060
2061                 local ff=""
2062                 #
2063                 # Don't unmount/remount the OSTs if we don't need to do that.
2064                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2065                 # update too, until that use mount/ll_decode_filter_fid/mount.
2066                 # Re-enable when debugfs will understand new filter_fid.
2067                 #
2068                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2069                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2070                                 $dev 2>/dev/null" | grep "parent=")
2071                 fi
2072                 if [ -z "$ff" ]; then
2073                         stop ost$ost
2074                         mount_fstype ost$ost
2075                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2076                                 $(facet_mntpt ost$ost)/$obj_file)
2077                         unmount_fstype ost$ost
2078                         start ost$ost $dev $OST_MOUNT_OPTS
2079                         clients_up
2080                 fi
2081
2082                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2083
2084                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2085
2086                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2087                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2088                 #
2089                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2090                 #       stripe_size=1048576 component_id=1 component_start=0 \
2091                 #       component_end=33554432
2092                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2093                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2094                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2095                 local ff_pstripe
2096                 if grep -q 'stripe=' <<<$ff; then
2097                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2098                 else
2099                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2100                         # into f_ver in this case.  See comment on ff_parent.
2101                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2102                 fi
2103
2104                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2105                 [ $ff_pseq = $lmm_seq ] ||
2106                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2107                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2108                 [ $ff_poid = $lmm_oid ] ||
2109                         error "FF parent OID $ff_poid != $lmm_oid"
2110                 (($ff_pstripe == $stripe_nr)) ||
2111                         error "FF stripe $ff_pstripe != $stripe_nr"
2112
2113                 stripe_nr=$((stripe_nr + 1))
2114                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2115                         continue
2116                 if grep -q 'stripe_count=' <<<$ff; then
2117                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2118                                             -e 's/ .*//' <<<$ff)
2119                         [ $lmm_count = $ff_scnt ] ||
2120                                 error "FF stripe count $lmm_count != $ff_scnt"
2121                 fi
2122         done
2123 }
2124
2125 test_27z() {
2126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2127         remote_ost_nodsh && skip "remote OST with nodsh"
2128
2129         test_mkdir $DIR/$tdir
2130         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2131                 { error "setstripe -c -1 failed"; return 1; }
2132         # We need to send a write to every object to get parent FID info set.
2133         # This _should_ also work for setattr, but does not currently.
2134         # touch $DIR/$tdir/$tfile-1 ||
2135         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2136                 { error "dd $tfile-1 failed"; return 2; }
2137         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2138                 { error "setstripe -c -1 failed"; return 3; }
2139         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2140                 { error "dd $tfile-2 failed"; return 4; }
2141
2142         # make sure write RPCs have been sent to OSTs
2143         sync; sleep 5; sync
2144
2145         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2146         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2147 }
2148 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2149
2150 test_27A() { # b=19102
2151         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2152
2153         save_layout_restore_at_exit $MOUNT
2154         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2155         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2156                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2157         local default_size=$($GETSTRIPE -S $MOUNT)
2158         local default_offset=$($GETSTRIPE -i $MOUNT)
2159         local dsize=$(do_facet $SINGLEMDS \
2160                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2161         [ $default_size -eq $dsize ] ||
2162                 error "stripe size $default_size != $dsize"
2163         [ $default_offset -eq -1 ] ||
2164                 error "stripe offset $default_offset != -1"
2165 }
2166 run_test 27A "check filesystem-wide default LOV EA values"
2167
2168 test_27B() { # LU-2523
2169         test_mkdir $DIR/$tdir
2170         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2171         touch $DIR/$tdir/f0
2172         # open f1 with O_LOV_DELAY_CREATE
2173         # rename f0 onto f1
2174         # call setstripe ioctl on open file descriptor for f1
2175         # close
2176         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2177                 $DIR/$tdir/f0
2178
2179         rm -f $DIR/$tdir/f1
2180         # open f1 with O_LOV_DELAY_CREATE
2181         # unlink f1
2182         # call setstripe ioctl on open file descriptor for f1
2183         # close
2184         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2185
2186         # Allow multiop to fail in imitation of NFS's busted semantics.
2187         true
2188 }
2189 run_test 27B "call setstripe on open unlinked file/rename victim"
2190
2191 test_27C() { #LU-2871
2192         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2193
2194         declare -a ost_idx
2195         local index
2196         local found
2197         local i
2198         local j
2199
2200         test_mkdir $DIR/$tdir
2201         cd $DIR/$tdir
2202         for i in $(seq 0 $((OSTCOUNT - 1))); do
2203                 # set stripe across all OSTs starting from OST$i
2204                 $SETSTRIPE -i $i -c -1 $tfile$i
2205                 # get striping information
2206                 ost_idx=($($GETSTRIPE $tfile$i |
2207                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2208                 echo ${ost_idx[@]}
2209
2210                 # check the layout
2211                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2212                         error "${#ost_idx[@]} != $OSTCOUNT"
2213
2214                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2215                         found=0
2216                         for j in $(echo ${ost_idx[@]}); do
2217                                 if [ $index -eq $j ]; then
2218                                         found=1
2219                                         break
2220                                 fi
2221                         done
2222                         [ $found = 1 ] ||
2223                                 error "Can not find $index in ${ost_idx[@]}"
2224                 done
2225         done
2226 }
2227 run_test 27C "check full striping across all OSTs"
2228
2229 test_27D() {
2230         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2231         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2232         remote_mds_nodsh && skip "remote MDS with nodsh"
2233
2234         local POOL=${POOL:-testpool}
2235         local first_ost=0
2236         local last_ost=$(($OSTCOUNT - 1))
2237         local ost_step=1
2238         local ost_list=$(seq $first_ost $ost_step $last_ost)
2239         local ost_range="$first_ost $last_ost $ost_step"
2240
2241         test_mkdir $DIR/$tdir
2242         pool_add $POOL || error "pool_add failed"
2243         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2244
2245         local skip27D
2246         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2247                 skip27D+="-s 29"
2248         [ $MDS1_VERSION -lt $(version_code 2.9.55) -o \
2249           $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2250                 skip27D+=" -s 30,31"
2251         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2252                 error "llapi_layout_test failed"
2253
2254         destroy_test_pools || error "destroy test pools failed"
2255 }
2256 run_test 27D "validate llapi_layout API"
2257
2258 # Verify that default_easize is increased from its initial value after
2259 # accessing a widely striped file.
2260 test_27E() {
2261         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2262         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2263                 skip "client does not have LU-3338 fix"
2264
2265         # 72 bytes is the minimum space required to store striping
2266         # information for a file striped across one OST:
2267         # (sizeof(struct lov_user_md_v3) +
2268         #  sizeof(struct lov_user_ost_data_v1))
2269         local min_easize=72
2270         $LCTL set_param -n llite.*.default_easize $min_easize ||
2271                 error "lctl set_param failed"
2272         local easize=$($LCTL get_param -n llite.*.default_easize)
2273
2274         [ $easize -eq $min_easize ] ||
2275                 error "failed to set default_easize"
2276
2277         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2278                 error "setstripe failed"
2279         cat $DIR/$tfile
2280         rm $DIR/$tfile
2281
2282         easize=$($LCTL get_param -n llite.*.default_easize)
2283
2284         [ $easize -gt $min_easize ] ||
2285                 error "default_easize not updated"
2286 }
2287 run_test 27E "check that default extended attribute size properly increases"
2288
2289 test_27F() { # LU-5346/LU-7975
2290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2291         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2292         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2293                 skip "Need MDS version at least 2.8.51"
2294         remote_ost_nodsh && skip "remote OST with nodsh"
2295
2296         test_mkdir $DIR/$tdir
2297         rm -f $DIR/$tdir/f0
2298         $SETSTRIPE -c 2 $DIR/$tdir
2299
2300         # stop all OSTs to reproduce situation for LU-7975 ticket
2301         for num in $(seq $OSTCOUNT); do
2302                 stop ost$num
2303         done
2304
2305         # open/create f0 with O_LOV_DELAY_CREATE
2306         # truncate f0 to a non-0 size
2307         # close
2308         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2309
2310         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2311         # open/write it again to force delayed layout creation
2312         cat /etc/hosts > $DIR/$tdir/f0 &
2313         catpid=$!
2314
2315         # restart OSTs
2316         for num in $(seq $OSTCOUNT); do
2317                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2318                         error "ost$num failed to start"
2319         done
2320
2321         wait $catpid || error "cat failed"
2322
2323         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2324         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2325
2326 }
2327 run_test 27F "Client resend delayed layout creation with non-zero size"
2328
2329 test_27G() { #LU-10629
2330         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2331                 skip "Need MDS version at least 2.11.51"
2332         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2333         remote_mds_nodsh && skip "remote MDS with nodsh"
2334         local POOL=${POOL:-testpool}
2335         local ostrange="0 0 1"
2336
2337         test_mkdir $DIR/$tdir
2338         pool_add $POOL || error "pool_add failed"
2339         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2340         $LFS setstripe -p $POOL $DIR/$tdir
2341
2342         local pool=$($LFS getstripe -p $DIR/$tdir)
2343
2344         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2345
2346         $LFS setstripe -d $DIR/$tdir
2347
2348         pool=$($LFS getstripe -p $DIR/$tdir)
2349
2350         rmdir $DIR/$tdir
2351
2352         [ -z "$pool" ] || error "'$pool' is not empty"
2353 }
2354 run_test 27G "Clear OST pool from stripe"
2355
2356 test_27H() {
2357         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2358                 skip "Need MDS version newer than 2.11.54"
2359         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2360         test_mkdir $DIR/$tdir
2361         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2362         touch $DIR/$tdir/$tfile
2363         $LFS getstripe -c $DIR/$tdir/$tfile
2364         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2365                 error "two-stripe file doesn't have two stripes"
2366
2367         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2368         $LFS getstripe -y $DIR/$tdir/$tfile
2369         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2370              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2371                 error "expected l_ost_idx: [02]$ not matched"
2372
2373         # make sure ost list have been cleared
2374         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
2375         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2376                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2377         touch $DIR/$tdir/f3
2378         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2379 }
2380 run_test 27H "Set specific OSTs stripe"
2381
2382 test_27M() {
2383         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2384                 skip "Need MDS version >= than 2.12.57"
2385         remote_mds_nodsh && skip "remote MDS with nodsh"
2386         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2387
2388         test_mkdir $DIR/$tdir
2389
2390         # Set default striping on directory
2391         $LFS setstripe -C 4 $DIR/$tdir
2392
2393         echo 1 > $DIR/$tdir/${tfile}.1
2394         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2395         local setcount=4
2396         [ $count -eq $setcount ] ||
2397                 error "(1) stripe count $count, should be $setcount"
2398
2399         # Capture existing append_stripe_count setting for restore
2400         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2401         local mdts=$(comma_list $(mdts_nodes))
2402         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2403
2404         local appendcount=$orig_count
2405         echo 1 >> $DIR/$tdir/${tfile}.2_append
2406         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2407         [ $count -eq $appendcount ] ||
2408                 error "(2)stripe count $count, should be $appendcount for append"
2409
2410         # Disable O_APPEND striping, verify it works
2411         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2412
2413         # Should now get the default striping, which is 4
2414         setcount=4
2415         echo 1 >> $DIR/$tdir/${tfile}.3_append
2416         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2417         [ $count -eq $setcount ] ||
2418                 error "(3) stripe count $count, should be $setcount"
2419
2420         # Try changing the stripe count for append files
2421         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2422
2423         # Append striping is now 2 (directory default is still 4)
2424         appendcount=2
2425         echo 1 >> $DIR/$tdir/${tfile}.4_append
2426         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2427         [ $count -eq $appendcount ] ||
2428                 error "(4) stripe count $count, should be $appendcount for append"
2429
2430         # Test append stripe count of -1
2431         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2432         appendcount=$OSTCOUNT
2433         echo 1 >> $DIR/$tdir/${tfile}.5
2434         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2435         [ $count -eq $appendcount ] ||
2436                 error "(5) stripe count $count, should be $appendcount for append"
2437
2438         # Set append striping back to default of 1
2439         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2440
2441         # Try a new default striping, PFL + DOM
2442         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2443
2444         # Create normal DOM file, DOM returns stripe count == 0
2445         setcount=0
2446         touch $DIR/$tdir/${tfile}.6
2447         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2448         [ $count -eq $setcount ] ||
2449                 error "(6) stripe count $count, should be $setcount"
2450
2451         # Show
2452         appendcount=1
2453         echo 1 >> $DIR/$tdir/${tfile}.7_append
2454         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2455         [ $count -eq $appendcount ] ||
2456                 error "(7) stripe count $count, should be $appendcount for append"
2457
2458         # Clean up DOM layout
2459         $LFS setstripe -d $DIR/$tdir
2460
2461         # Now test that append striping works when layout is from root
2462         $LFS setstripe -c 2 $MOUNT
2463         # Make a special directory for this
2464         mkdir $DIR/${tdir}/${tdir}.2
2465         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2466
2467         # Verify for normal file
2468         setcount=2
2469         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2470         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2471         [ $count -eq $setcount ] ||
2472                 error "(8) stripe count $count, should be $setcount"
2473
2474         appendcount=1
2475         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2476         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2477         [ $count -eq $appendcount ] ||
2478                 error "(9) stripe count $count, should be $appendcount for append"
2479
2480         # Now test O_APPEND striping with pools
2481         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2482         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2483
2484         # Create the pool
2485         pool_add $TESTNAME || error "pool creation failed"
2486         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2487
2488         echo 1 >> $DIR/$tdir/${tfile}.10_append
2489
2490         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2491         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2492
2493         # Check that count is still correct
2494         appendcount=1
2495         echo 1 >> $DIR/$tdir/${tfile}.11_append
2496         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2497         [ $count -eq $appendcount ] ||
2498                 error "(11) stripe count $count, should be $appendcount for append"
2499
2500         # Disable O_APPEND stripe count, verify pool works separately
2501         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2502
2503         echo 1 >> $DIR/$tdir/${tfile}.12_append
2504
2505         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2506         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2507
2508         # Remove pool setting, verify it's not applied
2509         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2510
2511         echo 1 >> $DIR/$tdir/${tfile}.13_append
2512
2513         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2514         [ "$pool" = "" ] || error "(13) pool found: $pool"
2515 }
2516 run_test 27M "test O_APPEND striping"
2517
2518 # createtest also checks that device nodes are created and
2519 # then visible correctly (#2091)
2520 test_28() { # bug 2091
2521         test_mkdir $DIR/d28
2522         $CREATETEST $DIR/d28/ct || error "createtest failed"
2523 }
2524 run_test 28 "create/mknod/mkdir with bad file types ============"
2525
2526 test_29() {
2527         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2528
2529         sync; sleep 1; sync # flush out any dirty pages from previous tests
2530         cancel_lru_locks
2531         test_mkdir $DIR/d29
2532         touch $DIR/d29/foo
2533         log 'first d29'
2534         ls -l $DIR/d29
2535
2536         declare -i LOCKCOUNTORIG=0
2537         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2538                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2539         done
2540         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2541
2542         declare -i LOCKUNUSEDCOUNTORIG=0
2543         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2544                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2545         done
2546
2547         log 'second d29'
2548         ls -l $DIR/d29
2549         log 'done'
2550
2551         declare -i LOCKCOUNTCURRENT=0
2552         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2553                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2554         done
2555
2556         declare -i LOCKUNUSEDCOUNTCURRENT=0
2557         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2558                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2559         done
2560
2561         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2562                 $LCTL set_param -n ldlm.dump_namespaces ""
2563                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2564                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2565                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2566                 return 2
2567         fi
2568         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2569                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2570                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2571                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2572                 return 3
2573         fi
2574 }
2575 run_test 29 "IT_GETATTR regression  ============================"
2576
2577 test_30a() { # was test_30
2578         cp $(which ls) $DIR || cp /bin/ls $DIR
2579         $DIR/ls / || error "Can't execute binary from lustre"
2580         rm $DIR/ls
2581 }
2582 run_test 30a "execute binary from Lustre (execve) =============="
2583
2584 test_30b() {
2585         cp `which ls` $DIR || cp /bin/ls $DIR
2586         chmod go+rx $DIR/ls
2587         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
2588         rm $DIR/ls
2589 }
2590 run_test 30b "execute binary from Lustre as non-root ==========="
2591
2592 test_30c() { # b=22376
2593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2594
2595         cp `which ls` $DIR || cp /bin/ls $DIR
2596         chmod a-rw $DIR/ls
2597         cancel_lru_locks mdc
2598         cancel_lru_locks osc
2599         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
2600         rm -f $DIR/ls
2601 }
2602 run_test 30c "execute binary from Lustre without read perms ===="
2603
2604 test_31a() {
2605         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
2606         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
2607 }
2608 run_test 31a "open-unlink file =================================="
2609
2610 test_31b() {
2611         touch $DIR/f31 || error "touch $DIR/f31 failed"
2612         ln $DIR/f31 $DIR/f31b || error "ln failed"
2613         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
2614         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
2615 }
2616 run_test 31b "unlink file with multiple links while open ======="
2617
2618 test_31c() {
2619         touch $DIR/f31 || error "touch $DIR/f31 failed"
2620         ln $DIR/f31 $DIR/f31c || error "ln failed"
2621         multiop_bg_pause $DIR/f31 O_uc ||
2622                 error "multiop_bg_pause for $DIR/f31 failed"
2623         MULTIPID=$!
2624         $MULTIOP $DIR/f31c Ouc
2625         kill -USR1 $MULTIPID
2626         wait $MULTIPID
2627 }
2628 run_test 31c "open-unlink file with multiple links ============="
2629
2630 test_31d() {
2631         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
2632         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
2633 }
2634 run_test 31d "remove of open directory ========================="
2635
2636 test_31e() { # bug 2904
2637         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
2638 }
2639 run_test 31e "remove of open non-empty directory ==============="
2640
2641 test_31f() { # bug 4554
2642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2643
2644         set -vx
2645         test_mkdir $DIR/d31f
2646         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2647         cp /etc/hosts $DIR/d31f
2648         ls -l $DIR/d31f
2649         $GETSTRIPE $DIR/d31f/hosts
2650         multiop_bg_pause $DIR/d31f D_c || return 1
2651         MULTIPID=$!
2652
2653         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2654         test_mkdir $DIR/d31f
2655         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2656         cp /etc/hosts $DIR/d31f
2657         ls -l $DIR/d31f
2658         $GETSTRIPE $DIR/d31f/hosts
2659         multiop_bg_pause $DIR/d31f D_c || return 1
2660         MULTIPID2=$!
2661
2662         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2663         wait $MULTIPID || error "first opendir $MULTIPID failed"
2664
2665         sleep 6
2666
2667         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2668         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2669         set +vx
2670 }
2671 run_test 31f "remove of open directory with open-unlink file ==="
2672
2673 test_31g() {
2674         echo "-- cross directory link --"
2675         test_mkdir -c1 $DIR/${tdir}ga
2676         test_mkdir -c1 $DIR/${tdir}gb
2677         touch $DIR/${tdir}ga/f
2678         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2679         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2680         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2681         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2682         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2683 }
2684 run_test 31g "cross directory link==============="
2685
2686 test_31h() {
2687         echo "-- cross directory link --"
2688         test_mkdir -c1 $DIR/${tdir}
2689         test_mkdir -c1 $DIR/${tdir}/dir
2690         touch $DIR/${tdir}/f
2691         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2692         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2693         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2694         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2695         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2696 }
2697 run_test 31h "cross directory link under child==============="
2698
2699 test_31i() {
2700         echo "-- cross directory link --"
2701         test_mkdir -c1 $DIR/$tdir
2702         test_mkdir -c1 $DIR/$tdir/dir
2703         touch $DIR/$tdir/dir/f
2704         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2705         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2706         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2707         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2708         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2709 }
2710 run_test 31i "cross directory link under parent==============="
2711
2712 test_31j() {
2713         test_mkdir -c1 -p $DIR/$tdir
2714         test_mkdir -c1 -p $DIR/$tdir/dir1
2715         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2716         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2717         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2718         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2719         return 0
2720 }
2721 run_test 31j "link for directory==============="
2722
2723 test_31k() {
2724         test_mkdir -c1 -p $DIR/$tdir
2725         touch $DIR/$tdir/s
2726         touch $DIR/$tdir/exist
2727         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2728         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2729         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2730         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2731         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2732         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2733         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2734         return 0
2735 }
2736 run_test 31k "link to file: the same, non-existing, dir==============="
2737
2738 test_31m() {
2739         mkdir $DIR/d31m
2740         touch $DIR/d31m/s
2741         mkdir $DIR/d31m2
2742         touch $DIR/d31m2/exist
2743         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2744         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2745         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2746         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2747         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2748         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2749         return 0
2750 }
2751 run_test 31m "link to file: the same, non-existing, dir==============="
2752
2753 test_31n() {
2754         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2755         nlink=$(stat --format=%h $DIR/$tfile)
2756         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2757         local fd=$(free_fd)
2758         local cmd="exec $fd<$DIR/$tfile"
2759         eval $cmd
2760         cmd="exec $fd<&-"
2761         trap "eval $cmd" EXIT
2762         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2763         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2764         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2765         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2766         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2767         eval $cmd
2768 }
2769 run_test 31n "check link count of unlinked file"
2770
2771 link_one() {
2772         local TEMPNAME=$(mktemp $1_XXXXXX)
2773         mlink $TEMPNAME $1 2> /dev/null &&
2774                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2775         munlink $TEMPNAME
2776 }
2777
2778 test_31o() { # LU-2901
2779         test_mkdir $DIR/$tdir
2780         for LOOP in $(seq 100); do
2781                 rm -f $DIR/$tdir/$tfile*
2782                 for THREAD in $(seq 8); do
2783                         link_one $DIR/$tdir/$tfile.$LOOP &
2784                 done
2785                 wait
2786                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2787                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2788                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2789                         break || true
2790         done
2791 }
2792 run_test 31o "duplicate hard links with same filename"
2793
2794 test_31p() {
2795         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
2796
2797         test_mkdir $DIR/$tdir
2798         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2799         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
2800
2801         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2802                 error "open unlink test1 failed"
2803         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2804                 error "open unlink test2 failed"
2805
2806         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2807                 error "test1 still exists"
2808         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2809                 error "test2 still exists"
2810 }
2811 run_test 31p "remove of open striped directory"
2812
2813 cleanup_test32_mount() {
2814         local rc=0
2815         trap 0
2816         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2817         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2818         losetup -d $loopdev || true
2819         rm -rf $DIR/$tdir
2820         return $rc
2821 }
2822
2823 test_32a() {
2824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2825
2826         echo "== more mountpoints and symlinks ================="
2827         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2828         trap cleanup_test32_mount EXIT
2829         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2830         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2831                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2832         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
2833                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
2834         cleanup_test32_mount
2835 }
2836 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2837
2838 test_32b() {
2839         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2840
2841         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2842         trap cleanup_test32_mount EXIT
2843         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2844         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2845                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2846         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
2847                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
2848         cleanup_test32_mount
2849 }
2850 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2851
2852 test_32c() {
2853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2854
2855         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2856         trap cleanup_test32_mount EXIT
2857         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2858         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2859                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2860         test_mkdir -p $DIR/$tdir/d2/test_dir
2861         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2862                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
2863         cleanup_test32_mount
2864 }
2865 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2866
2867 test_32d() {
2868         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2869
2870         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2871         trap cleanup_test32_mount EXIT
2872         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2873         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2874                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2875         test_mkdir -p $DIR/$tdir/d2/test_dir
2876         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
2877                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
2878         cleanup_test32_mount
2879 }
2880 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2881
2882 test_32e() {
2883         rm -fr $DIR/$tdir
2884         test_mkdir -p $DIR/$tdir/tmp
2885         local tmp_dir=$DIR/$tdir/tmp
2886         ln -s $DIR/$tdir $tmp_dir/symlink11
2887         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2888         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2889         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2890 }
2891 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2892
2893 test_32f() {
2894         rm -fr $DIR/$tdir
2895         test_mkdir -p $DIR/$tdir/tmp
2896         local tmp_dir=$DIR/$tdir/tmp
2897         ln -s $DIR/$tdir $tmp_dir/symlink11
2898         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2899         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2900         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2901 }
2902 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2903
2904 test_32g() {
2905         local tmp_dir=$DIR/$tdir/tmp
2906         test_mkdir -p $tmp_dir
2907         test_mkdir $DIR/${tdir}2
2908         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2909         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2910         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2911         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2912         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2913         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2914 }
2915 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2916
2917 test_32h() {
2918         rm -fr $DIR/$tdir $DIR/${tdir}2
2919         tmp_dir=$DIR/$tdir/tmp
2920         test_mkdir -p $tmp_dir
2921         test_mkdir $DIR/${tdir}2
2922         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2923         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2924         ls $tmp_dir/symlink12 || error "listing symlink12"
2925         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2926 }
2927 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2928
2929 test_32i() {
2930         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2931
2932         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2933         trap cleanup_test32_mount EXIT
2934         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2935         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2936                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2937         touch $DIR/$tdir/test_file
2938         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
2939                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
2940         cleanup_test32_mount
2941 }
2942 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2943
2944 test_32j() {
2945         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2946
2947         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2948         trap cleanup_test32_mount EXIT
2949         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2950         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2951                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2952         touch $DIR/$tdir/test_file
2953         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
2954                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
2955         cleanup_test32_mount
2956 }
2957 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2958
2959 test_32k() {
2960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2961
2962         rm -fr $DIR/$tdir
2963         trap cleanup_test32_mount EXIT
2964         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2965         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2966                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2967         test_mkdir -p $DIR/$tdir/d2
2968         touch $DIR/$tdir/d2/test_file || error "touch failed"
2969         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2970                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
2971         cleanup_test32_mount
2972 }
2973 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2974
2975 test_32l() {
2976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2977
2978         rm -fr $DIR/$tdir
2979         trap cleanup_test32_mount EXIT
2980         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2981         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
2982                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
2983         test_mkdir -p $DIR/$tdir/d2
2984         touch $DIR/$tdir/d2/test_file || error "touch failed"
2985         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
2986                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
2987         cleanup_test32_mount
2988 }
2989 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2990
2991 test_32m() {
2992         rm -fr $DIR/d32m
2993         test_mkdir -p $DIR/d32m/tmp
2994         TMP_DIR=$DIR/d32m/tmp
2995         ln -s $DIR $TMP_DIR/symlink11
2996         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2997         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
2998                 error "symlink11 not a link"
2999         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3000                 error "symlink01 not a link"
3001 }
3002 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3003
3004 test_32n() {
3005         rm -fr $DIR/d32n
3006         test_mkdir -p $DIR/d32n/tmp
3007         TMP_DIR=$DIR/d32n/tmp
3008         ln -s $DIR $TMP_DIR/symlink11
3009         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3010         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3011         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3012 }
3013 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3014
3015 test_32o() {
3016         touch $DIR/$tfile
3017         test_mkdir -p $DIR/d32o/tmp
3018         TMP_DIR=$DIR/d32o/tmp
3019         ln -s $DIR/$tfile $TMP_DIR/symlink12
3020         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3021         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3022                 error "symlink12 not a link"
3023         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3024         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3025                 error "$DIR/d32o/tmp/symlink12 not file type"
3026         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3027                 error "$DIR/d32o/symlink02 not file type"
3028 }
3029 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3030
3031 test_32p() {
3032         log 32p_1
3033         rm -fr $DIR/d32p
3034         log 32p_2
3035         rm -f $DIR/$tfile
3036         log 32p_3
3037         touch $DIR/$tfile
3038         log 32p_4
3039         test_mkdir -p $DIR/d32p/tmp
3040         log 32p_5
3041         TMP_DIR=$DIR/d32p/tmp
3042         log 32p_6
3043         ln -s $DIR/$tfile $TMP_DIR/symlink12
3044         log 32p_7
3045         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3046         log 32p_8
3047         cat $DIR/d32p/tmp/symlink12 ||
3048                 error "Can't open $DIR/d32p/tmp/symlink12"
3049         log 32p_9
3050         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3051         log 32p_10
3052 }
3053 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3054
3055 test_32q() {
3056         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3057
3058         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3059         trap cleanup_test32_mount EXIT
3060         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3061         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3062         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3063                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3064         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3065         cleanup_test32_mount
3066 }
3067 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3068
3069 test_32r() {
3070         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3071
3072         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3073         trap cleanup_test32_mount EXIT
3074         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3075         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3076         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3077                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3078         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3079         cleanup_test32_mount
3080 }
3081 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3082
3083 test_33aa() {
3084         rm -f $DIR/$tfile
3085         touch $DIR/$tfile
3086         chmod 444 $DIR/$tfile
3087         chown $RUNAS_ID $DIR/$tfile
3088         log 33_1
3089         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3090         log 33_2
3091 }
3092 run_test 33aa "write file with mode 444 (should return error)"
3093
3094 test_33a() {
3095         rm -fr $DIR/$tdir
3096         test_mkdir $DIR/$tdir
3097         chown $RUNAS_ID $DIR/$tdir
3098         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3099                 error "$RUNAS create $tdir/$tfile failed"
3100         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3101                 error "open RDWR" || true
3102 }
3103 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3104
3105 test_33b() {
3106         rm -fr $DIR/$tdir
3107         test_mkdir $DIR/$tdir
3108         chown $RUNAS_ID $DIR/$tdir
3109         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3110 }
3111 run_test 33b "test open file with malformed flags (No panic)"
3112
3113 test_33c() {
3114         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3115         remote_ost_nodsh && skip "remote OST with nodsh"
3116
3117         local ostnum
3118         local ostname
3119         local write_bytes
3120         local all_zeros
3121
3122         all_zeros=:
3123         rm -fr $DIR/$tdir
3124         test_mkdir $DIR/$tdir
3125         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3126
3127         sync
3128         for ostnum in $(seq $OSTCOUNT); do
3129                 # test-framework's OST numbering is one-based, while Lustre's
3130                 # is zero-based
3131                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3132                 # Parsing llobdstat's output sucks; we could grep the /proc
3133                 # path, but that's likely to not be as portable as using the
3134                 # llobdstat utility.  So we parse lctl output instead.
3135                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3136                         obdfilter/$ostname/stats |
3137                         awk '/^write_bytes/ {print $7}' )
3138                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3139                 if (( ${write_bytes:-0} > 0 ))
3140                 then
3141                         all_zeros=false
3142                         break;
3143                 fi
3144         done
3145
3146         $all_zeros || return 0
3147
3148         # Write four bytes
3149         echo foo > $DIR/$tdir/bar
3150         # Really write them
3151         sync
3152
3153         # Total up write_bytes after writing.  We'd better find non-zeros.
3154         for ostnum in $(seq $OSTCOUNT); do
3155                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3156                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3157                         obdfilter/$ostname/stats |
3158                         awk '/^write_bytes/ {print $7}' )
3159                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3160                 if (( ${write_bytes:-0} > 0 ))
3161                 then
3162                         all_zeros=false
3163                         break;
3164                 fi
3165         done
3166
3167         if $all_zeros
3168         then
3169                 for ostnum in $(seq $OSTCOUNT); do
3170                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3171                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3172                         do_facet ost$ostnum lctl get_param -n \
3173                                 obdfilter/$ostname/stats
3174                 done
3175                 error "OST not keeping write_bytes stats (b22312)"
3176         fi
3177 }
3178 run_test 33c "test llobdstat and write_bytes"
3179
3180 test_33d() {
3181         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3182         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3183
3184         local MDTIDX=1
3185         local remote_dir=$DIR/$tdir/remote_dir
3186
3187         test_mkdir $DIR/$tdir
3188         $LFS mkdir -i $MDTIDX $remote_dir ||
3189                 error "create remote directory failed"
3190
3191         touch $remote_dir/$tfile
3192         chmod 444 $remote_dir/$tfile
3193         chown $RUNAS_ID $remote_dir/$tfile
3194
3195         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3196
3197         chown $RUNAS_ID $remote_dir
3198         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3199                                         error "create" || true
3200         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3201                                     error "open RDWR" || true
3202         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3203 }
3204 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3205
3206 test_33e() {
3207         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3208
3209         mkdir $DIR/$tdir
3210
3211         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3212         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3213         mkdir $DIR/$tdir/local_dir
3214
3215         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3216         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3217         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3218
3219         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3220                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3221
3222         rmdir $DIR/$tdir/* || error "rmdir failed"
3223
3224         umask 777
3225         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3226         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3227         mkdir $DIR/$tdir/local_dir
3228
3229         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3230         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3231         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3232
3233         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3234                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3235
3236         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3237
3238         umask 000
3239         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3240         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3241         mkdir $DIR/$tdir/local_dir
3242
3243         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3244         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3245         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3246
3247         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3248                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3249 }
3250 run_test 33e "mkdir and striped directory should have same mode"
3251
3252 cleanup_33f() {
3253         trap 0
3254         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3255 }
3256
3257 test_33f() {
3258         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3259         remote_mds_nodsh && skip "remote MDS with nodsh"
3260
3261         mkdir $DIR/$tdir
3262         chmod go+rwx $DIR/$tdir
3263         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3264         trap cleanup_33f EXIT
3265
3266         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3267                 error "cannot create striped directory"
3268
3269         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3270                 error "cannot create files in striped directory"
3271
3272         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3273                 error "cannot remove files in striped directory"
3274
3275         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3276                 error "cannot remove striped directory"
3277
3278         cleanup_33f
3279 }
3280 run_test 33f "nonroot user can create, access, and remove a striped directory"
3281
3282 test_33g() {
3283         mkdir -p $DIR/$tdir/dir2
3284
3285         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3286         echo $err
3287         [[ $err =~ "exists" ]] || error "Not exists error"
3288 }
3289 run_test 33g "nonroot user create already existing root created file"
3290
3291 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3292 test_34a() {
3293         rm -f $DIR/f34
3294         $MCREATE $DIR/f34 || error "mcreate failed"
3295         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3296                 error "getstripe failed"
3297         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3298         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3299                 error "getstripe failed"
3300         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3301                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3302 }
3303 run_test 34a "truncate file that has not been opened ==========="
3304
3305 test_34b() {
3306         [ ! -f $DIR/f34 ] && test_34a
3307         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3308                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3309         $OPENFILE -f O_RDONLY $DIR/f34
3310         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" ||
3311                 error "getstripe failed"
3312         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3313                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3314 }
3315 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3316
3317 test_34c() {
3318         [ ! -f $DIR/f34 ] && test_34a
3319         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3320                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3321         $OPENFILE -f O_RDWR $DIR/f34
3322         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
3323         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3324                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3325 }
3326 run_test 34c "O_RDWR opening file-with-size works =============="
3327
3328 test_34d() {
3329         [ ! -f $DIR/f34 ] && test_34a
3330         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3331                 error "dd failed"
3332         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3333                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3334         rm $DIR/f34
3335 }
3336 run_test 34d "write to sparse file ============================="
3337
3338 test_34e() {
3339         rm -f $DIR/f34e
3340         $MCREATE $DIR/f34e || error "mcreate failed"
3341         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3342         $CHECKSTAT -s 1000 $DIR/f34e ||
3343                 error "Size of $DIR/f34e not equal to 1000 bytes"
3344         $OPENFILE -f O_RDWR $DIR/f34e
3345         $CHECKSTAT -s 1000 $DIR/f34e ||
3346                 error "Size of $DIR/f34e not equal to 1000 bytes"
3347 }
3348 run_test 34e "create objects, some with size and some without =="
3349
3350 test_34f() { # bug 6242, 6243
3351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3352
3353         SIZE34F=48000
3354         rm -f $DIR/f34f
3355         $MCREATE $DIR/f34f || error "mcreate failed"
3356         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3357         dd if=$DIR/f34f of=$TMP/f34f
3358         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3359         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3360         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3361         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3362         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3363 }
3364 run_test 34f "read from a file with no objects until EOF ======="
3365
3366 test_34g() {
3367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3368
3369         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3370                 error "dd failed"
3371         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3372         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3373                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3374         cancel_lru_locks osc
3375         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3376                 error "wrong size after lock cancel"
3377
3378         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3379         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3380                 error "expanding truncate failed"
3381         cancel_lru_locks osc
3382         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3383                 error "wrong expanded size after lock cancel"
3384 }
3385 run_test 34g "truncate long file ==============================="
3386
3387 test_34h() {
3388         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3389
3390         local gid=10
3391         local sz=1000
3392
3393         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3394         sync # Flush the cache so that multiop below does not block on cache
3395              # flush when getting the group lock
3396         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3397         MULTIPID=$!
3398
3399         # Since just timed wait is not good enough, let's do a sync write
3400         # that way we are sure enough time for a roundtrip + processing
3401         # passed + 2 seconds of extra margin.
3402         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3403         rm $DIR/${tfile}-1
3404         sleep 2
3405
3406         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3407                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3408                 kill -9 $MULTIPID
3409         fi
3410         wait $MULTIPID
3411         local nsz=`stat -c %s $DIR/$tfile`
3412         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3413 }
3414 run_test 34h "ftruncate file under grouplock should not block"
3415
3416 test_35a() {
3417         cp /bin/sh $DIR/f35a
3418         chmod 444 $DIR/f35a
3419         chown $RUNAS_ID $DIR/f35a
3420         $RUNAS $DIR/f35a && error || true
3421         rm $DIR/f35a
3422 }
3423 run_test 35a "exec file with mode 444 (should return and not leak)"
3424
3425 test_36a() {
3426         rm -f $DIR/f36
3427         utime $DIR/f36 || error "utime failed for MDS"
3428 }
3429 run_test 36a "MDS utime check (mknod, utime)"
3430
3431 test_36b() {
3432         echo "" > $DIR/f36
3433         utime $DIR/f36 || error "utime failed for OST"
3434 }
3435 run_test 36b "OST utime check (open, utime)"
3436
3437 test_36c() {
3438         rm -f $DIR/d36/f36
3439         test_mkdir $DIR/d36
3440         chown $RUNAS_ID $DIR/d36
3441         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3442 }
3443 run_test 36c "non-root MDS utime check (mknod, utime)"
3444
3445 test_36d() {
3446         [ ! -d $DIR/d36 ] && test_36c
3447         echo "" > $DIR/d36/f36
3448         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3449 }
3450 run_test 36d "non-root OST utime check (open, utime)"
3451
3452 test_36e() {
3453         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3454
3455         test_mkdir $DIR/$tdir
3456         touch $DIR/$tdir/$tfile
3457         $RUNAS utime $DIR/$tdir/$tfile &&
3458                 error "utime worked, expected failure" || true
3459 }
3460 run_test 36e "utime on non-owned file (should return error)"
3461
3462 subr_36fh() {
3463         local fl="$1"
3464         local LANG_SAVE=$LANG
3465         local LC_LANG_SAVE=$LC_LANG
3466         export LANG=C LC_LANG=C # for date language
3467
3468         DATESTR="Dec 20  2000"
3469         test_mkdir $DIR/$tdir
3470         lctl set_param fail_loc=$fl
3471         date; date +%s
3472         cp /etc/hosts $DIR/$tdir/$tfile
3473         sync & # write RPC generated with "current" inode timestamp, but delayed
3474         sleep 1
3475         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3476         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3477         cancel_lru_locks $OSC
3478         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3479         date; date +%s
3480         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3481                 echo "BEFORE: $LS_BEFORE" && \
3482                 echo "AFTER : $LS_AFTER" && \
3483                 echo "WANT  : $DATESTR" && \
3484                 error "$DIR/$tdir/$tfile timestamps changed" || true
3485
3486         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3487 }
3488
3489 test_36f() {
3490         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3491
3492         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3493         subr_36fh "0x80000214"
3494 }
3495 run_test 36f "utime on file racing with OST BRW write =========="
3496
3497 test_36g() {
3498         remote_ost_nodsh && skip "remote OST with nodsh"
3499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3500         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
3501                 skip "Need MDS version at least 2.12.51"
3502
3503         local fmd_max_age
3504         local fmd
3505         local facet="ost1"
3506         local tgt="obdfilter"
3507
3508         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
3509
3510         test_mkdir $DIR/$tdir
3511         fmd_max_age=$(do_facet $facet \
3512                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
3513                 head -n 1")
3514
3515         echo "FMD max age: ${fmd_max_age}s"
3516         touch $DIR/$tdir/$tfile
3517         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3518                 gawk '{cnt=cnt+$1}  END{print cnt}')
3519         echo "FMD before: $fmd"
3520         [[ $fmd == 0 ]] &&
3521                 error "FMD wasn't create by touch"
3522         sleep $((fmd_max_age + 12))
3523         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
3524                 gawk '{cnt=cnt+$1}  END{print cnt}')
3525         echo "FMD after: $fmd"
3526         [[ $fmd == 0 ]] ||
3527                 error "FMD wasn't expired by ping"
3528 }
3529 run_test 36g "FMD cache expiry ====================="
3530
3531 test_36h() {
3532         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3533
3534         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3535         subr_36fh "0x80000227"
3536 }
3537 run_test 36h "utime on file racing with OST BRW write =========="
3538
3539 test_36i() {
3540         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3541
3542         test_mkdir $DIR/$tdir
3543         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3544
3545         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3546         local new_mtime=$((mtime + 200))
3547
3548         #change Modify time of striped dir
3549         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3550                         error "change mtime failed"
3551
3552         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3553
3554         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3555 }
3556 run_test 36i "change mtime on striped directory"
3557
3558 # test_37 - duplicate with tests 32q 32r
3559
3560 test_38() {
3561         local file=$DIR/$tfile
3562         touch $file
3563         openfile -f O_DIRECTORY $file
3564         local RC=$?
3565         local ENOTDIR=20
3566         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3567         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3568 }
3569 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3570
3571 test_39a() { # was test_39
3572         touch $DIR/$tfile
3573         touch $DIR/${tfile}2
3574 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3575 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3576 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3577         sleep 2
3578         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3579         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3580                 echo "mtime"
3581                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3582                 echo "atime"
3583                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3584                 echo "ctime"
3585                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3586                 error "O_TRUNC didn't change timestamps"
3587         fi
3588 }
3589 run_test 39a "mtime changed on create"
3590
3591 test_39b() {
3592         test_mkdir -c1 $DIR/$tdir
3593         cp -p /etc/passwd $DIR/$tdir/fopen
3594         cp -p /etc/passwd $DIR/$tdir/flink
3595         cp -p /etc/passwd $DIR/$tdir/funlink
3596         cp -p /etc/passwd $DIR/$tdir/frename
3597         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3598
3599         sleep 1
3600         echo "aaaaaa" >> $DIR/$tdir/fopen
3601         echo "aaaaaa" >> $DIR/$tdir/flink
3602         echo "aaaaaa" >> $DIR/$tdir/funlink
3603         echo "aaaaaa" >> $DIR/$tdir/frename
3604
3605         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3606         local link_new=`stat -c %Y $DIR/$tdir/flink`
3607         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3608         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3609
3610         cat $DIR/$tdir/fopen > /dev/null
3611         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3612         rm -f $DIR/$tdir/funlink2
3613         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3614
3615         for (( i=0; i < 2; i++ )) ; do
3616                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3617                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3618                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3619                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3620
3621                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3622                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3623                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3624                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3625
3626                 cancel_lru_locks $OSC
3627                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3628         done
3629 }
3630 run_test 39b "mtime change on open, link, unlink, rename  ======"
3631
3632 # this should be set to past
3633 TEST_39_MTIME=`date -d "1 year ago" +%s`
3634
3635 # bug 11063
3636 test_39c() {
3637         touch $DIR1/$tfile
3638         sleep 2
3639         local mtime0=`stat -c %Y $DIR1/$tfile`
3640
3641         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3642         local mtime1=`stat -c %Y $DIR1/$tfile`
3643         [ "$mtime1" = $TEST_39_MTIME ] || \
3644                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3645
3646         local d1=`date +%s`
3647         echo hello >> $DIR1/$tfile
3648         local d2=`date +%s`
3649         local mtime2=`stat -c %Y $DIR1/$tfile`
3650         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3651                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3652
3653         mv $DIR1/$tfile $DIR1/$tfile-1
3654
3655         for (( i=0; i < 2; i++ )) ; do
3656                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3657                 [ "$mtime2" = "$mtime3" ] || \
3658                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3659
3660                 cancel_lru_locks $OSC
3661                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3662         done
3663 }
3664 run_test 39c "mtime change on rename ==========================="
3665
3666 # bug 21114
3667 test_39d() {
3668         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3669
3670         touch $DIR1/$tfile
3671         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3672
3673         for (( i=0; i < 2; i++ )) ; do
3674                 local mtime=`stat -c %Y $DIR1/$tfile`
3675                 [ $mtime = $TEST_39_MTIME ] || \
3676                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3677
3678                 cancel_lru_locks $OSC
3679                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3680         done
3681 }
3682 run_test 39d "create, utime, stat =============================="
3683
3684 # bug 21114
3685 test_39e() {
3686         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3687
3688         touch $DIR1/$tfile
3689         local mtime1=`stat -c %Y $DIR1/$tfile`
3690
3691         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3692
3693         for (( i=0; i < 2; i++ )) ; do
3694                 local mtime2=`stat -c %Y $DIR1/$tfile`
3695                 [ $mtime2 = $TEST_39_MTIME ] || \
3696                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3697
3698                 cancel_lru_locks $OSC
3699                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3700         done
3701 }
3702 run_test 39e "create, stat, utime, stat ========================"
3703
3704 # bug 21114
3705 test_39f() {
3706         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3707
3708         touch $DIR1/$tfile
3709         mtime1=`stat -c %Y $DIR1/$tfile`
3710
3711         sleep 2
3712         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3713
3714         for (( i=0; i < 2; i++ )) ; do
3715                 local mtime2=`stat -c %Y $DIR1/$tfile`
3716                 [ $mtime2 = $TEST_39_MTIME ] || \
3717                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3718
3719                 cancel_lru_locks $OSC
3720                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3721         done
3722 }
3723 run_test 39f "create, stat, sleep, utime, stat ================="
3724
3725 # bug 11063
3726 test_39g() {
3727         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3728
3729         echo hello >> $DIR1/$tfile
3730         local mtime1=`stat -c %Y $DIR1/$tfile`
3731
3732         sleep 2
3733         chmod o+r $DIR1/$tfile
3734
3735         for (( i=0; i < 2; i++ )) ; do
3736                 local mtime2=`stat -c %Y $DIR1/$tfile`
3737                 [ "$mtime1" = "$mtime2" ] || \
3738                         error "lost mtime: $mtime2, should be $mtime1"
3739
3740                 cancel_lru_locks $OSC
3741                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3742         done
3743 }
3744 run_test 39g "write, chmod, stat ==============================="
3745
3746 # bug 11063
3747 test_39h() {
3748         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3749
3750         touch $DIR1/$tfile
3751         sleep 1
3752
3753         local d1=`date`
3754         echo hello >> $DIR1/$tfile
3755         local mtime1=`stat -c %Y $DIR1/$tfile`
3756
3757         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3758         local d2=`date`
3759         if [ "$d1" != "$d2" ]; then
3760                 echo "write and touch not within one second"
3761         else
3762                 for (( i=0; i < 2; i++ )) ; do
3763                         local mtime2=`stat -c %Y $DIR1/$tfile`
3764                         [ "$mtime2" = $TEST_39_MTIME ] || \
3765                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3766
3767                         cancel_lru_locks $OSC
3768                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3769                 done
3770         fi
3771 }
3772 run_test 39h "write, utime within one second, stat ============="
3773
3774 test_39i() {
3775         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3776
3777         touch $DIR1/$tfile
3778         sleep 1
3779
3780         echo hello >> $DIR1/$tfile
3781         local mtime1=`stat -c %Y $DIR1/$tfile`
3782
3783         mv $DIR1/$tfile $DIR1/$tfile-1
3784
3785         for (( i=0; i < 2; i++ )) ; do
3786                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3787
3788                 [ "$mtime1" = "$mtime2" ] || \
3789                         error "lost mtime: $mtime2, should be $mtime1"
3790
3791                 cancel_lru_locks $OSC
3792                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3793         done
3794 }
3795 run_test 39i "write, rename, stat =============================="
3796
3797 test_39j() {
3798         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3799
3800         start_full_debug_logging
3801         touch $DIR1/$tfile
3802         sleep 1
3803
3804         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3805         lctl set_param fail_loc=0x80000412
3806         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3807                 error "multiop failed"
3808         local multipid=$!
3809         local mtime1=`stat -c %Y $DIR1/$tfile`
3810
3811         mv $DIR1/$tfile $DIR1/$tfile-1
3812
3813         kill -USR1 $multipid
3814         wait $multipid || error "multiop close failed"
3815
3816         for (( i=0; i < 2; i++ )) ; do
3817                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3818                 [ "$mtime1" = "$mtime2" ] ||
3819                         error "mtime is lost on close: $mtime2, " \
3820                               "should be $mtime1"
3821
3822                 cancel_lru_locks $OSC
3823                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3824         done
3825         lctl set_param fail_loc=0
3826         stop_full_debug_logging
3827 }
3828 run_test 39j "write, rename, close, stat ======================="
3829
3830 test_39k() {
3831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3832
3833         touch $DIR1/$tfile
3834         sleep 1
3835
3836         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3837         local multipid=$!
3838         local mtime1=`stat -c %Y $DIR1/$tfile`
3839
3840         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3841
3842         kill -USR1 $multipid
3843         wait $multipid || error "multiop close failed"
3844
3845         for (( i=0; i < 2; i++ )) ; do
3846                 local mtime2=`stat -c %Y $DIR1/$tfile`
3847
3848                 [ "$mtime2" = $TEST_39_MTIME ] || \
3849                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3850
3851                 cancel_lru_locks osc
3852                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3853         done
3854 }
3855 run_test 39k "write, utime, close, stat ========================"
3856
3857 # this should be set to future
3858 TEST_39_ATIME=`date -d "1 year" +%s`
3859
3860 test_39l() {
3861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3862         remote_mds_nodsh && skip "remote MDS with nodsh"
3863
3864         local atime_diff=$(do_facet $SINGLEMDS \
3865                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3866         rm -rf $DIR/$tdir
3867         mkdir -p $DIR/$tdir
3868
3869         # test setting directory atime to future
3870         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3871         local atime=$(stat -c %X $DIR/$tdir)
3872         [ "$atime" = $TEST_39_ATIME ] ||
3873                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3874
3875         # test setting directory atime from future to now
3876         local now=$(date +%s)
3877         touch -a -d @$now $DIR/$tdir
3878
3879         atime=$(stat -c %X $DIR/$tdir)
3880         [ "$atime" -eq "$now"  ] ||
3881                 error "atime is not updated from future: $atime, $now"
3882
3883         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3884         sleep 3
3885
3886         # test setting directory atime when now > dir atime + atime_diff
3887         local d1=$(date +%s)
3888         ls $DIR/$tdir
3889         local d2=$(date +%s)
3890         cancel_lru_locks mdc
3891         atime=$(stat -c %X $DIR/$tdir)
3892         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3893                 error "atime is not updated  : $atime, should be $d2"
3894
3895         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3896         sleep 3
3897
3898         # test not setting directory atime when now < dir atime + atime_diff
3899         ls $DIR/$tdir
3900         cancel_lru_locks mdc
3901         atime=$(stat -c %X $DIR/$tdir)
3902         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3903                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3904
3905         do_facet $SINGLEMDS \
3906                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3907 }
3908 run_test 39l "directory atime update ==========================="
3909
3910 test_39m() {
3911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3912
3913         touch $DIR1/$tfile
3914         sleep 2
3915         local far_past_mtime=$(date -d "May 29 1953" +%s)
3916         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3917
3918         touch -m -d @$far_past_mtime $DIR1/$tfile
3919         touch -a -d @$far_past_atime $DIR1/$tfile
3920
3921         for (( i=0; i < 2; i++ )) ; do
3922                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3923                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3924                         error "atime or mtime set incorrectly"
3925
3926                 cancel_lru_locks $OSC
3927                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3928         done
3929 }
3930 run_test 39m "test atime and mtime before 1970"
3931
3932 test_39n() { # LU-3832
3933         remote_mds_nodsh && skip "remote MDS with nodsh"
3934
3935         local atime_diff=$(do_facet $SINGLEMDS \
3936                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3937         local atime0
3938         local atime1
3939         local atime2
3940
3941         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3942
3943         rm -rf $DIR/$tfile
3944         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3945         atime0=$(stat -c %X $DIR/$tfile)
3946
3947         sleep 5
3948         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3949         atime1=$(stat -c %X $DIR/$tfile)
3950
3951         sleep 5
3952         cancel_lru_locks mdc
3953         cancel_lru_locks osc
3954         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3955         atime2=$(stat -c %X $DIR/$tfile)
3956
3957         do_facet $SINGLEMDS \
3958                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3959
3960         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3961         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3962 }
3963 run_test 39n "check that O_NOATIME is honored"
3964
3965 test_39o() {
3966         TESTDIR=$DIR/$tdir/$tfile
3967         [ -e $TESTDIR ] && rm -rf $TESTDIR
3968         mkdir -p $TESTDIR
3969         cd $TESTDIR
3970         links1=2
3971         ls
3972         mkdir a b
3973         ls
3974         links2=$(stat -c %h .)
3975         [ $(($links1 + 2)) != $links2 ] &&
3976                 error "wrong links count $(($links1 + 2)) != $links2"
3977         rmdir b
3978         links3=$(stat -c %h .)
3979         [ $(($links1 + 1)) != $links3 ] &&
3980                 error "wrong links count $links1 != $links3"
3981         return 0
3982 }
3983 run_test 39o "directory cached attributes updated after create"
3984
3985 test_39p() {
3986         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3987
3988         local MDTIDX=1
3989         TESTDIR=$DIR/$tdir/$tdir
3990         [ -e $TESTDIR ] && rm -rf $TESTDIR
3991         test_mkdir -p $TESTDIR
3992         cd $TESTDIR
3993         links1=2
3994         ls
3995         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3996         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3997         ls
3998         links2=$(stat -c %h .)
3999         [ $(($links1 + 2)) != $links2 ] &&
4000                 error "wrong links count $(($links1 + 2)) != $links2"
4001         rmdir remote_dir2
4002         links3=$(stat -c %h .)
4003         [ $(($links1 + 1)) != $links3 ] &&
4004                 error "wrong links count $links1 != $links3"
4005         return 0
4006 }
4007 run_test 39p "remote directory cached attributes updated after create ========"
4008
4009
4010 test_39q() { # LU-8041
4011         local testdir=$DIR/$tdir
4012         mkdir -p $testdir
4013         multiop_bg_pause $testdir D_c || error "multiop failed"
4014         local multipid=$!
4015         cancel_lru_locks mdc
4016         kill -USR1 $multipid
4017         local atime=$(stat -c %X $testdir)
4018         [ "$atime" -ne 0 ] || error "atime is zero"
4019 }
4020 run_test 39q "close won't zero out atime"
4021
4022 test_40() {
4023         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4024         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4025                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4026         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4027                 error "$tfile is not 4096 bytes in size"
4028 }
4029 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4030
4031 test_41() {
4032         # bug 1553
4033         small_write $DIR/f41 18
4034 }
4035 run_test 41 "test small file write + fstat ====================="
4036
4037 count_ost_writes() {
4038         lctl get_param -n ${OSC}.*.stats |
4039                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4040                         END { printf("%0.0f", writes) }'
4041 }
4042
4043 # decent default
4044 WRITEBACK_SAVE=500
4045 DIRTY_RATIO_SAVE=40
4046 MAX_DIRTY_RATIO=50
4047 BG_DIRTY_RATIO_SAVE=10
4048 MAX_BG_DIRTY_RATIO=25
4049
4050 start_writeback() {
4051         trap 0
4052         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4053         # dirty_ratio, dirty_background_ratio
4054         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4055                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4056                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4057                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4058         else
4059                 # if file not here, we are a 2.4 kernel
4060                 kill -CONT `pidof kupdated`
4061         fi
4062 }
4063
4064 stop_writeback() {
4065         # setup the trap first, so someone cannot exit the test at the
4066         # exact wrong time and mess up a machine
4067         trap start_writeback EXIT
4068         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4069         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4070                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4071                 sysctl -w vm.dirty_writeback_centisecs=0
4072                 sysctl -w vm.dirty_writeback_centisecs=0
4073                 # save and increase /proc/sys/vm/dirty_ratio
4074                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4075                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4076                 # save and increase /proc/sys/vm/dirty_background_ratio
4077                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4078                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4079         else
4080                 # if file not here, we are a 2.4 kernel
4081                 kill -STOP `pidof kupdated`
4082         fi
4083 }
4084
4085 # ensure that all stripes have some grant before we test client-side cache
4086 setup_test42() {
4087         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4088                 dd if=/dev/zero of=$i bs=4k count=1
4089                 rm $i
4090         done
4091 }
4092
4093 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4094 # file truncation, and file removal.
4095 test_42a() {
4096         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4097
4098         setup_test42
4099         cancel_lru_locks $OSC
4100         stop_writeback
4101         sync; sleep 1; sync # just to be safe
4102         BEFOREWRITES=`count_ost_writes`
4103         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4104         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4105         AFTERWRITES=`count_ost_writes`
4106         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4107                 error "$BEFOREWRITES < $AFTERWRITES"
4108         start_writeback
4109 }
4110 run_test 42a "ensure that we don't flush on close"
4111
4112 test_42b() {
4113         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4114
4115         setup_test42
4116         cancel_lru_locks $OSC
4117         stop_writeback
4118         sync
4119         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4120         BEFOREWRITES=$(count_ost_writes)
4121         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4122         AFTERWRITES=$(count_ost_writes)
4123         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4124                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4125         fi
4126         BEFOREWRITES=$(count_ost_writes)
4127         sync || error "sync: $?"
4128         AFTERWRITES=$(count_ost_writes)
4129         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4130                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4131         fi
4132         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4133         start_writeback
4134         return 0
4135 }
4136 run_test 42b "test destroy of file with cached dirty data ======"
4137
4138 # if these tests just want to test the effect of truncation,
4139 # they have to be very careful.  consider:
4140 # - the first open gets a {0,EOF}PR lock
4141 # - the first write conflicts and gets a {0, count-1}PW
4142 # - the rest of the writes are under {count,EOF}PW
4143 # - the open for truncate tries to match a {0,EOF}PR
4144 #   for the filesize and cancels the PWs.
4145 # any number of fixes (don't get {0,EOF} on open, match
4146 # composite locks, do smarter file size management) fix
4147 # this, but for now we want these tests to verify that
4148 # the cancellation with truncate intent works, so we
4149 # start the file with a full-file pw lock to match against
4150 # until the truncate.
4151 trunc_test() {
4152         test=$1
4153         file=$DIR/$test
4154         offset=$2
4155         cancel_lru_locks $OSC
4156         stop_writeback
4157         # prime the file with 0,EOF PW to match
4158         touch $file
4159         $TRUNCATE $file 0
4160         sync; sync
4161         # now the real test..
4162         dd if=/dev/zero of=$file bs=1024 count=100
4163         BEFOREWRITES=`count_ost_writes`
4164         $TRUNCATE $file $offset
4165         cancel_lru_locks $OSC
4166         AFTERWRITES=`count_ost_writes`
4167         start_writeback
4168 }
4169
4170 test_42c() {
4171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4172
4173         trunc_test 42c 1024
4174         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4175                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4176         rm $file
4177 }
4178 run_test 42c "test partial truncate of file with cached dirty data"
4179
4180 test_42d() {
4181         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4182
4183         trunc_test 42d 0
4184         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4185                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4186         rm $file
4187 }
4188 run_test 42d "test complete truncate of file with cached dirty data"
4189
4190 test_42e() { # bug22074
4191         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4192
4193         local TDIR=$DIR/${tdir}e
4194         local pages=16 # hardcoded 16 pages, don't change it.
4195         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4196         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4197         local max_dirty_mb
4198         local warmup_files
4199
4200         test_mkdir $DIR/${tdir}e
4201         $SETSTRIPE -c 1 $TDIR
4202         createmany -o $TDIR/f $files
4203
4204         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4205
4206         # we assume that with $OSTCOUNT files, at least one of them will
4207         # be allocated on OST0.
4208         warmup_files=$((OSTCOUNT * max_dirty_mb))
4209         createmany -o $TDIR/w $warmup_files
4210
4211         # write a large amount of data into one file and sync, to get good
4212         # avail_grant number from OST.
4213         for ((i=0; i<$warmup_files; i++)); do
4214                 idx=$($GETSTRIPE -i $TDIR/w$i)
4215                 [ $idx -ne 0 ] && continue
4216                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4217                 break
4218         done
4219         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4220         sync
4221         $LCTL get_param $proc_osc0/cur_dirty_bytes
4222         $LCTL get_param $proc_osc0/cur_grant_bytes
4223
4224         # create as much dirty pages as we can while not to trigger the actual
4225         # RPCs directly. but depends on the env, VFS may trigger flush during this
4226         # period, hopefully we are good.
4227         for ((i=0; i<$warmup_files; i++)); do
4228                 idx=$($GETSTRIPE -i $TDIR/w$i)
4229                 [ $idx -ne 0 ] && continue
4230                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4231         done
4232         $LCTL get_param $proc_osc0/cur_dirty_bytes
4233         $LCTL get_param $proc_osc0/cur_grant_bytes
4234
4235         # perform the real test
4236         $LCTL set_param $proc_osc0/rpc_stats 0
4237         for ((;i<$files; i++)); do
4238                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
4239                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4240         done
4241         sync
4242         $LCTL get_param $proc_osc0/rpc_stats
4243
4244         local percent=0
4245         local have_ppr=false
4246         $LCTL get_param $proc_osc0/rpc_stats |
4247                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4248                         # skip lines until we are at the RPC histogram data
4249                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4250                         $have_ppr || continue
4251
4252                         # we only want the percent stat for < 16 pages
4253                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4254
4255                         percent=$((percent + WPCT))
4256                         if [[ $percent -gt 15 ]]; then
4257                                 error "less than 16-pages write RPCs" \
4258                                       "$percent% > 15%"
4259                                 break
4260                         fi
4261                 done
4262         rm -rf $TDIR
4263 }
4264 run_test 42e "verify sub-RPC writes are not done synchronously"
4265
4266 test_43A() { # was test_43
4267         test_mkdir $DIR/$tdir
4268         cp -p /bin/ls $DIR/$tdir/$tfile
4269         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4270         pid=$!
4271         # give multiop a chance to open
4272         sleep 1
4273
4274         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4275         kill -USR1 $pid
4276 }
4277 run_test 43A "execution of file opened for write should return -ETXTBSY"
4278
4279 test_43a() {
4280         test_mkdir $DIR/$tdir
4281         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4282         $DIR/$tdir/sleep 60 &
4283         SLEEP_PID=$!
4284         # Make sure exec of $tdir/sleep wins race with truncate
4285         sleep 1
4286         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4287         kill $SLEEP_PID
4288 }
4289 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4290
4291 test_43b() {
4292         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4293
4294         test_mkdir $DIR/$tdir
4295         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4296         $DIR/$tdir/sleep 60 &
4297         SLEEP_PID=$!
4298         # Make sure exec of $tdir/sleep wins race with truncate
4299         sleep 1
4300         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4301         kill $SLEEP_PID
4302 }
4303 run_test 43b "truncate of file being executed should return -ETXTBSY"
4304
4305 test_43c() {
4306         local testdir="$DIR/$tdir"
4307         test_mkdir $testdir
4308         cp $SHELL $testdir/
4309         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4310                 ( cd $testdir && md5sum -c )
4311 }
4312 run_test 43c "md5sum of copy into lustre"
4313
4314 test_44A() { # was test_44
4315         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4316
4317         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4318         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4319 }
4320 run_test 44A "zero length read from a sparse stripe"
4321
4322 test_44a() {
4323         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4324                 awk '{ print $2 }')
4325         [ -z "$nstripe" ] && skip "can't get stripe info"
4326         [[ $nstripe -gt $OSTCOUNT ]] &&
4327                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4328
4329         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4330                 awk '{ print $2 }')
4331         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4332                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4333                         awk '{ print $2 }')
4334         fi
4335
4336         OFFSETS="0 $((stride/2)) $((stride-1))"
4337         for offset in $OFFSETS; do
4338                 for i in $(seq 0 $((nstripe-1))); do
4339                         local GLOBALOFFSETS=""
4340                         # size in Bytes
4341                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4342                         local myfn=$DIR/d44a-$size
4343                         echo "--------writing $myfn at $size"
4344                         ll_sparseness_write $myfn $size ||
4345                                 error "ll_sparseness_write"
4346                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4347                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4348                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4349
4350                         for j in $(seq 0 $((nstripe-1))); do
4351                                 # size in Bytes
4352                                 size=$((((j + $nstripe )*$stride + $offset)))
4353                                 ll_sparseness_write $myfn $size ||
4354                                         error "ll_sparseness_write"
4355                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4356                         done
4357                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4358                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4359                         rm -f $myfn
4360                 done
4361         done
4362 }
4363 run_test 44a "test sparse pwrite ==============================="
4364
4365 dirty_osc_total() {
4366         tot=0
4367         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4368                 tot=$(($tot + $d))
4369         done
4370         echo $tot
4371 }
4372 do_dirty_record() {
4373         before=`dirty_osc_total`
4374         echo executing "\"$*\""
4375         eval $*
4376         after=`dirty_osc_total`
4377         echo before $before, after $after
4378 }
4379 test_45() {
4380         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4381
4382         f="$DIR/f45"
4383         # Obtain grants from OST if it supports it
4384         echo blah > ${f}_grant
4385         stop_writeback
4386         sync
4387         do_dirty_record "echo blah > $f"
4388         [[ $before -eq $after ]] && error "write wasn't cached"
4389         do_dirty_record "> $f"
4390         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4391         do_dirty_record "echo blah > $f"
4392         [[ $before -eq $after ]] && error "write wasn't cached"
4393         do_dirty_record "sync"
4394         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4395         do_dirty_record "echo blah > $f"
4396         [[ $before -eq $after ]] && error "write wasn't cached"
4397         do_dirty_record "cancel_lru_locks osc"
4398         [[ $before -gt $after ]] ||
4399                 error "lock cancellation didn't lower dirty count"
4400         start_writeback
4401 }
4402 run_test 45 "osc io page accounting ============================"
4403
4404 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4405 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4406 # objects offset and an assert hit when an rpc was built with 1023's mapped
4407 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4408 test_46() {
4409         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4410
4411         f="$DIR/f46"
4412         stop_writeback
4413         sync
4414         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4415         sync
4416         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
4417         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4418         sync
4419         start_writeback
4420 }
4421 run_test 46 "dirtying a previously written page ================"
4422
4423 # test_47 is removed "Device nodes check" is moved to test_28
4424
4425 test_48a() { # bug 2399
4426         [ "$mds1_FSTYPE" = "zfs" ] &&
4427         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
4428                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
4429
4430         test_mkdir $DIR/$tdir
4431         cd $DIR/$tdir
4432         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4433         test_mkdir $DIR/$tdir
4434         touch foo || error "'touch foo' failed after recreating cwd"
4435         test_mkdir bar
4436         touch .foo || error "'touch .foo' failed after recreating cwd"
4437         test_mkdir .bar
4438         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4439         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4440         cd . || error "'cd .' failed after recreating cwd"
4441         mkdir . && error "'mkdir .' worked after recreating cwd"
4442         rmdir . && error "'rmdir .' worked after recreating cwd"
4443         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4444         cd .. || error "'cd ..' failed after recreating cwd"
4445 }
4446 run_test 48a "Access renamed working dir (should return errors)="
4447
4448 test_48b() { # bug 2399
4449         rm -rf $DIR/$tdir
4450         test_mkdir $DIR/$tdir
4451         cd $DIR/$tdir
4452         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4453         touch foo && error "'touch foo' worked after removing cwd"
4454         mkdir foo && error "'mkdir foo' worked after removing cwd"
4455         touch .foo && error "'touch .foo' worked after removing cwd"
4456         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4457         ls . > /dev/null && error "'ls .' worked after removing cwd"
4458         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4459         mkdir . && error "'mkdir .' worked after removing cwd"
4460         rmdir . && error "'rmdir .' worked after removing cwd"
4461         ln -s . foo && error "'ln -s .' worked after removing cwd"
4462         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4463 }
4464 run_test 48b "Access removed working dir (should return errors)="
4465
4466 test_48c() { # bug 2350
4467         #lctl set_param debug=-1
4468         #set -vx
4469         rm -rf $DIR/$tdir
4470         test_mkdir -p $DIR/$tdir/dir
4471         cd $DIR/$tdir/dir
4472         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4473         $TRACE touch foo && error "touch foo worked after removing cwd"
4474         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4475         touch .foo && error "touch .foo worked after removing cwd"
4476         mkdir .foo && error "mkdir .foo worked after removing cwd"
4477         $TRACE ls . && error "'ls .' worked after removing cwd"
4478         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4479         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4480         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4481         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4482         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4483 }
4484 run_test 48c "Access removed working subdir (should return errors)"
4485
4486 test_48d() { # bug 2350
4487         #lctl set_param debug=-1
4488         #set -vx
4489         rm -rf $DIR/$tdir
4490         test_mkdir -p $DIR/$tdir/dir
4491         cd $DIR/$tdir/dir
4492         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4493         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4494         $TRACE touch foo && error "'touch foo' worked after removing parent"
4495         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4496         touch .foo && error "'touch .foo' worked after removing parent"
4497         mkdir .foo && error "mkdir .foo worked after removing parent"
4498         $TRACE ls . && error "'ls .' worked after removing parent"
4499         $TRACE ls .. && error "'ls ..' worked after removing parent"
4500         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4501         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4502         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4503         true
4504 }
4505 run_test 48d "Access removed parent subdir (should return errors)"
4506
4507 test_48e() { # bug 4134
4508         #lctl set_param debug=-1
4509         #set -vx
4510         rm -rf $DIR/$tdir
4511         test_mkdir -p $DIR/$tdir/dir
4512         cd $DIR/$tdir/dir
4513         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4514         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4515         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4516         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4517         # On a buggy kernel addition of "touch foo" after cd .. will
4518         # produce kernel oops in lookup_hash_it
4519         touch ../foo && error "'cd ..' worked after recreate parent"
4520         cd $DIR
4521         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4522 }
4523 run_test 48e "Access to recreated parent subdir (should return errors)"
4524
4525 test_49() { # LU-1030
4526         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4527         remote_ost_nodsh && skip "remote OST with nodsh"
4528
4529         # get ost1 size - lustre-OST0000
4530         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4531                 awk '{ print $4 }')
4532         # write 800M at maximum
4533         [[ $ost1_size -lt 2 ]] && ost1_size=2
4534         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4535
4536         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4537         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4538         local dd_pid=$!
4539
4540         # change max_pages_per_rpc while writing the file
4541         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4542         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4543         # loop until dd process exits
4544         while ps ax -opid | grep -wq $dd_pid; do
4545                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4546                 sleep $((RANDOM % 5 + 1))
4547         done
4548         # restore original max_pages_per_rpc
4549         $LCTL set_param $osc1_mppc=$orig_mppc
4550         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4551 }
4552 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4553
4554 test_50() {
4555         # bug 1485
4556         test_mkdir $DIR/$tdir
4557         cd $DIR/$tdir
4558         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4559 }
4560 run_test 50 "special situations: /proc symlinks  ==============="
4561
4562 test_51a() {    # was test_51
4563         # bug 1516 - create an empty entry right after ".." then split dir
4564         test_mkdir -c1 $DIR/$tdir
4565         touch $DIR/$tdir/foo
4566         $MCREATE $DIR/$tdir/bar
4567         rm $DIR/$tdir/foo
4568         createmany -m $DIR/$tdir/longfile 201
4569         FNUM=202
4570         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4571                 $MCREATE $DIR/$tdir/longfile$FNUM
4572                 FNUM=$(($FNUM + 1))
4573                 echo -n "+"
4574         done
4575         echo
4576         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4577 }
4578 run_test 51a "special situations: split htree with empty entry =="
4579
4580 cleanup_print_lfs_df () {
4581         trap 0
4582         $LFS df
4583         $LFS df -i
4584 }
4585
4586 test_51b() {
4587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4588
4589         local dir=$DIR/$tdir
4590         local nrdirs=$((65536 + 100))
4591
4592         # cleanup the directory
4593         rm -fr $dir
4594
4595         test_mkdir -c1 $dir
4596
4597         $LFS df
4598         $LFS df -i
4599         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
4600         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4601         [[ $numfree -lt $nrdirs ]] &&
4602                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
4603
4604         # need to check free space for the directories as well
4605         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4606         numfree=$(( blkfree / $(fs_inode_ksize) ))
4607         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
4608
4609         trap cleanup_print_lfs_df EXIT
4610
4611         # create files
4612         createmany -d $dir/d $nrdirs || {
4613                 unlinkmany $dir/d $nrdirs
4614                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4615         }
4616
4617         # really created :
4618         nrdirs=$(ls -U $dir | wc -l)
4619
4620         # unlink all but 100 subdirectories, then check it still works
4621         local left=100
4622         local delete=$((nrdirs - left))
4623
4624         $LFS df
4625         $LFS df -i
4626
4627         # for ldiskfs the nlink count should be 1, but this is OSD specific
4628         # and so this is listed for informational purposes only
4629         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4630         unlinkmany -d $dir/d $delete ||
4631                 error "unlink of first $delete subdirs failed"
4632
4633         echo "nlink between: $(stat -c %h $dir)"
4634         local found=$(ls -U $dir | wc -l)
4635         [ $found -ne $left ] &&
4636                 error "can't find subdirs: found only $found, expected $left"
4637
4638         unlinkmany -d $dir/d $delete $left ||
4639                 error "unlink of second $left subdirs failed"
4640         # regardless of whether the backing filesystem tracks nlink accurately
4641         # or not, the nlink count shouldn't be more than "." and ".." here
4642         local after=$(stat -c %h $dir)
4643         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4644                 echo "nlink after: $after"
4645
4646         cleanup_print_lfs_df
4647 }
4648 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4649
4650 test_51d() {
4651         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4652         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
4653
4654         test_mkdir $DIR/$tdir
4655         createmany -o $DIR/$tdir/t- 1000
4656         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4657         for N in $(seq 0 $((OSTCOUNT - 1))); do
4658                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4659                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4660                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4661                         '($1 == '$N') { objs += 1 } \
4662                         END { printf("%0.0f", objs) }')
4663                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4664         done
4665         unlinkmany $DIR/$tdir/t- 1000
4666
4667         NLAST=0
4668         for N in $(seq 1 $((OSTCOUNT - 1))); do
4669                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4670                         error "OST $N has less objects vs OST $NLAST" \
4671                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4672                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4673                         error "OST $N has less objects vs OST $NLAST" \
4674                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4675
4676                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4677                         error "OST $N has less #0 objects vs OST $NLAST" \
4678                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4679                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4680                         error "OST $N has less #0 objects vs OST $NLAST" \
4681                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4682                 NLAST=$N
4683         done
4684         rm -f $TMP/$tfile
4685 }
4686 run_test 51d "check object distribution"
4687
4688 test_51e() {
4689         if [ "$mds1_FSTYPE" != ldiskfs ]; then
4690                 skip_env "ldiskfs only test"
4691         fi
4692
4693         test_mkdir -c1 $DIR/$tdir
4694         test_mkdir -c1 $DIR/$tdir/d0
4695
4696         touch $DIR/$tdir/d0/foo
4697         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4698                 error "file exceed 65000 nlink limit!"
4699         unlinkmany $DIR/$tdir/d0/f- 65001
4700         return 0
4701 }
4702 run_test 51e "check file nlink limit"
4703
4704 test_51f() {
4705         test_mkdir $DIR/$tdir
4706
4707         local max=100000
4708         local ulimit_old=$(ulimit -n)
4709         local spare=20 # number of spare fd's for scripts/libraries, etc.
4710         local mdt=$($LFS getstripe -m $DIR/$tdir)
4711         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4712
4713         echo "MDT$mdt numfree=$numfree, max=$max"
4714         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4715         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4716                 while ! ulimit -n $((numfree + spare)); do
4717                         numfree=$((numfree * 3 / 4))
4718                 done
4719                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4720         else
4721                 echo "left ulimit at $ulimit_old"
4722         fi
4723
4724         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4725                 unlinkmany $DIR/$tdir/f $numfree
4726                 error "create+open $numfree files in $DIR/$tdir failed"
4727         }
4728         ulimit -n $ulimit_old
4729
4730         # if createmany exits at 120s there will be fewer than $numfree files
4731         unlinkmany $DIR/$tdir/f $numfree || true
4732 }
4733 run_test 51f "check many open files limit"
4734
4735 test_52a() {
4736         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4737         test_mkdir $DIR/$tdir
4738         touch $DIR/$tdir/foo
4739         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4740         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4741         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4742         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4743         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4744                                         error "link worked"
4745         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4746         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4747         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4748                                                      error "lsattr"
4749         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4750         cp -r $DIR/$tdir $TMP/
4751         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4752 }
4753 run_test 52a "append-only flag test (should return errors)"
4754
4755 test_52b() {
4756         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4757         test_mkdir $DIR/$tdir
4758         touch $DIR/$tdir/foo
4759         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4760         cat test > $DIR/$tdir/foo && error "cat test worked"
4761         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4762         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4763         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4764                                         error "link worked"
4765         echo foo >> $DIR/$tdir/foo && error "echo worked"
4766         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4767         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4768         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4769         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4770                                                         error "lsattr"
4771         chattr -i $DIR/$tdir/foo || error "chattr failed"
4772
4773         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4774 }
4775 run_test 52b "immutable flag test (should return errors) ======="
4776
4777 test_53() {
4778         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4779         remote_mds_nodsh && skip "remote MDS with nodsh"
4780         remote_ost_nodsh && skip "remote OST with nodsh"
4781
4782         local param
4783         local param_seq
4784         local ostname
4785         local mds_last
4786         local mds_last_seq
4787         local ost_last
4788         local ost_last_seq
4789         local ost_last_id
4790         local ostnum
4791         local node
4792         local found=false
4793         local support_last_seq=true
4794
4795         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
4796                 support_last_seq=false
4797
4798         # only test MDT0000
4799         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4800         local value
4801         for value in $(do_facet $SINGLEMDS \
4802                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
4803                 param=$(echo ${value[0]} | cut -d "=" -f1)
4804                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4805
4806                 if $support_last_seq; then
4807                         param_seq=$(echo $param |
4808                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4809                         mds_last_seq=$(do_facet $SINGLEMDS \
4810                                        $LCTL get_param -n $param_seq)
4811                 fi
4812                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4813
4814                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4815                 node=$(facet_active_host ost$((ostnum+1)))
4816                 param="obdfilter.$ostname.last_id"
4817                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4818                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4819                         ost_last_id=$ost_last
4820
4821                         if $support_last_seq; then
4822                                 ost_last_id=$(echo $ost_last |
4823                                               awk -F':' '{print $2}' |
4824                                               sed -e "s/^0x//g")
4825                                 ost_last_seq=$(echo $ost_last |
4826                                                awk -F':' '{print $1}')
4827                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4828                         fi
4829
4830                         if [[ $ost_last_id != $mds_last ]]; then
4831                                 error "$ost_last_id != $mds_last"
4832                         else
4833                                 found=true
4834                                 break
4835                         fi
4836                 done
4837         done
4838         $found || error "can not match last_seq/last_id for $mdtosc"
4839         return 0
4840 }
4841 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4842
4843 test_54a() {
4844         perl -MSocket -e ';' || skip "no Socket perl module installed"
4845
4846         $SOCKETSERVER $DIR/socket ||
4847                 error "$SOCKETSERVER $DIR/socket failed: $?"
4848         $SOCKETCLIENT $DIR/socket ||
4849                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4850         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4851 }
4852 run_test 54a "unix domain socket test =========================="
4853
4854 test_54b() {
4855         f="$DIR/f54b"
4856         mknod $f c 1 3
4857         chmod 0666 $f
4858         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
4859 }
4860 run_test 54b "char device works in lustre ======================"
4861
4862 find_loop_dev() {
4863         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4864         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4865         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4866
4867         for i in $(seq 3 7); do
4868                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4869                 LOOPDEV=$LOOPBASE$i
4870                 LOOPNUM=$i
4871                 break
4872         done
4873 }
4874
4875 cleanup_54c() {
4876         local rc=0
4877         loopdev="$DIR/loop54c"
4878
4879         trap 0
4880         $UMOUNT $DIR/$tdir || rc=$?
4881         losetup -d $loopdev || true
4882         losetup -d $LOOPDEV || true
4883         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4884         return $rc
4885 }
4886
4887 test_54c() {
4888         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4889
4890         loopdev="$DIR/loop54c"
4891
4892         find_loop_dev
4893         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
4894         trap cleanup_54c EXIT
4895         mknod $loopdev b 7 $LOOPNUM
4896         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4897         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
4898         losetup $loopdev $DIR/$tfile ||
4899                 error "can't set up $loopdev for $DIR/$tfile"
4900         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4901         test_mkdir $DIR/$tdir
4902         mount -t ext2 $loopdev $DIR/$tdir ||
4903                 error "error mounting $loopdev on $DIR/$tdir"
4904         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
4905                 error "dd write"
4906         df $DIR/$tdir
4907         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
4908                 error "dd read"
4909         cleanup_54c
4910 }
4911 run_test 54c "block device works in lustre ====================="
4912
4913 test_54d() {
4914         f="$DIR/f54d"
4915         string="aaaaaa"
4916         mknod $f p
4917         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4918 }
4919 run_test 54d "fifo device works in lustre ======================"
4920
4921 test_54e() {
4922         f="$DIR/f54e"
4923         string="aaaaaa"
4924         cp -aL /dev/console $f
4925         echo $string > $f || error "echo $string to $f failed"
4926 }
4927 run_test 54e "console/tty device works in lustre ======================"
4928
4929 test_56a() {
4930         local numfiles=3
4931         local dir=$DIR/$tdir
4932
4933         rm -rf $dir
4934         test_mkdir -p $dir/dir
4935         for i in $(seq $numfiles); do
4936                 touch $dir/file$i
4937                 touch $dir/dir/file$i
4938         done
4939
4940         local numcomp=$($LFS getstripe --component-count $dir)
4941
4942         [[ $numcomp == 0 ]] && numcomp=1
4943
4944         # test lfs getstripe with --recursive
4945         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
4946
4947         [[ $filenum -eq $((numfiles * 2)) ]] ||
4948                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
4949         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
4950         [[ $filenum -eq $numfiles ]] ||
4951                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
4952         echo "$LFS getstripe showed obdidx or l_ost_idx"
4953
4954         # test lfs getstripe with file instead of dir
4955         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
4956         [[ $filenum -eq 1 ]] ||
4957                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
4958         echo "$LFS getstripe file1 passed"
4959
4960         #test lfs getstripe with --verbose
4961         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
4962         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4963                 error "$LFS getstripe --verbose $dir: "\
4964                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
4965         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
4966                 error "$LFS getstripe $dir: showed lmm_magic"
4967
4968         #test lfs getstripe with -v prints lmm_fid
4969         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
4970         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4971                 error "$LFS getstripe -v $dir: "\
4972                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
4973         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
4974                 error "$LFS getstripe $dir: showed lmm_fid by default"
4975         echo "$LFS getstripe --verbose passed"
4976
4977         #check for FID information
4978         local fid1=$($LFS getstripe --fid $dir/file1)
4979         local fid2=$($LFS getstripe --verbose $dir/file1 |
4980                      awk '/lmm_fid: / { print $2; exit; }')
4981         local fid3=$($LFS path2fid $dir/file1)
4982
4983         [ "$fid1" != "$fid2" ] &&
4984                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
4985         [ "$fid1" != "$fid3" ] &&
4986                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
4987         echo "$LFS getstripe --fid passed"
4988
4989         #test lfs getstripe with --obd
4990         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
4991                 error "$LFS getstripe --obd wrong_uuid: should return error"
4992
4993         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4994
4995         local ostidx=1
4996         local obduuid=$(ostuuid_from_index $ostidx)
4997         local found=$($LFS getstripe -r --obd $obduuid $dir |
4998                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
4999
5000         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5001         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5002                 ((filenum--))
5003         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5004                 ((filenum--))
5005
5006         [[ $found -eq $filenum ]] ||
5007                 error "$LFS getstripe --obd: found $found expect $filenum"
5008         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5009                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5010                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5011                 error "$LFS getstripe --obd: should not show file on other obd"
5012         echo "$LFS getstripe --obd passed"
5013 }
5014 run_test 56a "check $LFS getstripe"
5015
5016 test_56b() {
5017         local dir=$DIR/$tdir
5018         local numdirs=3
5019
5020         test_mkdir $dir
5021         for i in $(seq $numdirs); do
5022                 test_mkdir $dir/dir$i
5023         done
5024
5025         # test lfs getdirstripe default mode is non-recursion, which is
5026         # different from lfs getstripe
5027         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5028
5029         [[ $dircnt -eq 1 ]] ||
5030                 error "$LFS getdirstripe: found $dircnt, not 1"
5031         dircnt=$($LFS getdirstripe --recursive $dir |
5032                 grep -c lmv_stripe_count)
5033         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5034                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5035 }
5036 run_test 56b "check $LFS getdirstripe"
5037
5038 test_56c() {
5039         remote_ost_nodsh && skip "remote OST with nodsh"
5040
5041         local ost_idx=0
5042         local ost_name=$(ostname_from_index $ost_idx)
5043         local old_status=$(ost_dev_status $ost_idx)
5044
5045         [[ -z "$old_status" ]] ||
5046                 skip_env "OST $ost_name is in $old_status status"
5047
5048         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5049         [[ $OST1_VERSION -ge $(version_code 2.12.3) ]] && do_facet ost1 \
5050                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5051         sleep_maxage
5052
5053         local new_status=$(ost_dev_status $ost_idx)
5054
5055         [[ "$new_status" =~ "D" ]] ||
5056                 error "$ost_name status is '$new_status', missing 'D'"
5057         if [[ $OST1_VERSION -ge $(version_code 2.12.3) ]]; then
5058                 [[ "$new_status" =~ "N" ]] ||
5059                         error "$ost_name status is '$new_status', missing 'N'"
5060         fi
5061
5062         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5063         [[ $OST1_VERSION -ge $(version_code 2.12.3) ]] && do_facet ost1 \
5064                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5065         sleep_maxage
5066
5067         new_status=$(ost_dev_status $ost_idx)
5068         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5069                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5070 }
5071 run_test 56c "check 'lfs df' showing device status"
5072
5073 NUMFILES=3
5074 NUMDIRS=3
5075 setup_56() {
5076         local local_tdir="$1"
5077         local local_numfiles="$2"
5078         local local_numdirs="$3"
5079         local dir_params="$4"
5080         local dir_stripe_params="$5"
5081
5082         if [ ! -d "$local_tdir" ] ; then
5083                 test_mkdir -p $dir_stripe_params $local_tdir
5084                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5085                 for i in $(seq $local_numfiles) ; do
5086                         touch $local_tdir/file$i
5087                 done
5088                 for i in $(seq $local_numdirs) ; do
5089                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5090                         for j in $(seq $local_numfiles) ; do
5091                                 touch $local_tdir/dir$i/file$j
5092                         done
5093                 done
5094         fi
5095 }
5096
5097 setup_56_special() {
5098         local local_tdir=$1
5099         local local_numfiles=$2
5100         local local_numdirs=$3
5101
5102         setup_56 $local_tdir $local_numfiles $local_numdirs
5103
5104         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5105                 for i in $(seq $local_numfiles) ; do
5106                         mknod $local_tdir/loop${i}b b 7 $i
5107                         mknod $local_tdir/null${i}c c 1 3
5108                         ln -s $local_tdir/file1 $local_tdir/link${i}
5109                 done
5110                 for i in $(seq $local_numdirs) ; do
5111                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5112                         mknod $local_tdir/dir$i/null${i}c c 1 3
5113                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5114                 done
5115         fi
5116 }
5117
5118 test_56g() {
5119         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5120         local expected=$(($NUMDIRS + 2))
5121
5122         setup_56 $dir $NUMFILES $NUMDIRS
5123
5124         # test lfs find with -name
5125         for i in $(seq $NUMFILES) ; do
5126                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5127
5128                 [ $nums -eq $expected ] ||
5129                         error "lfs find -name '*$i' $dir wrong: "\
5130                               "found $nums, expected $expected"
5131         done
5132 }
5133 run_test 56g "check lfs find -name"
5134
5135 test_56h() {
5136         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5137         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5138
5139         setup_56 $dir $NUMFILES $NUMDIRS
5140
5141         # test lfs find with ! -name
5142         for i in $(seq $NUMFILES) ; do
5143                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5144
5145                 [ $nums -eq $expected ] ||
5146                         error "lfs find ! -name '*$i' $dir wrong: "\
5147                               "found $nums, expected $expected"
5148         done
5149 }
5150 run_test 56h "check lfs find ! -name"
5151
5152 test_56i() {
5153         local dir=$DIR/$tdir
5154
5155         test_mkdir $dir
5156
5157         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5158         local out=$($cmd)
5159
5160         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5161 }
5162 run_test 56i "check 'lfs find -ost UUID' skips directories"
5163
5164 test_56j() {
5165         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5166
5167         setup_56_special $dir $NUMFILES $NUMDIRS
5168
5169         local expected=$((NUMDIRS + 1))
5170         local cmd="$LFS find -type d $dir"
5171         local nums=$($cmd | wc -l)
5172
5173         [ $nums -eq $expected ] ||
5174                 error "'$cmd' wrong: found $nums, expected $expected"
5175 }
5176 run_test 56j "check lfs find -type d"
5177
5178 test_56k() {
5179         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5180
5181         setup_56_special $dir $NUMFILES $NUMDIRS
5182
5183         local expected=$(((NUMDIRS + 1) * NUMFILES))
5184         local cmd="$LFS find -type f $dir"
5185         local nums=$($cmd | wc -l)
5186
5187         [ $nums -eq $expected ] ||
5188                 error "'$cmd' wrong: found $nums, expected $expected"
5189 }
5190 run_test 56k "check lfs find -type f"
5191
5192 test_56l() {
5193         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5194
5195         setup_56_special $dir $NUMFILES $NUMDIRS
5196
5197         local expected=$((NUMDIRS + NUMFILES))
5198         local cmd="$LFS find -type b $dir"
5199         local nums=$($cmd | wc -l)
5200
5201         [ $nums -eq $expected ] ||
5202                 error "'$cmd' wrong: found $nums, expected $expected"
5203 }
5204 run_test 56l "check lfs find -type b"
5205
5206 test_56m() {
5207         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5208
5209         setup_56_special $dir $NUMFILES $NUMDIRS
5210
5211         local expected=$((NUMDIRS + NUMFILES))
5212         local cmd="$LFS find -type c $dir"
5213         local nums=$($cmd | wc -l)
5214         [ $nums -eq $expected ] ||
5215                 error "'$cmd' wrong: found $nums, expected $expected"
5216 }
5217 run_test 56m "check lfs find -type c"
5218
5219 test_56n() {
5220         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5221         setup_56_special $dir $NUMFILES $NUMDIRS
5222
5223         local expected=$((NUMDIRS + NUMFILES))
5224         local cmd="$LFS find -type l $dir"
5225         local nums=$($cmd | wc -l)
5226
5227         [ $nums -eq $expected ] ||
5228                 error "'$cmd' wrong: found $nums, expected $expected"
5229 }
5230 run_test 56n "check lfs find -type l"
5231
5232 test_56o() {
5233         local dir=$DIR/$tdir
5234
5235         setup_56 $dir $NUMFILES $NUMDIRS
5236         utime $dir/file1 > /dev/null || error "utime (1)"
5237         utime $dir/file2 > /dev/null || error "utime (2)"
5238         utime $dir/dir1 > /dev/null || error "utime (3)"
5239         utime $dir/dir2 > /dev/null || error "utime (4)"
5240         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5241         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5242
5243         local expected=4
5244         local nums=$($LFS find -mtime +0 $dir | wc -l)
5245
5246         [ $nums -eq $expected ] ||
5247                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5248
5249         expected=12
5250         cmd="$LFS find -mtime 0 $dir"
5251         nums=$($cmd | wc -l)
5252         [ $nums -eq $expected ] ||
5253                 error "'$cmd' wrong: found $nums, expected $expected"
5254 }
5255 run_test 56o "check lfs find -mtime for old files"
5256
5257 test_56p() {
5258         [ $RUNAS_ID -eq $UID ] &&
5259                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5260
5261         local dir=$DIR/$tdir
5262
5263         setup_56 $dir $NUMFILES $NUMDIRS
5264         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
5265
5266         local expected=$NUMFILES
5267         local cmd="$LFS find -uid $RUNAS_ID $dir"
5268         local nums=$($cmd | wc -l)
5269
5270         [ $nums -eq $expected ] ||
5271                 error "'$cmd' wrong: found $nums, expected $expected"
5272
5273         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
5274         cmd="$LFS find ! -uid $RUNAS_ID $dir"
5275         nums=$($cmd | wc -l)
5276         [ $nums -eq $expected ] ||
5277                 error "'$cmd' wrong: found $nums, expected $expected"
5278 }
5279 run_test 56p "check lfs find -uid and ! -uid"
5280
5281 test_56q() {
5282         [ $RUNAS_ID -eq $UID ] &&
5283                 skip_env "RUNAS_ID = UID = $UID -- skipping"
5284
5285         local dir=$DIR/$tdir
5286
5287         setup_56 $dir $NUMFILES $NUMDIRS
5288         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
5289
5290         local expected=$NUMFILES
5291         local cmd="$LFS find -gid $RUNAS_GID $dir"
5292         local nums=$($cmd | wc -l)
5293
5294         [ $nums -eq $expected ] ||
5295                 error "'$cmd' wrong: found $nums, expected $expected"
5296
5297         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
5298         cmd="$LFS find ! -gid $RUNAS_GID $dir"
5299         nums=$($cmd | wc -l)
5300         [ $nums -eq $expected ] ||
5301                 error "'$cmd' wrong: found $nums, expected $expected"
5302 }
5303 run_test 56q "check lfs find -gid and ! -gid"
5304
5305 test_56r() {
5306         local dir=$DIR/$tdir
5307
5308         setup_56 $dir $NUMFILES $NUMDIRS
5309
5310         local expected=12
5311         local cmd="$LFS find -size 0 -type f -lazy $dir"
5312         local nums=$($cmd | wc -l)
5313
5314         [ $nums -eq $expected ] ||
5315                 error "'$cmd' wrong: found $nums, expected $expected"
5316         cmd="$LFS find -size 0 -type f $dir"
5317         nums=$($cmd | wc -l)
5318         [ $nums -eq $expected ] ||
5319                 error "'$cmd' wrong: found $nums, expected $expected"
5320
5321         expected=0
5322         cmd="$LFS find ! -size 0 -type f -lazy $dir"
5323         nums=$($cmd | wc -l)
5324         [ $nums -eq $expected ] ||
5325                 error "'$cmd' wrong: found $nums, expected $expected"
5326         cmd="$LFS find ! -size 0 -type f $dir"
5327         nums=$($cmd | wc -l)
5328         [ $nums -eq $expected ] ||
5329                 error "'$cmd' wrong: found $nums, expected $expected"
5330
5331         echo "test" > $dir/$tfile
5332         echo "test2" > $dir/$tfile.2 && sync
5333         expected=1
5334         cmd="$LFS find -size 5 -type f -lazy $dir"
5335         nums=$($cmd | wc -l)
5336         [ $nums -eq $expected ] ||
5337                 error "'$cmd' wrong: found $nums, expected $expected"
5338         cmd="$LFS find -size 5 -type f $dir"
5339         nums=$($cmd | wc -l)
5340         [ $nums -eq $expected ] ||
5341                 error "'$cmd' wrong: found $nums, expected $expected"
5342
5343         expected=1
5344         cmd="$LFS find -size +5 -type f -lazy $dir"
5345         nums=$($cmd | wc -l)
5346         [ $nums -eq $expected ] ||
5347                 error "'$cmd' wrong: found $nums, expected $expected"
5348         cmd="$LFS find -size +5 -type f $dir"
5349         nums=$($cmd | wc -l)
5350         [ $nums -eq $expected ] ||
5351                 error "'$cmd' wrong: found $nums, expected $expected"
5352
5353         expected=2
5354         cmd="$LFS find -size +0 -type f -lazy $dir"
5355         nums=$($cmd | wc -l)
5356         [ $nums -eq $expected ] ||
5357                 error "'$cmd' wrong: found $nums, expected $expected"
5358         cmd="$LFS find -size +0 -type f $dir"
5359         nums=$($cmd | wc -l)
5360         [ $nums -eq $expected ] ||
5361                 error "'$cmd' wrong: found $nums, expected $expected"
5362
5363         expected=2
5364         cmd="$LFS find ! -size -5 -type f -lazy $dir"
5365         nums=$($cmd | wc -l)
5366         [ $nums -eq $expected ] ||
5367                 error "'$cmd' wrong: found $nums, expected $expected"
5368         cmd="$LFS find ! -size -5 -type f $dir"
5369         nums=$($cmd | wc -l)
5370         [ $nums -eq $expected ] ||
5371                 error "'$cmd' wrong: found $nums, expected $expected"
5372
5373         expected=12
5374         cmd="$LFS find -size -5 -type f -lazy $dir"
5375         nums=$($cmd | wc -l)
5376         [ $nums -eq $expected ] ||
5377                 error "'$cmd' wrong: found $nums, expected $expected"
5378         cmd="$LFS find -size -5 -type f $dir"
5379         nums=$($cmd | wc -l)
5380         [ $nums -eq $expected ] ||
5381                 error "'$cmd' wrong: found $nums, expected $expected"
5382 }
5383 run_test 56r "check lfs find -size works"
5384
5385 test_56ra_sub() {
5386         local expected=$1
5387         local glimpses=$2
5388         local cmd="$3"
5389
5390         cancel_lru_locks $OSC
5391
5392         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5393         local nums=$($cmd | wc -l)
5394
5395         [ $nums -eq $expected ] ||
5396                 error "'$cmd' wrong: found $nums, expected $expected"
5397
5398         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
5399
5400         if (( rpcs_before + glimpses != rpcs_after )); then
5401                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
5402                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
5403
5404                 if [[ $glimpses == 0 ]]; then
5405                         error "'$cmd' should not send glimpse RPCs to OST"
5406                 else
5407                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
5408                 fi
5409         fi
5410 }
5411
5412 test_56ra() {
5413         [[ $MDS1_VERSION -ge $(version_code 2.12.4) ]] ||
5414                 skip "MDS < 2.12.4 doesn't return LSOM data"
5415         local dir=$DIR/$tdir
5416
5417         [[ $OSC == "mdc" ]] && skip "DoM files"
5418
5419         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5420         # open and close all files to ensure LSOM is updated
5421         cancel_lru_locks $OSC
5422         find $dir -type f | xargs cat > /dev/null
5423
5424         #   expect_found  glimpse_rpcs  command_to_run
5425         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
5426         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
5427         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
5428         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
5429
5430         echo "test" > $dir/$tfile
5431         echo "test2" > $dir/$tfile.2 && sync
5432         cancel_lru_locks $OSC
5433         cat $dir/$tfile $dir/$tfile.2 > /dev/null
5434
5435         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
5436         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
5437         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
5438         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
5439
5440         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
5441         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
5442         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
5443         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
5444         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
5445         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
5446 }
5447 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
5448
5449 test_56s() { # LU-611 #LU-9369
5450         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
5451
5452         local dir=$DIR/$tdir
5453         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
5454
5455         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
5456         for i in $(seq $NUMDIRS); do
5457                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
5458         done
5459
5460         local expected=$NUMDIRS
5461         local cmd="$LFS find -c $OSTCOUNT $dir"
5462         local nums=$($cmd | wc -l)
5463
5464         [ $nums -eq $expected ] || {
5465                 $LFS getstripe -R $dir
5466                 error "'$cmd' wrong: found $nums, expected $expected"
5467         }
5468
5469         expected=$((NUMDIRS + onestripe))
5470         cmd="$LFS find -stripe-count +0 -type f $dir"
5471         nums=$($cmd | wc -l)
5472         [ $nums -eq $expected ] || {
5473                 $LFS getstripe -R $dir
5474                 error "'$cmd' wrong: found $nums, expected $expected"
5475         }
5476
5477         expected=$onestripe
5478         cmd="$LFS find -stripe-count 1 -type f $dir"
5479         nums=$($cmd | wc -l)
5480         [ $nums -eq $expected ] || {
5481                 $LFS getstripe -R $dir
5482                 error "'$cmd' wrong: found $nums, expected $expected"
5483         }
5484
5485         cmd="$LFS find -stripe-count -2 -type f $dir"
5486         nums=$($cmd | wc -l)
5487         [ $nums -eq $expected ] || {
5488                 $LFS getstripe -R $dir
5489                 error "'$cmd' wrong: found $nums, expected $expected"
5490         }
5491
5492         expected=0
5493         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
5494         nums=$($cmd | wc -l)
5495         [ $nums -eq $expected ] || {
5496                 $LFS getstripe -R $dir
5497                 error "'$cmd' wrong: found $nums, expected $expected"
5498         }
5499 }
5500 run_test 56s "check lfs find -stripe-count works"
5501
5502 test_56t() { # LU-611 #LU-9369
5503         local dir=$DIR/$tdir
5504
5505         setup_56 $dir 0 $NUMDIRS
5506         for i in $(seq $NUMDIRS); do
5507                 $LFS setstripe -S 8M $dir/dir$i/$tfile
5508         done
5509
5510         local expected=$NUMDIRS
5511         local cmd="$LFS find -S 8M $dir"
5512         local nums=$($cmd | wc -l)
5513
5514         [ $nums -eq $expected ] || {
5515                 $LFS getstripe -R $dir
5516                 error "'$cmd' wrong: found $nums, expected $expected"
5517         }
5518         rm -rf $dir
5519
5520         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
5521
5522         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
5523
5524         expected=$(((NUMDIRS + 1) * NUMFILES))
5525         cmd="$LFS find -stripe-size 512k -type f $dir"
5526         nums=$($cmd | wc -l)
5527         [ $nums -eq $expected ] ||
5528                 error "'$cmd' wrong: found $nums, expected $expected"
5529
5530         cmd="$LFS find -stripe-size +320k -type f $dir"
5531         nums=$($cmd | wc -l)
5532         [ $nums -eq $expected ] ||
5533                 error "'$cmd' wrong: found $nums, expected $expected"
5534
5535         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
5536         cmd="$LFS find -stripe-size +200k -type f $dir"
5537         nums=$($cmd | wc -l)
5538         [ $nums -eq $expected ] ||
5539                 error "'$cmd' wrong: found $nums, expected $expected"
5540
5541         cmd="$LFS find -stripe-size -640k -type f $dir"
5542         nums=$($cmd | wc -l)
5543         [ $nums -eq $expected ] ||
5544                 error "'$cmd' wrong: found $nums, expected $expected"
5545
5546         expected=4
5547         cmd="$LFS find -stripe-size 256k -type f $dir"
5548         nums=$($cmd | wc -l)
5549         [ $nums -eq $expected ] ||
5550                 error "'$cmd' wrong: found $nums, expected $expected"
5551
5552         cmd="$LFS find -stripe-size -320k -type f $dir"
5553         nums=$($cmd | wc -l)
5554         [ $nums -eq $expected ] ||
5555                 error "'$cmd' wrong: found $nums, expected $expected"
5556
5557         expected=0
5558         cmd="$LFS find -stripe-size 1024k -type f $dir"
5559         nums=$($cmd | wc -l)
5560         [ $nums -eq $expected ] ||
5561                 error "'$cmd' wrong: found $nums, expected $expected"
5562 }
5563 run_test 56t "check lfs find -stripe-size works"
5564
5565 test_56u() { # LU-611
5566         local dir=$DIR/$tdir
5567
5568         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5569
5570         if [[ $OSTCOUNT -gt 1 ]]; then
5571                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5572                 onestripe=4
5573         else
5574                 onestripe=0
5575         fi
5576
5577         local expected=$(((NUMDIRS + 1) * NUMFILES))
5578         local cmd="$LFS find -stripe-index 0 -type f $dir"
5579         local nums=$($cmd | wc -l)
5580
5581         [ $nums -eq $expected ] ||
5582                 error "'$cmd' wrong: found $nums, expected $expected"
5583
5584         expected=$onestripe
5585         cmd="$LFS find -stripe-index 1 -type f $dir"
5586         nums=$($cmd | wc -l)
5587         [ $nums -eq $expected ] ||
5588                 error "'$cmd' wrong: found $nums, expected $expected"
5589
5590         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5591         nums=$($cmd | wc -l)
5592         [ $nums -eq $expected ] ||
5593                 error "'$cmd' wrong: found $nums, expected $expected"
5594
5595         expected=0
5596         # This should produce an error and not return any files
5597         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5598         nums=$($cmd 2>/dev/null | wc -l)
5599         [ $nums -eq $expected ] ||
5600                 error "'$cmd' wrong: found $nums, expected $expected"
5601
5602         if [[ $OSTCOUNT -gt 1 ]]; then
5603                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5604                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5605                 nums=$($cmd | wc -l)
5606                 [ $nums -eq $expected ] ||
5607                         error "'$cmd' wrong: found $nums, expected $expected"
5608         fi
5609 }
5610 run_test 56u "check lfs find -stripe-index works"
5611
5612 test_56v() {
5613         local mdt_idx=0
5614         local dir=$DIR/$tdir
5615
5616         setup_56 $dir $NUMFILES $NUMDIRS
5617
5618         UUID=$(mdtuuid_from_index $mdt_idx $dir)
5619         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
5620
5621         for file in $($LFS find -m $UUID $dir); do
5622                 file_midx=$($LFS getstripe -m $file)
5623                 [ $file_midx -eq $mdt_idx ] ||
5624                         error "lfs find -m $UUID != getstripe -m $file_midx"
5625         done
5626 }
5627 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
5628
5629 test_56w() {
5630         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5632
5633         local dir=$DIR/$tdir
5634
5635         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5636
5637         local stripe_size=$($LFS getstripe -S -d $dir) ||
5638                 error "$LFS getstripe -S -d $dir failed"
5639         stripe_size=${stripe_size%% *}
5640
5641         local file_size=$((stripe_size * OSTCOUNT))
5642         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5643         local required_space=$((file_num * file_size))
5644         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5645                            head -n1)
5646         [[ $free_space -le $((required_space / 1024)) ]] &&
5647                 skip_env "need $required_space, have $free_space kbytes"
5648
5649         local dd_bs=65536
5650         local dd_count=$((file_size / dd_bs))
5651
5652         # write data into the files
5653         local i
5654         local j
5655         local file
5656
5657         for i in $(seq $NUMFILES); do
5658                 file=$dir/file$i
5659                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5660                         error "write data into $file failed"
5661         done
5662         for i in $(seq $NUMDIRS); do
5663                 for j in $(seq $NUMFILES); do
5664                         file=$dir/dir$i/file$j
5665                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5666                                 error "write data into $file failed"
5667                 done
5668         done
5669
5670         # $LFS_MIGRATE will fail if hard link migration is unsupported
5671         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5672                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5673                         error "creating links to $dir/dir1/file1 failed"
5674         fi
5675
5676         local expected=-1
5677
5678         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5679
5680         # lfs_migrate file
5681         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5682
5683         echo "$cmd"
5684         eval $cmd || error "$cmd failed"
5685
5686         check_stripe_count $dir/file1 $expected
5687
5688         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
5689         then
5690                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5691                 # OST 1 if it is on OST 0. This file is small enough to
5692                 # be on only one stripe.
5693                 file=$dir/migr_1_ost
5694                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5695                         error "write data into $file failed"
5696                 local obdidx=$($LFS getstripe -i $file)
5697                 local oldmd5=$(md5sum $file)
5698                 local newobdidx=0
5699
5700                 [[ $obdidx -eq 0 ]] && newobdidx=1
5701                 cmd="$LFS migrate -i $newobdidx $file"
5702                 echo $cmd
5703                 eval $cmd || error "$cmd failed"
5704
5705                 local realobdix=$($LFS getstripe -i $file)
5706                 local newmd5=$(md5sum $file)
5707
5708                 [[ $newobdidx -ne $realobdix ]] &&
5709                         error "new OST is different (was=$obdidx, "\
5710                               "wanted=$newobdidx, got=$realobdix)"
5711                 [[ "$oldmd5" != "$newmd5" ]] &&
5712                         error "md5sum differ: $oldmd5, $newmd5"
5713         fi
5714
5715         # lfs_migrate dir
5716         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5717         echo "$cmd"
5718         eval $cmd || error "$cmd failed"
5719
5720         for j in $(seq $NUMFILES); do
5721                 check_stripe_count $dir/dir1/file$j $expected
5722         done
5723
5724         # lfs_migrate works with lfs find
5725         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5726              $LFS_MIGRATE -y -c $expected"
5727         echo "$cmd"
5728         eval $cmd || error "$cmd failed"
5729
5730         for i in $(seq 2 $NUMFILES); do
5731                 check_stripe_count $dir/file$i $expected
5732         done
5733         for i in $(seq 2 $NUMDIRS); do
5734                 for j in $(seq $NUMFILES); do
5735                 check_stripe_count $dir/dir$i/file$j $expected
5736                 done
5737         done
5738 }
5739 run_test 56w "check lfs_migrate -c stripe_count works"
5740
5741 test_56wb() {
5742         local file1=$DIR/$tdir/file1
5743         local create_pool=false
5744         local initial_pool=$($LFS getstripe -p $DIR)
5745         local pool_list=()
5746         local pool=""
5747
5748         echo -n "Creating test dir..."
5749         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5750         echo "done."
5751
5752         echo -n "Creating test file..."
5753         touch $file1 || error "cannot create file"
5754         echo "done."
5755
5756         echo -n "Detecting existing pools..."
5757         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
5758
5759         if [ ${#pool_list[@]} -gt 0 ]; then
5760                 echo "${pool_list[@]}"
5761                 for thispool in "${pool_list[@]}"; do
5762                         if [[ -z "$initial_pool" ||
5763                               "$initial_pool" != "$thispool" ]]; then
5764                                 pool="$thispool"
5765                                 echo "Using existing pool '$pool'"
5766                                 break
5767                         fi
5768                 done
5769         else
5770                 echo "none detected."
5771         fi
5772         if [ -z "$pool" ]; then
5773                 pool=${POOL:-testpool}
5774                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5775                 echo -n "Creating pool '$pool'..."
5776                 create_pool=true
5777                 pool_add $pool &> /dev/null ||
5778                         error "pool_add failed"
5779                 echo "done."
5780
5781                 echo -n "Adding target to pool..."
5782                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5783                         error "pool_add_targets failed"
5784                 echo "done."
5785         fi
5786
5787         echo -n "Setting pool using -p option..."
5788         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5789                 error "migrate failed rc = $?"
5790         echo "done."
5791
5792         echo -n "Verifying test file is in pool after migrating..."
5793         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5794                 error "file was not migrated to pool $pool"
5795         echo "done."
5796
5797         echo -n "Removing test file from pool '$pool'..."
5798         # "lfs migrate $file" won't remove the file from the pool
5799         # until some striping information is changed.
5800         $LFS migrate -c 1 $file1 &> /dev/null ||
5801                 error "cannot remove from pool"
5802         [ "$($LFS getstripe -p $file1)" ] &&
5803                 error "pool still set"
5804         echo "done."
5805
5806         echo -n "Setting pool using --pool option..."
5807         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5808                 error "migrate failed rc = $?"
5809         echo "done."
5810
5811         # Clean up
5812         rm -f $file1
5813         if $create_pool; then
5814                 destroy_test_pools 2> /dev/null ||
5815                         error "destroy test pools failed"
5816         fi
5817 }
5818 run_test 56wb "check lfs_migrate pool support"
5819
5820 test_56wc() {
5821         local file1="$DIR/$tdir/file1"
5822         local parent_ssize
5823         local parent_scount
5824         local cur_ssize
5825         local cur_scount
5826         local orig_ssize
5827
5828         echo -n "Creating test dir..."
5829         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5830         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5831                 error "cannot set stripe by '-S 1M -c 1'"
5832         echo "done"
5833
5834         echo -n "Setting initial stripe for test file..."
5835         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5836                 error "cannot set stripe"
5837         cur_ssize=$($LFS getstripe -S "$file1")
5838         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
5839         echo "done."
5840
5841         # File currently set to -S 512K -c 1
5842
5843         # Ensure -c and -S options are rejected when -R is set
5844         echo -n "Verifying incompatible options are detected..."
5845         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5846                 error "incompatible -c and -R options not detected"
5847         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5848                 error "incompatible -S and -R options not detected"
5849         echo "done."
5850
5851         # Ensure unrecognized options are passed through to 'lfs migrate'
5852         echo -n "Verifying -S option is passed through to lfs migrate..."
5853         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5854                 error "migration failed"
5855         cur_ssize=$($LFS getstripe -S "$file1")
5856         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
5857         echo "done."
5858
5859         # File currently set to -S 1M -c 1
5860
5861         # Ensure long options are supported
5862         echo -n "Verifying long options supported..."
5863         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5864                 error "long option without argument not supported"
5865         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5866                 error "long option with argument not supported"
5867         cur_ssize=$($LFS getstripe -S "$file1")
5868         [ $cur_ssize -eq 524288 ] ||
5869                 error "migrate --stripe-size $cur_ssize != 524288"
5870         echo "done."
5871
5872         # File currently set to -S 512K -c 1
5873
5874         if [ "$OSTCOUNT" -gt 1 ]; then
5875                 echo -n "Verifying explicit stripe count can be set..."
5876                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5877                         error "migrate failed"
5878                 cur_scount=$($LFS getstripe -c "$file1")
5879                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
5880                 echo "done."
5881         fi
5882
5883         # File currently set to -S 512K -c 1 or -S 512K -c 2
5884
5885         # Ensure parent striping is used if -R is set, and no stripe
5886         # count or size is specified
5887         echo -n "Setting stripe for parent directory..."
5888         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5889                 error "cannot set stripe '-S 2M -c 1'"
5890         echo "done."
5891
5892         echo -n "Verifying restripe option uses parent stripe settings..."
5893         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
5894         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
5895         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5896                 error "migrate failed"
5897         cur_ssize=$($LFS getstripe -S "$file1")
5898         [ $cur_ssize -eq $parent_ssize ] ||
5899                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
5900         cur_scount=$($LFS getstripe -c "$file1")
5901         [ $cur_scount -eq $parent_scount ] ||
5902                 error "migrate -R stripe_count $cur_scount != $parent_scount"
5903         echo "done."
5904
5905         # File currently set to -S 1M -c 1
5906
5907         # Ensure striping is preserved if -R is not set, and no stripe
5908         # count or size is specified
5909         echo -n "Verifying striping size preserved when not specified..."
5910         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
5911         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5912                 error "cannot set stripe on parent directory"
5913         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5914                 error "migrate failed"
5915         cur_ssize=$($LFS getstripe -S "$file1")
5916         [ $cur_ssize -eq $orig_ssize ] ||
5917                 error "migrate by default $cur_ssize != $orig_ssize"
5918         echo "done."
5919
5920         # Ensure file name properly detected when final option has no argument
5921         echo -n "Verifying file name properly detected..."
5922         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5923                 error "file name interpreted as option argument"
5924         echo "done."
5925
5926         # Clean up
5927         rm -f "$file1"
5928 }
5929 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5930
5931 test_56wd() {
5932         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5933
5934         local file1=$DIR/$tdir/file1
5935
5936         echo -n "Creating test dir..."
5937         test_mkdir $DIR/$tdir || error "cannot create dir"
5938         echo "done."
5939
5940         echo -n "Creating test file..."
5941         touch $file1
5942         echo "done."
5943
5944         # Ensure 'lfs migrate' will fail by using a non-existent option,
5945         # and make sure rsync is not called to recover
5946         echo -n "Make sure --no-rsync option works..."
5947         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5948                 grep -q 'refusing to fall back to rsync' ||
5949                 error "rsync was called with --no-rsync set"
5950         echo "done."
5951
5952         # Ensure rsync is called without trying 'lfs migrate' first
5953         echo -n "Make sure --rsync option works..."
5954         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5955                 grep -q 'falling back to rsync' &&
5956                 error "lfs migrate was called with --rsync set"
5957         echo "done."
5958
5959         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
5960         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
5961                 grep -q 'at the same time' ||
5962                 error "--rsync and --no-rsync accepted concurrently"
5963         echo "done."
5964
5965         # Clean up
5966         rm -f $file1
5967 }
5968 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
5969
5970 test_56x() {
5971         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5972         check_swap_layouts_support
5973
5974         local dir=$DIR/$tdir
5975         local ref1=/etc/passwd
5976         local file1=$dir/file1
5977
5978         test_mkdir $dir || error "creating dir $dir"
5979         $LFS setstripe -c 2 $file1
5980         cp $ref1 $file1
5981         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5982         stripe=$($LFS getstripe -c $file1)
5983         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5984         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5985
5986         # clean up
5987         rm -f $file1
5988 }
5989 run_test 56x "lfs migration support"
5990
5991 test_56xa() {
5992         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5993         check_swap_layouts_support
5994
5995         local dir=$DIR/$tdir/$testnum
5996
5997         test_mkdir -p $dir
5998
5999         local ref1=/etc/passwd
6000         local file1=$dir/file1
6001
6002         $LFS setstripe -c 2 $file1
6003         cp $ref1 $file1
6004         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6005
6006         local stripe=$($LFS getstripe -c $file1)
6007
6008         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6009         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6010
6011         # clean up
6012         rm -f $file1
6013 }
6014 run_test 56xa "lfs migration --block support"
6015
6016 check_migrate_links() {
6017         local dir="$1"
6018         local file1="$dir/file1"
6019         local begin="$2"
6020         local count="$3"
6021         local total_count=$(($begin + $count - 1))
6022         local symlink_count=10
6023         local uniq_count=10
6024
6025         if [ ! -f "$file1" ]; then
6026                 echo -n "creating initial file..."
6027                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6028                         error "cannot setstripe initial file"
6029                 echo "done"
6030
6031                 echo -n "creating symlinks..."
6032                 for s in $(seq 1 $symlink_count); do
6033                         ln -s "$file1" "$dir/slink$s" ||
6034                                 error "cannot create symlinks"
6035                 done
6036                 echo "done"
6037
6038                 echo -n "creating nonlinked files..."
6039                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6040                         error "cannot create nonlinked files"
6041                 echo "done"
6042         fi
6043
6044         # create hard links
6045         if [ ! -f "$dir/file$total_count" ]; then
6046                 echo -n "creating hard links $begin:$total_count..."
6047                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6048                         /dev/null || error "cannot create hard links"
6049                 echo "done"
6050         fi
6051
6052         echo -n "checking number of hard links listed in xattrs..."
6053         local fid=$($LFS getstripe -F "$file1")
6054         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6055
6056         echo "${#paths[*]}"
6057         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6058                         skip "hard link list has unexpected size, skipping test"
6059         fi
6060         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6061                         error "link names should exceed xattrs size"
6062         fi
6063
6064         echo -n "migrating files..."
6065         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
6066         local rc=$?
6067         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6068         echo "done"
6069
6070         # make sure all links have been properly migrated
6071         echo -n "verifying files..."
6072         fid=$($LFS getstripe -F "$file1") ||
6073                 error "cannot get fid for file $file1"
6074         for i in $(seq 2 $total_count); do
6075                 local fid2=$($LFS getstripe -F $dir/file$i)
6076
6077                 [ "$fid2" == "$fid" ] ||
6078                         error "migrated hard link has mismatched FID"
6079         done
6080
6081         # make sure hard links were properly detected, and migration was
6082         # performed only once for the entire link set; nonlinked files should
6083         # also be migrated
6084         local actual=$(grep -c 'done' <<< "$migrate_out")
6085         local expected=$(($uniq_count + 1))
6086
6087         [ "$actual" -eq  "$expected" ] ||
6088                 error "hard links individually migrated ($actual != $expected)"
6089
6090         # make sure the correct number of hard links are present
6091         local hardlinks=$(stat -c '%h' "$file1")
6092
6093         [ $hardlinks -eq $total_count ] ||
6094                 error "num hard links $hardlinks != $total_count"
6095         echo "done"
6096
6097         return 0
6098 }
6099
6100 test_56xb() {
6101         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6102                 skip "Need MDS version at least 2.10.55"
6103
6104         local dir="$DIR/$tdir"
6105
6106         test_mkdir "$dir" || error "cannot create dir $dir"
6107
6108         echo "testing lfs migrate mode when all links fit within xattrs"
6109         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6110
6111         echo "testing rsync mode when all links fit within xattrs"
6112         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6113
6114         echo "testing lfs migrate mode when all links do not fit within xattrs"
6115         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6116
6117         echo "testing rsync mode when all links do not fit within xattrs"
6118         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6119
6120
6121         # clean up
6122         rm -rf $dir
6123 }
6124 run_test 56xb "lfs migration hard link support"
6125
6126 test_56xc() {
6127         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6128
6129         local dir="$DIR/$tdir"
6130
6131         test_mkdir "$dir" || error "cannot create dir $dir"
6132
6133         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6134         echo -n "Setting initial stripe for 20MB test file..."
6135         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6136                 error "cannot setstripe 20MB file"
6137         echo "done"
6138         echo -n "Sizing 20MB test file..."
6139         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6140         echo "done"
6141         echo -n "Verifying small file autostripe count is 1..."
6142         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6143                 error "cannot migrate 20MB file"
6144         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6145                 error "cannot get stripe for $dir/20mb"
6146         [ $stripe_count -eq 1 ] ||
6147                 error "unexpected stripe count $stripe_count for 20MB file"
6148         rm -f "$dir/20mb"
6149         echo "done"
6150
6151         # Test 2: File is small enough to fit within the available space on
6152         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
6153         # have at least an additional 1KB for each desired stripe for test 3
6154         echo -n "Setting stripe for 1GB test file..."
6155         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
6156         echo "done"
6157         echo -n "Sizing 1GB test file..."
6158         # File size is 1GB + 3KB
6159         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
6160         echo "done"
6161
6162         # need at least 512MB per OST for 1GB file to fit in 2 stripes
6163         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
6164         if (( avail > 524288 * OSTCOUNT )); then
6165                 echo -n "Migrating 1GB file..."
6166                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
6167                         error "cannot migrate 1GB file"
6168                 echo "done"
6169                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
6170                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
6171                         error "cannot getstripe for 1GB file"
6172                 [ $stripe_count -eq 2 ] ||
6173                         error "unexpected stripe count $stripe_count != 2"
6174                 echo "done"
6175         fi
6176
6177         # Test 3: File is too large to fit within the available space on
6178         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
6179         if [ $OSTCOUNT -ge 3 ]; then
6180                 # The required available space is calculated as
6181                 # file size (1GB + 3KB) / OST count (3).
6182                 local kb_per_ost=349526
6183
6184                 echo -n "Migrating 1GB file with limit..."
6185                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
6186                         error "cannot migrate 1GB file with limit"
6187                 echo "done"
6188
6189                 stripe_count=$($LFS getstripe -c "$dir/1gb")
6190                 echo -n "Verifying 1GB autostripe count with limited space..."
6191                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
6192                         error "unexpected stripe count $stripe_count (min 3)"
6193                 echo "done"
6194         fi
6195
6196         # clean up
6197         rm -rf $dir
6198 }
6199 run_test 56xc "lfs migration autostripe"
6200
6201 test_56xd() {
6202         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6203
6204         local dir=$DIR/$tdir
6205         local f_mgrt=$dir/$tfile.mgrt
6206         local f_yaml=$dir/$tfile.yaml
6207         local f_copy=$dir/$tfile.copy
6208         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6209         local layout_copy="-c 2 -S 2M -i 1"
6210         local yamlfile=$dir/yamlfile
6211         local layout_before;
6212         local layout_after;
6213
6214         test_mkdir "$dir" || error "cannot create dir $dir"
6215         $LFS setstripe $layout_yaml $f_yaml ||
6216                 error "cannot setstripe $f_yaml with layout $layout_yaml"
6217         $LFS getstripe --yaml $f_yaml > $yamlfile
6218         $LFS setstripe $layout_copy $f_copy ||
6219                 error "cannot setstripe $f_copy with layout $layout_copy"
6220         touch $f_mgrt
6221         dd if=/dev/zero of=$f_mgrt bs=1M count=4
6222
6223         # 1. test option --yaml
6224         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
6225                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
6226         layout_before=$(get_layout_param $f_yaml)
6227         layout_after=$(get_layout_param $f_mgrt)
6228         [ "$layout_after" == "$layout_before" ] ||
6229                 error "lfs_migrate --yaml: $layout_after != $layout_before"
6230
6231         # 2. test option --copy
6232         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
6233                 error "cannot migrate $f_mgrt with --copy $f_copy"
6234         layout_before=$(get_layout_param $f_copy)
6235         layout_after=$(get_layout_param $f_mgrt)
6236         [ "$layout_after" == "$layout_before" ] ||
6237                 error "lfs_migrate --copy: $layout_after != $layout_before"
6238 }
6239 run_test 56xd "check lfs_migrate --yaml and --copy support"
6240
6241 test_56xe() {
6242         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6243
6244         local dir=$DIR/$tdir
6245         local f_comp=$dir/$tfile
6246         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
6247         local layout_before=""
6248         local layout_after=""
6249
6250         test_mkdir "$dir" || error "cannot create dir $dir"
6251         $LFS setstripe $layout $f_comp ||
6252                 error "cannot setstripe $f_comp with layout $layout"
6253         layout_before=$(get_layout_param $f_comp)
6254         dd if=/dev/zero of=$f_comp bs=1M count=4
6255
6256         # 1. migrate a comp layout file by lfs_migrate
6257         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
6258         layout_after=$(get_layout_param $f_comp)
6259         [ "$layout_before" == "$layout_after" ] ||
6260                 error "lfs_migrate: $layout_before != $layout_after"
6261
6262         # 2. migrate a comp layout file by lfs migrate
6263         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
6264         layout_after=$(get_layout_param $f_comp)
6265         [ "$layout_before" == "$layout_after" ] ||
6266                 error "lfs migrate: $layout_before != $layout_after"
6267 }
6268 run_test 56xe "migrate a composite layout file"
6269
6270 test_56y() {
6271         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
6272                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
6273
6274         local res=""
6275         local dir=$DIR/$tdir
6276         local f1=$dir/file1
6277         local f2=$dir/file2
6278
6279         test_mkdir -p $dir || error "creating dir $dir"
6280         touch $f1 || error "creating std file $f1"
6281         $MULTIOP $f2 H2c || error "creating released file $f2"
6282
6283         # a directory can be raid0, so ask only for files
6284         res=$($LFS find $dir -L raid0 -type f | wc -l)
6285         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
6286
6287         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
6288         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
6289
6290         # only files can be released, so no need to force file search
6291         res=$($LFS find $dir -L released)
6292         [[ $res == $f2 ]] || error "search released: found $res != $f2"
6293
6294         res=$($LFS find $dir -type f \! -L released)
6295         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
6296 }
6297 run_test 56y "lfs find -L raid0|released"
6298
6299 test_56z() { # LU-4824
6300         # This checks to make sure 'lfs find' continues after errors
6301         # There are two classes of errors that should be caught:
6302         # - If multiple paths are provided, all should be searched even if one
6303         #   errors out
6304         # - If errors are encountered during the search, it should not terminate
6305         #   early
6306         local dir=$DIR/$tdir
6307         local i
6308
6309         test_mkdir $dir
6310         for i in d{0..9}; do
6311                 test_mkdir $dir/$i
6312                 touch $dir/$i/$tfile
6313         done
6314         $LFS find $DIR/non_existent_dir $dir &&
6315                 error "$LFS find did not return an error"
6316         # Make a directory unsearchable. This should NOT be the last entry in
6317         # directory order.  Arbitrarily pick the 6th entry
6318         chmod 700 $($LFS find $dir -type d | sed '6!d')
6319
6320         $RUNAS $LFS find $DIR/non_existent $dir
6321         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
6322
6323         # The user should be able to see 10 directories and 9 files
6324         (( count == 19 )) ||
6325                 error "$LFS find found $count != 19 entries after error"
6326 }
6327 run_test 56z "lfs find should continue after an error"
6328
6329 test_56aa() { # LU-5937
6330         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
6331
6332         local dir=$DIR/$tdir
6333
6334         mkdir $dir
6335         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
6336
6337         createmany -o $dir/striped_dir/${tfile}- 1024
6338         local dirs=$($LFS find --size +8k $dir/)
6339
6340         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
6341 }
6342 run_test 56aa "lfs find --size under striped dir"
6343
6344 test_56ab() { # LU-10705
6345         test_mkdir $DIR/$tdir
6346         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
6347         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
6348         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
6349         # Flush writes to ensure valid blocks.  Need to be more thorough for
6350         # ZFS, since blocks are not allocated/returned to client immediately.
6351         sync_all_data
6352         wait_zfs_commit ost1 2
6353         cancel_lru_locks osc
6354         ls -ls $DIR/$tdir
6355
6356         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
6357
6358         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
6359
6360         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
6361         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
6362
6363         rm -f $DIR/$tdir/$tfile.[123]
6364 }
6365 run_test 56ab "lfs find --blocks"
6366
6367 test_56ba() {
6368         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
6369                 skip "Need MDS version at least 2.10.50"
6370
6371         # Create composite files with one component
6372         local dir=$DIR/$tdir
6373
6374         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
6375         # Create composite files with three components
6376         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
6377         # Create non-composite files
6378         createmany -o $dir/${tfile}- 10
6379
6380         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
6381
6382         [[ $nfiles == 10 ]] ||
6383                 error "lfs find -E 1M found $nfiles != 10 files"
6384
6385         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
6386         [[ $nfiles == 25 ]] ||
6387                 error "lfs find ! -E 1M found $nfiles != 25 files"
6388
6389         # All files have a component that starts at 0
6390         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
6391         [[ $nfiles == 35 ]] ||
6392                 error "lfs find --component-start 0 - $nfiles != 35 files"
6393
6394         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
6395         [[ $nfiles == 15 ]] ||
6396                 error "lfs find --component-start 2M - $nfiles != 15 files"
6397
6398         # All files created here have a componenet that does not starts at 2M
6399         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
6400         [[ $nfiles == 35 ]] ||
6401                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
6402
6403         # Find files with a specified number of components
6404         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
6405         [[ $nfiles == 15 ]] ||
6406                 error "lfs find --component-count 3 - $nfiles != 15 files"
6407
6408         # Remember non-composite files have a component count of zero
6409         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
6410         [[ $nfiles == 10 ]] ||
6411                 error "lfs find --component-count 0 - $nfiles != 10 files"
6412
6413         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
6414         [[ $nfiles == 20 ]] ||
6415                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
6416
6417         # All files have a flag called "init"
6418         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
6419         [[ $nfiles == 35 ]] ||
6420                 error "lfs find --component-flags init - $nfiles != 35 files"
6421
6422         # Multi-component files will have a component not initialized
6423         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
6424         [[ $nfiles == 15 ]] ||
6425                 error "lfs find !--component-flags init - $nfiles != 15 files"
6426
6427         rm -rf $dir
6428
6429 }
6430 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
6431
6432 test_56ca() {
6433         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
6434                 skip "Need MDS version at least 2.10.57"
6435
6436         local td=$DIR/$tdir
6437         local tf=$td/$tfile
6438         local dir
6439         local nfiles
6440         local cmd
6441         local i
6442         local j
6443
6444         # create mirrored directories and mirrored files
6445         mkdir $td || error "mkdir $td failed"
6446         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
6447         createmany -o $tf- 10 || error "create $tf- failed"
6448
6449         for i in $(seq 2); do
6450                 dir=$td/dir$i
6451                 mkdir $dir || error "mkdir $dir failed"
6452                 $LFS mirror create -N$((3 + i)) $dir ||
6453                         error "create mirrored dir $dir failed"
6454                 createmany -o $dir/$tfile- 10 ||
6455                         error "create $dir/$tfile- failed"
6456         done
6457
6458         # change the states of some mirrored files
6459         echo foo > $tf-6
6460         for i in $(seq 2); do
6461                 dir=$td/dir$i
6462                 for j in $(seq 4 9); do
6463                         echo foo > $dir/$tfile-$j
6464                 done
6465         done
6466
6467         # find mirrored files with specific mirror count
6468         cmd="$LFS find --mirror-count 3 --type f $td"
6469         nfiles=$($cmd | wc -l)
6470         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
6471
6472         cmd="$LFS find ! --mirror-count 3 --type f $td"
6473         nfiles=$($cmd | wc -l)
6474         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
6475
6476         cmd="$LFS find --mirror-count +2 --type f $td"
6477         nfiles=$($cmd | wc -l)
6478         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6479
6480         cmd="$LFS find --mirror-count -6 --type f $td"
6481         nfiles=$($cmd | wc -l)
6482         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6483
6484         # find mirrored files with specific file state
6485         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
6486         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
6487
6488         cmd="$LFS find --mirror-state=ro --type f $td"
6489         nfiles=$($cmd | wc -l)
6490         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
6491
6492         cmd="$LFS find ! --mirror-state=ro --type f $td"
6493         nfiles=$($cmd | wc -l)
6494         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6495
6496         cmd="$LFS find --mirror-state=wp --type f $td"
6497         nfiles=$($cmd | wc -l)
6498         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
6499
6500         cmd="$LFS find ! --mirror-state=sp --type f $td"
6501         nfiles=$($cmd | wc -l)
6502         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
6503 }
6504 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
6505
6506 test_57a() {
6507         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6508         # note test will not do anything if MDS is not local
6509         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6510                 skip_env "ldiskfs only test"
6511         fi
6512         remote_mds_nodsh && skip "remote MDS with nodsh"
6513
6514         local MNTDEV="osd*.*MDT*.mntdev"
6515         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
6516         [ -z "$DEV" ] && error "can't access $MNTDEV"
6517         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
6518                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
6519                         error "can't access $DEV"
6520                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
6521                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
6522                 rm $TMP/t57a.dump
6523         done
6524 }
6525 run_test 57a "verify MDS filesystem created with large inodes =="
6526
6527 test_57b() {
6528         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6529         if [ "$mds1_FSTYPE" != ldiskfs ]; then
6530                 skip_env "ldiskfs only test"
6531         fi
6532         remote_mds_nodsh && skip "remote MDS with nodsh"
6533
6534         local dir=$DIR/$tdir
6535         local filecount=100
6536         local file1=$dir/f1
6537         local fileN=$dir/f$filecount
6538
6539         rm -rf $dir || error "removing $dir"
6540         test_mkdir -c1 $dir
6541         local mdtidx=$($LFS getstripe -m $dir)
6542         local mdtname=MDT$(printf %04x $mdtidx)
6543         local facet=mds$((mdtidx + 1))
6544
6545         echo "mcreating $filecount files"
6546         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
6547
6548         # verify that files do not have EAs yet
6549         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
6550                 error "$file1 has an EA"
6551         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
6552                 error "$fileN has an EA"
6553
6554         sync
6555         sleep 1
6556         df $dir  #make sure we get new statfs data
6557         local mdsfree=$(do_facet $facet \
6558                         lctl get_param -n osd*.*$mdtname.kbytesfree)
6559         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6560         local file
6561
6562         echo "opening files to create objects/EAs"
6563         for file in $(seq -f $dir/f%g 1 $filecount); do
6564                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
6565                         error "opening $file"
6566         done
6567
6568         # verify that files have EAs now
6569         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
6570         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
6571
6572         sleep 1  #make sure we get new statfs data
6573         df $dir
6574         local mdsfree2=$(do_facet $facet \
6575                          lctl get_param -n osd*.*$mdtname.kbytesfree)
6576         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
6577
6578         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
6579                 if [ "$mdsfree" != "$mdsfree2" ]; then
6580                         error "MDC before $mdcfree != after $mdcfree2"
6581                 else
6582                         echo "MDC before $mdcfree != after $mdcfree2"
6583                         echo "unable to confirm if MDS has large inodes"
6584                 fi
6585         fi
6586         rm -rf $dir
6587 }
6588 run_test 57b "default LOV EAs are stored inside large inodes ==="
6589
6590 test_58() {
6591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6592         [ -z "$(which wiretest 2>/dev/null)" ] &&
6593                         skip_env "could not find wiretest"
6594
6595         wiretest
6596 }
6597 run_test 58 "verify cross-platform wire constants =============="
6598
6599 test_59() {
6600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6601
6602         echo "touch 130 files"
6603         createmany -o $DIR/f59- 130
6604         echo "rm 130 files"
6605         unlinkmany $DIR/f59- 130
6606         sync
6607         # wait for commitment of removal
6608         wait_delete_completed
6609 }
6610 run_test 59 "verify cancellation of llog records async ========="
6611
6612 TEST60_HEAD="test_60 run $RANDOM"
6613 test_60a() {
6614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6615         remote_mgs_nodsh && skip "remote MGS with nodsh"
6616         do_facet mgs "! which run-llog.sh &> /dev/null" &&
6617                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
6618                         skip_env "missing subtest run-llog.sh"
6619
6620         log "$TEST60_HEAD - from kernel mode"
6621         do_facet mgs "$LCTL dk > /dev/null"
6622         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
6623         do_facet mgs $LCTL dk > $TMP/$tfile
6624
6625         # LU-6388: test llog_reader
6626         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
6627         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
6628         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
6629                         skip_env "missing llog_reader"
6630         local fstype=$(facet_fstype mgs)
6631         [ $fstype != ldiskfs -a $fstype != zfs ] &&
6632                 skip_env "Only for ldiskfs or zfs type mgs"
6633
6634         local mntpt=$(facet_mntpt mgs)
6635         local mgsdev=$(mgsdevname 1)
6636         local fid_list
6637         local fid
6638         local rec_list
6639         local rec
6640         local rec_type
6641         local obj_file
6642         local path
6643         local seq
6644         local oid
6645         local pass=true
6646
6647         #get fid and record list
6648         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
6649                 tail -n 4))
6650         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
6651                 tail -n 4))
6652         #remount mgs as ldiskfs or zfs type
6653         stop mgs || error "stop mgs failed"
6654         mount_fstype mgs || error "remount mgs failed"
6655         for ((i = 0; i < ${#fid_list[@]}; i++)); do
6656                 fid=${fid_list[i]}
6657                 rec=${rec_list[i]}
6658                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
6659                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
6660                 oid=$((16#$oid))
6661
6662                 case $fstype in
6663                         ldiskfs )
6664                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
6665                         zfs )
6666                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
6667                 esac
6668                 echo "obj_file is $obj_file"
6669                 do_facet mgs $llog_reader $obj_file
6670
6671                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
6672                         awk '{ print $3 }' | sed -e "s/^type=//g")
6673                 if [ $rec_type != $rec ]; then
6674                         echo "FAILED test_60a wrong record type $rec_type," \
6675                               "should be $rec"
6676                         pass=false
6677                         break
6678                 fi
6679
6680                 #check obj path if record type is LLOG_LOGID_MAGIC
6681                 if [ "$rec" == "1064553b" ]; then
6682                         path=$(do_facet mgs $llog_reader $obj_file |
6683                                 grep "path=" | awk '{ print $NF }' |
6684                                 sed -e "s/^path=//g")
6685                         if [ $obj_file != $mntpt/$path ]; then
6686                                 echo "FAILED test_60a wrong obj path" \
6687                                       "$montpt/$path, should be $obj_file"
6688                                 pass=false
6689                                 break
6690                         fi
6691                 fi
6692         done
6693         rm -f $TMP/$tfile
6694         #restart mgs before "error", otherwise it will block the next test
6695         stop mgs || error "stop mgs failed"
6696         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
6697         $pass || error "test failed, see FAILED test_60a messages for specifics"
6698 }
6699 run_test 60a "llog_test run from kernel module and test llog_reader"
6700
6701 test_60b() { # bug 6411
6702         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6703
6704         dmesg > $DIR/$tfile
6705         LLOG_COUNT=$(do_facet mgs dmesg |
6706                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
6707                           /llog_[a-z]*.c:[0-9]/ {
6708                                 if (marker)
6709                                         from_marker++
6710                                 from_begin++
6711                           }
6712                           END {
6713                                 if (marker)
6714                                         print from_marker
6715                                 else
6716                                         print from_begin
6717                           }")
6718
6719         [[ $LLOG_COUNT -gt 120 ]] &&
6720                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
6721 }
6722 run_test 60b "limit repeated messages from CERROR/CWARN"
6723
6724 test_60c() {
6725         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6726
6727         echo "create 5000 files"
6728         createmany -o $DIR/f60c- 5000
6729 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
6730         lctl set_param fail_loc=0x80000137
6731         unlinkmany $DIR/f60c- 5000
6732         lctl set_param fail_loc=0
6733 }
6734 run_test 60c "unlink file when mds full"
6735
6736 test_60d() {
6737         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6738
6739         SAVEPRINTK=$(lctl get_param -n printk)
6740         # verify "lctl mark" is even working"
6741         MESSAGE="test message ID $RANDOM $$"
6742         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6743         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
6744
6745         lctl set_param printk=0 || error "set lnet.printk failed"
6746         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
6747         MESSAGE="new test message ID $RANDOM $$"
6748         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
6749         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
6750         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
6751
6752         lctl set_param -n printk="$SAVEPRINTK"
6753 }
6754 run_test 60d "test printk console message masking"
6755
6756 test_60e() {
6757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6758         remote_mds_nodsh && skip "remote MDS with nodsh"
6759
6760         touch $DIR/$tfile
6761 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
6762         do_facet mds1 lctl set_param fail_loc=0x15b
6763         rm $DIR/$tfile
6764 }
6765 run_test 60e "no space while new llog is being created"
6766
6767 test_60g() {
6768         local pid
6769
6770         test_mkdir -c $MDSCOUNT $DIR/$tdir
6771         $LFS setdirstripe -D -i -1 -c $MDSCOUNT $DIR/$tdir
6772
6773         (
6774                 local index=0
6775                 while true; do
6776                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
6777                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
6778                         index=$((index + 1))
6779                 done
6780         ) &
6781
6782         pid=$!
6783
6784         for i in $(seq 100); do 
6785                 # define OBD_FAIL_OSD_TXN_START    0x19a
6786                 do_facet mds1 lctl set_param fail_loc=0x8000019a
6787                 usleep 100
6788         done
6789
6790         kill -9 $pid
6791
6792         mkdir $DIR/$tdir/new || error "mkdir failed"
6793         rmdir $DIR/$tdir/new || error "rmdir failed"
6794 }
6795 run_test 60g "transaction abort won't cause MDT hung"
6796
6797 test_60h() {
6798         [ $MDS1_VERSION -le $(version_code 2.12.52) ] ||
6799                 skip "Need MDS version at least 2.12.52"
6800         [ $MDSCOUNT -le 2 ] && skip "Need >= 2 MDTs"
6801
6802         local f
6803
6804         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
6805         #define OBD_FAIL_MDS_STRIPE_FID          0x189
6806         for fail_loc in 0x80000188 0x80000189; do
6807                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
6808                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
6809                         error "mkdir $dir-$fail_loc failed"
6810                 for i in {0..10}; do
6811                         # create may fail on missing stripe
6812                         echo $i > $DIR/$tdir-$fail_loc/$i
6813                 done
6814                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6815                         error "getdirstripe $tdir-$fail_loc failed"
6816                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
6817                         error "migrate $tdir-$fail_loc failed"
6818                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
6819                         error "getdirstripe $tdir-$fail_loc failed"
6820                 pushd $DIR/$tdir-$fail_loc
6821                 for f in *; do
6822                         echo $f | cmp $f - || error "$f data mismatch"
6823                 done
6824                 popd
6825                 rm -rf $DIR/$tdir-$fail_loc
6826         done
6827 }
6828 run_test 60h "striped directory with missing stripes can be accessed"
6829
6830 test_61a() {
6831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6832
6833         f="$DIR/f61"
6834         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
6835         cancel_lru_locks osc
6836         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
6837         sync
6838 }
6839 run_test 61a "mmap() writes don't make sync hang ================"
6840
6841 test_61b() {
6842         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
6843 }
6844 run_test 61b "mmap() of unstriped file is successful"
6845
6846 # bug 2330 - insufficient obd_match error checking causes LBUG
6847 test_62() {
6848         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6849
6850         f="$DIR/f62"
6851         echo foo > $f
6852         cancel_lru_locks osc
6853         lctl set_param fail_loc=0x405
6854         cat $f && error "cat succeeded, expect -EIO"
6855         lctl set_param fail_loc=0
6856 }
6857 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
6858 # match every page all of the time.
6859 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
6860
6861 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
6862 # Though this test is irrelevant anymore, it helped to reveal some
6863 # other grant bugs (LU-4482), let's keep it.
6864 test_63a() {   # was test_63
6865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6866
6867         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
6868
6869         for i in `seq 10` ; do
6870                 dd if=/dev/zero of=$DIR/f63 bs=8k &
6871                 sleep 5
6872                 kill $!
6873                 sleep 1
6874         done
6875
6876         rm -f $DIR/f63 || true
6877 }
6878 run_test 63a "Verify oig_wait interruption does not crash ======="
6879
6880 # bug 2248 - async write errors didn't return to application on sync
6881 # bug 3677 - async write errors left page locked
6882 test_63b() {
6883         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6884
6885         debugsave
6886         lctl set_param debug=-1
6887
6888         # ensure we have a grant to do async writes
6889         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6890         rm $DIR/$tfile
6891
6892         sync    # sync lest earlier test intercept the fail_loc
6893
6894         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6895         lctl set_param fail_loc=0x80000406
6896         $MULTIOP $DIR/$tfile Owy && \
6897                 error "sync didn't return ENOMEM"
6898         sync; sleep 2; sync     # do a real sync this time to flush page
6899         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6900                 error "locked page left in cache after async error" || true
6901         debugrestore
6902 }
6903 run_test 63b "async write errors should be returned to fsync ==="
6904
6905 test_64a () {
6906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6907
6908         df $DIR
6909         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6910 }
6911 run_test 64a "verify filter grant calculations (in kernel) ====="
6912
6913 test_64b () {
6914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6915
6916         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6917 }
6918 run_test 64b "check out-of-space detection on client"
6919
6920 test_64c() {
6921         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6922 }
6923 run_test 64c "verify grant shrink"
6924
6925 # this does exactly what osc_request.c:osc_announce_cached() does in
6926 # order to calculate max amount of grants to ask from server
6927 want_grant() {
6928         local tgt=$1
6929
6930         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6931         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6932
6933         ((rpc_in_flight ++));
6934         nrpages=$((nrpages * rpc_in_flight))
6935
6936         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6937
6938         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
6939
6940         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6941         local undirty=$((nrpages * PAGE_SIZE))
6942
6943         local max_extent_pages
6944         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6945             grep grant_max_extent_size | awk '{print $2}')
6946         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
6947         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6948         local grant_extent_tax
6949         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6950             grep grant_extent_tax | awk '{print $2}')
6951
6952         undirty=$((undirty + nrextents * grant_extent_tax))
6953
6954         echo $undirty
6955 }
6956
6957 # this is size of unit for grant allocation. It should be equal to
6958 # what tgt_grant.c:tgt_grant_chunk() calculates
6959 grant_chunk() {
6960         local tgt=$1
6961         local max_brw_size
6962         local grant_extent_tax
6963
6964         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6965             grep max_brw_size | awk '{print $2}')
6966
6967         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6968             grep grant_extent_tax | awk '{print $2}')
6969
6970         echo $(((max_brw_size + grant_extent_tax) * 2))
6971 }
6972
6973 test_64d() {
6974         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
6975                 skip "OST < 2.10.55 doesn't limit grants enough"
6976
6977         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6978         local file=$DIR/$tfile
6979
6980         [[ $($LCTL get_param osc.${tgt}.import |
6981              grep "connect_flags:.*grant_param") ]] ||
6982                 skip "no grant_param connect flag"
6983
6984         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6985
6986         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6987
6988         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6989         stack_trap "rm -f $file" EXIT
6990
6991         $SETSTRIPE $file -i 0 -c 1
6992         dd if=/dev/zero of=$file bs=1M count=1000 &
6993         ddpid=$!
6994
6995         while true
6996         do
6997                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6998                 if [[ $cur_grant -gt $max_cur_granted ]]
6999                 then
7000                         kill $ddpid
7001                         error "cur_grant $cur_grant > $max_cur_granted"
7002                 fi
7003                 kill -0 $ddpid
7004                 [[ $? -ne 0 ]] && break;
7005                 sleep 2
7006         done
7007
7008         rm -f $DIR/$tfile
7009         wait_delete_completed
7010         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7011 }
7012 run_test 64d "check grant limit exceed"
7013
7014 # bug 1414 - set/get directories' stripe info
7015 test_65a() {
7016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7017
7018         test_mkdir $DIR/$tdir
7019         touch $DIR/$tdir/f1
7020         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7021 }
7022 run_test 65a "directory with no stripe info"
7023
7024 test_65b() {
7025         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7026
7027         test_mkdir $DIR/$tdir
7028         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7029
7030         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7031                                                 error "setstripe"
7032         touch $DIR/$tdir/f2
7033         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7034 }
7035 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7036
7037 test_65c() {
7038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7039         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7040
7041         test_mkdir $DIR/$tdir
7042         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
7043
7044         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7045                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7046         touch $DIR/$tdir/f3
7047         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7048 }
7049 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7050
7051 test_65d() {
7052         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7053
7054         test_mkdir $DIR/$tdir
7055         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
7056         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7057
7058         if [[ $STRIPECOUNT -le 0 ]]; then
7059                 sc=1
7060         elif [[ $STRIPECOUNT -gt 2000 ]]; then
7061 #LOV_MAX_STRIPE_COUNT is 2000
7062                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
7063         else
7064                 sc=$(($STRIPECOUNT - 1))
7065         fi
7066         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7067         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7068         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7069                 error "lverify failed"
7070 }
7071 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7072
7073 test_65e() {
7074         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7075
7076         test_mkdir $DIR/$tdir
7077
7078         $SETSTRIPE $DIR/$tdir || error "setstripe"
7079         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
7080                                         error "no stripe info failed"
7081         touch $DIR/$tdir/f6
7082         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7083 }
7084 run_test 65e "directory setstripe defaults"
7085
7086 test_65f() {
7087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7088
7089         test_mkdir $DIR/${tdir}f
7090         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
7091 }
7092 run_test 65f "dir setstripe permission (should return error) ==="
7093
7094 test_65g() {
7095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7096
7097         test_mkdir $DIR/$tdir
7098         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7099
7100         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7101                 error "setstripe -S failed"
7102         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
7103         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7104                 error "delete default stripe failed"
7105 }
7106 run_test 65g "directory setstripe -d"
7107
7108 test_65h() {
7109         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7110
7111         test_mkdir $DIR/$tdir
7112         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
7113
7114         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7115                 error "setstripe -S failed"
7116         test_mkdir $DIR/$tdir/dd1
7117         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
7118                 error "stripe info inherit failed"
7119 }
7120 run_test 65h "directory stripe info inherit ===================="
7121
7122 test_65i() {
7123         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7124
7125         save_layout_restore_at_exit $MOUNT
7126
7127         # bug6367: set non-default striping on root directory
7128         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
7129
7130         # bug12836: getstripe on -1 default directory striping
7131         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
7132
7133         # bug12836: getstripe -v on -1 default directory striping
7134         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
7135
7136         # bug12836: new find on -1 default directory striping
7137         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
7138 }
7139 run_test 65i "various tests to set root directory striping"
7140
7141 test_65j() { # bug6367
7142         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7143
7144         sync; sleep 1
7145
7146         # if we aren't already remounting for each test, do so for this test
7147         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
7148                 cleanup || error "failed to unmount"
7149                 setup
7150         fi
7151
7152         save_layout_restore_at_exit $MOUNT
7153
7154         $SETSTRIPE -d $MOUNT || error "setstripe failed"
7155 }
7156 run_test 65j "set default striping on root directory (bug 6367)="
7157
7158 cleanup_65k() {
7159         rm -rf $DIR/$tdir
7160         wait_delete_completed
7161         do_facet $SINGLEMDS "lctl set_param -n \
7162                 osp.$ost*MDT0000.max_create_count=$max_count"
7163         do_facet $SINGLEMDS "lctl set_param -n \
7164                 osp.$ost*MDT0000.create_count=$count"
7165         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7166         echo $INACTIVE_OSC "is Activate"
7167
7168         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7169 }
7170
7171 test_65k() { # bug11679
7172         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7173         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7174         remote_mds_nodsh && skip "remote MDS with nodsh"
7175
7176         local disable_precreate=true
7177         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
7178                 disable_precreate=false
7179
7180         echo "Check OST status: "
7181         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
7182                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
7183
7184         for OSC in $MDS_OSCS; do
7185                 echo $OSC "is active"
7186                 do_facet $SINGLEMDS lctl --device %$OSC activate
7187         done
7188
7189         for INACTIVE_OSC in $MDS_OSCS; do
7190                 local ost=$(osc_to_ost $INACTIVE_OSC)
7191                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
7192                                lov.*md*.target_obd |
7193                                awk -F: /$ost/'{ print $1 }' | head -n 1)
7194
7195                 mkdir -p $DIR/$tdir
7196                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
7197                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
7198
7199                 echo "Deactivate: " $INACTIVE_OSC
7200                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
7201
7202                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
7203                               osp.$ost*MDT0000.create_count")
7204                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
7205                                   osp.$ost*MDT0000.max_create_count")
7206                 $disable_precreate &&
7207                         do_facet $SINGLEMDS "lctl set_param -n \
7208                                 osp.$ost*MDT0000.max_create_count=0"
7209
7210                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
7211                         [ -f $DIR/$tdir/$idx ] && continue
7212                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
7213                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
7214                                 { cleanup_65k;
7215                                   error "setstripe $idx should succeed"; }
7216                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
7217                 done
7218                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
7219                 rmdir $DIR/$tdir
7220
7221                 do_facet $SINGLEMDS "lctl set_param -n \
7222                         osp.$ost*MDT0000.max_create_count=$max_count"
7223                 do_facet $SINGLEMDS "lctl set_param -n \
7224                         osp.$ost*MDT0000.create_count=$count"
7225                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
7226                 echo $INACTIVE_OSC "is Activate"
7227
7228                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
7229         done
7230 }
7231 run_test 65k "validate manual striping works properly with deactivated OSCs"
7232
7233 test_65l() { # bug 12836
7234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7235
7236         test_mkdir -p $DIR/$tdir/test_dir
7237         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
7238         $LFS find -mtime -1 $DIR/$tdir >/dev/null
7239 }
7240 run_test 65l "lfs find on -1 stripe dir ========================"
7241
7242 test_65m() {
7243         local layout=$(save_layout $MOUNT)
7244         $RUNAS $SETSTRIPE -c 2 $MOUNT && {
7245                 restore_layout $MOUNT $layout
7246                 error "setstripe should fail by non-root users"
7247         }
7248         true
7249 }
7250 run_test 65m "normal user can't set filesystem default stripe"
7251
7252 test_65n() {
7253         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.12.0) ]] ||
7254                 skip "Need MDS version at least 2.12.0"
7255         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7256
7257         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7258         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
7259         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
7260
7261         local root_layout=$(save_layout $MOUNT)
7262         stack_trap "restore_layout $MOUNT $root_layout" EXIT
7263
7264         # new subdirectory under root directory should not inherit
7265         # the default layout from root
7266         local dir1=$MOUNT/$tdir-1
7267         mkdir $dir1 || error "mkdir $dir1 failed"
7268         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
7269                 error "$dir1 shouldn't have LOV EA"
7270
7271         # delete the default layout on root directory
7272         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
7273
7274         local dir2=$MOUNT/$tdir-2
7275         mkdir $dir2 || error "mkdir $dir2 failed"
7276         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
7277                 error "$dir2 shouldn't have LOV EA"
7278
7279         # set a new striping pattern on root directory
7280         local def_stripe_size=$($LFS getstripe -S $MOUNT)
7281         local new_def_stripe_size=$((def_stripe_size * 2))
7282         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
7283                 error "set stripe size on $MOUNT failed"
7284
7285         # new file created in $dir2 should inherit the new stripe size from
7286         # the filesystem default
7287         local file2=$dir2/$tfile-2
7288         touch $file2 || error "touch $file2 failed"
7289
7290         local file2_stripe_size=$($LFS getstripe -S $file2)
7291         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
7292                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
7293
7294         local dir3=$MOUNT/$tdir-3
7295         mkdir $dir3 || error "mkdir $dir3 failed"
7296         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
7297         # the root layout, which is the actual default layout that will be used
7298         # when new files are created in $dir3.
7299         local dir3_layout=$(get_layout_param $dir3)
7300         local root_dir_layout=$(get_layout_param $MOUNT)
7301         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
7302                 error "$dir3 should show the default layout from $MOUNT"
7303
7304         # set OST pool on root directory
7305         local pool=$TESTNAME
7306         pool_add $pool || error "add $pool failed"
7307         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
7308                 error "add targets to $pool failed"
7309
7310         $LFS setstripe -p $pool $MOUNT ||
7311                 error "set OST pool on $MOUNT failed"
7312
7313         # new file created in $dir3 should inherit the pool from
7314         # the filesystem default
7315         local file3=$dir3/$tfile-3
7316         touch $file3 || error "touch $file3 failed"
7317
7318         local file3_pool=$($LFS getstripe -p $file3)
7319         [[ "$file3_pool" = "$pool" ]] ||
7320                 error "$file3 didn't inherit OST pool $pool"
7321
7322         local dir4=$MOUNT/$tdir-4
7323         mkdir $dir4 || error "mkdir $dir4 failed"
7324         local dir4_layout=$(get_layout_param $dir4)
7325         root_dir_layout=$(get_layout_param $MOUNT)
7326         echo "$LFS getstripe -d $dir4"
7327         $LFS getstripe -d $dir4
7328         echo "$LFS getstripe -d $MOUNT"
7329         $LFS getstripe -d $MOUNT
7330         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
7331                 error "$dir4 should show the default layout from $MOUNT"
7332
7333         # new file created in $dir4 should inherit the pool from
7334         # the filesystem default
7335         local file4=$dir4/$tfile-4
7336         touch $file4 || error "touch $file4 failed"
7337
7338         local file4_pool=$($LFS getstripe -p $file4)
7339         [[ "$file4_pool" = "$pool" ]] ||
7340                 error "$file4 didn't inherit OST pool $pool"
7341
7342         # new subdirectory under non-root directory should inherit
7343         # the default layout from its parent directory
7344         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
7345                 error "set directory layout on $dir4 failed"
7346
7347         local dir5=$dir4/$tdir-5
7348         mkdir $dir5 || error "mkdir $dir5 failed"
7349
7350         dir4_layout=$(get_layout_param $dir4)
7351         local dir5_layout=$(get_layout_param $dir5)
7352         [[ "$dir4_layout" = "$dir5_layout" ]] ||
7353                 error "$dir5 should inherit the default layout from $dir4"
7354
7355         # though subdir under ROOT doesn't inherit default layout, but
7356         # its sub dir/file should be created with default layout.
7357         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
7358         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
7359         [[ $MDS1_VERSION -ge $(version_code 2.12.3) &&
7360            $MDS1_VERSION -lt $(version_code 2.12.50) ]] ||
7361                 skip "Need MDS version at least 2.12.3 or 2.12.59"
7362
7363         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
7364         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
7365         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
7366
7367         if [ $default_lmv_hash == "none" ]; then
7368                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
7369         else
7370                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
7371                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
7372         fi
7373
7374         $LFS setdirstripe -D -c 2 $MOUNT ||
7375                 error "setdirstripe -D -c 2 failed"
7376         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
7377         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
7378         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
7379 }
7380 run_test 65n "don't inherit default layout from root for new subdirectories"
7381
7382 # bug 2543 - update blocks count on client
7383 test_66() {
7384         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7385
7386         COUNT=${COUNT:-8}
7387         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
7388         sync; sync_all_data; sync; sync_all_data
7389         cancel_lru_locks osc
7390         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
7391         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
7392 }
7393 run_test 66 "update inode blocks count on client ==============="
7394
7395 meminfo() {
7396         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
7397 }
7398
7399 swap_used() {
7400         swapon -s | awk '($1 == "'$1'") { print $4 }'
7401 }
7402
7403 # bug5265, obdfilter oa2dentry return -ENOENT
7404 # #define OBD_FAIL_SRV_ENOENT 0x217
7405 test_69() {
7406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7407         remote_ost_nodsh && skip "remote OST with nodsh"
7408
7409         f="$DIR/$tfile"
7410         $SETSTRIPE -c 1 -i 0 $f
7411
7412         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
7413
7414         do_facet ost1 lctl set_param fail_loc=0x217
7415         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
7416         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
7417
7418         do_facet ost1 lctl set_param fail_loc=0
7419         $DIRECTIO write $f 0 2 || error "write error"
7420
7421         cancel_lru_locks osc
7422         $DIRECTIO read $f 0 1 || error "read error"
7423
7424         do_facet ost1 lctl set_param fail_loc=0x217
7425         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
7426
7427         do_facet ost1 lctl set_param fail_loc=0
7428         rm -f $f
7429 }
7430 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
7431
7432 test_71() {
7433         test_mkdir $DIR/$tdir
7434         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
7435         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
7436 }
7437 run_test 71 "Running dbench on lustre (don't segment fault) ===="
7438
7439 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
7440         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7441         [ "$RUNAS_ID" = "$UID" ] &&
7442                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7443         # Check that testing environment is properly set up. Skip if not
7444         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
7445                 skip_env "User $RUNAS_ID does not exist - skipping"
7446
7447         touch $DIR/$tfile
7448         chmod 777 $DIR/$tfile
7449         chmod ug+s $DIR/$tfile
7450         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
7451                 error "$RUNAS dd $DIR/$tfile failed"
7452         # See if we are still setuid/sgid
7453         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7454                 error "S/gid is not dropped on write"
7455         # Now test that MDS is updated too
7456         cancel_lru_locks mdc
7457         test -u $DIR/$tfile -o -g $DIR/$tfile &&
7458                 error "S/gid is not dropped on MDS"
7459         rm -f $DIR/$tfile
7460 }
7461 run_test 72a "Test that remove suid works properly (bug5695) ===="
7462
7463 test_72b() { # bug 24226 -- keep mode setting when size is not changing
7464         local perm
7465
7466         [ "$RUNAS_ID" = "$UID" ] &&
7467                 skip_env "RUNAS_ID = UID = $UID -- skipping"
7468         [ "$RUNAS_ID" -eq 0 ] &&
7469                 skip_env "RUNAS_ID = 0 -- skipping"
7470         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7471         # Check that testing environment is properly set up. Skip if not
7472         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
7473                 skip_env "User $RUNAS_ID does not exist - skipping"
7474
7475         touch $DIR/${tfile}-f{g,u}
7476         test_mkdir $DIR/${tfile}-dg
7477         test_mkdir $DIR/${tfile}-du
7478         chmod 770 $DIR/${tfile}-{f,d}{g,u}
7479         chmod g+s $DIR/${tfile}-{f,d}g
7480         chmod u+s $DIR/${tfile}-{f,d}u
7481         for perm in 777 2777 4777; do
7482                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
7483                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
7484                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
7485                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
7486         done
7487         true
7488 }
7489 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
7490
7491 # bug 3462 - multiple simultaneous MDC requests
7492 test_73() {
7493         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7494
7495         test_mkdir $DIR/d73-1
7496         test_mkdir $DIR/d73-2
7497         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
7498         pid1=$!
7499
7500         lctl set_param fail_loc=0x80000129
7501         $MULTIOP $DIR/d73-1/f73-2 Oc &
7502         sleep 1
7503         lctl set_param fail_loc=0
7504
7505         $MULTIOP $DIR/d73-2/f73-3 Oc &
7506         pid3=$!
7507
7508         kill -USR1 $pid1
7509         wait $pid1 || return 1
7510
7511         sleep 25
7512
7513         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
7514         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
7515         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
7516
7517         rm -rf $DIR/d73-*
7518 }
7519 run_test 73 "multiple MDC requests (should not deadlock)"
7520
7521 test_74a() { # bug 6149, 6184
7522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7523
7524         touch $DIR/f74a
7525         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7526         #
7527         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7528         # will spin in a tight reconnection loop
7529         $LCTL set_param fail_loc=0x8000030e
7530         # get any lock that won't be difficult - lookup works.
7531         ls $DIR/f74a
7532         $LCTL set_param fail_loc=0
7533         rm -f $DIR/f74a
7534         true
7535 }
7536 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
7537
7538 test_74b() { # bug 13310
7539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7540
7541         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
7542         #
7543         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
7544         # will spin in a tight reconnection loop
7545         $LCTL set_param fail_loc=0x8000030e
7546         # get a "difficult" lock
7547         touch $DIR/f74b
7548         $LCTL set_param fail_loc=0
7549         rm -f $DIR/f74b
7550         true
7551 }
7552 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
7553
7554 test_74c() {
7555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7556
7557         #define OBD_FAIL_LDLM_NEW_LOCK
7558         $LCTL set_param fail_loc=0x319
7559         touch $DIR/$tfile && error "touch successful"
7560         $LCTL set_param fail_loc=0
7561         true
7562 }
7563 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
7564
7565 num_inodes() {
7566         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
7567 }
7568
7569 test_76() { # Now for bug 20433, added originally in bug 1443
7570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7571
7572         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
7573
7574         cancel_lru_locks osc
7575         BEFORE_INODES=$(num_inodes)
7576         echo "before inodes: $BEFORE_INODES"
7577         local COUNT=1000
7578         [ "$SLOW" = "no" ] && COUNT=100
7579         for i in $(seq $COUNT); do
7580                 touch $DIR/$tfile
7581                 rm -f $DIR/$tfile
7582         done
7583         cancel_lru_locks osc
7584         AFTER_INODES=$(num_inodes)
7585         echo "after inodes: $AFTER_INODES"
7586         local wait=0
7587         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
7588                 sleep 2
7589                 AFTER_INODES=$(num_inodes)
7590                 wait=$((wait+2))
7591                 echo "wait $wait seconds inodes: $AFTER_INODES"
7592                 if [ $wait -gt 30 ]; then
7593                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
7594                 fi
7595         done
7596 }
7597 run_test 76 "confirm clients recycle inodes properly ===="
7598
7599
7600 export ORIG_CSUM=""
7601 set_checksums()
7602 {
7603         # Note: in sptlrpc modes which enable its own bulk checksum, the
7604         # original crc32_le bulk checksum will be automatically disabled,
7605         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
7606         # will be checked by sptlrpc code against sptlrpc bulk checksum.
7607         # In this case set_checksums() will not be no-op, because sptlrpc
7608         # bulk checksum will be enabled all through the test.
7609
7610         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
7611         lctl set_param -n osc.*.checksums $1
7612         return 0
7613 }
7614
7615 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7616                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
7617 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
7618                              tr -d [] | head -n1)}
7619 set_checksum_type()
7620 {
7621         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
7622         log "set checksum type to $1"
7623         return 0
7624 }
7625 F77_TMP=$TMP/f77-temp
7626 F77SZ=8
7627 setup_f77() {
7628         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
7629                 error "error writing to $F77_TMP"
7630 }
7631
7632 test_77a() { # bug 10889
7633         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7634         $GSS && skip_env "could not run with gss"
7635
7636         [ ! -f $F77_TMP ] && setup_f77
7637         set_checksums 1
7638         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
7639         set_checksums 0
7640         rm -f $DIR/$tfile
7641 }
7642 run_test 77a "normal checksum read/write operation"
7643
7644 test_77b() { # bug 10889
7645         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7646         $GSS && skip_env "could not run with gss"
7647
7648         [ ! -f $F77_TMP ] && setup_f77
7649         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7650         $LCTL set_param fail_loc=0x80000409
7651         set_checksums 1
7652
7653         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7654                 error "dd error: $?"
7655         $LCTL set_param fail_loc=0
7656
7657         for algo in $CKSUM_TYPES; do
7658                 cancel_lru_locks osc
7659                 set_checksum_type $algo
7660                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7661                 $LCTL set_param fail_loc=0x80000408
7662                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
7663                 $LCTL set_param fail_loc=0
7664         done
7665         set_checksums 0
7666         set_checksum_type $ORIG_CSUM_TYPE
7667         rm -f $DIR/$tfile
7668 }
7669 run_test 77b "checksum error on client write, read"
7670
7671 cleanup_77c() {
7672         trap 0
7673         set_checksums 0
7674         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
7675         $check_ost &&
7676                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
7677         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
7678         $check_ost && [ -n "$ost_file_prefix" ] &&
7679                 do_facet ost1 rm -f ${ost_file_prefix}\*
7680 }
7681
7682 test_77c() {
7683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7684         $GSS && skip_env "could not run with gss"
7685         remote_ost_nodsh && skip "remote OST with nodsh"
7686
7687         local bad1
7688         local osc_file_prefix
7689         local osc_file
7690         local check_ost=false
7691         local ost_file_prefix
7692         local ost_file
7693         local orig_cksum
7694         local dump_cksum
7695         local fid
7696
7697         # ensure corruption will occur on first OSS/OST
7698         $LFS setstripe -i 0 $DIR/$tfile
7699
7700         [ ! -f $F77_TMP ] && setup_f77
7701         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
7702                 error "dd write error: $?"
7703         fid=$($LFS path2fid $DIR/$tfile)
7704
7705         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
7706         then
7707                 check_ost=true
7708                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
7709                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
7710         else
7711                 echo "OSS do not support bulk pages dump upon error"
7712         fi
7713
7714         osc_file_prefix=$($LCTL get_param -n debug_path)
7715         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
7716
7717         trap cleanup_77c EXIT
7718
7719         set_checksums 1
7720         # enable bulk pages dump upon error on Client
7721         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
7722         # enable bulk pages dump upon error on OSS
7723         $check_ost &&
7724                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
7725
7726         # flush Client cache to allow next read to reach OSS
7727         cancel_lru_locks osc
7728
7729         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
7730         $LCTL set_param fail_loc=0x80000408
7731         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
7732         $LCTL set_param fail_loc=0
7733
7734         rm -f $DIR/$tfile
7735
7736         # check cksum dump on Client
7737         osc_file=$(ls ${osc_file_prefix}*)
7738         [ -n "$osc_file" ] || error "no checksum dump file on Client"
7739         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
7740         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
7741         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
7742         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
7743                      cksum)
7744         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
7745         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7746                 error "dump content does not match on Client"
7747
7748         $check_ost || skip "No need to check cksum dump on OSS"
7749
7750         # check cksum dump on OSS
7751         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
7752         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
7753         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
7754         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
7755         [[ "$orig_cksum" == "$dump_cksum" ]] ||
7756                 error "dump content does not match on OSS"
7757
7758         cleanup_77c
7759 }
7760 run_test 77c "checksum error on client read with debug"
7761
7762 test_77d() { # bug 10889
7763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7764         $GSS && skip_env "could not run with gss"
7765
7766         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7767         $LCTL set_param fail_loc=0x80000409
7768         set_checksums 1
7769         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7770                 error "direct write: rc=$?"
7771         $LCTL set_param fail_loc=0
7772         set_checksums 0
7773
7774         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
7775         $LCTL set_param fail_loc=0x80000408
7776         set_checksums 1
7777         cancel_lru_locks osc
7778         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
7779                 error "direct read: rc=$?"
7780         $LCTL set_param fail_loc=0
7781         set_checksums 0
7782 }
7783 run_test 77d "checksum error on OST direct write, read"
7784
7785 test_77f() { # bug 10889
7786         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7787         $GSS && skip_env "could not run with gss"
7788
7789         set_checksums 1
7790         for algo in $CKSUM_TYPES; do
7791                 cancel_lru_locks osc
7792                 set_checksum_type $algo
7793                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
7794                 $LCTL set_param fail_loc=0x409
7795                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
7796                         error "direct write succeeded"
7797                 $LCTL set_param fail_loc=0
7798         done
7799         set_checksum_type $ORIG_CSUM_TYPE
7800         set_checksums 0
7801 }
7802 run_test 77f "repeat checksum error on write (expect error)"
7803
7804 test_77g() { # bug 10889
7805         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7806         $GSS && skip_env "could not run with gss"
7807         remote_ost_nodsh && skip "remote OST with nodsh"
7808
7809         [ ! -f $F77_TMP ] && setup_f77
7810
7811         local file=$DIR/$tfile
7812         stack_trap "rm -f $file" EXIT
7813
7814         $SETSTRIPE -c 1 -i 0 $file
7815         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
7816         do_facet ost1 lctl set_param fail_loc=0x8000021a
7817         set_checksums 1
7818         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
7819                 error "write error: rc=$?"
7820         do_facet ost1 lctl set_param fail_loc=0
7821         set_checksums 0
7822
7823         cancel_lru_locks osc
7824         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
7825         do_facet ost1 lctl set_param fail_loc=0x8000021b
7826         set_checksums 1
7827         cmp $F77_TMP $file || error "file compare failed"
7828         do_facet ost1 lctl set_param fail_loc=0
7829         set_checksums 0
7830 }
7831 run_test 77g "checksum error on OST write, read"
7832
7833 test_77k() { # LU-10906
7834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7835         $GSS && skip_env "could not run with gss"
7836
7837         local cksum_param="osc.$FSNAME*.checksums"
7838         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
7839         local checksum
7840         local i
7841
7842         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
7843         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
7844         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
7845                 EXIT
7846
7847         for i in 0 1; do
7848                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
7849                         error "failed to set checksum=$i on MGS"
7850                 wait_update $HOSTNAME "$get_checksum" $i
7851                 #remount
7852                 echo "remount client, checksum should be $i"
7853                 remount_client $MOUNT || "failed to remount client"
7854                 checksum=$(eval $get_checksum)
7855                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7856         done
7857
7858         for opt in "checksum" "nochecksum"; do
7859                 #remount with mount option
7860                 echo "remount client with option $opt, checksum should be $i"
7861                 umount_client $MOUNT || "failed to umount client"
7862                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
7863                         "failed to mount client with option '$opt'"
7864                 checksum=$(eval $get_checksum)
7865                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
7866                 i=$((i - 1))
7867         done
7868
7869         remount_client $MOUNT || "failed to remount client"
7870 }
7871 run_test 77k "enable/disable checksum correctly"
7872
7873 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
7874 rm -f $F77_TMP
7875 unset F77_TMP
7876
7877 cleanup_test_78() {
7878         trap 0
7879         rm -f $DIR/$tfile
7880 }
7881
7882 test_78() { # bug 10901
7883         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7884         remote_ost || skip_env "local OST"
7885
7886         NSEQ=5
7887         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
7888         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
7889         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
7890         echo "MemTotal: $MEMTOTAL"
7891
7892         # reserve 256MB of memory for the kernel and other running processes,
7893         # and then take 1/2 of the remaining memory for the read/write buffers.
7894         if [ $MEMTOTAL -gt 512 ] ;then
7895                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
7896         else
7897                 # for those poor memory-starved high-end clusters...
7898                 MEMTOTAL=$((MEMTOTAL / 2))
7899         fi
7900         echo "Mem to use for directio: $MEMTOTAL"
7901
7902         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
7903         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
7904         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
7905         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
7906                 head -n1)
7907         echo "Smallest OST: $SMALLESTOST"
7908         [[ $SMALLESTOST -lt 10240 ]] &&
7909                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
7910
7911         trap cleanup_test_78 EXIT
7912
7913         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
7914                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
7915
7916         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
7917         echo "File size: $F78SIZE"
7918         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
7919         for i in $(seq 1 $NSEQ); do
7920                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
7921                 echo directIO rdwr round $i of $NSEQ
7922                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
7923         done
7924
7925         cleanup_test_78
7926 }
7927 run_test 78 "handle large O_DIRECT writes correctly ============"
7928
7929 test_79() { # bug 12743
7930         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7931
7932         wait_delete_completed
7933
7934         BKTOTAL=$(calc_osc_kbytes kbytestotal)
7935         BKFREE=$(calc_osc_kbytes kbytesfree)
7936         BKAVAIL=$(calc_osc_kbytes kbytesavail)
7937
7938         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
7939         DFTOTAL=`echo $STRING | cut -d, -f1`
7940         DFUSED=`echo $STRING  | cut -d, -f2`
7941         DFAVAIL=`echo $STRING | cut -d, -f3`
7942         DFFREE=$(($DFTOTAL - $DFUSED))
7943
7944         ALLOWANCE=$((64 * $OSTCOUNT))
7945
7946         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
7947            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
7948                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
7949         fi
7950         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
7951            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
7952                 error "df free($DFFREE) mismatch OST free($BKFREE)"
7953         fi
7954         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
7955            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
7956                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
7957         fi
7958 }
7959 run_test 79 "df report consistency check ======================="
7960
7961 test_80() { # bug 10718
7962         remote_ost_nodsh && skip "remote OST with nodsh"
7963         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7964
7965         # relax strong synchronous semantics for slow backends like ZFS
7966         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
7967                 local soc="obdfilter.*.sync_lock_cancel"
7968                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
7969
7970                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
7971                 if [ -z "$save" ]; then
7972                         soc="obdfilter.*.sync_on_lock_cancel"
7973                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
7974                 fi
7975
7976                 if [ "$save" != "never" ]; then
7977                         local hosts=$(comma_list $(osts_nodes))
7978
7979                         do_nodes $hosts $LCTL set_param $soc=never
7980                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
7981                 fi
7982         fi
7983
7984         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
7985         sync; sleep 1; sync
7986         local before=$(date +%s)
7987         cancel_lru_locks osc
7988         local after=$(date +%s)
7989         local diff=$((after - before))
7990         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
7991
7992         rm -f $DIR/$tfile
7993 }
7994 run_test 80 "Page eviction is equally fast at high offsets too"
7995
7996 test_81a() { # LU-456
7997         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7998         remote_ost_nodsh && skip "remote OST with nodsh"
7999
8000         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8001         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8002         do_facet ost1 lctl set_param fail_loc=0x80000228
8003
8004         # write should trigger a retry and success
8005         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8006         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8007         RC=$?
8008         if [ $RC -ne 0 ] ; then
8009                 error "write should success, but failed for $RC"
8010         fi
8011 }
8012 run_test 81a "OST should retry write when get -ENOSPC ==============="
8013
8014 test_81b() { # LU-456
8015         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8016         remote_ost_nodsh && skip "remote OST with nodsh"
8017
8018         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8019         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8020         do_facet ost1 lctl set_param fail_loc=0x228
8021
8022         # write should retry several times and return -ENOSPC finally
8023         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
8024         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8025         RC=$?
8026         ENOSPC=28
8027         if [ $RC -ne $ENOSPC ] ; then
8028                 error "dd should fail for -ENOSPC, but succeed."
8029         fi
8030 }
8031 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8032
8033 test_82() { # LU-1031
8034         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8035         local gid1=14091995
8036         local gid2=16022000
8037
8038         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8039         local MULTIPID1=$!
8040         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
8041         local MULTIPID2=$!
8042         kill -USR1 $MULTIPID2
8043         sleep 2
8044         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
8045                 error "First grouplock does not block second one"
8046         else
8047                 echo "Second grouplock blocks first one"
8048         fi
8049         kill -USR1 $MULTIPID1
8050         wait $MULTIPID1
8051         wait $MULTIPID2
8052 }
8053 run_test 82 "Basic grouplock test"
8054
8055 test_99() {
8056         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
8057
8058         test_mkdir $DIR/$tdir.cvsroot
8059         chown $RUNAS_ID $DIR/$tdir.cvsroot
8060
8061         cd $TMP
8062         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
8063
8064         cd /etc/init.d
8065         # some versions of cvs import exit(1) when asked to import links or
8066         # files they can't read.  ignore those files.
8067         local toignore=$(find . -type l -printf '-I %f\n' -o \
8068                          ! -perm /4 -printf '-I %f\n')
8069         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
8070                 $tdir.reposname vtag rtag
8071
8072         cd $DIR
8073         test_mkdir $DIR/$tdir.reposname
8074         chown $RUNAS_ID $DIR/$tdir.reposname
8075         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
8076
8077         cd $DIR/$tdir.reposname
8078         $RUNAS touch foo99
8079         $RUNAS cvs add -m 'addmsg' foo99
8080         $RUNAS cvs update
8081         $RUNAS cvs commit -m 'nomsg' foo99
8082         rm -fr $DIR/$tdir.cvsroot
8083 }
8084 run_test 99 "cvs strange file/directory operations"
8085
8086 test_100() {
8087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8088         [[ "$NETTYPE" =~ tcp ]] ||
8089                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
8090         remote_ost_nodsh && skip "remote OST with nodsh"
8091         remote_mds_nodsh && skip "remote MDS with nodsh"
8092         remote_servers ||
8093                 skip "useless for local single node setup"
8094
8095         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
8096                 [ "$PROT" != "tcp" ] && continue
8097                 RPORT=$(echo $REMOTE | cut -d: -f2)
8098                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
8099
8100                 rc=0
8101                 LPORT=`echo $LOCAL | cut -d: -f2`
8102                 if [ $LPORT -ge 1024 ]; then
8103                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
8104                         netstat -tna
8105                         error_exit "local: $LPORT > 1024, remote: $RPORT"
8106                 fi
8107         done
8108         [ "$rc" = 0 ] || error_exit "privileged port not found" )
8109 }
8110 run_test 100 "check local port using privileged port ==========="
8111
8112 function get_named_value()
8113 {
8114     local tag
8115
8116     tag=$1
8117     while read ;do
8118         line=$REPLY
8119         case $line in
8120         $tag*)
8121             echo $line | sed "s/^$tag[ ]*//"
8122             break
8123             ;;
8124         esac
8125     done
8126 }
8127
8128 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
8129                    awk '/^max_cached_mb/ { print $2 }')
8130
8131 cleanup_101a() {
8132         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
8133         trap 0
8134 }
8135
8136 test_101a() {
8137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8138         [ $MDSCOUNT -ge 2 ] && skip_env "needs < 2 MDTs" #LU-4322
8139
8140         local s
8141         local discard
8142         local nreads=10000
8143         local cache_limit=32
8144
8145         $LCTL set_param -n osc.*-osc*.rpc_stats 0
8146         trap cleanup_101a EXIT
8147         $LCTL set_param -n llite.*.read_ahead_stats 0
8148         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
8149
8150         #
8151         # randomly read 10000 of 64K chunks from file 3x 32MB in size
8152         #
8153         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
8154         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
8155
8156         discard=0
8157         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
8158                 get_named_value 'read but discarded' | cut -d" " -f1); do
8159                         discard=$(($discard + $s))
8160         done
8161         cleanup_101a
8162
8163         if [[ $(($discard * 10)) -gt $nreads ]]; then
8164                 $LCTL get_param osc.*-osc*.rpc_stats
8165                 $LCTL get_param llite.*.read_ahead_stats
8166                 error "too many ($discard) discarded pages"
8167         fi
8168         rm -f $DIR/$tfile || true
8169 }
8170 run_test 101a "check read-ahead for random reads"
8171
8172 setup_test101bc() {
8173         test_mkdir $DIR/$tdir
8174         local STRIPE_SIZE=$1
8175         local FILE_LENGTH=$2
8176         STRIPE_OFFSET=0
8177
8178         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
8179
8180         local list=$(comma_list $(osts_nodes))
8181         set_osd_param $list '' read_cache_enable 0
8182         set_osd_param $list '' writethrough_cache_enable 0
8183
8184         trap cleanup_test101bc EXIT
8185         # prepare the read-ahead file
8186         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
8187
8188         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
8189                                 count=$FILE_SIZE_MB 2> /dev/null
8190
8191 }
8192
8193 cleanup_test101bc() {
8194         trap 0
8195         rm -rf $DIR/$tdir
8196         rm -f $DIR/$tfile
8197
8198         local list=$(comma_list $(osts_nodes))
8199         set_osd_param $list '' read_cache_enable 1
8200         set_osd_param $list '' writethrough_cache_enable 1
8201 }
8202
8203 calc_total() {
8204         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
8205 }
8206
8207 ra_check_101() {
8208         local READ_SIZE=$1
8209         local STRIPE_SIZE=$2
8210         local FILE_LENGTH=$3
8211         local RA_INC=1048576
8212         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
8213         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
8214                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
8215         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
8216                         get_named_value 'read but discarded' |
8217                         cut -d" " -f1 | calc_total)
8218         if [[ $DISCARD -gt $discard_limit ]]; then
8219                 $LCTL get_param llite.*.read_ahead_stats
8220                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
8221         else
8222                 echo "Read-ahead success for size ${READ_SIZE}"
8223         fi
8224 }
8225
8226 test_101b() {
8227         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8228         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8229
8230         local STRIPE_SIZE=1048576
8231         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
8232
8233         if [ $SLOW == "yes" ]; then
8234                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
8235         else
8236                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
8237         fi
8238
8239         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
8240
8241         # prepare the read-ahead file
8242         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8243         cancel_lru_locks osc
8244         for BIDX in 2 4 8 16 32 64 128 256
8245         do
8246                 local BSIZE=$((BIDX*4096))
8247                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
8248                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
8249                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
8250                 $LCTL set_param -n llite.*.read_ahead_stats 0
8251                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
8252                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
8253                 cancel_lru_locks osc
8254                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
8255         done
8256         cleanup_test101bc
8257         true
8258 }
8259 run_test 101b "check stride-io mode read-ahead ================="
8260
8261 test_101c() {
8262         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8263
8264         local STRIPE_SIZE=1048576
8265         local FILE_LENGTH=$((STRIPE_SIZE*100))
8266         local nreads=10000
8267         local osc_rpc_stats
8268
8269         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
8270
8271         cancel_lru_locks osc
8272         $LCTL set_param osc.*.rpc_stats 0
8273         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
8274         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
8275                 local stats=$($LCTL get_param -n $osc_rpc_stats)
8276                 local lines=$(echo "$stats" | awk 'END {print NR;}')
8277                 local size
8278
8279                 if [ $lines -le 20 ]; then
8280                         continue
8281                 fi
8282                 for size in 1 2 4 8; do
8283                         local rpc=$(echo "$stats" |
8284                                     awk '($1 == "'$size':") {print $2; exit; }')
8285                         [ $rpc != 0 ] &&
8286                                 error "Small $((size*4))k read IO $rpc !"
8287                 done
8288                 echo "$osc_rpc_stats check passed!"
8289         done
8290         cleanup_test101bc
8291         true
8292 }
8293 run_test 101c "check stripe_size aligned read-ahead ================="
8294
8295 set_read_ahead() {
8296         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
8297         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
8298 }
8299
8300 test_101d() {
8301         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8302
8303         local file=$DIR/$tfile
8304         local sz_MB=${FILESIZE_101d:-500}
8305         local ra_MB=${READAHEAD_MB:-40}
8306
8307         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
8308         [ $free_MB -lt $sz_MB ] &&
8309                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
8310
8311         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
8312         $SETSTRIPE -c -1 $file || error "setstripe failed"
8313
8314         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
8315         echo Cancel LRU locks on lustre client to flush the client cache
8316         cancel_lru_locks osc
8317
8318         echo Disable read-ahead
8319         local old_READAHEAD=$(set_read_ahead 0)
8320
8321         echo Reading the test file $file with read-ahead disabled
8322         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8323
8324         echo Cancel LRU locks on lustre client to flush the client cache
8325         cancel_lru_locks osc
8326         echo Enable read-ahead with ${ra_MB}MB
8327         set_read_ahead $ra_MB
8328
8329         echo Reading the test file $file with read-ahead enabled
8330         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
8331
8332         echo "read-ahead disabled time read $raOFF"
8333         echo "read-ahead enabled  time read $raON"
8334
8335         set_read_ahead $old_READAHEAD
8336         rm -f $file
8337         wait_delete_completed
8338
8339         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
8340                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
8341 }
8342 run_test 101d "file read with and without read-ahead enabled"
8343
8344 test_101e() {
8345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8346
8347         local file=$DIR/$tfile
8348         local size_KB=500  #KB
8349         local count=100
8350         local bsize=1024
8351
8352         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
8353         local need_KB=$((count * size_KB))
8354         [[ $free_KB -le $need_KB ]] &&
8355                 skip_env "Need free space $need_KB, have $free_KB"
8356
8357         echo "Creating $count ${size_KB}K test files"
8358         for ((i = 0; i < $count; i++)); do
8359                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
8360         done
8361
8362         echo "Cancel LRU locks on lustre client to flush the client cache"
8363         cancel_lru_locks $OSC
8364
8365         echo "Reset readahead stats"
8366         $LCTL set_param -n llite.*.read_ahead_stats 0
8367
8368         for ((i = 0; i < $count; i++)); do
8369                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
8370         done
8371
8372         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8373                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
8374
8375         for ((i = 0; i < $count; i++)); do
8376                 rm -rf $file.$i 2>/dev/null
8377         done
8378
8379         #10000 means 20% reads are missing in readahead
8380         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
8381 }
8382 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
8383
8384 test_101f() {
8385         which iozone || skip_env "no iozone installed"
8386
8387         local old_debug=$($LCTL get_param debug)
8388         old_debug=${old_debug#*=}
8389         $LCTL set_param debug="reada mmap"
8390
8391         # create a test file
8392         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
8393
8394         echo Cancel LRU locks on lustre client to flush the client cache
8395         cancel_lru_locks osc
8396
8397         echo Reset readahead stats
8398         $LCTL set_param -n llite.*.read_ahead_stats 0
8399
8400         echo mmap read the file with small block size
8401         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
8402                 > /dev/null 2>&1
8403
8404         echo checking missing pages
8405         $LCTL get_param llite.*.read_ahead_stats
8406         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
8407                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
8408
8409         $LCTL set_param debug="$old_debug"
8410         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
8411         rm -f $DIR/$tfile
8412 }
8413 run_test 101f "check mmap read performance"
8414
8415 test_101g_brw_size_test() {
8416         local mb=$1
8417         local pages=$((mb * 1048576 / PAGE_SIZE))
8418         local file=$DIR/$tfile
8419
8420         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
8421                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
8422         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
8423                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
8424                         return 2
8425         done
8426
8427         stack_trap "rm -f $file" EXIT
8428         $LCTL set_param -n osc.*.rpc_stats=0
8429
8430         # 10 RPCs should be enough for the test
8431         local count=10
8432         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
8433                 { error "dd write ${mb} MB blocks failed"; return 3; }
8434         cancel_lru_locks osc
8435         dd of=/dev/null if=$file bs=${mb}M count=$count ||
8436                 { error "dd write ${mb} MB blocks failed"; return 4; }
8437
8438         # calculate number of full-sized read and write RPCs
8439         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
8440                 sed -n '/pages per rpc/,/^$/p' |
8441                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
8442                 END { print reads,writes }'))
8443         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
8444                 return 5
8445         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
8446                 return 6
8447
8448         return 0
8449 }
8450
8451 test_101g() {
8452         remote_ost_nodsh && skip "remote OST with nodsh"
8453
8454         local rpcs
8455         local osts=$(get_facets OST)
8456         local list=$(comma_list $(osts_nodes))
8457         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
8458         local brw_size="obdfilter.*.brw_size"
8459
8460         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8461
8462         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
8463         if [ $OST1_VERSION -ge $(version_code 2.8.52) -o \
8464              \( $OST1_VERSION -ge $(version_code 2.7.17) -a \
8465                 $OST1_VERSION -lt $(version_code 2.7.50) \) ] &&
8466            [ $CLIENT_VERSION -ge $(version_code 2.8.52) -o \
8467              \( $CLIENT_VERSION -ge $(version_code 2.7.17) -a \
8468                 $CLIENT_VERSION -lt $(version_code 2.7.50) \) ]; then
8469                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] && suffix="M"
8470                 if [[ $orig_mb -lt 16 ]]; then
8471                         save_lustre_params $osts "$brw_size" > $p
8472                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
8473                                 error "set 16MB RPC size failed"
8474
8475                         echo "remount client to enable new RPC size"
8476                         remount_client $MOUNT || error "remount_client failed"
8477                 fi
8478
8479                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
8480                 # should be able to set brw_size=12, but no rpc_stats for that
8481                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
8482         fi
8483
8484         test_101g_brw_size_test 4 || error "4MB RPC test failed"
8485
8486         if [[ $orig_mb -lt 16 ]]; then
8487                 restore_lustre_params < $p
8488                 remount_client $MOUNT || error "remount_client restore failed"
8489         fi
8490
8491         rm -f $p $DIR/$tfile
8492 }
8493 run_test 101g "Big bulk(4/16 MiB) readahead"
8494
8495 setup_test102() {
8496         test_mkdir $DIR/$tdir
8497         chown $RUNAS_ID $DIR/$tdir
8498         STRIPE_SIZE=65536
8499         STRIPE_OFFSET=1
8500         STRIPE_COUNT=$OSTCOUNT
8501         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
8502
8503         trap cleanup_test102 EXIT
8504         cd $DIR
8505         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
8506         cd $DIR/$tdir
8507         for num in 1 2 3 4; do
8508                 for count in $(seq 1 $STRIPE_COUNT); do
8509                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
8510                                 local size=`expr $STRIPE_SIZE \* $num`
8511                                 local file=file"$num-$idx-$count"
8512                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
8513                         done
8514                 done
8515         done
8516
8517         cd $DIR
8518         $1 tar cf $TMP/f102.tar $tdir --xattrs
8519 }
8520
8521 cleanup_test102() {
8522         trap 0
8523         rm -f $TMP/f102.tar
8524         rm -rf $DIR/d0.sanity/d102
8525 }
8526
8527 test_102a() {
8528         [ "$UID" != 0 ] && skip "must run as root"
8529         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
8530                 skip_env "must have user_xattr"
8531
8532         [ -z "$(which setfattr 2>/dev/null)" ] &&
8533                 skip_env "could not find setfattr"
8534
8535         local testfile=$DIR/$tfile
8536
8537         touch $testfile
8538         echo "set/get xattr..."
8539         setfattr -n trusted.name1 -v value1 $testfile ||
8540                 error "setfattr -n trusted.name1=value1 $testfile failed"
8541         getfattr -n trusted.name1 $testfile 2> /dev/null |
8542           grep "trusted.name1=.value1" ||
8543                 error "$testfile missing trusted.name1=value1"
8544
8545         setfattr -n user.author1 -v author1 $testfile ||
8546                 error "setfattr -n user.author1=author1 $testfile failed"
8547         getfattr -n user.author1 $testfile 2> /dev/null |
8548           grep "user.author1=.author1" ||
8549                 error "$testfile missing trusted.author1=author1"
8550
8551         echo "listxattr..."
8552         setfattr -n trusted.name2 -v value2 $testfile ||
8553                 error "$testfile unable to set trusted.name2"
8554         setfattr -n trusted.name3 -v value3 $testfile ||
8555                 error "$testfile unable to set trusted.name3"
8556         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
8557             grep "trusted.name" | wc -l) -eq 3 ] ||
8558                 error "$testfile missing 3 trusted.name xattrs"
8559
8560         setfattr -n user.author2 -v author2 $testfile ||
8561                 error "$testfile unable to set user.author2"
8562         setfattr -n user.author3 -v author3 $testfile ||
8563                 error "$testfile unable to set user.author3"
8564         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
8565             grep "user.author" | wc -l) -eq 3 ] ||
8566                 error "$testfile missing 3 user.author xattrs"
8567
8568         echo "remove xattr..."
8569         setfattr -x trusted.name1 $testfile ||
8570                 error "$testfile error deleting trusted.name1"
8571         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
8572                 error "$testfile did not delete trusted.name1 xattr"
8573
8574         setfattr -x user.author1 $testfile ||
8575                 error "$testfile error deleting user.author1"
8576         echo "set lustre special xattr ..."
8577         $LFS setstripe -c1 $testfile
8578         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
8579                 awk -F "=" '/trusted.lov/ { print $2 }' )
8580         setfattr -n "trusted.lov" -v $lovea $testfile ||
8581                 error "$testfile doesn't ignore setting trusted.lov again"
8582         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
8583                 error "$testfile allow setting invalid trusted.lov"
8584         rm -f $testfile
8585 }
8586 run_test 102a "user xattr test =================================="
8587
8588 test_102b() {
8589         [ -z "$(which setfattr 2>/dev/null)" ] &&
8590                 skip_env "could not find setfattr"
8591         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8592
8593         # b10930: get/set/list trusted.lov xattr
8594         echo "get/set/list trusted.lov xattr ..."
8595         local testfile=$DIR/$tfile
8596         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8597                 error "setstripe failed"
8598         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
8599                 error "getstripe failed"
8600         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
8601                 error "can't get trusted.lov from $testfile"
8602
8603         local testfile2=${testfile}2
8604         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
8605                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
8606
8607         $MCREATE $testfile2
8608         setfattr -n trusted.lov -v $value $testfile2
8609         local stripe_size=$($GETSTRIPE -S $testfile2)
8610         local stripe_count=$($GETSTRIPE -c $testfile2)
8611         [[ $stripe_size -eq 65536 ]] ||
8612                 error "stripe size $stripe_size != 65536"
8613         [[ $stripe_count -eq $STRIPECOUNT ]] ||
8614                 error "stripe count $stripe_count != $STRIPECOUNT"
8615         rm -f $DIR/$tfile
8616 }
8617 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
8618
8619 test_102c() {
8620         [ -z "$(which setfattr 2>/dev/null)" ] &&
8621                 skip_env "could not find setfattr"
8622         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8623
8624         # b10930: get/set/list lustre.lov xattr
8625         echo "get/set/list lustre.lov xattr ..."
8626         test_mkdir $DIR/$tdir
8627         chown $RUNAS_ID $DIR/$tdir
8628         local testfile=$DIR/$tdir/$tfile
8629         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
8630                 error "setstripe failed"
8631         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
8632                 error "getstripe failed"
8633         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
8634         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
8635
8636         local testfile2=${testfile}2
8637         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
8638                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
8639
8640         $RUNAS $MCREATE $testfile2
8641         $RUNAS setfattr -n lustre.lov -v $value $testfile2
8642         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
8643         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
8644         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
8645         [ $stripe_count -eq $STRIPECOUNT ] ||
8646                 error "stripe count $stripe_count != $STRIPECOUNT"
8647 }
8648 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
8649
8650 compare_stripe_info1() {
8651         local stripe_index_all_zero=true
8652
8653         for num in 1 2 3 4; do
8654                 for count in $(seq 1 $STRIPE_COUNT); do
8655                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
8656                                 local size=$((STRIPE_SIZE * num))
8657                                 local file=file"$num-$offset-$count"
8658                                 stripe_size=$($LFS getstripe -S $PWD/$file)
8659                                 [[ $stripe_size -ne $size ]] &&
8660                                     error "$file: size $stripe_size != $size"
8661                                 stripe_count=$($LFS getstripe -c $PWD/$file)
8662                                 # allow fewer stripes to be created, ORI-601
8663                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
8664                                     error "$file: count $stripe_count != $count"
8665                                 stripe_index=$($LFS getstripe -i $PWD/$file)
8666                                 [[ $stripe_index -ne 0 ]] &&
8667                                         stripe_index_all_zero=false
8668                         done
8669                 done
8670         done
8671         $stripe_index_all_zero &&
8672                 error "all files are being extracted starting from OST index 0"
8673         return 0
8674 }
8675
8676 have_xattrs_include() {
8677         tar --help | grep -q xattrs-include &&
8678                 echo --xattrs-include="lustre.*"
8679 }
8680
8681 test_102d() {
8682         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8683         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8684
8685         XINC=$(have_xattrs_include)
8686         setup_test102
8687         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8688         cd $DIR/$tdir/$tdir
8689         compare_stripe_info1
8690 }
8691 run_test 102d "tar restore stripe info from tarfile,not keep osts"
8692
8693 test_102f() {
8694         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8695         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8696
8697         XINC=$(have_xattrs_include)
8698         setup_test102
8699         test_mkdir $DIR/$tdir.restore
8700         cd $DIR
8701         tar cf - --xattrs $tdir | tar xf - \
8702                 -C $DIR/$tdir.restore --xattrs $XINC
8703         cd $DIR/$tdir.restore/$tdir
8704         compare_stripe_info1
8705 }
8706 run_test 102f "tar copy files, not keep osts"
8707
8708 grow_xattr() {
8709         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
8710                 skip "must have user_xattr"
8711         [ -z "$(which setfattr 2>/dev/null)" ] &&
8712                 skip_env "could not find setfattr"
8713         [ -z "$(which getfattr 2>/dev/null)" ] &&
8714                 skip_env "could not find getfattr"
8715
8716         local xsize=${1:-1024}  # in bytes
8717         local file=$DIR/$tfile
8718         local value="$(generate_string $xsize)"
8719         local xbig=trusted.big
8720
8721         touch $file
8722         log "save $xbig on $file"
8723         setfattr -n $xbig -v $value $file ||
8724                 error "saving $xbig on $file failed"
8725
8726         local orig=$(get_xattr_value $xbig $file)
8727         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
8728
8729         local xsml=trusted.sml
8730         log "save $xsml on $file"
8731         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
8732
8733         local new=$(get_xattr_value $xbig $file)
8734         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
8735
8736         log "grow $xsml on $file"
8737         setfattr -n $xsml -v "$value" $file ||
8738                 error "growing $xsml on $file failed"
8739
8740         new=$(get_xattr_value $xbig $file)
8741         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
8742         log "$xbig still valid after growing $xsml"
8743
8744         rm -f $file
8745 }
8746
8747 test_102h() { # bug 15777
8748         grow_xattr 1024
8749 }
8750 run_test 102h "grow xattr from inside inode to external block"
8751
8752 test_102ha() {
8753         large_xattr_enabled || skip_env "ea_inode feature disabled"
8754
8755         grow_xattr $(max_xattr_size)
8756 }
8757 run_test 102ha "grow xattr from inside inode to external inode"
8758
8759 test_102i() { # bug 17038
8760         [ -z "$(which getfattr 2>/dev/null)" ] &&
8761                 skip "could not find getfattr"
8762
8763         touch $DIR/$tfile
8764         ln -s $DIR/$tfile $DIR/${tfile}link
8765         getfattr -n trusted.lov $DIR/$tfile ||
8766                 error "lgetxattr on $DIR/$tfile failed"
8767         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
8768                 grep -i "no such attr" ||
8769                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
8770         rm -f $DIR/$tfile $DIR/${tfile}link
8771 }
8772 run_test 102i "lgetxattr test on symbolic link ============"
8773
8774 test_102j() {
8775         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8776         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8777
8778         XINC=$(have_xattrs_include)
8779         setup_test102 "$RUNAS"
8780         chown $RUNAS_ID $DIR/$tdir
8781         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
8782         cd $DIR/$tdir/$tdir
8783         compare_stripe_info1 "$RUNAS"
8784 }
8785 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
8786
8787 test_102k() {
8788         [ -z "$(which setfattr 2>/dev/null)" ] &&
8789                 skip "could not find setfattr"
8790
8791         touch $DIR/$tfile
8792         # b22187 just check that does not crash for regular file.
8793         setfattr -n trusted.lov $DIR/$tfile
8794         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
8795         local test_kdir=$DIR/$tdir
8796         test_mkdir $test_kdir
8797         local default_size=$($LFS getstripe -S $test_kdir)
8798         local default_count=$($LFS getstripe -c $test_kdir)
8799         local default_offset=$($LFS getstripe -i $test_kdir)
8800         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
8801                 error 'dir setstripe failed'
8802         setfattr -n trusted.lov $test_kdir
8803         local stripe_size=$($LFS getstripe -S $test_kdir)
8804         local stripe_count=$($LFS getstripe -c $test_kdir)
8805         local stripe_offset=$($LFS getstripe -i $test_kdir)
8806         [ $stripe_size -eq $default_size ] ||
8807                 error "stripe size $stripe_size != $default_size"
8808         [ $stripe_count -eq $default_count ] ||
8809                 error "stripe count $stripe_count != $default_count"
8810         [ $stripe_offset -eq $default_offset ] ||
8811                 error "stripe offset $stripe_offset != $default_offset"
8812         rm -rf $DIR/$tfile $test_kdir
8813 }
8814 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
8815
8816 test_102l() {
8817         [ -z "$(which getfattr 2>/dev/null)" ] &&
8818                 skip "could not find getfattr"
8819
8820         # LU-532 trusted. xattr is invisible to non-root
8821         local testfile=$DIR/$tfile
8822
8823         touch $testfile
8824
8825         echo "listxattr as user..."
8826         chown $RUNAS_ID $testfile
8827         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
8828             grep -q "trusted" &&
8829                 error "$testfile trusted xattrs are user visible"
8830
8831         return 0;
8832 }
8833 run_test 102l "listxattr size test =================================="
8834
8835 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
8836         local path=$DIR/$tfile
8837         touch $path
8838
8839         listxattr_size_check $path || error "listattr_size_check $path failed"
8840 }
8841 run_test 102m "Ensure listxattr fails on small bufffer ========"
8842
8843 cleanup_test102
8844
8845 getxattr() { # getxattr path name
8846         # Return the base64 encoding of the value of xattr name on path.
8847         local path=$1
8848         local name=$2
8849
8850         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
8851         # file: $path
8852         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8853         #
8854         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
8855
8856         getfattr --absolute-names --encoding=base64 --name=$name $path |
8857                 awk -F= -v name=$name '$1 == name {
8858                         print substr($0, index($0, "=") + 1);
8859         }'
8860 }
8861
8862 test_102n() { # LU-4101 mdt: protect internal xattrs
8863         [ -z "$(which setfattr 2>/dev/null)" ] &&
8864                 skip "could not find setfattr"
8865         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
8866         then
8867                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
8868         fi
8869
8870         local file0=$DIR/$tfile.0
8871         local file1=$DIR/$tfile.1
8872         local xattr0=$TMP/$tfile.0
8873         local xattr1=$TMP/$tfile.1
8874         local namelist="lov lma lmv link fid version som hsm"
8875         local name
8876         local value
8877
8878         rm -rf $file0 $file1 $xattr0 $xattr1
8879         touch $file0 $file1
8880
8881         # Get 'before' xattrs of $file1.
8882         getfattr --absolute-names --dump --match=- $file1 > $xattr0
8883
8884         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
8885                 namelist+=" lfsck_namespace"
8886         for name in $namelist; do
8887                 # Try to copy xattr from $file0 to $file1.
8888                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8889
8890                 setfattr --name=trusted.$name --value="$value" $file1 ||
8891                         error "setxattr 'trusted.$name' failed"
8892
8893                 # Try to set a garbage xattr.
8894                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8895
8896                 if [[ x$name == "xlov" ]]; then
8897                         setfattr --name=trusted.lov --value="$value" $file1 &&
8898                         error "setxattr invalid 'trusted.lov' success"
8899                 else
8900                         setfattr --name=trusted.$name --value="$value" $file1 ||
8901                                 error "setxattr invalid 'trusted.$name' failed"
8902                 fi
8903
8904                 # Try to remove the xattr from $file1. We don't care if this
8905                 # appears to succeed or fail, we just don't want there to be
8906                 # any changes or crashes.
8907                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8908         done
8909
8910         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
8911         then
8912                 name="lfsck_ns"
8913                 # Try to copy xattr from $file0 to $file1.
8914                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
8915
8916                 setfattr --name=trusted.$name --value="$value" $file1 ||
8917                         error "setxattr 'trusted.$name' failed"
8918
8919                 # Try to set a garbage xattr.
8920                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
8921
8922                 setfattr --name=trusted.$name --value="$value" $file1 ||
8923                         error "setxattr 'trusted.$name' failed"
8924
8925                 # Try to remove the xattr from $file1. We don't care if this
8926                 # appears to succeed or fail, we just don't want there to be
8927                 # any changes or crashes.
8928                 setfattr --remove=$trusted.$name $file1 2> /dev/null
8929         fi
8930
8931         # Get 'after' xattrs of file1.
8932         getfattr --absolute-names --dump --match=- $file1 > $xattr1
8933
8934         if ! diff $xattr0 $xattr1; then
8935                 error "before and after xattrs of '$file1' differ"
8936         fi
8937
8938         rm -rf $file0 $file1 $xattr0 $xattr1
8939
8940         return 0
8941 }
8942 run_test 102n "silently ignore setxattr on internal trusted xattrs"
8943
8944 test_102p() { # LU-4703 setxattr did not check ownership
8945         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
8946                 skip "MDS needs to be at least 2.5.56"
8947
8948         local testfile=$DIR/$tfile
8949
8950         touch $testfile
8951
8952         echo "setfacl as user..."
8953         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
8954         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
8955
8956         echo "setfattr as user..."
8957         setfacl -m "u:$RUNAS_ID:---" $testfile
8958         $RUNAS setfattr -x system.posix_acl_access $testfile
8959         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
8960 }
8961 run_test 102p "check setxattr(2) correctly fails without permission"
8962
8963 test_102q() {
8964         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
8965                 skip "MDS needs to be at least 2.6.92"
8966
8967         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
8968 }
8969 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
8970
8971 test_102r() {
8972         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
8973                 skip "MDS needs to be at least 2.6.93"
8974
8975         touch $DIR/$tfile || error "touch"
8976         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
8977         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
8978         rm $DIR/$tfile || error "rm"
8979
8980         #normal directory
8981         mkdir -p $DIR/$tdir || error "mkdir"
8982         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8983         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8984         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8985                 error "$testfile error deleting user.author1"
8986         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8987                 grep "user.$(basename $tdir)" &&
8988                 error "$tdir did not delete user.$(basename $tdir)"
8989         rmdir $DIR/$tdir || error "rmdir"
8990
8991         #striped directory
8992         test_mkdir $DIR/$tdir
8993         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
8994         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
8995         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
8996                 error "$testfile error deleting user.author1"
8997         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
8998                 grep "user.$(basename $tdir)" &&
8999                 error "$tdir did not delete user.$(basename $tdir)"
9000         rmdir $DIR/$tdir || error "rm striped dir"
9001 }
9002 run_test 102r "set EAs with empty values"
9003
9004 test_102s() {
9005         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9006                 skip "MDS needs to be at least 2.11.52"
9007
9008         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9009
9010         save_lustre_params client "llite.*.xattr_cache" > $save
9011
9012         for cache in 0 1; do
9013                 lctl set_param llite.*.xattr_cache=$cache
9014
9015                 rm -f $DIR/$tfile
9016                 touch $DIR/$tfile || error "touch"
9017                 for prefix in lustre security system trusted user; do
9018                         # Note getxattr() may fail with 'Operation not
9019                         # supported' or 'No such attribute' depending
9020                         # on prefix and cache.
9021                         getfattr -n $prefix.n102s $DIR/$tfile &&
9022                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
9023                 done
9024         done
9025
9026         restore_lustre_params < $save
9027 }
9028 run_test 102s "getting nonexistent xattrs should fail"
9029
9030 test_102t() {
9031         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
9032                 skip "MDS needs to be at least 2.11.52"
9033
9034         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
9035
9036         save_lustre_params client "llite.*.xattr_cache" > $save
9037
9038         for cache in 0 1; do
9039                 lctl set_param llite.*.xattr_cache=$cache
9040
9041                 for buf_size in 0 256; do
9042                         rm -f $DIR/$tfile
9043                         touch $DIR/$tfile || error "touch"
9044                         setfattr -n user.multiop $DIR/$tfile
9045                         $MULTIOP $DIR/$tfile oa$buf_size ||
9046                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
9047                 done
9048         done
9049
9050         restore_lustre_params < $save
9051 }
9052 run_test 102t "zero length xattr values handled correctly"
9053
9054 run_acl_subtest()
9055 {
9056     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
9057     return $?
9058 }
9059
9060 test_103a() {
9061         [ "$UID" != 0 ] && skip "must run as root"
9062         $GSS && skip_env "could not run under gss"
9063         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
9064                 skip_env "must have acl enabled"
9065         [ -z "$(which setfacl 2>/dev/null)" ] &&
9066                 skip_env "could not find setfacl"
9067         remote_mds_nodsh && skip "remote MDS with nodsh"
9068
9069         gpasswd -a daemon bin                           # LU-5641
9070         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
9071
9072         declare -a identity_old
9073
9074         for num in $(seq $MDSCOUNT); do
9075                 switch_identity $num true || identity_old[$num]=$?
9076         done
9077
9078         SAVE_UMASK=$(umask)
9079         umask 0022
9080         mkdir -p $DIR/$tdir
9081         cd $DIR/$tdir
9082
9083         echo "performing cp ..."
9084         run_acl_subtest cp || error "run_acl_subtest cp failed"
9085         echo "performing getfacl-noacl..."
9086         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
9087         echo "performing misc..."
9088         run_acl_subtest misc || error  "misc test failed"
9089         echo "performing permissions..."
9090         run_acl_subtest permissions || error "permissions failed"
9091         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
9092         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
9093                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
9094                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
9095         then
9096                 echo "performing permissions xattr..."
9097                 run_acl_subtest permissions_xattr ||
9098                         error "permissions_xattr failed"
9099         fi
9100         echo "performing setfacl..."
9101         run_acl_subtest setfacl || error  "setfacl test failed"
9102
9103         # inheritance test got from HP
9104         echo "performing inheritance..."
9105         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
9106         chmod +x make-tree || error "chmod +x failed"
9107         run_acl_subtest inheritance || error "inheritance test failed"
9108         rm -f make-tree
9109
9110         echo "LU-974 ignore umask when acl is enabled..."
9111         run_acl_subtest 974 || error "LU-974 umask test failed"
9112         if [ $MDSCOUNT -ge 2 ]; then
9113                 run_acl_subtest 974_remote ||
9114                         error "LU-974 umask test failed under remote dir"
9115         fi
9116
9117         echo "LU-2561 newly created file is same size as directory..."
9118         if [ "$mds1_FSTYPE" != "zfs" ]; then
9119                 run_acl_subtest 2561 || error "LU-2561 test failed"
9120         else
9121                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
9122         fi
9123
9124         run_acl_subtest 4924 || error "LU-4924 test failed"
9125
9126         cd $SAVE_PWD
9127         umask $SAVE_UMASK
9128
9129         for num in $(seq $MDSCOUNT); do
9130                 if [ "${identity_old[$num]}" = 1 ]; then
9131                         switch_identity $num false || identity_old[$num]=$?
9132                 fi
9133         done
9134 }
9135 run_test 103a "acl test"
9136
9137 test_103b() {
9138         declare -a pids
9139         local U
9140
9141         for U in {0..511}; do
9142                 {
9143                 local O=$(printf "%04o" $U)
9144
9145                 umask $(printf "%04o" $((511 ^ $O)))
9146                 $LFS setstripe -c 1 $DIR/$tfile.s$O
9147                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
9148
9149                 (( $S == ($O & 0666) )) ||
9150                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
9151
9152                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
9153                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
9154                 (( $S == ($O & 0666) )) ||
9155                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
9156
9157                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
9158                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
9159                 (( $S == ($O & 0666) )) ||
9160                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
9161                 rm -f $DIR/$tfile.[smp]$0
9162                 } &
9163                 local pid=$!
9164
9165                 # limit the concurrently running threads to 64. LU-11878
9166                 local idx=$((U % 64))
9167                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
9168                 pids[idx]=$pid
9169         done
9170         wait
9171 }
9172 run_test 103b "umask lfs setstripe"
9173
9174 test_103c() {
9175         mkdir -p $DIR/$tdir
9176         cp -rp $DIR/$tdir $DIR/$tdir.bak
9177
9178         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
9179                 error "$DIR/$tdir shouldn't contain default ACL"
9180         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
9181                 error "$DIR/$tdir.bak shouldn't contain default ACL"
9182         true
9183 }
9184 run_test 103c "'cp -rp' won't set empty acl"
9185
9186 test_104a() {
9187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9188
9189         touch $DIR/$tfile
9190         lfs df || error "lfs df failed"
9191         lfs df -ih || error "lfs df -ih failed"
9192         lfs df -h $DIR || error "lfs df -h $DIR failed"
9193         lfs df -i $DIR || error "lfs df -i $DIR failed"
9194         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
9195         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
9196
9197         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
9198         lctl --device %$OSC deactivate
9199         lfs df || error "lfs df with deactivated OSC failed"
9200         lctl --device %$OSC activate
9201         # wait the osc back to normal
9202         wait_osc_import_ready client ost
9203
9204         lfs df || error "lfs df with reactivated OSC failed"
9205         rm -f $DIR/$tfile
9206 }
9207 run_test 104a "lfs df [-ih] [path] test ========================="
9208
9209 test_104b() {
9210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9211         [ $RUNAS_ID -eq $UID ] &&
9212                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9213
9214         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
9215                         grep "Permission denied" | wc -l)))
9216         if [ $denied_cnt -ne 0 ]; then
9217                 error "lfs check servers test failed"
9218         fi
9219 }
9220 run_test 104b "$RUNAS lfs check servers test ===================="
9221
9222 test_105a() {
9223         # doesn't work on 2.4 kernels
9224         touch $DIR/$tfile
9225         if $(flock_is_enabled); then
9226                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
9227         else
9228                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
9229         fi
9230         rm -f $DIR/$tfile
9231 }
9232 run_test 105a "flock when mounted without -o flock test ========"
9233
9234 test_105b() {
9235         touch $DIR/$tfile
9236         if $(flock_is_enabled); then
9237                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
9238         else
9239                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
9240         fi
9241         rm -f $DIR/$tfile
9242 }
9243 run_test 105b "fcntl when mounted without -o flock test ========"
9244
9245 test_105c() {
9246         touch $DIR/$tfile
9247         if $(flock_is_enabled); then
9248                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
9249         else
9250                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
9251         fi
9252         rm -f $DIR/$tfile
9253 }
9254 run_test 105c "lockf when mounted without -o flock test"
9255
9256 test_105d() { # bug 15924
9257         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9258
9259         test_mkdir $DIR/$tdir
9260         flock_is_enabled || skip_env "mount w/o flock enabled"
9261         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
9262         $LCTL set_param fail_loc=0x80000315
9263         flocks_test 2 $DIR/$tdir
9264 }
9265 run_test 105d "flock race (should not freeze) ========"
9266
9267 test_105e() { # bug 22660 && 22040
9268         flock_is_enabled || skip_env "mount w/o flock enabled"
9269
9270         touch $DIR/$tfile
9271         flocks_test 3 $DIR/$tfile
9272 }
9273 run_test 105e "Two conflicting flocks from same process"
9274
9275 test_106() { #bug 10921
9276         test_mkdir $DIR/$tdir
9277         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
9278         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
9279 }
9280 run_test 106 "attempt exec of dir followed by chown of that dir"
9281
9282 test_107() {
9283         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9284
9285         CDIR=`pwd`
9286         local file=core
9287
9288         cd $DIR
9289         rm -f $file
9290
9291         local save_pattern=$(sysctl -n kernel.core_pattern)
9292         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
9293         sysctl -w kernel.core_pattern=$file
9294         sysctl -w kernel.core_uses_pid=0
9295
9296         ulimit -c unlimited
9297         sleep 60 &
9298         SLEEPPID=$!
9299
9300         sleep 1
9301
9302         kill -s 11 $SLEEPPID
9303         wait $SLEEPPID
9304         if [ -e $file ]; then
9305                 size=`stat -c%s $file`
9306                 [ $size -eq 0 ] && error "Fail to create core file $file"
9307         else
9308                 error "Fail to create core file $file"
9309         fi
9310         rm -f $file
9311         sysctl -w kernel.core_pattern=$save_pattern
9312         sysctl -w kernel.core_uses_pid=$save_uses_pid
9313         cd $CDIR
9314 }
9315 run_test 107 "Coredump on SIG"
9316
9317 test_110() {
9318         test_mkdir $DIR/$tdir
9319         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
9320         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
9321                 error "mkdir with 256 char should fail, but did not"
9322         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
9323                 error "create with 255 char failed"
9324         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
9325                 error "create with 256 char should fail, but did not"
9326
9327         ls -l $DIR/$tdir
9328         rm -rf $DIR/$tdir
9329 }
9330 run_test 110 "filename length checking"
9331
9332 #
9333 # Purpose: To verify dynamic thread (OSS) creation.
9334 #
9335 test_115() {
9336         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9337         remote_ost_nodsh && skip "remote OST with nodsh"
9338
9339         # Lustre does not stop service threads once they are started.
9340         # Reset number of running threads to default.
9341         stopall
9342         setupall
9343
9344         local OSTIO_pre
9345         local save_params="$TMP/sanity-$TESTNAME.parameters"
9346
9347         # Get ll_ost_io count before I/O
9348         OSTIO_pre=$(do_facet ost1 \
9349                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
9350         # Exit if lustre is not running (ll_ost_io not running).
9351         [ -z "$OSTIO_pre" ] && error "no OSS threads"
9352
9353         echo "Starting with $OSTIO_pre threads"
9354         local thread_max=$((OSTIO_pre * 2))
9355         local rpc_in_flight=$((thread_max * 2))
9356         # Number of I/O Process proposed to be started.
9357         local nfiles
9358         local facets=$(get_facets OST)
9359
9360         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
9361         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
9362
9363         # Set in_flight to $rpc_in_flight
9364         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
9365                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
9366         nfiles=${rpc_in_flight}
9367         # Set ost thread_max to $thread_max
9368         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
9369
9370         # 5 Minutes should be sufficient for max number of OSS
9371         # threads(thread_max) to be created.
9372         local timeout=300
9373
9374         # Start I/O.
9375         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
9376         test_mkdir $DIR/$tdir
9377         for i in $(seq $nfiles); do
9378                 local file=$DIR/$tdir/${tfile}-$i
9379                 $LFS setstripe -c -1 -i 0 $file
9380                 ($WTL $file $timeout)&
9381         done
9382
9383         # I/O Started - Wait for thread_started to reach thread_max or report
9384         # error if thread_started is more than thread_max.
9385         echo "Waiting for thread_started to reach thread_max"
9386         local thread_started=0
9387         local end_time=$((SECONDS + timeout))
9388
9389         while [ $SECONDS -le $end_time ] ; do
9390                 echo -n "."
9391                 # Get ost i/o thread_started count.
9392                 thread_started=$(do_facet ost1 \
9393                         "$LCTL get_param \
9394                         ost.OSS.ost_io.threads_started | cut -d= -f2")
9395                 # Break out if thread_started is equal/greater than thread_max
9396                 if [[ $thread_started -ge $thread_max ]]; then
9397                         echo ll_ost_io thread_started $thread_started, \
9398                                 equal/greater than thread_max $thread_max
9399                         break
9400                 fi
9401                 sleep 1
9402         done
9403
9404         # Cleanup - We have the numbers, Kill i/o jobs if running.
9405         jobcount=($(jobs -p))
9406         for i in $(seq 0 $((${#jobcount[@]}-1)))
9407         do
9408                 kill -9 ${jobcount[$i]}
9409                 if [ $? -ne 0 ] ; then
9410                         echo Warning: \
9411                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
9412                 fi
9413         done
9414
9415         # Cleanup files left by WTL binary.
9416         for i in $(seq $nfiles); do
9417                 local file=$DIR/$tdir/${tfile}-$i
9418                 rm -rf $file
9419                 if [ $? -ne 0 ] ; then
9420                         echo "Warning: Failed to delete file $file"
9421                 fi
9422         done
9423
9424         restore_lustre_params <$save_params
9425         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
9426
9427         # Error out if no new thread has started or Thread started is greater
9428         # than thread max.
9429         if [[ $thread_started -le $OSTIO_pre ||
9430                         $thread_started -gt $thread_max ]]; then
9431                 error "ll_ost_io: thread_started $thread_started" \
9432                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
9433                       "No new thread started or thread started greater " \
9434                       "than thread_max."
9435         fi
9436 }
9437 run_test 115 "verify dynamic thread creation===================="
9438
9439 free_min_max () {
9440         wait_delete_completed
9441         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
9442         echo "OST kbytes available: ${AVAIL[@]}"
9443         MAXV=${AVAIL[0]}
9444         MAXI=0
9445         MINV=${AVAIL[0]}
9446         MINI=0
9447         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
9448                 #echo OST $i: ${AVAIL[i]}kb
9449                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
9450                         MAXV=${AVAIL[i]}
9451                         MAXI=$i
9452                 fi
9453                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
9454                         MINV=${AVAIL[i]}
9455                         MINI=$i
9456                 fi
9457         done
9458         echo "Min free space: OST $MINI: $MINV"
9459         echo "Max free space: OST $MAXI: $MAXV"
9460 }
9461
9462 test_116a() { # was previously test_116()
9463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9464         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9465         remote_mds_nodsh && skip "remote MDS with nodsh"
9466
9467         echo -n "Free space priority "
9468         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
9469                 head -n1
9470         declare -a AVAIL
9471         free_min_max
9472
9473         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
9474         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
9475         trap simple_cleanup_common EXIT
9476
9477         # Check if we need to generate uneven OSTs
9478         test_mkdir -p $DIR/$tdir/OST${MINI}
9479         local FILL=$((MINV / 4))
9480         local DIFF=$((MAXV - MINV))
9481         local DIFF2=$((DIFF * 100 / MINV))
9482
9483         local threshold=$(do_facet $SINGLEMDS \
9484                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
9485         threshold=${threshold%%%}
9486         echo -n "Check for uneven OSTs: "
9487         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
9488
9489         if [[ $DIFF2 -gt $threshold ]]; then
9490                 echo "ok"
9491                 echo "Don't need to fill OST$MINI"
9492         else
9493                 # generate uneven OSTs. Write 2% over the QOS threshold value
9494                 echo "no"
9495                 DIFF=$((threshold - DIFF2 + 2))
9496                 DIFF2=$((MINV * DIFF / 100))
9497                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
9498                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
9499                         error "setstripe failed"
9500                 DIFF=$((DIFF2 / 2048))
9501                 i=0
9502                 while [ $i -lt $DIFF ]; do
9503                         i=$((i + 1))
9504                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
9505                                 bs=2M count=1 2>/dev/null
9506                         echo -n .
9507                 done
9508                 echo .
9509                 sync
9510                 sleep_maxage
9511                 free_min_max
9512         fi
9513
9514         DIFF=$((MAXV - MINV))
9515         DIFF2=$((DIFF * 100 / MINV))
9516         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
9517         if [ $DIFF2 -gt $threshold ]; then
9518                 echo "ok"
9519         else
9520                 echo "failed - QOS mode won't be used"
9521                 simple_cleanup_common
9522                 skip "QOS imbalance criteria not met"
9523         fi
9524
9525         MINI1=$MINI
9526         MINV1=$MINV
9527         MAXI1=$MAXI
9528         MAXV1=$MAXV
9529
9530         # now fill using QOS
9531         $SETSTRIPE -c 1 $DIR/$tdir
9532         FILL=$((FILL / 200))
9533         if [ $FILL -gt 600 ]; then
9534                 FILL=600
9535         fi
9536         echo "writing $FILL files to QOS-assigned OSTs"
9537         i=0
9538         while [ $i -lt $FILL ]; do
9539                 i=$((i + 1))
9540                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
9541                         count=1 2>/dev/null
9542                 echo -n .
9543         done
9544         echo "wrote $i 200k files"
9545         sync
9546         sleep_maxage
9547
9548         echo "Note: free space may not be updated, so measurements might be off"
9549         free_min_max
9550         DIFF2=$((MAXV - MINV))
9551         echo "free space delta: orig $DIFF final $DIFF2"
9552         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
9553         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
9554         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
9555         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
9556         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
9557         if [[ $DIFF -gt 0 ]]; then
9558                 FILL=$((DIFF2 * 100 / DIFF - 100))
9559                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
9560         fi
9561
9562         # Figure out which files were written where
9563         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9564                awk '/'$MINI1': / {print $2; exit}')
9565         echo $UUID
9566         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9567         echo "$MINC files created on smaller OST $MINI1"
9568         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
9569                awk '/'$MAXI1': / {print $2; exit}')
9570         echo $UUID
9571         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
9572         echo "$MAXC files created on larger OST $MAXI1"
9573         if [[ $MINC -gt 0 ]]; then
9574                 FILL=$((MAXC * 100 / MINC - 100))
9575                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
9576         fi
9577         [[ $MAXC -gt $MINC ]] ||
9578                 error_ignore LU-9 "stripe QOS didn't balance free space"
9579         simple_cleanup_common
9580 }
9581 run_test 116a "stripe QOS: free space balance ==================="
9582
9583 test_116b() { # LU-2093
9584         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9585         remote_mds_nodsh && skip "remote MDS with nodsh"
9586
9587 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
9588         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
9589                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
9590         [ -z "$old_rr" ] && skip "no QOS"
9591         do_facet $SINGLEMDS lctl set_param \
9592                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
9593         mkdir -p $DIR/$tdir
9594         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
9595         createmany -o $DIR/$tdir/f- 20 || error "can't create"
9596         do_facet $SINGLEMDS lctl set_param fail_loc=0
9597         rm -rf $DIR/$tdir
9598         do_facet $SINGLEMDS lctl set_param \
9599                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
9600 }
9601 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
9602
9603 test_117() # bug 10891
9604 {
9605         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9606
9607         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
9608         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
9609         lctl set_param fail_loc=0x21e
9610         > $DIR/$tfile || error "truncate failed"
9611         lctl set_param fail_loc=0
9612         echo "Truncate succeeded."
9613         rm -f $DIR/$tfile
9614 }
9615 run_test 117 "verify osd extend =========="
9616
9617 NO_SLOW_RESENDCOUNT=4
9618 export OLD_RESENDCOUNT=""
9619 set_resend_count () {
9620         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
9621         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
9622         lctl set_param -n $PROC_RESENDCOUNT $1
9623         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
9624 }
9625
9626 # for reduce test_118* time (b=14842)
9627 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9628
9629 # Reset async IO behavior after error case
9630 reset_async() {
9631         FILE=$DIR/reset_async
9632
9633         # Ensure all OSCs are cleared
9634         $SETSTRIPE -c -1 $FILE
9635         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
9636         sync
9637         rm $FILE
9638 }
9639
9640 test_118a() #bug 11710
9641 {
9642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9643
9644         reset_async
9645
9646         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9647         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9648         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9649
9650         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9651                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9652                 return 1;
9653         fi
9654         rm -f $DIR/$tfile
9655 }
9656 run_test 118a "verify O_SYNC works =========="
9657
9658 test_118b()
9659 {
9660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9661         remote_ost_nodsh && skip "remote OST with nodsh"
9662
9663         reset_async
9664
9665         #define OBD_FAIL_SRV_ENOENT 0x217
9666         set_nodes_failloc "$(osts_nodes)" 0x217
9667         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9668         RC=$?
9669         set_nodes_failloc "$(osts_nodes)" 0
9670         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9671         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9672                     grep -c writeback)
9673
9674         if [[ $RC -eq 0 ]]; then
9675                 error "Must return error due to dropped pages, rc=$RC"
9676                 return 1;
9677         fi
9678
9679         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9680                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9681                 return 1;
9682         fi
9683
9684         echo "Dirty pages not leaked on ENOENT"
9685
9686         # Due to the above error the OSC will issue all RPCs syncronously
9687         # until a subsequent RPC completes successfully without error.
9688         $MULTIOP $DIR/$tfile Ow4096yc
9689         rm -f $DIR/$tfile
9690
9691         return 0
9692 }
9693 run_test 118b "Reclaim dirty pages on fatal error =========="
9694
9695 test_118c()
9696 {
9697         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9698
9699         # for 118c, restore the original resend count, LU-1940
9700         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
9701                                 set_resend_count $OLD_RESENDCOUNT
9702         remote_ost_nodsh && skip "remote OST with nodsh"
9703
9704         reset_async
9705
9706         #define OBD_FAIL_OST_EROFS               0x216
9707         set_nodes_failloc "$(osts_nodes)" 0x216
9708
9709         # multiop should block due to fsync until pages are written
9710         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9711         MULTIPID=$!
9712         sleep 1
9713
9714         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9715                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9716         fi
9717
9718         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9719                     grep -c writeback)
9720         if [[ $WRITEBACK -eq 0 ]]; then
9721                 error "No page in writeback, writeback=$WRITEBACK"
9722         fi
9723
9724         set_nodes_failloc "$(osts_nodes)" 0
9725         wait $MULTIPID
9726         RC=$?
9727         if [[ $RC -ne 0 ]]; then
9728                 error "Multiop fsync failed, rc=$RC"
9729         fi
9730
9731         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9732         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9733                     grep -c writeback)
9734         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9735                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9736         fi
9737
9738         rm -f $DIR/$tfile
9739         echo "Dirty pages flushed via fsync on EROFS"
9740         return 0
9741 }
9742 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
9743
9744 # continue to use small resend count to reduce test_118* time (b=14842)
9745 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
9746
9747 test_118d()
9748 {
9749         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9750         remote_ost_nodsh && skip "remote OST with nodsh"
9751
9752         reset_async
9753
9754         #define OBD_FAIL_OST_BRW_PAUSE_BULK
9755         set_nodes_failloc "$(osts_nodes)" 0x214
9756         # multiop should block due to fsync until pages are written
9757         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9758         MULTIPID=$!
9759         sleep 1
9760
9761         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
9762                 error "Multiop failed to block on fsync, pid=$MULTIPID"
9763         fi
9764
9765         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9766                     grep -c writeback)
9767         if [[ $WRITEBACK -eq 0 ]]; then
9768                 error "No page in writeback, writeback=$WRITEBACK"
9769         fi
9770
9771         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
9772         set_nodes_failloc "$(osts_nodes)" 0
9773
9774         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9775         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9776                     grep -c writeback)
9777         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9778                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9779         fi
9780
9781         rm -f $DIR/$tfile
9782         echo "Dirty pages gaurenteed flushed via fsync"
9783         return 0
9784 }
9785 run_test 118d "Fsync validation inject a delay of the bulk =========="
9786
9787 test_118f() {
9788         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9789
9790         reset_async
9791
9792         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
9793         lctl set_param fail_loc=0x8000040a
9794
9795         # Should simulate EINVAL error which is fatal
9796         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9797         RC=$?
9798         if [[ $RC -eq 0 ]]; then
9799                 error "Must return error due to dropped pages, rc=$RC"
9800         fi
9801
9802         lctl set_param fail_loc=0x0
9803
9804         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9805         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9806         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9807                     grep -c writeback)
9808         if [[ $LOCKED -ne 0 ]]; then
9809                 error "Locked pages remain in cache, locked=$LOCKED"
9810         fi
9811
9812         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9813                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9814         fi
9815
9816         rm -f $DIR/$tfile
9817         echo "No pages locked after fsync"
9818
9819         reset_async
9820         return 0
9821 }
9822 run_test 118f "Simulate unrecoverable OSC side error =========="
9823
9824 test_118g() {
9825         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9826
9827         reset_async
9828
9829         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
9830         lctl set_param fail_loc=0x406
9831
9832         # simulate local -ENOMEM
9833         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9834         RC=$?
9835
9836         lctl set_param fail_loc=0
9837         if [[ $RC -eq 0 ]]; then
9838                 error "Must return error due to dropped pages, rc=$RC"
9839         fi
9840
9841         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9842         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9843         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9844                         grep -c writeback)
9845         if [[ $LOCKED -ne 0 ]]; then
9846                 error "Locked pages remain in cache, locked=$LOCKED"
9847         fi
9848
9849         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9850                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9851         fi
9852
9853         rm -f $DIR/$tfile
9854         echo "No pages locked after fsync"
9855
9856         reset_async
9857         return 0
9858 }
9859 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
9860
9861 test_118h() {
9862         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9863         remote_ost_nodsh && skip "remote OST with nodsh"
9864
9865         reset_async
9866
9867         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9868         set_nodes_failloc "$(osts_nodes)" 0x20e
9869         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9870         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9871         RC=$?
9872
9873         set_nodes_failloc "$(osts_nodes)" 0
9874         if [[ $RC -eq 0 ]]; then
9875                 error "Must return error due to dropped pages, rc=$RC"
9876         fi
9877
9878         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9879         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9880         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
9881                     grep -c writeback)
9882         if [[ $LOCKED -ne 0 ]]; then
9883                 error "Locked pages remain in cache, locked=$LOCKED"
9884         fi
9885
9886         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9887                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9888         fi
9889
9890         rm -f $DIR/$tfile
9891         echo "No pages locked after fsync"
9892
9893         return 0
9894 }
9895 run_test 118h "Verify timeout in handling recoverables errors  =========="
9896
9897 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
9898
9899 test_118i() {
9900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9901         remote_ost_nodsh && skip "remote OST with nodsh"
9902
9903         reset_async
9904
9905         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9906         set_nodes_failloc "$(osts_nodes)" 0x20e
9907
9908         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
9909         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
9910         PID=$!
9911         sleep 5
9912         set_nodes_failloc "$(osts_nodes)" 0
9913
9914         wait $PID
9915         RC=$?
9916         if [[ $RC -ne 0 ]]; then
9917                 error "got error, but should be not, rc=$RC"
9918         fi
9919
9920         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9921         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9922         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9923         if [[ $LOCKED -ne 0 ]]; then
9924                 error "Locked pages remain in cache, locked=$LOCKED"
9925         fi
9926
9927         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9928                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9929         fi
9930
9931         rm -f $DIR/$tfile
9932         echo "No pages locked after fsync"
9933
9934         return 0
9935 }
9936 run_test 118i "Fix error before timeout in recoverable error  =========="
9937
9938 [ "$SLOW" = "no" ] && set_resend_count 4
9939
9940 test_118j() {
9941         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9942         remote_ost_nodsh && skip "remote OST with nodsh"
9943
9944         reset_async
9945
9946         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
9947         set_nodes_failloc "$(osts_nodes)" 0x220
9948
9949         # return -EIO from OST
9950         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9951         RC=$?
9952         set_nodes_failloc "$(osts_nodes)" 0x0
9953         if [[ $RC -eq 0 ]]; then
9954                 error "Must return error due to dropped pages, rc=$RC"
9955         fi
9956
9957         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
9958         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
9959         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
9960         if [[ $LOCKED -ne 0 ]]; then
9961                 error "Locked pages remain in cache, locked=$LOCKED"
9962         fi
9963
9964         # in recoverable error on OST we want resend and stay until it finished
9965         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
9966                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
9967         fi
9968
9969         rm -f $DIR/$tfile
9970         echo "No pages locked after fsync"
9971
9972         return 0
9973 }
9974 run_test 118j "Simulate unrecoverable OST side error =========="
9975
9976 test_118k()
9977 {
9978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9979         remote_ost_nodsh && skip "remote OSTs with nodsh"
9980
9981         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
9982         set_nodes_failloc "$(osts_nodes)" 0x20e
9983         test_mkdir $DIR/$tdir
9984
9985         for ((i=0;i<10;i++)); do
9986                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
9987                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
9988                 SLEEPPID=$!
9989                 sleep 0.500s
9990                 kill $SLEEPPID
9991                 wait $SLEEPPID
9992         done
9993
9994         set_nodes_failloc "$(osts_nodes)" 0
9995         rm -rf $DIR/$tdir
9996 }
9997 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
9998
9999 test_118l() # LU-646
10000 {
10001         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10002
10003         test_mkdir $DIR/$tdir
10004         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
10005         rm -rf $DIR/$tdir
10006 }
10007 run_test 118l "fsync dir"
10008
10009 test_118m() # LU-3066
10010 {
10011         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10012
10013         test_mkdir $DIR/$tdir
10014         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
10015         rm -rf $DIR/$tdir
10016 }
10017 run_test 118m "fdatasync dir ========="
10018
10019 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10020
10021 test_118n()
10022 {
10023         local begin
10024         local end
10025
10026         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10027         remote_ost_nodsh && skip "remote OSTs with nodsh"
10028
10029         # Sleep to avoid a cached response.
10030         #define OBD_STATFS_CACHE_SECONDS 1
10031         sleep 2
10032
10033         # Inject a 10 second delay in the OST_STATFS handler.
10034         #define OBD_FAIL_OST_STATFS_DELAY 0x242
10035         set_nodes_failloc "$(osts_nodes)" 0x242
10036
10037         begin=$SECONDS
10038         stat --file-system $MOUNT > /dev/null
10039         end=$SECONDS
10040
10041         set_nodes_failloc "$(osts_nodes)" 0
10042
10043         if ((end - begin > 20)); then
10044             error "statfs took $((end - begin)) seconds, expected 10"
10045         fi
10046 }
10047 run_test 118n "statfs() sends OST_STATFS requests in parallel"
10048
10049 test_119a() # bug 11737
10050 {
10051         BSIZE=$((512 * 1024))
10052         directio write $DIR/$tfile 0 1 $BSIZE
10053         # We ask to read two blocks, which is more than a file size.
10054         # directio will indicate an error when requested and actual
10055         # sizes aren't equeal (a normal situation in this case) and
10056         # print actual read amount.
10057         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
10058         if [ "$NOB" != "$BSIZE" ]; then
10059                 error "read $NOB bytes instead of $BSIZE"
10060         fi
10061         rm -f $DIR/$tfile
10062 }
10063 run_test 119a "Short directIO read must return actual read amount"
10064
10065 test_119b() # bug 11737
10066 {
10067         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10068
10069         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
10070         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
10071         sync
10072         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
10073                 error "direct read failed"
10074         rm -f $DIR/$tfile
10075 }
10076 run_test 119b "Sparse directIO read must return actual read amount"
10077
10078 test_119c() # bug 13099
10079 {
10080         BSIZE=1048576
10081         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
10082         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
10083         rm -f $DIR/$tfile
10084 }
10085 run_test 119c "Testing for direct read hitting hole"
10086
10087 test_119d() # bug 15950
10088 {
10089         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10090
10091         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
10092         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
10093         BSIZE=1048576
10094         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
10095         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
10096         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
10097         lctl set_param fail_loc=0x40d
10098         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
10099         pid_dio=$!
10100         sleep 1
10101         cat $DIR/$tfile > /dev/null &
10102         lctl set_param fail_loc=0
10103         pid_reads=$!
10104         wait $pid_dio
10105         log "the DIO writes have completed, now wait for the reads (should not block very long)"
10106         sleep 2
10107         [ -n "`ps h -p $pid_reads -o comm`" ] && \
10108         error "the read rpcs have not completed in 2s"
10109         rm -f $DIR/$tfile
10110         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
10111 }
10112 run_test 119d "The DIO path should try to send a new rpc once one is completed"
10113
10114 test_120a() {
10115         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10116         remote_mds_nodsh && skip "remote MDS with nodsh"
10117         test_mkdir $DIR/$tdir
10118         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10119                 skip_env "no early lock cancel on server"
10120
10121         lru_resize_disable mdc
10122         lru_resize_disable osc
10123         cancel_lru_locks mdc
10124         # asynchronous object destroy at MDT could cause bl ast to client
10125         cancel_lru_locks osc
10126
10127         stat $DIR/$tdir > /dev/null
10128         can1=$(do_facet $SINGLEMDS \
10129                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10130                awk '/ldlm_cancel/ {print $2}')
10131         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10132                awk '/ldlm_bl_callback/ {print $2}')
10133         test_mkdir -c1 $DIR/$tdir/d1
10134         can2=$(do_facet $SINGLEMDS \
10135                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10136                awk '/ldlm_cancel/ {print $2}')
10137         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10138                awk '/ldlm_bl_callback/ {print $2}')
10139         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10140         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10141         lru_resize_enable mdc
10142         lru_resize_enable osc
10143 }
10144 run_test 120a "Early Lock Cancel: mkdir test"
10145
10146 test_120b() {
10147         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10148         remote_mds_nodsh && skip "remote MDS with nodsh"
10149         test_mkdir $DIR/$tdir
10150         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10151                 skip_env "no early lock cancel on server"
10152
10153         lru_resize_disable mdc
10154         lru_resize_disable osc
10155         cancel_lru_locks mdc
10156         stat $DIR/$tdir > /dev/null
10157         can1=$(do_facet $SINGLEMDS \
10158                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10159                awk '/ldlm_cancel/ {print $2}')
10160         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10161                awk '/ldlm_bl_callback/ {print $2}')
10162         touch $DIR/$tdir/f1
10163         can2=$(do_facet $SINGLEMDS \
10164                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10165                awk '/ldlm_cancel/ {print $2}')
10166         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10167                awk '/ldlm_bl_callback/ {print $2}')
10168         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10169         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10170         lru_resize_enable mdc
10171         lru_resize_enable osc
10172 }
10173 run_test 120b "Early Lock Cancel: create test"
10174
10175 test_120c() {
10176         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10177         remote_mds_nodsh && skip "remote MDS with nodsh"
10178         test_mkdir -c1 $DIR/$tdir
10179         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10180                 skip "no early lock cancel on server"
10181
10182         lru_resize_disable mdc
10183         lru_resize_disable osc
10184         test_mkdir -c1 $DIR/$tdir/d1
10185         test_mkdir -c1 $DIR/$tdir/d2
10186         touch $DIR/$tdir/d1/f1
10187         cancel_lru_locks mdc
10188         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
10189         can1=$(do_facet $SINGLEMDS \
10190                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10191                awk '/ldlm_cancel/ {print $2}')
10192         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10193                awk '/ldlm_bl_callback/ {print $2}')
10194         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10195         can2=$(do_facet $SINGLEMDS \
10196                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10197                awk '/ldlm_cancel/ {print $2}')
10198         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10199                awk '/ldlm_bl_callback/ {print $2}')
10200         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10201         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10202         lru_resize_enable mdc
10203         lru_resize_enable osc
10204 }
10205 run_test 120c "Early Lock Cancel: link test"
10206
10207 test_120d() {
10208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10209         remote_mds_nodsh && skip "remote MDS with nodsh"
10210         test_mkdir -c1 $DIR/$tdir
10211         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10212                 skip_env "no early lock cancel on server"
10213
10214         lru_resize_disable mdc
10215         lru_resize_disable osc
10216         touch $DIR/$tdir
10217         cancel_lru_locks mdc
10218         stat $DIR/$tdir > /dev/null
10219         can1=$(do_facet $SINGLEMDS \
10220                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10221                awk '/ldlm_cancel/ {print $2}')
10222         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10223                awk '/ldlm_bl_callback/ {print $2}')
10224         chmod a+x $DIR/$tdir
10225         can2=$(do_facet $SINGLEMDS \
10226                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10227                awk '/ldlm_cancel/ {print $2}')
10228         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10229                awk '/ldlm_bl_callback/ {print $2}')
10230         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10231         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10232         lru_resize_enable mdc
10233         lru_resize_enable osc
10234 }
10235 run_test 120d "Early Lock Cancel: setattr test"
10236
10237 test_120e() {
10238         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10239         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10240                 skip_env "no early lock cancel on server"
10241         remote_mds_nodsh && skip "remote MDS with nodsh"
10242
10243         local dlmtrace_set=false
10244
10245         test_mkdir -c1 $DIR/$tdir
10246         lru_resize_disable mdc
10247         lru_resize_disable osc
10248         ! $LCTL get_param debug | grep -q dlmtrace &&
10249                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
10250         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
10251         cancel_lru_locks mdc
10252         cancel_lru_locks osc
10253         dd if=$DIR/$tdir/f1 of=/dev/null
10254         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
10255         # XXX client can not do early lock cancel of OST lock
10256         # during unlink (LU-4206), so cancel osc lock now.
10257         sleep 2
10258         cancel_lru_locks osc
10259         can1=$(do_facet $SINGLEMDS \
10260                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10261                awk '/ldlm_cancel/ {print $2}')
10262         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10263                awk '/ldlm_bl_callback/ {print $2}')
10264         unlink $DIR/$tdir/f1
10265         sleep 5
10266         can2=$(do_facet $SINGLEMDS \
10267                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10268                awk '/ldlm_cancel/ {print $2}')
10269         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10270                awk '/ldlm_bl_callback/ {print $2}')
10271         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
10272                 $LCTL dk $TMP/cancel.debug.txt
10273         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
10274                 $LCTL dk $TMP/blocking.debug.txt
10275         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
10276         lru_resize_enable mdc
10277         lru_resize_enable osc
10278 }
10279 run_test 120e "Early Lock Cancel: unlink test"
10280
10281 test_120f() {
10282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10283         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10284                 skip_env "no early lock cancel on server"
10285         remote_mds_nodsh && skip "remote MDS with nodsh"
10286
10287         test_mkdir -c1 $DIR/$tdir
10288         lru_resize_disable mdc
10289         lru_resize_disable osc
10290         test_mkdir -c1 $DIR/$tdir/d1
10291         test_mkdir -c1 $DIR/$tdir/d2
10292         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
10293         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
10294         cancel_lru_locks mdc
10295         cancel_lru_locks osc
10296         dd if=$DIR/$tdir/d1/f1 of=/dev/null
10297         dd if=$DIR/$tdir/d2/f2 of=/dev/null
10298         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
10299         # XXX client can not do early lock cancel of OST lock
10300         # during rename (LU-4206), so cancel osc lock now.
10301         sleep 2
10302         cancel_lru_locks osc
10303         can1=$(do_facet $SINGLEMDS \
10304                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10305                awk '/ldlm_cancel/ {print $2}')
10306         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10307                awk '/ldlm_bl_callback/ {print $2}')
10308         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
10309         sleep 5
10310         can2=$(do_facet $SINGLEMDS \
10311                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10312                awk '/ldlm_cancel/ {print $2}')
10313         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10314                awk '/ldlm_bl_callback/ {print $2}')
10315         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
10316         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
10317         lru_resize_enable mdc
10318         lru_resize_enable osc
10319 }
10320 run_test 120f "Early Lock Cancel: rename test"
10321
10322 test_120g() {
10323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10324         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
10325                 skip_env "no early lock cancel on server"
10326         remote_mds_nodsh && skip "remote MDS with nodsh"
10327
10328         lru_resize_disable mdc
10329         lru_resize_disable osc
10330         count=10000
10331         echo create $count files
10332         test_mkdir $DIR/$tdir
10333         cancel_lru_locks mdc
10334         cancel_lru_locks osc
10335         t0=$(date +%s)
10336
10337         can0=$(do_facet $SINGLEMDS \
10338                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10339                awk '/ldlm_cancel/ {print $2}')
10340         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10341                awk '/ldlm_bl_callback/ {print $2}')
10342         createmany -o $DIR/$tdir/f $count
10343         sync
10344         can1=$(do_facet $SINGLEMDS \
10345                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10346                awk '/ldlm_cancel/ {print $2}')
10347         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10348                awk '/ldlm_bl_callback/ {print $2}')
10349         t1=$(date +%s)
10350         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
10351         echo rm $count files
10352         rm -r $DIR/$tdir
10353         sync
10354         can2=$(do_facet $SINGLEMDS \
10355                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
10356                awk '/ldlm_cancel/ {print $2}')
10357         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
10358                awk '/ldlm_bl_callback/ {print $2}')
10359         t2=$(date +%s)
10360         echo total: $count removes in $((t2-t1))
10361         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
10362         sleep 2
10363         # wait for commitment of removal
10364         lru_resize_enable mdc
10365         lru_resize_enable osc
10366 }
10367 run_test 120g "Early Lock Cancel: performance test"
10368
10369 test_121() { #bug #10589
10370         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10371
10372         rm -rf $DIR/$tfile
10373         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
10374 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
10375         lctl set_param fail_loc=0x310
10376         cancel_lru_locks osc > /dev/null
10377         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
10378         lctl set_param fail_loc=0
10379         [[ $reads -eq $writes ]] ||
10380                 error "read $reads blocks, must be $writes blocks"
10381 }
10382 run_test 121 "read cancel race ========="
10383
10384 test_123a() { # was test 123, statahead(bug 11401)
10385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10386
10387         SLOWOK=0
10388         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
10389                 log "testing UP system. Performance may be lower than expected."
10390                 SLOWOK=1
10391         fi
10392
10393         rm -rf $DIR/$tdir
10394         test_mkdir $DIR/$tdir
10395         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
10396         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
10397         MULT=10
10398         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
10399                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
10400
10401                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10402                 lctl set_param -n llite.*.statahead_max 0
10403                 lctl get_param llite.*.statahead_max
10404                 cancel_lru_locks mdc
10405                 cancel_lru_locks osc
10406                 stime=`date +%s`
10407                 time ls -l $DIR/$tdir | wc -l
10408                 etime=`date +%s`
10409                 delta=$((etime - stime))
10410                 log "ls $i files without statahead: $delta sec"
10411                 lctl set_param llite.*.statahead_max=$max
10412
10413                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10414                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
10415                 cancel_lru_locks mdc
10416                 cancel_lru_locks osc
10417                 stime=`date +%s`
10418                 time ls -l $DIR/$tdir | wc -l
10419                 etime=`date +%s`
10420                 delta_sa=$((etime - stime))
10421                 log "ls $i files with statahead: $delta_sa sec"
10422                 lctl get_param -n llite.*.statahead_stats
10423                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
10424
10425                 [[ $swrong -lt $ewrong ]] &&
10426                         log "statahead was stopped, maybe too many locks held!"
10427                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
10428
10429                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10430                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
10431                     lctl set_param -n llite.*.statahead_max 0
10432                     lctl get_param llite.*.statahead_max
10433                     cancel_lru_locks mdc
10434                     cancel_lru_locks osc
10435                     stime=`date +%s`
10436                     time ls -l $DIR/$tdir | wc -l
10437                     etime=`date +%s`
10438                     delta=$((etime - stime))
10439                     log "ls $i files again without statahead: $delta sec"
10440                     lctl set_param llite.*.statahead_max=$max
10441                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
10442                         if [  $SLOWOK -eq 0 ]; then
10443                                 error "ls $i files is slower with statahead!"
10444                         else
10445                                 log "ls $i files is slower with statahead!"
10446                         fi
10447                         break
10448                     fi
10449                 fi
10450
10451                 [ $delta -gt 20 ] && break
10452                 [ $delta -gt 8 ] && MULT=$((50 / delta))
10453                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
10454         done
10455         log "ls done"
10456
10457         stime=`date +%s`
10458         rm -r $DIR/$tdir
10459         sync
10460         etime=`date +%s`
10461         delta=$((etime - stime))
10462         log "rm -r $DIR/$tdir/: $delta seconds"
10463         log "rm done"
10464         lctl get_param -n llite.*.statahead_stats
10465 }
10466 run_test 123a "verify statahead work"
10467
10468 test_123b () { # statahead(bug 15027)
10469         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10470
10471         test_mkdir $DIR/$tdir
10472         createmany -o $DIR/$tdir/$tfile-%d 1000
10473
10474         cancel_lru_locks mdc
10475         cancel_lru_locks osc
10476
10477 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
10478         lctl set_param fail_loc=0x80000803
10479         ls -lR $DIR/$tdir > /dev/null
10480         log "ls done"
10481         lctl set_param fail_loc=0x0
10482         lctl get_param -n llite.*.statahead_stats
10483         rm -r $DIR/$tdir
10484         sync
10485
10486 }
10487 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
10488
10489 test_123c() {
10490         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
10491
10492         test_mkdir -i 0 -c 1 $DIR/$tdir.0
10493         test_mkdir -i 1 -c 1 $DIR/$tdir.1
10494         touch $DIR/$tdir.1/{1..3}
10495         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
10496
10497         remount_client $MOUNT
10498
10499         $MULTIOP $DIR/$tdir.0 Q
10500
10501         # let statahead to complete
10502         ls -l $DIR/$tdir.0 > /dev/null
10503
10504         testid=$(echo $TESTNAME | tr '_' ' ')
10505         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
10506                 error "statahead warning" || true
10507 }
10508 run_test 123c "Can not initialize inode warning on DNE statahead"
10509
10510 test_124a() {
10511         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10512         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10513                 skip_env "no lru resize on server"
10514
10515         local NR=2000
10516
10517         test_mkdir $DIR/$tdir
10518
10519         log "create $NR files at $DIR/$tdir"
10520         createmany -o $DIR/$tdir/f $NR ||
10521                 error "failed to create $NR files in $DIR/$tdir"
10522
10523         cancel_lru_locks mdc
10524         ls -l $DIR/$tdir > /dev/null
10525
10526         local NSDIR=""
10527         local LRU_SIZE=0
10528         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
10529                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
10530                 LRU_SIZE=$($LCTL get_param -n $PARAM)
10531                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
10532                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
10533                         log "NSDIR=$NSDIR"
10534                         log "NS=$(basename $NSDIR)"
10535                         break
10536                 fi
10537         done
10538
10539         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
10540                 skip "Not enough cached locks created!"
10541         fi
10542         log "LRU=$LRU_SIZE"
10543
10544         local SLEEP=30
10545
10546         # We know that lru resize allows one client to hold $LIMIT locks
10547         # for 10h. After that locks begin to be killed by client.
10548         local MAX_HRS=10
10549         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
10550         log "LIMIT=$LIMIT"
10551         if [ $LIMIT -lt $LRU_SIZE ]; then
10552                 skip "Limit is too small $LIMIT"
10553         fi
10554
10555         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
10556         # killing locks. Some time was spent for creating locks. This means
10557         # that up to the moment of sleep finish we must have killed some of
10558         # them (10-100 locks). This depends on how fast ther were created.
10559         # Many of them were touched in almost the same moment and thus will
10560         # be killed in groups.
10561         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
10562
10563         # Use $LRU_SIZE_B here to take into account real number of locks
10564         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
10565         local LRU_SIZE_B=$LRU_SIZE
10566         log "LVF=$LVF"
10567         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
10568         log "OLD_LVF=$OLD_LVF"
10569         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
10570
10571         # Let's make sure that we really have some margin. Client checks
10572         # cached locks every 10 sec.
10573         SLEEP=$((SLEEP+20))
10574         log "Sleep ${SLEEP} sec"
10575         local SEC=0
10576         while ((SEC<$SLEEP)); do
10577                 echo -n "..."
10578                 sleep 5
10579                 SEC=$((SEC+5))
10580                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
10581                 echo -n "$LRU_SIZE"
10582         done
10583         echo ""
10584         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
10585         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
10586
10587         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
10588                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
10589                 unlinkmany $DIR/$tdir/f $NR
10590                 return
10591         }
10592
10593         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
10594         log "unlink $NR files at $DIR/$tdir"
10595         unlinkmany $DIR/$tdir/f $NR
10596 }
10597 run_test 124a "lru resize ======================================="
10598
10599 get_max_pool_limit()
10600 {
10601         local limit=$($LCTL get_param \
10602                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
10603         local max=0
10604         for l in $limit; do
10605                 if [[ $l -gt $max ]]; then
10606                         max=$l
10607                 fi
10608         done
10609         echo $max
10610 }
10611
10612 test_124b() {
10613         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10614         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10615                 skip_env "no lru resize on server"
10616
10617         LIMIT=$(get_max_pool_limit)
10618
10619         NR=$(($(default_lru_size)*20))
10620         if [[ $NR -gt $LIMIT ]]; then
10621                 log "Limit lock number by $LIMIT locks"
10622                 NR=$LIMIT
10623         fi
10624
10625         IFree=$(mdsrate_inodes_available)
10626         if [ $IFree -lt $NR ]; then
10627                 log "Limit lock number by $IFree inodes"
10628                 NR=$IFree
10629         fi
10630
10631         lru_resize_disable mdc
10632         test_mkdir -p $DIR/$tdir/disable_lru_resize
10633
10634         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
10635         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
10636         cancel_lru_locks mdc
10637         stime=`date +%s`
10638         PID=""
10639         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10640         PID="$PID $!"
10641         sleep 2
10642         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10643         PID="$PID $!"
10644         sleep 2
10645         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
10646         PID="$PID $!"
10647         wait $PID
10648         etime=`date +%s`
10649         nolruresize_delta=$((etime-stime))
10650         log "ls -la time: $nolruresize_delta seconds"
10651         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10652         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
10653
10654         lru_resize_enable mdc
10655         test_mkdir -p $DIR/$tdir/enable_lru_resize
10656
10657         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
10658         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
10659         cancel_lru_locks mdc
10660         stime=`date +%s`
10661         PID=""
10662         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10663         PID="$PID $!"
10664         sleep 2
10665         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10666         PID="$PID $!"
10667         sleep 2
10668         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
10669         PID="$PID $!"
10670         wait $PID
10671         etime=`date +%s`
10672         lruresize_delta=$((etime-stime))
10673         log "ls -la time: $lruresize_delta seconds"
10674         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
10675
10676         if [ $lruresize_delta -gt $nolruresize_delta ]; then
10677                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
10678         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
10679                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
10680         else
10681                 log "lru resize performs the same with no lru resize"
10682         fi
10683         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
10684 }
10685 run_test 124b "lru resize (performance test) ======================="
10686
10687 test_124c() {
10688         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10689         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10690                 skip_env "no lru resize on server"
10691
10692         # cache ununsed locks on client
10693         local nr=100
10694         cancel_lru_locks mdc
10695         test_mkdir $DIR/$tdir
10696         createmany -o $DIR/$tdir/f $nr ||
10697                 error "failed to create $nr files in $DIR/$tdir"
10698         ls -l $DIR/$tdir > /dev/null
10699
10700         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10701         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10702         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
10703         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
10704         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
10705
10706         # set lru_max_age to 1 sec
10707         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
10708         echo "sleep $((recalc_p * 2)) seconds..."
10709         sleep $((recalc_p * 2))
10710
10711         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
10712         # restore lru_max_age
10713         $LCTL set_param -n $nsdir.lru_max_age $max_age
10714         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
10715         unlinkmany $DIR/$tdir/f $nr
10716 }
10717 run_test 124c "LRUR cancel very aged locks"
10718
10719 test_124d() {
10720         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10721         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
10722                 skip_env "no lru resize on server"
10723
10724         # cache ununsed locks on client
10725         local nr=100
10726
10727         lru_resize_disable mdc
10728         stack_trap "lru_resize_enable mdc" EXIT
10729
10730         cancel_lru_locks mdc
10731
10732         # asynchronous object destroy at MDT could cause bl ast to client
10733         test_mkdir $DIR/$tdir
10734         createmany -o $DIR/$tdir/f $nr ||
10735                 error "failed to create $nr files in $DIR/$tdir"
10736         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
10737
10738         ls -l $DIR/$tdir > /dev/null
10739
10740         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10741         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10742         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
10743         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
10744
10745         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
10746
10747         # set lru_max_age to 1 sec
10748         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
10749         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
10750
10751         echo "sleep $((recalc_p * 2)) seconds..."
10752         sleep $((recalc_p * 2))
10753
10754         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
10755
10756         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
10757 }
10758 run_test 124d "cancel very aged locks if lru-resize diasbaled"
10759
10760 test_125() { # 13358
10761         $LCTL get_param -n llite.*.client_type | grep -q local ||
10762                 skip "must run as local client"
10763         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
10764                 skip_env "must have acl enabled"
10765         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
10766
10767         test_mkdir $DIR/$tdir
10768         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
10769         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
10770         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
10771 }
10772 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
10773
10774 test_126() { # bug 12829/13455
10775         $GSS && skip_env "must run as gss disabled"
10776         $LCTL get_param -n llite.*.client_type | grep -q local ||
10777                 skip "must run as local client"
10778         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
10779
10780         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
10781         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
10782         rm -f $DIR/$tfile
10783         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
10784 }
10785 run_test 126 "check that the fsgid provided by the client is taken into account"
10786
10787 test_127a() { # bug 15521
10788         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10789
10790         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
10791         $LCTL set_param osc.*.stats=0
10792         FSIZE=$((2048 * 1024))
10793         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
10794         cancel_lru_locks osc
10795         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
10796
10797         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
10798         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
10799                 echo "got $COUNT $NAME"
10800                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
10801                 eval $NAME=$COUNT || error "Wrong proc format"
10802
10803                 case $NAME in
10804                         read_bytes|write_bytes)
10805                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
10806                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
10807                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
10808                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
10809                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
10810                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
10811                                 error "sumsquare is too small: $SUMSQ"
10812                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
10813                                 error "sumsquare is too big: $SUMSQ"
10814                         ;;
10815                         *) ;;
10816                 esac
10817         done < $DIR/${tfile}.tmp
10818
10819         #check that we actually got some stats
10820         [ "$read_bytes" ] || error "Missing read_bytes stats"
10821         [ "$write_bytes" ] || error "Missing write_bytes stats"
10822         [ "$read_bytes" != 0 ] || error "no read done"
10823         [ "$write_bytes" != 0 ] || error "no write done"
10824 }
10825 run_test 127a "verify the client stats are sane"
10826
10827 test_127b() { # bug LU-333
10828         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10829         local name count samp unit min max sum sumsq
10830
10831         $LCTL set_param llite.*.stats=0
10832
10833         # perform 2 reads and writes so MAX is different from SUM.
10834         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10835         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
10836         cancel_lru_locks osc
10837         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10838         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
10839
10840         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
10841         while read name count samp unit min max sum sumsq; do
10842                 echo "got $count $name"
10843                 eval $name=$count || error "Wrong proc format"
10844
10845                 case $name in
10846                 read_bytes)
10847                         [ $count -ne 2 ] && error "count is not 2: $count"
10848                         [ $min -ne $PAGE_SIZE ] &&
10849                                 error "min is not $PAGE_SIZE: $min"
10850                         [ $max -ne $PAGE_SIZE ] &&
10851                                 error "max is incorrect: $max"
10852                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10853                                 error "sum is wrong: $sum"
10854                         ;;
10855                 write_bytes)
10856                         [ $count -ne 2 ] && error "count is not 2: $count"
10857                         [ $min -ne $PAGE_SIZE ] &&
10858                                 error "min is not $PAGE_SIZE: $min"
10859                         [ $max -ne $PAGE_SIZE ] &&
10860                                 error "max is incorrect: $max"
10861                         [ $sum -ne $((PAGE_SIZE * 2)) ] &&
10862                                 error "sum is wrong: $sum"
10863                         ;;
10864                 *) ;;
10865                 esac
10866         done < $TMP/$tfile.tmp
10867
10868         #check that we actually got some stats
10869         [ "$read_bytes" ] || error "Missing read_bytes stats"
10870         [ "$write_bytes" ] || error "Missing write_bytes stats"
10871         [ "$read_bytes" != 0 ] || error "no read done"
10872         [ "$write_bytes" != 0 ] || error "no write done"
10873
10874         rm -f $TMP/${tfile}.tmp
10875 }
10876 run_test 127b "verify the llite client stats are sane"
10877
10878 test_127c() { # LU-12394
10879         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
10880         local size
10881         local bsize
10882         local reads
10883         local writes
10884         local count
10885
10886         $LCTL set_param llite.*.extents_stats=1
10887         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
10888
10889         # Use two stripes so there is enough space in default config
10890         $LFS setstripe -c 2 $DIR/$tfile
10891
10892         # Extent stats start at 0-4K and go in power of two buckets
10893         # LL_HIST_START = 12 --> 2^12 = 4K
10894         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
10895         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
10896         # small configs
10897         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
10898                 do
10899                 # Write and read, 2x each, second time at a non-zero offset
10900                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
10901                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
10902                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
10903                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
10904                 rm -f $DIR/$tfile
10905         done
10906
10907         $LCTL get_param llite.*.extents_stats
10908
10909         count=2
10910         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
10911                 do
10912                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
10913                                 grep -m 1 $bsize)
10914                 reads=$(echo $bucket | awk '{print $5}')
10915                 writes=$(echo $bucket | awk '{print $9}')
10916                 [ "$reads" -eq $count ] ||
10917                         error "$reads reads in < $bsize bucket, expect $count"
10918                 [ "$writes" -eq $count ] ||
10919                         error "$writes writes in < $bsize bucket, expect $count"
10920         done
10921
10922         # Test mmap write and read
10923         $LCTL set_param llite.*.extents_stats=c
10924         size=512
10925         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
10926         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
10927         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
10928
10929         $LCTL get_param llite.*.extents_stats
10930
10931         count=$(((size*1024) / PAGE_SIZE))
10932
10933         bsize=$((2 * PAGE_SIZE / 1024))K
10934
10935         bucket=$($LCTL get_param -n llite.*.extents_stats |
10936                         grep -m 1 $bsize)
10937         reads=$(echo $bucket | awk '{print $5}')
10938         writes=$(echo $bucket | awk '{print $9}')
10939         # mmap writes fault in the page first, creating an additonal read
10940         [ "$reads" -eq $((2 * count)) ] ||
10941                 error "$reads reads in < $bsize bucket, expect $count"
10942         [ "$writes" -eq $count ] ||
10943                 error "$writes writes in < $bsize bucket, expect $count"
10944 }
10945 run_test 127c "test llite extent stats with regular & mmap i/o"
10946
10947 test_128() { # bug 15212
10948         touch $DIR/$tfile
10949         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
10950                 find $DIR/$tfile
10951                 find $DIR/$tfile
10952         EOF
10953
10954         result=$(grep error $TMP/$tfile.log)
10955         rm -f $DIR/$tfile $TMP/$tfile.log
10956         [ -z "$result" ] ||
10957                 error "consecutive find's under interactive lfs failed"
10958 }
10959 run_test 128 "interactive lfs for 2 consecutive find's"
10960
10961 set_dir_limits () {
10962         local mntdev
10963         local canondev
10964         local node
10965
10966         local ldproc=/proc/fs/ldiskfs
10967         local facets=$(get_facets MDS)
10968
10969         for facet in ${facets//,/ }; do
10970                 canondev=$(ldiskfs_canon \
10971                            *.$(convert_facet2label $facet).mntdev $facet)
10972                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
10973                         ldproc=/sys/fs/ldiskfs
10974                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
10975                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
10976         done
10977 }
10978
10979 check_mds_dmesg() {
10980         local facets=$(get_facets MDS)
10981         for facet in ${facets//,/ }; do
10982                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
10983         done
10984         return 1
10985 }
10986
10987 test_129() {
10988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10989         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
10990                 skip "Need MDS version with at least 2.5.56"
10991         if [ "$mds1_FSTYPE" != ldiskfs ]; then
10992                 skip_env "ldiskfs only test"
10993         fi
10994         remote_mds_nodsh && skip "remote MDS with nodsh"
10995
10996         local ENOSPC=28
10997         local EFBIG=27
10998         local has_warning=false
10999
11000         rm -rf $DIR/$tdir
11001         mkdir -p $DIR/$tdir
11002
11003         # block size of mds1
11004         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
11005         set_dir_limits $maxsize $maxsize
11006         local dirsize=$(stat -c%s "$DIR/$tdir")
11007         local nfiles=0
11008         while [[ $dirsize -le $maxsize ]]; do
11009                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
11010                 rc=$?
11011                 if ! $has_warning; then
11012                         check_mds_dmesg '"is approaching"' && has_warning=true
11013                 fi
11014                 # check two errors:
11015                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
11016                 # EFBIG for previous versions included in ldiskfs series
11017                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
11018                         set_dir_limits 0 0
11019                         echo "return code $rc received as expected"
11020
11021                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
11022                                 error_exit "create failed w/o dir size limit"
11023
11024                         check_mds_dmesg '"has reached"' ||
11025                                 error_exit "reached message should be output"
11026
11027                         [ $has_warning = "false" ] &&
11028                                 error_exit "warning message should be output"
11029
11030                         dirsize=$(stat -c%s "$DIR/$tdir")
11031
11032                         [[ $dirsize -ge $maxsize ]] && return 0
11033                         error_exit "current dir size $dirsize, " \
11034                                    "previous limit $maxsize"
11035                 elif [ $rc -ne 0 ]; then
11036                         set_dir_limits 0 0
11037                         error_exit "return $rc received instead of expected " \
11038                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
11039                 fi
11040                 nfiles=$((nfiles + 1))
11041                 dirsize=$(stat -c%s "$DIR/$tdir")
11042         done
11043
11044         set_dir_limits 0 0
11045         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
11046 }
11047 run_test 129 "test directory size limit ========================"
11048
11049 OLDIFS="$IFS"
11050 cleanup_130() {
11051         trap 0
11052         IFS="$OLDIFS"
11053 }
11054
11055 test_130a() {
11056         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11057         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11058
11059         trap cleanup_130 EXIT RETURN
11060
11061         local fm_file=$DIR/$tfile
11062         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
11063         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
11064                 error "dd failed for $fm_file"
11065
11066         # LU-1795: test filefrag/FIEMAP once, even if unsupported
11067         filefrag -ves $fm_file
11068         RC=$?
11069         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11070                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11071         [ $RC != 0 ] && error "filefrag $fm_file failed"
11072
11073         filefrag_op=$(filefrag -ve -k $fm_file |
11074                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11075         lun=$($GETSTRIPE -i $fm_file)
11076
11077         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
11078         IFS=$'\n'
11079         tot_len=0
11080         for line in $filefrag_op
11081         do
11082                 frag_lun=`echo $line | cut -d: -f5`
11083                 ext_len=`echo $line | cut -d: -f4`
11084                 if (( $frag_lun != $lun )); then
11085                         cleanup_130
11086                         error "FIEMAP on 1-stripe file($fm_file) failed"
11087                         return
11088                 fi
11089                 (( tot_len += ext_len ))
11090         done
11091
11092         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
11093                 cleanup_130
11094                 error "FIEMAP on 1-stripe file($fm_file) failed;"
11095                 return
11096         fi
11097
11098         cleanup_130
11099
11100         echo "FIEMAP on single striped file succeeded"
11101 }
11102 run_test 130a "FIEMAP (1-stripe file)"
11103
11104 test_130b() {
11105         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11106
11107         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11108         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11109
11110         trap cleanup_130 EXIT RETURN
11111
11112         local fm_file=$DIR/$tfile
11113         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11114                         error "setstripe on $fm_file"
11115         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11116                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11117
11118         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
11119                 error "dd failed on $fm_file"
11120
11121         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11122         filefrag_op=$(filefrag -ve -k $fm_file |
11123                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11124
11125         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11126                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11127
11128         IFS=$'\n'
11129         tot_len=0
11130         num_luns=1
11131         for line in $filefrag_op
11132         do
11133                 frag_lun=$(echo $line | cut -d: -f5 |
11134                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11135                 ext_len=$(echo $line | cut -d: -f4)
11136                 if (( $frag_lun != $last_lun )); then
11137                         if (( tot_len != 1024 )); then
11138                                 cleanup_130
11139                                 error "FIEMAP on $fm_file failed; returned " \
11140                                 "len $tot_len for OST $last_lun instead of 1024"
11141                                 return
11142                         else
11143                                 (( num_luns += 1 ))
11144                                 tot_len=0
11145                         fi
11146                 fi
11147                 (( tot_len += ext_len ))
11148                 last_lun=$frag_lun
11149         done
11150         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
11151                 cleanup_130
11152                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11153                         "luns or wrong len for OST $last_lun"
11154                 return
11155         fi
11156
11157         cleanup_130
11158
11159         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
11160 }
11161 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
11162
11163 test_130c() {
11164         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11165
11166         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11167         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11168
11169         trap cleanup_130 EXIT RETURN
11170
11171         local fm_file=$DIR/$tfile
11172         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
11173         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11174                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11175
11176         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
11177                         error "dd failed on $fm_file"
11178
11179         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11180         filefrag_op=$(filefrag -ve -k $fm_file |
11181                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11182
11183         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11184                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11185
11186         IFS=$'\n'
11187         tot_len=0
11188         num_luns=1
11189         for line in $filefrag_op
11190         do
11191                 frag_lun=$(echo $line | cut -d: -f5 |
11192                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11193                 ext_len=$(echo $line | cut -d: -f4)
11194                 if (( $frag_lun != $last_lun )); then
11195                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
11196                         if (( logical != 512 )); then
11197                                 cleanup_130
11198                                 error "FIEMAP on $fm_file failed; returned " \
11199                                 "logical start for lun $logical instead of 512"
11200                                 return
11201                         fi
11202                         if (( tot_len != 512 )); then
11203                                 cleanup_130
11204                                 error "FIEMAP on $fm_file failed; returned " \
11205                                 "len $tot_len for OST $last_lun instead of 1024"
11206                                 return
11207                         else
11208                                 (( num_luns += 1 ))
11209                                 tot_len=0
11210                         fi
11211                 fi
11212                 (( tot_len += ext_len ))
11213                 last_lun=$frag_lun
11214         done
11215         if (( num_luns != 2 || tot_len != 512 )); then
11216                 cleanup_130
11217                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11218                         "luns or wrong len for OST $last_lun"
11219                 return
11220         fi
11221
11222         cleanup_130
11223
11224         echo "FIEMAP on 2-stripe file with hole succeeded"
11225 }
11226 run_test 130c "FIEMAP (2-stripe file with hole)"
11227
11228 test_130d() {
11229         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
11230
11231         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11232         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11233
11234         trap cleanup_130 EXIT RETURN
11235
11236         local fm_file=$DIR/$tfile
11237         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
11238                         error "setstripe on $fm_file"
11239         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11240                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11241
11242         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
11243         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
11244                 error "dd failed on $fm_file"
11245
11246         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11247         filefrag_op=$(filefrag -ve -k $fm_file |
11248                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11249
11250         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11251                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11252
11253         IFS=$'\n'
11254         tot_len=0
11255         num_luns=1
11256         for line in $filefrag_op
11257         do
11258                 frag_lun=$(echo $line | cut -d: -f5 |
11259                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11260                 ext_len=$(echo $line | cut -d: -f4)
11261                 if (( $frag_lun != $last_lun )); then
11262                         if (( tot_len != 1024 )); then
11263                                 cleanup_130
11264                                 error "FIEMAP on $fm_file failed; returned " \
11265                                 "len $tot_len for OST $last_lun instead of 1024"
11266                                 return
11267                         else
11268                                 (( num_luns += 1 ))
11269                                 tot_len=0
11270                         fi
11271                 fi
11272                 (( tot_len += ext_len ))
11273                 last_lun=$frag_lun
11274         done
11275         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
11276                 cleanup_130
11277                 error "FIEMAP on $fm_file failed; returned wrong number of " \
11278                         "luns or wrong len for OST $last_lun"
11279                 return
11280         fi
11281
11282         cleanup_130
11283
11284         echo "FIEMAP on N-stripe file succeeded"
11285 }
11286 run_test 130d "FIEMAP (N-stripe file)"
11287
11288 test_130e() {
11289         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11290
11291         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11292         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
11293
11294         trap cleanup_130 EXIT RETURN
11295
11296         local fm_file=$DIR/$tfile
11297         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
11298         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
11299                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
11300
11301         NUM_BLKS=512
11302         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
11303         for ((i = 0; i < $NUM_BLKS; i++))
11304         do
11305                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
11306         done
11307
11308         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11309         filefrag_op=$(filefrag -ve -k $fm_file |
11310                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
11311
11312         last_lun=$(echo $filefrag_op | cut -d: -f5 |
11313                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11314
11315         IFS=$'\n'
11316         tot_len=0
11317         num_luns=1
11318         for line in $filefrag_op
11319         do
11320                 frag_lun=$(echo $line | cut -d: -f5 |
11321                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
11322                 ext_len=$(echo $line | cut -d: -f4)
11323                 if (( $frag_lun != $last_lun )); then
11324                         if (( tot_len != $EXPECTED_LEN )); then
11325                                 cleanup_130
11326                                 error "FIEMAP on $fm_file failed; returned " \
11327                                 "len $tot_len for OST $last_lun instead " \
11328                                 "of $EXPECTED_LEN"
11329                                 return
11330                         else
11331                                 (( num_luns += 1 ))
11332                                 tot_len=0
11333                         fi
11334                 fi
11335                 (( tot_len += ext_len ))
11336                 last_lun=$frag_lun
11337         done
11338         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
11339                 cleanup_130
11340                 error "FIEMAP on $fm_file failed; returned wrong number " \
11341                         "of luns or wrong len for OST $last_lun"
11342                 return
11343         fi
11344
11345         cleanup_130
11346
11347         echo "FIEMAP with continuation calls succeeded"
11348 }
11349 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
11350
11351 test_130f() {
11352         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
11353         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
11354
11355         local fm_file=$DIR/$tfile
11356         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
11357                 error "multiop create with lov_delay_create on $fm_file"
11358
11359         filefrag -ves $fm_file || error "filefrag $fm_file failed"
11360         filefrag_extents=$(filefrag -vek $fm_file |
11361                            awk '/extents? found/ { print $2 }')
11362         if [[ "$filefrag_extents" != "0" ]]; then
11363                 error "FIEMAP on $fm_file failed; " \
11364                       "returned $filefrag_extents expected 0"
11365         fi
11366
11367         rm -f $fm_file
11368 }
11369 run_test 130f "FIEMAP (unstriped file)"
11370
11371 # Test for writev/readv
11372 test_131a() {
11373         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
11374                 error "writev test failed"
11375         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
11376                 error "readv failed"
11377         rm -f $DIR/$tfile
11378 }
11379 run_test 131a "test iov's crossing stripe boundary for writev/readv"
11380
11381 test_131b() {
11382         local fsize=$((524288 + 1048576 + 1572864))
11383         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
11384                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11385                         error "append writev test failed"
11386
11387         ((fsize += 1572864 + 1048576))
11388         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
11389                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
11390                         error "append writev test failed"
11391         rm -f $DIR/$tfile
11392 }
11393 run_test 131b "test append writev"
11394
11395 test_131c() {
11396         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
11397         error "NOT PASS"
11398 }
11399 run_test 131c "test read/write on file w/o objects"
11400
11401 test_131d() {
11402         rwv -f $DIR/$tfile -w -n 1 1572864
11403         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
11404         if [ "$NOB" != 1572864 ]; then
11405                 error "Short read filed: read $NOB bytes instead of 1572864"
11406         fi
11407         rm -f $DIR/$tfile
11408 }
11409 run_test 131d "test short read"
11410
11411 test_131e() {
11412         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
11413         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
11414         error "read hitting hole failed"
11415         rm -f $DIR/$tfile
11416 }
11417 run_test 131e "test read hitting hole"
11418
11419 check_stats() {
11420         local facet=$1
11421         local op=$2
11422         local want=${3:-0}
11423         local res
11424
11425         case $facet in
11426         mds*) res=$(do_facet $facet \
11427                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
11428                  ;;
11429         ost*) res=$(do_facet $facet \
11430                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
11431                  ;;
11432         *) error "Wrong facet '$facet'" ;;
11433         esac
11434         [ "$res" ] || error "The counter for $op on $facet was not incremented"
11435         # if the argument $3 is zero, it means any stat increment is ok.
11436         if [[ $want -gt 0 ]]; then
11437                 local count=$(echo $res | awk '{ print $2 }')
11438                 [[ $count -ne $want ]] &&
11439                         error "The $op counter on $facet is $count, not $want"
11440         fi
11441 }
11442
11443 test_133a() {
11444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11445         remote_ost_nodsh && skip "remote OST with nodsh"
11446         remote_mds_nodsh && skip "remote MDS with nodsh"
11447         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11448                 skip_env "MDS doesn't support rename stats"
11449
11450         local testdir=$DIR/${tdir}/stats_testdir
11451
11452         mkdir -p $DIR/${tdir}
11453
11454         # clear stats.
11455         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11456         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11457
11458         # verify mdt stats first.
11459         mkdir ${testdir} || error "mkdir failed"
11460         check_stats $SINGLEMDS "mkdir" 1
11461         touch ${testdir}/${tfile} || error "touch failed"
11462         check_stats $SINGLEMDS "open" 1
11463         check_stats $SINGLEMDS "close" 1
11464         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
11465                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
11466                 check_stats $SINGLEMDS "mknod" 2
11467         }
11468         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
11469         check_stats $SINGLEMDS "unlink" 1
11470         rm -f ${testdir}/${tfile} || error "file remove failed"
11471         check_stats $SINGLEMDS "unlink" 2
11472
11473         # remove working dir and check mdt stats again.
11474         rmdir ${testdir} || error "rmdir failed"
11475         check_stats $SINGLEMDS "rmdir" 1
11476
11477         local testdir1=$DIR/${tdir}/stats_testdir1
11478         mkdir -p ${testdir}
11479         mkdir -p ${testdir1}
11480         touch ${testdir1}/test1
11481         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
11482         check_stats $SINGLEMDS "crossdir_rename" 1
11483
11484         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
11485         check_stats $SINGLEMDS "samedir_rename" 1
11486
11487         rm -rf $DIR/${tdir}
11488 }
11489 run_test 133a "Verifying MDT stats ========================================"
11490
11491 test_133b() {
11492         local res
11493
11494         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11495         remote_ost_nodsh && skip "remote OST with nodsh"
11496         remote_mds_nodsh && skip "remote MDS with nodsh"
11497
11498         local testdir=$DIR/${tdir}/stats_testdir
11499
11500         mkdir -p ${testdir} || error "mkdir failed"
11501         touch ${testdir}/${tfile} || error "touch failed"
11502         cancel_lru_locks mdc
11503
11504         # clear stats.
11505         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11506         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11507
11508         # extra mdt stats verification.
11509         chmod 444 ${testdir}/${tfile} || error "chmod failed"
11510         check_stats $SINGLEMDS "setattr" 1
11511         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11512         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
11513         then            # LU-1740
11514                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
11515                 check_stats $SINGLEMDS "getattr" 1
11516         fi
11517         rm -rf $DIR/${tdir}
11518
11519         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
11520         # so the check below is not reliable
11521         [ $MDSCOUNT -eq 1 ] || return 0
11522
11523         # Sleep to avoid a cached response.
11524         #define OBD_STATFS_CACHE_SECONDS 1
11525         sleep 2
11526         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11527         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11528         $LFS df || error "lfs failed"
11529         check_stats $SINGLEMDS "statfs" 1
11530
11531         # check aggregated statfs (LU-10018)
11532         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
11533                 return 0
11534         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
11535                 return 0
11536         sleep 2
11537         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11538         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
11539         df $DIR
11540         check_stats $SINGLEMDS "statfs" 1
11541
11542         # We want to check that the client didn't send OST_STATFS to
11543         # ost1 but the MDT also uses OST_STATFS for precreate. So some
11544         # extra care is needed here.
11545         if remote_mds; then
11546                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
11547                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
11548
11549                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
11550                 [ "$res" ] && error "OST got STATFS"
11551         fi
11552
11553         return 0
11554 }
11555 run_test 133b "Verifying extra MDT stats =================================="
11556
11557 test_133c() {
11558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11559         remote_ost_nodsh && skip "remote OST with nodsh"
11560         remote_mds_nodsh && skip "remote MDS with nodsh"
11561
11562         local testdir=$DIR/$tdir/stats_testdir
11563
11564         test_mkdir -p $testdir
11565
11566         # verify obdfilter stats.
11567         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
11568         sync
11569         cancel_lru_locks osc
11570         wait_delete_completed
11571
11572         # clear stats.
11573         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
11574         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
11575
11576         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
11577                 error "dd failed"
11578         sync
11579         cancel_lru_locks osc
11580         check_stats ost1 "write" 1
11581
11582         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
11583         check_stats ost1 "read" 1
11584
11585         > $testdir/$tfile || error "truncate failed"
11586         check_stats ost1 "punch" 1
11587
11588         rm -f $testdir/$tfile || error "file remove failed"
11589         wait_delete_completed
11590         check_stats ost1 "destroy" 1
11591
11592         rm -rf $DIR/$tdir
11593 }
11594 run_test 133c "Verifying OST stats ========================================"
11595
11596 order_2() {
11597         local value=$1
11598         local orig=$value
11599         local order=1
11600
11601         while [ $value -ge 2 ]; do
11602                 order=$((order*2))
11603                 value=$((value/2))
11604         done
11605
11606         if [ $orig -gt $order ]; then
11607                 order=$((order*2))
11608         fi
11609         echo $order
11610 }
11611
11612 size_in_KMGT() {
11613     local value=$1
11614     local size=('K' 'M' 'G' 'T');
11615     local i=0
11616     local size_string=$value
11617
11618     while [ $value -ge 1024 ]; do
11619         if [ $i -gt 3 ]; then
11620             #T is the biggest unit we get here, if that is bigger,
11621             #just return XXXT
11622             size_string=${value}T
11623             break
11624         fi
11625         value=$((value >> 10))
11626         if [ $value -lt 1024 ]; then
11627             size_string=${value}${size[$i]}
11628             break
11629         fi
11630         i=$((i + 1))
11631     done
11632
11633     echo $size_string
11634 }
11635
11636 get_rename_size() {
11637         local size=$1
11638         local context=${2:-.}
11639         local sample=$(do_facet $SINGLEMDS $LCTL \
11640                 get_param mdt.$FSNAME-MDT0000.rename_stats |
11641                 grep -A1 $context |
11642                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
11643         echo $sample
11644 }
11645
11646 test_133d() {
11647         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11648         remote_ost_nodsh && skip "remote OST with nodsh"
11649         remote_mds_nodsh && skip "remote MDS with nodsh"
11650         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
11651                 skip_env "MDS doesn't support rename stats"
11652
11653         local testdir1=$DIR/${tdir}/stats_testdir1
11654         local testdir2=$DIR/${tdir}/stats_testdir2
11655         mkdir -p $DIR/${tdir}
11656
11657         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11658
11659         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
11660         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
11661
11662         createmany -o $testdir1/test 512 || error "createmany failed"
11663
11664         # check samedir rename size
11665         mv ${testdir1}/test0 ${testdir1}/test_0
11666
11667         local testdir1_size=$(ls -l $DIR/${tdir} |
11668                 awk '/stats_testdir1/ {print $5}')
11669         local testdir2_size=$(ls -l $DIR/${tdir} |
11670                 awk '/stats_testdir2/ {print $5}')
11671
11672         testdir1_size=$(order_2 $testdir1_size)
11673         testdir2_size=$(order_2 $testdir2_size)
11674
11675         testdir1_size=$(size_in_KMGT $testdir1_size)
11676         testdir2_size=$(size_in_KMGT $testdir2_size)
11677
11678         echo "source rename dir size: ${testdir1_size}"
11679         echo "target rename dir size: ${testdir2_size}"
11680
11681         local cmd="do_facet $SINGLEMDS $LCTL "
11682         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
11683
11684         eval $cmd || error "$cmd failed"
11685         local samedir=$($cmd | grep 'same_dir')
11686         local same_sample=$(get_rename_size $testdir1_size)
11687         [ -z "$samedir" ] && error "samedir_rename_size count error"
11688         [[ $same_sample -eq 1 ]] ||
11689                 error "samedir_rename_size error $same_sample"
11690         echo "Check same dir rename stats success"
11691
11692         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
11693
11694         # check crossdir rename size
11695         mv ${testdir1}/test_0 ${testdir2}/test_0
11696
11697         testdir1_size=$(ls -l $DIR/${tdir} |
11698                 awk '/stats_testdir1/ {print $5}')
11699         testdir2_size=$(ls -l $DIR/${tdir} |
11700                 awk '/stats_testdir2/ {print $5}')
11701
11702         testdir1_size=$(order_2 $testdir1_size)
11703         testdir2_size=$(order_2 $testdir2_size)
11704
11705         testdir1_size=$(size_in_KMGT $testdir1_size)
11706         testdir2_size=$(size_in_KMGT $testdir2_size)
11707
11708         echo "source rename dir size: ${testdir1_size}"
11709         echo "target rename dir size: ${testdir2_size}"
11710
11711         eval $cmd || error "$cmd failed"
11712         local crossdir=$($cmd | grep 'crossdir')
11713         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
11714         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
11715         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
11716         [[ $src_sample -eq 1 ]] ||
11717                 error "crossdir_rename_size error $src_sample"
11718         [[ $tgt_sample -eq 1 ]] ||
11719                 error "crossdir_rename_size error $tgt_sample"
11720         echo "Check cross dir rename stats success"
11721         rm -rf $DIR/${tdir}
11722 }
11723 run_test 133d "Verifying rename_stats ========================================"
11724
11725 test_133e() {
11726         remote_mds_nodsh && skip "remote MDS with nodsh"
11727         remote_ost_nodsh && skip "remote OST with nodsh"
11728         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11729
11730         local testdir=$DIR/${tdir}/stats_testdir
11731         local ctr f0 f1 bs=32768 count=42 sum
11732
11733         mkdir -p ${testdir} || error "mkdir failed"
11734
11735         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
11736
11737         for ctr in {write,read}_bytes; do
11738                 sync
11739                 cancel_lru_locks osc
11740
11741                 do_facet ost1 $LCTL set_param -n \
11742                         "obdfilter.*.exports.clear=clear"
11743
11744                 if [ $ctr = write_bytes ]; then
11745                         f0=/dev/zero
11746                         f1=${testdir}/${tfile}
11747                 else
11748                         f0=${testdir}/${tfile}
11749                         f1=/dev/null
11750                 fi
11751
11752                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
11753                         error "dd failed"
11754                 sync
11755                 cancel_lru_locks osc
11756
11757                 sum=$(do_facet ost1 $LCTL get_param \
11758                         "obdfilter.*.exports.*.stats" |
11759                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
11760                                 $1 == ctr { sum += $7 }
11761                                 END { printf("%0.0f", sum) }')
11762
11763                 if ((sum != bs * count)); then
11764                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
11765                 fi
11766         done
11767
11768         rm -rf $DIR/${tdir}
11769 }
11770 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
11771
11772 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
11773
11774 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
11775 # not honor the -ignore_readdir_race option correctly. So we call
11776 # error_ignore() rather than error() in these cases. See LU-11152.
11777 error_133() {
11778         if (find --version; do_facet mds1 find --version) |
11779                 grep -q '\b4\.5\.1[1-4]\b'; then
11780                 error_ignore LU-11152 "$@"
11781         else
11782                 error "$@"
11783         fi
11784 }
11785
11786 test_133f() {
11787         # First without trusting modes.
11788         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
11789         echo "proc_dirs='$proc_dirs'"
11790         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
11791         find $proc_dirs -exec cat '{}' \; &> /dev/null
11792
11793         # Second verifying readability.
11794         $LCTL get_param -R '*' &> /dev/null
11795
11796         # Verifing writability with badarea_io.
11797         find $proc_dirs \
11798                 -ignore_readdir_race \
11799                 -type f \
11800                 -not -name force_lbug \
11801                 -not -name changelog_mask \
11802                 -exec badarea_io '{}' \; ||
11803                         error_133 "find $proc_dirs failed"
11804 }
11805 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
11806
11807 test_133g() {
11808         remote_mds_nodsh && skip "remote MDS with nodsh"
11809         remote_ost_nodsh && skip "remote OST with nodsh"
11810
11811         # eventually, this can also be replaced with "lctl get_param -R",
11812         # but not until that option is always available on the server
11813         local facet
11814         for facet in mds1 ost1; do
11815                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
11816                         skip_noexit "Too old lustre on $facet"
11817                 local facet_proc_dirs=$(do_facet $facet \
11818                                         \\\ls -d $proc_regexp 2>/dev/null)
11819                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11820                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11821                 do_facet $facet find $facet_proc_dirs \
11822                         ! -name req_history \
11823                         -exec cat '{}' \\\; &> /dev/null
11824
11825                 do_facet $facet find $facet_proc_dirs \
11826                         ! -name req_history \
11827                         -type f \
11828                         -exec cat '{}' \\\; &> /dev/null ||
11829                                 error "proc file read failed"
11830
11831                 do_facet $facet find $facet_proc_dirs \
11832                         -ignore_readdir_race \
11833                         -type f \
11834                         -not -name force_lbug \
11835                         -not -name changelog_mask \
11836                         -exec badarea_io '{}' \\\; ||
11837                                 error_133 "$facet find $facet_proc_dirs failed"
11838         done
11839
11840         # remount the FS in case writes/reads /proc break the FS
11841         cleanup || error "failed to unmount"
11842         setup || error "failed to setup"
11843         true
11844 }
11845 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
11846
11847 test_133h() {
11848         remote_mds_nodsh && skip "remote MDS with nodsh"
11849         remote_ost_nodsh && skip "remote OST with nodsh"
11850         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
11851                 skip "Need MDS version at least 2.9.54"
11852
11853         local facet
11854
11855         for facet in client mds1 ost1; do
11856                 local facet_proc_dirs=$(do_facet $facet \
11857                                         \\\ls -d $proc_regexp 2> /dev/null)
11858                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
11859                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
11860                 # Get the list of files that are missing the terminating newline
11861                 local missing=($(do_facet $facet \
11862                         find ${facet_proc_dirs} -type f \|              \
11863                                 while read F\; do                       \
11864                                         awk -v FS='\v' -v RS='\v\v'     \
11865                                         "'END { if(NR>0 &&              \
11866                                         \\\$NF !~ /.*\\\n\$/)           \
11867                                                 print FILENAME}'"       \
11868                                         '\$F'\;                         \
11869                                 done 2>/dev/null))
11870                 [ ${#missing[*]} -eq 0 ] ||
11871                         error "files do not end with newline: ${missing[*]}"
11872         done
11873 }
11874 run_test 133h "Proc files should end with newlines"
11875
11876 test_134a() {
11877         remote_mds_nodsh && skip "remote MDS with nodsh"
11878         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11879                 skip "Need MDS version at least 2.7.54"
11880
11881         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11882         cancel_lru_locks mdc
11883
11884         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11885         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11886         [ $unused -eq 0 ] || error "$unused locks are not cleared"
11887
11888         local nr=1000
11889         createmany -o $DIR/$tdir/f $nr ||
11890                 error "failed to create $nr files in $DIR/$tdir"
11891         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11892
11893         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
11894         do_facet mds1 $LCTL set_param fail_loc=0x327
11895         do_facet mds1 $LCTL set_param fail_val=500
11896         touch $DIR/$tdir/m
11897
11898         echo "sleep 10 seconds ..."
11899         sleep 10
11900         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
11901
11902         do_facet mds1 $LCTL set_param fail_loc=0
11903         do_facet mds1 $LCTL set_param fail_val=0
11904         [ $lck_cnt -lt $unused ] ||
11905                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
11906
11907         rm $DIR/$tdir/m
11908         unlinkmany $DIR/$tdir/f $nr
11909 }
11910 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
11911
11912 test_134b() {
11913         remote_mds_nodsh && skip "remote MDS with nodsh"
11914         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
11915                 skip "Need MDS version at least 2.7.54"
11916
11917         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
11918         cancel_lru_locks mdc
11919
11920         local low_wm=$(do_facet mds1 $LCTL get_param -n \
11921                         ldlm.lock_reclaim_threshold_mb)
11922         # disable reclaim temporarily
11923         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
11924
11925         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
11926         do_facet mds1 $LCTL set_param fail_loc=0x328
11927         do_facet mds1 $LCTL set_param fail_val=500
11928
11929         $LCTL set_param debug=+trace
11930
11931         local nr=600
11932         createmany -o $DIR/$tdir/f $nr &
11933         local create_pid=$!
11934
11935         echo "Sleep $TIMEOUT seconds ..."
11936         sleep $TIMEOUT
11937         if ! ps -p $create_pid  > /dev/null 2>&1; then
11938                 do_facet mds1 $LCTL set_param fail_loc=0
11939                 do_facet mds1 $LCTL set_param fail_val=0
11940                 do_facet mds1 $LCTL set_param \
11941                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
11942                 error "createmany finished incorrectly!"
11943         fi
11944         do_facet mds1 $LCTL set_param fail_loc=0
11945         do_facet mds1 $LCTL set_param fail_val=0
11946         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
11947         wait $create_pid || return 1
11948
11949         unlinkmany $DIR/$tdir/f $nr
11950 }
11951 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
11952
11953 test_140() { #bug-17379
11954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11955
11956         test_mkdir $DIR/$tdir
11957         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
11958         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
11959
11960         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
11961         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
11962         local i=0
11963         while i=$((i + 1)); do
11964                 test_mkdir $i
11965                 cd $i || error "Changing to $i"
11966                 ln -s ../stat stat || error "Creating stat symlink"
11967                 # Read the symlink until ELOOP present,
11968                 # not LBUGing the system is considered success,
11969                 # we didn't overrun the stack.
11970                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
11971                 if [ $ret -ne 0 ]; then
11972                         if [ $ret -eq 40 ]; then
11973                                 break  # -ELOOP
11974                         else
11975                                 error "Open stat symlink"
11976                                         return
11977                         fi
11978                 fi
11979         done
11980         i=$((i - 1))
11981         echo "The symlink depth = $i"
11982         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
11983                                         error "Invalid symlink depth"
11984
11985         # Test recursive symlink
11986         ln -s symlink_self symlink_self
11987         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
11988         echo "open symlink_self returns $ret"
11989         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
11990 }
11991 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
11992
11993 test_150() {
11994         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11995
11996         local TF="$TMP/$tfile"
11997
11998         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
11999         cp $TF $DIR/$tfile
12000         cancel_lru_locks $OSC
12001         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
12002         remount_client $MOUNT
12003         df -P $MOUNT
12004         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
12005
12006         $TRUNCATE $TF 6000
12007         $TRUNCATE $DIR/$tfile 6000
12008         cancel_lru_locks $OSC
12009         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
12010
12011         echo "12345" >>$TF
12012         echo "12345" >>$DIR/$tfile
12013         cancel_lru_locks $OSC
12014         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
12015
12016         echo "12345" >>$TF
12017         echo "12345" >>$DIR/$tfile
12018         cancel_lru_locks $OSC
12019         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
12020
12021         rm -f $TF
12022         true
12023 }
12024 run_test 150 "truncate/append tests"
12025
12026 #LU-2902 roc_hit was not able to read all values from lproc
12027 function roc_hit_init() {
12028         local list=$(comma_list $(osts_nodes))
12029         local dir=$DIR/$tdir-check
12030         local file=$dir/$tfile
12031         local BEFORE
12032         local AFTER
12033         local idx
12034
12035         test_mkdir $dir
12036         #use setstripe to do a write to every ost
12037         for i in $(seq 0 $((OSTCOUNT-1))); do
12038                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
12039                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
12040                 idx=$(printf %04x $i)
12041                 BEFORE=$(get_osd_param $list *OST*$idx stats |
12042                         awk '$1 == "cache_access" {sum += $7}
12043                                 END { printf("%0.0f", sum) }')
12044
12045                 cancel_lru_locks osc
12046                 cat $file >/dev/null
12047
12048                 AFTER=$(get_osd_param $list *OST*$idx stats |
12049                         awk '$1 == "cache_access" {sum += $7}
12050                                 END { printf("%0.0f", sum) }')
12051
12052                 echo BEFORE:$BEFORE AFTER:$AFTER
12053                 if ! let "AFTER - BEFORE == 4"; then
12054                         rm -rf $dir
12055                         error "roc_hit is not safe to use"
12056                 fi
12057                 rm $file
12058         done
12059
12060         rm -rf $dir
12061 }
12062
12063 function roc_hit() {
12064         local list=$(comma_list $(osts_nodes))
12065         echo $(get_osd_param $list '' stats |
12066                 awk '$1 == "cache_hit" {sum += $7}
12067                         END { printf("%0.0f", sum) }')
12068 }
12069
12070 function set_cache() {
12071         local on=1
12072
12073         if [ "$2" == "off" ]; then
12074                 on=0;
12075         fi
12076         local list=$(comma_list $(osts_nodes))
12077         set_osd_param $list '' $1_cache_enable $on
12078
12079         cancel_lru_locks osc
12080 }
12081
12082 test_151() {
12083         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12084         remote_ost_nodsh && skip "remote OST with nodsh"
12085
12086         local CPAGES=3
12087         local list=$(comma_list $(osts_nodes))
12088
12089         # check whether obdfilter is cache capable at all
12090         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
12091                 skip "not cache-capable obdfilter"
12092         fi
12093
12094         # check cache is enabled on all obdfilters
12095         if get_osd_param $list '' read_cache_enable | grep 0; then
12096                 skip "oss cache is disabled"
12097         fi
12098
12099         set_osd_param $list '' writethrough_cache_enable 1
12100
12101         # check write cache is enabled on all obdfilters
12102         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
12103                 skip "oss write cache is NOT enabled"
12104         fi
12105
12106         roc_hit_init
12107
12108         #define OBD_FAIL_OBD_NO_LRU  0x609
12109         do_nodes $list $LCTL set_param fail_loc=0x609
12110
12111         # pages should be in the case right after write
12112         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
12113                 error "dd failed"
12114
12115         local BEFORE=$(roc_hit)
12116         cancel_lru_locks osc
12117         cat $DIR/$tfile >/dev/null
12118         local AFTER=$(roc_hit)
12119
12120         do_nodes $list $LCTL set_param fail_loc=0
12121
12122         if ! let "AFTER - BEFORE == CPAGES"; then
12123                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12124         fi
12125
12126         # the following read invalidates the cache
12127         cancel_lru_locks osc
12128         set_osd_param $list '' read_cache_enable 0
12129         cat $DIR/$tfile >/dev/null
12130
12131         # now data shouldn't be found in the cache
12132         BEFORE=$(roc_hit)
12133         cancel_lru_locks osc
12134         cat $DIR/$tfile >/dev/null
12135         AFTER=$(roc_hit)
12136         if let "AFTER - BEFORE != 0"; then
12137                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12138         fi
12139
12140         set_osd_param $list '' read_cache_enable 1
12141         rm -f $DIR/$tfile
12142 }
12143 run_test 151 "test cache on oss and controls ==============================="
12144
12145 test_152() {
12146         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12147
12148         local TF="$TMP/$tfile"
12149
12150         # simulate ENOMEM during write
12151 #define OBD_FAIL_OST_NOMEM      0x226
12152         lctl set_param fail_loc=0x80000226
12153         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
12154         cp $TF $DIR/$tfile
12155         sync || error "sync failed"
12156         lctl set_param fail_loc=0
12157
12158         # discard client's cache
12159         cancel_lru_locks osc
12160
12161         # simulate ENOMEM during read
12162         lctl set_param fail_loc=0x80000226
12163         cmp $TF $DIR/$tfile || error "cmp failed"
12164         lctl set_param fail_loc=0
12165
12166         rm -f $TF
12167 }
12168 run_test 152 "test read/write with enomem ============================"
12169
12170 test_153() {
12171         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
12172 }
12173 run_test 153 "test if fdatasync does not crash ======================="
12174
12175 dot_lustre_fid_permission_check() {
12176         local fid=$1
12177         local ffid=$MOUNT/.lustre/fid/$fid
12178         local test_dir=$2
12179
12180         echo "stat fid $fid"
12181         stat $ffid > /dev/null || error "stat $ffid failed."
12182         echo "touch fid $fid"
12183         touch $ffid || error "touch $ffid failed."
12184         echo "write to fid $fid"
12185         cat /etc/hosts > $ffid || error "write $ffid failed."
12186         echo "read fid $fid"
12187         diff /etc/hosts $ffid || error "read $ffid failed."
12188         echo "append write to fid $fid"
12189         cat /etc/hosts >> $ffid || error "append write $ffid failed."
12190         echo "rename fid $fid"
12191         mv $ffid $test_dir/$tfile.1 &&
12192                 error "rename $ffid to $tfile.1 should fail."
12193         touch $test_dir/$tfile.1
12194         mv $test_dir/$tfile.1 $ffid &&
12195                 error "rename $tfile.1 to $ffid should fail."
12196         rm -f $test_dir/$tfile.1
12197         echo "truncate fid $fid"
12198         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
12199         echo "link fid $fid"
12200         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
12201         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
12202                 echo "setfacl fid $fid"
12203                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
12204                 echo "getfacl fid $fid"
12205                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
12206         fi
12207         echo "unlink fid $fid"
12208         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
12209         echo "mknod fid $fid"
12210         mknod $ffid c 1 3 && error "mknod $ffid should fail."
12211
12212         fid=[0xf00000400:0x1:0x0]
12213         ffid=$MOUNT/.lustre/fid/$fid
12214
12215         echo "stat non-exist fid $fid"
12216         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
12217         echo "write to non-exist fid $fid"
12218         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
12219         echo "link new fid $fid"
12220         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
12221
12222         mkdir -p $test_dir/$tdir
12223         touch $test_dir/$tdir/$tfile
12224         fid=$($LFS path2fid $test_dir/$tdir)
12225         rc=$?
12226         [ $rc -ne 0 ] &&
12227                 error "error: could not get fid for $test_dir/$dir/$tfile."
12228
12229         ffid=$MOUNT/.lustre/fid/$fid
12230
12231         echo "ls $fid"
12232         ls $ffid > /dev/null || error "ls $ffid failed."
12233         echo "touch $fid/$tfile.1"
12234         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
12235
12236         echo "touch $MOUNT/.lustre/fid/$tfile"
12237         touch $MOUNT/.lustre/fid/$tfile && \
12238                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
12239
12240         echo "setxattr to $MOUNT/.lustre/fid"
12241         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
12242
12243         echo "listxattr for $MOUNT/.lustre/fid"
12244         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
12245
12246         echo "delxattr from $MOUNT/.lustre/fid"
12247         setfattr -x trusted.name1 $MOUNT/.lustre/fid
12248
12249         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
12250         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
12251                 error "touch invalid fid should fail."
12252
12253         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
12254         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
12255                 error "touch non-normal fid should fail."
12256
12257         echo "rename $tdir to $MOUNT/.lustre/fid"
12258         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
12259                 error "rename to $MOUNT/.lustre/fid should fail."
12260
12261         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
12262         then            # LU-3547
12263                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
12264                 local new_obf_mode=777
12265
12266                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
12267                 chmod $new_obf_mode $DIR/.lustre/fid ||
12268                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
12269
12270                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
12271                 [ $obf_mode -eq $new_obf_mode ] ||
12272                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
12273
12274                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
12275                 chmod $old_obf_mode $DIR/.lustre/fid ||
12276                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
12277         fi
12278
12279         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
12280         fid=$($LFS path2fid $test_dir/$tfile-2)
12281
12282         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
12283         then # LU-5424
12284                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
12285                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
12286                         error "create lov data thru .lustre failed"
12287         fi
12288         echo "cp /etc/passwd $test_dir/$tfile-2"
12289         cp /etc/passwd $test_dir/$tfile-2 ||
12290                 error "copy to $test_dir/$tfile-2 failed."
12291         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
12292         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
12293                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
12294
12295         rm -rf $test_dir/tfile.lnk
12296         rm -rf $test_dir/$tfile-2
12297 }
12298
12299 test_154A() {
12300         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12301                 skip "Need MDS version at least 2.4.1"
12302
12303         local tf=$DIR/$tfile
12304         touch $tf
12305
12306         local fid=$($LFS path2fid $tf)
12307         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
12308
12309         # check that we get the same pathname back
12310         local found=$($LFS fid2path $MOUNT "$fid")
12311         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
12312         [ "$found" == "$tf" ] ||
12313                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
12314 }
12315 run_test 154A "lfs path2fid and fid2path basic checks"
12316
12317 test_154B() {
12318         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12319                 skip "Need MDS version at least 2.4.1"
12320
12321         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
12322         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
12323         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
12324         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
12325
12326         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
12327         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
12328
12329         # check that we get the same pathname
12330         echo "PFID: $PFID, name: $name"
12331         local FOUND=$($LFS fid2path $MOUNT "$PFID")
12332         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
12333         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
12334                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
12335
12336         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
12337 }
12338 run_test 154B "verify the ll_decode_linkea tool"
12339
12340 test_154a() {
12341         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12342         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12343         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12344                 skip "Need MDS version at least 2.2.51"
12345         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12346
12347         cp /etc/hosts $DIR/$tfile
12348
12349         fid=$($LFS path2fid $DIR/$tfile)
12350         rc=$?
12351         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
12352
12353         dot_lustre_fid_permission_check "$fid" $DIR ||
12354                 error "dot lustre permission check $fid failed"
12355
12356         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
12357
12358         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
12359
12360         touch $MOUNT/.lustre/file &&
12361                 error "creation is not allowed under .lustre"
12362
12363         mkdir $MOUNT/.lustre/dir &&
12364                 error "mkdir is not allowed under .lustre"
12365
12366         rm -rf $DIR/$tfile
12367 }
12368 run_test 154a "Open-by-FID"
12369
12370 test_154b() {
12371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12372         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12373         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
12374         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
12375                 skip "Need MDS version at least 2.2.51"
12376
12377         local remote_dir=$DIR/$tdir/remote_dir
12378         local MDTIDX=1
12379         local rc=0
12380
12381         mkdir -p $DIR/$tdir
12382         $LFS mkdir -i $MDTIDX $remote_dir ||
12383                 error "create remote directory failed"
12384
12385         cp /etc/hosts $remote_dir/$tfile
12386
12387         fid=$($LFS path2fid $remote_dir/$tfile)
12388         rc=$?
12389         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
12390
12391         dot_lustre_fid_permission_check "$fid" $remote_dir ||
12392                 error "dot lustre permission check $fid failed"
12393         rm -rf $DIR/$tdir
12394 }
12395 run_test 154b "Open-by-FID for remote directory"
12396
12397 test_154c() {
12398         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
12399                 skip "Need MDS version at least 2.4.1"
12400
12401         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
12402         local FID1=$($LFS path2fid $DIR/$tfile.1)
12403         local FID2=$($LFS path2fid $DIR/$tfile.2)
12404         local FID3=$($LFS path2fid $DIR/$tfile.3)
12405
12406         local N=1
12407         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
12408                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
12409                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
12410                 local want=FID$N
12411                 [ "$FID" = "${!want}" ] ||
12412                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
12413                 N=$((N + 1))
12414         done
12415
12416         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
12417         do
12418                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
12419                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
12420                 N=$((N + 1))
12421         done
12422 }
12423 run_test 154c "lfs path2fid and fid2path multiple arguments"
12424
12425 test_154d() {
12426         remote_mds_nodsh && skip "remote MDS with nodsh"
12427         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
12428                 skip "Need MDS version at least 2.5.53"
12429
12430         if remote_mds; then
12431                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
12432         else
12433                 nid="0@lo"
12434         fi
12435         local proc_ofile="mdt.*.exports.'$nid'.open_files"
12436         local fd
12437         local cmd
12438
12439         rm -f $DIR/$tfile
12440         touch $DIR/$tfile
12441
12442         local fid=$($LFS path2fid $DIR/$tfile)
12443         # Open the file
12444         fd=$(free_fd)
12445         cmd="exec $fd<$DIR/$tfile"
12446         eval $cmd
12447         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
12448         echo "$fid_list" | grep "$fid"
12449         rc=$?
12450
12451         cmd="exec $fd>/dev/null"
12452         eval $cmd
12453         if [ $rc -ne 0 ]; then
12454                 error "FID $fid not found in open files list $fid_list"
12455         fi
12456 }
12457 run_test 154d "Verify open file fid"
12458
12459 test_154e()
12460 {
12461         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
12462                 skip "Need MDS version at least 2.6.50"
12463
12464         if ls -a $MOUNT | grep -q '^\.lustre$'; then
12465                 error ".lustre returned by readdir"
12466         fi
12467 }
12468 run_test 154e ".lustre is not returned by readdir"
12469
12470 test_154f() {
12471         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12472
12473         # create parent directory on a single MDT to avoid cross-MDT hardlinks
12474         test_mkdir -p -c1 $DIR/$tdir/d
12475         # test dirs inherit from its stripe
12476         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
12477         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
12478         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
12479         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
12480         touch $DIR/f
12481
12482         # get fid of parents
12483         local FID0=$($LFS path2fid $DIR/$tdir/d)
12484         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
12485         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
12486         local FID3=$($LFS path2fid $DIR)
12487
12488         # check that path2fid --parents returns expected <parent_fid>/name
12489         # 1) test for a directory (single parent)
12490         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
12491         [ "$parent" == "$FID0/foo1" ] ||
12492                 error "expected parent: $FID0/foo1, got: $parent"
12493
12494         # 2) test for a file with nlink > 1 (multiple parents)
12495         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
12496         echo "$parent" | grep -F "$FID1/$tfile" ||
12497                 error "$FID1/$tfile not returned in parent list"
12498         echo "$parent" | grep -F "$FID2/link" ||
12499                 error "$FID2/link not returned in parent list"
12500
12501         # 3) get parent by fid
12502         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
12503         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12504         echo "$parent" | grep -F "$FID1/$tfile" ||
12505                 error "$FID1/$tfile not returned in parent list (by fid)"
12506         echo "$parent" | grep -F "$FID2/link" ||
12507                 error "$FID2/link not returned in parent list (by fid)"
12508
12509         # 4) test for entry in root directory
12510         parent=$($LFS path2fid --parents $DIR/f)
12511         echo "$parent" | grep -F "$FID3/f" ||
12512                 error "$FID3/f not returned in parent list"
12513
12514         # 5) test it on root directory
12515         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
12516                 error "$MOUNT should not have parents"
12517
12518         # enable xattr caching and check that linkea is correctly updated
12519         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
12520         save_lustre_params client "llite.*.xattr_cache" > $save
12521         lctl set_param llite.*.xattr_cache 1
12522
12523         # 6.1) linkea update on rename
12524         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
12525
12526         # get parents by fid
12527         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12528         # foo1 should no longer be returned in parent list
12529         echo "$parent" | grep -F "$FID1" &&
12530                 error "$FID1 should no longer be in parent list"
12531         # the new path should appear
12532         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
12533                 error "$FID2/$tfile.moved is not in parent list"
12534
12535         # 6.2) linkea update on unlink
12536         rm -f $DIR/$tdir/d/foo2/link
12537         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
12538         # foo2/link should no longer be returned in parent list
12539         echo "$parent" | grep -F "$FID2/link" &&
12540                 error "$FID2/link should no longer be in parent list"
12541         true
12542
12543         rm -f $DIR/f
12544         restore_lustre_params < $save
12545         rm -f $save
12546 }
12547 run_test 154f "get parent fids by reading link ea"
12548
12549 test_154g()
12550 {
12551         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
12552         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
12553            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
12554                 skip "Need MDS version at least 2.6.92"
12555
12556         mkdir -p $DIR/$tdir
12557         llapi_fid_test -d $DIR/$tdir
12558 }
12559 run_test 154g "various llapi FID tests"
12560
12561 test_155_small_load() {
12562     local temp=$TMP/$tfile
12563     local file=$DIR/$tfile
12564
12565     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
12566         error "dd of=$temp bs=6096 count=1 failed"
12567     cp $temp $file
12568     cancel_lru_locks $OSC
12569     cmp $temp $file || error "$temp $file differ"
12570
12571     $TRUNCATE $temp 6000
12572     $TRUNCATE $file 6000
12573     cmp $temp $file || error "$temp $file differ (truncate1)"
12574
12575     echo "12345" >>$temp
12576     echo "12345" >>$file
12577     cmp $temp $file || error "$temp $file differ (append1)"
12578
12579     echo "12345" >>$temp
12580     echo "12345" >>$file
12581     cmp $temp $file || error "$temp $file differ (append2)"
12582
12583     rm -f $temp $file
12584     true
12585 }
12586
12587 test_155_big_load() {
12588         remote_ost_nodsh && skip "remote OST with nodsh"
12589
12590         local temp=$TMP/$tfile
12591         local file=$DIR/$tfile
12592
12593         free_min_max
12594         local cache_size=$(do_facet ost$((MAXI+1)) \
12595                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
12596         local large_file_size=$((cache_size * 2))
12597
12598         echo "OSS cache size: $cache_size KB"
12599         echo "Large file size: $large_file_size KB"
12600
12601         [ $MAXV -le $large_file_size ] &&
12602                 skip_env "max available OST size needs > $large_file_size KB"
12603
12604         $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
12605
12606         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
12607                 error "dd of=$temp bs=$large_file_size count=1k failed"
12608         cp $temp $file
12609         ls -lh $temp $file
12610         cancel_lru_locks osc
12611         cmp $temp $file || error "$temp $file differ"
12612
12613         rm -f $temp $file
12614         true
12615 }
12616
12617 save_writethrough() {
12618         local facets=$(get_facets OST)
12619
12620         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
12621 }
12622
12623 test_155a() {
12624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12625
12626         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12627
12628         save_writethrough $p
12629
12630         set_cache read on
12631         set_cache writethrough on
12632         test_155_small_load
12633         restore_lustre_params < $p
12634         rm -f $p
12635 }
12636 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
12637
12638 test_155b() {
12639         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12640
12641         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12642
12643         save_writethrough $p
12644
12645         set_cache read on
12646         set_cache writethrough off
12647         test_155_small_load
12648         restore_lustre_params < $p
12649         rm -f $p
12650 }
12651 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
12652
12653 test_155c() {
12654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12655
12656         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12657
12658         save_writethrough $p
12659
12660         set_cache read off
12661         set_cache writethrough on
12662         test_155_small_load
12663         restore_lustre_params < $p
12664         rm -f $p
12665 }
12666 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
12667
12668 test_155d() {
12669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12670
12671         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12672
12673         save_writethrough $p
12674
12675         set_cache read off
12676         set_cache writethrough off
12677         test_155_small_load
12678         restore_lustre_params < $p
12679         rm -f $p
12680 }
12681 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
12682
12683 test_155e() {
12684         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12685
12686         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12687
12688         save_writethrough $p
12689
12690         set_cache read on
12691         set_cache writethrough on
12692         test_155_big_load
12693         restore_lustre_params < $p
12694         rm -f $p
12695 }
12696 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
12697
12698 test_155f() {
12699         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12700
12701         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12702
12703         save_writethrough $p
12704
12705         set_cache read on
12706         set_cache writethrough off
12707         test_155_big_load
12708         restore_lustre_params < $p
12709         rm -f $p
12710 }
12711 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
12712
12713 test_155g() {
12714         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12715
12716         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12717
12718         save_writethrough $p
12719
12720         set_cache read off
12721         set_cache writethrough on
12722         test_155_big_load
12723         restore_lustre_params < $p
12724         rm -f $p
12725 }
12726 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
12727
12728 test_155h() {
12729         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12730
12731         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12732
12733         save_writethrough $p
12734
12735         set_cache read off
12736         set_cache writethrough off
12737         test_155_big_load
12738         restore_lustre_params < $p
12739         rm -f $p
12740 }
12741 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
12742
12743 test_156() {
12744         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12745         remote_ost_nodsh && skip "remote OST with nodsh"
12746         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
12747                 skip "stats not implemented on old servers"
12748         [ "$ost1_FSTYPE" = "zfs" ] &&
12749                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
12750
12751         local CPAGES=3
12752         local BEFORE
12753         local AFTER
12754         local file="$DIR/$tfile"
12755         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12756
12757         save_writethrough $p
12758         roc_hit_init
12759
12760         log "Turn on read and write cache"
12761         set_cache read on
12762         set_cache writethrough on
12763
12764         log "Write data and read it back."
12765         log "Read should be satisfied from the cache."
12766         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12767         BEFORE=$(roc_hit)
12768         cancel_lru_locks osc
12769         cat $file >/dev/null
12770         AFTER=$(roc_hit)
12771         if ! let "AFTER - BEFORE == CPAGES"; then
12772                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12773         else
12774                 log "cache hits:: before: $BEFORE, after: $AFTER"
12775         fi
12776
12777         log "Read again; it should be satisfied from the cache."
12778         BEFORE=$AFTER
12779         cancel_lru_locks osc
12780         cat $file >/dev/null
12781         AFTER=$(roc_hit)
12782         if ! let "AFTER - BEFORE == CPAGES"; then
12783                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12784         else
12785                 log "cache hits:: before: $BEFORE, after: $AFTER"
12786         fi
12787
12788         log "Turn off the read cache and turn on the write cache"
12789         set_cache read off
12790         set_cache writethrough on
12791
12792         log "Read again; it should be satisfied from the cache."
12793         BEFORE=$(roc_hit)
12794         cancel_lru_locks osc
12795         cat $file >/dev/null
12796         AFTER=$(roc_hit)
12797         if ! let "AFTER - BEFORE == CPAGES"; then
12798                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12799         else
12800                 log "cache hits:: before: $BEFORE, after: $AFTER"
12801         fi
12802
12803         log "Read again; it should not be satisfied from the cache."
12804         BEFORE=$AFTER
12805         cancel_lru_locks osc
12806         cat $file >/dev/null
12807         AFTER=$(roc_hit)
12808         if ! let "AFTER - BEFORE == 0"; then
12809                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12810         else
12811                 log "cache hits:: before: $BEFORE, after: $AFTER"
12812         fi
12813
12814         log "Write data and read it back."
12815         log "Read should be satisfied from the cache."
12816         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12817         BEFORE=$(roc_hit)
12818         cancel_lru_locks osc
12819         cat $file >/dev/null
12820         AFTER=$(roc_hit)
12821         if ! let "AFTER - BEFORE == CPAGES"; then
12822                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12823         else
12824                 log "cache hits:: before: $BEFORE, after: $AFTER"
12825         fi
12826
12827         log "Read again; it should not be satisfied from the cache."
12828         BEFORE=$AFTER
12829         cancel_lru_locks osc
12830         cat $file >/dev/null
12831         AFTER=$(roc_hit)
12832         if ! let "AFTER - BEFORE == 0"; then
12833                 error "IN CACHE: before: $BEFORE, after: $AFTER"
12834         else
12835                 log "cache hits:: before: $BEFORE, after: $AFTER"
12836         fi
12837
12838         log "Turn off read and write cache"
12839         set_cache read off
12840         set_cache writethrough off
12841
12842         log "Write data and read it back"
12843         log "It should not be satisfied from the cache."
12844         rm -f $file
12845         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12846         cancel_lru_locks osc
12847         BEFORE=$(roc_hit)
12848         cat $file >/dev/null
12849         AFTER=$(roc_hit)
12850         if ! let "AFTER - BEFORE == 0"; then
12851                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12852         else
12853                 log "cache hits:: before: $BEFORE, after: $AFTER"
12854         fi
12855
12856         log "Turn on the read cache and turn off the write cache"
12857         set_cache read on
12858         set_cache writethrough off
12859
12860         log "Write data and read it back"
12861         log "It should not be satisfied from the cache."
12862         rm -f $file
12863         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
12864         BEFORE=$(roc_hit)
12865         cancel_lru_locks osc
12866         cat $file >/dev/null
12867         AFTER=$(roc_hit)
12868         if ! let "AFTER - BEFORE == 0"; then
12869                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
12870         else
12871                 log "cache hits:: before: $BEFORE, after: $AFTER"
12872         fi
12873
12874         log "Read again; it should be satisfied from the cache."
12875         BEFORE=$(roc_hit)
12876         cancel_lru_locks osc
12877         cat $file >/dev/null
12878         AFTER=$(roc_hit)
12879         if ! let "AFTER - BEFORE == CPAGES"; then
12880                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
12881         else
12882                 log "cache hits:: before: $BEFORE, after: $AFTER"
12883         fi
12884
12885         restore_lustre_params < $p
12886         rm -f $p $file
12887 }
12888 run_test 156 "Verification of tunables"
12889
12890 test_160a() {
12891         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12892         remote_mds_nodsh && skip "remote MDS with nodsh"
12893         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
12894                 skip "Need MDS version at least 2.2.0"
12895
12896         changelog_register || error "changelog_register failed"
12897         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
12898         changelog_users $SINGLEMDS | grep -q $cl_user ||
12899                 error "User $cl_user not found in changelog_users"
12900
12901         # change something
12902         test_mkdir -p $DIR/$tdir/pics/2008/zachy
12903         changelog_clear 0 || error "changelog_clear failed"
12904         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
12905         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
12906         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
12907         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
12908         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
12909         rm $DIR/$tdir/pics/desktop.jpg
12910
12911         changelog_dump | tail -10
12912
12913         echo "verifying changelog mask"
12914         changelog_chmask "-MKDIR"
12915         changelog_chmask "-CLOSE"
12916
12917         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
12918         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
12919
12920         changelog_chmask "+MKDIR"
12921         changelog_chmask "+CLOSE"
12922
12923         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
12924         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
12925
12926         changelog_dump | tail -10
12927         MKDIRS=$(changelog_dump | grep -c "MKDIR")
12928         CLOSES=$(changelog_dump | grep -c "CLOSE")
12929         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
12930         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
12931
12932         # verify contents
12933         echo "verifying target fid"
12934         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
12935         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
12936         [ "$fidc" == "$fidf" ] ||
12937                 error "changelog '$tfile' fid $fidc != file fid $fidf"
12938         echo "verifying parent fid"
12939         # The FID returned from the Changelog may be the directory shard on
12940         # a different MDT, and not the FID returned by path2fid on the parent.
12941         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
12942         # since this is what will matter when recreating this file in the tree.
12943         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
12944         local pathp=$($LFS fid2path $MOUNT "$fidp")
12945         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
12946                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
12947
12948         echo "getting records for $cl_user"
12949         changelog_users $SINGLEMDS
12950         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
12951         local nclr=3
12952         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
12953                 error "changelog_clear failed"
12954         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
12955         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
12956         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
12957                 error "user index expect $user_rec1 + $nclr != $user_rec2"
12958
12959         local min0_rec=$(changelog_users $SINGLEMDS |
12960                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
12961         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
12962                           awk '{ print $1; exit; }')
12963
12964         changelog_dump | tail -n 5
12965         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
12966         [ $first_rec == $((min0_rec + 1)) ] ||
12967                 error "first index should be $min0_rec + 1 not $first_rec"
12968
12969         # LU-3446 changelog index reset on MDT restart
12970         local cur_rec1=$(changelog_users $SINGLEMDS |
12971                          awk '/^current.index:/ { print $NF }')
12972         changelog_clear 0 ||
12973                 error "clear all changelog records for $cl_user failed"
12974         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
12975         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
12976                 error "Fail to start $SINGLEMDS"
12977         local cur_rec2=$(changelog_users $SINGLEMDS |
12978                          awk '/^current.index:/ { print $NF }')
12979         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
12980         [ $cur_rec1 == $cur_rec2 ] ||
12981                 error "current index should be $cur_rec1 not $cur_rec2"
12982
12983         echo "verifying users from this test are deregistered"
12984         changelog_deregister || error "changelog_deregister failed"
12985         changelog_users $SINGLEMDS | grep -q $cl_user &&
12986                 error "User '$cl_user' still in changelog_users"
12987
12988         # lctl get_param -n mdd.*.changelog_users
12989         # current index: 144
12990         # ID    index (idle seconds)
12991         # cl3   144 (2)
12992         if ! changelog_users $SINGLEMDS | grep "^cl"; then
12993                 # this is the normal case where all users were deregistered
12994                 # make sure no new records are added when no users are present
12995                 local last_rec1=$(changelog_users $SINGLEMDS |
12996                                   awk '/^current.index:/ { print $NF }')
12997                 touch $DIR/$tdir/chloe
12998                 local last_rec2=$(changelog_users $SINGLEMDS |
12999                                   awk '/^current.index:/ { print $NF }')
13000                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
13001                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
13002         else
13003                 # any changelog users must be leftovers from a previous test
13004                 changelog_users $SINGLEMDS
13005                 echo "other changelog users; can't verify off"
13006         fi
13007 }
13008 run_test 160a "changelog sanity"
13009
13010 test_160b() { # LU-3587
13011         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13012         remote_mds_nodsh && skip "remote MDS with nodsh"
13013         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
13014                 skip "Need MDS version at least 2.2.0"
13015
13016         changelog_register || error "changelog_register failed"
13017         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13018         changelog_users $SINGLEMDS | grep -q $cl_user ||
13019                 error "User '$cl_user' not found in changelog_users"
13020
13021         local longname1=$(str_repeat a 255)
13022         local longname2=$(str_repeat b 255)
13023
13024         cd $DIR
13025         echo "creating very long named file"
13026         touch $longname1 || error "create of '$longname1' failed"
13027         echo "renaming very long named file"
13028         mv $longname1 $longname2
13029
13030         changelog_dump | grep RENME | tail -n 5
13031         rm -f $longname2
13032 }
13033 run_test 160b "Verify that very long rename doesn't crash in changelog"
13034
13035 test_160c() {
13036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13037         remote_mds_nodsh && skip "remote MDS with nodsh"
13038
13039         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
13040                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
13041                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
13042                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
13043
13044         local rc=0
13045
13046         # Registration step
13047         changelog_register || error "changelog_register failed"
13048
13049         rm -rf $DIR/$tdir
13050         mkdir -p $DIR/$tdir
13051         $MCREATE $DIR/$tdir/foo_160c
13052         changelog_chmask "-TRUNC"
13053         $TRUNCATE $DIR/$tdir/foo_160c 200
13054         changelog_chmask "+TRUNC"
13055         $TRUNCATE $DIR/$tdir/foo_160c 199
13056         changelog_dump | tail -n 5
13057         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
13058         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
13059 }
13060 run_test 160c "verify that changelog log catch the truncate event"
13061
13062 test_160d() {
13063         remote_mds_nodsh && skip "remote MDS with nodsh"
13064         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13065         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13066         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
13067                 skip "Need MDS version at least 2.7.60"
13068
13069         # Registration step
13070         changelog_register || error "changelog_register failed"
13071
13072         mkdir -p $DIR/$tdir/migrate_dir
13073         changelog_clear 0 || error "changelog_clear failed"
13074
13075         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
13076         changelog_dump | tail -n 5
13077         local migrates=$(changelog_dump | grep -c "MIGRT")
13078         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
13079 }
13080 run_test 160d "verify that changelog log catch the migrate event"
13081
13082 test_160e() {
13083         remote_mds_nodsh && skip "remote MDS with nodsh"
13084
13085         # Create a user
13086         changelog_register || error "changelog_register failed"
13087
13088         # Delete a future user (expect fail)
13089         local MDT0=$(facet_svc $SINGLEMDS)
13090         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
13091         local rc=$?
13092
13093         if [ $rc -eq 0 ]; then
13094                 error "Deleted non-existant user cl77"
13095         elif [ $rc -ne 2 ]; then
13096                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
13097         fi
13098
13099         # Clear to a bad index (1 billion should be safe)
13100         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
13101         rc=$?
13102
13103         if [ $rc -eq 0 ]; then
13104                 error "Successfully cleared to invalid CL index"
13105         elif [ $rc -ne 22 ]; then
13106                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
13107         fi
13108 }
13109 run_test 160e "changelog negative testing (should return errors)"
13110
13111 test_160f() {
13112         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13113         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13114                 skip "Need MDS version at least 2.10.56"
13115
13116         local mdts=$(comma_list $(mdts_nodes))
13117
13118         # Create a user
13119         changelog_register || error "first changelog_register failed"
13120         changelog_register || error "second changelog_register failed"
13121         local cl_users
13122         declare -A cl_user1
13123         declare -A cl_user2
13124         local user_rec1
13125         local user_rec2
13126         local i
13127
13128         # generate some changelog records to accumulate on each MDT
13129         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13130         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13131                 error "create $DIR/$tdir/$tfile failed"
13132
13133         # check changelogs have been generated
13134         local nbcl=$(changelog_dump | wc -l)
13135         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13136
13137         for param in "changelog_max_idle_time=10" \
13138                      "changelog_gc=1" \
13139                      "changelog_min_gc_interval=2" \
13140                      "changelog_min_free_cat_entries=3"; do
13141                 local MDT0=$(facet_svc $SINGLEMDS)
13142                 local var="${param%=*}"
13143                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13144
13145                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13146                 do_nodes $mdts $LCTL set_param mdd.*.$param
13147         done
13148
13149         # force cl_user2 to be idle (1st part)
13150         sleep 9
13151
13152         # simulate changelog catalog almost full
13153         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13154         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13155
13156         for i in $(seq $MDSCOUNT); do
13157                 cl_users=(${CL_USERS[mds$i]})
13158                 cl_user1[mds$i]="${cl_users[0]}"
13159                 cl_user2[mds$i]="${cl_users[1]}"
13160
13161                 [ -n "${cl_user1[mds$i]}" ] ||
13162                         error "mds$i: no user registered"
13163                 [ -n "${cl_user2[mds$i]}" ] ||
13164                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13165
13166                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13167                 [ -n "$user_rec1" ] ||
13168                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13169                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13170                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13171                 [ -n "$user_rec2" ] ||
13172                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13173                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13174                      "$user_rec1 + 2 == $user_rec2"
13175                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13176                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13177                               "$user_rec1 + 2, but is $user_rec2"
13178                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13179                 [ -n "$user_rec2" ] ||
13180                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13181                 [ $user_rec1 == $user_rec2 ] ||
13182                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13183                               "$user_rec1, but is $user_rec2"
13184         done
13185
13186         # force cl_user2 to be idle (2nd part) and to reach
13187         # changelog_max_idle_time
13188         sleep 2
13189
13190         # generate one more changelog to trigger fail_loc
13191         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13192                 error "create $DIR/$tdir/${tfile}bis failed"
13193
13194         # ensure gc thread is done
13195         for i in $(mdts_nodes); do
13196                 wait_update $i \
13197                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13198                         error "$i: GC-thread not done"
13199         done
13200
13201         local first_rec
13202         for i in $(seq $MDSCOUNT); do
13203                 # check cl_user1 still registered
13204                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13205                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13206                 # check cl_user2 unregistered
13207                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13208                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13209
13210                 # check changelogs are present and starting at $user_rec1 + 1
13211                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13212                 [ -n "$user_rec1" ] ||
13213                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13214                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13215                             awk '{ print $1; exit; }')
13216
13217                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13218                 [ $((user_rec1 + 1)) == $first_rec ] ||
13219                         error "mds$i: first index should be $user_rec1 + 1, " \
13220                               "but is $first_rec"
13221         done
13222 }
13223 run_test 160f "changelog garbage collect (timestamped users)"
13224
13225 test_160g() {
13226         remote_mds_nodsh && skip "remote MDS with nodsh"
13227         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13228                 skip "Need MDS version at least 2.10.56"
13229
13230         local mdts=$(comma_list $(mdts_nodes))
13231
13232         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
13233         do_nodes $mdts $LCTL set_param fail_loc=0x1314
13234
13235         # Create a user
13236         changelog_register || error "first changelog_register failed"
13237         changelog_register || error "second changelog_register failed"
13238         local cl_users
13239         declare -A cl_user1
13240         declare -A cl_user2
13241         local user_rec1
13242         local user_rec2
13243         local i
13244
13245         # generate some changelog records to accumulate on each MDT
13246         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13247         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13248                 error "create $DIR/$tdir/$tfile failed"
13249
13250         # check changelogs have been generated
13251         local nbcl=$(changelog_dump | wc -l)
13252         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13253
13254         # reduce the max_idle_indexes value to make sure we exceed it
13255         max_ndx=$((nbcl / 2 - 1))
13256
13257         for param in "changelog_max_idle_indexes=$max_ndx" \
13258                      "changelog_gc=1" \
13259                      "changelog_min_gc_interval=2" \
13260                      "changelog_min_free_cat_entries=3"; do
13261                 local MDT0=$(facet_svc $SINGLEMDS)
13262                 local var="${param%=*}"
13263                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13264
13265                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13266                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
13267                         error "unable to set mdd.*.$param"
13268         done
13269
13270         # simulate changelog catalog almost full
13271         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
13272         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
13273
13274         for i in $(seq $MDSCOUNT); do
13275                 cl_users=(${CL_USERS[mds$i]})
13276                 cl_user1[mds$i]="${cl_users[0]}"
13277                 cl_user2[mds$i]="${cl_users[1]}"
13278
13279                 [ -n "${cl_user1[mds$i]}" ] ||
13280                         error "mds$i: no user registered"
13281                 [ -n "${cl_user2[mds$i]}" ] ||
13282                         error "mds$i: only ${cl_user1[mds$i]} is registered"
13283
13284                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13285                 [ -n "$user_rec1" ] ||
13286                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13287                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13288                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13289                 [ -n "$user_rec2" ] ||
13290                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13291                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13292                      "$user_rec1 + 2 == $user_rec2"
13293                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13294                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13295                               "$user_rec1 + 2, but is $user_rec2"
13296                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13297                 [ -n "$user_rec2" ] ||
13298                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13299                 [ $user_rec1 == $user_rec2 ] ||
13300                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13301                               "$user_rec1, but is $user_rec2"
13302         done
13303
13304         # ensure we are past the previous changelog_min_gc_interval set above
13305         sleep 2
13306
13307         # generate one more changelog to trigger fail_loc
13308         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13309                 error "create $DIR/$tdir/${tfile}bis failed"
13310
13311         # ensure gc thread is done
13312         for i in $(mdts_nodes); do
13313                 wait_update $i \
13314                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
13315                         error "$i: GC-thread not done"
13316         done
13317
13318         local first_rec
13319         for i in $(seq $MDSCOUNT); do
13320                 # check cl_user1 still registered
13321                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13322                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13323                 # check cl_user2 unregistered
13324                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13325                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13326
13327                 # check changelogs are present and starting at $user_rec1 + 1
13328                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13329                 [ -n "$user_rec1" ] ||
13330                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13331                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13332                             awk '{ print $1; exit; }')
13333
13334                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13335                 [ $((user_rec1 + 1)) == $first_rec ] ||
13336                         error "mds$i: first index should be $user_rec1 + 1, " \
13337                               "but is $first_rec"
13338         done
13339 }
13340 run_test 160g "changelog garbage collect (old users)"
13341
13342 test_160h() {
13343         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13344         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
13345                 skip "Need MDS version at least 2.10.56"
13346
13347         local mdts=$(comma_list $(mdts_nodes))
13348
13349         # Create a user
13350         changelog_register || error "first changelog_register failed"
13351         changelog_register || error "second changelog_register failed"
13352         local cl_users
13353         declare -A cl_user1
13354         declare -A cl_user2
13355         local user_rec1
13356         local user_rec2
13357         local i
13358
13359         # generate some changelog records to accumulate on each MDT
13360         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "test_mkdir $tdir failed"
13361         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13362                 error "create $DIR/$tdir/$tfile failed"
13363
13364         # check changelogs have been generated
13365         local nbcl=$(changelog_dump | wc -l)
13366         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13367
13368         for param in "changelog_max_idle_time=10" \
13369                      "changelog_gc=1" \
13370                      "changelog_min_gc_interval=2"; do
13371                 local MDT0=$(facet_svc $SINGLEMDS)
13372                 local var="${param%=*}"
13373                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
13374
13375                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
13376                 do_nodes $mdts $LCTL set_param mdd.*.$param
13377         done
13378
13379         # force cl_user2 to be idle (1st part)
13380         sleep 9
13381
13382         for i in $(seq $MDSCOUNT); do
13383                 cl_users=(${CL_USERS[mds$i]})
13384                 cl_user1[mds$i]="${cl_users[0]}"
13385                 cl_user2[mds$i]="${cl_users[1]}"
13386
13387                 [ -n "${cl_user1[mds$i]}" ] ||
13388                         error "mds$i: no user registered"
13389                 [ -n "${cl_user2[mds$i]}" ] ||
13390                         error "mds$i: only ${cl_user2[mds$i]} is registered"
13391
13392                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13393                 [ -n "$user_rec1" ] ||
13394                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13395                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
13396                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13397                 [ -n "$user_rec2" ] ||
13398                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13399                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
13400                      "$user_rec1 + 2 == $user_rec2"
13401                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
13402                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
13403                               "$user_rec1 + 2, but is $user_rec2"
13404                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
13405                 [ -n "$user_rec2" ] ||
13406                         error "mds$i: User ${cl_user2[mds$i]} not registered"
13407                 [ $user_rec1 == $user_rec2 ] ||
13408                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
13409                               "$user_rec1, but is $user_rec2"
13410         done
13411
13412         # force cl_user2 to be idle (2nd part) and to reach
13413         # changelog_max_idle_time
13414         sleep 2
13415
13416         # force each GC-thread start and block then
13417         # one per MDT/MDD, set fail_val accordingly
13418         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
13419         do_nodes $mdts $LCTL set_param fail_loc=0x1316
13420
13421         # generate more changelogs to trigger fail_loc
13422         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13423                 error "create $DIR/$tdir/${tfile}bis failed"
13424
13425         # stop MDT to stop GC-thread, should be done in back-ground as it will
13426         # block waiting for the thread to be released and exit
13427         declare -A stop_pids
13428         for i in $(seq $MDSCOUNT); do
13429                 stop mds$i &
13430                 stop_pids[mds$i]=$!
13431         done
13432
13433         for i in $(mdts_nodes); do
13434                 local facet
13435                 local nb=0
13436                 local facets=$(facets_up_on_host $i)
13437
13438                 for facet in ${facets//,/ }; do
13439                         if [[ $facet == mds* ]]; then
13440                                 nb=$((nb + 1))
13441                         fi
13442                 done
13443                 # ensure each MDS's gc threads are still present and all in "R"
13444                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
13445                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
13446                         error "$i: expected $nb GC-thread"
13447                 wait_update $i \
13448                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
13449                         "R" 20 ||
13450                         error "$i: GC-thread not found in R-state"
13451                 # check umounts of each MDT on MDS have reached kthread_stop()
13452                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
13453                         error "$i: expected $nb umount"
13454                 wait_update $i \
13455                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
13456                         error "$i: umount not found in D-state"
13457         done
13458
13459         # release all GC-threads
13460         do_nodes $mdts $LCTL set_param fail_loc=0
13461
13462         # wait for MDT stop to complete
13463         for i in $(seq $MDSCOUNT); do
13464                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
13465         done
13466
13467         # XXX
13468         # may try to check if any orphan changelog records are present
13469         # via ldiskfs/zfs and llog_reader...
13470
13471         # re-start/mount MDTs
13472         for i in $(seq $MDSCOUNT); do
13473                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
13474                         error "Fail to start mds$i"
13475         done
13476
13477         local first_rec
13478         for i in $(seq $MDSCOUNT); do
13479                 # check cl_user1 still registered
13480                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
13481                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13482                 # check cl_user2 unregistered
13483                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
13484                         error "mds$i: User ${cl_user2[mds$i]} still registered"
13485
13486                 # check changelogs are present and starting at $user_rec1 + 1
13487                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
13488                 [ -n "$user_rec1" ] ||
13489                         error "mds$i: User ${cl_user1[mds$i]} not registered"
13490                 first_rec=$($LFS changelog $(facet_svc mds$i) |
13491                             awk '{ print $1; exit; }')
13492
13493                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
13494                 [ $((user_rec1 + 1)) == $first_rec ] ||
13495                         error "mds$i: first index should be $user_rec1 + 1, " \
13496                               "but is $first_rec"
13497         done
13498 }
13499 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
13500               "during mount"
13501
13502 test_160i() {
13503
13504         local mdts=$(comma_list $(mdts_nodes))
13505
13506         changelog_register || error "first changelog_register failed"
13507
13508         # generate some changelog records to accumulate on each MDT
13509         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13510         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
13511                 error "create $DIR/$tdir/$tfile failed"
13512
13513         # check changelogs have been generated
13514         local nbcl=$(changelog_dump | wc -l)
13515         [[ $nbcl -eq 0 ]] && error "no changelogs found"
13516
13517         # simulate race between register and unregister
13518         # XXX as fail_loc is set per-MDS, with DNE configs the race
13519         # simulation will only occur for one MDT per MDS and for the
13520         # others the normal race scenario will take place
13521         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
13522         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
13523         do_nodes $mdts $LCTL set_param fail_val=1
13524
13525         # unregister 1st user
13526         changelog_deregister &
13527         local pid1=$!
13528         # wait some time for deregister work to reach race rdv
13529         sleep 2
13530         # register 2nd user
13531         changelog_register || error "2nd user register failed"
13532
13533         wait $pid1 || error "1st user deregister failed"
13534
13535         local i
13536         local last_rec
13537         declare -A LAST_REC
13538         for i in $(seq $MDSCOUNT); do
13539                 if changelog_users mds$i | grep "^cl"; then
13540                         # make sure new records are added with one user present
13541                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
13542                                           awk '/^current.index:/ { print $NF }')
13543                 else
13544                         error "mds$i has no user registered"
13545                 fi
13546         done
13547
13548         # generate more changelog records to accumulate on each MDT
13549         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13550                 error "create $DIR/$tdir/${tfile}bis failed"
13551
13552         for i in $(seq $MDSCOUNT); do
13553                 last_rec=$(changelog_users $SINGLEMDS |
13554                            awk '/^current.index:/ { print $NF }')
13555                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
13556                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
13557                         error "changelogs are off on mds$i"
13558         done
13559 }
13560 run_test 160i "changelog user register/unregister race"
13561
13562 test_160k() {
13563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13564         remote_mds_nodsh && skip "remote MDS with nodsh"
13565
13566         mkdir -p $DIR/$tdir/1/1
13567
13568         changelog_register || error "changelog_register failed"
13569         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13570
13571         changelog_users $SINGLEMDS | grep -q $cl_user ||
13572                 error "User '$cl_user' not found in changelog_users"
13573 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
13574         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
13575         rmdir $DIR/$tdir/1/1 & sleep 1
13576         mkdir $DIR/$tdir/2
13577         touch $DIR/$tdir/2/2
13578         rm -rf $DIR/$tdir/2
13579
13580         wait
13581         sleep 4
13582
13583         changelog_dump | grep rmdir || error "rmdir not recorded"
13584
13585         rm -rf $DIR/$tdir
13586         changelog_deregister
13587 }
13588 run_test 160k "Verify that changelog records are not lost"
13589
13590 test_160j() {
13591         remote_mds_nodsh && skip "remote MDS with nodsh"
13592         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
13593                 skip "Need MDS version at least 2.12.56"
13594
13595         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
13596
13597         changelog_register || error "first changelog_register failed"
13598
13599         # generate some changelog
13600         test_mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
13601         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
13602                 error "create $DIR/$tdir/${tfile}bis failed"
13603
13604         # open the changelog device
13605         exec 3>/dev/changelog-$FSNAME-MDT0000
13606         exec 4</dev/changelog-$FSNAME-MDT0000
13607
13608         # umount the first lustre mount
13609         umount $MOUNT
13610
13611         # read changelog
13612         cat <&4 >/dev/null || error "read changelog failed"
13613
13614         # clear changelog
13615         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
13616         changelog_users $SINGLEMDS | grep -q $cl_user ||
13617                 error "User $cl_user not found in changelog_users"
13618
13619         printf 'clear:'$cl_user':0' >&3
13620
13621         # close
13622         exec 3>&-
13623         exec 4<&-
13624
13625         # cleanup
13626         changelog_deregister || error "changelog_deregister failed"
13627
13628         umount $MOUNT2
13629         mount_client $MOUNT || error "mount_client on $MOUNT failed"
13630 }
13631 run_test 160j "client can be umounted  while its chanangelog is being used"
13632
13633 test_161a() {
13634         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13635
13636         test_mkdir -c1 $DIR/$tdir
13637         cp /etc/hosts $DIR/$tdir/$tfile
13638         test_mkdir -c1 $DIR/$tdir/foo1
13639         test_mkdir -c1 $DIR/$tdir/foo2
13640         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
13641         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
13642         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
13643         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
13644         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
13645         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13646                 $LFS fid2path $DIR $FID
13647                 error "bad link ea"
13648         fi
13649         # middle
13650         rm $DIR/$tdir/foo2/zachary
13651         # last
13652         rm $DIR/$tdir/foo2/thor
13653         # first
13654         rm $DIR/$tdir/$tfile
13655         # rename
13656         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
13657         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
13658                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
13659         rm $DIR/$tdir/foo2/maggie
13660
13661         # overflow the EA
13662         local longname=$tfile.avg_len_is_thirty_two_
13663         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
13664                 error_noexit 'failed to unlink many hardlinks'" EXIT
13665         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
13666                 error "failed to hardlink many files"
13667         links=$($LFS fid2path $DIR $FID | wc -l)
13668         echo -n "${links}/1000 links in link EA"
13669         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
13670 }
13671 run_test 161a "link ea sanity"
13672
13673 test_161b() {
13674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13675         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
13676
13677         local MDTIDX=1
13678         local remote_dir=$DIR/$tdir/remote_dir
13679
13680         mkdir -p $DIR/$tdir
13681         $LFS mkdir -i $MDTIDX $remote_dir ||
13682                 error "create remote directory failed"
13683
13684         cp /etc/hosts $remote_dir/$tfile
13685         mkdir -p $remote_dir/foo1
13686         mkdir -p $remote_dir/foo2
13687         ln $remote_dir/$tfile $remote_dir/foo1/sofia
13688         ln $remote_dir/$tfile $remote_dir/foo2/zachary
13689         ln $remote_dir/$tfile $remote_dir/foo1/luna
13690         ln $remote_dir/$tfile $remote_dir/foo2/thor
13691
13692         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
13693                      tr -d ']')
13694         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
13695                 $LFS fid2path $DIR $FID
13696                 error "bad link ea"
13697         fi
13698         # middle
13699         rm $remote_dir/foo2/zachary
13700         # last
13701         rm $remote_dir/foo2/thor
13702         # first
13703         rm $remote_dir/$tfile
13704         # rename
13705         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
13706         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
13707         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
13708                 $LFS fid2path $DIR $FID
13709                 error "bad link rename"
13710         fi
13711         rm $remote_dir/foo2/maggie
13712
13713         # overflow the EA
13714         local longname=filename_avg_len_is_thirty_two_
13715         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
13716                 error "failed to hardlink many files"
13717         links=$($LFS fid2path $DIR $FID | wc -l)
13718         echo -n "${links}/1000 links in link EA"
13719         [[ ${links} -gt 60 ]] ||
13720                 error "expected at least 60 links in link EA"
13721         unlinkmany $remote_dir/foo2/$longname 1000 ||
13722         error "failed to unlink many hardlinks"
13723 }
13724 run_test 161b "link ea sanity under remote directory"
13725
13726 test_161c() {
13727         remote_mds_nodsh && skip "remote MDS with nodsh"
13728         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13729         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
13730                 skip "Need MDS version at least 2.1.5"
13731
13732         # define CLF_RENAME_LAST 0x0001
13733         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
13734         changelog_register || error "changelog_register failed"
13735
13736         rm -rf $DIR/$tdir
13737         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
13738         touch $DIR/$tdir/foo_161c
13739         touch $DIR/$tdir/bar_161c
13740         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13741         changelog_dump | grep RENME | tail -n 5
13742         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13743         changelog_clear 0 || error "changelog_clear failed"
13744         if [ x$flags != "x0x1" ]; then
13745                 error "flag $flags is not 0x1"
13746         fi
13747
13748         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
13749         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
13750         touch $DIR/$tdir/foo_161c
13751         touch $DIR/$tdir/bar_161c
13752         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13753         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
13754         changelog_dump | grep RENME | tail -n 5
13755         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
13756         changelog_clear 0 || error "changelog_clear failed"
13757         if [ x$flags != "x0x0" ]; then
13758                 error "flag $flags is not 0x0"
13759         fi
13760         echo "rename overwrite a target having nlink > 1," \
13761                 "changelog record has flags of $flags"
13762
13763         # rename doesn't overwrite a target (changelog flag 0x0)
13764         touch $DIR/$tdir/foo_161c
13765         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
13766         changelog_dump | grep RENME | tail -n 5
13767         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
13768         changelog_clear 0 || error "changelog_clear failed"
13769         if [ x$flags != "x0x0" ]; then
13770                 error "flag $flags is not 0x0"
13771         fi
13772         echo "rename doesn't overwrite a target," \
13773                 "changelog record has flags of $flags"
13774
13775         # define CLF_UNLINK_LAST 0x0001
13776         # unlink a file having nlink = 1 (changelog flag 0x1)
13777         rm -f $DIR/$tdir/foo2_161c
13778         changelog_dump | grep UNLNK | tail -n 5
13779         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13780         changelog_clear 0 || error "changelog_clear failed"
13781         if [ x$flags != "x0x1" ]; then
13782                 error "flag $flags is not 0x1"
13783         fi
13784         echo "unlink a file having nlink = 1," \
13785                 "changelog record has flags of $flags"
13786
13787         # unlink a file having nlink > 1 (changelog flag 0x0)
13788         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
13789         rm -f $DIR/$tdir/foobar_161c
13790         changelog_dump | grep UNLNK | tail -n 5
13791         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
13792         changelog_clear 0 || error "changelog_clear failed"
13793         if [ x$flags != "x0x0" ]; then
13794                 error "flag $flags is not 0x0"
13795         fi
13796         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
13797 }
13798 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
13799
13800 test_161d() {
13801         remote_mds_nodsh && skip "remote MDS with nodsh"
13802
13803         local pid
13804         local fid
13805
13806         changelog_register || error "changelog_register failed"
13807
13808         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
13809         # interfer with $MOUNT/.lustre/fid/ access
13810         mkdir $DIR/$tdir
13811         [[ $? -eq 0 ]] || error "mkdir failed"
13812
13813         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
13814         $LCTL set_param fail_loc=0x8000140c
13815         # 5s pause
13816         $LCTL set_param fail_val=5
13817
13818         # create file
13819         echo foofoo > $DIR/$tdir/$tfile &
13820         pid=$!
13821
13822         # wait for create to be delayed
13823         sleep 2
13824
13825         ps -p $pid
13826         [[ $? -eq 0 ]] || error "create should be blocked"
13827
13828         local tempfile=$(mktemp)
13829         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
13830         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
13831         # some delay may occur during ChangeLog publishing and file read just
13832         # above, that could allow file write to happen finally
13833         [[ -s $tempfile ]] && echo "file should be empty"
13834
13835         $LCTL set_param fail_loc=0
13836
13837         wait $pid
13838         [[ $? -eq 0 ]] || error "create failed"
13839 }
13840 run_test 161d "create with concurrent .lustre/fid access"
13841
13842 check_path() {
13843         local expected="$1"
13844         shift
13845         local fid="$2"
13846
13847         local path
13848         path=$($LFS fid2path "$@")
13849         local rc=$?
13850
13851         if [ $rc -ne 0 ]; then
13852                 error "path looked up of '$expected' failed: rc=$rc"
13853         elif [ "$path" != "$expected" ]; then
13854                 error "path looked up '$path' instead of '$expected'"
13855         else
13856                 echo "FID '$fid' resolves to path '$path' as expected"
13857         fi
13858 }
13859
13860 test_162a() { # was test_162
13861         test_mkdir -p -c1 $DIR/$tdir/d2
13862         touch $DIR/$tdir/d2/$tfile
13863         touch $DIR/$tdir/d2/x1
13864         touch $DIR/$tdir/d2/x2
13865         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
13866         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
13867         # regular file
13868         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
13869         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
13870
13871         # softlink
13872         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
13873         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
13874         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
13875
13876         # softlink to wrong file
13877         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
13878         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
13879         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
13880
13881         # hardlink
13882         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
13883         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
13884         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
13885         # fid2path dir/fsname should both work
13886         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
13887         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
13888
13889         # hardlink count: check that there are 2 links
13890         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
13891         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
13892
13893         # hardlink indexing: remove the first link
13894         rm $DIR/$tdir/d2/p/q/r/hlink
13895         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
13896 }
13897 run_test 162a "path lookup sanity"
13898
13899 test_162b() {
13900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13901         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13902
13903         mkdir $DIR/$tdir
13904         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
13905                                 error "create striped dir failed"
13906
13907         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
13908                                         tail -n 1 | awk '{print $2}')
13909         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
13910
13911         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
13912         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
13913
13914         # regular file
13915         for ((i=0;i<5;i++)); do
13916                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
13917                         error "get fid for f$i failed"
13918                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
13919
13920                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
13921                         error "get fid for d$i failed"
13922                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
13923         done
13924
13925         return 0
13926 }
13927 run_test 162b "striped directory path lookup sanity"
13928
13929 # LU-4239: Verify fid2path works with paths 100 or more directories deep
13930 test_162c() {
13931         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
13932                 skip "Need MDS version at least 2.7.51"
13933
13934         local lpath=$tdir.local
13935         local rpath=$tdir.remote
13936
13937         test_mkdir $DIR/$lpath
13938         test_mkdir $DIR/$rpath
13939
13940         for ((i = 0; i <= 101; i++)); do
13941                 lpath="$lpath/$i"
13942                 mkdir $DIR/$lpath
13943                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
13944                         error "get fid for local directory $DIR/$lpath failed"
13945                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
13946
13947                 rpath="$rpath/$i"
13948                 test_mkdir $DIR/$rpath
13949                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
13950                         error "get fid for remote directory $DIR/$rpath failed"
13951                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
13952         done
13953
13954         return 0
13955 }
13956 run_test 162c "fid2path works with paths 100 or more directories deep"
13957
13958 test_169() {
13959         # do directio so as not to populate the page cache
13960         log "creating a 10 Mb file"
13961         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13962         log "starting reads"
13963         dd if=$DIR/$tfile of=/dev/null bs=4096 &
13964         log "truncating the file"
13965         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13966         log "killing dd"
13967         kill %+ || true # reads might have finished
13968         echo "wait until dd is finished"
13969         wait
13970         log "removing the temporary file"
13971         rm -rf $DIR/$tfile || error "tmp file removal failed"
13972 }
13973 run_test 169 "parallel read and truncate should not deadlock"
13974
13975 test_170() {
13976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13977
13978         $LCTL clear     # bug 18514
13979         $LCTL debug_daemon start $TMP/${tfile}_log_good
13980         touch $DIR/$tfile
13981         $LCTL debug_daemon stop
13982         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
13983                 error "sed failed to read log_good"
13984
13985         $LCTL debug_daemon start $TMP/${tfile}_log_good
13986         rm -rf $DIR/$tfile
13987         $LCTL debug_daemon stop
13988
13989         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
13990                error "lctl df log_bad failed"
13991
13992         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
13993         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
13994
13995         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
13996         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
13997
13998         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
13999                 error "bad_line good_line1 good_line2 are empty"
14000
14001         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14002         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
14003         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
14004
14005         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
14006         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
14007         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
14008
14009         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
14010                 error "bad_line_new good_line_new are empty"
14011
14012         local expected_good=$((good_line1 + good_line2*2))
14013
14014         rm -f $TMP/${tfile}*
14015         # LU-231, short malformed line may not be counted into bad lines
14016         if [ $bad_line -ne $bad_line_new ] &&
14017                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
14018                 error "expected $bad_line bad lines, but got $bad_line_new"
14019                 return 1
14020         fi
14021
14022         if [ $expected_good -ne $good_line_new ]; then
14023                 error "expected $expected_good good lines, but got $good_line_new"
14024                 return 2
14025         fi
14026         true
14027 }
14028 run_test 170 "test lctl df to handle corrupted log ====================="
14029
14030 test_171() { # bug20592
14031         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14032
14033         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
14034         $LCTL set_param fail_loc=0x50e
14035         $LCTL set_param fail_val=3000
14036         multiop_bg_pause $DIR/$tfile O_s || true
14037         local MULTIPID=$!
14038         kill -USR1 $MULTIPID
14039         # cause log dump
14040         sleep 3
14041         wait $MULTIPID
14042         if dmesg | grep "recursive fault"; then
14043                 error "caught a recursive fault"
14044         fi
14045         $LCTL set_param fail_loc=0
14046         true
14047 }
14048 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
14049
14050 # it would be good to share it with obdfilter-survey/iokit-libecho code
14051 setup_obdecho_osc () {
14052         local rc=0
14053         local ost_nid=$1
14054         local obdfilter_name=$2
14055         echo "Creating new osc for $obdfilter_name on $ost_nid"
14056         # make sure we can find loopback nid
14057         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
14058
14059         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
14060                            ${obdfilter_name}_osc_UUID || rc=2; }
14061         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
14062                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
14063         return $rc
14064 }
14065
14066 cleanup_obdecho_osc () {
14067         local obdfilter_name=$1
14068         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
14069         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
14070         return 0
14071 }
14072
14073 obdecho_test() {
14074         local OBD=$1
14075         local node=$2
14076         local pages=${3:-64}
14077         local rc=0
14078         local id
14079
14080         local count=10
14081         local obd_size=$(get_obd_size $node $OBD)
14082         local page_size=$(get_page_size $node)
14083         if [[ -n "$obd_size" ]]; then
14084                 local new_count=$((obd_size / (pages * page_size / 1024)))
14085                 [[ $new_count -ge $count ]] || count=$new_count
14086         fi
14087
14088         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
14089         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
14090                            rc=2; }
14091         if [ $rc -eq 0 ]; then
14092             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
14093             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
14094         fi
14095         echo "New object id is $id"
14096         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
14097                            rc=4; }
14098         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
14099                            "test_brw $count w v $pages $id" || rc=4; }
14100         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
14101                            rc=4; }
14102         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
14103                                         "cleanup" || rc=5; }
14104         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
14105                                         "detach" || rc=6; }
14106         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
14107         return $rc
14108 }
14109
14110 test_180a() {
14111         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14112
14113         if ! module_loaded obdecho; then
14114                 load_module obdecho/obdecho &&
14115                         stack_trap "rmmod obdecho" EXIT ||
14116                         error "unable to load obdecho on client"
14117         fi
14118
14119         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
14120         local host=$($LCTL get_param -n osc.$osc.import |
14121                      awk '/current_connection:/ { print $2 }' )
14122         local target=$($LCTL get_param -n osc.$osc.import |
14123                        awk '/target:/ { print $2 }' )
14124         target=${target%_UUID}
14125
14126         if [ -n "$target" ]; then
14127                 setup_obdecho_osc $host $target &&
14128                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
14129                         { error "obdecho setup failed with $?"; return; }
14130
14131                 obdecho_test ${target}_osc client ||
14132                         error "obdecho_test failed on ${target}_osc"
14133         else
14134                 $LCTL get_param osc.$osc.import
14135                 error "there is no osc.$osc.import target"
14136         fi
14137 }
14138 run_test 180a "test obdecho on osc"
14139
14140 test_180b() {
14141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14142         remote_ost_nodsh && skip "remote OST with nodsh"
14143
14144         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14145                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14146                 error "failed to load module obdecho"
14147
14148         local target=$(do_facet ost1 $LCTL dl |
14149                        awk '/obdfilter/ { print $4; exit; }')
14150
14151         if [ -n "$target" ]; then
14152                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
14153         else
14154                 do_facet ost1 $LCTL dl
14155                 error "there is no obdfilter target on ost1"
14156         fi
14157 }
14158 run_test 180b "test obdecho directly on obdfilter"
14159
14160 test_180c() { # LU-2598
14161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14162         remote_ost_nodsh && skip "remote OST with nodsh"
14163         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
14164                 skip "Need MDS version at least 2.4.0"
14165
14166         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
14167                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
14168                 error "failed to load module obdecho"
14169
14170         local target=$(do_facet ost1 $LCTL dl |
14171                        awk '/obdfilter/ { print $4; exit; }')
14172
14173         if [ -n "$target" ]; then
14174                 local pages=16384 # 64MB bulk I/O RPC size
14175
14176                 obdecho_test "$target" ost1 "$pages" ||
14177                         error "obdecho_test with pages=$pages failed with $?"
14178         else
14179                 do_facet ost1 $LCTL dl
14180                 error "there is no obdfilter target on ost1"
14181         fi
14182 }
14183 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
14184
14185 test_181() { # bug 22177
14186         test_mkdir $DIR/$tdir
14187         # create enough files to index the directory
14188         createmany -o $DIR/$tdir/foobar 4000
14189         # print attributes for debug purpose
14190         lsattr -d .
14191         # open dir
14192         multiop_bg_pause $DIR/$tdir D_Sc || return 1
14193         MULTIPID=$!
14194         # remove the files & current working dir
14195         unlinkmany $DIR/$tdir/foobar 4000
14196         rmdir $DIR/$tdir
14197         kill -USR1 $MULTIPID
14198         wait $MULTIPID
14199         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
14200         return 0
14201 }
14202 run_test 181 "Test open-unlinked dir ========================"
14203
14204 test_182() {
14205         local fcount=1000
14206         local tcount=10
14207
14208         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14209
14210         $LCTL set_param mdc.*.rpc_stats=clear
14211
14212         for (( i = 0; i < $tcount; i++ )) ; do
14213                 mkdir $DIR/$tdir/$i
14214         done
14215
14216         for (( i = 0; i < $tcount; i++ )) ; do
14217                 createmany -o $DIR/$tdir/$i/f- $fcount &
14218         done
14219         wait
14220
14221         for (( i = 0; i < $tcount; i++ )) ; do
14222                 unlinkmany $DIR/$tdir/$i/f- $fcount &
14223         done
14224         wait
14225
14226         $LCTL get_param mdc.*.rpc_stats
14227
14228         rm -rf $DIR/$tdir
14229 }
14230 run_test 182 "Test parallel modify metadata operations ================"
14231
14232 test_183() { # LU-2275
14233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14234         remote_mds_nodsh && skip "remote MDS with nodsh"
14235         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
14236                 skip "Need MDS version at least 2.3.56"
14237
14238         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14239         echo aaa > $DIR/$tdir/$tfile
14240
14241 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
14242         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
14243
14244         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
14245         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
14246
14247         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14248
14249         # Flush negative dentry cache
14250         touch $DIR/$tdir/$tfile
14251
14252         # We are not checking for any leaked references here, they'll
14253         # become evident next time we do cleanup with module unload.
14254         rm -rf $DIR/$tdir
14255 }
14256 run_test 183 "No crash or request leak in case of strange dispositions ========"
14257
14258 # test suite 184 is for LU-2016, LU-2017
14259 test_184a() {
14260         check_swap_layouts_support
14261
14262         dir0=$DIR/$tdir/$testnum
14263         test_mkdir -p -c1 $dir0
14264         ref1=/etc/passwd
14265         ref2=/etc/group
14266         file1=$dir0/f1
14267         file2=$dir0/f2
14268         $SETSTRIPE -c1 $file1
14269         cp $ref1 $file1
14270         $SETSTRIPE -c2 $file2
14271         cp $ref2 $file2
14272         gen1=$($GETSTRIPE -g $file1)
14273         gen2=$($GETSTRIPE -g $file2)
14274
14275         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
14276         gen=$($GETSTRIPE -g $file1)
14277         [[ $gen1 != $gen ]] ||
14278                 "Layout generation on $file1 does not change"
14279         gen=$($GETSTRIPE -g $file2)
14280         [[ $gen2 != $gen ]] ||
14281                 "Layout generation on $file2 does not change"
14282
14283         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
14284         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14285
14286         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
14287 }
14288 run_test 184a "Basic layout swap"
14289
14290 test_184b() {
14291         check_swap_layouts_support
14292
14293         dir0=$DIR/$tdir/$testnum
14294         mkdir -p $dir0 || error "creating dir $dir0"
14295         file1=$dir0/f1
14296         file2=$dir0/f2
14297         file3=$dir0/f3
14298         dir1=$dir0/d1
14299         dir2=$dir0/d2
14300         mkdir $dir1 $dir2
14301         $SETSTRIPE -c1 $file1
14302         $SETSTRIPE -c2 $file2
14303         $SETSTRIPE -c1 $file3
14304         chown $RUNAS_ID $file3
14305         gen1=$($GETSTRIPE -g $file1)
14306         gen2=$($GETSTRIPE -g $file2)
14307
14308         $LFS swap_layouts $dir1 $dir2 &&
14309                 error "swap of directories layouts should fail"
14310         $LFS swap_layouts $dir1 $file1 &&
14311                 error "swap of directory and file layouts should fail"
14312         $RUNAS $LFS swap_layouts $file1 $file2 &&
14313                 error "swap of file we cannot write should fail"
14314         $LFS swap_layouts $file1 $file3 &&
14315                 error "swap of file with different owner should fail"
14316         /bin/true # to clear error code
14317 }
14318 run_test 184b "Forbidden layout swap (will generate errors)"
14319
14320 test_184c() {
14321         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
14322         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
14323         check_swap_layouts_support
14324
14325         local dir0=$DIR/$tdir/$testnum
14326         mkdir -p $dir0 || error "creating dir $dir0"
14327
14328         local ref1=$dir0/ref1
14329         local ref2=$dir0/ref2
14330         local file1=$dir0/file1
14331         local file2=$dir0/file2
14332         # create a file large enough for the concurrent test
14333         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
14334         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
14335         echo "ref file size: ref1($(stat -c %s $ref1))," \
14336              "ref2($(stat -c %s $ref2))"
14337
14338         cp $ref2 $file2
14339         dd if=$ref1 of=$file1 bs=16k &
14340         local DD_PID=$!
14341
14342         # Make sure dd starts to copy file
14343         while [ ! -f $file1 ]; do sleep 0.1; done
14344
14345         $LFS swap_layouts $file1 $file2
14346         local rc=$?
14347         wait $DD_PID
14348         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
14349         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
14350
14351         # how many bytes copied before swapping layout
14352         local copied=$(stat -c %s $file2)
14353         local remaining=$(stat -c %s $ref1)
14354         remaining=$((remaining - copied))
14355         echo "Copied $copied bytes before swapping layout..."
14356
14357         cmp -n $copied $file1 $ref2 | grep differ &&
14358                 error "Content mismatch [0, $copied) of ref2 and file1"
14359         cmp -n $copied $file2 $ref1 ||
14360                 error "Content mismatch [0, $copied) of ref1 and file2"
14361         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
14362                 error "Content mismatch [$copied, EOF) of ref1 and file1"
14363
14364         # clean up
14365         rm -f $ref1 $ref2 $file1 $file2
14366 }
14367 run_test 184c "Concurrent write and layout swap"
14368
14369 test_184d() {
14370         check_swap_layouts_support
14371         [ -z "$(which getfattr 2>/dev/null)" ] &&
14372                 skip_env "no getfattr command"
14373
14374         local file1=$DIR/$tdir/$tfile-1
14375         local file2=$DIR/$tdir/$tfile-2
14376         local file3=$DIR/$tdir/$tfile-3
14377         local lovea1
14378         local lovea2
14379
14380         mkdir -p $DIR/$tdir
14381         touch $file1 || error "create $file1 failed"
14382         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14383                 error "create $file2 failed"
14384         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14385                 error "create $file3 failed"
14386         lovea1=$(get_layout_param $file1)
14387
14388         $LFS swap_layouts $file2 $file3 ||
14389                 error "swap $file2 $file3 layouts failed"
14390         $LFS swap_layouts $file1 $file2 ||
14391                 error "swap $file1 $file2 layouts failed"
14392
14393         lovea2=$(get_layout_param $file2)
14394         echo "$lovea1"
14395         echo "$lovea2"
14396         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
14397
14398         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14399         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
14400 }
14401 run_test 184d "allow stripeless layouts swap"
14402
14403 test_184e() {
14404         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
14405                 skip "Need MDS version at least 2.6.94"
14406         check_swap_layouts_support
14407         [ -z "$(which getfattr 2>/dev/null)" ] &&
14408                 skip_env "no getfattr command"
14409
14410         local file1=$DIR/$tdir/$tfile-1
14411         local file2=$DIR/$tdir/$tfile-2
14412         local file3=$DIR/$tdir/$tfile-3
14413         local lovea
14414
14415         mkdir -p $DIR/$tdir
14416         touch $file1 || error "create $file1 failed"
14417         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
14418                 error "create $file2 failed"
14419         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
14420                 error "create $file3 failed"
14421
14422         $LFS swap_layouts $file1 $file2 ||
14423                 error "swap $file1 $file2 layouts failed"
14424
14425         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
14426         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
14427
14428         echo 123 > $file1 || error "Should be able to write into $file1"
14429
14430         $LFS swap_layouts $file1 $file3 ||
14431                 error "swap $file1 $file3 layouts failed"
14432
14433         echo 123 > $file1 || error "Should be able to write into $file1"
14434
14435         rm -rf $file1 $file2 $file3
14436 }
14437 run_test 184e "Recreate layout after stripeless layout swaps"
14438
14439 test_184f() {
14440         # Create a file with name longer than sizeof(struct stat) ==
14441         # 144 to see if we can get chars from the file name to appear
14442         # in the returned striping. Note that 'f' == 0x66.
14443         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
14444
14445         mkdir -p $DIR/$tdir
14446         mcreate $DIR/$tdir/$file
14447         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
14448                 error "IOC_MDC_GETFILEINFO returned garbage striping"
14449         fi
14450 }
14451 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
14452
14453 test_185() { # LU-2441
14454         # LU-3553 - no volatile file support in old servers
14455         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
14456                 skip "Need MDS version at least 2.3.60"
14457
14458         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
14459         touch $DIR/$tdir/spoo
14460         local mtime1=$(stat -c "%Y" $DIR/$tdir)
14461         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
14462                 error "cannot create/write a volatile file"
14463         [ "$FILESET" == "" ] &&
14464         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
14465                 error "FID is still valid after close"
14466
14467         multiop_bg_pause $DIR/$tdir vVw4096_c
14468         local multi_pid=$!
14469
14470         local OLD_IFS=$IFS
14471         IFS=":"
14472         local fidv=($fid)
14473         IFS=$OLD_IFS
14474         # assume that the next FID for this client is sequential, since stdout
14475         # is unfortunately eaten by multiop_bg_pause
14476         local n=$((${fidv[1]} + 1))
14477         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
14478         if [ "$FILESET" == "" ]; then
14479                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
14480                         error "FID is missing before close"
14481         fi
14482         kill -USR1 $multi_pid
14483         # 1 second delay, so if mtime change we will see it
14484         sleep 1
14485         local mtime2=$(stat -c "%Y" $DIR/$tdir)
14486         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
14487 }
14488 run_test 185 "Volatile file support"
14489
14490 function create_check_volatile() {
14491         local idx=$1
14492         local tgt
14493
14494         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
14495         local PID=$!
14496         sleep 1
14497         local FID=$(cat /tmp/${tfile}.fid)
14498         [ "$FID" == "" ] && error "can't get FID for volatile"
14499         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
14500         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
14501         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
14502         kill -USR1 $PID
14503         wait
14504         sleep 1
14505         cancel_lru_locks mdc # flush opencache
14506         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
14507         return 0
14508 }
14509
14510 test_185a(){
14511         # LU-12516 - volatile creation via .lustre
14512         [[ $MDS1_VERSION -ge $(version_code 2.12.2) ]] ||
14513                 skip "Need MDS version at least 2.12.2"
14514
14515         create_check_volatile 0
14516         [ $MDSCOUNT -lt 2 ] && return 0
14517
14518         # DNE case
14519         create_check_volatile 1
14520
14521         return 0
14522 }
14523 run_test 185a "Volatile file creation in .lustre/fid/"
14524
14525 test_187a() {
14526         remote_mds_nodsh && skip "remote MDS with nodsh"
14527         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14528                 skip "Need MDS version at least 2.3.0"
14529
14530         local dir0=$DIR/$tdir/$testnum
14531         mkdir -p $dir0 || error "creating dir $dir0"
14532
14533         local file=$dir0/file1
14534         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
14535         local dv1=$($LFS data_version $file)
14536         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
14537         local dv2=$($LFS data_version $file)
14538         [[ $dv1 != $dv2 ]] ||
14539                 error "data version did not change on write $dv1 == $dv2"
14540
14541         # clean up
14542         rm -f $file1
14543 }
14544 run_test 187a "Test data version change"
14545
14546 test_187b() {
14547         remote_mds_nodsh && skip "remote MDS with nodsh"
14548         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
14549                 skip "Need MDS version at least 2.3.0"
14550
14551         local dir0=$DIR/$tdir/$testnum
14552         mkdir -p $dir0 || error "creating dir $dir0"
14553
14554         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
14555         [[ ${DV[0]} != ${DV[1]} ]] ||
14556                 error "data version did not change on write"\
14557                       " ${DV[0]} == ${DV[1]}"
14558
14559         # clean up
14560         rm -f $file1
14561 }
14562 run_test 187b "Test data version change on volatile file"
14563
14564 test_200() {
14565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14566         remote_mgs_nodsh && skip "remote MGS with nodsh"
14567         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14568
14569         local POOL=${POOL:-cea1}
14570         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
14571         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
14572         # Pool OST targets
14573         local first_ost=0
14574         local last_ost=$(($OSTCOUNT - 1))
14575         local ost_step=2
14576         local ost_list=$(seq $first_ost $ost_step $last_ost)
14577         local ost_range="$first_ost $last_ost $ost_step"
14578         local test_path=$POOL_ROOT/$POOL_DIR_NAME
14579         local file_dir=$POOL_ROOT/file_tst
14580         local subdir=$test_path/subdir
14581         local rc=0
14582
14583         while : ; do
14584                 # former test_200a test_200b
14585                 pool_add $POOL                          || { rc=$? ; break; }
14586                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
14587                 # former test_200c test_200d
14588                 mkdir -p $test_path
14589                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
14590                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
14591                 mkdir -p $subdir
14592                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
14593                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
14594                                                         || { rc=$? ; break; }
14595                 # former test_200e test_200f
14596                 local files=$((OSTCOUNT*3))
14597                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
14598                                                         || { rc=$? ; break; }
14599                 pool_create_files $POOL $file_dir $files "$ost_list" \
14600                                                         || { rc=$? ; break; }
14601                 # former test_200g test_200h
14602                 pool_lfs_df $POOL                       || { rc=$? ; break; }
14603                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
14604
14605                 # former test_201a test_201b test_201c
14606                 pool_remove_first_target $POOL          || { rc=$? ; break; }
14607
14608                 local f=$test_path/$tfile
14609                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
14610                 pool_remove $POOL $f                    || { rc=$? ; break; }
14611                 break
14612         done
14613
14614         destroy_test_pools
14615
14616         return $rc
14617 }
14618 run_test 200 "OST pools"
14619
14620 # usage: default_attr <count | size | offset>
14621 default_attr() {
14622         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
14623 }
14624
14625 # usage: check_default_stripe_attr
14626 check_default_stripe_attr() {
14627         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
14628         case $1 in
14629         --stripe-count|-c)
14630                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
14631         --stripe-size|-S)
14632                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
14633         --stripe-index|-i)
14634                 EXPECTED=-1;;
14635         *)
14636                 error "unknown getstripe attr '$1'"
14637         esac
14638
14639         [ $ACTUAL == $EXPECTED ] ||
14640                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
14641 }
14642
14643 test_204a() {
14644         test_mkdir $DIR/$tdir
14645         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
14646
14647         check_default_stripe_attr --stripe-count
14648         check_default_stripe_attr --stripe-size
14649         check_default_stripe_attr --stripe-index
14650 }
14651 run_test 204a "Print default stripe attributes"
14652
14653 test_204b() {
14654         test_mkdir $DIR/$tdir
14655         $SETSTRIPE --stripe-count 1 $DIR/$tdir
14656
14657         check_default_stripe_attr --stripe-size
14658         check_default_stripe_attr --stripe-index
14659 }
14660 run_test 204b "Print default stripe size and offset"
14661
14662 test_204c() {
14663         test_mkdir $DIR/$tdir
14664         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
14665
14666         check_default_stripe_attr --stripe-count
14667         check_default_stripe_attr --stripe-index
14668 }
14669 run_test 204c "Print default stripe count and offset"
14670
14671 test_204d() {
14672         test_mkdir $DIR/$tdir
14673         $SETSTRIPE --stripe-index 0 $DIR/$tdir
14674
14675         check_default_stripe_attr --stripe-count
14676         check_default_stripe_attr --stripe-size
14677 }
14678 run_test 204d "Print default stripe count and size"
14679
14680 test_204e() {
14681         test_mkdir $DIR/$tdir
14682         $SETSTRIPE -d $DIR/$tdir
14683
14684         check_default_stripe_attr --stripe-count --raw
14685         check_default_stripe_attr --stripe-size --raw
14686         check_default_stripe_attr --stripe-index --raw
14687 }
14688 run_test 204e "Print raw stripe attributes"
14689
14690 test_204f() {
14691         test_mkdir $DIR/$tdir
14692         $SETSTRIPE --stripe-count 1 $DIR/$tdir
14693
14694         check_default_stripe_attr --stripe-size --raw
14695         check_default_stripe_attr --stripe-index --raw
14696 }
14697 run_test 204f "Print raw stripe size and offset"
14698
14699 test_204g() {
14700         test_mkdir $DIR/$tdir
14701         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
14702
14703         check_default_stripe_attr --stripe-count --raw
14704         check_default_stripe_attr --stripe-index --raw
14705 }
14706 run_test 204g "Print raw stripe count and offset"
14707
14708 test_204h() {
14709         test_mkdir $DIR/$tdir
14710         $SETSTRIPE --stripe-index 0 $DIR/$tdir
14711
14712         check_default_stripe_attr --stripe-count --raw
14713         check_default_stripe_attr --stripe-size --raw
14714 }
14715 run_test 204h "Print raw stripe count and size"
14716
14717 # Figure out which job scheduler is being used, if any,
14718 # or use a fake one
14719 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
14720         JOBENV=SLURM_JOB_ID
14721 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
14722         JOBENV=LSB_JOBID
14723 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
14724         JOBENV=PBS_JOBID
14725 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
14726         JOBENV=LOADL_STEP_ID
14727 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
14728         JOBENV=JOB_ID
14729 else
14730         $LCTL list_param jobid_name > /dev/null 2>&1
14731         if [ $? -eq 0 ]; then
14732                 JOBENV=nodelocal
14733         else
14734                 JOBENV=FAKE_JOBID
14735         fi
14736 fi
14737 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
14738
14739 verify_jobstats() {
14740         local cmd=($1)
14741         shift
14742         local facets="$@"
14743
14744 # we don't really need to clear the stats for this test to work, since each
14745 # command has a unique jobid, but it makes debugging easier if needed.
14746 #       for facet in $facets; do
14747 #               local dev=$(convert_facet2label $facet)
14748 #               # clear old jobstats
14749 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
14750 #       done
14751
14752         # use a new JobID for each test, or we might see an old one
14753         [ "$JOBENV" = "FAKE_JOBID" ] &&
14754                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
14755
14756         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
14757
14758         [ "$JOBENV" = "nodelocal" ] && {
14759                 FAKE_JOBID=id.$testnum.%e.$RANDOM
14760                 $LCTL set_param jobid_name=$FAKE_JOBID
14761                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
14762         }
14763
14764         log "Test: ${cmd[*]}"
14765         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
14766
14767         if [ $JOBENV = "FAKE_JOBID" ]; then
14768                 FAKE_JOBID=$JOBVAL ${cmd[*]}
14769         else
14770                 ${cmd[*]}
14771         fi
14772
14773         # all files are created on OST0000
14774         for facet in $facets; do
14775                 local stats="*.$(convert_facet2label $facet).job_stats"
14776
14777                 # strip out libtool wrappers for in-tree executables
14778                 if [ $(do_facet $facet lctl get_param $stats |
14779                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
14780                         do_facet $facet lctl get_param $stats
14781                         error "No jobstats for $JOBVAL found on $facet::$stats"
14782                 fi
14783         done
14784 }
14785
14786 jobstats_set() {
14787         local new_jobenv=$1
14788
14789         set_persistent_param_and_check client "jobid_var" \
14790                 "$FSNAME.sys.jobid_var" $new_jobenv
14791 }
14792
14793 test_205a() { # Job stats
14794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14795         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
14796                 skip "Need MDS version with at least 2.7.1"
14797         remote_mgs_nodsh && skip "remote MGS with nodsh"
14798         remote_mds_nodsh && skip "remote MDS with nodsh"
14799         remote_ost_nodsh && skip "remote OST with nodsh"
14800         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
14801                 skip "Server doesn't support jobstats"
14802         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
14803
14804         local old_jobenv=$($LCTL get_param -n jobid_var)
14805         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
14806
14807         if [[ $PERM_CMD = *"set_param -P"* ]]; then
14808                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
14809         else
14810                 stack_trap "do_facet mgs $PERM_CMD \
14811                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
14812         fi
14813         changelog_register
14814
14815         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
14816                                 mdt.*.job_cleanup_interval | head -n 1)
14817         local new_interval=5
14818         do_facet $SINGLEMDS \
14819                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
14820         stack_trap "do_facet $SINGLEMDS \
14821                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
14822         local start=$SECONDS
14823
14824         local cmd
14825         # mkdir
14826         cmd="mkdir $DIR/$tdir"
14827         verify_jobstats "$cmd" "$SINGLEMDS"
14828         # rmdir
14829         cmd="rmdir $DIR/$tdir"
14830         verify_jobstats "$cmd" "$SINGLEMDS"
14831         # mkdir on secondary MDT
14832         if [ $MDSCOUNT -gt 1 ]; then
14833                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
14834                 verify_jobstats "$cmd" "mds2"
14835         fi
14836         # mknod
14837         cmd="mknod $DIR/$tfile c 1 3"
14838         verify_jobstats "$cmd" "$SINGLEMDS"
14839         # unlink
14840         cmd="rm -f $DIR/$tfile"
14841         verify_jobstats "$cmd" "$SINGLEMDS"
14842         # create all files on OST0000 so verify_jobstats can find OST stats
14843         # open & close
14844         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
14845         verify_jobstats "$cmd" "$SINGLEMDS"
14846         # setattr
14847         cmd="touch $DIR/$tfile"
14848         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14849         # write
14850         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
14851         verify_jobstats "$cmd" "ost1"
14852         # read
14853         cancel_lru_locks osc
14854         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
14855         verify_jobstats "$cmd" "ost1"
14856         # truncate
14857         cmd="$TRUNCATE $DIR/$tfile 0"
14858         verify_jobstats "$cmd" "$SINGLEMDS ost1"
14859         # rename
14860         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
14861         verify_jobstats "$cmd" "$SINGLEMDS"
14862         # jobstats expiry - sleep until old stats should be expired
14863         local left=$((new_interval + 5 - (SECONDS - start)))
14864         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
14865                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
14866                         "0" $left
14867         cmd="mkdir $DIR/$tdir.expire"
14868         verify_jobstats "$cmd" "$SINGLEMDS"
14869         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
14870             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
14871
14872         # Ensure that jobid are present in changelog (if supported by MDS)
14873         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
14874                 changelog_dump | tail -10
14875                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
14876                 [ $jobids -eq 9 ] ||
14877                         error "Wrong changelog jobid count $jobids != 9"
14878
14879                 # LU-5862
14880                 JOBENV="disable"
14881                 jobstats_set $JOBENV
14882                 touch $DIR/$tfile
14883                 changelog_dump | grep $tfile
14884                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
14885                 [ $jobids -eq 0 ] ||
14886                         error "Unexpected jobids when jobid_var=$JOBENV"
14887         fi
14888
14889         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
14890         JOBENV="JOBCOMPLEX"
14891         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
14892
14893         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
14894 }
14895 run_test 205a "Verify job stats"
14896
14897 # LU-13117
14898 test_205b() {
14899         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
14900         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
14901         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
14902                 grep job_id: | grep foolish &&
14903                         error "Unexpected jobid found"
14904         true
14905 }
14906 run_test 205b "Verify job stats jobid parsing"
14907
14908 # LU-1480, LU-1773 and LU-1657
14909 test_206() {
14910         mkdir -p $DIR/$tdir
14911         $SETSTRIPE -c -1 $DIR/$tdir
14912 #define OBD_FAIL_LOV_INIT 0x1403
14913         $LCTL set_param fail_loc=0xa0001403
14914         $LCTL set_param fail_val=1
14915         touch $DIR/$tdir/$tfile || true
14916 }
14917 run_test 206 "fail lov_init_raid0() doesn't lbug"
14918
14919 test_207a() {
14920         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14921         local fsz=`stat -c %s $DIR/$tfile`
14922         cancel_lru_locks mdc
14923
14924         # do not return layout in getattr intent
14925 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
14926         $LCTL set_param fail_loc=0x170
14927         local sz=`stat -c %s $DIR/$tfile`
14928
14929         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
14930
14931         rm -rf $DIR/$tfile
14932 }
14933 run_test 207a "can refresh layout at glimpse"
14934
14935 test_207b() {
14936         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
14937         local cksum=`md5sum $DIR/$tfile`
14938         local fsz=`stat -c %s $DIR/$tfile`
14939         cancel_lru_locks mdc
14940         cancel_lru_locks osc
14941
14942         # do not return layout in getattr intent
14943 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
14944         $LCTL set_param fail_loc=0x171
14945
14946         # it will refresh layout after the file is opened but before read issues
14947         echo checksum is "$cksum"
14948         echo "$cksum" |md5sum -c --quiet || error "file differs"
14949
14950         rm -rf $DIR/$tfile
14951 }
14952 run_test 207b "can refresh layout at open"
14953
14954 test_208() {
14955         # FIXME: in this test suite, only RD lease is used. This is okay
14956         # for now as only exclusive open is supported. After generic lease
14957         # is done, this test suite should be revised. - Jinshan
14958
14959         remote_mds_nodsh && skip "remote MDS with nodsh"
14960         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
14961                 skip "Need MDS version at least 2.4.52"
14962
14963         echo "==== test 1: verify get lease work"
14964         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
14965
14966         echo "==== test 2: verify lease can be broken by upcoming open"
14967         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14968         local PID=$!
14969         sleep 1
14970
14971         $MULTIOP $DIR/$tfile oO_RDONLY:c
14972         kill -USR1 $PID && wait $PID || error "break lease error"
14973
14974         echo "==== test 3: verify lease can't be granted if an open already exists"
14975         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
14976         local PID=$!
14977         sleep 1
14978
14979         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
14980         kill -USR1 $PID && wait $PID || error "open file error"
14981
14982         echo "==== test 4: lease can sustain over recovery"
14983         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
14984         PID=$!
14985         sleep 1
14986
14987         fail mds1
14988
14989         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
14990
14991         echo "==== test 5: lease broken can't be regained by replay"
14992         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
14993         PID=$!
14994         sleep 1
14995
14996         # open file to break lease and then recovery
14997         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
14998         fail mds1
14999
15000         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
15001
15002         rm -f $DIR/$tfile
15003 }
15004 run_test 208 "Exclusive open"
15005
15006 test_209() {
15007         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
15008                 skip_env "must have disp_stripe"
15009
15010         touch $DIR/$tfile
15011         sync; sleep 5; sync;
15012
15013         echo 3 > /proc/sys/vm/drop_caches
15014         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15015
15016         # open/close 500 times
15017         for i in $(seq 500); do
15018                 cat $DIR/$tfile
15019         done
15020
15021         echo 3 > /proc/sys/vm/drop_caches
15022         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
15023
15024         echo "before: $req_before, after: $req_after"
15025         [ $((req_after - req_before)) -ge 300 ] &&
15026                 error "open/close requests are not freed"
15027         return 0
15028 }
15029 run_test 209 "read-only open/close requests should be freed promptly"
15030
15031 test_212() {
15032         size=`date +%s`
15033         size=$((size % 8192 + 1))
15034         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
15035         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
15036         rm -f $DIR/f212 $DIR/f212.xyz
15037 }
15038 run_test 212 "Sendfile test ============================================"
15039
15040 test_213() {
15041         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
15042         cancel_lru_locks osc
15043         lctl set_param fail_loc=0x8000040f
15044         # generate a read lock
15045         cat $DIR/$tfile > /dev/null
15046         # write to the file, it will try to cancel the above read lock.
15047         cat /etc/hosts >> $DIR/$tfile
15048 }
15049 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
15050
15051 test_214() { # for bug 20133
15052         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
15053         for (( i=0; i < 340; i++ )) ; do
15054                 touch $DIR/$tdir/d214c/a$i
15055         done
15056
15057         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
15058         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
15059         ls $DIR/d214c || error "ls $DIR/d214c failed"
15060         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
15061         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
15062 }
15063 run_test 214 "hash-indexed directory test - bug 20133"
15064
15065 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
15066 create_lnet_proc_files() {
15067         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
15068 }
15069
15070 # counterpart of create_lnet_proc_files
15071 remove_lnet_proc_files() {
15072         rm -f $TMP/lnet_$1.sys
15073 }
15074
15075 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15076 # 3rd arg as regexp for body
15077 check_lnet_proc_stats() {
15078         local l=$(cat "$TMP/lnet_$1" |wc -l)
15079         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
15080
15081         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
15082 }
15083
15084 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
15085 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
15086 # optional and can be regexp for 2nd line (lnet.routes case)
15087 check_lnet_proc_entry() {
15088         local blp=2          # blp stands for 'position of 1st line of body'
15089         [ -z "$5" ] || blp=3 # lnet.routes case
15090
15091         local l=$(cat "$TMP/lnet_$1" |wc -l)
15092         # subtracting one from $blp because the body can be empty
15093         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
15094
15095         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
15096                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
15097
15098         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
15099                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
15100
15101         # bail out if any unexpected line happened
15102         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
15103         [ "$?" != 0 ] || error "$2 misformatted"
15104 }
15105
15106 test_215() { # for bugs 18102, 21079, 21517
15107         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15108
15109         local N='(0|[1-9][0-9]*)'       # non-negative numeric
15110         local P='[1-9][0-9]*'           # positive numeric
15111         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
15112         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
15113         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
15114         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
15115
15116         local L1 # regexp for 1st line
15117         local L2 # regexp for 2nd line (optional)
15118         local BR # regexp for the rest (body)
15119
15120         # lnet.stats should look as 11 space-separated non-negative numerics
15121         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
15122         create_lnet_proc_files "stats"
15123         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
15124         remove_lnet_proc_files "stats"
15125
15126         # lnet.routes should look like this:
15127         # Routing disabled/enabled
15128         # net hops priority state router
15129         # where net is a string like tcp0, hops > 0, priority >= 0,
15130         # state is up/down,
15131         # router is a string like 192.168.1.1@tcp2
15132         L1="^Routing (disabled|enabled)$"
15133         L2="^net +hops +priority +state +router$"
15134         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
15135         create_lnet_proc_files "routes"
15136         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
15137         remove_lnet_proc_files "routes"
15138
15139         # lnet.routers should look like this:
15140         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
15141         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
15142         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
15143         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
15144         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
15145         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
15146         create_lnet_proc_files "routers"
15147         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
15148         remove_lnet_proc_files "routers"
15149
15150         # lnet.peers should look like this:
15151         # nid refs state last max rtr min tx min queue
15152         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
15153         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
15154         # numeric (0 or >0 or <0), queue >= 0.
15155         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
15156         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
15157         create_lnet_proc_files "peers"
15158         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
15159         remove_lnet_proc_files "peers"
15160
15161         # lnet.buffers  should look like this:
15162         # pages count credits min
15163         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
15164         L1="^pages +count +credits +min$"
15165         BR="^ +$N +$N +$I +$I$"
15166         create_lnet_proc_files "buffers"
15167         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
15168         remove_lnet_proc_files "buffers"
15169
15170         # lnet.nis should look like this:
15171         # nid status alive refs peer rtr max tx min
15172         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
15173         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
15174         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
15175         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
15176         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
15177         create_lnet_proc_files "nis"
15178         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
15179         remove_lnet_proc_files "nis"
15180
15181         # can we successfully write to lnet.stats?
15182         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
15183 }
15184 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
15185
15186 test_216() { # bug 20317
15187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15188         remote_ost_nodsh && skip "remote OST with nodsh"
15189
15190         local node
15191         local facets=$(get_facets OST)
15192         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15193
15194         save_lustre_params client "osc.*.contention_seconds" > $p
15195         save_lustre_params $facets \
15196                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
15197         save_lustre_params $facets \
15198                 "ldlm.namespaces.filter-*.contended_locks" >> $p
15199         save_lustre_params $facets \
15200                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
15201         clear_stats osc.*.osc_stats
15202
15203         # agressive lockless i/o settings
15204         do_nodes $(comma_list $(osts_nodes)) \
15205                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
15206                         ldlm.namespaces.filter-*.contended_locks=0 \
15207                         ldlm.namespaces.filter-*.contention_seconds=60"
15208         lctl set_param -n osc.*.contention_seconds=60
15209
15210         $DIRECTIO write $DIR/$tfile 0 10 4096
15211         $CHECKSTAT -s 40960 $DIR/$tfile
15212
15213         # disable lockless i/o
15214         do_nodes $(comma_list $(osts_nodes)) \
15215                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
15216                         ldlm.namespaces.filter-*.contended_locks=32 \
15217                         ldlm.namespaces.filter-*.contention_seconds=0"
15218         lctl set_param -n osc.*.contention_seconds=0
15219         clear_stats osc.*.osc_stats
15220
15221         dd if=/dev/zero of=$DIR/$tfile count=0
15222         $CHECKSTAT -s 0 $DIR/$tfile
15223
15224         restore_lustre_params <$p
15225         rm -f $p
15226         rm $DIR/$tfile
15227 }
15228 run_test 216 "check lockless direct write updates file size and kms correctly"
15229
15230 test_217() { # bug 22430
15231         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15232
15233         local node
15234         local nid
15235
15236         for node in $(nodes_list); do
15237                 nid=$(host_nids_address $node $NETTYPE)
15238                 if [[ $nid = *-* ]] ; then
15239                         echo "lctl ping $(h2nettype $nid)"
15240                         lctl ping $(h2nettype $nid)
15241                 else
15242                         echo "skipping $node (no hyphen detected)"
15243                 fi
15244         done
15245 }
15246 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
15247
15248 test_218() {
15249        # do directio so as not to populate the page cache
15250        log "creating a 10 Mb file"
15251        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15252        log "starting reads"
15253        dd if=$DIR/$tfile of=/dev/null bs=4096 &
15254        log "truncating the file"
15255        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15256        log "killing dd"
15257        kill %+ || true # reads might have finished
15258        echo "wait until dd is finished"
15259        wait
15260        log "removing the temporary file"
15261        rm -rf $DIR/$tfile || error "tmp file removal failed"
15262 }
15263 run_test 218 "parallel read and truncate should not deadlock"
15264
15265 test_219() {
15266         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15267
15268         # write one partial page
15269         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
15270         # set no grant so vvp_io_commit_write will do sync write
15271         $LCTL set_param fail_loc=0x411
15272         # write a full page at the end of file
15273         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
15274
15275         $LCTL set_param fail_loc=0
15276         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
15277         $LCTL set_param fail_loc=0x411
15278         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
15279
15280         # LU-4201
15281         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
15282         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
15283 }
15284 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
15285
15286 test_220() { #LU-325
15287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15288         remote_ost_nodsh && skip "remote OST with nodsh"
15289         remote_mds_nodsh && skip "remote MDS with nodsh"
15290         remote_mgs_nodsh && skip "remote MGS with nodsh"
15291
15292         local OSTIDX=0
15293
15294         # create on MDT0000 so the last_id and next_id are correct
15295         mkdir $DIR/$tdir
15296         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
15297         OST=${OST%_UUID}
15298
15299         # on the mdt's osc
15300         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
15301         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
15302                         osp.$mdtosc_proc1.prealloc_last_id)
15303         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
15304                         osp.$mdtosc_proc1.prealloc_next_id)
15305
15306         $LFS df -i
15307
15308         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
15309         #define OBD_FAIL_OST_ENOINO              0x229
15310         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
15311         create_pool $FSNAME.$TESTNAME || return 1
15312         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
15313
15314         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
15315
15316         MDSOBJS=$((last_id - next_id))
15317         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
15318
15319         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
15320         echo "OST still has $count kbytes free"
15321
15322         echo "create $MDSOBJS files @next_id..."
15323         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
15324
15325         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15326                         osp.$mdtosc_proc1.prealloc_last_id)
15327         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
15328                         osp.$mdtosc_proc1.prealloc_next_id)
15329
15330         echo "after creation, last_id=$last_id2, next_id=$next_id2"
15331         $LFS df -i
15332
15333         echo "cleanup..."
15334
15335         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
15336         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
15337
15338         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
15339                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
15340         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15341                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
15342         echo "unlink $MDSOBJS files @$next_id..."
15343         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
15344 }
15345 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
15346
15347 test_221() {
15348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15349
15350         dd if=`which date` of=$MOUNT/date oflag=sync
15351         chmod +x $MOUNT/date
15352
15353         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
15354         $LCTL set_param fail_loc=0x80001401
15355
15356         $MOUNT/date > /dev/null
15357         rm -f $MOUNT/date
15358 }
15359 run_test 221 "make sure fault and truncate race to not cause OOM"
15360
15361 test_222a () {
15362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15363
15364         rm -rf $DIR/$tdir
15365         test_mkdir $DIR/$tdir
15366         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15367         createmany -o $DIR/$tdir/$tfile 10
15368         cancel_lru_locks mdc
15369         cancel_lru_locks osc
15370         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15371         $LCTL set_param fail_loc=0x31a
15372         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
15373         $LCTL set_param fail_loc=0
15374         rm -r $DIR/$tdir
15375 }
15376 run_test 222a "AGL for ls should not trigger CLIO lock failure"
15377
15378 test_222b () {
15379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15380
15381         rm -rf $DIR/$tdir
15382         test_mkdir $DIR/$tdir
15383         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15384         createmany -o $DIR/$tdir/$tfile 10
15385         cancel_lru_locks mdc
15386         cancel_lru_locks osc
15387         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
15388         $LCTL set_param fail_loc=0x31a
15389         rm -r $DIR/$tdir || error "AGL for rmdir failed"
15390         $LCTL set_param fail_loc=0
15391 }
15392 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
15393
15394 test_223 () {
15395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15396
15397         rm -rf $DIR/$tdir
15398         test_mkdir $DIR/$tdir
15399         $LFS setstripe -c 1 -i 0 $DIR/$tdir
15400         createmany -o $DIR/$tdir/$tfile 10
15401         cancel_lru_locks mdc
15402         cancel_lru_locks osc
15403         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
15404         $LCTL set_param fail_loc=0x31b
15405         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
15406         $LCTL set_param fail_loc=0
15407         rm -r $DIR/$tdir
15408 }
15409 run_test 223 "osc reenqueue if without AGL lock granted ======================="
15410
15411 test_224a() { # LU-1039, MRP-303
15412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15413
15414         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
15415         $LCTL set_param fail_loc=0x508
15416         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
15417         $LCTL set_param fail_loc=0
15418         df $DIR
15419 }
15420 run_test 224a "Don't panic on bulk IO failure"
15421
15422 test_224b() { # LU-1039, MRP-303
15423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15424
15425         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
15426         cancel_lru_locks osc
15427         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
15428         $LCTL set_param fail_loc=0x515
15429         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
15430         $LCTL set_param fail_loc=0
15431         df $DIR
15432 }
15433 run_test 224b "Don't panic on bulk IO failure"
15434
15435 test_224c() { # LU-6441
15436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15437         remote_mds_nodsh && skip "remote MDS with nodsh"
15438
15439         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15440         save_writethrough $p
15441         set_cache writethrough on
15442
15443         local pages_per_rpc=$($LCTL get_param \
15444                                 osc.*.max_pages_per_rpc)
15445         local at_max=$($LCTL get_param -n at_max)
15446         local timeout=$($LCTL get_param -n timeout)
15447         local test_at="at_max"
15448         local param_at="$FSNAME.sys.at_max"
15449         local test_timeout="timeout"
15450         local param_timeout="$FSNAME.sys.timeout"
15451
15452         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
15453
15454         set_persistent_param_and_check client "$test_at" "$param_at" 0
15455         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
15456
15457         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
15458         do_facet ost1 $LCTL set_param fail_loc=0x520
15459         $LFS setstripe -c 1 -i 0 $DIR/$tfile
15460         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
15461         sync
15462         do_facet ost1 $LCTL set_param fail_loc=0
15463
15464         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
15465         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
15466                 $timeout
15467
15468         $LCTL set_param -n $pages_per_rpc
15469         restore_lustre_params < $p
15470         rm -f $p
15471 }
15472 run_test 224c "Don't hang if one of md lost during large bulk RPC"
15473
15474 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
15475 test_225a () {
15476         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15477         if [ -z ${MDSSURVEY} ]; then
15478                 skip_env "mds-survey not found"
15479         fi
15480         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15481                 skip "Need MDS version at least 2.2.51"
15482
15483         local mds=$(facet_host $SINGLEMDS)
15484         local target=$(do_nodes $mds 'lctl dl' |
15485                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15486
15487         local cmd1="file_count=1000 thrhi=4"
15488         local cmd2="dir_count=2 layer=mdd stripe_count=0"
15489         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15490         local cmd="$cmd1 $cmd2 $cmd3"
15491
15492         rm -f ${TMP}/mds_survey*
15493         echo + $cmd
15494         eval $cmd || error "mds-survey with zero-stripe failed"
15495         cat ${TMP}/mds_survey*
15496         rm -f ${TMP}/mds_survey*
15497 }
15498 run_test 225a "Metadata survey sanity with zero-stripe"
15499
15500 test_225b () {
15501         if [ -z ${MDSSURVEY} ]; then
15502                 skip_env "mds-survey not found"
15503         fi
15504         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
15505                 skip "Need MDS version at least 2.2.51"
15506         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15507         remote_mds_nodsh && skip "remote MDS with nodsh"
15508         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
15509                 skip_env "Need to mount OST to test"
15510         fi
15511
15512         local mds=$(facet_host $SINGLEMDS)
15513         local target=$(do_nodes $mds 'lctl dl' |
15514                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
15515
15516         local cmd1="file_count=1000 thrhi=4"
15517         local cmd2="dir_count=2 layer=mdd stripe_count=1"
15518         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
15519         local cmd="$cmd1 $cmd2 $cmd3"
15520
15521         rm -f ${TMP}/mds_survey*
15522         echo + $cmd
15523         eval $cmd || error "mds-survey with stripe_count failed"
15524         cat ${TMP}/mds_survey*
15525         rm -f ${TMP}/mds_survey*
15526 }
15527 run_test 225b "Metadata survey sanity with stripe_count = 1"
15528
15529 mcreate_path2fid () {
15530         local mode=$1
15531         local major=$2
15532         local minor=$3
15533         local name=$4
15534         local desc=$5
15535         local path=$DIR/$tdir/$name
15536         local fid
15537         local rc
15538         local fid_path
15539
15540         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
15541                 error "cannot create $desc"
15542
15543         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
15544         rc=$?
15545         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
15546
15547         fid_path=$($LFS fid2path $MOUNT $fid)
15548         rc=$?
15549         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
15550
15551         [ "$path" == "$fid_path" ] ||
15552                 error "fid2path returned $fid_path, expected $path"
15553
15554         echo "pass with $path and $fid"
15555 }
15556
15557 test_226a () {
15558         rm -rf $DIR/$tdir
15559         mkdir -p $DIR/$tdir
15560
15561         mcreate_path2fid 0010666 0 0 fifo "FIFO"
15562         mcreate_path2fid 0020666 1 3 null "character special file (null)"
15563         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
15564         mcreate_path2fid 0040666 0 0 dir "directory"
15565         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
15566         mcreate_path2fid 0100666 0 0 file "regular file"
15567         mcreate_path2fid 0120666 0 0 link "symbolic link"
15568         mcreate_path2fid 0140666 0 0 sock "socket"
15569 }
15570 run_test 226a "call path2fid and fid2path on files of all type"
15571
15572 test_226b () {
15573         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15574
15575         local MDTIDX=1
15576
15577         rm -rf $DIR/$tdir
15578         mkdir -p $DIR/$tdir
15579         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
15580                 error "create remote directory failed"
15581         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
15582         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
15583                                 "character special file (null)"
15584         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
15585                                 "character special file (no device)"
15586         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
15587         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
15588                                 "block special file (loop)"
15589         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
15590         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
15591         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
15592 }
15593 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
15594
15595 # LU-1299 Executing or running ldd on a truncated executable does not
15596 # cause an out-of-memory condition.
15597 test_227() {
15598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15599         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
15600
15601         dd if=$(which date) of=$MOUNT/date bs=1k count=1
15602         chmod +x $MOUNT/date
15603
15604         $MOUNT/date > /dev/null
15605         ldd $MOUNT/date > /dev/null
15606         rm -f $MOUNT/date
15607 }
15608 run_test 227 "running truncated executable does not cause OOM"
15609
15610 # LU-1512 try to reuse idle OI blocks
15611 test_228a() {
15612         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15613         remote_mds_nodsh && skip "remote MDS with nodsh"
15614         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15615
15616         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15617         local myDIR=$DIR/$tdir
15618
15619         mkdir -p $myDIR
15620         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15621         $LCTL set_param fail_loc=0x80001002
15622         createmany -o $myDIR/t- 10000
15623         $LCTL set_param fail_loc=0
15624         # The guard is current the largest FID holder
15625         touch $myDIR/guard
15626         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15627                     tr -d '[')
15628         local IDX=$(($SEQ % 64))
15629
15630         do_facet $SINGLEMDS sync
15631         # Make sure journal flushed.
15632         sleep 6
15633         local blk1=$(do_facet $SINGLEMDS \
15634                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15635                      grep Blockcount | awk '{print $4}')
15636
15637         # Remove old files, some OI blocks will become idle.
15638         unlinkmany $myDIR/t- 10000
15639         # Create new files, idle OI blocks should be reused.
15640         createmany -o $myDIR/t- 2000
15641         do_facet $SINGLEMDS sync
15642         # Make sure journal flushed.
15643         sleep 6
15644         local blk2=$(do_facet $SINGLEMDS \
15645                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15646                      grep Blockcount | awk '{print $4}')
15647
15648         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15649 }
15650 run_test 228a "try to reuse idle OI blocks"
15651
15652 test_228b() {
15653         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15654         remote_mds_nodsh && skip "remote MDS with nodsh"
15655         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15656
15657         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15658         local myDIR=$DIR/$tdir
15659
15660         mkdir -p $myDIR
15661         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15662         $LCTL set_param fail_loc=0x80001002
15663         createmany -o $myDIR/t- 10000
15664         $LCTL set_param fail_loc=0
15665         # The guard is current the largest FID holder
15666         touch $myDIR/guard
15667         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15668                     tr -d '[')
15669         local IDX=$(($SEQ % 64))
15670
15671         do_facet $SINGLEMDS sync
15672         # Make sure journal flushed.
15673         sleep 6
15674         local blk1=$(do_facet $SINGLEMDS \
15675                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15676                      grep Blockcount | awk '{print $4}')
15677
15678         # Remove old files, some OI blocks will become idle.
15679         unlinkmany $myDIR/t- 10000
15680
15681         # stop the MDT
15682         stop $SINGLEMDS || error "Fail to stop MDT."
15683         # remount the MDT
15684         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
15685
15686         df $MOUNT || error "Fail to df."
15687         # Create new files, idle OI blocks should be reused.
15688         createmany -o $myDIR/t- 2000
15689         do_facet $SINGLEMDS sync
15690         # Make sure journal flushed.
15691         sleep 6
15692         local blk2=$(do_facet $SINGLEMDS \
15693                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15694                      grep Blockcount | awk '{print $4}')
15695
15696         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15697 }
15698 run_test 228b "idle OI blocks can be reused after MDT restart"
15699
15700 #LU-1881
15701 test_228c() {
15702         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15703         remote_mds_nodsh && skip "remote MDS with nodsh"
15704         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
15705
15706         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
15707         local myDIR=$DIR/$tdir
15708
15709         mkdir -p $myDIR
15710         #define OBD_FAIL_SEQ_EXHAUST             0x1002
15711         $LCTL set_param fail_loc=0x80001002
15712         # 20000 files can guarantee there are index nodes in the OI file
15713         createmany -o $myDIR/t- 20000
15714         $LCTL set_param fail_loc=0
15715         # The guard is current the largest FID holder
15716         touch $myDIR/guard
15717         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
15718                     tr -d '[')
15719         local IDX=$(($SEQ % 64))
15720
15721         do_facet $SINGLEMDS sync
15722         # Make sure journal flushed.
15723         sleep 6
15724         local blk1=$(do_facet $SINGLEMDS \
15725                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15726                      grep Blockcount | awk '{print $4}')
15727
15728         # Remove old files, some OI blocks will become idle.
15729         unlinkmany $myDIR/t- 20000
15730         rm -f $myDIR/guard
15731         # The OI file should become empty now
15732
15733         # Create new files, idle OI blocks should be reused.
15734         createmany -o $myDIR/t- 2000
15735         do_facet $SINGLEMDS sync
15736         # Make sure journal flushed.
15737         sleep 6
15738         local blk2=$(do_facet $SINGLEMDS \
15739                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
15740                      grep Blockcount | awk '{print $4}')
15741
15742         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
15743 }
15744 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
15745
15746 test_229() { # LU-2482, LU-3448
15747         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15748         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
15749         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
15750                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
15751
15752         rm -f $DIR/$tfile
15753
15754         # Create a file with a released layout and stripe count 2.
15755         $MULTIOP $DIR/$tfile H2c ||
15756                 error "failed to create file with released layout"
15757
15758         $GETSTRIPE -v $DIR/$tfile
15759
15760         local pattern=$($GETSTRIPE -L $DIR/$tfile)
15761         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
15762
15763         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
15764         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
15765         stat $DIR/$tfile || error "failed to stat released file"
15766
15767         chown $RUNAS_ID $DIR/$tfile ||
15768                 error "chown $RUNAS_ID $DIR/$tfile failed"
15769
15770         chgrp $RUNAS_ID $DIR/$tfile ||
15771                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
15772
15773         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
15774         rm $DIR/$tfile || error "failed to remove released file"
15775 }
15776 run_test 229 "getstripe/stat/rm/attr changes work on released files"
15777
15778 test_230a() {
15779         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15780         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15781         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15782                 skip "Need MDS version at least 2.11.52"
15783
15784         local MDTIDX=1
15785
15786         test_mkdir $DIR/$tdir
15787         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
15788         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
15789         [ $mdt_idx -ne 0 ] &&
15790                 error "create local directory on wrong MDT $mdt_idx"
15791
15792         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
15793                         error "create remote directory failed"
15794         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
15795         [ $mdt_idx -ne $MDTIDX ] &&
15796                 error "create remote directory on wrong MDT $mdt_idx"
15797
15798         createmany -o $DIR/$tdir/test_230/t- 10 ||
15799                 error "create files on remote directory failed"
15800         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
15801         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
15802         rm -r $DIR/$tdir || error "unlink remote directory failed"
15803 }
15804 run_test 230a "Create remote directory and files under the remote directory"
15805
15806 test_230b() {
15807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15808         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15809         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
15810                 skip "Need MDS version at least 2.11.52"
15811
15812         local MDTIDX=1
15813         local mdt_index
15814         local i
15815         local file
15816         local pid
15817         local stripe_count
15818         local migrate_dir=$DIR/$tdir/migrate_dir
15819         local other_dir=$DIR/$tdir/other_dir
15820
15821         test_mkdir $DIR/$tdir
15822         test_mkdir -i0 -c1 $migrate_dir
15823         test_mkdir -i0 -c1 $other_dir
15824         for ((i=0; i<10; i++)); do
15825                 mkdir -p $migrate_dir/dir_${i}
15826                 createmany -o $migrate_dir/dir_${i}/f 10 ||
15827                         error "create files under remote dir failed $i"
15828         done
15829
15830         cp /etc/passwd $migrate_dir/$tfile
15831         cp /etc/passwd $other_dir/$tfile
15832         chattr +SAD $migrate_dir
15833         chattr +SAD $migrate_dir/$tfile
15834
15835         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15836         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15837         local old_dir_mode=$(stat -c%f $migrate_dir)
15838         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
15839
15840         mkdir -p $migrate_dir/dir_default_stripe2
15841         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
15842         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
15843
15844         mkdir -p $other_dir
15845         ln $migrate_dir/$tfile $other_dir/luna
15846         ln $migrate_dir/$tfile $migrate_dir/sofia
15847         ln $other_dir/$tfile $migrate_dir/david
15848         ln -s $migrate_dir/$tfile $other_dir/zachary
15849         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
15850         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
15851
15852         local len
15853         local lnktgt
15854
15855         # inline symlink
15856         for len in 58 59 60; do
15857                 lnktgt=$(str_repeat 'l' $len)
15858                 touch $migrate_dir/$lnktgt
15859                 ln -s $lnktgt $migrate_dir/${len}char_ln
15860         done
15861
15862         # PATH_MAX
15863         for len in 4094 4095; do
15864                 lnktgt=$(str_repeat 'l' $len)
15865                 ln -s $lnktgt $migrate_dir/${len}char_ln
15866         done
15867
15868         # NAME_MAX
15869         for len in 254 255; do
15870                 touch $migrate_dir/$(str_repeat 'l' $len)
15871         done
15872
15873         $LFS migrate -m $MDTIDX $migrate_dir ||
15874                 error "fails on migrating remote dir to MDT1"
15875
15876         echo "migratate to MDT1, then checking.."
15877         for ((i = 0; i < 10; i++)); do
15878                 for file in $(find $migrate_dir/dir_${i}); do
15879                         mdt_index=$($LFS getstripe -m $file)
15880                         # broken symlink getstripe will fail
15881                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
15882                                 error "$file is not on MDT${MDTIDX}"
15883                 done
15884         done
15885
15886         # the multiple link file should still in MDT0
15887         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
15888         [ $mdt_index == 0 ] ||
15889                 error "$file is not on MDT${MDTIDX}"
15890
15891         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15892         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15893                 error " expect $old_dir_flag get $new_dir_flag"
15894
15895         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15896         [ "$old_file_flag" = "$new_file_flag" ] ||
15897                 error " expect $old_file_flag get $new_file_flag"
15898
15899         local new_dir_mode=$(stat -c%f $migrate_dir)
15900         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15901                 error "expect mode $old_dir_mode get $new_dir_mode"
15902
15903         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15904         [ "$old_file_mode" = "$new_file_mode" ] ||
15905                 error "expect mode $old_file_mode get $new_file_mode"
15906
15907         diff /etc/passwd $migrate_dir/$tfile ||
15908                 error "$tfile different after migration"
15909
15910         diff /etc/passwd $other_dir/luna ||
15911                 error "luna different after migration"
15912
15913         diff /etc/passwd $migrate_dir/sofia ||
15914                 error "sofia different after migration"
15915
15916         diff /etc/passwd $migrate_dir/david ||
15917                 error "david different after migration"
15918
15919         diff /etc/passwd $other_dir/zachary ||
15920                 error "zachary different after migration"
15921
15922         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15923                 error "${tfile}_ln different after migration"
15924
15925         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15926                 error "${tfile}_ln_other different after migration"
15927
15928         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
15929         [ $stripe_count = 2 ] ||
15930                 error "dir strpe_count $d != 2 after migration."
15931
15932         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
15933         [ $stripe_count = 2 ] ||
15934                 error "file strpe_count $d != 2 after migration."
15935
15936         #migrate back to MDT0
15937         MDTIDX=0
15938
15939         $LFS migrate -m $MDTIDX $migrate_dir ||
15940                 error "fails on migrating remote dir to MDT0"
15941
15942         echo "migrate back to MDT0, checking.."
15943         for file in $(find $migrate_dir); do
15944                 mdt_index=$($LFS getstripe -m $file)
15945                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
15946                         error "$file is not on MDT${MDTIDX}"
15947         done
15948
15949         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
15950         [ "$old_dir_flag" = "$new_dir_flag" ] ||
15951                 error " expect $old_dir_flag get $new_dir_flag"
15952
15953         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
15954         [ "$old_file_flag" = "$new_file_flag" ] ||
15955                 error " expect $old_file_flag get $new_file_flag"
15956
15957         local new_dir_mode=$(stat -c%f $migrate_dir)
15958         [ "$old_dir_mode" = "$new_dir_mode" ] ||
15959                 error "expect mode $old_dir_mode get $new_dir_mode"
15960
15961         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
15962         [ "$old_file_mode" = "$new_file_mode" ] ||
15963                 error "expect mode $old_file_mode get $new_file_mode"
15964
15965         diff /etc/passwd ${migrate_dir}/$tfile ||
15966                 error "$tfile different after migration"
15967
15968         diff /etc/passwd ${other_dir}/luna ||
15969                 error "luna different after migration"
15970
15971         diff /etc/passwd ${migrate_dir}/sofia ||
15972                 error "sofia different after migration"
15973
15974         diff /etc/passwd ${other_dir}/zachary ||
15975                 error "zachary different after migration"
15976
15977         diff /etc/passwd $migrate_dir/${tfile}_ln ||
15978                 error "${tfile}_ln different after migration"
15979
15980         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
15981                 error "${tfile}_ln_other different after migration"
15982
15983         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
15984         [ $stripe_count = 2 ] ||
15985                 error "dir strpe_count $d != 2 after migration."
15986
15987         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
15988         [ $stripe_count = 2 ] ||
15989                 error "file strpe_count $d != 2 after migration."
15990
15991         rm -rf $DIR/$tdir || error "rm dir failed after migration"
15992 }
15993 run_test 230b "migrate directory"
15994
15995 test_230c() {
15996         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15997         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15998         remote_mds_nodsh && skip "remote MDS with nodsh"
15999         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16000                 skip "Need MDS version at least 2.11.52"
16001
16002         local MDTIDX=1
16003         local total=3
16004         local mdt_index
16005         local file
16006         local migrate_dir=$DIR/$tdir/migrate_dir
16007
16008         #If migrating directory fails in the middle, all entries of
16009         #the directory is still accessiable.
16010         test_mkdir $DIR/$tdir
16011         test_mkdir -i0 -c1 $migrate_dir
16012         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
16013         stat $migrate_dir
16014         createmany -o $migrate_dir/f $total ||
16015                 error "create files under ${migrate_dir} failed"
16016
16017         # fail after migrating top dir, and this will fail only once, so the
16018         # first sub file migration will fail (currently f3), others succeed.
16019         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
16020         do_facet mds1 lctl set_param fail_loc=0x1801
16021         local t=$(ls $migrate_dir | wc -l)
16022         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
16023                 error "migrate should fail"
16024         local u=$(ls $migrate_dir | wc -l)
16025         [ "$u" == "$t" ] || error "$u != $t during migration"
16026
16027         # add new dir/file should succeed
16028         mkdir $migrate_dir/dir ||
16029                 error "mkdir failed under migrating directory"
16030         touch $migrate_dir/file ||
16031                 error "create file failed under migrating directory"
16032
16033         # add file with existing name should fail
16034         for file in $migrate_dir/f*; do
16035                 stat $file > /dev/null || error "stat $file failed"
16036                 $OPENFILE -f O_CREAT:O_EXCL $file &&
16037                         error "open(O_CREAT|O_EXCL) $file should fail"
16038                 $MULTIOP $file m && error "create $file should fail"
16039                 touch $DIR/$tdir/remote_dir/$tfile ||
16040                         error "touch $tfile failed"
16041                 ln $DIR/$tdir/remote_dir/$tfile $file &&
16042                         error "link $file should fail"
16043                 mdt_index=$($LFS getstripe -m $file)
16044                 if [ $mdt_index == 0 ]; then
16045                         # file failed to migrate is not allowed to rename to
16046                         mv $DIR/$tdir/remote_dir/$tfile $file &&
16047                                 error "rename to $file should fail"
16048                 else
16049                         mv $DIR/$tdir/remote_dir/$tfile $file ||
16050                                 error "rename to $file failed"
16051                 fi
16052                 echo hello >> $file || error "write $file failed"
16053         done
16054
16055         # resume migration with different options should fail
16056         $LFS migrate -m 0 $migrate_dir &&
16057                 error "migrate -m 0 $migrate_dir should fail"
16058
16059         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
16060                 error "migrate -c 2 $migrate_dir should fail"
16061
16062         # resume migration should succeed
16063         $LFS migrate -m $MDTIDX $migrate_dir ||
16064                 error "migrate $migrate_dir failed"
16065
16066         echo "Finish migration, then checking.."
16067         for file in $(find $migrate_dir); do
16068                 mdt_index=$($LFS getstripe -m $file)
16069                 [ $mdt_index == $MDTIDX ] ||
16070                         error "$file is not on MDT${MDTIDX}"
16071         done
16072
16073         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16074 }
16075 run_test 230c "check directory accessiblity if migration failed"
16076
16077 test_230d() {
16078         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16079         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16080         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16081                 skip "Need MDS version at least 2.11.52"
16082         # LU-11235
16083         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
16084
16085         local migrate_dir=$DIR/$tdir/migrate_dir
16086         local old_index
16087         local new_index
16088         local old_count
16089         local new_count
16090         local new_hash
16091         local mdt_index
16092         local i
16093         local j
16094
16095         old_index=$((RANDOM % MDSCOUNT))
16096         old_count=$((MDSCOUNT - old_index))
16097         new_index=$((RANDOM % MDSCOUNT))
16098         new_count=$((MDSCOUNT - new_index))
16099         new_hash="all_char"
16100
16101         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
16102         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
16103
16104         test_mkdir $DIR/$tdir
16105         test_mkdir -i $old_index -c $old_count $migrate_dir
16106
16107         for ((i=0; i<100; i++)); do
16108                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
16109                 createmany -o $migrate_dir/dir_${i}/f 100 ||
16110                         error "create files under remote dir failed $i"
16111         done
16112
16113         echo -n "Migrate from MDT$old_index "
16114         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
16115         echo -n "to MDT$new_index"
16116         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
16117         echo
16118
16119         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
16120         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
16121                 error "migrate remote dir error"
16122
16123         echo "Finish migration, then checking.."
16124         for file in $(find $migrate_dir); do
16125                 mdt_index=$($LFS getstripe -m $file)
16126                 if [ $mdt_index -lt $new_index ] ||
16127                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
16128                         error "$file is on MDT$mdt_index"
16129                 fi
16130         done
16131
16132         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16133 }
16134 run_test 230d "check migrate big directory"
16135
16136 test_230e() {
16137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16138         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16139         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16140                 skip "Need MDS version at least 2.11.52"
16141
16142         local i
16143         local j
16144         local a_fid
16145         local b_fid
16146
16147         mkdir -p $DIR/$tdir
16148         mkdir $DIR/$tdir/migrate_dir
16149         mkdir $DIR/$tdir/other_dir
16150         touch $DIR/$tdir/migrate_dir/a
16151         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
16152         ls $DIR/$tdir/other_dir
16153
16154         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16155                 error "migrate dir fails"
16156
16157         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16158         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16159
16160         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16161         [ $mdt_index == 0 ] || error "a is not on MDT0"
16162
16163         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
16164                 error "migrate dir fails"
16165
16166         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
16167         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
16168
16169         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16170         [ $mdt_index == 1 ] || error "a is not on MDT1"
16171
16172         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
16173         [ $mdt_index == 1 ] || error "b is not on MDT1"
16174
16175         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16176         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
16177
16178         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
16179
16180         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16181 }
16182 run_test 230e "migrate mulitple local link files"
16183
16184 test_230f() {
16185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16186         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16187         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16188                 skip "Need MDS version at least 2.11.52"
16189
16190         local a_fid
16191         local ln_fid
16192
16193         mkdir -p $DIR/$tdir
16194         mkdir $DIR/$tdir/migrate_dir
16195         $LFS mkdir -i1 $DIR/$tdir/other_dir
16196         touch $DIR/$tdir/migrate_dir/a
16197         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
16198         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
16199         ls $DIR/$tdir/other_dir
16200
16201         # a should be migrated to MDT1, since no other links on MDT0
16202         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16203                 error "#1 migrate dir fails"
16204         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
16205         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
16206         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16207         [ $mdt_index == 1 ] || error "a is not on MDT1"
16208
16209         # a should stay on MDT1, because it is a mulitple link file
16210         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16211                 error "#2 migrate dir fails"
16212         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16213         [ $mdt_index == 1 ] || error "a is not on MDT1"
16214
16215         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
16216                 error "#3 migrate dir fails"
16217
16218         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
16219         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
16220         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
16221
16222         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
16223         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
16224
16225         # a should be migrated to MDT0, since no other links on MDT1
16226         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
16227                 error "#4 migrate dir fails"
16228         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
16229         [ $mdt_index == 0 ] || error "a is not on MDT0"
16230
16231         rm -rf $DIR/$tdir || error "rm dir failed after migration"
16232 }
16233 run_test 230f "migrate mulitple remote link files"
16234
16235 test_230g() {
16236         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16237         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16238         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16239                 skip "Need MDS version at least 2.11.52"
16240
16241         mkdir -p $DIR/$tdir/migrate_dir
16242
16243         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
16244                 error "migrating dir to non-exist MDT succeeds"
16245         true
16246 }
16247 run_test 230g "migrate dir to non-exist MDT"
16248
16249 test_230h() {
16250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16251         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16252         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16253                 skip "Need MDS version at least 2.11.52"
16254
16255         local mdt_index
16256
16257         mkdir -p $DIR/$tdir/migrate_dir
16258
16259         $LFS migrate -m1 $DIR &&
16260                 error "migrating mountpoint1 should fail"
16261
16262         $LFS migrate -m1 $DIR/$tdir/.. &&
16263                 error "migrating mountpoint2 should fail"
16264
16265         # same as mv
16266         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
16267                 error "migrating $tdir/migrate_dir/.. should fail"
16268
16269         true
16270 }
16271 run_test 230h "migrate .. and root"
16272
16273 test_230i() {
16274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16275         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16276         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16277                 skip "Need MDS version at least 2.11.52"
16278
16279         mkdir -p $DIR/$tdir/migrate_dir
16280
16281         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
16282                 error "migration fails with a tailing slash"
16283
16284         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
16285                 error "migration fails with two tailing slashes"
16286 }
16287 run_test 230i "lfs migrate -m tolerates trailing slashes"
16288
16289 test_230j() {
16290         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16291         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
16292                 skip "Need MDS version at least 2.11.52"
16293
16294         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
16295         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
16296                 error "create $tfile failed"
16297         cat /etc/passwd > $DIR/$tdir/$tfile
16298
16299         $LFS migrate -m 1 $DIR/$tdir
16300
16301         cmp /etc/passwd $DIR/$tdir/$tfile ||
16302                 error "DoM file mismatch after migration"
16303 }
16304 run_test 230j "DoM file data not changed after dir migration"
16305
16306 test_230k() {
16307         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
16308         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16309                 skip "Need MDS version at least 2.11.56"
16310
16311         local total=20
16312         local files_on_starting_mdt=0
16313
16314         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
16315         $LFS getdirstripe $DIR/$tdir
16316         for i in $(seq $total); do
16317                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
16318                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16319                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16320         done
16321
16322         echo "$files_on_starting_mdt files on MDT0"
16323
16324         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
16325         $LFS getdirstripe $DIR/$tdir
16326
16327         files_on_starting_mdt=0
16328         for i in $(seq $total); do
16329                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16330                         error "file $tfile.$i mismatch after migration"
16331                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
16332                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16333         done
16334
16335         echo "$files_on_starting_mdt files on MDT1 after migration"
16336         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
16337
16338         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
16339         $LFS getdirstripe $DIR/$tdir
16340
16341         files_on_starting_mdt=0
16342         for i in $(seq $total); do
16343                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
16344                         error "file $tfile.$i mismatch after 2nd migration"
16345                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
16346                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
16347         done
16348
16349         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
16350         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
16351
16352         true
16353 }
16354 run_test 230k "file data not changed after dir migration"
16355
16356 test_230l() {
16357         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16358         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16359                 skip "Need MDS version at least 2.11.56"
16360
16361         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
16362         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
16363                 error "create files under remote dir failed $i"
16364         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
16365 }
16366 run_test 230l "readdir between MDTs won't crash"
16367
16368 test_230m() {
16369         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
16370         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
16371                 skip "Need MDS version at least 2.11.56"
16372
16373         local MDTIDX=1
16374         local mig_dir=$DIR/$tdir/migrate_dir
16375         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
16376         local shortstr="b"
16377         local val
16378
16379         echo "Creating files and dirs with xattrs"
16380         test_mkdir $DIR/$tdir
16381         test_mkdir -i0 -c1 $mig_dir
16382         mkdir $mig_dir/dir
16383         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
16384                 error "cannot set xattr attr1 on dir"
16385         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
16386                 error "cannot set xattr attr2 on dir"
16387         touch $mig_dir/dir/f0
16388         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
16389                 error "cannot set xattr attr1 on file"
16390         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
16391                 error "cannot set xattr attr2 on file"
16392         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16393         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16394         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
16395         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16396         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
16397         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16398         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
16399         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16400         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
16401
16402         echo "Migrating to MDT1"
16403         $LFS migrate -m $MDTIDX $mig_dir ||
16404                 error "fails on migrating dir to MDT1"
16405
16406         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
16407         echo "Checking xattrs"
16408         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
16409         [ "$val" = $longstr ] ||
16410                 error "expecting xattr1 $longstr on dir, found $val"
16411         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
16412         [ "$val" = $shortstr ] ||
16413                 error "expecting xattr2 $shortstr on dir, found $val"
16414         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
16415         [ "$val" = $longstr ] ||
16416                 error "expecting xattr1 $longstr on file, found $val"
16417         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
16418         [ "$val" = $shortstr ] ||
16419                 error "expecting xattr2 $shortstr on file, found $val"
16420 }
16421 run_test 230m "xattrs not changed after dir migration"
16422
16423 test_231a()
16424 {
16425         # For simplicity this test assumes that max_pages_per_rpc
16426         # is the same across all OSCs
16427         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
16428         local bulk_size=$((max_pages * PAGE_SIZE))
16429         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
16430                                        head -n 1)
16431
16432         mkdir -p $DIR/$tdir
16433         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
16434                 error "failed to set stripe with -S ${brw_size}M option"
16435
16436         # clear the OSC stats
16437         $LCTL set_param osc.*.stats=0 &>/dev/null
16438         stop_writeback
16439
16440         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
16441         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
16442                 oflag=direct &>/dev/null || error "dd failed"
16443
16444         sync; sleep 1; sync # just to be safe
16445         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
16446         if [ x$nrpcs != "x1" ]; then
16447                 $LCTL get_param osc.*.stats
16448                 error "found $nrpcs ost_write RPCs, not 1 as expected"
16449         fi
16450
16451         start_writeback
16452         # Drop the OSC cache, otherwise we will read from it
16453         cancel_lru_locks osc
16454
16455         # clear the OSC stats
16456         $LCTL set_param osc.*.stats=0 &>/dev/null
16457
16458         # Client reads $bulk_size.
16459         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
16460                 iflag=direct &>/dev/null || error "dd failed"
16461
16462         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
16463         if [ x$nrpcs != "x1" ]; then
16464                 $LCTL get_param osc.*.stats
16465                 error "found $nrpcs ost_read RPCs, not 1 as expected"
16466         fi
16467 }
16468 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
16469
16470 test_231b() {
16471         mkdir -p $DIR/$tdir
16472         local i
16473         for i in {0..1023}; do
16474                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
16475                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
16476                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
16477         done
16478         sync
16479 }
16480 run_test 231b "must not assert on fully utilized OST request buffer"
16481
16482 test_232a() {
16483         mkdir -p $DIR/$tdir
16484         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16485
16486         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16487         do_facet ost1 $LCTL set_param fail_loc=0x31c
16488
16489         # ignore dd failure
16490         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
16491
16492         do_facet ost1 $LCTL set_param fail_loc=0
16493         umount_client $MOUNT || error "umount failed"
16494         mount_client $MOUNT || error "mount failed"
16495         stop ost1 || error "cannot stop ost1"
16496         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16497 }
16498 run_test 232a "failed lock should not block umount"
16499
16500 test_232b() {
16501         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
16502                 skip "Need MDS version at least 2.10.58"
16503
16504         mkdir -p $DIR/$tdir
16505         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
16506         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
16507         sync
16508         cancel_lru_locks osc
16509
16510         #define OBD_FAIL_LDLM_OST_LVB            0x31c
16511         do_facet ost1 $LCTL set_param fail_loc=0x31c
16512
16513         # ignore failure
16514         $LFS data_version $DIR/$tdir/$tfile || true
16515
16516         do_facet ost1 $LCTL set_param fail_loc=0
16517         umount_client $MOUNT || error "umount failed"
16518         mount_client $MOUNT || error "mount failed"
16519         stop ost1 || error "cannot stop ost1"
16520         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
16521 }
16522 run_test 232b "failed data version lock should not block umount"
16523
16524 test_233a() {
16525         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
16526                 skip "Need MDS version at least 2.3.64"
16527         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16528
16529         local fid=$($LFS path2fid $MOUNT)
16530
16531         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16532                 error "cannot access $MOUNT using its FID '$fid'"
16533 }
16534 run_test 233a "checking that OBF of the FS root succeeds"
16535
16536 test_233b() {
16537         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
16538                 skip "Need MDS version at least 2.5.90"
16539         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
16540
16541         local fid=$($LFS path2fid $MOUNT/.lustre)
16542
16543         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16544                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
16545
16546         fid=$($LFS path2fid $MOUNT/.lustre/fid)
16547         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
16548                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
16549 }
16550 run_test 233b "checking that OBF of the FS .lustre succeeds"
16551
16552 test_234() {
16553         local p="$TMP/sanityN-$TESTNAME.parameters"
16554         save_lustre_params client "llite.*.xattr_cache" > $p
16555         lctl set_param llite.*.xattr_cache 1 ||
16556                 skip_env "xattr cache is not supported"
16557
16558         mkdir -p $DIR/$tdir || error "mkdir failed"
16559         touch $DIR/$tdir/$tfile || error "touch failed"
16560         # OBD_FAIL_LLITE_XATTR_ENOMEM
16561         $LCTL set_param fail_loc=0x1405
16562         getfattr -n user.attr $DIR/$tdir/$tfile &&
16563                 error "getfattr should have failed with ENOMEM"
16564         $LCTL set_param fail_loc=0x0
16565         rm -rf $DIR/$tdir
16566
16567         restore_lustre_params < $p
16568         rm -f $p
16569 }
16570 run_test 234 "xattr cache should not crash on ENOMEM"
16571
16572 test_235() {
16573         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
16574                 skip "Need MDS version at least 2.4.52"
16575
16576         flock_deadlock $DIR/$tfile
16577         local RC=$?
16578         case $RC in
16579                 0)
16580                 ;;
16581                 124) error "process hangs on a deadlock"
16582                 ;;
16583                 *) error "error executing flock_deadlock $DIR/$tfile"
16584                 ;;
16585         esac
16586 }
16587 run_test 235 "LU-1715: flock deadlock detection does not work properly"
16588
16589 #LU-2935
16590 test_236() {
16591         check_swap_layouts_support
16592
16593         local ref1=/etc/passwd
16594         local ref2=/etc/group
16595         local file1=$DIR/$tdir/f1
16596         local file2=$DIR/$tdir/f2
16597
16598         test_mkdir -c1 $DIR/$tdir
16599         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
16600         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
16601         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
16602         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
16603         local fd=$(free_fd)
16604         local cmd="exec $fd<>$file2"
16605         eval $cmd
16606         rm $file2
16607         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
16608                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
16609         cmd="exec $fd>&-"
16610         eval $cmd
16611         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
16612
16613         #cleanup
16614         rm -rf $DIR/$tdir
16615 }
16616 run_test 236 "Layout swap on open unlinked file"
16617
16618 # LU-4659 linkea consistency
16619 test_238() {
16620         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
16621                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
16622                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
16623                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
16624
16625         touch $DIR/$tfile
16626         ln $DIR/$tfile $DIR/$tfile.lnk
16627         touch $DIR/$tfile.new
16628         mv $DIR/$tfile.new $DIR/$tfile
16629         local fid1=$($LFS path2fid $DIR/$tfile)
16630         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
16631         local path1=$($LFS fid2path $FSNAME "$fid1")
16632         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
16633         local path2=$($LFS fid2path $FSNAME "$fid2")
16634         [ $tfile.lnk == $path2 ] ||
16635                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
16636         rm -f $DIR/$tfile*
16637 }
16638 run_test 238 "Verify linkea consistency"
16639
16640 test_239A() { # was test_239
16641         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
16642                 skip "Need MDS version at least 2.5.60"
16643
16644         local list=$(comma_list $(mdts_nodes))
16645
16646         mkdir -p $DIR/$tdir
16647         createmany -o $DIR/$tdir/f- 5000
16648         unlinkmany $DIR/$tdir/f- 5000
16649         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
16650                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
16651         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
16652                         osp.*MDT*.sync_in_flight" | calc_sum)
16653         [ "$changes" -eq 0 ] || error "$changes not synced"
16654 }
16655 run_test 239A "osp_sync test"
16656
16657 test_239a() { #LU-5297
16658         remote_mds_nodsh && skip "remote MDS with nodsh"
16659
16660         touch $DIR/$tfile
16661         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
16662         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
16663         chgrp $RUNAS_GID $DIR/$tfile
16664         wait_delete_completed
16665 }
16666 run_test 239a "process invalid osp sync record correctly"
16667
16668 test_239b() { #LU-5297
16669         remote_mds_nodsh && skip "remote MDS with nodsh"
16670
16671         touch $DIR/$tfile1
16672         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
16673         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
16674         chgrp $RUNAS_GID $DIR/$tfile1
16675         wait_delete_completed
16676         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
16677         touch $DIR/$tfile2
16678         chgrp $RUNAS_GID $DIR/$tfile2
16679         wait_delete_completed
16680 }
16681 run_test 239b "process osp sync record with ENOMEM error correctly"
16682
16683 test_240() {
16684         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16685         remote_mds_nodsh && skip "remote MDS with nodsh"
16686
16687         mkdir -p $DIR/$tdir
16688
16689         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
16690                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
16691         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
16692                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
16693
16694         umount_client $MOUNT || error "umount failed"
16695         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
16696         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
16697         mount_client $MOUNT || error "failed to mount client"
16698
16699         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
16700         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
16701 }
16702 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
16703
16704 test_241_bio() {
16705         local count=$1
16706         local bsize=$2
16707
16708         for LOOP in $(seq $count); do
16709                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
16710                 cancel_lru_locks $OSC || true
16711         done
16712 }
16713
16714 test_241_dio() {
16715         local count=$1
16716         local bsize=$2
16717
16718         for LOOP in $(seq $1); do
16719                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
16720                         2>/dev/null
16721         done
16722 }
16723
16724 test_241a() { # was test_241
16725         local bsize=$PAGE_SIZE
16726
16727         (( bsize < 40960 )) && bsize=40960
16728         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
16729         ls -la $DIR/$tfile
16730         cancel_lru_locks $OSC
16731         test_241_bio 1000 $bsize &
16732         PID=$!
16733         test_241_dio 1000 $bsize
16734         wait $PID
16735 }
16736 run_test 241a "bio vs dio"
16737
16738 test_241b() {
16739         local bsize=$PAGE_SIZE
16740
16741         (( bsize < 40960 )) && bsize=40960
16742         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
16743         ls -la $DIR/$tfile
16744         test_241_dio 1000 $bsize &
16745         PID=$!
16746         test_241_dio 1000 $bsize
16747         wait $PID
16748 }
16749 run_test 241b "dio vs dio"
16750
16751 test_242() {
16752         remote_mds_nodsh && skip "remote MDS with nodsh"
16753
16754         mkdir -p $DIR/$tdir
16755         touch $DIR/$tdir/$tfile
16756
16757         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
16758         do_facet mds1 lctl set_param fail_loc=0x105
16759         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
16760
16761         do_facet mds1 lctl set_param fail_loc=0
16762         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
16763 }
16764 run_test 242 "mdt_readpage failure should not cause directory unreadable"
16765
16766 test_243()
16767 {
16768         test_mkdir $DIR/$tdir
16769         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
16770 }
16771 run_test 243 "various group lock tests"
16772
16773 test_244()
16774 {
16775         test_mkdir $DIR/$tdir
16776         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
16777         sendfile_grouplock $DIR/$tdir/$tfile || \
16778                 error "sendfile+grouplock failed"
16779         rm -rf $DIR/$tdir
16780 }
16781 run_test 244 "sendfile with group lock tests"
16782
16783 test_245() {
16784         local flagname="multi_mod_rpcs"
16785         local connect_data_name="max_mod_rpcs"
16786         local out
16787
16788         # check if multiple modify RPCs flag is set
16789         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
16790                 grep "connect_flags:")
16791         echo "$out"
16792
16793         echo "$out" | grep -qw $flagname
16794         if [ $? -ne 0 ]; then
16795                 echo "connect flag $flagname is not set"
16796                 return
16797         fi
16798
16799         # check if multiple modify RPCs data is set
16800         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
16801         echo "$out"
16802
16803         echo "$out" | grep -qw $connect_data_name ||
16804                 error "import should have connect data $connect_data_name"
16805 }
16806 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
16807
16808 test_246() { # LU-7371
16809         remote_ost_nodsh && skip "remote OST with nodsh"
16810         [ $OST1_VERSION -lt $(version_code 2.7.62) ] &&
16811                 skip "Need OST version >= 2.7.62"
16812
16813         do_facet ost1 $LCTL set_param fail_val=4095
16814 #define OBD_FAIL_OST_READ_SIZE          0x234
16815         do_facet ost1 $LCTL set_param fail_loc=0x234
16816         $LFS setstripe $DIR/$tfile -i 0 -c 1
16817         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
16818         cancel_lru_locks $FSNAME-OST0000
16819         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
16820 }
16821 run_test 246 "Read file of size 4095 should return right length"
16822
16823 cleanup_247() {
16824         local submount=$1
16825
16826         trap 0
16827         umount_client $submount
16828         rmdir $submount
16829 }
16830
16831 test_247a() {
16832         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16833                 grep -q subtree ||
16834                 skip_env "Fileset feature is not supported"
16835
16836         local submount=${MOUNT}_$tdir
16837
16838         mkdir $MOUNT/$tdir
16839         mkdir -p $submount || error "mkdir $submount failed"
16840         FILESET="$FILESET/$tdir" mount_client $submount ||
16841                 error "mount $submount failed"
16842         trap "cleanup_247 $submount" EXIT
16843         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
16844         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
16845                 error "read $MOUNT/$tdir/$tfile failed"
16846         cleanup_247 $submount
16847 }
16848 run_test 247a "mount subdir as fileset"
16849
16850 test_247b() {
16851         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16852                 skip_env "Fileset feature is not supported"
16853
16854         local submount=${MOUNT}_$tdir
16855
16856         rm -rf $MOUNT/$tdir
16857         mkdir -p $submount || error "mkdir $submount failed"
16858         SKIP_FILESET=1
16859         FILESET="$FILESET/$tdir" mount_client $submount &&
16860                 error "mount $submount should fail"
16861         rmdir $submount
16862 }
16863 run_test 247b "mount subdir that dose not exist"
16864
16865 test_247c() {
16866         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16867                 skip_env "Fileset feature is not supported"
16868
16869         local submount=${MOUNT}_$tdir
16870
16871         mkdir -p $MOUNT/$tdir/dir1
16872         mkdir -p $submount || error "mkdir $submount failed"
16873         trap "cleanup_247 $submount" EXIT
16874         FILESET="$FILESET/$tdir" mount_client $submount ||
16875                 error "mount $submount failed"
16876         local fid=$($LFS path2fid $MOUNT/)
16877         $LFS fid2path $submount $fid && error "fid2path should fail"
16878         cleanup_247 $submount
16879 }
16880 run_test 247c "running fid2path outside root"
16881
16882 test_247d() {
16883         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
16884                 skip "Fileset feature is not supported"
16885
16886         local submount=${MOUNT}_$tdir
16887
16888         mkdir -p $MOUNT/$tdir/dir1
16889         mkdir -p $submount || error "mkdir $submount failed"
16890         FILESET="$FILESET/$tdir" mount_client $submount ||
16891                 error "mount $submount failed"
16892         trap "cleanup_247 $submount" EXIT
16893         local fid=$($LFS path2fid $submount/dir1)
16894         $LFS fid2path $submount $fid || error "fid2path should succeed"
16895         cleanup_247 $submount
16896 }
16897 run_test 247d "running fid2path inside root"
16898
16899 # LU-8037
16900 test_247e() {
16901         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
16902                 grep -q subtree ||
16903                 skip "Fileset feature is not supported"
16904
16905         local submount=${MOUNT}_$tdir
16906
16907         mkdir $MOUNT/$tdir
16908         mkdir -p $submount || error "mkdir $submount failed"
16909         FILESET="$FILESET/.." mount_client $submount &&
16910                 error "mount $submount should fail"
16911         rmdir $submount
16912 }
16913 run_test 247e "mount .. as fileset"
16914
16915 test_248() {
16916         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
16917         [ -z "$fast_read_sav" ] && skip "no fast read support"
16918
16919         # create a large file for fast read verification
16920         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
16921
16922         # make sure the file is created correctly
16923         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
16924                 { rm -f $DIR/$tfile; skip "file creation error"; }
16925
16926         echo "Test 1: verify that fast read is 4 times faster on cache read"
16927
16928         # small read with fast read enabled
16929         $LCTL set_param -n llite.*.fast_read=1
16930         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16931                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16932                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16933         # small read with fast read disabled
16934         $LCTL set_param -n llite.*.fast_read=0
16935         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
16936                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16937                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16938
16939         # verify that fast read is 4 times faster for cache read
16940         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
16941                 error_not_in_vm "fast read was not 4 times faster: " \
16942                            "$t_fast vs $t_slow"
16943
16944         echo "Test 2: verify the performance between big and small read"
16945         $LCTL set_param -n llite.*.fast_read=1
16946
16947         # 1k non-cache read
16948         cancel_lru_locks osc
16949         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16950                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16951                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16952
16953         # 1M non-cache read
16954         cancel_lru_locks osc
16955         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
16956                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
16957                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
16958
16959         # verify that big IO is not 4 times faster than small IO
16960         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
16961                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
16962
16963         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
16964         rm -f $DIR/$tfile
16965 }
16966 run_test 248 "fast read verification"
16967
16968 test_249() { # LU-7890
16969         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
16970                 skip "Need at least version 2.8.54"
16971
16972         rm -f $DIR/$tfile
16973         $SETSTRIPE -c 1 $DIR/$tfile
16974         # Offset 2T == 4k * 512M
16975         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
16976                 error "dd to 2T offset failed"
16977 }
16978 run_test 249 "Write above 2T file size"
16979
16980 test_250() {
16981         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
16982          && skip "no 16TB file size limit on ZFS"
16983
16984         $SETSTRIPE -c 1 $DIR/$tfile
16985         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
16986         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
16987         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
16988         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
16989                 conv=notrunc,fsync && error "append succeeded"
16990         return 0
16991 }
16992 run_test 250 "Write above 16T limit"
16993
16994 test_251() {
16995         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
16996
16997         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
16998         #Skip once - writing the first stripe will succeed
16999         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17000         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
17001                 error "short write happened"
17002
17003         $LCTL set_param fail_loc=0xa0001407 fail_val=1
17004         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
17005                 error "short read happened"
17006
17007         rm -f $DIR/$tfile
17008 }
17009 run_test 251 "Handling short read and write correctly"
17010
17011 test_252() {
17012         remote_mds_nodsh && skip "remote MDS with nodsh"
17013         remote_ost_nodsh && skip "remote OST with nodsh"
17014         if [ "$ost1_FSTYPE" != "ldiskfs" -o "$mds1_FSTYPE" != "ldiskfs" ]; then
17015                 skip_env "ldiskfs only test"
17016         fi
17017
17018         local tgt
17019         local dev
17020         local out
17021         local uuid
17022         local num
17023         local gen
17024
17025         # check lr_reader on OST0000
17026         tgt=ost1
17027         dev=$(facet_device $tgt)
17028         out=$(do_facet $tgt $LR_READER $dev)
17029         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17030         echo "$out"
17031         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
17032         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
17033                 error "Invalid uuid returned by $LR_READER on target $tgt"
17034         echo -e "uuid returned by $LR_READER is '$uuid'\n"
17035
17036         # check lr_reader -c on MDT0000
17037         tgt=mds1
17038         dev=$(facet_device $tgt)
17039         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
17040                 skip "$LR_READER does not support additional options"
17041         fi
17042         out=$(do_facet $tgt $LR_READER -c $dev)
17043         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17044         echo "$out"
17045         num=$(echo "$out" | grep -c "mdtlov")
17046         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
17047                 error "Invalid number of mdtlov clients returned by $LR_READER"
17048         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
17049
17050         # check lr_reader -cr on MDT0000
17051         out=$(do_facet $tgt $LR_READER -cr $dev)
17052         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
17053         echo "$out"
17054         echo "$out" | grep -q "^reply_data:$" ||
17055                 error "$LR_READER should have returned 'reply_data' section"
17056         num=$(echo "$out" | grep -c "client_generation")
17057         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
17058 }
17059 run_test 252 "check lr_reader tool"
17060
17061 test_253_fill_ost() {
17062         local size_mb #how many MB should we write to pass watermark
17063         local lwm=$3  #low watermark
17064         local free_10mb #10% of free space
17065
17066         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17067         size_mb=$((free_kb / 1024 - lwm))
17068         free_10mb=$((free_kb / 10240))
17069         #If 10% of free space cross low watermark use it
17070         if (( free_10mb > size_mb )); then
17071                 size_mb=$free_10mb
17072         else
17073                 #At least we need to store 1.1 of difference between
17074                 #free space and low watermark
17075                 size_mb=$((size_mb + size_mb / 10))
17076         fi
17077         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
17078                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
17079                          oflag=append conv=notrunc
17080         fi
17081
17082         sleep_maxage
17083
17084         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
17085         echo "OST still has $((free_kb / 1024)) mbytes free"
17086 }
17087
17088 test_253() {
17089         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17090         remote_mds_nodsh && skip "remote MDS with nodsh"
17091         remote_mgs_nodsh && skip "remote MGS with nodsh"
17092
17093         local ostidx=0
17094         local rc=0
17095
17096         local ost_name=$($LFS osts |
17097                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
17098         # on the mdt's osc
17099         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
17100         do_facet $SINGLEMDS $LCTL get_param -n \
17101                 osp.$mdtosc_proc1.reserved_mb_high ||
17102                 skip  "remote MDS does not support reserved_mb_high"
17103
17104         rm -rf $DIR/$tdir
17105         wait_mds_ost_sync
17106         wait_delete_completed
17107         mkdir $DIR/$tdir
17108
17109         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
17110                         osp.$mdtosc_proc1.reserved_mb_high)
17111         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
17112                         osp.$mdtosc_proc1.reserved_mb_low)
17113         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
17114
17115         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
17116         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
17117                 error "Adding $ost_name to pool failed"
17118
17119         # Wait for client to see a OST at pool
17120         wait_update $HOSTNAME "$LCTL get_param -n
17121                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
17122                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
17123                 error "Client can not see the pool"
17124         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
17125                 error "Setstripe failed"
17126
17127         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
17128         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
17129         echo "OST still has $((blocks/1024)) mbytes free"
17130
17131         local new_lwm=$((blocks/1024-10))
17132         do_facet $SINGLEMDS $LCTL set_param \
17133                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
17134         do_facet $SINGLEMDS $LCTL set_param \
17135                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
17136
17137         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17138
17139         #First enospc could execute orphan deletion so repeat.
17140         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
17141
17142         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17143                         osp.$mdtosc_proc1.prealloc_status)
17144         echo "prealloc_status $oa_status"
17145
17146         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
17147                 error "File creation should fail"
17148         #object allocation was stopped, but we still able to append files
17149         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
17150                 error "Append failed"
17151         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
17152
17153         wait_delete_completed
17154
17155         sleep_maxage
17156
17157         for i in $(seq 10 12); do
17158                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
17159                         error "File creation failed after rm";
17160         done
17161
17162         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
17163                         osp.$mdtosc_proc1.prealloc_status)
17164         echo "prealloc_status $oa_status"
17165
17166         if (( oa_status != 0 )); then
17167                 error "Object allocation still disable after rm"
17168         fi
17169         do_facet $SINGLEMDS $LCTL set_param \
17170                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
17171         do_facet $SINGLEMDS $LCTL set_param \
17172                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
17173
17174
17175         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
17176                 error "Remove $ost_name from pool failed"
17177         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
17178                 error "Pool destroy fialed"
17179 }
17180 run_test 253 "Check object allocation limit"
17181
17182 test_254() {
17183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17184         remote_mds_nodsh && skip "remote MDS with nodsh"
17185         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
17186                 skip "MDS does not support changelog_size"
17187
17188         local cl_user
17189         local MDT0=$(facet_svc $SINGLEMDS)
17190
17191         changelog_register || error "changelog_register failed"
17192
17193         changelog_clear 0 || error "changelog_clear failed"
17194
17195         local size1=$(do_facet $SINGLEMDS \
17196                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17197         echo "Changelog size $size1"
17198
17199         rm -rf $DIR/$tdir
17200         $LFS mkdir -i 0 $DIR/$tdir
17201         # change something
17202         mkdir -p $DIR/$tdir/pics/2008/zachy
17203         touch $DIR/$tdir/pics/2008/zachy/timestamp
17204         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
17205         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
17206         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
17207         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
17208         rm $DIR/$tdir/pics/desktop.jpg
17209
17210         local size2=$(do_facet $SINGLEMDS \
17211                       $LCTL get_param -n mdd.$MDT0.changelog_size)
17212         echo "Changelog size after work $size2"
17213
17214         (( $size2 > $size1 )) ||
17215                 error "new Changelog size=$size2 less than old size=$size1"
17216 }
17217 run_test 254 "Check changelog size"
17218
17219 ladvise_no_type()
17220 {
17221         local type=$1
17222         local file=$2
17223
17224         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
17225                 awk -F: '{print $2}' | grep $type > /dev/null
17226         if [ $? -ne 0 ]; then
17227                 return 0
17228         fi
17229         return 1
17230 }
17231
17232 ladvise_no_ioctl()
17233 {
17234         local file=$1
17235
17236         lfs ladvise -a willread $file > /dev/null 2>&1
17237         if [ $? -eq 0 ]; then
17238                 return 1
17239         fi
17240
17241         lfs ladvise -a willread $file 2>&1 |
17242                 grep "Inappropriate ioctl for device" > /dev/null
17243         if [ $? -eq 0 ]; then
17244                 return 0
17245         fi
17246         return 1
17247 }
17248
17249 percent() {
17250         bc <<<"scale=2; ($1 - $2) * 100 / $2"
17251 }
17252
17253 # run a random read IO workload
17254 # usage: random_read_iops <filename> <filesize> <iosize>
17255 random_read_iops() {
17256         local file=$1
17257         local fsize=$2
17258         local iosize=${3:-4096}
17259
17260         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
17261                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
17262 }
17263
17264 drop_file_oss_cache() {
17265         local file="$1"
17266         local nodes="$2"
17267
17268         $LFS ladvise -a dontneed $file 2>/dev/null ||
17269                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
17270 }
17271
17272 ladvise_willread_performance()
17273 {
17274         local repeat=10
17275         local average_origin=0
17276         local average_cache=0
17277         local average_ladvise=0
17278
17279         for ((i = 1; i <= $repeat; i++)); do
17280                 echo "Iter $i/$repeat: reading without willread hint"
17281                 cancel_lru_locks osc
17282                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17283                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
17284                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
17285                 average_origin=$(bc <<<"$average_origin + $speed_origin")
17286
17287                 cancel_lru_locks osc
17288                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
17289                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
17290                 average_cache=$(bc <<<"$average_cache + $speed_cache")
17291
17292                 cancel_lru_locks osc
17293                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
17294                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
17295                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
17296                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
17297                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
17298         done
17299         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
17300         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
17301         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
17302
17303         speedup_cache=$(percent $average_cache $average_origin)
17304         speedup_ladvise=$(percent $average_ladvise $average_origin)
17305
17306         echo "Average uncached read: $average_origin"
17307         echo "Average speedup with OSS cached read: " \
17308                 "$average_cache = +$speedup_cache%"
17309         echo "Average speedup with ladvise willread: " \
17310                 "$average_ladvise = +$speedup_ladvise%"
17311
17312         local lowest_speedup=20
17313         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
17314                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
17315                         "got $average_cache%. Skipping ladvise willread check."
17316                 return 0
17317         fi
17318
17319         # the test won't work on ZFS until it supports 'ladvise dontneed', but
17320         # it is still good to run until then to exercise 'ladvise willread'
17321         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17322                 [ "$ost1_FSTYPE" = "zfs" ] &&
17323                 echo "osd-zfs does not support dontneed or drop_caches" &&
17324                 return 0
17325
17326         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
17327         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
17328                 error_not_in_vm "Speedup with willread is less than " \
17329                         "$lowest_speedup%, got $average_ladvise%"
17330 }
17331
17332 test_255a() {
17333         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17334                 skip "lustre < 2.8.54 does not support ladvise "
17335         remote_ost_nodsh && skip "remote OST with nodsh"
17336
17337         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
17338
17339         ladvise_no_type willread $DIR/$tfile &&
17340                 skip "willread ladvise is not supported"
17341
17342         ladvise_no_ioctl $DIR/$tfile &&
17343                 skip "ladvise ioctl is not supported"
17344
17345         local size_mb=100
17346         local size=$((size_mb * 1048576))
17347         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17348                 error "dd to $DIR/$tfile failed"
17349
17350         lfs ladvise -a willread $DIR/$tfile ||
17351                 error "Ladvise failed with no range argument"
17352
17353         lfs ladvise -a willread -s 0 $DIR/$tfile ||
17354                 error "Ladvise failed with no -l or -e argument"
17355
17356         lfs ladvise -a willread -e 1 $DIR/$tfile ||
17357                 error "Ladvise failed with only -e argument"
17358
17359         lfs ladvise -a willread -l 1 $DIR/$tfile ||
17360                 error "Ladvise failed with only -l argument"
17361
17362         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
17363                 error "End offset should not be smaller than start offset"
17364
17365         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
17366                 error "End offset should not be equal to start offset"
17367
17368         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
17369                 error "Ladvise failed with overflowing -s argument"
17370
17371         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
17372                 error "Ladvise failed with overflowing -e argument"
17373
17374         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
17375                 error "Ladvise failed with overflowing -l argument"
17376
17377         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
17378                 error "Ladvise succeeded with conflicting -l and -e arguments"
17379
17380         echo "Synchronous ladvise should wait"
17381         local delay=4
17382 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
17383         do_nodes $(comma_list $(osts_nodes)) \
17384                 $LCTL set_param fail_val=$delay fail_loc=0x237
17385
17386         local start_ts=$SECONDS
17387         lfs ladvise -a willread $DIR/$tfile ||
17388                 error "Ladvise failed with no range argument"
17389         local end_ts=$SECONDS
17390         local inteval_ts=$((end_ts - start_ts))
17391
17392         if [ $inteval_ts -lt $(($delay - 1)) ]; then
17393                 error "Synchronous advice didn't wait reply"
17394         fi
17395
17396         echo "Asynchronous ladvise shouldn't wait"
17397         local start_ts=$SECONDS
17398         lfs ladvise -a willread -b $DIR/$tfile ||
17399                 error "Ladvise failed with no range argument"
17400         local end_ts=$SECONDS
17401         local inteval_ts=$((end_ts - start_ts))
17402
17403         if [ $inteval_ts -gt $(($delay / 2)) ]; then
17404                 error "Asynchronous advice blocked"
17405         fi
17406
17407         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
17408         ladvise_willread_performance
17409 }
17410 run_test 255a "check 'lfs ladvise -a willread'"
17411
17412 facet_meminfo() {
17413         local facet=$1
17414         local info=$2
17415
17416         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
17417 }
17418
17419 test_255b() {
17420         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
17421                 skip "lustre < 2.8.54 does not support ladvise "
17422         remote_ost_nodsh && skip "remote OST with nodsh"
17423
17424         lfs setstripe -c 1 -i 0 $DIR/$tfile
17425
17426         ladvise_no_type dontneed $DIR/$tfile &&
17427                 skip "dontneed ladvise is not supported"
17428
17429         ladvise_no_ioctl $DIR/$tfile &&
17430                 skip "ladvise ioctl is not supported"
17431
17432         ! $LFS ladvise -a dontneed $DIR/$tfile &&
17433                 [ "$ost1_FSTYPE" = "zfs" ] &&
17434                 skip "zfs-osd does not support 'ladvise dontneed'"
17435
17436         local size_mb=100
17437         local size=$((size_mb * 1048576))
17438         # In order to prevent disturbance of other processes, only check 3/4
17439         # of the memory usage
17440         local kibibytes=$((size_mb * 1024 * 3 / 4))
17441
17442         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
17443                 error "dd to $DIR/$tfile failed"
17444
17445         #force write to complete before dropping OST cache & checking memory
17446         sync
17447
17448         local total=$(facet_meminfo ost1 MemTotal)
17449         echo "Total memory: $total KiB"
17450
17451         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
17452         local before_read=$(facet_meminfo ost1 Cached)
17453         echo "Cache used before read: $before_read KiB"
17454
17455         lfs ladvise -a willread $DIR/$tfile ||
17456                 error "Ladvise willread failed"
17457         local after_read=$(facet_meminfo ost1 Cached)
17458         echo "Cache used after read: $after_read KiB"
17459
17460         lfs ladvise -a dontneed $DIR/$tfile ||
17461                 error "Ladvise dontneed again failed"
17462         local no_read=$(facet_meminfo ost1 Cached)
17463         echo "Cache used after dontneed ladvise: $no_read KiB"
17464
17465         if [ $total -lt $((before_read + kibibytes)) ]; then
17466                 echo "Memory is too small, abort checking"
17467                 return 0
17468         fi
17469
17470         if [ $((before_read + kibibytes)) -gt $after_read ]; then
17471                 error "Ladvise willread should use more memory" \
17472                         "than $kibibytes KiB"
17473         fi
17474
17475         if [ $((no_read + kibibytes)) -gt $after_read ]; then
17476                 error "Ladvise dontneed should release more memory" \
17477                         "than $kibibytes KiB"
17478         fi
17479 }
17480 run_test 255b "check 'lfs ladvise -a dontneed'"
17481
17482 test_255c() {
17483         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
17484                 skip "lustre < 2.10.50 does not support lockahead"
17485
17486         local count
17487         local new_count
17488         local difference
17489         local i
17490         local rc
17491
17492         test_mkdir -p $DIR/$tdir
17493         $SETSTRIPE -i 0 $DIR/$tdir
17494
17495         #test 10 returns only success/failure
17496         i=10
17497         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17498         rc=$?
17499         if [ $rc -eq 255 ]; then
17500                 error "Ladvise test${i} failed, ${rc}"
17501         fi
17502
17503         #test 11 counts lock enqueue requests, all others count new locks
17504         i=11
17505         count=$(do_facet ost1 \
17506                 $LCTL get_param -n ost.OSS.ost.stats)
17507         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
17508
17509         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17510         rc=$?
17511         if [ $rc -eq 255 ]; then
17512                 error "Ladvise test${i} failed, ${rc}"
17513         fi
17514
17515         new_count=$(do_facet ost1 \
17516                 $LCTL get_param -n ost.OSS.ost.stats)
17517         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
17518                    awk '{ print $2 }')
17519
17520         difference="$((new_count - count))"
17521         if [ $difference -ne $rc ]; then
17522                 error "Ladvise test${i}, bad enqueue count, returned " \
17523                       "${rc}, actual ${difference}"
17524         fi
17525
17526         for i in $(seq 12 21); do
17527                 # If we do not do this, we run the risk of having too many
17528                 # locks and starting lock cancellation while we are checking
17529                 # lock counts.
17530                 cancel_lru_locks osc
17531
17532                 count=$($LCTL get_param -n \
17533                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17534
17535                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
17536                 rc=$?
17537                 if [ $rc -eq 255 ]; then
17538                         error "Ladvise test ${i} failed, ${rc}"
17539                 fi
17540
17541                 new_count=$($LCTL get_param -n \
17542                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
17543                 difference="$((new_count - count))"
17544
17545                 # Test 15 output is divided by 100 to map down to valid return
17546                 if [ $i -eq 15 ]; then
17547                         rc="$((rc * 100))"
17548                 fi
17549
17550                 if [ $difference -ne $rc ]; then
17551                         error "Ladvise test ${i}, bad lock count, returned " \
17552                               "${rc}, actual ${difference}"
17553                 fi
17554         done
17555
17556         #test 22 returns only success/failure
17557         i=22
17558         lockahead_test -d $DIR/$tdir -t $i -f $tfile
17559         rc=$?
17560         if [ $rc -eq 255 ]; then
17561                 error "Ladvise test${i} failed, ${rc}"
17562         fi
17563 }
17564 run_test 255c "suite of ladvise lockahead tests"
17565
17566 test_256() {
17567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17568         remote_mds_nodsh && skip "remote MDS with nodsh"
17569         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
17570         changelog_users $SINGLEMDS | grep "^cl" &&
17571                 skip "active changelog user"
17572
17573         local cl_user
17574         local cat_sl
17575         local mdt_dev
17576
17577         mdt_dev=$(mdsdevname 1)
17578         echo $mdt_dev
17579
17580         changelog_register || error "changelog_register failed"
17581
17582         rm -rf $DIR/$tdir
17583         mkdir -p $DIR/$tdir
17584
17585         changelog_clear 0 || error "changelog_clear failed"
17586
17587         # change something
17588         touch $DIR/$tdir/{1..10}
17589
17590         # stop the MDT
17591         stop $SINGLEMDS || error "Fail to stop MDT"
17592
17593         # remount the MDT
17594
17595         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
17596
17597         #after mount new plainllog is used
17598         touch $DIR/$tdir/{11..19}
17599         local tmpfile=$(mktemp -u $tfile.XXXXXX)
17600         cat_sl=$(do_facet $SINGLEMDS "sync; \
17601                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
17602                  llog_reader $tmpfile | grep -c type=1064553b")
17603         do_facet $SINGLEMDS llog_reader $tmpfile
17604
17605         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
17606
17607         changelog_clear 0 || error "changelog_clear failed"
17608
17609         cat_sl=$(do_facet $SINGLEMDS "sync; \
17610                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
17611                  llog_reader $tmpfile | grep -c type=1064553b; rm -f $tmpfile")
17612
17613         if (( cat_sl == 2 )); then
17614                 error "Empty plain llog was not deleted from changelog catalog"
17615         elif (( cat_sl != 1 )); then
17616                 error "Active plain llog shouldn't be deleted from catalog"
17617         fi
17618 }
17619 run_test 256 "Check llog delete for empty and not full state"
17620
17621 test_257() {
17622         remote_mds_nodsh && skip "remote MDS with nodsh"
17623         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
17624                 skip "Need MDS version at least 2.8.55"
17625
17626         test_mkdir $DIR/$tdir
17627
17628         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
17629                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
17630         stat $DIR/$tdir
17631
17632 #define OBD_FAIL_MDS_XATTR_REP                  0x161
17633         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
17634         local facet=mds$((mdtidx + 1))
17635         set_nodes_failloc $(facet_active_host $facet) 0x80000161
17636         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
17637
17638         stop $facet || error "stop MDS failed"
17639         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
17640                 error "start MDS fail"
17641 }
17642 run_test 257 "xattr locks are not lost"
17643
17644 # Verify we take the i_mutex when security requires it
17645 test_258a() {
17646 #define OBD_FAIL_IMUTEX_SEC 0x141c
17647         $LCTL set_param fail_loc=0x141c
17648         touch $DIR/$tfile
17649         chmod u+s $DIR/$tfile
17650         chmod a+rwx $DIR/$tfile
17651         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
17652         RC=$?
17653         if [ $RC -ne 0 ]; then
17654                 error "error, failed to take i_mutex, rc=$?"
17655         fi
17656         rm -f $DIR/$tfile
17657 }
17658 run_test 258a "verify i_mutex security behavior when suid attributes is set"
17659
17660 # Verify we do NOT take the i_mutex in the normal case
17661 test_258b() {
17662 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
17663         $LCTL set_param fail_loc=0x141d
17664         touch $DIR/$tfile
17665         chmod a+rwx $DIR
17666         chmod a+rw $DIR/$tfile
17667         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
17668         RC=$?
17669         if [ $RC -ne 0 ]; then
17670                 error "error, took i_mutex unnecessarily, rc=$?"
17671         fi
17672         rm -f $DIR/$tfile
17673
17674 }
17675 run_test 258b "verify i_mutex security behavior"
17676
17677 test_259() {
17678         local file=$DIR/$tfile
17679         local before
17680         local after
17681
17682         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
17683
17684         stack_trap "rm -f $file" EXIT
17685
17686         wait_delete_completed
17687         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17688         echo "before: $before"
17689
17690         $LFS setstripe -i 0 -c 1 $file
17691         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
17692         sync_all_data
17693         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17694         echo "after write: $after"
17695
17696 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
17697         do_facet ost1 $LCTL set_param fail_loc=0x2301
17698         $TRUNCATE $file 0
17699         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17700         echo "after truncate: $after"
17701
17702         stop ost1
17703         do_facet ost1 $LCTL set_param fail_loc=0
17704         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17705         sleep 2
17706         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
17707         echo "after restart: $after"
17708         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
17709                 error "missing truncate?"
17710
17711         return 0
17712 }
17713 run_test 259 "crash at delayed truncate"
17714
17715 test_260() {
17716 #define OBD_FAIL_MDC_CLOSE               0x806
17717         $LCTL set_param fail_loc=0x80000806
17718         touch $DIR/$tfile
17719
17720 }
17721 run_test 260 "Check mdc_close fail"
17722
17723 ### Data-on-MDT sanity tests ###
17724 test_270a() {
17725         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17726                 skip "Need MDS version at least 2.10.55 for DoM"
17727
17728         # create DoM file
17729         local dom=$DIR/$tdir/dom_file
17730         local tmp=$DIR/$tdir/tmp_file
17731
17732         mkdir -p $DIR/$tdir
17733
17734         # basic checks for DoM component creation
17735         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
17736                 error "Can set MDT layout to non-first entry"
17737
17738         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
17739                 error "Can define multiple entries as MDT layout"
17740
17741         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
17742
17743         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
17744         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
17745         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
17746
17747         local mdtidx=$($LFS getstripe -m $dom)
17748         local mdtname=MDT$(printf %04x $mdtidx)
17749         local facet=mds$((mdtidx + 1))
17750         local space_check=1
17751
17752         # Skip free space checks with ZFS
17753         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
17754
17755         # write
17756         sync
17757         local size_tmp=$((65536 * 3))
17758         local mdtfree1=$(do_facet $facet \
17759                          lctl get_param -n osd*.*$mdtname.kbytesfree)
17760
17761         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
17762         # check also direct IO along write
17763         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
17764         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
17765         sync
17766         cmp $tmp $dom || error "file data is different"
17767         [ $(stat -c%s $dom) == $size_tmp ] ||
17768                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
17769         if [ $space_check == 1 ]; then
17770                 local mdtfree2=$(do_facet $facet \
17771                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
17772
17773                 # increase in usage from by $size_tmp
17774                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17775                         error "MDT free space wrong after write: " \
17776                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17777         fi
17778
17779         # truncate
17780         local size_dom=10000
17781
17782         $TRUNCATE $dom $size_dom
17783         [ $(stat -c%s $dom) == $size_dom ] ||
17784                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
17785         if [ $space_check == 1 ]; then
17786                 mdtfree1=$(do_facet $facet \
17787                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17788                 # decrease in usage from $size_tmp to new $size_dom
17789                 [ $(($mdtfree1 - $mdtfree2)) -ge \
17790                   $(((size_tmp - size_dom) / 1024)) ] ||
17791                         error "MDT free space is wrong after truncate: " \
17792                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
17793         fi
17794
17795         # append
17796         cat $tmp >> $dom
17797         sync
17798         size_dom=$((size_dom + size_tmp))
17799         [ $(stat -c%s $dom) == $size_dom ] ||
17800                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
17801         if [ $space_check == 1 ]; then
17802                 mdtfree2=$(do_facet $facet \
17803                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17804                 # increase in usage by $size_tmp from previous
17805                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
17806                         error "MDT free space is wrong after append: " \
17807                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
17808         fi
17809
17810         # delete
17811         rm $dom
17812         if [ $space_check == 1 ]; then
17813                 mdtfree1=$(do_facet $facet \
17814                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
17815                 # decrease in usage by $size_dom from previous
17816                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
17817                         error "MDT free space is wrong after removal: " \
17818                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
17819         fi
17820
17821         # combined striping
17822         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
17823                 error "Can't create DoM + OST striping"
17824
17825         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
17826         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
17827         # check also direct IO along write
17828         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
17829         sync
17830         cmp $tmp $dom || error "file data is different"
17831         [ $(stat -c%s $dom) == $size_tmp ] ||
17832                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
17833         rm $dom $tmp
17834
17835         return 0
17836 }
17837 run_test 270a "DoM: basic functionality tests"
17838
17839 test_270b() {
17840         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17841                 skip "Need MDS version at least 2.10.55"
17842
17843         local dom=$DIR/$tdir/dom_file
17844         local max_size=1048576
17845
17846         mkdir -p $DIR/$tdir
17847         $LFS setstripe -E $max_size -L mdt $dom
17848
17849         # truncate over the limit
17850         $TRUNCATE $dom $(($max_size + 1)) &&
17851                 error "successful truncate over the maximum size"
17852         # write over the limit
17853         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
17854                 error "successful write over the maximum size"
17855         # append over the limit
17856         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
17857         echo "12345" >> $dom && error "successful append over the maximum size"
17858         rm $dom
17859
17860         return 0
17861 }
17862 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
17863
17864 test_270c() {
17865         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17866                 skip "Need MDS version at least 2.10.55"
17867
17868         mkdir -p $DIR/$tdir
17869         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17870
17871         # check files inherit DoM EA
17872         touch $DIR/$tdir/first
17873         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
17874                 error "bad pattern"
17875         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
17876                 error "bad stripe count"
17877         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
17878                 error "bad stripe size"
17879
17880         # check directory inherits DoM EA and uses it as default
17881         mkdir $DIR/$tdir/subdir
17882         touch $DIR/$tdir/subdir/second
17883         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
17884                 error "bad pattern in sub-directory"
17885         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
17886                 error "bad stripe count in sub-directory"
17887         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
17888                 error "bad stripe size in sub-directory"
17889         return 0
17890 }
17891 run_test 270c "DoM: DoM EA inheritance tests"
17892
17893 test_270d() {
17894         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17895                 skip "Need MDS version at least 2.10.55"
17896
17897         mkdir -p $DIR/$tdir
17898         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
17899
17900         # inherit default DoM striping
17901         mkdir $DIR/$tdir/subdir
17902         touch $DIR/$tdir/subdir/f1
17903
17904         # change default directory striping
17905         $LFS setstripe -c 1 $DIR/$tdir/subdir
17906         touch $DIR/$tdir/subdir/f2
17907         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
17908                 error "wrong default striping in file 2"
17909         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
17910                 error "bad pattern in file 2"
17911         return 0
17912 }
17913 run_test 270d "DoM: change striping from DoM to RAID0"
17914
17915 test_270e() {
17916         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17917                 skip "Need MDS version at least 2.10.55"
17918
17919         mkdir -p $DIR/$tdir/dom
17920         mkdir -p $DIR/$tdir/norm
17921         DOMFILES=20
17922         NORMFILES=10
17923         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
17924         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
17925
17926         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
17927         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
17928
17929         # find DoM files by layout
17930         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
17931         [ $NUM -eq  $DOMFILES ] ||
17932                 error "lfs find -L: found $NUM, expected $DOMFILES"
17933         echo "Test 1: lfs find 20 DOM files by layout: OK"
17934
17935         # there should be 1 dir with default DOM striping
17936         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
17937         [ $NUM -eq  1 ] ||
17938                 error "lfs find -L: found $NUM, expected 1 dir"
17939         echo "Test 2: lfs find 1 DOM dir by layout: OK"
17940
17941         # find DoM files by stripe size
17942         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
17943         [ $NUM -eq  $DOMFILES ] ||
17944                 error "lfs find -S: found $NUM, expected $DOMFILES"
17945         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
17946
17947         # find files by stripe offset except DoM files
17948         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
17949         [ $NUM -eq  $NORMFILES ] ||
17950                 error "lfs find -i: found $NUM, expected $NORMFILES"
17951         echo "Test 5: lfs find no DOM files by stripe index: OK"
17952         return 0
17953 }
17954 run_test 270e "DoM: lfs find with DoM files test"
17955
17956 test_270f() {
17957         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
17958                 skip "Need MDS version at least 2.10.55"
17959
17960         local mdtname=${FSNAME}-MDT0000-mdtlov
17961         local dom=$DIR/$tdir/dom_file
17962         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
17963                                                 lod.$mdtname.dom_stripesize)
17964         local dom_limit=131072
17965
17966         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
17967         local dom_current=$(do_facet mds1 $LCTL get_param -n \
17968                                                 lod.$mdtname.dom_stripesize)
17969         [ ${dom_limit} -eq ${dom_current} ] ||
17970                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
17971
17972         $LFS mkdir -i 0 -c 1 $DIR/$tdir
17973         $LFS setstripe -d $DIR/$tdir
17974         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
17975                 error "Can't set directory default striping"
17976
17977         # exceed maximum stripe size
17978         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
17979                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
17980         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
17981                 error "Able to create DoM component size more than LOD limit"
17982
17983         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
17984         dom_current=$(do_facet mds1 $LCTL get_param -n \
17985                                                 lod.$mdtname.dom_stripesize)
17986         [ 0 -eq ${dom_current} ] ||
17987                 error "Can't set zero DoM stripe limit"
17988         rm $dom
17989
17990         # attempt to create DoM file on server with disabled DoM should
17991         # remove DoM entry from layout and be succeed
17992         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
17993                 error "Can't create DoM file (DoM is disabled)"
17994         [ $($LFS getstripe -L $dom) == "mdt" ] &&
17995                 error "File has DoM component while DoM is disabled"
17996         rm $dom
17997
17998         # attempt to create DoM file with only DoM stripe should return error
17999         $LFS setstripe -E $dom_limit -L mdt $dom &&
18000                 error "Able to create DoM-only file while DoM is disabled"
18001
18002         # too low values to be aligned with smallest stripe size 64K
18003         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
18004         dom_current=$(do_facet mds1 $LCTL get_param -n \
18005                                                 lod.$mdtname.dom_stripesize)
18006         [ 30000 -eq ${dom_current} ] &&
18007                 error "Can set too small DoM stripe limit"
18008
18009         # 64K is a minimal stripe size in Lustre, expect limit of that size
18010         [ 65536 -eq ${dom_current} ] ||
18011                 error "Limit is not set to 64K but ${dom_current}"
18012
18013         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
18014         dom_current=$(do_facet mds1 $LCTL get_param -n \
18015                                                 lod.$mdtname.dom_stripesize)
18016         echo $dom_current
18017         [ 2147483648 -eq ${dom_current} ] &&
18018                 error "Can set too large DoM stripe limit"
18019
18020         do_facet mds1 $LCTL set_param -n \
18021                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
18022         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
18023                 error "Can't create DoM component size after limit change"
18024         do_facet mds1 $LCTL set_param -n \
18025                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
18026         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
18027                 error "Can't create DoM file after limit decrease"
18028         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
18029                 error "Can create big DoM component after limit decrease"
18030         touch ${dom}_def ||
18031                 error "Can't create file with old default layout"
18032
18033         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
18034         return 0
18035 }
18036 run_test 270f "DoM: maximum DoM stripe size checks"
18037
18038 test_271a() {
18039         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18040                 skip "Need MDS version at least 2.10.55"
18041
18042         local dom=$DIR/$tdir/dom
18043
18044         mkdir -p $DIR/$tdir
18045
18046         $LFS setstripe -E 1024K -L mdt $dom
18047
18048         lctl set_param -n mdc.*.stats=clear
18049         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18050         cat $dom > /dev/null
18051         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
18052         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
18053         ls $dom
18054         rm -f $dom
18055 }
18056 run_test 271a "DoM: data is cached for read after write"
18057
18058 test_271b() {
18059         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18060                 skip "Need MDS version at least 2.10.55"
18061
18062         local dom=$DIR/$tdir/dom
18063
18064         mkdir -p $DIR/$tdir
18065
18066         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18067
18068         lctl set_param -n mdc.*.stats=clear
18069         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
18070         cancel_lru_locks mdc
18071         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
18072         # second stat to check size is cached on client
18073         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
18074         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18075         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
18076         rm -f $dom
18077 }
18078 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
18079
18080 test_271ba() {
18081         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18082                 skip "Need MDS version at least 2.10.55"
18083
18084         local dom=$DIR/$tdir/dom
18085
18086         mkdir -p $DIR/$tdir
18087
18088         $LFS setstripe -E 1024K -L mdt -E EOF $dom
18089
18090         lctl set_param -n mdc.*.stats=clear
18091         lctl set_param -n osc.*.stats=clear
18092         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
18093         cancel_lru_locks mdc
18094         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18095         # second stat to check size is cached on client
18096         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
18097         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
18098         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
18099         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
18100         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
18101         rm -f $dom
18102 }
18103 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
18104
18105
18106 get_mdc_stats() {
18107         local mdtidx=$1
18108         local param=$2
18109         local mdt=MDT$(printf %04x $mdtidx)
18110
18111         if [ -z $param ]; then
18112                 lctl get_param -n mdc.*$mdt*.stats
18113         else
18114                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
18115         fi
18116 }
18117
18118 test_271c() {
18119         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
18120                 skip "Need MDS version at least 2.10.55"
18121
18122         local dom=$DIR/$tdir/dom
18123
18124         mkdir -p $DIR/$tdir
18125
18126         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18127
18128         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
18129         local facet=mds$((mdtidx + 1))
18130
18131         cancel_lru_locks mdc
18132         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
18133         createmany -o $dom 1000
18134         lctl set_param -n mdc.*.stats=clear
18135         smalliomany -w $dom 1000 200
18136         get_mdc_stats $mdtidx
18137         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18138         # Each file has 1 open, 1 IO enqueues, total 2000
18139         # but now we have also +1 getxattr for security.capability, total 3000
18140         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
18141         unlinkmany $dom 1000
18142
18143         cancel_lru_locks mdc
18144         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
18145         createmany -o $dom 1000
18146         lctl set_param -n mdc.*.stats=clear
18147         smalliomany -w $dom 1000 200
18148         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
18149         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
18150         # for OPEN and IO lock.
18151         [ $((enq - enq_2)) -ge 1000 ] ||
18152                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
18153         unlinkmany $dom 1000
18154         return 0
18155 }
18156 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
18157
18158 cleanup_271def_tests() {
18159         trap 0
18160         rm -f $1
18161 }
18162
18163 test_271d() {
18164         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18165                 skip "Need MDS version at least 2.10.57"
18166
18167         local dom=$DIR/$tdir/dom
18168         local tmp=$TMP/$tfile
18169         trap "cleanup_271def_tests $tmp" EXIT
18170
18171         mkdir -p $DIR/$tdir
18172
18173         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18174
18175         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18176
18177         cancel_lru_locks mdc
18178         dd if=/dev/urandom of=$tmp bs=1000 count=1
18179         dd if=$tmp of=$dom bs=1000 count=1
18180         cancel_lru_locks mdc
18181
18182         cat /etc/hosts >> $tmp
18183         lctl set_param -n mdc.*.stats=clear
18184
18185         # append data to the same file it should update local page
18186         echo "Append to the same page"
18187         cat /etc/hosts >> $dom
18188         local num=$(get_mdc_stats $mdtidx ost_read)
18189         local ra=$(get_mdc_stats $mdtidx req_active)
18190         local rw=$(get_mdc_stats $mdtidx req_waittime)
18191
18192         [ -z $num ] || error "$num READ RPC occured"
18193         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18194         echo "... DONE"
18195
18196         # compare content
18197         cmp $tmp $dom || error "file miscompare"
18198
18199         cancel_lru_locks mdc
18200         lctl set_param -n mdc.*.stats=clear
18201
18202         echo "Open and read file"
18203         cat $dom > /dev/null
18204         local num=$(get_mdc_stats $mdtidx ost_read)
18205         local ra=$(get_mdc_stats $mdtidx req_active)
18206         local rw=$(get_mdc_stats $mdtidx req_waittime)
18207
18208         [ -z $num ] || error "$num READ RPC occured"
18209         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18210         echo "... DONE"
18211
18212         # compare content
18213         cmp $tmp $dom || error "file miscompare"
18214
18215         return 0
18216 }
18217 run_test 271d "DoM: read on open (1K file in reply buffer)"
18218
18219 test_271f() {
18220         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
18221                 skip "Need MDS version at least 2.10.57"
18222
18223         local dom=$DIR/$tdir/dom
18224         local tmp=$TMP/$tfile
18225         trap "cleanup_271def_tests $tmp" EXIT
18226
18227         mkdir -p $DIR/$tdir
18228
18229         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
18230
18231         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
18232
18233         cancel_lru_locks mdc
18234         dd if=/dev/urandom of=$tmp bs=200000 count=1
18235         dd if=$tmp of=$dom bs=200000 count=1
18236         cancel_lru_locks mdc
18237         cat /etc/hosts >> $tmp
18238         lctl set_param -n mdc.*.stats=clear
18239
18240         echo "Append to the same page"
18241         cat /etc/hosts >> $dom
18242         local num=$(get_mdc_stats $mdtidx ost_read)
18243         local ra=$(get_mdc_stats $mdtidx req_active)
18244         local rw=$(get_mdc_stats $mdtidx req_waittime)
18245
18246         [ -z $num ] || error "$num READ RPC occured"
18247         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18248         echo "... DONE"
18249
18250         # compare content
18251         cmp $tmp $dom || error "file miscompare"
18252
18253         cancel_lru_locks mdc
18254         lctl set_param -n mdc.*.stats=clear
18255
18256         echo "Open and read file"
18257         cat $dom > /dev/null
18258         local num=$(get_mdc_stats $mdtidx ost_read)
18259         local ra=$(get_mdc_stats $mdtidx req_active)
18260         local rw=$(get_mdc_stats $mdtidx req_waittime)
18261
18262         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
18263         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
18264         echo "... DONE"
18265
18266         # compare content
18267         cmp $tmp $dom || error "file miscompare"
18268
18269         return 0
18270 }
18271 run_test 271f "DoM: read on open (200K file and read tail)"
18272
18273 test_271g() {
18274         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
18275                 skip "Skipping due to old client or server version"
18276
18277         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
18278         # to get layout
18279         $CHECKSTAT -t file $DIR1/$tfile
18280
18281         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
18282         MULTIOP_PID=$!
18283         sleep 1
18284         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
18285         $LCTL set_param fail_loc=0x80000314
18286         rm $DIR1/$tfile || error "Unlink fails"
18287         RC=$?
18288         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
18289         [ $RC -eq 0 ] || error "Failed write to stale object"
18290 }
18291 run_test 271g "Discard DoM data vs client flush race"
18292
18293 test_272a() {
18294         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18295                 skip "Need MDS version at least 2.11.50"
18296
18297         local dom=$DIR/$tdir/dom
18298         mkdir -p $DIR/$tdir
18299
18300         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
18301         dd if=/dev/urandom of=$dom bs=512K count=1 ||
18302                 error "failed to write data into $dom"
18303         local old_md5=$(md5sum $dom)
18304
18305         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
18306                 error "failed to migrate to the same DoM component"
18307
18308         [ $($LFS getstripe -c $dom) -eq 2 ] ||
18309                 error "layout was not changed silently"
18310
18311         local new_md5=$(md5sum $dom)
18312
18313         [ "$old_md5" != "$new_md5" ] &&
18314                 error "md5sum differ: $old_md5, $new_md5"
18315         return 0
18316 }
18317 run_test 272a "DoM migration: new layout with the same DOM component"
18318
18319 test_272b() {
18320         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18321                 skip "Need MDS version at least 2.11.50"
18322
18323         local dom=$DIR/$tdir/dom
18324         mkdir -p $DIR/$tdir
18325         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18326
18327         local mdtidx=$($LFS getstripe -m $dom)
18328         local mdtname=MDT$(printf %04x $mdtidx)
18329         local facet=mds$((mdtidx + 1))
18330
18331         local mdtfree1=$(do_facet $facet \
18332                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18333         dd if=/dev/urandom of=$dom bs=2M count=1 ||
18334                 error "failed to write data into $dom"
18335         local old_md5=$(md5sum $dom)
18336         cancel_lru_locks mdc
18337         local mdtfree1=$(do_facet $facet \
18338                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18339
18340         $LFS migrate -c2 $dom ||
18341                 error "failed to migrate to the new composite layout"
18342         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18343                 error "MDT stripe was not removed"
18344
18345         cancel_lru_locks mdc
18346         local new_md5=$(md5sum $dom)
18347         [ "$old_md5" != "$new_md5" ] &&
18348                 error "$old_md5 != $new_md5"
18349
18350         # Skip free space checks with ZFS
18351         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18352                 local mdtfree2=$(do_facet $facet \
18353                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18354                 [ $mdtfree2 -gt $mdtfree1 ] ||
18355                         error "MDT space is not freed after migration"
18356         fi
18357         return 0
18358 }
18359 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
18360
18361 test_272c() {
18362         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18363                 skip "Need MDS version at least 2.11.50"
18364
18365         local dom=$DIR/$tdir/$tfile
18366         mkdir -p $DIR/$tdir
18367         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
18368
18369         local mdtidx=$($LFS getstripe -m $dom)
18370         local mdtname=MDT$(printf %04x $mdtidx)
18371         local facet=mds$((mdtidx + 1))
18372
18373         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
18374                 error "failed to write data into $dom"
18375         local old_md5=$(md5sum $dom)
18376         cancel_lru_locks mdc
18377         local mdtfree1=$(do_facet $facet \
18378                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18379
18380         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
18381                 error "failed to migrate to the new composite layout"
18382         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
18383                 error "MDT stripe was not removed"
18384
18385         cancel_lru_locks mdc
18386         local new_md5=$(md5sum $dom)
18387         [ "$old_md5" != "$new_md5" ] &&
18388                 error "$old_md5 != $new_md5"
18389
18390         # Skip free space checks with ZFS
18391         if [ "$(facet_fstype $facet)" != "zfs" ]; then
18392                 local mdtfree2=$(do_facet $facet \
18393                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
18394                 [ $mdtfree2 -gt $mdtfree1 ] ||
18395                         error "MDS space is not freed after migration"
18396         fi
18397         return 0
18398 }
18399 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
18400
18401 test_273a() {
18402         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
18403                 skip "Need MDS version at least 2.11.50"
18404
18405         # Layout swap cannot be done if either file has DOM component,
18406         # this will never be supported, migration should be used instead
18407
18408         local dom=$DIR/$tdir/$tfile
18409         mkdir -p $DIR/$tdir
18410
18411         $LFS setstripe -c2 ${dom}_plain
18412         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
18413         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
18414                 error "can swap layout with DoM component"
18415         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
18416                 error "can swap layout with DoM component"
18417
18418         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
18419         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
18420                 error "can swap layout with DoM component"
18421         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
18422                 error "can swap layout with DoM component"
18423         return 0
18424 }
18425 run_test 273a "DoM: layout swapping should fail with DOM"
18426
18427 test_275() {
18428         remote_ost_nodsh && skip "remote OST with nodsh"
18429         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
18430                 skip "Need OST version >= 2.10.57"
18431
18432         local file=$DIR/$tfile
18433         local oss
18434
18435         oss=$(comma_list $(osts_nodes))
18436
18437         dd if=/dev/urandom of=$file bs=1M count=2 ||
18438                 error "failed to create a file"
18439         cancel_lru_locks osc
18440
18441         #lock 1
18442         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18443                 error "failed to read a file"
18444
18445 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
18446         $LCTL set_param fail_loc=0x8000031f
18447
18448         cancel_lru_locks osc &
18449         sleep 1
18450
18451 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
18452         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
18453         #IO takes another lock, but matches the PENDING one
18454         #and places it to the IO RPC
18455         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
18456                 error "failed to read a file with PENDING lock"
18457 }
18458 run_test 275 "Read on a canceled duplicate lock"
18459
18460 test_276() {
18461         remote_ost_nodsh && skip "remote OST with nodsh"
18462         local pid
18463
18464         do_facet ost1 "(while true; do \
18465                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
18466                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
18467         pid=$!
18468
18469         for LOOP in $(seq 20); do
18470                 stop ost1
18471                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
18472         done
18473         kill -9 $pid
18474         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
18475                 rm $TMP/sanity_276_pid"
18476 }
18477 run_test 276 "Race between mount and obd_statfs"
18478
18479 cleanup_test_300() {
18480         trap 0
18481         umask $SAVE_UMASK
18482 }
18483 test_striped_dir() {
18484         local mdt_index=$1
18485         local stripe_count
18486         local stripe_index
18487
18488         mkdir -p $DIR/$tdir
18489
18490         SAVE_UMASK=$(umask)
18491         trap cleanup_test_300 RETURN EXIT
18492
18493         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
18494                                                 $DIR/$tdir/striped_dir ||
18495                 error "set striped dir error"
18496
18497         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
18498         [ "$mode" = "755" ] || error "expect 755 got $mode"
18499
18500         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
18501                 error "getdirstripe failed"
18502         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
18503         if [ "$stripe_count" != "2" ]; then
18504                 error "1:stripe_count is $stripe_count, expect 2"
18505         fi
18506         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
18507         if [ "$stripe_count" != "2" ]; then
18508                 error "2:stripe_count is $stripe_count, expect 2"
18509         fi
18510
18511         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
18512         if [ "$stripe_index" != "$mdt_index" ]; then
18513                 error "stripe_index is $stripe_index, expect $mdt_index"
18514         fi
18515
18516         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18517                 error "nlink error after create striped dir"
18518
18519         mkdir $DIR/$tdir/striped_dir/a
18520         mkdir $DIR/$tdir/striped_dir/b
18521
18522         stat $DIR/$tdir/striped_dir/a ||
18523                 error "create dir under striped dir failed"
18524         stat $DIR/$tdir/striped_dir/b ||
18525                 error "create dir under striped dir failed"
18526
18527         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
18528                 error "nlink error after mkdir"
18529
18530         rmdir $DIR/$tdir/striped_dir/a
18531         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
18532                 error "nlink error after rmdir"
18533
18534         rmdir $DIR/$tdir/striped_dir/b
18535         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
18536                 error "nlink error after rmdir"
18537
18538         chattr +i $DIR/$tdir/striped_dir
18539         createmany -o $DIR/$tdir/striped_dir/f 10 &&
18540                 error "immutable flags not working under striped dir!"
18541         chattr -i $DIR/$tdir/striped_dir
18542
18543         rmdir $DIR/$tdir/striped_dir ||
18544                 error "rmdir striped dir error"
18545
18546         cleanup_test_300
18547
18548         true
18549 }
18550
18551 test_300a() {
18552         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18553                 skip "skipped for lustre < 2.7.0"
18554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18555         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18556
18557         test_striped_dir 0 || error "failed on striped dir on MDT0"
18558         test_striped_dir 1 || error "failed on striped dir on MDT0"
18559 }
18560 run_test 300a "basic striped dir sanity test"
18561
18562 test_300b() {
18563         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18564                 skip "skipped for lustre < 2.7.0"
18565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18566         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18567
18568         local i
18569         local mtime1
18570         local mtime2
18571         local mtime3
18572
18573         test_mkdir $DIR/$tdir || error "mkdir fail"
18574         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18575                 error "set striped dir error"
18576         for i in {0..9}; do
18577                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
18578                 sleep 1
18579                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
18580                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
18581                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
18582                 sleep 1
18583                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
18584                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
18585                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
18586         done
18587         true
18588 }
18589 run_test 300b "check ctime/mtime for striped dir"
18590
18591 test_300c() {
18592         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18593                 skip "skipped for lustre < 2.7.0"
18594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18595         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18596
18597         local file_count
18598
18599         mkdir -p $DIR/$tdir
18600         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
18601                 error "set striped dir error"
18602
18603         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
18604                 error "chown striped dir failed"
18605
18606         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
18607                 error "create 5k files failed"
18608
18609         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
18610
18611         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
18612
18613         rm -rf $DIR/$tdir
18614 }
18615 run_test 300c "chown && check ls under striped directory"
18616
18617 test_300d() {
18618         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
18619                 skip "skipped for lustre < 2.7.0"
18620         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18621         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18622
18623         local stripe_count
18624         local file
18625
18626         mkdir -p $DIR/$tdir
18627         $SETSTRIPE -c 2 $DIR/$tdir
18628
18629         #local striped directory
18630         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18631                 error "set striped dir error"
18632         #look at the directories for debug purposes
18633         ls -l $DIR/$tdir
18634         $LFS getdirstripe $DIR/$tdir
18635         ls -l $DIR/$tdir/striped_dir
18636         $LFS getdirstripe $DIR/$tdir/striped_dir
18637         createmany -o $DIR/$tdir/striped_dir/f 10 ||
18638                 error "create 10 files failed"
18639
18640         #remote striped directory
18641         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
18642                 error "set striped dir error"
18643         #look at the directories for debug purposes
18644         ls -l $DIR/$tdir
18645         $LFS getdirstripe $DIR/$tdir
18646         ls -l $DIR/$tdir/remote_striped_dir
18647         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
18648         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
18649                 error "create 10 files failed"
18650
18651         for file in $(find $DIR/$tdir); do
18652                 stripe_count=$($LFS getstripe -c $file)
18653                 [ $stripe_count -eq 2 ] ||
18654                         error "wrong stripe $stripe_count for $file"
18655         done
18656
18657         rm -rf $DIR/$tdir
18658 }
18659 run_test 300d "check default stripe under striped directory"
18660
18661 test_300e() {
18662         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18663                 skip "Need MDS version at least 2.7.55"
18664         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18665         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18666
18667         local stripe_count
18668         local file
18669
18670         mkdir -p $DIR/$tdir
18671
18672         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18673                 error "set striped dir error"
18674
18675         touch $DIR/$tdir/striped_dir/a
18676         touch $DIR/$tdir/striped_dir/b
18677         touch $DIR/$tdir/striped_dir/c
18678
18679         mkdir $DIR/$tdir/striped_dir/dir_a
18680         mkdir $DIR/$tdir/striped_dir/dir_b
18681         mkdir $DIR/$tdir/striped_dir/dir_c
18682
18683         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
18684                 error "set striped adir under striped dir error"
18685
18686         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
18687                 error "set striped bdir under striped dir error"
18688
18689         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
18690                 error "set striped cdir under striped dir error"
18691
18692         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
18693                 error "rename dir under striped dir fails"
18694
18695         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
18696                 error "rename dir under different stripes fails"
18697
18698         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
18699                 error "rename file under striped dir should succeed"
18700
18701         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
18702                 error "rename dir under striped dir should succeed"
18703
18704         rm -rf $DIR/$tdir
18705 }
18706 run_test 300e "check rename under striped directory"
18707
18708 test_300f() {
18709         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18710         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18711         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18712                 skip "Need MDS version at least 2.7.55"
18713
18714         local stripe_count
18715         local file
18716
18717         rm -rf $DIR/$tdir
18718         mkdir -p $DIR/$tdir
18719
18720         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
18721                 error "set striped dir error"
18722
18723         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
18724                 error "set striped dir error"
18725
18726         touch $DIR/$tdir/striped_dir/a
18727         mkdir $DIR/$tdir/striped_dir/dir_a
18728         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
18729                 error "create striped dir under striped dir fails"
18730
18731         touch $DIR/$tdir/striped_dir1/b
18732         mkdir $DIR/$tdir/striped_dir1/dir_b
18733         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
18734                 error "create striped dir under striped dir fails"
18735
18736         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
18737                 error "rename dir under different striped dir should fail"
18738
18739         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
18740                 error "rename striped dir under diff striped dir should fail"
18741
18742         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
18743                 error "rename file under diff striped dirs fails"
18744
18745         rm -rf $DIR/$tdir
18746 }
18747 run_test 300f "check rename cross striped directory"
18748
18749 test_300_check_default_striped_dir()
18750 {
18751         local dirname=$1
18752         local default_count=$2
18753         local default_index=$3
18754         local stripe_count
18755         local stripe_index
18756         local dir_stripe_index
18757         local dir
18758
18759         echo "checking $dirname $default_count $default_index"
18760         $LFS setdirstripe -D -c $default_count -i $default_index \
18761                                 -t all_char $DIR/$tdir/$dirname ||
18762                 error "set default stripe on striped dir error"
18763         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
18764         [ $stripe_count -eq $default_count ] ||
18765                 error "expect $default_count get $stripe_count for $dirname"
18766
18767         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
18768         [ $stripe_index -eq $default_index ] ||
18769                 error "expect $default_index get $stripe_index for $dirname"
18770
18771         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
18772                                                 error "create dirs failed"
18773
18774         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
18775         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
18776         for dir in $(find $DIR/$tdir/$dirname/*); do
18777                 stripe_count=$($LFS getdirstripe -c $dir)
18778                 [ $stripe_count -eq $default_count ] ||
18779                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
18780                 error "stripe count $default_count != $stripe_count for $dir"
18781
18782                 stripe_index=$($LFS getdirstripe -i $dir)
18783                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
18784                         error "$stripe_index != $default_index for $dir"
18785
18786                 #check default stripe
18787                 stripe_count=$($LFS getdirstripe -D -c $dir)
18788                 [ $stripe_count -eq $default_count ] ||
18789                 error "default count $default_count != $stripe_count for $dir"
18790
18791                 stripe_index=$($LFS getdirstripe -D -i $dir)
18792                 [ $stripe_index -eq $default_index ] ||
18793                 error "default index $default_index != $stripe_index for $dir"
18794         done
18795         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
18796 }
18797
18798 test_300g() {
18799         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18800         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18801                 skip "Need MDS version at least 2.7.55"
18802
18803         local dir
18804         local stripe_count
18805         local stripe_index
18806
18807         mkdir $DIR/$tdir
18808         mkdir $DIR/$tdir/normal_dir
18809
18810         #Checking when client cache stripe index
18811         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
18812         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
18813                 error "create striped_dir failed"
18814
18815         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
18816                 error "create dir0 fails"
18817         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
18818         [ $stripe_index -eq 0 ] ||
18819                 error "dir0 expect index 0 got $stripe_index"
18820
18821         mkdir $DIR/$tdir/striped_dir/dir1 ||
18822                 error "create dir1 fails"
18823         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
18824         [ $stripe_index -eq 1 ] ||
18825                 error "dir1 expect index 1 got $stripe_index"
18826
18827         #check default stripe count/stripe index
18828         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
18829         test_300_check_default_striped_dir normal_dir 1 0
18830         test_300_check_default_striped_dir normal_dir 2 1
18831         test_300_check_default_striped_dir normal_dir 2 -1
18832
18833         #delete default stripe information
18834         echo "delete default stripeEA"
18835         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
18836                 error "set default stripe on striped dir error"
18837
18838         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
18839         for dir in $(find $DIR/$tdir/normal_dir/*); do
18840                 stripe_count=$($LFS getdirstripe -c $dir)
18841                 [ $stripe_count -eq 0 ] ||
18842                         error "expect 1 get $stripe_count for $dir"
18843                 stripe_index=$($LFS getdirstripe -i $dir)
18844                 [ $stripe_index -eq 0 ] ||
18845                         error "expect 0 get $stripe_index for $dir"
18846         done
18847 }
18848 run_test 300g "check default striped directory for normal directory"
18849
18850 test_300h() {
18851         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18852         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18853                 skip "Need MDS version at least 2.7.55"
18854
18855         local dir
18856         local stripe_count
18857
18858         mkdir $DIR/$tdir
18859         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18860                 error "set striped dir error"
18861
18862         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
18863         test_300_check_default_striped_dir striped_dir 1 0
18864         test_300_check_default_striped_dir striped_dir 2 1
18865         test_300_check_default_striped_dir striped_dir 2 -1
18866
18867         #delete default stripe information
18868         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
18869                 error "set default stripe on striped dir error"
18870
18871         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
18872         for dir in $(find $DIR/$tdir/striped_dir/*); do
18873                 stripe_count=$($LFS getdirstripe -c $dir)
18874                 [ $stripe_count -eq 0 ] ||
18875                         error "expect 1 get $stripe_count for $dir"
18876         done
18877 }
18878 run_test 300h "check default striped directory for striped directory"
18879
18880 test_300i() {
18881         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18882         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18883         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18884                 skip "Need MDS version at least 2.7.55"
18885
18886         local stripe_count
18887         local file
18888
18889         mkdir $DIR/$tdir
18890
18891         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18892                 error "set striped dir error"
18893
18894         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18895                 error "create files under striped dir failed"
18896
18897         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
18898                 error "set striped hashdir error"
18899
18900         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
18901                 error "create dir0 under hash dir failed"
18902         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
18903                 error "create dir1 under hash dir failed"
18904
18905         # unfortunately, we need to umount to clear dir layout cache for now
18906         # once we fully implement dir layout, we can drop this
18907         umount_client $MOUNT || error "umount failed"
18908         mount_client $MOUNT || error "mount failed"
18909
18910         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
18911         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
18912         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
18913
18914         #set the stripe to be unknown hash type
18915         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
18916         $LCTL set_param fail_loc=0x1901
18917         for ((i = 0; i < 10; i++)); do
18918                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
18919                         error "stat f-$i failed"
18920                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
18921         done
18922
18923         touch $DIR/$tdir/striped_dir/f0 &&
18924                 error "create under striped dir with unknown hash should fail"
18925
18926         $LCTL set_param fail_loc=0
18927
18928         umount_client $MOUNT || error "umount failed"
18929         mount_client $MOUNT || error "mount failed"
18930
18931         return 0
18932 }
18933 run_test 300i "client handle unknown hash type striped directory"
18934
18935 test_300j() {
18936         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18938         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18939                 skip "Need MDS version at least 2.7.55"
18940
18941         local stripe_count
18942         local file
18943
18944         mkdir $DIR/$tdir
18945
18946         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
18947         $LCTL set_param fail_loc=0x1702
18948         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
18949                 error "set striped dir error"
18950
18951         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
18952                 error "create files under striped dir failed"
18953
18954         $LCTL set_param fail_loc=0
18955
18956         rm -rf $DIR/$tdir || error "unlink striped dir fails"
18957
18958         return 0
18959 }
18960 run_test 300j "test large update record"
18961
18962 test_300k() {
18963         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18964         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18965         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18966                 skip "Need MDS version at least 2.7.55"
18967
18968         local stripe_count
18969         local file
18970
18971         mkdir $DIR/$tdir
18972
18973         #define OBD_FAIL_LARGE_STRIPE   0x1703
18974         $LCTL set_param fail_loc=0x1703
18975         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
18976                 error "set striped dir error"
18977         $LCTL set_param fail_loc=0
18978
18979         $LFS getdirstripe $DIR/$tdir/striped_dir ||
18980                 error "getstripeddir fails"
18981         rm -rf $DIR/$tdir/striped_dir ||
18982                 error "unlink striped dir fails"
18983
18984         return 0
18985 }
18986 run_test 300k "test large striped directory"
18987
18988 test_300l() {
18989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18990         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18991         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
18992                 skip "Need MDS version at least 2.7.55"
18993
18994         local stripe_index
18995
18996         test_mkdir -p $DIR/$tdir/striped_dir
18997         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
18998                         error "chown $RUNAS_ID failed"
18999         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
19000                 error "set default striped dir failed"
19001
19002         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
19003         $LCTL set_param fail_loc=0x80000158
19004         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
19005
19006         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
19007         [ $stripe_index -eq 1 ] ||
19008                 error "expect 1 get $stripe_index for $dir"
19009 }
19010 run_test 300l "non-root user to create dir under striped dir with stale layout"
19011
19012 test_300m() {
19013         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19014         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
19015         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19016                 skip "Need MDS version at least 2.7.55"
19017
19018         mkdir -p $DIR/$tdir/striped_dir
19019         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
19020                 error "set default stripes dir error"
19021
19022         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
19023
19024         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
19025         [ $stripe_count -eq 0 ] ||
19026                         error "expect 0 get $stripe_count for a"
19027
19028         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
19029                 error "set default stripes dir error"
19030
19031         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
19032
19033         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
19034         [ $stripe_count -eq 0 ] ||
19035                         error "expect 0 get $stripe_count for b"
19036
19037         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
19038                 error "set default stripes dir error"
19039
19040         mkdir $DIR/$tdir/striped_dir/c &&
19041                 error "default stripe_index is invalid, mkdir c should fails"
19042
19043         rm -rf $DIR/$tdir || error "rmdir fails"
19044 }
19045 run_test 300m "setstriped directory on single MDT FS"
19046
19047 cleanup_300n() {
19048         local list=$(comma_list $(mdts_nodes))
19049
19050         trap 0
19051         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19052 }
19053
19054 test_300n() {
19055         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19056         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19057         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19058                 skip "Need MDS version at least 2.7.55"
19059         remote_mds_nodsh && skip "remote MDS with nodsh"
19060
19061         local stripe_index
19062         local list=$(comma_list $(mdts_nodes))
19063
19064         trap cleanup_300n RETURN EXIT
19065         mkdir -p $DIR/$tdir
19066         chmod 777 $DIR/$tdir
19067         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
19068                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19069                 error "create striped dir succeeds with gid=0"
19070
19071         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19072         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
19073                 error "create striped dir fails with gid=-1"
19074
19075         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19076         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
19077                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
19078                 error "set default striped dir succeeds with gid=0"
19079
19080
19081         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
19082         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
19083                 error "set default striped dir fails with gid=-1"
19084
19085
19086         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
19087         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
19088                                         error "create test_dir fails"
19089         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
19090                                         error "create test_dir1 fails"
19091         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
19092                                         error "create test_dir2 fails"
19093         cleanup_300n
19094 }
19095 run_test 300n "non-root user to create dir under striped dir with default EA"
19096
19097 test_300o() {
19098         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19099         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19100         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
19101                 skip "Need MDS version at least 2.7.55"
19102
19103         local numfree1
19104         local numfree2
19105
19106         mkdir -p $DIR/$tdir
19107
19108         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
19109         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
19110         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
19111                 skip "not enough free inodes $numfree1 $numfree2"
19112         fi
19113
19114         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
19115         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
19116         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
19117                 skip "not enough free space $numfree1 $numfree2"
19118         fi
19119
19120         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
19121                 error "setdirstripe fails"
19122
19123         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
19124                 error "create dirs fails"
19125
19126         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
19127         ls $DIR/$tdir/striped_dir > /dev/null ||
19128                 error "ls striped dir fails"
19129         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
19130                 error "unlink big striped dir fails"
19131 }
19132 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
19133
19134 test_300p() {
19135         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19136         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19137         remote_mds_nodsh && skip "remote MDS with nodsh"
19138
19139         mkdir -p $DIR/$tdir
19140
19141         #define OBD_FAIL_OUT_ENOSPC     0x1704
19142         do_facet mds2 lctl set_param fail_loc=0x80001704
19143         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
19144                  && error "create striped directory should fail"
19145
19146         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
19147
19148         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
19149         true
19150 }
19151 run_test 300p "create striped directory without space"
19152
19153 test_300q() {
19154         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19155         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19156
19157         local fd=$(free_fd)
19158         local cmd="exec $fd<$tdir"
19159         cd $DIR
19160         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
19161         eval $cmd
19162         cmd="exec $fd<&-"
19163         trap "eval $cmd" EXIT
19164         cd $tdir || error "cd $tdir fails"
19165         rmdir  ../$tdir || error "rmdir $tdir fails"
19166         mkdir local_dir && error "create dir succeeds"
19167         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
19168         eval $cmd
19169         return 0
19170 }
19171 run_test 300q "create remote directory under orphan directory"
19172
19173 test_300r() {
19174         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
19175                 skip "Need MDS version at least 2.7.55" && return
19176         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
19177
19178         mkdir $DIR/$tdir
19179
19180         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
19181                 error "set striped dir error"
19182
19183         $LFS getdirstripe $DIR/$tdir/striped_dir ||
19184                 error "getstripeddir fails"
19185
19186         local stripe_count
19187         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
19188                       awk '/lmv_stripe_count:/ { print $2 }')
19189
19190         [ $MDSCOUNT -ne $stripe_count ] &&
19191                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
19192
19193         rm -rf $DIR/$tdir/striped_dir ||
19194                 error "unlink striped dir fails"
19195 }
19196 run_test 300r "test -1 striped directory"
19197
19198 prepare_remote_file() {
19199         mkdir $DIR/$tdir/src_dir ||
19200                 error "create remote source failed"
19201
19202         cp /etc/hosts $DIR/$tdir/src_dir/a ||
19203                  error "cp to remote source failed"
19204         touch $DIR/$tdir/src_dir/a
19205
19206         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
19207                 error "create remote target dir failed"
19208
19209         touch $DIR/$tdir/tgt_dir/b
19210
19211         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
19212                 error "rename dir cross MDT failed!"
19213
19214         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
19215                 error "src_child still exists after rename"
19216
19217         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
19218                 error "missing file(a) after rename"
19219
19220         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
19221                 error "diff after rename"
19222 }
19223
19224 test_310a() {
19225         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19227
19228         local remote_file=$DIR/$tdir/tgt_dir/b
19229
19230         mkdir -p $DIR/$tdir
19231
19232         prepare_remote_file || error "prepare remote file failed"
19233
19234         #open-unlink file
19235         $OPENUNLINK $remote_file $remote_file ||
19236                 error "openunlink $remote_file failed"
19237         $CHECKSTAT -a $remote_file || error "$remote_file exists"
19238 }
19239 run_test 310a "open unlink remote file"
19240
19241 test_310b() {
19242         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
19243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19244
19245         local remote_file=$DIR/$tdir/tgt_dir/b
19246
19247         mkdir -p $DIR/$tdir
19248
19249         prepare_remote_file || error "prepare remote file failed"
19250
19251         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19252         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
19253         $CHECKSTAT -t file $remote_file || error "check file failed"
19254 }
19255 run_test 310b "unlink remote file with multiple links while open"
19256
19257 test_310c() {
19258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19259         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
19260
19261         local remote_file=$DIR/$tdir/tgt_dir/b
19262
19263         mkdir -p $DIR/$tdir
19264
19265         prepare_remote_file || error "prepare remote file failed"
19266
19267         ln $remote_file $DIR/$tfile || error "link failed for remote file"
19268         multiop_bg_pause $remote_file O_uc ||
19269                         error "mulitop failed for remote file"
19270         MULTIPID=$!
19271         $MULTIOP $DIR/$tfile Ouc
19272         kill -USR1 $MULTIPID
19273         wait $MULTIPID
19274 }
19275 run_test 310c "open-unlink remote file with multiple links"
19276
19277 #LU-4825
19278 test_311() {
19279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19280         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19281         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
19282                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
19283         remote_mds_nodsh && skip "remote MDS with nodsh"
19284
19285         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19286         local mdts=$(comma_list $(mdts_nodes))
19287
19288         mkdir -p $DIR/$tdir
19289         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
19290         createmany -o $DIR/$tdir/$tfile. 1000
19291
19292         # statfs data is not real time, let's just calculate it
19293         old_iused=$((old_iused + 1000))
19294
19295         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19296                         osp.*OST0000*MDT0000.create_count")
19297         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
19298                                 osp.*OST0000*MDT0000.max_create_count")
19299         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
19300
19301         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
19302         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
19303         [ $index -ne 0 ] || error "$tfile stripe index is 0"
19304
19305         unlinkmany $DIR/$tdir/$tfile. 1000
19306
19307         do_nodes $mdts "$LCTL set_param -n \
19308                         osp.*OST0000*.max_create_count=$max_count"
19309         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
19310                 do_nodes $mdts "$LCTL set_param -n \
19311                                 osp.*OST0000*.create_count=$count"
19312         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
19313                         grep "=0" && error "create_count is zero"
19314
19315         local new_iused
19316         for i in $(seq 120); do
19317                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
19318                 # system may be too busy to destroy all objs in time, use
19319                 # a somewhat small value to not fail autotest
19320                 [ $((old_iused - new_iused)) -gt 400 ] && break
19321                 sleep 1
19322         done
19323
19324         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
19325         [ $((old_iused - new_iused)) -gt 400 ] ||
19326                 error "objs not destroyed after unlink"
19327 }
19328 run_test 311 "disable OSP precreate, and unlink should destroy objs"
19329
19330 zfs_oid_to_objid()
19331 {
19332         local ost=$1
19333         local objid=$2
19334
19335         local vdevdir=$(dirname $(facet_vdevice $ost))
19336         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
19337         local zfs_zapid=$(do_facet $ost $cmd |
19338                           grep -w "/O/0/d$((objid%32))" -C 5 |
19339                           awk '/Object/{getline; print $1}')
19340         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
19341                           awk "/$objid = /"'{printf $3}')
19342
19343         echo $zfs_objid
19344 }
19345
19346 zfs_object_blksz() {
19347         local ost=$1
19348         local objid=$2
19349
19350         local vdevdir=$(dirname $(facet_vdevice $ost))
19351         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
19352         local blksz=$(do_facet $ost $cmd $objid |
19353                       awk '/dblk/{getline; printf $4}')
19354
19355         case "${blksz: -1}" in
19356                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
19357                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
19358                 *) ;;
19359         esac
19360
19361         echo $blksz
19362 }
19363
19364 test_312() { # LU-4856
19365         remote_ost_nodsh && skip "remote OST with nodsh"
19366         [ "$ost1_FSTYPE" = "zfs" ] ||
19367                 skip_env "the test only applies to zfs"
19368
19369         local max_blksz=$(do_facet ost1 \
19370                           $ZFS get -p recordsize $(facet_device ost1) |
19371                           awk '!/VALUE/{print $3}')
19372
19373         # to make life a little bit easier
19374         $LFS mkdir -c 1 -i 0 $DIR/$tdir
19375         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19376
19377         local tf=$DIR/$tdir/$tfile
19378         touch $tf
19379         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19380
19381         # Get ZFS object id
19382         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19383         # block size change by sequential overwrite
19384         local bs
19385
19386         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
19387                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
19388
19389                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
19390                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
19391         done
19392         rm -f $tf
19393
19394         # block size change by sequential append write
19395         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
19396         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19397         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19398         local count
19399
19400         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
19401                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
19402                         oflag=sync conv=notrunc
19403
19404                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
19405                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
19406                         error "blksz error, actual $blksz, " \
19407                                 "expected: 2 * $count * $PAGE_SIZE"
19408         done
19409         rm -f $tf
19410
19411         # random write
19412         touch $tf
19413         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
19414         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
19415
19416         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
19417         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19418         [ $blksz -eq $PAGE_SIZE ] ||
19419                 error "blksz error: $blksz, expected: $PAGE_SIZE"
19420
19421         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
19422         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19423         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
19424
19425         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
19426         blksz=$(zfs_object_blksz ost1 $zfs_objid)
19427         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
19428 }
19429 run_test 312 "make sure ZFS adjusts its block size by write pattern"
19430
19431 test_313() {
19432         remote_ost_nodsh && skip "remote OST with nodsh"
19433
19434         local file=$DIR/$tfile
19435
19436         rm -f $file
19437         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
19438
19439         # define OBD_FAIL_TGT_RCVD_EIO           0x720
19440         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19441         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
19442                 error "write should failed"
19443         do_facet ost1 "$LCTL set_param fail_loc=0"
19444         rm -f $file
19445 }
19446 run_test 313 "io should fail after last_rcvd update fail"
19447
19448 test_314() {
19449         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
19450
19451         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
19452         do_facet ost1 "$LCTL set_param fail_loc=0x720"
19453         rm -f $DIR/$tfile
19454         wait_delete_completed
19455         do_facet ost1 "$LCTL set_param fail_loc=0"
19456 }
19457 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
19458
19459 test_315() { # LU-618
19460         local file=$DIR/$tfile
19461         rm -f $file
19462
19463         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
19464                 error "multiop file write failed"
19465         $MULTIOP $file oO_RDONLY:r4063232_c &
19466         PID=$!
19467
19468         sleep 2
19469
19470         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
19471         kill -USR1 $PID
19472
19473         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
19474         rm -f $file
19475 }
19476 run_test 315 "read should be accounted"
19477
19478 test_316() {
19479         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19480         large_xattr_enabled || skip_env "ea_inode feature disabled"
19481
19482         rm -rf $DIR/$tdir/d
19483         mkdir -p $DIR/$tdir/d
19484         chown nobody $DIR/$tdir/d
19485         touch $DIR/$tdir/d/file
19486
19487         $LFS mv -M1 $DIR/$tdir/d || error "lfs mv failed"
19488 }
19489 run_test 316 "lfs mv"
19490
19491 test_317() {
19492         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
19493                 skip "Need MDS version at least 2.11.53"
19494         if [ "$ost1_FSTYPE" == "zfs" ]; then
19495                 skip "LU-10370: no implementation for ZFS"
19496         fi
19497
19498         local trunc_sz
19499         local grant_blk_size
19500
19501         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
19502                         awk '/grant_block_size:/ { print $2; exit; }')
19503         #
19504         # Create File of size 5M. Truncate it to below size's and verify
19505         # blocks count.
19506         #
19507         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
19508                 error "Create file $DIR/$tfile failed"
19509         stack_trap "rm -f $DIR/$tfile" EXIT
19510
19511         for trunc_sz in 2097152 4097 4000 509 0; do
19512                 $TRUNCATE $DIR/$tfile $trunc_sz ||
19513                         error "truncate $tfile to $trunc_sz failed"
19514                 local sz=$(stat --format=%s $DIR/$tfile)
19515                 local blk=$(stat --format=%b $DIR/$tfile)
19516                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
19517                                      grant_blk_size) * 8))
19518
19519                 if [[ $blk -ne $trunc_blk ]]; then
19520                         $(which stat) $DIR/$tfile
19521                         error "Expected Block $trunc_blk got $blk for $tfile"
19522                 fi
19523
19524                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
19525                         error "Expected Size $trunc_sz got $sz for $tfile"
19526         done
19527
19528         #
19529         # sparse file test
19530         # Create file with a hole and write actual two blocks. Block count
19531         # must be 16.
19532         #
19533         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
19534                 conv=fsync || error "Create file : $DIR/$tfile"
19535
19536         # Calculate the final truncate size.
19537         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
19538
19539         #
19540         # truncate to size $trunc_sz bytes. Strip the last block
19541         # The block count must drop to 8
19542         #
19543         $TRUNCATE $DIR/$tfile $trunc_sz ||
19544                 error "truncate $tfile to $trunc_sz failed"
19545
19546         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
19547         sz=$(stat --format=%s $DIR/$tfile)
19548         blk=$(stat --format=%b $DIR/$tfile)
19549
19550         if [[ $blk -ne $trunc_bsz ]]; then
19551                 $(which stat) $DIR/$tfile
19552                 error "Expected Block $trunc_bsz got $blk for $tfile"
19553         fi
19554
19555         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
19556                 error "Expected Size $trunc_sz got $sz for $tfile"
19557 }
19558 run_test 317 "Verify blocks get correctly update after truncate"
19559
19560 test_fake_rw() {
19561         local read_write=$1
19562         if [ "$read_write" = "write" ]; then
19563                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
19564         elif [ "$read_write" = "read" ]; then
19565                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
19566         else
19567                 error "argument error"
19568         fi
19569
19570         # turn off debug for performance testing
19571         local saved_debug=$($LCTL get_param -n debug)
19572         $LCTL set_param debug=0
19573
19574         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
19575
19576         # get ost1 size - lustre-OST0000
19577         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
19578         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
19579         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
19580
19581         if [ "$read_write" = "read" ]; then
19582                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
19583         fi
19584
19585         local start_time=$(date +%s.%N)
19586         $dd_cmd bs=1M count=$blocks oflag=sync ||
19587                 error "real dd $read_write error"
19588         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
19589
19590         if [ "$read_write" = "write" ]; then
19591                 rm -f $DIR/$tfile
19592         fi
19593
19594         # define OBD_FAIL_OST_FAKE_RW           0x238
19595         do_facet ost1 $LCTL set_param fail_loc=0x238
19596
19597         local start_time=$(date +%s.%N)
19598         $dd_cmd bs=1M count=$blocks oflag=sync ||
19599                 error "fake dd $read_write error"
19600         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
19601
19602         if [ "$read_write" = "write" ]; then
19603                 # verify file size
19604                 cancel_lru_locks osc
19605                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
19606                         error "$tfile size not $blocks MB"
19607         fi
19608         do_facet ost1 $LCTL set_param fail_loc=0
19609
19610         echo "fake $read_write $duration_fake vs. normal $read_write" \
19611                 "$duration in seconds"
19612         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
19613                 error_not_in_vm "fake write is slower"
19614
19615         $LCTL set_param -n debug="$saved_debug"
19616         rm -f $DIR/$tfile
19617 }
19618 test_399a() { # LU-7655 for OST fake write
19619         remote_ost_nodsh && skip "remote OST with nodsh"
19620
19621         test_fake_rw write
19622 }
19623 run_test 399a "fake write should not be slower than normal write"
19624
19625 test_399b() { # LU-8726 for OST fake read
19626         remote_ost_nodsh && skip "remote OST with nodsh"
19627         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
19628                 skip_env "ldiskfs only test"
19629         fi
19630
19631         test_fake_rw read
19632 }
19633 run_test 399b "fake read should not be slower than normal read"
19634
19635 test_400a() { # LU-1606, was conf-sanity test_74
19636         if ! which $CC > /dev/null 2>&1; then
19637                 skip_env "$CC is not installed"
19638         fi
19639
19640         local extra_flags=''
19641         local out=$TMP/$tfile
19642         local prefix=/usr/include/lustre
19643         local prog
19644
19645         # Oleg removes c files in his test rig so test if any c files exist
19646         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
19647                 skip_env "Needed c test files are missing"
19648
19649         if ! [[ -d $prefix ]]; then
19650                 # Assume we're running in tree and fixup the include path.
19651                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
19652                 extra_flags+=" -L$LUSTRE/utils/.lib"
19653         fi
19654
19655         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
19656                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
19657                         error "client api broken"
19658         done
19659         rm -f $out
19660 }
19661 run_test 400a "Lustre client api program can compile and link"
19662
19663 test_400b() { # LU-1606, LU-5011
19664         local header
19665         local out=$TMP/$tfile
19666         local prefix=/usr/include/linux/lustre
19667
19668         # We use a hard coded prefix so that this test will not fail
19669         # when run in tree. There are headers in lustre/include/lustre/
19670         # that are not packaged (like lustre_idl.h) and have more
19671         # complicated include dependencies (like config.h and lnet/types.h).
19672         # Since this test about correct packaging we just skip them when
19673         # they don't exist (see below) rather than try to fixup cppflags.
19674
19675         if ! which $CC > /dev/null 2>&1; then
19676                 skip_env "$CC is not installed"
19677         fi
19678
19679         for header in $prefix/*.h; do
19680                 if ! [[ -f "$header" ]]; then
19681                         continue
19682                 fi
19683
19684                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
19685                         continue # lustre_ioctl.h is internal header
19686                 fi
19687
19688                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
19689                         error "cannot compile '$header'"
19690         done
19691         rm -f $out
19692 }
19693 run_test 400b "packaged headers can be compiled"
19694
19695 test_401a() { #LU-7437
19696         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
19697         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
19698
19699         #count the number of parameters by "list_param -R"
19700         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
19701         #count the number of parameters by listing proc files
19702         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
19703         echo "proc_dirs='$proc_dirs'"
19704         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
19705         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
19706                       sort -u | wc -l)
19707
19708         [ $params -eq $procs ] ||
19709                 error "found $params parameters vs. $procs proc files"
19710
19711         # test the list_param -D option only returns directories
19712         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
19713         #count the number of parameters by listing proc directories
19714         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
19715                 sort -u | wc -l)
19716
19717         [ $params -eq $procs ] ||
19718                 error "found $params parameters vs. $procs proc files"
19719 }
19720 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
19721
19722 test_401b() {
19723         local save=$($LCTL get_param -n jobid_var)
19724         local tmp=testing
19725
19726         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
19727                 error "no error returned when setting bad parameters"
19728
19729         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
19730         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
19731
19732         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
19733         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
19734         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
19735 }
19736 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
19737
19738 test_401c() {
19739         local jobid_var_old=$($LCTL get_param -n jobid_var)
19740         local jobid_var_new
19741
19742         $LCTL set_param jobid_var= &&
19743                 error "no error returned for 'set_param a='"
19744
19745         jobid_var_new=$($LCTL get_param -n jobid_var)
19746         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
19747                 error "jobid_var was changed by setting without value"
19748
19749         $LCTL set_param jobid_var &&
19750                 error "no error returned for 'set_param a'"
19751
19752         jobid_var_new=$($LCTL get_param -n jobid_var)
19753         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
19754                 error "jobid_var was changed by setting without value"
19755 }
19756 run_test 401c "Verify 'lctl set_param' without value fails in either format."
19757
19758 test_401d() {
19759         local jobid_var_old=$($LCTL get_param -n jobid_var)
19760         local jobid_var_new
19761         local new_value="foo=bar"
19762
19763         $LCTL set_param jobid_var=$new_value ||
19764                 error "'set_param a=b' did not accept a value containing '='"
19765
19766         jobid_var_new=$($LCTL get_param -n jobid_var)
19767         [[ "$jobid_var_new" == "$new_value" ]] ||
19768                 error "'set_param a=b' failed on a value containing '='"
19769
19770         # Reset the jobid_var to test the other format
19771         $LCTL set_param jobid_var=$jobid_var_old
19772         jobid_var_new=$($LCTL get_param -n jobid_var)
19773         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
19774                 error "failed to reset jobid_var"
19775
19776         $LCTL set_param jobid_var $new_value ||
19777                 error "'set_param a b' did not accept a value containing '='"
19778
19779         jobid_var_new=$($LCTL get_param -n jobid_var)
19780         [[ "$jobid_var_new" == "$new_value" ]] ||
19781                 error "'set_param a b' failed on a value containing '='"
19782
19783         $LCTL set_param jobid_var $jobid_var_old
19784         jobid_var_new=$($LCTL get_param -n jobid_var)
19785         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
19786                 error "failed to reset jobid_var"
19787 }
19788 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
19789
19790 test_402() {
19791         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
19792         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
19793                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
19794         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
19795                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
19796                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
19797         remote_mds_nodsh && skip "remote MDS with nodsh"
19798
19799         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
19800 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
19801         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
19802         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
19803                 echo "Touch failed - OK"
19804 }
19805 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
19806
19807 test_403() {
19808         local file1=$DIR/$tfile.1
19809         local file2=$DIR/$tfile.2
19810         local tfile=$TMP/$tfile
19811
19812         rm -f $file1 $file2 $tfile
19813
19814         touch $file1
19815         ln $file1 $file2
19816
19817         # 30 sec OBD_TIMEOUT in ll_getattr()
19818         # right before populating st_nlink
19819         $LCTL set_param fail_loc=0x80001409
19820         stat -c %h $file1 > $tfile &
19821
19822         # create an alias, drop all locks and reclaim the dentry
19823         < $file2
19824         cancel_lru_locks mdc
19825         cancel_lru_locks osc
19826         sysctl -w vm.drop_caches=2
19827
19828         wait
19829
19830         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
19831
19832         rm -f $tfile $file1 $file2
19833 }
19834 run_test 403 "i_nlink should not drop to zero due to aliasing"
19835
19836 test_404() { # LU-6601
19837         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
19838                 skip "Need server version newer than 2.8.52"
19839         remote_mds_nodsh && skip "remote MDS with nodsh"
19840
19841         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
19842                 awk '/osp .*-osc-MDT/ { print $4}')
19843
19844         local osp
19845         for osp in $mosps; do
19846                 echo "Deactivate: " $osp
19847                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
19848                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19849                         awk -vp=$osp '$4 == p { print $2 }')
19850                 [ $stat = IN ] || {
19851                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19852                         error "deactivate error"
19853                 }
19854                 echo "Activate: " $osp
19855                 do_facet $SINGLEMDS $LCTL --device %$osp activate
19856                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
19857                         awk -vp=$osp '$4 == p { print $2 }')
19858                 [ $stat = UP ] || {
19859                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
19860                         error "activate error"
19861                 }
19862         done
19863 }
19864 run_test 404 "validate manual {de}activated works properly for OSPs"
19865
19866 test_405() {
19867         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
19868                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
19869                         skip "Layout swap lock is not supported"
19870
19871         check_swap_layouts_support
19872
19873         test_mkdir $DIR/$tdir
19874         swap_lock_test -d $DIR/$tdir ||
19875                 error "One layout swap locked test failed"
19876 }
19877 run_test 405 "Various layout swap lock tests"
19878
19879 test_406() {
19880         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19881         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
19882         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
19883         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19884         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
19885                 skip "Need MDS version at least 2.8.50"
19886
19887         local def_stripe_size=$($LFS getstripe -S $MOUNT)
19888         local test_pool=$TESTNAME
19889
19890         pool_add $test_pool || error "pool_add failed"
19891         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
19892                 error "pool_add_targets failed"
19893
19894         save_layout_restore_at_exit $MOUNT
19895
19896         # parent set default stripe count only, child will stripe from both
19897         # parent and fs default
19898         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
19899                 error "setstripe $MOUNT failed"
19900         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
19901         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
19902         for i in $(seq 10); do
19903                 local f=$DIR/$tdir/$tfile.$i
19904                 touch $f || error "touch failed"
19905                 local count=$($LFS getstripe -c $f)
19906                 [ $count -eq $OSTCOUNT ] ||
19907                         error "$f stripe count $count != $OSTCOUNT"
19908                 local offset=$($LFS getstripe -i $f)
19909                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
19910                 local size=$($LFS getstripe -S $f)
19911                 [ $size -eq $((def_stripe_size * 2)) ] ||
19912                         error "$f stripe size $size != $((def_stripe_size * 2))"
19913                 local pool=$($LFS getstripe -p $f)
19914                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
19915         done
19916
19917         # change fs default striping, delete parent default striping, now child
19918         # will stripe from new fs default striping only
19919         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
19920                 error "change $MOUNT default stripe failed"
19921         $LFS setstripe -c 0 $DIR/$tdir ||
19922                 error "delete $tdir default stripe failed"
19923         for i in $(seq 11 20); do
19924                 local f=$DIR/$tdir/$tfile.$i
19925                 touch $f || error "touch $f failed"
19926                 local count=$($LFS getstripe -c $f)
19927                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
19928                 local offset=$($LFS getstripe -i $f)
19929                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
19930                 local size=$($LFS getstripe -S $f)
19931                 [ $size -eq $def_stripe_size ] ||
19932                         error "$f stripe size $size != $def_stripe_size"
19933                 local pool=$($LFS getstripe -p $f)
19934                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
19935         done
19936
19937         unlinkmany $DIR/$tdir/$tfile. 1 20
19938
19939         local f=$DIR/$tdir/$tfile
19940         pool_remove_all_targets $test_pool $f
19941         pool_remove $test_pool $f
19942 }
19943 run_test 406 "DNE support fs default striping"
19944
19945 test_407() {
19946         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19947         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
19948                 skip "Need MDS version at least 2.8.55"
19949         remote_mds_nodsh && skip "remote MDS with nodsh"
19950
19951         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
19952                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
19953         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
19954                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
19955         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
19956
19957         #define OBD_FAIL_DT_TXN_STOP    0x2019
19958         for idx in $(seq $MDSCOUNT); do
19959                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
19960         done
19961         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
19962         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
19963                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
19964         true
19965 }
19966 run_test 407 "transaction fail should cause operation fail"
19967
19968 test_408() {
19969         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
19970
19971         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
19972         lctl set_param fail_loc=0x8000040a
19973         # let ll_prepare_partial_page() fail
19974         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
19975
19976         rm -f $DIR/$tfile
19977
19978         # create at least 100 unused inodes so that
19979         # shrink_icache_memory(0) should not return 0
19980         touch $DIR/$tfile-{0..100}
19981         rm -f $DIR/$tfile-{0..100}
19982         sync
19983
19984         echo 2 > /proc/sys/vm/drop_caches
19985 }
19986 run_test 408 "drop_caches should not hang due to page leaks"
19987
19988 test_409()
19989 {
19990         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19991         check_mount_and_prep
19992
19993         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
19994         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
19995         touch $DIR/$tdir/guard || error "(2) Fail to create"
19996
19997         local PREFIX=$(str_repeat 'A' 128)
19998         echo "Create 1K hard links start at $(date)"
19999         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20000                 error "(3) Fail to hard link"
20001
20002         echo "Links count should be right although linkEA overflow"
20003         stat $DIR/$tdir/guard || error "(4) Fail to stat"
20004         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
20005         [ $linkcount -eq 1001 ] ||
20006                 error "(5) Unexpected hard links count: $linkcount"
20007
20008         echo "List all links start at $(date)"
20009         ls -l $DIR/$tdir/foo > /dev/null ||
20010                 error "(6) Fail to list $DIR/$tdir/foo"
20011
20012         echo "Unlink hard links start at $(date)"
20013         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
20014                 error "(7) Fail to unlink"
20015 }
20016 run_test 409 "Large amount of cross-MDTs hard links on the same file"
20017
20018 test_410()
20019 {
20020         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
20021                 skip "Need client version at least 2.9.59"
20022
20023         # Create a file, and stat it from the kernel
20024         local testfile=$DIR/$tfile
20025         touch $testfile
20026
20027         local run_id=$RANDOM
20028         local my_ino=$(stat --format "%i" $testfile)
20029
20030         # Try to insert the module. This will always fail as the
20031         # module is designed to not be inserted.
20032         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
20033             &> /dev/null
20034
20035         # Anything but success is a test failure
20036         dmesg | grep -q \
20037             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
20038             error "no inode match"
20039 }
20040 run_test 410 "Test inode number returned from kernel thread"
20041
20042 cleanup_test411_cgroup() {
20043         trap 0
20044         rmdir "$1"
20045 }
20046
20047 test_411() {
20048         local cg_basedir=/sys/fs/cgroup/memory
20049         # LU-9966
20050         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
20051                 skip "no setup for cgroup"
20052
20053         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
20054                 error "test file creation failed"
20055         cancel_lru_locks osc
20056
20057         # Create a very small memory cgroup to force a slab allocation error
20058         local cgdir=$cg_basedir/osc_slab_alloc
20059         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
20060         trap "cleanup_test411_cgroup $cgdir" EXIT
20061         echo 2M > $cgdir/memory.kmem.limit_in_bytes
20062         echo 1M > $cgdir/memory.limit_in_bytes
20063
20064         # Should not LBUG, just be killed by oom-killer
20065         # dd will return 0 even allocation failure in some environment.
20066         # So don't check return value
20067         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
20068         cleanup_test411_cgroup $cgdir
20069
20070         return 0
20071 }
20072 run_test 411 "Slab allocation error with cgroup does not LBUG"
20073
20074 test_412() {
20075         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20076         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20077                 skip "Need server version at least 2.10.55"
20078         fi
20079
20080         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
20081                 error "mkdir failed"
20082         $LFS getdirstripe $DIR/$tdir
20083         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20084         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
20085                 error "expect $((MDSCOUT - 1)) get $stripe_index"
20086         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
20087         [ $stripe_count -eq 2 ] ||
20088                 error "expect 2 get $stripe_count"
20089 }
20090 run_test 412 "mkdir on specific MDTs"
20091
20092 test_413() {
20093         [ $MDSCOUNT -lt 2 ] &&
20094                 skip "We need at least 2 MDTs for this test"
20095
20096         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
20097                 skip "Need server version at least 2.10.55"
20098         fi
20099
20100         mkdir $DIR/$tdir || error "mkdir failed"
20101
20102         # find MDT that is the most full
20103         local max=$($LFS df | grep MDT |
20104                 awk 'BEGIN { a=0 }
20105                         { sub("%", "", $5)
20106                           if (0+$5 >= a)
20107                           {
20108                                 a = $5
20109                                 b = $6
20110                           }
20111                         }
20112                      END { split(b, c, ":")
20113                            sub("]", "", c[2])
20114                            print c[2]
20115                          }')
20116
20117         for i in $(seq $((MDSCOUNT - 1))); do
20118                 $LFS mkdir -c $i $DIR/$tdir/d$i ||
20119                         error "mkdir d$i failed"
20120                 $LFS getdirstripe $DIR/$tdir/d$i
20121                 local stripe_index=$($LFS getdirstripe -i $DIR/$tdir/d$i)
20122                 [ $stripe_index -ne $max ] ||
20123                         error "don't expect $max"
20124         done
20125 }
20126 run_test 413 "mkdir on less full MDTs"
20127
20128 test_414() {
20129 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
20130         $LCTL set_param fail_loc=0x80000521
20131         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
20132         rm -f $DIR/$tfile
20133 }
20134 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
20135
20136 test_415() {
20137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20138         [ $(lustre_version_code mds1) -lt $(version_code 2.11.52) ] &&
20139                 skip "Need server version at least 2.11.52"
20140
20141         # LU-11102
20142         local total
20143         local setattr_pid
20144         local start_time
20145         local end_time
20146         local duration
20147
20148         total=500
20149         # this test may be slow on ZFS
20150         [ "$mds1_FSTYPE" == "zfs" ] && total=100
20151
20152         # though this test is designed for striped directory, let's test normal
20153         # directory too since lock is always saved as CoS lock.
20154         test_mkdir $DIR/$tdir || error "mkdir $tdir"
20155         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
20156
20157         (
20158                 while true; do
20159                         touch $DIR/$tdir
20160                 done
20161         ) &
20162         setattr_pid=$!
20163
20164         start_time=$(date +%s)
20165         for i in $(seq $total); do
20166                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
20167                         > /dev/null
20168         done
20169         end_time=$(date +%s)
20170         duration=$((end_time - start_time))
20171
20172         kill -9 $setattr_pid
20173
20174         echo "rename $total files took $duration sec"
20175         [ $duration -lt 100 ] || error "rename took $duration sec"
20176 }
20177 run_test 415 "lock revoke is not missing"
20178
20179 test_416() {
20180         [ $(lustre_version_code mds1) -lt $(version_code 2.11.55) ] &&
20181                 skip "Need server version at least 2.11.55"
20182
20183         # define OBD_FAIL_OSD_TXN_START    0x19a
20184         do_facet mds1 lctl set_param fail_loc=0x19a
20185
20186         lfs mkdir -c $MDSCOUNT $DIR/$tdir
20187
20188         true
20189 }
20190 run_test 416 "transaction start failure won't cause system hung"
20191
20192 cleanup_417() {
20193         trap 0
20194         do_nodes $(comma_list $(mdts_nodes)) \
20195                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
20196         do_nodes $(comma_list $(mdts_nodes)) \
20197                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
20198         do_nodes $(comma_list $(mdts_nodes)) \
20199                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
20200 }
20201
20202 test_417() {
20203         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20204         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
20205                 skip "Need MDS version at least 2.11.56"
20206
20207         trap cleanup_417 RETURN EXIT
20208
20209         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
20210         do_nodes $(comma_list $(mdts_nodes)) \
20211                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
20212         $LFS migrate -m 0 $DIR/$tdir.1 &&
20213                 error "migrate dir $tdir.1 should fail"
20214
20215         do_nodes $(comma_list $(mdts_nodes)) \
20216                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
20217         $LFS mkdir -i 1 $DIR/$tdir.2 &&
20218                 error "create remote dir $tdir.2 should fail"
20219
20220         do_nodes $(comma_list $(mdts_nodes)) \
20221                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
20222         $LFS mkdir -c 2 $DIR/$tdir.3 &&
20223                 error "create striped dir $tdir.3 should fail"
20224         true
20225 }
20226 run_test 417 "disable remote dir, striped dir and dir migration"
20227
20228 # Checks that the outputs of df [-i] and lfs df [-i] match
20229 #
20230 # usage: check_lfs_df <blocks | inodes> <mountpoint>
20231 check_lfs_df() {
20232         local dir=$2
20233         local inodes
20234         local df_out
20235         local lfs_df_out
20236         local count
20237         local passed=false
20238
20239         # blocks or inodes
20240         [ "$1" == "blocks" ] && inodes= || inodes="-i"
20241
20242         for count in {1..100}; do
20243                 cancel_lru_locks
20244                 sync; sleep 2
20245
20246                 # read the lines of interest
20247                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
20248                         error "df $inodes $dir | tail -n +2 failed"
20249                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
20250                         error "lfs df $inodes $dir | grep summary: failed"
20251
20252                 # skip first substrings of each output as they are different
20253                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
20254                 # compare the remaining fields of the two outputs
20255                 passed=true
20256                 for i in {1..5}; do
20257                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
20258                 done
20259                 $passed && break
20260         done
20261
20262         if ! $passed; then
20263                 df -P $inodes $dir
20264                 echo
20265                 lfs df $inodes $dir
20266                 error "df and lfs df $1 output mismatch: "      \
20267                       "df ${inodes}: ${df_out[*]}, "            \
20268                       "lfs df ${inodes}: ${lfs_df_out[*]}"
20269         fi
20270 }
20271
20272 test_418() {
20273         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20274
20275         local dir=$DIR/$tdir
20276         local numfiles=$((RANDOM % 4096 + 2))
20277         local numblocks=$((RANDOM % 256 + 1))
20278
20279         wait_delete_completed
20280         test_mkdir $dir
20281
20282         # check block output
20283         check_lfs_df blocks $dir
20284         # check inode output
20285         check_lfs_df inodes $dir
20286
20287         # create a single file and retest
20288         echo "Creating a single file and testing"
20289         createmany -o $dir/$tfile- 1 &>/dev/null ||
20290                 error "creating 1 file in $dir failed"
20291         check_lfs_df blocks $dir
20292         check_lfs_df inodes $dir
20293
20294         # create a random number of files
20295         echo "Creating $((numfiles - 1)) files and testing"
20296         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
20297                 error "creating $((numfiles - 1)) files in $dir failed"
20298
20299         # write a random number of blocks to the first test file
20300         echo "Writing $numblocks 4K blocks and testing"
20301         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
20302                 count=$numblocks &>/dev/null ||
20303                 error "dd to $dir/${tfile}-0 failed"
20304
20305         # retest
20306         check_lfs_df blocks $dir
20307         check_lfs_df inodes $dir
20308
20309         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
20310                 error "unlinking $numfiles files in $dir failed"
20311 }
20312 run_test 418 "df and lfs df outputs match"
20313
20314 test_419()
20315 {
20316         local dir=$DIR/$tdir
20317
20318         mkdir -p $dir
20319         touch $dir/file
20320
20321         cancel_lru_locks mdc
20322
20323         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
20324         $LCTL set_param fail_loc=0x1410
20325         cat $dir/file
20326         $LCTL set_param fail_loc=0
20327         rm -rf $dir
20328 }
20329 run_test 419 "Verify open file by name doesn't crash kernel"
20330
20331 test_420()
20332 {
20333         [[ $MDS1_VERSION -ge $(version_code 2.12.1) ]] ||
20334                 skip "Need MDS version at least 2.12.1"
20335
20336         local SAVE_UMASK=$(umask)
20337         local dir=$DIR/$tdir
20338         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
20339
20340         mkdir -p $dir
20341         umask 0000
20342         mkdir -m03777 $dir/testdir
20343         ls -dn $dir/testdir
20344         # Need to remove trailing '.' when SELinux is enabled
20345         local dirperms=$(ls -dn $dir/testdir |
20346                          awk '{ sub(/\.$/, "", $1); print $1}')
20347         [ $dirperms == "drwxrwsrwt" ] ||
20348                 error "incorrect perms on $dir/testdir"
20349
20350         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
20351                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
20352         ls -n $dir/testdir/testfile
20353         local fileperms=$(ls -n $dir/testdir/testfile |
20354                           awk '{ sub(/\.$/, "", $1); print $1}')
20355         [ $fileperms == "-rwxr-xr-x" ] ||
20356                 error "incorrect perms on $dir/testdir/testfile"
20357
20358         umask $SAVE_UMASK
20359 }
20360 run_test 420 "clear SGID bit on non-directories for non-members"
20361
20362 test_421a() {
20363         local cnt
20364         local fid1
20365         local fid2
20366
20367         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20368                 skip "Need MDS version at least 2.12.2"
20369
20370         test_mkdir $DIR/$tdir
20371         createmany -o $DIR/$tdir/f 3
20372         cnt=$(ls -1 $DIR/$tdir | wc -l)
20373         [ $cnt != 3 ] && error "unexpected #files: $cnt"
20374
20375         fid1=$(lfs path2fid $DIR/$tdir/f1)
20376         fid2=$(lfs path2fid $DIR/$tdir/f2)
20377         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
20378
20379         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
20380         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
20381
20382         cnt=$(ls -1 $DIR/$tdir | wc -l)
20383         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
20384
20385         rm -f $DIR/$tdir/f3 || error "can't remove f3"
20386         createmany -o $DIR/$tdir/f 3
20387         cnt=$(ls -1 $DIR/$tdir | wc -l)
20388         [ $cnt != 3 ] && error "unexpected #files: $cnt"
20389
20390         fid1=$(lfs path2fid $DIR/$tdir/f1)
20391         fid2=$(lfs path2fid $DIR/$tdir/f2)
20392         echo "remove using fsname $FSNAME"
20393         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
20394
20395         cnt=$(ls -1 $DIR/$tdir | wc -l)
20396         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
20397 }
20398 run_test 421a "simple rm by fid"
20399
20400 test_421b() {
20401         local cnt
20402         local FID1
20403         local FID2
20404
20405         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20406                 skip "Need MDS version at least 2.12.2"
20407
20408         test_mkdir $DIR/$tdir
20409         createmany -o $DIR/$tdir/f 3
20410         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
20411         MULTIPID=$!
20412
20413         FID1=$(lfs path2fid $DIR/$tdir/f1)
20414         FID2=$(lfs path2fid $DIR/$tdir/f2)
20415         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
20416
20417         kill -USR1 $MULTIPID
20418         wait
20419
20420         cnt=$(ls $DIR/$tdir | wc -l)
20421         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
20422 }
20423 run_test 421b "rm by fid on open file"
20424
20425 test_421c() {
20426         local cnt
20427         local FIDS
20428
20429         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20430                 skip "Need MDS version at least 2.12.2"
20431
20432         test_mkdir $DIR/$tdir
20433         createmany -o $DIR/$tdir/f 3
20434         touch $DIR/$tdir/$tfile
20435         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
20436         cnt=$(ls -1 $DIR/$tdir | wc -l)
20437         [ $cnt != 184 ] && error "unexpected #files: $cnt"
20438
20439         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
20440         $LFS rmfid $DIR $FID1 || error "rmfid failed"
20441
20442         cnt=$(ls $DIR/$tdir | wc -l)
20443         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
20444 }
20445 run_test 421c "rm by fid against hardlinked files"
20446
20447 test_421d() {
20448         local cnt
20449         local FIDS
20450
20451         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20452                 skip "Need MDS version at least 2.12.2"
20453
20454         test_mkdir $DIR/$tdir
20455         createmany -o $DIR/$tdir/f 4097
20456         cnt=$(ls -1 $DIR/$tdir | wc -l)
20457         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
20458
20459         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
20460         $LFS rmfid $DIR $FIDS || error "rmfid failed"
20461
20462         cnt=$(ls $DIR/$tdir | wc -l)
20463         rm -rf $DIR/$tdir
20464         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
20465 }
20466 run_test 421d "rmfid en masse"
20467
20468 test_421e() {
20469         local cnt
20470         local FID
20471
20472         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20473         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20474                 skip "Need MDS version at least 2.12.2"
20475
20476         mkdir -p $DIR/$tdir
20477         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
20478         createmany -o $DIR/$tdir/striped_dir/f 512
20479         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
20480         [ $cnt != 512 ] && error "unexpected #files: $cnt"
20481
20482         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
20483                 sed "s/[/][^:]*://g")
20484         $LFS rmfid $DIR $FIDS || error "rmfid failed"
20485
20486         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
20487         rm -rf $DIR/$tdir
20488         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
20489 }
20490 run_test 421e "rmfid in DNE"
20491
20492 test_421f() {
20493         local cnt
20494         local FID
20495
20496         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20497                 skip "Need MDS version at least 2.12.2"
20498
20499         test_mkdir $DIR/$tdir
20500         touch $DIR/$tdir/f
20501         cnt=$(ls -1 $DIR/$tdir | wc -l)
20502         [ $cnt != 1 ] && error "unexpected #files: $cnt"
20503
20504         FID=$(lfs path2fid $DIR/$tdir/f)
20505         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
20506         # rmfid should fail
20507         cnt=$(ls -1 $DIR/$tdir | wc -l)
20508         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
20509
20510         chmod a+rw $DIR/$tdir
20511         ls -la $DIR/$tdir
20512         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
20513         # rmfid should fail
20514         cnt=$(ls -1 $DIR/$tdir | wc -l)
20515         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
20516
20517         rm -f $DIR/$tdir/f
20518         $RUNAS touch $DIR/$tdir/f
20519         FID=$(lfs path2fid $DIR/$tdir/f)
20520         echo "rmfid as root"
20521         $LFS rmfid $DIR $FID || error "rmfid as root failed"
20522         cnt=$(ls -1 $DIR/$tdir | wc -l)
20523         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
20524
20525         rm -f $DIR/$tdir/f
20526         $RUNAS touch $DIR/$tdir/f
20527         cnt=$(ls -1 $DIR/$tdir | wc -l)
20528         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
20529         FID=$(lfs path2fid $DIR/$tdir/f)
20530         # rmfid w/o user_fid2path mount option should fail
20531         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
20532         cnt=$(ls -1 $DIR/$tdir | wc -l)
20533         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
20534
20535         umount_client $MOUNT || "failed to umount client"
20536         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
20537                 "failed to mount client'"
20538
20539         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
20540         # rmfid should succeed
20541         cnt=$(ls -1 $DIR/$tdir | wc -l)
20542         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
20543
20544         # rmfid shouldn't allow to remove files due to dir's permission
20545         chmod a+rwx $DIR/$tdir
20546         touch $DIR/$tdir/f
20547         ls -la $DIR/$tdir
20548         FID=$(lfs path2fid $DIR/$tdir/f)
20549         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
20550
20551         umount_client $MOUNT || "failed to umount client"
20552         mount_client $MOUNT "$MOUNT_OPTS" ||
20553                 "failed to mount client'"
20554
20555 }
20556 run_test 421f "rmfid checks permissions"
20557
20558 test_421g() {
20559         local cnt
20560         local FIDS
20561
20562         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20563         [ $MDS1_VERSION -lt $(version_code 2.12.2) ] &&
20564                 skip "Need MDS version at least 2.12.2"
20565
20566         mkdir -p $DIR/$tdir
20567         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
20568         createmany -o $DIR/$tdir/striped_dir/f 512
20569         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
20570         [ $cnt != 512 ] && error "unexpected #files: $cnt"
20571
20572         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
20573                 sed "s/[/][^:]*://g")
20574
20575         rm -f $DIR/$tdir/striped_dir/f1*
20576         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
20577         removed=$((512 - cnt))
20578
20579         # few files have been just removed, so we expect
20580         # rmfid to fail on their fids
20581         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
20582         [ $removed != $errors ] && error "$errors != $removed"
20583
20584         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
20585         rm -rf $DIR/$tdir
20586         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
20587 }
20588 run_test 421g "rmfid to return errors properly"
20589
20590 stat_test() {
20591     df -h $MOUNT &
20592     df -h $MOUNT &
20593     df -h $MOUNT &
20594     df -h $MOUNT &
20595     df -h $MOUNT &
20596     df -h $MOUNT &
20597 }
20598
20599 test_423() {
20600     local _stats
20601     # ensure statfs cache is expired
20602     sleep 2;
20603
20604     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
20605     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
20606
20607     return 0
20608 }
20609 run_test 423 "statfs should return a right data"
20610
20611 prep_801() {
20612         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
20613         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
20614                 skip "Need server version at least 2.9.55"
20615
20616         start_full_debug_logging
20617 }
20618
20619 post_801() {
20620         stop_full_debug_logging
20621 }
20622
20623 barrier_stat() {
20624         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
20625                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
20626                            awk '/The barrier for/ { print $7 }')
20627                 echo $st
20628         else
20629                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
20630                 echo \'$st\'
20631         fi
20632 }
20633
20634 barrier_expired() {
20635         local expired
20636
20637         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
20638                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
20639                           awk '/will be expired/ { print $7 }')
20640         else
20641                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
20642         fi
20643
20644         echo $expired
20645 }
20646
20647 test_801a() {
20648         prep_801
20649
20650         echo "Start barrier_freeze at: $(date)"
20651         #define OBD_FAIL_BARRIER_DELAY          0x2202
20652         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
20653         # Do not reduce barrier time - See LU-11873
20654         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
20655
20656         sleep 2
20657         local b_status=$(barrier_stat)
20658         echo "Got barrier status at: $(date)"
20659         [ "$b_status" = "'freezing_p1'" ] ||
20660                 error "(1) unexpected barrier status $b_status"
20661
20662         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
20663         wait
20664         b_status=$(barrier_stat)
20665         [ "$b_status" = "'frozen'" ] ||
20666                 error "(2) unexpected barrier status $b_status"
20667
20668         local expired=$(barrier_expired)
20669         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
20670         sleep $((expired + 3))
20671
20672         b_status=$(barrier_stat)
20673         [ "$b_status" = "'expired'" ] ||
20674                 error "(3) unexpected barrier status $b_status"
20675
20676         # Do not reduce barrier time - See LU-11873
20677         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
20678                 error "(4) fail to freeze barrier"
20679
20680         b_status=$(barrier_stat)
20681         [ "$b_status" = "'frozen'" ] ||
20682                 error "(5) unexpected barrier status $b_status"
20683
20684         echo "Start barrier_thaw at: $(date)"
20685         #define OBD_FAIL_BARRIER_DELAY          0x2202
20686         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
20687         do_facet mgs $LCTL barrier_thaw $FSNAME &
20688
20689         sleep 2
20690         b_status=$(barrier_stat)
20691         echo "Got barrier status at: $(date)"
20692         [ "$b_status" = "'thawing'" ] ||
20693                 error "(6) unexpected barrier status $b_status"
20694
20695         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
20696         wait
20697         b_status=$(barrier_stat)
20698         [ "$b_status" = "'thawed'" ] ||
20699                 error "(7) unexpected barrier status $b_status"
20700
20701         #define OBD_FAIL_BARRIER_FAILURE        0x2203
20702         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
20703         do_facet mgs $LCTL barrier_freeze $FSNAME
20704
20705         b_status=$(barrier_stat)
20706         [ "$b_status" = "'failed'" ] ||
20707                 error "(8) unexpected barrier status $b_status"
20708
20709         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
20710         do_facet mgs $LCTL barrier_thaw $FSNAME
20711
20712         post_801
20713 }
20714 run_test 801a "write barrier user interfaces and stat machine"
20715
20716 test_801b() {
20717         prep_801
20718
20719         mkdir $DIR/$tdir || error "(1) fail to mkdir"
20720         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
20721         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
20722         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
20723         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
20724
20725         cancel_lru_locks mdc
20726
20727         # 180 seconds should be long enough
20728         do_facet mgs $LCTL barrier_freeze $FSNAME 180
20729
20730         local b_status=$(barrier_stat)
20731         [ "$b_status" = "'frozen'" ] ||
20732                 error "(6) unexpected barrier status $b_status"
20733
20734         mkdir $DIR/$tdir/d0/d10 &
20735         mkdir_pid=$!
20736
20737         touch $DIR/$tdir/d1/f13 &
20738         touch_pid=$!
20739
20740         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
20741         ln_pid=$!
20742
20743         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
20744         mv_pid=$!
20745
20746         rm -f $DIR/$tdir/d4/f12 &
20747         rm_pid=$!
20748
20749         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
20750
20751         # To guarantee taht the 'stat' is not blocked
20752         b_status=$(barrier_stat)
20753         [ "$b_status" = "'frozen'" ] ||
20754                 error "(8) unexpected barrier status $b_status"
20755
20756         # let above commands to run at background
20757         sleep 5
20758
20759         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
20760         ps -p $touch_pid || error "(10) touch should be blocked"
20761         ps -p $ln_pid || error "(11) link should be blocked"
20762         ps -p $mv_pid || error "(12) rename should be blocked"
20763         ps -p $rm_pid || error "(13) unlink should be blocked"
20764
20765         b_status=$(barrier_stat)
20766         [ "$b_status" = "'frozen'" ] ||
20767                 error "(14) unexpected barrier status $b_status"
20768
20769         do_facet mgs $LCTL barrier_thaw $FSNAME
20770         b_status=$(barrier_stat)
20771         [ "$b_status" = "'thawed'" ] ||
20772                 error "(15) unexpected barrier status $b_status"
20773
20774         wait $mkdir_pid || error "(16) mkdir should succeed"
20775         wait $touch_pid || error "(17) touch should succeed"
20776         wait $ln_pid || error "(18) link should succeed"
20777         wait $mv_pid || error "(19) rename should succeed"
20778         wait $rm_pid || error "(20) unlink should succeed"
20779
20780         post_801
20781 }
20782 run_test 801b "modification will be blocked by write barrier"
20783
20784 test_801c() {
20785         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20786
20787         prep_801
20788
20789         stop mds2 || error "(1) Fail to stop mds2"
20790
20791         do_facet mgs $LCTL barrier_freeze $FSNAME 30
20792
20793         local b_status=$(barrier_stat)
20794         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
20795                 do_facet mgs $LCTL barrier_thaw $FSNAME
20796                 error "(2) unexpected barrier status $b_status"
20797         }
20798
20799         do_facet mgs $LCTL barrier_rescan $FSNAME ||
20800                 error "(3) Fail to rescan barrier bitmap"
20801
20802         # Do not reduce barrier time - See LU-11873
20803         do_facet mgs $LCTL barrier_freeze $FSNAME 20
20804
20805         b_status=$(barrier_stat)
20806         [ "$b_status" = "'frozen'" ] ||
20807                 error "(4) unexpected barrier status $b_status"
20808
20809         do_facet mgs $LCTL barrier_thaw $FSNAME
20810         b_status=$(barrier_stat)
20811         [ "$b_status" = "'thawed'" ] ||
20812                 error "(5) unexpected barrier status $b_status"
20813
20814         local devname=$(mdsdevname 2)
20815
20816         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
20817
20818         do_facet mgs $LCTL barrier_rescan $FSNAME ||
20819                 error "(7) Fail to rescan barrier bitmap"
20820
20821         post_801
20822 }
20823 run_test 801c "rescan barrier bitmap"
20824
20825 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
20826 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
20827 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
20828 saved_MOUNT_OPTS=$MOUNT_OPTS
20829
20830 cleanup_802() {
20831         trap 0
20832
20833         stopall
20834         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
20835         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
20836         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
20837         MOUNT_OPTS=$saved_MOUNT_OPTS
20838         setupall
20839 }
20840
20841 test_802() {
20842
20843         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
20844         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
20845                 skip "Need server version at least 2.9.55"
20846
20847         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
20848
20849         mkdir $DIR/$tdir || error "(1) fail to mkdir"
20850
20851         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
20852                 error "(2) Fail to copy"
20853
20854         trap cleanup_802 EXIT
20855
20856         # sync by force before remount as readonly
20857         sync; sync_all_data; sleep 3; sync_all_data
20858
20859         stopall
20860
20861         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
20862         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
20863         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
20864
20865         echo "Mount the server as read only"
20866         setupall server_only || error "(3) Fail to start servers"
20867
20868         echo "Mount client without ro should fail"
20869         mount_client $MOUNT &&
20870                 error "(4) Mount client without 'ro' should fail"
20871
20872         echo "Mount client with ro should succeed"
20873         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
20874         mount_client $MOUNT ||
20875                 error "(5) Mount client with 'ro' should succeed"
20876
20877         echo "Modify should be refused"
20878         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
20879
20880         echo "Read should be allowed"
20881         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
20882                 error "(7) Read should succeed under ro mode"
20883
20884         cleanup_802
20885 }
20886 run_test 802 "simulate readonly device"
20887
20888 test_803() {
20889         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20890         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
20891                 skip "MDS needs to be newer than 2.10.54"
20892
20893         mkdir -p $DIR/$tdir
20894         # Create some objects on all MDTs to trigger related logs objects
20895         for idx in $(seq $MDSCOUNT); do
20896                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
20897                         $DIR/$tdir/dir${idx} ||
20898                         error "Fail to create $DIR/$tdir/dir${idx}"
20899         done
20900
20901         sync; sleep 3
20902         wait_delete_completed # ensure old test cleanups are finished
20903         echo "before create:"
20904         $LFS df -i $MOUNT
20905         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20906
20907         for i in {1..10}; do
20908                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
20909                         error "Fail to create $DIR/$tdir/foo$i"
20910         done
20911
20912         sync; sleep 3
20913         echo "after create:"
20914         $LFS df -i $MOUNT
20915         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20916
20917         # allow for an llog to be cleaned up during the test
20918         [ $after_used -ge $((before_used + 10 - 1)) ] ||
20919                 error "before ($before_used) + 10 > after ($after_used)"
20920
20921         for i in {1..10}; do
20922                 rm -rf $DIR/$tdir/foo$i ||
20923                         error "Fail to remove $DIR/$tdir/foo$i"
20924         done
20925
20926         sleep 3 # avoid MDT return cached statfs
20927         wait_delete_completed
20928         echo "after unlink:"
20929         $LFS df -i $MOUNT
20930         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
20931
20932         # allow for an llog to be created during the test
20933         [ $after_used -le $((before_used + 1)) ] ||
20934                 error "after ($after_used) > before ($before_used) + 1"
20935 }
20936 run_test 803 "verify agent object for remote object"
20937
20938 test_804() {
20939         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
20940         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
20941                 skip "MDS needs to be newer than 2.10.54"
20942         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
20943
20944         mkdir -p $DIR/$tdir
20945         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
20946                 error "Fail to create $DIR/$tdir/dir0"
20947
20948         local fid=$($LFS path2fid $DIR/$tdir/dir0)
20949         local dev=$(mdsdevname 2)
20950
20951         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20952                 grep ${fid} || error "NOT found agent entry for dir0"
20953
20954         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
20955                 error "Fail to create $DIR/$tdir/dir1"
20956
20957         touch $DIR/$tdir/dir1/foo0 ||
20958                 error "Fail to create $DIR/$tdir/dir1/foo0"
20959         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
20960         local rc=0
20961
20962         for idx in $(seq $MDSCOUNT); do
20963                 dev=$(mdsdevname $idx)
20964                 do_facet mds${idx} \
20965                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20966                         grep ${fid} && rc=$idx
20967         done
20968
20969         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
20970                 error "Fail to rename foo0 to foo1"
20971         if [ $rc -eq 0 ]; then
20972                 for idx in $(seq $MDSCOUNT); do
20973                         dev=$(mdsdevname $idx)
20974                         do_facet mds${idx} \
20975                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20976                         grep ${fid} && rc=$idx
20977                 done
20978         fi
20979
20980         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
20981                 error "Fail to rename foo1 to foo2"
20982         if [ $rc -eq 0 ]; then
20983                 for idx in $(seq $MDSCOUNT); do
20984                         dev=$(mdsdevname $idx)
20985                         do_facet mds${idx} \
20986                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
20987                         grep ${fid} && rc=$idx
20988                 done
20989         fi
20990
20991         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
20992
20993         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
20994                 error "Fail to link to $DIR/$tdir/dir1/foo2"
20995         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
20996                 error "Fail to rename foo2 to foo0"
20997         unlink $DIR/$tdir/dir1/foo0 ||
20998                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
20999         rm -rf $DIR/$tdir/dir0 ||
21000                 error "Fail to rm $DIR/$tdir/dir0"
21001
21002         for idx in $(seq $MDSCOUNT); do
21003                 dev=$(mdsdevname $idx)
21004                 rc=0
21005
21006                 stop mds${idx}
21007                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
21008                         rc=$?
21009                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
21010                         error "mount mds$idx failed"
21011                 df $MOUNT > /dev/null 2>&1
21012
21013                 # e2fsck should not return error
21014                 [ $rc -eq 0 ] ||
21015                         error "e2fsck detected error on MDT${idx}: rc=$rc"
21016         done
21017 }
21018 run_test 804 "verify agent entry for remote entry"
21019
21020 cleanup_805() {
21021         do_facet $SINGLEMDS zfs set quota=$old $fsset
21022         unlinkmany $DIR/$tdir/f- 1000000
21023         trap 0
21024 }
21025
21026 test_805() {
21027         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
21028         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
21029         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
21030                 skip "netfree not implemented before 0.7"
21031         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
21032                 skip "Need MDS version at least 2.10.57"
21033
21034         local fsset
21035         local freekb
21036         local usedkb
21037         local old
21038         local quota
21039         local pref="osd-zfs.lustre-MDT0000."
21040
21041         # limit available space on MDS dataset to meet nospace issue
21042         # quickly. then ZFS 0.7.2 can use reserved space if asked
21043         # properly (using netfree flag in osd_declare_destroy()
21044         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
21045         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
21046                 gawk '{print $3}')
21047         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
21048         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
21049         let "usedkb=usedkb-freekb"
21050         let "freekb=freekb/2"
21051         if let "freekb > 5000"; then
21052                 let "freekb=5000"
21053         fi
21054         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
21055         trap cleanup_805 EXIT
21056         mkdir $DIR/$tdir
21057         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
21058         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
21059         rm -rf $DIR/$tdir || error "not able to remove"
21060         do_facet $SINGLEMDS zfs set quota=$old $fsset
21061         trap 0
21062 }
21063 run_test 805 "ZFS can remove from full fs"
21064
21065 # Size-on-MDS test
21066 check_lsom_data()
21067 {
21068         local file=$1
21069         local size=$($LFS getsom -s $file)
21070         local expect=$(stat -c %s $file)
21071
21072         [[ $size == $expect ]] ||
21073                 error "$file expected size: $expect, got: $size"
21074
21075         local blocks=$($LFS getsom -b $file)
21076         expect=$(stat -c %b $file)
21077         [[ $blocks == $expect ]] ||
21078                 error "$file expected blocks: $expect, got: $blocks"
21079 }
21080
21081 check_lsom_size()
21082 {
21083         local size=$($LFS getsom -s $1)
21084         local expect=$2
21085
21086         [[ $size == $expect ]] ||
21087                 error "$file expected size: $expect, got: $size"
21088 }
21089
21090 test_806() {
21091         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21092                 skip "Need MDS version at least 2.11.52"
21093
21094         local bs=1048576
21095
21096         touch $DIR/$tfile || error "touch $tfile failed"
21097
21098         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21099         save_lustre_params client "llite.*.xattr_cache" > $save
21100         lctl set_param llite.*.xattr_cache=0
21101         stack_trap "restore_lustre_params < $save" EXIT
21102
21103         # single-threaded write
21104         echo "Test SOM for single-threaded write"
21105         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
21106                 error "write $tfile failed"
21107         check_lsom_size $DIR/$tfile $bs
21108
21109         local num=32
21110         local size=$(($num * $bs))
21111         local offset=0
21112         local i
21113
21114         echo "Test SOM for single client multi-threaded($num) write"
21115         $TRUNCATE $DIR/$tfile 0
21116         for ((i = 0; i < $num; i++)); do
21117                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21118                 local pids[$i]=$!
21119                 offset=$((offset + $bs))
21120         done
21121         for (( i=0; i < $num; i++ )); do
21122                 wait ${pids[$i]}
21123         done
21124         check_lsom_size $DIR/$tfile $size
21125
21126         $TRUNCATE $DIR/$tfile 0
21127         for ((i = 0; i < $num; i++)); do
21128                 offset=$((offset - $bs))
21129                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21130                 local pids[$i]=$!
21131         done
21132         for (( i=0; i < $num; i++ )); do
21133                 wait ${pids[$i]}
21134         done
21135         check_lsom_size $DIR/$tfile $size
21136
21137         # multi-client writes
21138         num=$(get_node_count ${CLIENTS//,/ })
21139         size=$(($num * $bs))
21140         offset=0
21141         i=0
21142
21143         echo "Test SOM for multi-client ($num) writes"
21144         $TRUNCATE $DIR/$tfile 0
21145         for client in ${CLIENTS//,/ }; do
21146                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21147                 local pids[$i]=$!
21148                 i=$((i + 1))
21149                 offset=$((offset + $bs))
21150         done
21151         for (( i=0; i < $num; i++ )); do
21152                 wait ${pids[$i]}
21153         done
21154         check_lsom_size $DIR/$tfile $offset
21155
21156         i=0
21157         $TRUNCATE $DIR/$tfile 0
21158         for client in ${CLIENTS//,/ }; do
21159                 offset=$((offset - $bs))
21160                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21161                 local pids[$i]=$!
21162                 i=$((i + 1))
21163         done
21164         for (( i=0; i < $num; i++ )); do
21165                 wait ${pids[$i]}
21166         done
21167         check_lsom_size $DIR/$tfile $size
21168
21169         # verify truncate
21170         echo "Test SOM for truncate"
21171         $TRUNCATE $DIR/$tfile 1048576
21172         check_lsom_size $DIR/$tfile 1048576
21173         $TRUNCATE $DIR/$tfile 1234
21174         check_lsom_size $DIR/$tfile 1234
21175
21176         # verify SOM blocks count
21177         echo "Verify SOM block count"
21178         $TRUNCATE $DIR/$tfile 0
21179         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
21180                 error "failed to write file $tfile"
21181         check_lsom_data $DIR/$tfile
21182 }
21183 run_test 806 "Verify Lazy Size on MDS"
21184
21185 test_807() {
21186         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
21187                 skip "Need MDS version at least 2.11.52"
21188
21189         # Registration step
21190         changelog_register || error "changelog_register failed"
21191         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
21192         changelog_users $SINGLEMDS | grep -q $cl_user ||
21193                 error "User $cl_user not found in changelog_users"
21194
21195         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21196         save_lustre_params client "llite.*.xattr_cache" > $save
21197         lctl set_param llite.*.xattr_cache=0
21198         stack_trap "restore_lustre_params < $save" EXIT
21199
21200         rm -rf $DIR/$tdir || error "rm $tdir failed"
21201         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
21202         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
21203         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
21204         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
21205                 error "truncate $tdir/trunc failed"
21206
21207         local bs=1048576
21208         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 ||
21209                 error "write $tfile failed"
21210
21211         # multi-client wirtes
21212         local num=$(get_node_count ${CLIENTS//,/ })
21213         local offset=0
21214         local i=0
21215
21216         echo "Test SOM for multi-client ($num) writes"
21217         touch $DIR/$tfile || error "touch $tfile failed"
21218         $TRUNCATE $DIR/$tfile 0
21219         for client in ${CLIENTS//,/ }; do
21220                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
21221                 local pids[$i]=$!
21222                 i=$((i + 1))
21223                 offset=$((offset + $bs))
21224         done
21225         for (( i=0; i < $num; i++ )); do
21226                 wait ${pids[$i]}
21227         done
21228
21229         sleep 5
21230         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
21231         check_lsom_data $DIR/$tdir/trunc
21232         check_lsom_data $DIR/$tdir/single_dd
21233         check_lsom_data $DIR/$tfile
21234
21235         rm -rf $DIR/$tdir
21236         # Deregistration step
21237         changelog_deregister || error "changelog_deregister failed"
21238 }
21239 run_test 807 "verify LSOM syncing tool"
21240
21241 check_som_nologged()
21242 {
21243         local lines=$($LFS changelog $FSNAME-MDT0000 |
21244                 grep 'x=trusted.som' | wc -l)
21245         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
21246 }
21247
21248 test_808() {
21249         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
21250                 skip "Need MDS version at least 2.11.55"
21251
21252         # Registration step
21253         changelog_register || error "changelog_register failed"
21254
21255         touch $DIR/$tfile || error "touch $tfile failed"
21256         check_som_nologged
21257
21258         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
21259                 error "write $tfile failed"
21260         check_som_nologged
21261
21262         $TRUNCATE $DIR/$tfile 1234
21263         check_som_nologged
21264
21265         $TRUNCATE $DIR/$tfile 1048576
21266         check_som_nologged
21267
21268         # Deregistration step
21269         changelog_deregister || error "changelog_deregister failed"
21270 }
21271 run_test 808 "Check trusted.som xattr not logged in Changelogs"
21272
21273 check_som_nodata()
21274 {
21275         $LFS getsom $1
21276         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
21277 }
21278
21279 test_809() {
21280         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
21281                 skip "Need MDS version at least 2.11.56"
21282
21283         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
21284                 error "failed to create DoM-only file $DIR/$tfile"
21285         touch $DIR/$tfile || error "touch $tfile failed"
21286         check_som_nodata $DIR/$tfile
21287
21288         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
21289                 error "write $tfile failed"
21290         check_som_nodata $DIR/$tfile
21291
21292         $TRUNCATE $DIR/$tfile 1234
21293         check_som_nodata $DIR/$tfile
21294
21295         $TRUNCATE $DIR/$tfile 4097
21296         check_som_nodata $DIR/$file
21297 }
21298 run_test 809 "Verify no SOM xattr store for DoM-only files"
21299
21300 test_810() {
21301         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21302         $GSS && skip_env "could not run with gss"
21303
21304         set_checksums 1
21305         stack_trap "set_checksums $ORIG_CSUM" EXIT
21306         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
21307
21308         local csum
21309         local before
21310         local after
21311         for csum in $CKSUM_TYPES; do
21312                 #define OBD_FAIL_OSC_NO_GRANT   0x411
21313                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
21314                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
21315                         eval set -- $i
21316                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
21317                         before=$(md5sum $DIR/$tfile)
21318                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
21319                         after=$(md5sum $DIR/$tfile)
21320                         [ "$before" == "$after" ] ||
21321                                 error "$csum: $before != $after bs=$1 seek=$2"
21322                 done
21323         done
21324 }
21325 run_test 810 "partial page writes on ZFS (LU-11663)"
21326
21327 test_811() {
21328         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
21329                 skip "Need MDS version at least 2.11.56"
21330
21331         #define OBD_FAIL_MDS_ORPHAN_DELETE      0x165
21332         do_facet mds1 $LCTL set_param fail_loc=0x165
21333         $MULTIOP $DIR/$tfile Ouc || error "multiop failed"
21334
21335         stop mds1
21336         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
21337
21338         sleep 5
21339         [[ $(do_facet mds1 pgrep orph_.*-MDD | wc -l) -eq 0 ]] ||
21340                 error "MDD orphan cleanup thread not quit"
21341 }
21342 run_test 811 "orphan name stub can be cleaned up in startup"
21343
21344 test_812() {
21345         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
21346                 skip "OST < 2.12.51 doesn't support this fail_loc"
21347         [ "$SHARED_KEY" = true ] &&
21348                 skip "OSC connections never go IDLE with Shared-Keys enabled"
21349
21350         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21351         # ensure ost1 is connected
21352         stat $DIR/$tfile >/dev/null || error "can't stat"
21353         wait_osc_import_state client ost1 FULL
21354         # no locks, no reqs to let the connection idle
21355         cancel_lru_locks osc
21356
21357         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
21358 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
21359         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
21360         wait_osc_import_state client ost1 CONNECTING
21361         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
21362
21363         stat $DIR/$tfile >/dev/null || error "can't stat file"
21364 }
21365 run_test 812 "do not drop reqs generated when imp is going to idle (LU-11951)"
21366
21367 test_814()
21368 {
21369         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
21370         echo -n y >> $DIR/$tfile
21371         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
21372         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
21373 }
21374 run_test 814 "sparse cp works as expected (LU-12361)"
21375
21376 test_815()
21377 {
21378         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
21379         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
21380 }
21381 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
21382
21383 test_816() {
21384         [ "$SHARED_KEY" = true ] &&
21385                 skip "OSC connections never go IDLE with Shared-Keys enabled"
21386
21387         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21388         # ensure ost1 is connected
21389         stat $DIR/$tfile >/dev/null || error "can't stat"
21390         wait_osc_import_state client ost1 FULL
21391         # no locks, no reqs to let the connection idle
21392         cancel_lru_locks osc
21393         lru_resize_disable osc
21394         local before
21395         local now
21396         before=$($LCTL get_param -n \
21397                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
21398
21399         wait_osc_import_state client ost1 IDLE
21400         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
21401         now=$($LCTL get_param -n \
21402               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
21403         [ $before == $now ] || error "lru_size changed $before != $now"
21404 }
21405 run_test 816 "do not reset lru_resize on idle reconnect"
21406
21407 cleanup_817() {
21408         umount $tmpdir
21409         exportfs -u localhost:$DIR/nfsexp
21410         rm -rf $DIR/nfsexp
21411 }
21412
21413 test_817() {
21414         systemctl restart nfs-server.service || skip "failed to restart nfsd"
21415
21416         mkdir -p $DIR/nfsexp
21417         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
21418                 error "failed to export nfs"
21419
21420         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
21421         stack_trap cleanup_817 EXIT
21422
21423         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
21424                 error "failed to mount nfs to $tmpdir"
21425
21426         cp /bin/true $tmpdir
21427         $DIR/nfsexp/true || error "failed to execute 'true' command"
21428 }
21429 run_test 817 "nfsd won't cache write lock for exec file"
21430
21431 test_819a() {
21432         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21433         cancel_lru_locks osc
21434         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
21435         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
21436         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
21437         rm -f $TDIR/$tfile
21438 }
21439 run_test 819a "too big niobuf in read"
21440
21441 test_819b() {
21442         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
21443         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
21444         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21445         cancel_lru_locks osc
21446         sleep 1
21447         rm -f $TDIR/$tfile
21448 }
21449 run_test 819b "too big niobuf in write"
21450
21451 test_818() {
21452         mkdir $DIR/$tdir
21453         $LFS setstripe -c1 -i0 $DIR/$tfile
21454         $LFS setstripe -c1 -i1 $DIR/$tfile
21455         stop $SINGLEMDS
21456         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
21457         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
21458         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
21459                 error "start $SINGLEMDS failed"
21460         rm -rf $DIR/$tdir
21461 }
21462 run_test 818 "unlink with failed llog"
21463
21464 #
21465 # tests that do cleanup/setup should be run at the end
21466 #
21467
21468 test_900() {
21469         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21470         local ls
21471
21472         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
21473         $LCTL set_param fail_loc=0x903
21474
21475         cancel_lru_locks MGC
21476
21477         FAIL_ON_ERROR=true cleanup
21478         FAIL_ON_ERROR=true setup
21479 }
21480 run_test 900 "umount should not race with any mgc requeue thread"
21481
21482 # LU-13377
21483 test_902() {
21484         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
21485                 skip "client does not have LU-13377 fix"
21486         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
21487         $LCTL set_param fail_loc=0x1415
21488         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21489         cancel_lru_locks osc
21490         rm -f $DIR/$tfile
21491 }
21492 run_test 902 "test short write doesn't hang lustre"
21493
21494 complete $SECONDS
21495 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
21496 check_and_cleanup_lustre
21497 if [ "$I_MOUNTED" != "yes" ]; then
21498         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
21499 fi
21500 exit_status