Whamcloud - gitweb
LU-10038 test: improve error reporting in sanity test_133g()
[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 3561 5188
12 ALWAYS_EXCEPT="                42a    42b      42c     45   68b $SANITY_EXCEPT"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054 LU-10199
16 ALWAYS_EXCEPT="  407     253     312     56xb     $ALWAYS_EXCEPT"
17
18 # Check Grants after these tests
19 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
20
21 is_sles11()                                             # LU-4341
22 {
23         if [ -r /etc/SuSE-release ]
24         then
25                 local vers=$(grep VERSION /etc/SuSE-release | awk '{print $3}')
26                 local patchlev=$(grep PATCHLEVEL /etc/SuSE-release |
27                         awk '{ print $3 }')
28                 if [ $vers -eq 11 ] && [ $patchlev -ge 3 ]; then
29                         return 0
30                 fi
31         fi
32         return 1
33 }
34
35 if is_sles11; then                                      # LU-4341
36         ALWAYS_EXCEPT="$ALWAYS_EXCEPT 170"
37 fi
38
39 SRCDIR=$(cd $(dirname $0); echo $PWD)
40 export PATH=$PATH:/sbin
41
42 TMP=${TMP:-/tmp}
43 OSC=${OSC:-"osc"}
44
45 CC=${CC:-cc}
46 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
47 CREATETEST=${CREATETEST:-createtest}
48 LFS=${LFS:-lfs}
49 LFIND=${LFIND:-"$LFS find"}
50 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
51 LCTL=${LCTL:-lctl}
52 OPENFILE=${OPENFILE:-openfile}
53 OPENUNLINK=${OPENUNLINK:-openunlink}
54 export MULTIOP=${MULTIOP:-multiop}
55 READS=${READS:-"reads"}
56 MUNLINK=${MUNLINK:-munlink}
57 SOCKETSERVER=${SOCKETSERVER:-socketserver}
58 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
59 MEMHOG=${MEMHOG:-memhog}
60 DIRECTIO=${DIRECTIO:-directio}
61 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
62 STRIPES_PER_OBJ=-1
63 CHECK_GRANT=${CHECK_GRANT:-"yes"}
64 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
65 export PARALLEL=${PARALLEL:-"no"}
66
67 export NAME=${NAME:-local}
68
69 SAVE_PWD=$PWD
70
71 CLEANUP=${CLEANUP:-:}
72 SETUP=${SETUP:-:}
73 TRACE=${TRACE:-""}
74 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
75 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
76 . $LUSTRE/tests/test-framework.sh
77 init_test_env $@
78 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
79 init_logging
80
81 #                                  5          12          (min)"
82 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
83
84 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
85         # bug number for skipped test: LU-1957
86         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
87         #                                               13    (min)"
88         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
89 fi
90
91 FAIL_ON_ERROR=false
92
93 cleanup() {
94         echo -n "cln.."
95         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
96         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
97 }
98 setup() {
99         echo -n "mnt.."
100         load_modules
101         setupall || exit 10
102         echo "done"
103 }
104
105 check_swap_layouts_support()
106 {
107         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
108                 { skip "Does not support layout lock."; return 0; }
109         return 1
110 }
111
112 check_and_setup_lustre
113 DIR=${DIR:-$MOUNT}
114 assert_DIR
115
116 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
117         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
118 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
119
120 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
121 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
122 rm -rf $DIR/[Rdfs][0-9]*
123
124 # $RUNAS_ID may get set incorrectly somewhere else
125 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
126
127 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
128
129 build_test_filter
130
131 if [ "${ONLY}" = "MOUNT" ] ; then
132         echo "Lustre is up, please go on"
133         exit
134 fi
135
136 echo "preparing for tests involving mounts"
137 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
138 touch $EXT2_DEV
139 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
140 echo # add a newline after mke2fs.
141
142 umask 077
143
144 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
145 lctl set_param debug=-1 2> /dev/null || true
146 test_0a() {
147         touch $DIR/$tfile
148         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
149         rm $DIR/$tfile
150         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
151 }
152 run_test 0a "touch; rm ====================="
153
154 test_0b() {
155         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
156         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
157 }
158 run_test 0b "chmod 0755 $DIR ============================="
159
160 test_0c() {
161         $LCTL get_param mdc.*.import | grep "state: FULL" ||
162                 error "import not FULL"
163         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
164                 error "bad target"
165 }
166 run_test 0c "check import proc"
167
168 test_1() {
169         test_mkdir $DIR/$tdir
170         test_mkdir $DIR/$tdir/d2
171         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
172         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
173         rmdir $DIR/$tdir/d2
174         rmdir $DIR/$tdir
175         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
176 }
177 run_test 1 "mkdir; remkdir; rmdir"
178
179 test_2() {
180         test_mkdir $DIR/$tdir
181         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
182         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
183         rm -r $DIR/$tdir
184         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
185 }
186 run_test 2 "mkdir; touch; rmdir; check file"
187
188 test_3() {
189         test_mkdir $DIR/$tdir
190         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
191         touch $DIR/$tdir/$tfile
192         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
193         rm -r $DIR/$tdir
194         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
195 }
196 run_test 3 "mkdir; touch; rmdir; check dir"
197
198 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
199 test_4() {
200         test_mkdir -i 1 $DIR/$tdir
201
202         touch $DIR/$tdir/$tfile ||
203                 error "Create file under remote directory failed"
204
205         rmdir $DIR/$tdir &&
206                 error "Expect error removing in-use dir $DIR/$tdir"
207
208         test -d $DIR/$tdir || error "Remote directory disappeared"
209
210         rm -rf $DIR/$tdir || error "remove remote dir error"
211 }
212 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
213
214 test_5() {
215         test_mkdir $DIR/$tdir
216         test_mkdir $DIR/$tdir/d2
217         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
218         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
219         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
220 }
221 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
222
223 test_6a() {
224         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
225         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
226         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
227                 error "$tfile does not have perm 0666 or UID $UID"
228         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
229         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
230                 error "$tfile should be 0666 and owned by UID $UID"
231 }
232 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
233
234 test_6c() {
235         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
236         touch $DIR/$tfile
237         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
238         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
239                 error "$tfile should be owned by UID $RUNAS_ID"
240         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
241         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
242                 error "$tfile should be owned by UID $RUNAS_ID"
243 }
244 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
245
246 test_6e() {
247         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
248         touch $DIR/$tfile
249         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
250         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
251                 error "$tfile should be owned by GID $UID"
252         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
253         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
254                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
255 }
256 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
257
258 test_6g() {
259         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
260         test_mkdir $DIR/$tdir
261         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
262         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
263         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
264         test_mkdir $DIR/$tdir/d/subdir
265         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
266                 error "$tdir/d/subdir should be GID $RUNAS_GID"
267         if [[ $MDSCOUNT -gt 1 ]]; then
268                 # check remote dir sgid inherite
269                 $LFS mkdir -i 0 $DIR/$tdir.local ||
270                         error "mkdir $tdir.local failed"
271                 chmod g+s $DIR/$tdir.local ||
272                         error "chmod $tdir.local failed"
273                 chgrp $RUNAS_GID $DIR/$tdir.local ||
274                         error "chgrp $tdir.local failed"
275                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
276                         error "mkdir $tdir.remote failed"
277                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
278                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
279                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
280                         error "$tdir.remote should be mode 02755"
281         fi
282 }
283 run_test 6g "verify new dir in sgid dir inherits group"
284
285 test_6h() { # bug 7331
286         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
287         touch $DIR/$tfile || error "touch failed"
288         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
289         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
290                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
291         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
292                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
293 }
294 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
295
296 test_7a() {
297         test_mkdir $DIR/$tdir
298         $MCREATE $DIR/$tdir/$tfile
299         chmod 0666 $DIR/$tdir/$tfile
300         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
301                 error "$tdir/$tfile should be mode 0666"
302 }
303 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
304
305 test_7b() {
306         if [ ! -d $DIR/$tdir ]; then
307                 test_mkdir $DIR/$tdir
308         fi
309         $MCREATE $DIR/$tdir/$tfile
310         echo -n foo > $DIR/$tdir/$tfile
311         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
312         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
313 }
314 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
315
316 test_8() {
317         test_mkdir $DIR/$tdir
318         touch $DIR/$tdir/$tfile
319         chmod 0666 $DIR/$tdir/$tfile
320         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
321                 error "$tfile mode not 0666"
322 }
323 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
324
325 test_9() {
326         test_mkdir $DIR/$tdir
327         test_mkdir $DIR/$tdir/d2
328         test_mkdir $DIR/$tdir/d2/d3
329         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
330 }
331 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
332
333 test_10() {
334         test_mkdir $DIR/$tdir
335         test_mkdir $DIR/$tdir/d2
336         touch $DIR/$tdir/d2/$tfile
337         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
338                 error "$tdir/d2/$tfile not a file"
339 }
340 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
341
342 test_11() {
343         test_mkdir $DIR/$tdir
344         test_mkdir $DIR/$tdir/d2
345         chmod 0666 $DIR/$tdir/d2
346         chmod 0705 $DIR/$tdir/d2
347         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
348                 error "$tdir/d2 mode not 0705"
349 }
350 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
351
352 test_12() {
353         test_mkdir $DIR/$tdir
354         touch $DIR/$tdir/$tfile
355         chmod 0666 $DIR/$tdir/$tfile
356         chmod 0654 $DIR/$tdir/$tfile
357         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
358                 error "$tdir/d2 mode not 0654"
359 }
360 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
361
362 test_13() {
363         test_mkdir $DIR/$tdir
364         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
365         >  $DIR/$tdir/$tfile
366         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
367                 error "$tdir/$tfile size not 0 after truncate"
368 }
369 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
370
371 test_14() {
372         test_mkdir $DIR/$tdir
373         touch $DIR/$tdir/$tfile
374         rm $DIR/$tdir/$tfile
375         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
376 }
377 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
378
379 test_15() {
380         test_mkdir $DIR/$tdir
381         touch $DIR/$tdir/$tfile
382         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
383         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
384                 error "$tdir/${tfile_2} not a file after rename"
385         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
386 }
387 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
388
389 test_16() {
390         test_mkdir $DIR/$tdir
391         touch $DIR/$tdir/$tfile
392         rm -rf $DIR/$tdir/$tfile
393         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
394 }
395 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
396
397 test_17a() {
398         test_mkdir $DIR/$tdir
399         touch $DIR/$tdir/$tfile
400         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
401         ls -l $DIR/$tdir
402         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
403                 error "$tdir/l-exist not a symlink"
404         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
405                 error "$tdir/l-exist not referencing a file"
406         rm -f $DIR/$tdir/l-exist
407         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
408 }
409 run_test 17a "symlinks: create, remove (real)"
410
411 test_17b() {
412         test_mkdir $DIR/$tdir
413         ln -s no-such-file $DIR/$tdir/l-dangle
414         ls -l $DIR/$tdir
415         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
416                 error "$tdir/l-dangle not referencing no-such-file"
417         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
418                 error "$tdir/l-dangle not referencing non-existent file"
419         rm -f $DIR/$tdir/l-dangle
420         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
421 }
422 run_test 17b "symlinks: create, remove (dangling)"
423
424 test_17c() { # bug 3440 - don't save failed open RPC for replay
425         test_mkdir $DIR/$tdir
426         ln -s foo $DIR/$tdir/$tfile
427         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
428 }
429 run_test 17c "symlinks: open dangling (should return error)"
430
431 test_17d() {
432         test_mkdir $DIR/$tdir
433         ln -s foo $DIR/$tdir/$tfile
434         touch $DIR/$tdir/$tfile || error "creating to new symlink"
435 }
436 run_test 17d "symlinks: create dangling"
437
438 test_17e() {
439         test_mkdir $DIR/$tdir
440         local foo=$DIR/$tdir/$tfile
441         ln -s $foo $foo || error "create symlink failed"
442         ls -l $foo || error "ls -l failed"
443         ls $foo && error "ls not failed" || true
444 }
445 run_test 17e "symlinks: create recursive symlink (should return error)"
446
447 test_17f() {
448         test_mkdir $DIR/$tdir
449         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
450         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
451         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
452         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
453         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
454         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
455         ls -l  $DIR/$tdir
456 }
457 run_test 17f "symlinks: long and very long symlink name"
458
459 # str_repeat(S, N) generate a string that is string S repeated N times
460 str_repeat() {
461         local s=$1
462         local n=$2
463         local ret=''
464         while [ $((n -= 1)) -ge 0 ]; do
465                 ret=$ret$s
466         done
467         echo $ret
468 }
469
470 # Long symlinks and LU-2241
471 test_17g() {
472         test_mkdir $DIR/$tdir
473         local TESTS="59 60 61 4094 4095"
474
475         # Fix for inode size boundary in 2.1.4
476         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
477                 TESTS="4094 4095"
478
479         # Patch not applied to 2.2 or 2.3 branches
480         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
481         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
482                 TESTS="4094 4095"
483
484         # skip long symlink name for rhel6.5.
485         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
486         grep -q '6.5' /etc/redhat-release &>/dev/null &&
487                 TESTS="59 60 61 4062 4063"
488
489         for i in $TESTS; do
490                 local SYMNAME=$(str_repeat 'x' $i)
491                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
492                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
493         done
494 }
495 run_test 17g "symlinks: really long symlink name and inode boundaries"
496
497 test_17h() { #bug 17378
498         remote_mds_nodsh && skip "remote MDS with nodsh" && return
499         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
500         local mdt_idx
501         test_mkdir $DIR/$tdir
502         if [[ $MDSCOUNT -gt 1 ]]; then
503                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
504         else
505                 mdt_idx=0
506         fi
507         $LFS setstripe -c -1 $DIR/$tdir
508         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
509         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
510         touch $DIR/$tdir/$tfile || true
511 }
512 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
513
514 test_17i() { #bug 20018
515         remote_mds_nodsh && skip "remote MDS with nodsh" && return
516         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
517         test_mkdir -c1 $DIR/$tdir
518         local foo=$DIR/$tdir/$tfile
519         local mdt_idx
520         if [[ $MDSCOUNT -gt 1 ]]; then
521                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
522         else
523                 mdt_idx=0
524         fi
525         ln -s $foo $foo || error "create symlink failed"
526 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
527         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
528         ls -l $foo && error "error not detected"
529         return 0
530 }
531 run_test 17i "don't panic on short symlink"
532
533 test_17k() { #bug 22301
534         [[ -z "$(which rsync 2>/dev/null)" ]] &&
535                 skip "no rsync command" && return 0
536         rsync --help | grep -q xattr ||
537                 skip_env "$(rsync --version | head -n1) does not support xattrs"
538         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
539         test_mkdir $DIR/$tdir
540         test_mkdir $DIR/$tdir.new
541         touch $DIR/$tdir/$tfile
542         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
543         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
544                 error "rsync failed with xattrs enabled"
545 }
546 run_test 17k "symlinks: rsync with xattrs enabled"
547
548 test_17l() { # LU-279
549         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
550                 skip "no getfattr command" && return 0
551         test_mkdir $DIR/$tdir
552         touch $DIR/$tdir/$tfile
553         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
554         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
555                 # -h to not follow symlinks. -m '' to list all the xattrs.
556                 # grep to remove first line: '# file: $path'.
557                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
558                 do
559                         lgetxattr_size_check $path $xattr ||
560                                 error "lgetxattr_size_check $path $xattr failed"
561                 done
562         done
563 }
564 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
565
566 # LU-1540
567 test_17m() {
568         local short_sym="0123456789"
569         local wdir=$DIR/$tdir
570         local i
571
572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
573         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
574         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
575                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
576
577         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
578                 skip "ldiskfs only test" && return 0
579
580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
581
582         test_mkdir $wdir
583         long_sym=$short_sym
584         # create a long symlink file
585         for ((i = 0; i < 4; ++i)); do
586                 long_sym=${long_sym}${long_sym}
587         done
588
589         echo "create 512 short and long symlink files under $wdir"
590         for ((i = 0; i < 256; ++i)); do
591                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
592                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
593         done
594
595         echo "erase them"
596         rm -f $wdir/*
597         sync
598         wait_delete_completed
599
600         echo "recreate the 512 symlink files with a shorter string"
601         for ((i = 0; i < 512; ++i)); do
602                 # rewrite the symlink file with a shorter string
603                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
604                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
605         done
606
607         local mds_index=$(($($LFS getstripe -M $wdir) + 1))
608         local devname=$(mdsdevname $mds_index)
609
610         echo "stop and checking mds${mds_index}:"
611         # e2fsck should not return error
612         stop mds${mds_index}
613         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
614         rc=$?
615
616         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
617                 error "start mds${mds_index} failed"
618         df $MOUNT > /dev/null 2>&1
619         [ $rc -eq 0 ] ||
620                 error "e2fsck detected error for short/long symlink: rc=$rc"
621 }
622 run_test 17m "run e2fsck against MDT which contains short/long symlink"
623
624 check_fs_consistency_17n() {
625         local mdt_index
626         local rc=0
627
628         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
629         # so it only check MDT1/MDT2 instead of all of MDTs.
630         for mdt_index in 1 2; do
631                 local devname=$(mdsdevname $mdt_index)
632                 # e2fsck should not return error
633                 stop mds${mdt_index}
634                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
635                         rc=$((rc + $?))
636
637                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
638                         error "mount mds$mdt_index failed"
639                 df $MOUNT > /dev/null 2>&1
640         done
641         return $rc
642 }
643
644 test_17n() {
645         local i
646
647         remote_mds_nodsh && skip "remote MDS with nodsh" && return
648         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
649         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
650                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
651
652         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
653                 skip "ldiskfs only test" && return 0
654
655         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
656
657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
658
659         test_mkdir $DIR/$tdir
660         for ((i=0; i<10; i++)); do
661                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
662                         error "create remote dir error $i"
663                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
664                         error "create files under remote dir failed $i"
665         done
666
667         check_fs_consistency_17n ||
668                 error "e2fsck report error after create files under remote dir"
669
670         for ((i = 0; i < 10; i++)); do
671                 rm -rf $DIR/$tdir/remote_dir_${i} ||
672                         error "destroy remote dir error $i"
673         done
674
675         check_fs_consistency_17n ||
676                 error "e2fsck report error after unlink files under remote dir"
677
678         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
679                 skip "lustre < 2.4.50 does not support migrate mv " && return
680
681         for ((i = 0; i < 10; i++)); do
682                 mkdir -p $DIR/$tdir/remote_dir_${i}
683                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
684                         error "create files under remote dir failed $i"
685                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
686                         error "migrate remote dir error $i"
687         done
688         check_fs_consistency_17n || error "e2fsck report error after migration"
689
690         for ((i = 0; i < 10; i++)); do
691                 rm -rf $DIR/$tdir/remote_dir_${i} ||
692                         error "destroy remote dir error $i"
693         done
694
695         check_fs_consistency_17n || error "e2fsck report error after unlink"
696 }
697 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
698
699 test_17o() {
700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
701         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
702                 skip "Need MDS version at least 2.3.64" && return
703
704         local wdir=$DIR/${tdir}o
705         local mdt_index
706         local rc=0
707
708         test_mkdir $wdir
709         touch $wdir/$tfile
710         mdt_index=$($LFS getstripe -M $wdir/$tfile)
711         mdt_index=$((mdt_index + 1))
712
713         cancel_lru_locks mdc
714         #fail mds will wait the failover finish then set
715         #following fail_loc to avoid interfer the recovery process.
716         fail mds${mdt_index}
717
718         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
719         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
720         ls -l $wdir/$tfile && rc=1
721         do_facet mds${mdt_index} lctl set_param fail_loc=0
722         [[ $rc -eq 0 ]] || error "stat file should fail"
723 }
724 run_test 17o "stat file with incompat LMA feature"
725
726 test_18() {
727         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
728         ls $DIR || error "Failed to ls $DIR: $?"
729 }
730 run_test 18 "touch .../f ; ls ... =============================="
731
732 test_19a() {
733         touch $DIR/$tfile
734         ls -l $DIR
735         rm $DIR/$tfile
736         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
737 }
738 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
739
740 test_19b() {
741         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
742 }
743 run_test 19b "ls -l .../f19 (should return error) =============="
744
745 test_19c() {
746         [ $RUNAS_ID -eq $UID ] &&
747                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
748         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
749 }
750 run_test 19c "$RUNAS touch .../f19 (should return error) =="
751
752 test_19d() {
753         cat $DIR/f19 && error || true
754 }
755 run_test 19d "cat .../f19 (should return error) =============="
756
757 test_20() {
758         touch $DIR/$tfile
759         rm $DIR/$tfile
760         touch $DIR/$tfile
761         rm $DIR/$tfile
762         touch $DIR/$tfile
763         rm $DIR/$tfile
764         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
765 }
766 run_test 20 "touch .../f ; ls -l ..."
767
768 test_21() {
769         test_mkdir $DIR/$tdir
770         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
771         ln -s dangle $DIR/$tdir/link
772         echo foo >> $DIR/$tdir/link
773         cat $DIR/$tdir/dangle
774         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
775         $CHECKSTAT -f -t file $DIR/$tdir/link ||
776                 error "$tdir/link not linked to a file"
777 }
778 run_test 21 "write to dangling link"
779
780 test_22() {
781         local wdir=$DIR/$tdir
782         test_mkdir $wdir
783         chown $RUNAS_ID:$RUNAS_GID $wdir
784         (cd $wdir || error "cd $wdir failed";
785                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
786                 $RUNAS tar xf -)
787         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
788         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
789         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
790                 error "checkstat -u failed"
791 }
792 run_test 22 "unpack tar archive as non-root user"
793
794 # was test_23
795 test_23a() {
796         test_mkdir $DIR/$tdir
797         local file=$DIR/$tdir/$tfile
798
799         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
800         openfile -f O_CREAT:O_EXCL $file &&
801                 error "$file recreate succeeded" || true
802 }
803 run_test 23a "O_CREAT|O_EXCL in subdir"
804
805 test_23b() { # bug 18988
806         test_mkdir $DIR/$tdir
807         local file=$DIR/$tdir/$tfile
808
809         rm -f $file
810         echo foo > $file || error "write filed"
811         echo bar >> $file || error "append filed"
812         $CHECKSTAT -s 8 $file || error "wrong size"
813         rm $file
814 }
815 run_test 23b "O_APPEND check"
816
817 # rename sanity
818 test_24a() {
819         echo '-- same directory rename'
820         test_mkdir $DIR/$tdir
821         touch $DIR/$tdir/$tfile.1
822         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
823         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
824 }
825 run_test 24a "rename file to non-existent target"
826
827 test_24b() {
828         test_mkdir $DIR/$tdir
829         touch $DIR/$tdir/$tfile.{1,2}
830         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
831         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
832         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
833 }
834 run_test 24b "rename file to existing target"
835
836 test_24c() {
837         test_mkdir $DIR/$tdir
838         test_mkdir $DIR/$tdir/d$testnum.1
839         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
840         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
841         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
842 }
843 run_test 24c "rename directory to non-existent target"
844
845 test_24d() {
846         test_mkdir -c1 $DIR/$tdir
847         test_mkdir -c1 $DIR/$tdir/d$testnum.1
848         test_mkdir -c1 $DIR/$tdir/d$testnum.2
849         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
850         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
851         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
852 }
853 run_test 24d "rename directory to existing target"
854
855 test_24e() {
856         echo '-- cross directory renames --'
857         test_mkdir $DIR/R5a
858         test_mkdir $DIR/R5b
859         touch $DIR/R5a/f
860         mv $DIR/R5a/f $DIR/R5b/g
861         $CHECKSTAT -a $DIR/R5a/f || error
862         $CHECKSTAT -t file $DIR/R5b/g || error
863 }
864 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
865
866 test_24f() {
867         test_mkdir $DIR/R6a
868         test_mkdir $DIR/R6b
869         touch $DIR/R6a/f $DIR/R6b/g
870         mv $DIR/R6a/f $DIR/R6b/g
871         $CHECKSTAT -a $DIR/R6a/f || error
872         $CHECKSTAT -t file $DIR/R6b/g || error
873 }
874 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
875
876 test_24g() {
877         test_mkdir $DIR/R7a
878         test_mkdir $DIR/R7b
879         test_mkdir $DIR/R7a/d
880         mv $DIR/R7a/d $DIR/R7b/e
881         $CHECKSTAT -a $DIR/R7a/d || error
882         $CHECKSTAT -t dir $DIR/R7b/e || error
883 }
884 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
885
886 test_24h() {
887         test_mkdir -c1 $DIR/R8a
888         test_mkdir -c1 $DIR/R8b
889         test_mkdir -c1 $DIR/R8a/d
890         test_mkdir -c1 $DIR/R8b/e
891         mrename $DIR/R8a/d $DIR/R8b/e
892         $CHECKSTAT -a $DIR/R8a/d || error
893         $CHECKSTAT -t dir $DIR/R8b/e || error
894 }
895 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
896
897 test_24i() {
898         echo "-- rename error cases"
899         test_mkdir $DIR/R9
900         test_mkdir $DIR/R9/a
901         touch $DIR/R9/f
902         mrename $DIR/R9/f $DIR/R9/a
903         $CHECKSTAT -t file $DIR/R9/f || error
904         $CHECKSTAT -t dir  $DIR/R9/a || error
905         $CHECKSTAT -a $DIR/R9/a/f || error
906 }
907 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
908
909 test_24j() {
910         test_mkdir $DIR/R10
911         mrename $DIR/R10/f $DIR/R10/g
912         $CHECKSTAT -t dir $DIR/R10 || error
913         $CHECKSTAT -a $DIR/R10/f || error
914         $CHECKSTAT -a $DIR/R10/g || error
915 }
916 run_test 24j "source does not exist ============================"
917
918 test_24k() {
919         test_mkdir $DIR/R11a
920         test_mkdir $DIR/R11a/d
921         touch $DIR/R11a/f
922         mv $DIR/R11a/f $DIR/R11a/d
923         $CHECKSTAT -a $DIR/R11a/f || error
924         $CHECKSTAT -t file $DIR/R11a/d/f || error
925 }
926 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
927
928 # bug 2429 - rename foo foo foo creates invalid file
929 test_24l() {
930         f="$DIR/f24l"
931         $MULTIOP $f OcNs || error
932 }
933 run_test 24l "Renaming a file to itself ========================"
934
935 test_24m() {
936         f="$DIR/f24m"
937         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
938         # on ext3 this does not remove either the source or target files
939         # though the "expected" operation would be to remove the source
940         $CHECKSTAT -t file ${f} || error "${f} missing"
941         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
942 }
943 run_test 24m "Renaming a file to a hard link to itself ========="
944
945 test_24n() {
946     f="$DIR/f24n"
947     # this stats the old file after it was renamed, so it should fail
948     touch ${f}
949     $CHECKSTAT ${f}
950     mv ${f} ${f}.rename
951     $CHECKSTAT ${f}.rename
952     $CHECKSTAT -a ${f}
953 }
954 run_test 24n "Statting the old file after renaming (Posix rename 2)"
955
956 test_24o() {
957         test_mkdir $DIR/$tdir
958         rename_many -s random -v -n 10 $DIR/$tdir
959 }
960 run_test 24o "rename of files during htree split"
961
962 test_24p() {
963         test_mkdir $DIR/R12a
964         test_mkdir $DIR/R12b
965         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
966         mrename $DIR/R12a $DIR/R12b
967         $CHECKSTAT -a $DIR/R12a || error
968         $CHECKSTAT -t dir $DIR/R12b || error
969         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
970         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
971 }
972 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
973
974 cleanup_multiop_pause() {
975         trap 0
976         kill -USR1 $MULTIPID
977 }
978
979 test_24q() {
980         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
981         test_mkdir $DIR/R13a
982         test_mkdir $DIR/R13b
983         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
984         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
985         MULTIPID=$!
986
987         trap cleanup_multiop_pause EXIT
988         mrename $DIR/R13a $DIR/R13b
989         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
990         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
991         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
992         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
993         cleanup_multiop_pause
994         wait $MULTIPID || error "multiop close failed"
995 }
996 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
997
998 test_24r() { #bug 3789
999         test_mkdir $DIR/R14a
1000         test_mkdir $DIR/R14a/b
1001         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1002         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1003         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1004 }
1005 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1006
1007 test_24s() {
1008         test_mkdir $DIR/R15a
1009         test_mkdir $DIR/R15a/b
1010         test_mkdir $DIR/R15a/b/c
1011         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1012         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1013         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1014 }
1015 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1016 test_24t() {
1017         test_mkdir $DIR/R16a
1018         test_mkdir $DIR/R16a/b
1019         test_mkdir $DIR/R16a/b/c
1020         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1021         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1022         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1023 }
1024 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1025
1026 test_24u() { # bug12192
1027         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1028         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1029 }
1030 run_test 24u "create stripe file"
1031
1032 page_size() {
1033         local size
1034         size=$(getconf PAGE_SIZE 2>/dev/null)
1035         echo -n ${size:-4096}
1036 }
1037
1038 simple_cleanup_common() {
1039         local rc=0
1040         trap 0
1041         [ -z "$DIR" -o -z "$tdir" ] && return 0
1042
1043         local start=$SECONDS
1044         rm -rf $DIR/$tdir
1045         rc=$?
1046         wait_delete_completed
1047         echo "cleanup time $((SECONDS - start))"
1048         return $rc
1049 }
1050
1051 max_pages_per_rpc() {
1052         local mdtname="$(printf "MDT%04x" ${1:-0})"
1053         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1054 }
1055
1056 test_24v() {
1057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1058         local nrfiles=${COUNT:-100000}
1059         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1060         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && nrfiles=${COUNT:-10000}
1061
1062         local fname="$DIR/$tdir/$tfile"
1063         test_mkdir "$(dirname $fname)"
1064         # assume MDT0000 has the fewest inodes
1065         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1066         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1067         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1068
1069         trap simple_cleanup_common EXIT
1070
1071         createmany -m "$fname" $nrfiles
1072
1073         cancel_lru_locks mdc
1074         lctl set_param mdc.*.stats clear
1075
1076         # was previously test_24D: LU-6101
1077         # readdir() returns correct number of entries after cursor reload
1078         local num_ls=$(ls $DIR/$tdir | wc -l)
1079         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1080         local num_all=$(ls -a $DIR/$tdir | wc -l)
1081         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1082              $num_all -ne $((nrfiles + 2)) ]; then
1083                 error "Expected $nrfiles files, got $num_ls " \
1084                         "($num_uniq unique $num_all .&..)"
1085         fi
1086         # LU-5 large readdir
1087         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1088         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1089         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1090         # take into account of overhead in lu_dirpage header and end mark in
1091         # each page, plus one in rpc_num calculation.
1092         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1093         local page_entries=$((($(page_size) - 24) / dirent_size))
1094         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1095         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1096         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1097         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1098         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1099         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1100                 error "large readdir doesn't take effect: " \
1101                       "$mds_readpage should be about $rpc_max"
1102
1103         simple_cleanup_common
1104 }
1105 run_test 24v "list large directory (test hash collision, b=17560)"
1106
1107 test_24w() { # bug21506
1108         SZ1=234852
1109         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1110         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1111         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1112         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1113         [[ "$SZ1" -eq "$SZ2" ]] ||
1114                 error "Error reading at the end of the file $tfile"
1115 }
1116 run_test 24w "Reading a file larger than 4Gb"
1117
1118 test_24x() {
1119         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1120
1121         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1122                 skip "Need MDS version at least 2.7.56" && return
1123
1124         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1125         local MDTIDX=1
1126         local remote_dir=$DIR/$tdir/remote_dir
1127
1128         test_mkdir $DIR/$tdir
1129         $LFS mkdir -i $MDTIDX $remote_dir ||
1130                 error "create remote directory failed"
1131
1132         test_mkdir $DIR/$tdir/src_dir
1133         touch $DIR/$tdir/src_file
1134         test_mkdir $remote_dir/tgt_dir
1135         touch $remote_dir/tgt_file
1136
1137         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1138                 error "rename dir cross MDT failed!"
1139
1140         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1141                 error "rename file cross MDT failed!"
1142
1143         touch $DIR/$tdir/ln_file
1144         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1145                 error "ln file cross MDT failed"
1146
1147         rm -rf $DIR/$tdir || error "Can not delete directories"
1148 }
1149 run_test 24x "cross MDT rename/link"
1150
1151 test_24y() {
1152         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1154         local remote_dir=$DIR/$tdir/remote_dir
1155         local mdtidx=1
1156
1157         test_mkdir $DIR/$tdir
1158         $LFS mkdir -i $mdtidx $remote_dir ||
1159                    error "create remote directory failed"
1160
1161         test_mkdir $remote_dir/src_dir
1162         touch $remote_dir/src_file
1163         test_mkdir $remote_dir/tgt_dir
1164         touch $remote_dir/tgt_file
1165
1166         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1167                 error "rename subdir in the same remote dir failed!"
1168
1169         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1170                 error "rename files in the same remote dir failed!"
1171
1172         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1173                 error "link files in the same remote dir failed!"
1174
1175         rm -rf $DIR/$tdir || error "Can not delete directories"
1176 }
1177 run_test 24y "rename/link on the same dir should succeed"
1178
1179 test_24A() { # LU-3182
1180         local NFILES=5000
1181
1182         rm -rf $DIR/$tdir
1183         test_mkdir $DIR/$tdir
1184         trap simple_cleanup_common EXIT
1185         createmany -m $DIR/$tdir/$tfile $NFILES
1186         local t=$(ls $DIR/$tdir | wc -l)
1187         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1188         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1189         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1190                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1191         fi
1192
1193         simple_cleanup_common || error "Can not delete directories"
1194 }
1195 run_test 24A "readdir() returns correct number of entries."
1196
1197 test_24B() { # LU-4805
1198         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1199         local count
1200
1201         test_mkdir $DIR/$tdir
1202         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1203                 error "create striped dir failed"
1204
1205         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1206         [ $count -eq 2 ] || error "Expected 2, got $count"
1207
1208         touch $DIR/$tdir/striped_dir/a
1209
1210         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1211         [ $count -eq 3 ] || error "Expected 3, got $count"
1212
1213         touch $DIR/$tdir/striped_dir/.f
1214
1215         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1216         [ $count -eq 4 ] || error "Expected 4, got $count"
1217
1218         rm -rf $DIR/$tdir || error "Can not delete directories"
1219 }
1220 run_test 24B "readdir for striped dir return correct number of entries"
1221
1222 test_24C() {
1223         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1224
1225         mkdir $DIR/$tdir
1226         mkdir $DIR/$tdir/d0
1227         mkdir $DIR/$tdir/d1
1228
1229         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1230                 error "create striped dir failed"
1231
1232         cd $DIR/$tdir/d0/striped_dir
1233
1234         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1235         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1236         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1237
1238         [ "$d0_ino" = "$parent_ino" ] ||
1239                 error ".. wrong, expect $d0_ino, get $parent_ino"
1240
1241         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1242                 error "mv striped dir failed"
1243
1244         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1245
1246         [ "$d1_ino" = "$parent_ino" ] ||
1247                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1248 }
1249 run_test 24C "check .. in striped dir"
1250
1251 test_24E() {
1252         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1254
1255         mkdir -p $DIR/$tdir
1256         mkdir $DIR/$tdir/src_dir
1257         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1258                 error "create remote source failed"
1259
1260         touch $DIR/$tdir/src_dir/src_child/a
1261
1262         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1263                 error "create remote target dir failed"
1264
1265         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1266                 error "create remote target child failed"
1267
1268         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1269                 error "rename dir cross MDT failed!"
1270
1271         find $DIR/$tdir
1272
1273         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1274                 error "src_child still exists after rename"
1275
1276         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1277                 error "missing file(a) after rename"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24E "cross MDT rename/link"
1282
1283 test_25a() {
1284         echo '== symlink sanity ============================================='
1285
1286         test_mkdir $DIR/d25
1287         ln -s d25 $DIR/s25
1288         touch $DIR/s25/foo || error
1289 }
1290 run_test 25a "create file in symlinked directory ==============="
1291
1292 test_25b() {
1293         [ ! -d $DIR/d25 ] && test_25a
1294         $CHECKSTAT -t file $DIR/s25/foo || error
1295 }
1296 run_test 25b "lookup file in symlinked directory ==============="
1297
1298 test_26a() {
1299         test_mkdir $DIR/d26
1300         test_mkdir $DIR/d26/d26-2
1301         ln -s d26/d26-2 $DIR/s26
1302         touch $DIR/s26/foo || error
1303 }
1304 run_test 26a "multiple component symlink ======================="
1305
1306 test_26b() {
1307         test_mkdir -p $DIR/$tdir/d26-2
1308         ln -s $tdir/d26-2/foo $DIR/s26-2
1309         touch $DIR/s26-2 || error
1310 }
1311 run_test 26b "multiple component symlink at end of lookup ======"
1312
1313 test_26c() {
1314         test_mkdir $DIR/d26.2
1315         touch $DIR/d26.2/foo
1316         ln -s d26.2 $DIR/s26.2-1
1317         ln -s s26.2-1 $DIR/s26.2-2
1318         ln -s s26.2-2 $DIR/s26.2-3
1319         chmod 0666 $DIR/s26.2-3/foo
1320 }
1321 run_test 26c "chain of symlinks"
1322
1323 # recursive symlinks (bug 439)
1324 test_26d() {
1325         ln -s d26-3/foo $DIR/d26-3
1326 }
1327 run_test 26d "create multiple component recursive symlink"
1328
1329 test_26e() {
1330         [ ! -h $DIR/d26-3 ] && test_26d
1331         rm $DIR/d26-3
1332 }
1333 run_test 26e "unlink multiple component recursive symlink"
1334
1335 # recursive symlinks (bug 7022)
1336 test_26f() {
1337         test_mkdir $DIR/$tdir
1338         test_mkdir $DIR/$tdir/$tfile
1339         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1340         test_mkdir -p lndir/bar1
1341         test_mkdir $DIR/$tdir/$tfile/$tfile
1342         cd $tfile                || error "cd $tfile failed"
1343         ln -s .. dotdot          || error "ln dotdot failed"
1344         ln -s dotdot/lndir lndir || error "ln lndir failed"
1345         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1346         output=`ls $tfile/$tfile/lndir/bar1`
1347         [ "$output" = bar1 ] && error "unexpected output"
1348         rm -r $tfile             || error "rm $tfile failed"
1349         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1350 }
1351 run_test 26f "rm -r of a directory which has recursive symlink"
1352
1353 test_27a() {
1354         test_mkdir $DIR/$tdir
1355         $LFS getstripe $DIR/$tdir
1356         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1357         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1358         cp /etc/hosts $DIR/$tdir/$tfile || error
1359 }
1360 run_test 27a "one stripe file"
1361
1362 test_27b() {
1363         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1364         test_mkdir $DIR/$tdir
1365         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1366         $LFS getstripe -c $DIR/$tdir/$tfile
1367         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1368                 error "two-stripe file doesn't have two stripes"
1369
1370         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1371 }
1372 run_test 27b "create and write to two stripe file"
1373
1374 test_27d() {
1375         test_mkdir $DIR/$tdir
1376         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1377                 error "setstripe failed"
1378         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1379         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1380 }
1381 run_test 27d "create file with default settings"
1382
1383 test_27e() {
1384         # LU-5839 adds check for existed layout before setting it
1385         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1386                 skip "Need MDS version at least 2.7.56" && return
1387         test_mkdir $DIR/$tdir
1388         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1389         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1390         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1391 }
1392 run_test 27e "setstripe existing file (should return error)"
1393
1394 test_27f() {
1395         test_mkdir $DIR/$tdir
1396         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1397                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1398         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1399                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1400         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1401         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1402 }
1403 run_test 27f "setstripe with bad stripe size (should return error)"
1404
1405 test_27g() {
1406         test_mkdir $DIR/$tdir
1407         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1408         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1409                 error "$DIR/$tdir/$tfile has object"
1410 }
1411 run_test 27g "$LFS getstripe with no objects"
1412
1413 test_27i() {
1414         test_mkdir $DIR/$tdir
1415         touch $DIR/$tdir/$tfile || error "touch failed"
1416         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1417                 error "missing objects"
1418 }
1419 run_test 27i "$LFS getstripe with some objects"
1420
1421 test_27j() {
1422         test_mkdir $DIR/$tdir
1423         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1424                 error "setstripe failed" || true
1425 }
1426 run_test 27j "setstripe with bad stripe offset (should return error)"
1427
1428 test_27k() { # bug 2844
1429         test_mkdir $DIR/$tdir
1430         local file=$DIR/$tdir/$tfile
1431         local ll_max_blksize=$((4 * 1024 * 1024))
1432         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1433         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1434         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1435         dd if=/dev/zero of=$file bs=4k count=1
1436         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1437         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1438 }
1439 run_test 27k "limit i_blksize for broken user apps"
1440
1441 test_27l() {
1442         mcreate $DIR/$tfile || error "creating file"
1443         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1444                 error "setstripe should have failed" || true
1445 }
1446 run_test 27l "check setstripe permissions (should return error)"
1447
1448 test_27m() {
1449         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1450
1451         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1452                    head -n1)
1453         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1454                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1455                 return
1456         fi
1457         trap simple_cleanup_common EXIT
1458         test_mkdir $DIR/$tdir
1459         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1460         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1461                 error "dd should fill OST0"
1462         i=2
1463         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1464                 i=$((i + 1))
1465                 [ $i -gt 256 ] && break
1466         done
1467         i=$((i + 1))
1468         touch $DIR/$tdir/$tfile.$i
1469         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1470             awk '{print $1}'| grep -w "0") ] &&
1471                 error "OST0 was full but new created file still use it"
1472         i=$((i + 1))
1473         touch $DIR/$tdir/$tfile.$i
1474         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1475             awk '{print $1}'| grep -w "0") ] &&
1476                 error "OST0 was full but new created file still use it"
1477         simple_cleanup_common
1478 }
1479 run_test 27m "create file while OST0 was full"
1480
1481 sleep_maxage() {
1482         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1483                       head -n 1 | awk '{ print $1 * 2 }')
1484         sleep $delay
1485 }
1486
1487 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1488 # if the OST isn't full anymore.
1489 reset_enospc() {
1490         local OSTIDX=${1:-""}
1491
1492         local list=$(comma_list $(osts_nodes))
1493         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1494
1495         do_nodes $list lctl set_param fail_loc=0
1496         sync    # initiate all OST_DESTROYs from MDS to OST
1497         sleep_maxage
1498 }
1499
1500 exhaust_precreations() {
1501         local OSTIDX=$1
1502         local FAILLOC=$2
1503         local FAILIDX=${3:-$OSTIDX}
1504         local ofacet=ost$((OSTIDX + 1))
1505
1506         test_mkdir -p -c1 $DIR/$tdir
1507         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1508         local mfacet=mds$((mdtidx + 1))
1509         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1510
1511         local OST=$(ostname_from_index $OSTIDX)
1512
1513         # on the mdt's osc
1514         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1515         local last_id=$(do_facet $mfacet lctl get_param -n \
1516                         osc.$mdtosc_proc1.prealloc_last_id)
1517         local next_id=$(do_facet $mfacet lctl get_param -n \
1518                         osc.$mdtosc_proc1.prealloc_next_id)
1519
1520         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1521         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1522
1523         test_mkdir -p $DIR/$tdir/${OST}
1524         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1525 #define OBD_FAIL_OST_ENOSPC              0x215
1526         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1527         echo "Creating to objid $last_id on ost $OST..."
1528         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1529         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1530         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1531         sleep_maxage
1532 }
1533
1534 exhaust_all_precreations() {
1535         local i
1536         for (( i=0; i < OSTCOUNT; i++ )) ; do
1537                 exhaust_precreations $i $1 -1
1538         done
1539 }
1540
1541 test_27n() {
1542         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1544         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1545         remote_ost_nodsh && skip "remote OST with nodsh" && return
1546
1547         reset_enospc
1548         rm -f $DIR/$tdir/$tfile
1549         exhaust_precreations 0 0x80000215
1550         $LFS setstripe -c -1 $DIR/$tdir
1551         touch $DIR/$tdir/$tfile || error
1552         $LFS getstripe $DIR/$tdir/$tfile
1553         reset_enospc
1554 }
1555 run_test 27n "create file with some full OSTs"
1556
1557 test_27o() {
1558         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1560         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1561         remote_ost_nodsh && skip "remote OST with nodsh" && return
1562
1563         reset_enospc
1564         rm -f $DIR/$tdir/$tfile
1565         exhaust_all_precreations 0x215
1566
1567         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1568
1569         reset_enospc
1570         rm -rf $DIR/$tdir/*
1571 }
1572 run_test 27o "create file with all full OSTs (should error)"
1573
1574 test_27p() {
1575         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1576         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1577         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1578         remote_ost_nodsh && skip "remote OST with nodsh" && return
1579
1580         reset_enospc
1581         rm -f $DIR/$tdir/$tfile
1582         test_mkdir $DIR/$tdir
1583
1584         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1585         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1586         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1587
1588         exhaust_precreations 0 0x80000215
1589         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1590         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1591         $LFS getstripe $DIR/$tdir/$tfile
1592
1593         reset_enospc
1594 }
1595 run_test 27p "append to a truncated file with some full OSTs"
1596
1597 test_27q() {
1598         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1600         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1601         remote_ost_nodsh && skip "remote OST with nodsh" && return
1602
1603         reset_enospc
1604         rm -f $DIR/$tdir/$tfile
1605
1606         test_mkdir $DIR/$tdir
1607         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1608         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1609                 error "truncate $DIR/$tdir/$tfile failed"
1610         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1611
1612         exhaust_all_precreations 0x215
1613
1614         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1615         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1616
1617         reset_enospc
1618 }
1619 run_test 27q "append to truncated file with all OSTs full (should error)"
1620
1621 test_27r() {
1622         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1623         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1624         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1625         remote_ost_nodsh && skip "remote OST with nodsh" && return
1626
1627         reset_enospc
1628         rm -f $DIR/$tdir/$tfile
1629         exhaust_precreations 0 0x80000215
1630
1631         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile # && error
1632
1633         reset_enospc
1634 }
1635 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1636
1637 test_27s() { # bug 10725
1638         test_mkdir $DIR/$tdir
1639         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1640         local stripe_count=0
1641         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1642         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1643                 error "stripe width >= 2^32 succeeded" || true
1644
1645 }
1646 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1647
1648 test_27t() { # bug 10864
1649         WDIR=$(pwd)
1650         WLFS=$(which lfs)
1651         cd $DIR
1652         touch $tfile
1653         $WLFS getstripe $tfile
1654         cd $WDIR
1655 }
1656 run_test 27t "check that utils parse path correctly"
1657
1658 test_27u() { # bug 4900
1659         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1660         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1661         local index
1662         local list=$(comma_list $(mdts_nodes))
1663
1664 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1665         do_nodes $list $LCTL set_param fail_loc=0x139
1666         test_mkdir -p $DIR/$tdir
1667         trap simple_cleanup_common EXIT
1668         createmany -o $DIR/$tdir/t- 1000
1669         do_nodes $list $LCTL set_param fail_loc=0
1670
1671         TLOG=$TMP/$tfile.getstripe
1672         $LFS getstripe $DIR/$tdir > $TLOG
1673         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1674         unlinkmany $DIR/$tdir/t- 1000
1675         trap 0
1676         [[ $OBJS -gt 0 ]] &&
1677                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1678 }
1679 run_test 27u "skip object creation on OSC w/o objects"
1680
1681 test_27v() { # bug 4900
1682         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1683         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1684         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1685         remote_ost_nodsh && skip "remote OST with nodsh" && return
1686
1687         exhaust_all_precreations 0x215
1688         reset_enospc
1689
1690         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1691
1692         touch $DIR/$tdir/$tfile
1693         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1694         # all except ost1
1695         for (( i=1; i < OSTCOUNT; i++ )); do
1696                 do_facet ost$i lctl set_param fail_loc=0x705
1697         done
1698         local START=`date +%s`
1699         createmany -o $DIR/$tdir/$tfile 32
1700
1701         local FINISH=`date +%s`
1702         local TIMEOUT=`lctl get_param -n timeout`
1703         local PROCESS=$((FINISH - START))
1704         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1705                error "$FINISH - $START >= $TIMEOUT / 2"
1706         sleep $((TIMEOUT / 2 - PROCESS))
1707         reset_enospc
1708 }
1709 run_test 27v "skip object creation on slow OST"
1710
1711 test_27w() { # bug 10997
1712         test_mkdir $DIR/$tdir
1713         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1714         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1715                 error "stripe size $size != 65536" || true
1716         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1717                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1718 }
1719 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1720
1721 test_27wa() {
1722         [[ $OSTCOUNT -lt 2 ]] &&
1723                 skip_env "skipping multiple stripe count/offset test" && return
1724
1725         test_mkdir $DIR/$tdir
1726         for i in $(seq 1 $OSTCOUNT); do
1727                 offset=$((i - 1))
1728                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1729                         error "setstripe -c $i -i $offset failed"
1730                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1731                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1732                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1733                 [ $index -ne $offset ] &&
1734                         error "stripe offset $index != $offset" || true
1735         done
1736 }
1737 run_test 27wa "check $LFS setstripe -c -i options"
1738
1739 test_27x() {
1740         remote_ost_nodsh && skip "remote OST with nodsh" && return
1741         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1742         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1743         OFFSET=$(($OSTCOUNT - 1))
1744         OSTIDX=0
1745         local OST=$(ostname_from_index $OSTIDX)
1746
1747         test_mkdir $DIR/$tdir
1748         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1749         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1750         sleep_maxage
1751         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1752         for i in $(seq 0 $OFFSET); do
1753                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1754                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1755                 error "OST0 was degraded but new created file still use it"
1756         done
1757         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1758 }
1759 run_test 27x "create files while OST0 is degraded"
1760
1761 test_27y() {
1762         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1763         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1764         remote_ost_nodsh && skip "remote OST with nodsh" && return
1765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1766
1767         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1768         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1769                 osc.$mdtosc.prealloc_last_id)
1770         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1771                 osc.$mdtosc.prealloc_next_id)
1772         local fcount=$((last_id - next_id))
1773         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1774         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1775
1776         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1777                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1778         local OST_DEACTIVE_IDX=-1
1779         local OSC
1780         local OSTIDX
1781         local OST
1782
1783         for OSC in $MDS_OSCS; do
1784                 OST=$(osc_to_ost $OSC)
1785                 OSTIDX=$(index_from_ostuuid $OST)
1786                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1787                         OST_DEACTIVE_IDX=$OSTIDX
1788                 fi
1789                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1790                         echo $OSC "is Deactivated:"
1791                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1792                 fi
1793         done
1794
1795         OSTIDX=$(index_from_ostuuid $OST)
1796         test_mkdir $DIR/$tdir
1797         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1798
1799         for OSC in $MDS_OSCS; do
1800                 OST=$(osc_to_ost $OSC)
1801                 OSTIDX=$(index_from_ostuuid $OST)
1802                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1803                         echo $OST "is degraded:"
1804                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1805                                                 obdfilter.$OST.degraded=1
1806                 fi
1807         done
1808
1809         sleep_maxage
1810         createmany -o $DIR/$tdir/$tfile $fcount
1811
1812         for OSC in $MDS_OSCS; do
1813                 OST=$(osc_to_ost $OSC)
1814                 OSTIDX=$(index_from_ostuuid $OST)
1815                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1816                         echo $OST "is recovered from degraded:"
1817                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1818                                                 obdfilter.$OST.degraded=0
1819                 else
1820                         do_facet $SINGLEMDS lctl --device %$OSC activate
1821                 fi
1822         done
1823
1824         # all osp devices get activated, hence -1 stripe count restored
1825         local stripe_count=0
1826
1827         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1828         # devices get activated.
1829         sleep_maxage
1830         $LFS setstripe -c -1 $DIR/$tfile
1831         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1832         rm -f $DIR/$tfile
1833         [ $stripe_count -ne $OSTCOUNT ] &&
1834                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1835         return 0
1836 }
1837 run_test 27y "create files while OST0 is degraded and the rest inactive"
1838
1839 check_seq_oid()
1840 {
1841         log "check file $1"
1842
1843         lmm_count=$($GETSTRIPE -c $1)
1844         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1845         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1846
1847         local old_ifs="$IFS"
1848         IFS=$'[:]'
1849         fid=($($LFS path2fid $1))
1850         IFS="$old_ifs"
1851
1852         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1853         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1854
1855         # compare lmm_seq and lu_fid->f_seq
1856         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1857         # compare lmm_object_id and lu_fid->oid
1858         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1859
1860         # check the trusted.fid attribute of the OST objects of the file
1861         local have_obdidx=false
1862         local stripe_nr=0
1863         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1864                 # skip lines up to and including "obdidx"
1865                 [ -z "$obdidx" ] && break
1866                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1867                 $have_obdidx || continue
1868
1869                 local ost=$((obdidx + 1))
1870                 local dev=$(ostdevname $ost)
1871                 local oid_hex
1872
1873                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1874
1875                 seq=$(echo $seq | sed -e "s/^0x//g")
1876                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1877                         oid_hex=$(echo $oid)
1878                 else
1879                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1880                 fi
1881                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1882
1883                 local ff=""
1884                 #
1885                 # Don't unmount/remount the OSTs if we don't need to do that.
1886                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1887                 # update too, until that use mount/ll_decode_filter_fid/mount.
1888                 # Re-enable when debugfs will understand new filter_fid.
1889                 #
1890                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
1891                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1892                                 $dev 2>/dev/null" | grep "parent=")
1893                 fi
1894                 if [ -z "$ff" ]; then
1895                         stop ost$ost
1896                         mount_fstype ost$ost
1897                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1898                                 $(facet_mntpt ost$ost)/$obj_file)
1899                         unmount_fstype ost$ost
1900                         start ost$ost $dev $OST_MOUNT_OPTS
1901                         clients_up
1902                 fi
1903
1904                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1905
1906                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1907
1908                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1909                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1910                 #
1911                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
1912                 #       stripe_size=1048576 component_id=1 component_start=0 \
1913                 #       component_end=33554432
1914                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
1915                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
1916                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
1917                 local ff_pstripe
1918                 if grep -q 'stripe=' <<<$ff; then
1919                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
1920                 else
1921                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1922                         # into f_ver in this case.  See comment on ff_parent.
1923                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
1924                 fi
1925
1926                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1927                 [ $ff_pseq = $lmm_seq ] ||
1928                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1929                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1930                 [ $ff_poid = $lmm_oid ] ||
1931                         error "FF parent OID $ff_poid != $lmm_oid"
1932                 (($ff_pstripe == $stripe_nr)) ||
1933                         error "FF stripe $ff_pstripe != $stripe_nr"
1934
1935                 stripe_nr=$((stripe_nr + 1))
1936                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
1937                         continue
1938                 if grep -q 'stripe_count=' <<<$ff; then
1939                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
1940                                             -e 's/ .*//' <<<$ff)
1941                         [ $lmm_count = $ff_scnt ] ||
1942                                 error "FF stripe count $lmm_count != $ff_scnt"
1943                 fi
1944         done
1945 }
1946
1947 test_27z() {
1948         remote_ost_nodsh && skip "remote OST with nodsh" && return
1949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1950         test_mkdir $DIR/$tdir
1951
1952         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1953                 { error "setstripe -c -1 failed"; return 1; }
1954         # We need to send a write to every object to get parent FID info set.
1955         # This _should_ also work for setattr, but does not currently.
1956         # touch $DIR/$tdir/$tfile-1 ||
1957         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1958                 { error "dd $tfile-1 failed"; return 2; }
1959         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1960                 { error "setstripe -c -1 failed"; return 3; }
1961         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1962                 { error "dd $tfile-2 failed"; return 4; }
1963
1964         # make sure write RPCs have been sent to OSTs
1965         sync; sleep 5; sync
1966
1967         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1968         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1969 }
1970 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1971
1972 test_27A() { # b=19102
1973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1974         local restore_size=$($GETSTRIPE -S $MOUNT)
1975         local restore_count=$($GETSTRIPE -c $MOUNT)
1976         local restore_offset=$($GETSTRIPE -i $MOUNT)
1977         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1978         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1979                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1980         local default_size=$($GETSTRIPE -S $MOUNT)
1981         local default_offset=$($GETSTRIPE -i $MOUNT)
1982         local dsize=$((1024 * 1024))
1983         [ $default_size -eq $dsize ] ||
1984                 error "stripe size $default_size != $dsize"
1985         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1986         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1987 }
1988 run_test 27A "check filesystem-wide default LOV EA values"
1989
1990 test_27B() { # LU-2523
1991         test_mkdir $DIR/$tdir
1992         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1993         touch $DIR/$tdir/f0
1994         # open f1 with O_LOV_DELAY_CREATE
1995         # rename f0 onto f1
1996         # call setstripe ioctl on open file descriptor for f1
1997         # close
1998         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
1999                 $DIR/$tdir/f0
2000
2001         rm -f $DIR/$tdir/f1
2002         # open f1 with O_LOV_DELAY_CREATE
2003         # unlink f1
2004         # call setstripe ioctl on open file descriptor for f1
2005         # close
2006         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2007
2008         # Allow multiop to fail in imitation of NFS's busted semantics.
2009         true
2010 }
2011 run_test 27B "call setstripe on open unlinked file/rename victim"
2012
2013 test_27C() { #LU-2871
2014         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2015
2016         declare -a ost_idx
2017         local index
2018         local found
2019         local i
2020         local j
2021
2022         test_mkdir $DIR/$tdir
2023         cd $DIR/$tdir
2024         for i in $(seq 0 $((OSTCOUNT - 1))); do
2025                 # set stripe across all OSTs starting from OST$i
2026                 $SETSTRIPE -i $i -c -1 $tfile$i
2027                 # get striping information
2028                 ost_idx=($($GETSTRIPE $tfile$i |
2029                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2030                 echo ${ost_idx[@]}
2031
2032                 # check the layout
2033                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2034                         error "${#ost_idx[@]} != $OSTCOUNT"
2035
2036                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2037                         found=0
2038                         for j in $(echo ${ost_idx[@]}); do
2039                                 if [ $index -eq $j ]; then
2040                                         found=1
2041                                         break
2042                                 fi
2043                         done
2044                         [ $found = 1 ] ||
2045                                 error "Can not find $index in ${ost_idx[@]}"
2046                 done
2047         done
2048 }
2049 run_test 27C "check full striping across all OSTs"
2050
2051 test_27D() {
2052         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2053         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2054         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2055         local POOL=${POOL:-testpool}
2056         local first_ost=0
2057         local last_ost=$(($OSTCOUNT - 1))
2058         local ost_step=1
2059         local ost_list=$(seq $first_ost $ost_step $last_ost)
2060         local ost_range="$first_ost $last_ost $ost_step"
2061
2062         if ! combined_mgs_mds ; then
2063                 mount_mgs_client
2064         fi
2065
2066         test_mkdir $DIR/$tdir
2067         pool_add $POOL || error "pool_add failed"
2068         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2069
2070         local skip27D
2071         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2072                 skip27D += "-s 29"
2073         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2074           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2075                 skip27D += "-s 30,31"
2076         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2077                 error "llapi_layout_test failed"
2078
2079         destroy_test_pools || error "destroy test pools failed"
2080
2081         if ! combined_mgs_mds ; then
2082                 umount_mgs_client
2083         fi
2084 }
2085 run_test 27D "validate llapi_layout API"
2086
2087 # Verify that default_easize is increased from its initial value after
2088 # accessing a widely striped file.
2089 test_27E() {
2090         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2091         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2092                 skip "client does not have LU-3338 fix" && return
2093
2094         # 72 bytes is the minimum space required to store striping
2095         # information for a file striped across one OST:
2096         # (sizeof(struct lov_user_md_v3) +
2097         #  sizeof(struct lov_user_ost_data_v1))
2098         local min_easize=72
2099         $LCTL set_param -n llite.*.default_easize $min_easize ||
2100                 error "lctl set_param failed"
2101         local easize=$($LCTL get_param -n llite.*.default_easize)
2102
2103         [ $easize -eq $min_easize ] ||
2104                 error "failed to set default_easize"
2105
2106         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2107                 error "setstripe failed"
2108         cat $DIR/$tfile
2109         rm $DIR/$tfile
2110
2111         easize=$($LCTL get_param -n llite.*.default_easize)
2112
2113         [ $easize -gt $min_easize ] ||
2114                 error "default_easize not updated"
2115 }
2116 run_test 27E "check that default extended attribute size properly increases"
2117
2118 test_27F() { # LU-5346/LU-7975
2119         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2120         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2121                 skip "Need MDS version at least 2.8.51" && return
2122         remote_ost_nodsh && skip "remote OST with nodsh" && return
2123
2124         test_mkdir $DIR/$tdir
2125         rm -f $DIR/$tdir/f0
2126         $SETSTRIPE -c 2 $DIR/$tdir
2127
2128         # stop all OSTs to reproduce situation for LU-7975 ticket
2129         for num in $(seq $OSTCOUNT); do
2130                 stop ost$num
2131         done
2132
2133         # open/create f0 with O_LOV_DELAY_CREATE
2134         # truncate f0 to a non-0 size
2135         # close
2136         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2137
2138         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2139         # open/write it again to force delayed layout creation
2140         cat /etc/hosts > $DIR/$tdir/f0 &
2141         catpid=$!
2142
2143         # restart OSTs
2144         for num in $(seq $OSTCOUNT); do
2145                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2146                         error "ost$num failed to start"
2147         done
2148
2149         wait $catpid || error "cat failed"
2150
2151         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2152         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2153
2154 }
2155 run_test 27F "Client resend delayed layout creation with non-zero size"
2156
2157 # createtest also checks that device nodes are created and
2158 # then visible correctly (#2091)
2159 test_28() { # bug 2091
2160         test_mkdir $DIR/d28
2161         $CREATETEST $DIR/d28/ct || error
2162 }
2163 run_test 28 "create/mknod/mkdir with bad file types ============"
2164
2165 test_29() {
2166         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2167         sync; sleep 1; sync # flush out any dirty pages from previous tests
2168         cancel_lru_locks
2169         test_mkdir $DIR/d29
2170         touch $DIR/d29/foo
2171         log 'first d29'
2172         ls -l $DIR/d29
2173
2174         declare -i LOCKCOUNTORIG=0
2175         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2176                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2177         done
2178         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2179
2180         declare -i LOCKUNUSEDCOUNTORIG=0
2181         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2182                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2183         done
2184
2185         log 'second d29'
2186         ls -l $DIR/d29
2187         log 'done'
2188
2189         declare -i LOCKCOUNTCURRENT=0
2190         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2191                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2192         done
2193
2194         declare -i LOCKUNUSEDCOUNTCURRENT=0
2195         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2196                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2197         done
2198
2199         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2200                 $LCTL set_param -n ldlm.dump_namespaces ""
2201                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2202                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2203                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2204                 return 2
2205         fi
2206         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2207                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2208                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2209                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2210                 return 3
2211         fi
2212 }
2213 run_test 29 "IT_GETATTR regression  ============================"
2214
2215 test_30a() { # was test_30
2216         cp $(which ls) $DIR || cp /bin/ls $DIR
2217         $DIR/ls / || error
2218         rm $DIR/ls
2219 }
2220 run_test 30a "execute binary from Lustre (execve) =============="
2221
2222 test_30b() {
2223         cp `which ls` $DIR || cp /bin/ls $DIR
2224         chmod go+rx $DIR/ls
2225         $RUNAS $DIR/ls / || error
2226         rm $DIR/ls
2227 }
2228 run_test 30b "execute binary from Lustre as non-root ==========="
2229
2230 test_30c() { # b=22376
2231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2232         cp `which ls` $DIR || cp /bin/ls $DIR
2233         chmod a-rw $DIR/ls
2234         cancel_lru_locks mdc
2235         cancel_lru_locks osc
2236         $RUNAS $DIR/ls / || error
2237         rm -f $DIR/ls
2238 }
2239 run_test 30c "execute binary from Lustre without read perms ===="
2240
2241 test_31a() {
2242         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2243         $CHECKSTAT -a $DIR/f31 || error
2244 }
2245 run_test 31a "open-unlink file =================================="
2246
2247 test_31b() {
2248         touch $DIR/f31 || error
2249         ln $DIR/f31 $DIR/f31b || error
2250         $MULTIOP $DIR/f31b Ouc || error
2251         $CHECKSTAT -t file $DIR/f31 || error
2252 }
2253 run_test 31b "unlink file with multiple links while open ======="
2254
2255 test_31c() {
2256         touch $DIR/f31 || error
2257         ln $DIR/f31 $DIR/f31c || error
2258         multiop_bg_pause $DIR/f31 O_uc || return 1
2259         MULTIPID=$!
2260         $MULTIOP $DIR/f31c Ouc
2261         kill -USR1 $MULTIPID
2262         wait $MULTIPID
2263 }
2264 run_test 31c "open-unlink file with multiple links ============="
2265
2266 test_31d() {
2267         opendirunlink $DIR/d31d $DIR/d31d || error
2268         $CHECKSTAT -a $DIR/d31d || error
2269 }
2270 run_test 31d "remove of open directory ========================="
2271
2272 test_31e() { # bug 2904
2273         openfilleddirunlink $DIR/d31e || error
2274 }
2275 run_test 31e "remove of open non-empty directory ==============="
2276
2277 test_31f() { # bug 4554
2278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2279         set -vx
2280         test_mkdir $DIR/d31f
2281         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2282         cp /etc/hosts $DIR/d31f
2283         ls -l $DIR/d31f
2284         $GETSTRIPE $DIR/d31f/hosts
2285         multiop_bg_pause $DIR/d31f D_c || return 1
2286         MULTIPID=$!
2287
2288         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2289         test_mkdir $DIR/d31f
2290         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2291         cp /etc/hosts $DIR/d31f
2292         ls -l $DIR/d31f
2293         $GETSTRIPE $DIR/d31f/hosts
2294         multiop_bg_pause $DIR/d31f D_c || return 1
2295         MULTIPID2=$!
2296
2297         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2298         wait $MULTIPID || error "first opendir $MULTIPID failed"
2299
2300         sleep 6
2301
2302         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2303         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2304         set +vx
2305 }
2306 run_test 31f "remove of open directory with open-unlink file ==="
2307
2308 test_31g() {
2309         echo "-- cross directory link --"
2310         test_mkdir -c1 $DIR/${tdir}ga
2311         test_mkdir -c1 $DIR/${tdir}gb
2312         touch $DIR/${tdir}ga/f
2313         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2314         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2315         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2316         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2317         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2318 }
2319 run_test 31g "cross directory link==============="
2320
2321 test_31h() {
2322         echo "-- cross directory link --"
2323         test_mkdir -c1 $DIR/${tdir}
2324         test_mkdir -c1 $DIR/${tdir}/dir
2325         touch $DIR/${tdir}/f
2326         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2327         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2328         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2329         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2330         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2331 }
2332 run_test 31h "cross directory link under child==============="
2333
2334 test_31i() {
2335         echo "-- cross directory link --"
2336         test_mkdir -c1 $DIR/$tdir
2337         test_mkdir -c1 $DIR/$tdir/dir
2338         touch $DIR/$tdir/dir/f
2339         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2340         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2341         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2342         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2343         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2344 }
2345 run_test 31i "cross directory link under parent==============="
2346
2347 test_31j() {
2348         test_mkdir -c1 -p $DIR/$tdir
2349         test_mkdir -c1 -p $DIR/$tdir/dir1
2350         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2351         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2352         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2353         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2354         return 0
2355 }
2356 run_test 31j "link for directory==============="
2357
2358 test_31k() {
2359         test_mkdir -c1 -p $DIR/$tdir
2360         touch $DIR/$tdir/s
2361         touch $DIR/$tdir/exist
2362         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2363         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2364         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2365         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2366         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2367         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2368         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2369         return 0
2370 }
2371 run_test 31k "link to file: the same, non-existing, dir==============="
2372
2373 test_31m() {
2374         mkdir $DIR/d31m
2375         touch $DIR/d31m/s
2376         mkdir $DIR/d31m2
2377         touch $DIR/d31m2/exist
2378         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2379         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2380         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2381         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2382         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2383         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2384         return 0
2385 }
2386 run_test 31m "link to file: the same, non-existing, dir==============="
2387
2388 test_31n() {
2389         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2390         nlink=$(stat --format=%h $DIR/$tfile)
2391         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2392         local fd=$(free_fd)
2393         local cmd="exec $fd<$DIR/$tfile"
2394         eval $cmd
2395         cmd="exec $fd<&-"
2396         trap "eval $cmd" EXIT
2397         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2398         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2399         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2400         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2401         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2402         eval $cmd
2403 }
2404 run_test 31n "check link count of unlinked file"
2405
2406 link_one() {
2407         local TEMPNAME=$(mktemp $1_XXXXXX)
2408         mlink $TEMPNAME $1 2> /dev/null &&
2409                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2410         munlink $TEMPNAME
2411 }
2412
2413 test_31o() { # LU-2901
2414         test_mkdir $DIR/$tdir
2415         for LOOP in $(seq 100); do
2416                 rm -f $DIR/$tdir/$tfile*
2417                 for THREAD in $(seq 8); do
2418                         link_one $DIR/$tdir/$tfile.$LOOP &
2419                 done
2420                 wait
2421                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2422                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2423                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2424                         break || true
2425         done
2426 }
2427 run_test 31o "duplicate hard links with same filename"
2428
2429 test_31p() {
2430         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2431
2432         test_mkdir $DIR/$tdir
2433         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2434         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2435
2436         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2437                 error "open unlink test1 failed"
2438         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2439                 error "open unlink test2 failed"
2440
2441         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2442                 error "test1 still exists"
2443         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2444                 error "test2 still exists"
2445 }
2446 run_test 31p "remove of open striped directory"
2447
2448 cleanup_test32_mount() {
2449         local rc=0
2450         trap 0
2451         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2452         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2453         losetup -d $loopdev || true
2454         rm -rf $DIR/$tdir
2455         return $rc
2456 }
2457
2458 test_32a() {
2459         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2460         echo "== more mountpoints and symlinks ================="
2461         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2462         trap cleanup_test32_mount EXIT
2463         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2464         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2465         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2466         cleanup_test32_mount
2467 }
2468 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2469
2470 test_32b() {
2471         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2472         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2473         trap cleanup_test32_mount EXIT
2474         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2475         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2476         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2477         cleanup_test32_mount
2478 }
2479 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2480
2481 test_32c() {
2482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2483         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2484         trap cleanup_test32_mount EXIT
2485         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2486         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2487         test_mkdir -p $DIR/$tdir/d2/test_dir
2488         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2489         cleanup_test32_mount
2490 }
2491 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2492
2493 test_32d() {
2494         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2495         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2496         trap cleanup_test32_mount EXIT
2497         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2498         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2499         test_mkdir -p $DIR/$tdir/d2/test_dir
2500         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2501         cleanup_test32_mount
2502 }
2503 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2504
2505 test_32e() {
2506         rm -fr $DIR/$tdir
2507         test_mkdir -p $DIR/$tdir/tmp
2508         local tmp_dir=$DIR/$tdir/tmp
2509         ln -s $DIR/$tdir $tmp_dir/symlink11
2510         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2511         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2512         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2513 }
2514 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2515
2516 test_32f() {
2517         rm -fr $DIR/$tdir
2518         test_mkdir -p $DIR/$tdir/tmp
2519         local tmp_dir=$DIR/$tdir/tmp
2520         ln -s $DIR/$tdir $tmp_dir/symlink11
2521         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2522         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2523         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2524 }
2525 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2526
2527 test_32g() {
2528         local tmp_dir=$DIR/$tdir/tmp
2529         test_mkdir -p $tmp_dir
2530         test_mkdir $DIR/${tdir}2
2531         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2532         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2533         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2534         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2535         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2536         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2537 }
2538 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2539
2540 test_32h() {
2541         rm -fr $DIR/$tdir $DIR/${tdir}2
2542         tmp_dir=$DIR/$tdir/tmp
2543         test_mkdir -p $tmp_dir
2544         test_mkdir $DIR/${tdir}2
2545         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2546         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2547         ls $tmp_dir/symlink12 || error "listing symlink12"
2548         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2549 }
2550 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2551
2552 test_32i() {
2553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2554         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2555         trap cleanup_test32_mount EXIT
2556         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2557         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2558         touch $DIR/$tdir/test_file
2559         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2560         cleanup_test32_mount
2561 }
2562 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2563
2564 test_32j() {
2565         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2566         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2567         trap cleanup_test32_mount EXIT
2568         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2569         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2570         touch $DIR/$tdir/test_file
2571         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2572         cleanup_test32_mount
2573 }
2574 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2575
2576 test_32k() {
2577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2578         rm -fr $DIR/$tdir
2579         trap cleanup_test32_mount EXIT
2580         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2581         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2582         test_mkdir -p $DIR/$tdir/d2
2583         touch $DIR/$tdir/d2/test_file || error
2584         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2585         cleanup_test32_mount
2586 }
2587 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2588
2589 test_32l() {
2590         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2591         rm -fr $DIR/$tdir
2592         trap cleanup_test32_mount EXIT
2593         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2594         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2595         test_mkdir -p $DIR/$tdir/d2
2596         touch $DIR/$tdir/d2/test_file
2597         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2598         cleanup_test32_mount
2599 }
2600 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2601
2602 test_32m() {
2603         rm -fr $DIR/d32m
2604         test_mkdir -p $DIR/d32m/tmp
2605         TMP_DIR=$DIR/d32m/tmp
2606         ln -s $DIR $TMP_DIR/symlink11
2607         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2608         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2609         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2610 }
2611 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2612
2613 test_32n() {
2614         rm -fr $DIR/d32n
2615         test_mkdir -p $DIR/d32n/tmp
2616         TMP_DIR=$DIR/d32n/tmp
2617         ln -s $DIR $TMP_DIR/symlink11
2618         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2619         ls -l $DIR/d32n/tmp/symlink11  || error
2620         ls -l $DIR/d32n/symlink01 || error
2621 }
2622 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2623
2624 test_32o() {
2625         touch $DIR/$tfile
2626         test_mkdir -p $DIR/d32o/tmp
2627         TMP_DIR=$DIR/d32o/tmp
2628         ln -s $DIR/$tfile $TMP_DIR/symlink12
2629         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2630         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2631         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2632         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2633         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2634 }
2635 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2636
2637 test_32p() {
2638         log 32p_1
2639         rm -fr $DIR/d32p
2640         log 32p_2
2641         rm -f $DIR/$tfile
2642         log 32p_3
2643         touch $DIR/$tfile
2644         log 32p_4
2645         test_mkdir -p $DIR/d32p/tmp
2646         log 32p_5
2647         TMP_DIR=$DIR/d32p/tmp
2648         log 32p_6
2649         ln -s $DIR/$tfile $TMP_DIR/symlink12
2650         log 32p_7
2651         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2652         log 32p_8
2653         cat $DIR/d32p/tmp/symlink12 || error
2654         log 32p_9
2655         cat $DIR/d32p/symlink02 || error
2656         log 32p_10
2657 }
2658 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2659
2660 test_32q() {
2661         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2662         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2663         trap cleanup_test32_mount EXIT
2664         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2665         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2666         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2667         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2668         cleanup_test32_mount
2669 }
2670 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2671
2672 test_32r() {
2673         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2674         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2675         trap cleanup_test32_mount EXIT
2676         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2677         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2678         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2679         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2680         cleanup_test32_mount
2681 }
2682 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2683
2684 test_33aa() {
2685         rm -f $DIR/$tfile
2686         touch $DIR/$tfile
2687         chmod 444 $DIR/$tfile
2688         chown $RUNAS_ID $DIR/$tfile
2689         log 33_1
2690         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2691         log 33_2
2692 }
2693 run_test 33aa "write file with mode 444 (should return error)"
2694
2695 test_33a() {
2696         rm -fr $DIR/$tdir
2697         test_mkdir $DIR/$tdir
2698         chown $RUNAS_ID $DIR/$tdir
2699         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2700                 error "$RUNAS create $tdir/$tfile failed"
2701         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2702                 error "open RDWR" || true
2703 }
2704 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2705
2706 test_33b() {
2707         rm -fr $DIR/$tdir
2708         test_mkdir $DIR/$tdir
2709         chown $RUNAS_ID $DIR/$tdir
2710         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2711 }
2712 run_test 33b "test open file with malformed flags (No panic)"
2713
2714 test_33c() {
2715         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2716         local ostnum
2717         local ostname
2718         local write_bytes
2719         local all_zeros
2720
2721         remote_ost_nodsh && skip "remote OST with nodsh" && return
2722         all_zeros=:
2723         rm -fr $DIR/$tdir
2724         test_mkdir $DIR/$tdir
2725         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2726
2727         sync
2728         for ostnum in $(seq $OSTCOUNT); do
2729                 # test-framework's OST numbering is one-based, while Lustre's
2730                 # is zero-based
2731                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2732                 # Parsing llobdstat's output sucks; we could grep the /proc
2733                 # path, but that's likely to not be as portable as using the
2734                 # llobdstat utility.  So we parse lctl output instead.
2735                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2736                         obdfilter/$ostname/stats |
2737                         awk '/^write_bytes/ {print $7}' )
2738                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2739                 if (( ${write_bytes:-0} > 0 ))
2740                 then
2741                         all_zeros=false
2742                         break;
2743                 fi
2744         done
2745
2746         $all_zeros || return 0
2747
2748         # Write four bytes
2749         echo foo > $DIR/$tdir/bar
2750         # Really write them
2751         sync
2752
2753         # Total up write_bytes after writing.  We'd better find non-zeros.
2754         for ostnum in $(seq $OSTCOUNT); do
2755                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2756                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2757                         obdfilter/$ostname/stats |
2758                         awk '/^write_bytes/ {print $7}' )
2759                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2760                 if (( ${write_bytes:-0} > 0 ))
2761                 then
2762                         all_zeros=false
2763                         break;
2764                 fi
2765         done
2766
2767         if $all_zeros
2768         then
2769                 for ostnum in $(seq $OSTCOUNT); do
2770                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2771                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2772                         do_facet ost$ostnum lctl get_param -n \
2773                                 obdfilter/$ostname/stats
2774                 done
2775                 error "OST not keeping write_bytes stats (b22312)"
2776         fi
2777 }
2778 run_test 33c "test llobdstat and write_bytes"
2779
2780 test_33d() {
2781         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2783         local MDTIDX=1
2784         local remote_dir=$DIR/$tdir/remote_dir
2785
2786         test_mkdir $DIR/$tdir
2787         $LFS mkdir -i $MDTIDX $remote_dir ||
2788                 error "create remote directory failed"
2789
2790         touch $remote_dir/$tfile
2791         chmod 444 $remote_dir/$tfile
2792         chown $RUNAS_ID $remote_dir/$tfile
2793
2794         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2795
2796         chown $RUNAS_ID $remote_dir
2797         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2798                                         error "create" || true
2799         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2800                                     error "open RDWR" || true
2801         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2802 }
2803 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2804
2805 test_33e() {
2806         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2807
2808         mkdir $DIR/$tdir
2809
2810         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2811         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2812         mkdir $DIR/$tdir/local_dir
2813
2814         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2815         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2816         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2817
2818         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2819                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2820
2821         rmdir $DIR/$tdir/* || error "rmdir failed"
2822
2823         umask 777
2824         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2825         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2826         mkdir $DIR/$tdir/local_dir
2827
2828         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2829         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2830         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2831
2832         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2833                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2834
2835         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2836
2837         umask 000
2838         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2839         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2840         mkdir $DIR/$tdir/local_dir
2841
2842         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2843         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2844         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2845
2846         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2847                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2848 }
2849 run_test 33e "mkdir and striped directory should have same mode"
2850
2851 cleanup_33f() {
2852         trap 0
2853         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2854 }
2855
2856 test_33f() {
2857         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2858         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2859
2860         mkdir $DIR/$tdir
2861         chmod go+rwx $DIR/$tdir
2862         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2863         trap cleanup_33f EXIT
2864
2865         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2866                 error "cannot create striped directory"
2867
2868         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2869                 error "cannot create files in striped directory"
2870
2871         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2872                 error "cannot remove files in striped directory"
2873
2874         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2875                 error "cannot remove striped directory"
2876
2877         cleanup_33f
2878 }
2879 run_test 33f "nonroot user can create, access, and remove a striped directory"
2880
2881 test_33g() {
2882         mkdir -p $DIR/$tdir/dir2
2883
2884         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2885         echo $err
2886         [[ $err =~ "exists" ]] || error "Not exists error"
2887 }
2888 run_test 33g "nonroot user create already existing root created file"
2889
2890 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2891 test_34a() {
2892         rm -f $DIR/f34
2893         $MCREATE $DIR/f34 || error
2894         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2895         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2896         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2897         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2898 }
2899 run_test 34a "truncate file that has not been opened ==========="
2900
2901 test_34b() {
2902         [ ! -f $DIR/f34 ] && test_34a
2903         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2904         $OPENFILE -f O_RDONLY $DIR/f34
2905         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2906         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2907 }
2908 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2909
2910 test_34c() {
2911         [ ! -f $DIR/f34 ] && test_34a
2912         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2913         $OPENFILE -f O_RDWR $DIR/f34
2914         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2915         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2916 }
2917 run_test 34c "O_RDWR opening file-with-size works =============="
2918
2919 test_34d() {
2920         [ ! -f $DIR/f34 ] && test_34a
2921         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2922         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2923         rm $DIR/f34
2924 }
2925 run_test 34d "write to sparse file ============================="
2926
2927 test_34e() {
2928         rm -f $DIR/f34e
2929         $MCREATE $DIR/f34e || error
2930         $TRUNCATE $DIR/f34e 1000 || error
2931         $CHECKSTAT -s 1000 $DIR/f34e || error
2932         $OPENFILE -f O_RDWR $DIR/f34e
2933         $CHECKSTAT -s 1000 $DIR/f34e || error
2934 }
2935 run_test 34e "create objects, some with size and some without =="
2936
2937 test_34f() { # bug 6242, 6243
2938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2939         SIZE34F=48000
2940         rm -f $DIR/f34f
2941         $MCREATE $DIR/f34f || error
2942         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2943         dd if=$DIR/f34f of=$TMP/f34f
2944         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2945         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2946         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2947         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2948         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2949 }
2950 run_test 34f "read from a file with no objects until EOF ======="
2951
2952 test_34g() {
2953         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2954         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2955         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2956         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2957         cancel_lru_locks osc
2958         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2959                 error "wrong size after lock cancel"
2960
2961         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2962         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2963                 error "expanding truncate failed"
2964         cancel_lru_locks osc
2965         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2966                 error "wrong expanded size after lock cancel"
2967 }
2968 run_test 34g "truncate long file ==============================="
2969
2970 test_34h() {
2971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2972         local gid=10
2973         local sz=1000
2974
2975         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2976         sync # Flush the cache so that multiop below does not block on cache
2977              # flush when getting the group lock
2978         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2979         MULTIPID=$!
2980
2981         # Since just timed wait is not good enough, let's do a sync write
2982         # that way we are sure enough time for a roundtrip + processing
2983         # passed + 2 seconds of extra margin.
2984         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2985         rm $DIR/${tfile}-1
2986         sleep 2
2987
2988         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2989                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2990                 kill -9 $MULTIPID
2991         fi
2992         wait $MULTIPID
2993         local nsz=`stat -c %s $DIR/$tfile`
2994         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
2995 }
2996 run_test 34h "ftruncate file under grouplock should not block"
2997
2998 test_35a() {
2999         cp /bin/sh $DIR/f35a
3000         chmod 444 $DIR/f35a
3001         chown $RUNAS_ID $DIR/f35a
3002         $RUNAS $DIR/f35a && error || true
3003         rm $DIR/f35a
3004 }
3005 run_test 35a "exec file with mode 444 (should return and not leak)"
3006
3007 test_36a() {
3008         rm -f $DIR/f36
3009         utime $DIR/f36 || error
3010 }
3011 run_test 36a "MDS utime check (mknod, utime)"
3012
3013 test_36b() {
3014         echo "" > $DIR/f36
3015         utime $DIR/f36 || error
3016 }
3017 run_test 36b "OST utime check (open, utime)"
3018
3019 test_36c() {
3020         rm -f $DIR/d36/f36
3021         test_mkdir $DIR/d36
3022         chown $RUNAS_ID $DIR/d36
3023         $RUNAS utime $DIR/d36/f36 || error
3024 }
3025 run_test 36c "non-root MDS utime check (mknod, utime)"
3026
3027 test_36d() {
3028         [ ! -d $DIR/d36 ] && test_36c
3029         echo "" > $DIR/d36/f36
3030         $RUNAS utime $DIR/d36/f36 || error
3031 }
3032 run_test 36d "non-root OST utime check (open, utime)"
3033
3034 test_36e() {
3035         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3036         test_mkdir $DIR/$tdir
3037         touch $DIR/$tdir/$tfile
3038         $RUNAS utime $DIR/$tdir/$tfile &&
3039                 error "utime worked, expected failure" || true
3040 }
3041 run_test 36e "utime on non-owned file (should return error)"
3042
3043 subr_36fh() {
3044         local fl="$1"
3045         local LANG_SAVE=$LANG
3046         local LC_LANG_SAVE=$LC_LANG
3047         export LANG=C LC_LANG=C # for date language
3048
3049         DATESTR="Dec 20  2000"
3050         test_mkdir $DIR/$tdir
3051         lctl set_param fail_loc=$fl
3052         date; date +%s
3053         cp /etc/hosts $DIR/$tdir/$tfile
3054         sync & # write RPC generated with "current" inode timestamp, but delayed
3055         sleep 1
3056         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3057         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3058         cancel_lru_locks osc
3059         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3060         date; date +%s
3061         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3062                 echo "BEFORE: $LS_BEFORE" && \
3063                 echo "AFTER : $LS_AFTER" && \
3064                 echo "WANT  : $DATESTR" && \
3065                 error "$DIR/$tdir/$tfile timestamps changed" || true
3066
3067         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3068 }
3069
3070 test_36f() {
3071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3072         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3073         subr_36fh "0x80000214"
3074 }
3075 run_test 36f "utime on file racing with OST BRW write =========="
3076
3077 test_36g() {
3078         remote_ost_nodsh && skip "remote OST with nodsh" && return
3079         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3080         local fmd_max_age
3081         local fmd_before
3082         local fmd_after
3083
3084         test_mkdir $DIR/$tdir
3085         fmd_max_age=$(do_facet ost1 \
3086                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3087                 head -n 1")
3088
3089         fmd_before=$(do_facet ost1 \
3090                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3091         touch $DIR/$tdir/$tfile
3092         sleep $((fmd_max_age + 12))
3093         fmd_after=$(do_facet ost1 \
3094                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3095
3096         echo "fmd_before: $fmd_before"
3097         echo "fmd_after: $fmd_after"
3098         [[ $fmd_after -gt $fmd_before ]] &&
3099                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3100                 error "fmd didn't expire after ping" || true
3101 }
3102 run_test 36g "filter mod data cache expiry ====================="
3103
3104 test_36h() {
3105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3106         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3107         subr_36fh "0x80000227"
3108 }
3109 run_test 36h "utime on file racing with OST BRW write =========="
3110
3111 test_36i() {
3112         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3113
3114         test_mkdir $DIR/$tdir
3115         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3116
3117         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3118         local new_mtime=$((mtime + 200))
3119
3120         #change Modify time of striped dir
3121         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3122                         error "change mtime failed"
3123
3124         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3125
3126         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3127 }
3128 run_test 36i "change mtime on striped directory"
3129
3130 # test_37 - duplicate with tests 32q 32r
3131
3132 test_38() {
3133         local file=$DIR/$tfile
3134         touch $file
3135         openfile -f O_DIRECTORY $file
3136         local RC=$?
3137         local ENOTDIR=20
3138         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3139         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3140 }
3141 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3142
3143 test_39a() { # was test_39
3144         touch $DIR/$tfile
3145         touch $DIR/${tfile}2
3146 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3147 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3148 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3149         sleep 2
3150         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3151         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3152                 echo "mtime"
3153                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3154                 echo "atime"
3155                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3156                 echo "ctime"
3157                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3158                 error "O_TRUNC didn't change timestamps"
3159         fi
3160 }
3161 run_test 39a "mtime changed on create"
3162
3163 test_39b() {
3164         test_mkdir -c1 $DIR/$tdir
3165         cp -p /etc/passwd $DIR/$tdir/fopen
3166         cp -p /etc/passwd $DIR/$tdir/flink
3167         cp -p /etc/passwd $DIR/$tdir/funlink
3168         cp -p /etc/passwd $DIR/$tdir/frename
3169         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3170
3171         sleep 1
3172         echo "aaaaaa" >> $DIR/$tdir/fopen
3173         echo "aaaaaa" >> $DIR/$tdir/flink
3174         echo "aaaaaa" >> $DIR/$tdir/funlink
3175         echo "aaaaaa" >> $DIR/$tdir/frename
3176
3177         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3178         local link_new=`stat -c %Y $DIR/$tdir/flink`
3179         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3180         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3181
3182         cat $DIR/$tdir/fopen > /dev/null
3183         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3184         rm -f $DIR/$tdir/funlink2
3185         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3186
3187         for (( i=0; i < 2; i++ )) ; do
3188                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3189                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3190                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3191                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3192
3193                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3194                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3195                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3196                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3197
3198                 cancel_lru_locks osc
3199                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3200         done
3201 }
3202 run_test 39b "mtime change on open, link, unlink, rename  ======"
3203
3204 # this should be set to past
3205 TEST_39_MTIME=`date -d "1 year ago" +%s`
3206
3207 # bug 11063
3208 test_39c() {
3209         touch $DIR1/$tfile
3210         sleep 2
3211         local mtime0=`stat -c %Y $DIR1/$tfile`
3212
3213         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3214         local mtime1=`stat -c %Y $DIR1/$tfile`
3215         [ "$mtime1" = $TEST_39_MTIME ] || \
3216                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3217
3218         local d1=`date +%s`
3219         echo hello >> $DIR1/$tfile
3220         local d2=`date +%s`
3221         local mtime2=`stat -c %Y $DIR1/$tfile`
3222         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3223                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3224
3225         mv $DIR1/$tfile $DIR1/$tfile-1
3226
3227         for (( i=0; i < 2; i++ )) ; do
3228                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3229                 [ "$mtime2" = "$mtime3" ] || \
3230                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3231
3232                 cancel_lru_locks osc
3233                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3234         done
3235 }
3236 run_test 39c "mtime change on rename ==========================="
3237
3238 # bug 21114
3239 test_39d() {
3240         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3241         touch $DIR1/$tfile
3242
3243         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3244
3245         for (( i=0; i < 2; i++ )) ; do
3246                 local mtime=`stat -c %Y $DIR1/$tfile`
3247                 [ $mtime = $TEST_39_MTIME ] || \
3248                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3249
3250                 cancel_lru_locks osc
3251                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3252         done
3253 }
3254 run_test 39d "create, utime, stat =============================="
3255
3256 # bug 21114
3257 test_39e() {
3258         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3259         touch $DIR1/$tfile
3260         local mtime1=`stat -c %Y $DIR1/$tfile`
3261
3262         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3263
3264         for (( i=0; i < 2; i++ )) ; do
3265                 local mtime2=`stat -c %Y $DIR1/$tfile`
3266                 [ $mtime2 = $TEST_39_MTIME ] || \
3267                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3268
3269                 cancel_lru_locks osc
3270                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3271         done
3272 }
3273 run_test 39e "create, stat, utime, stat ========================"
3274
3275 # bug 21114
3276 test_39f() {
3277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3278         touch $DIR1/$tfile
3279         mtime1=`stat -c %Y $DIR1/$tfile`
3280
3281         sleep 2
3282         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3283
3284         for (( i=0; i < 2; i++ )) ; do
3285                 local mtime2=`stat -c %Y $DIR1/$tfile`
3286                 [ $mtime2 = $TEST_39_MTIME ] || \
3287                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3288
3289                 cancel_lru_locks osc
3290                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3291         done
3292 }
3293 run_test 39f "create, stat, sleep, utime, stat ================="
3294
3295 # bug 11063
3296 test_39g() {
3297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3298         echo hello >> $DIR1/$tfile
3299         local mtime1=`stat -c %Y $DIR1/$tfile`
3300
3301         sleep 2
3302         chmod o+r $DIR1/$tfile
3303
3304         for (( i=0; i < 2; i++ )) ; do
3305                 local mtime2=`stat -c %Y $DIR1/$tfile`
3306                 [ "$mtime1" = "$mtime2" ] || \
3307                         error "lost mtime: $mtime2, should be $mtime1"
3308
3309                 cancel_lru_locks osc
3310                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3311         done
3312 }
3313 run_test 39g "write, chmod, stat ==============================="
3314
3315 # bug 11063
3316 test_39h() {
3317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3318         touch $DIR1/$tfile
3319         sleep 1
3320
3321         local d1=`date`
3322         echo hello >> $DIR1/$tfile
3323         local mtime1=`stat -c %Y $DIR1/$tfile`
3324
3325         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3326         local d2=`date`
3327         if [ "$d1" != "$d2" ]; then
3328                 echo "write and touch not within one second"
3329         else
3330                 for (( i=0; i < 2; i++ )) ; do
3331                         local mtime2=`stat -c %Y $DIR1/$tfile`
3332                         [ "$mtime2" = $TEST_39_MTIME ] || \
3333                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3334
3335                         cancel_lru_locks osc
3336                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3337                 done
3338         fi
3339 }
3340 run_test 39h "write, utime within one second, stat ============="
3341
3342 test_39i() {
3343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3344         touch $DIR1/$tfile
3345         sleep 1
3346
3347         echo hello >> $DIR1/$tfile
3348         local mtime1=`stat -c %Y $DIR1/$tfile`
3349
3350         mv $DIR1/$tfile $DIR1/$tfile-1
3351
3352         for (( i=0; i < 2; i++ )) ; do
3353                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3354
3355                 [ "$mtime1" = "$mtime2" ] || \
3356                         error "lost mtime: $mtime2, should be $mtime1"
3357
3358                 cancel_lru_locks osc
3359                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3360         done
3361 }
3362 run_test 39i "write, rename, stat =============================="
3363
3364 test_39j() {
3365         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3366         start_full_debug_logging
3367         touch $DIR1/$tfile
3368         sleep 1
3369
3370         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3371         lctl set_param fail_loc=0x80000412
3372         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3373                 error "multiop failed"
3374         local multipid=$!
3375         local mtime1=`stat -c %Y $DIR1/$tfile`
3376
3377         mv $DIR1/$tfile $DIR1/$tfile-1
3378
3379         kill -USR1 $multipid
3380         wait $multipid || error "multiop close failed"
3381
3382         for (( i=0; i < 2; i++ )) ; do
3383                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3384                 [ "$mtime1" = "$mtime2" ] ||
3385                         error "mtime is lost on close: $mtime2, " \
3386                               "should be $mtime1"
3387
3388                 cancel_lru_locks osc
3389                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3390         done
3391         lctl set_param fail_loc=0
3392         stop_full_debug_logging
3393 }
3394 run_test 39j "write, rename, close, stat ======================="
3395
3396 test_39k() {
3397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3398         touch $DIR1/$tfile
3399         sleep 1
3400
3401         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3402         local multipid=$!
3403         local mtime1=`stat -c %Y $DIR1/$tfile`
3404
3405         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3406
3407         kill -USR1 $multipid
3408         wait $multipid || error "multiop close failed"
3409
3410         for (( i=0; i < 2; i++ )) ; do
3411                 local mtime2=`stat -c %Y $DIR1/$tfile`
3412
3413                 [ "$mtime2" = $TEST_39_MTIME ] || \
3414                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3415
3416                 cancel_lru_locks osc
3417                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3418         done
3419 }
3420 run_test 39k "write, utime, close, stat ========================"
3421
3422 # this should be set to future
3423 TEST_39_ATIME=`date -d "1 year" +%s`
3424
3425 test_39l() {
3426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3427         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3428         local atime_diff=$(do_facet $SINGLEMDS \
3429                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3430         rm -rf $DIR/$tdir
3431         mkdir -p $DIR/$tdir
3432
3433         # test setting directory atime to future
3434         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3435         local atime=$(stat -c %X $DIR/$tdir)
3436         [ "$atime" = $TEST_39_ATIME ] ||
3437                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3438
3439         # test setting directory atime from future to now
3440         local now=$(date +%s)
3441         touch -a -d @$now $DIR/$tdir
3442
3443         atime=$(stat -c %X $DIR/$tdir)
3444         [ "$atime" -eq "$now"  ] ||
3445                 error "atime is not updated from future: $atime, $now"
3446
3447         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3448         sleep 3
3449
3450         # test setting directory atime when now > dir atime + atime_diff
3451         local d1=$(date +%s)
3452         ls $DIR/$tdir
3453         local d2=$(date +%s)
3454         cancel_lru_locks mdc
3455         atime=$(stat -c %X $DIR/$tdir)
3456         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3457                 error "atime is not updated  : $atime, should be $d2"
3458
3459         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3460         sleep 3
3461
3462         # test not setting directory atime when now < dir atime + atime_diff
3463         ls $DIR/$tdir
3464         cancel_lru_locks mdc
3465         atime=$(stat -c %X $DIR/$tdir)
3466         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3467                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3468
3469         do_facet $SINGLEMDS \
3470                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3471 }
3472 run_test 39l "directory atime update ==========================="
3473
3474 test_39m() {
3475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3476         touch $DIR1/$tfile
3477         sleep 2
3478         local far_past_mtime=$(date -d "May 29 1953" +%s)
3479         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3480
3481         touch -m -d @$far_past_mtime $DIR1/$tfile
3482         touch -a -d @$far_past_atime $DIR1/$tfile
3483
3484         for (( i=0; i < 2; i++ )) ; do
3485                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3486                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3487                         error "atime or mtime set incorrectly"
3488
3489                 cancel_lru_locks osc
3490                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3491         done
3492 }
3493 run_test 39m "test atime and mtime before 1970"
3494
3495 test_39n() { # LU-3832
3496         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3497         local atime_diff=$(do_facet $SINGLEMDS \
3498                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3499         local atime0
3500         local atime1
3501         local atime2
3502
3503         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3504
3505         rm -rf $DIR/$tfile
3506         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3507         atime0=$(stat -c %X $DIR/$tfile)
3508
3509         sleep 5
3510         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3511         atime1=$(stat -c %X $DIR/$tfile)
3512
3513         sleep 5
3514         cancel_lru_locks mdc
3515         cancel_lru_locks osc
3516         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3517         atime2=$(stat -c %X $DIR/$tfile)
3518
3519         do_facet $SINGLEMDS \
3520                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3521
3522         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3523         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3524 }
3525 run_test 39n "check that O_NOATIME is honored"
3526
3527 test_39o() {
3528         TESTDIR=$DIR/$tdir/$tfile
3529         [ -e $TESTDIR ] && rm -rf $TESTDIR
3530         mkdir -p $TESTDIR
3531         cd $TESTDIR
3532         links1=2
3533         ls
3534         mkdir a b
3535         ls
3536         links2=$(stat -c %h .)
3537         [ $(($links1 + 2)) != $links2 ] &&
3538                 error "wrong links count $(($links1 + 2)) != $links2"
3539         rmdir b
3540         links3=$(stat -c %h .)
3541         [ $(($links1 + 1)) != $links3 ] &&
3542                 error "wrong links count $links1 != $links3"
3543         return 0
3544 }
3545 run_test 39o "directory cached attributes updated after create"
3546
3547 test_39p() {
3548         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3549         local MDTIDX=1
3550         TESTDIR=$DIR/$tdir/$tdir
3551         [ -e $TESTDIR ] && rm -rf $TESTDIR
3552         test_mkdir -p $TESTDIR
3553         cd $TESTDIR
3554         links1=2
3555         ls
3556         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3557         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3558         ls
3559         links2=$(stat -c %h .)
3560         [ $(($links1 + 2)) != $links2 ] &&
3561                 error "wrong links count $(($links1 + 2)) != $links2"
3562         rmdir remote_dir2
3563         links3=$(stat -c %h .)
3564         [ $(($links1 + 1)) != $links3 ] &&
3565                 error "wrong links count $links1 != $links3"
3566         return 0
3567 }
3568 run_test 39p "remote directory cached attributes updated after create ========"
3569
3570
3571 test_39q() { # LU-8041
3572         local testdir=$DIR/$tdir
3573         mkdir -p $testdir
3574         multiop_bg_pause $testdir D_c || error "multiop failed"
3575         local multipid=$!
3576         cancel_lru_locks mdc
3577         kill -USR1 $multipid
3578         local atime=$(stat -c %X $testdir)
3579         [ "$atime" -ne 0 ] || error "atime is zero"
3580 }
3581 run_test 39q "close won't zero out atime"
3582
3583 test_40() {
3584         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3585         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3586                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3587         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3588                 error "$tfile is not 4096 bytes in size"
3589 }
3590 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3591
3592 test_41() {
3593         # bug 1553
3594         small_write $DIR/f41 18
3595 }
3596 run_test 41 "test small file write + fstat ====================="
3597
3598 count_ost_writes() {
3599         lctl get_param -n ${OSC}.*.stats |
3600                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3601                         END { printf("%0.0f", writes) }'
3602 }
3603
3604 # decent default
3605 WRITEBACK_SAVE=500
3606 DIRTY_RATIO_SAVE=40
3607 MAX_DIRTY_RATIO=50
3608 BG_DIRTY_RATIO_SAVE=10
3609 MAX_BG_DIRTY_RATIO=25
3610
3611 start_writeback() {
3612         trap 0
3613         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3614         # dirty_ratio, dirty_background_ratio
3615         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3616                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3617                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3618                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3619         else
3620                 # if file not here, we are a 2.4 kernel
3621                 kill -CONT `pidof kupdated`
3622         fi
3623 }
3624
3625 stop_writeback() {
3626         # setup the trap first, so someone cannot exit the test at the
3627         # exact wrong time and mess up a machine
3628         trap start_writeback EXIT
3629         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3630         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3631                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3632                 sysctl -w vm.dirty_writeback_centisecs=0
3633                 sysctl -w vm.dirty_writeback_centisecs=0
3634                 # save and increase /proc/sys/vm/dirty_ratio
3635                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3636                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3637                 # save and increase /proc/sys/vm/dirty_background_ratio
3638                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3639                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3640         else
3641                 # if file not here, we are a 2.4 kernel
3642                 kill -STOP `pidof kupdated`
3643         fi
3644 }
3645
3646 # ensure that all stripes have some grant before we test client-side cache
3647 setup_test42() {
3648         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3649                 dd if=/dev/zero of=$i bs=4k count=1
3650                 rm $i
3651         done
3652 }
3653
3654 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3655 # file truncation, and file removal.
3656 test_42a() {
3657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3658         setup_test42
3659         cancel_lru_locks $OSC
3660         stop_writeback
3661         sync; sleep 1; sync # just to be safe
3662         BEFOREWRITES=`count_ost_writes`
3663         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3664         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3665         AFTERWRITES=`count_ost_writes`
3666         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3667                 error "$BEFOREWRITES < $AFTERWRITES"
3668         start_writeback
3669 }
3670 run_test 42a "ensure that we don't flush on close"
3671
3672 test_42b() {
3673         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3674         setup_test42
3675         cancel_lru_locks $OSC
3676         stop_writeback
3677         sync
3678         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3679         BEFOREWRITES=$(count_ost_writes)
3680         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3681         AFTERWRITES=$(count_ost_writes)
3682         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3683                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3684         fi
3685         BEFOREWRITES=$(count_ost_writes)
3686         sync || error "sync: $?"
3687         AFTERWRITES=$(count_ost_writes)
3688         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3689                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3690         fi
3691         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3692         start_writeback
3693         return 0
3694 }
3695 run_test 42b "test destroy of file with cached dirty data ======"
3696
3697 # if these tests just want to test the effect of truncation,
3698 # they have to be very careful.  consider:
3699 # - the first open gets a {0,EOF}PR lock
3700 # - the first write conflicts and gets a {0, count-1}PW
3701 # - the rest of the writes are under {count,EOF}PW
3702 # - the open for truncate tries to match a {0,EOF}PR
3703 #   for the filesize and cancels the PWs.
3704 # any number of fixes (don't get {0,EOF} on open, match
3705 # composite locks, do smarter file size management) fix
3706 # this, but for now we want these tests to verify that
3707 # the cancellation with truncate intent works, so we
3708 # start the file with a full-file pw lock to match against
3709 # until the truncate.
3710 trunc_test() {
3711         test=$1
3712         file=$DIR/$test
3713         offset=$2
3714         cancel_lru_locks $OSC
3715         stop_writeback
3716         # prime the file with 0,EOF PW to match
3717         touch $file
3718         $TRUNCATE $file 0
3719         sync; sync
3720         # now the real test..
3721         dd if=/dev/zero of=$file bs=1024 count=100
3722         BEFOREWRITES=`count_ost_writes`
3723         $TRUNCATE $file $offset
3724         cancel_lru_locks $OSC
3725         AFTERWRITES=`count_ost_writes`
3726         start_writeback
3727 }
3728
3729 test_42c() {
3730         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3731         trunc_test 42c 1024
3732         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3733             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3734         rm $file
3735 }
3736 run_test 42c "test partial truncate of file with cached dirty data"
3737
3738 test_42d() {
3739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3740         trunc_test 42d 0
3741         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3742             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3743         rm $file
3744 }
3745 run_test 42d "test complete truncate of file with cached dirty data"
3746
3747 test_42e() { # bug22074
3748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3749         local TDIR=$DIR/${tdir}e
3750         local pagesz=$(page_size)
3751         local pages=16 # hardcoded 16 pages, don't change it.
3752         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3753         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3754         local max_dirty_mb
3755         local warmup_files
3756
3757         test_mkdir $DIR/${tdir}e
3758         $SETSTRIPE -c 1 $TDIR
3759         createmany -o $TDIR/f $files
3760
3761         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3762
3763         # we assume that with $OSTCOUNT files, at least one of them will
3764         # be allocated on OST0.
3765         warmup_files=$((OSTCOUNT * max_dirty_mb))
3766         createmany -o $TDIR/w $warmup_files
3767
3768         # write a large amount of data into one file and sync, to get good
3769         # avail_grant number from OST.
3770         for ((i=0; i<$warmup_files; i++)); do
3771                 idx=$($GETSTRIPE -i $TDIR/w$i)
3772                 [ $idx -ne 0 ] && continue
3773                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3774                 break
3775         done
3776         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3777         sync
3778         $LCTL get_param $proc_osc0/cur_dirty_bytes
3779         $LCTL get_param $proc_osc0/cur_grant_bytes
3780
3781         # create as much dirty pages as we can while not to trigger the actual
3782         # RPCs directly. but depends on the env, VFS may trigger flush during this
3783         # period, hopefully we are good.
3784         for ((i=0; i<$warmup_files; i++)); do
3785                 idx=$($GETSTRIPE -i $TDIR/w$i)
3786                 [ $idx -ne 0 ] && continue
3787                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3788         done
3789         $LCTL get_param $proc_osc0/cur_dirty_bytes
3790         $LCTL get_param $proc_osc0/cur_grant_bytes
3791
3792         # perform the real test
3793         $LCTL set_param $proc_osc0/rpc_stats 0
3794         for ((;i<$files; i++)); do
3795                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3796                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3797         done
3798         sync
3799         $LCTL get_param $proc_osc0/rpc_stats
3800
3801         local percent=0
3802         local have_ppr=false
3803         $LCTL get_param $proc_osc0/rpc_stats |
3804                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3805                         # skip lines until we are at the RPC histogram data
3806                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3807                         $have_ppr || continue
3808
3809                         # we only want the percent stat for < 16 pages
3810                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3811
3812                         percent=$((percent + WPCT))
3813                         if [[ $percent -gt 15 ]]; then
3814                                 error "less than 16-pages write RPCs" \
3815                                       "$percent% > 15%"
3816                                 break
3817                         fi
3818                 done
3819         rm -rf $TDIR
3820 }
3821 run_test 42e "verify sub-RPC writes are not done synchronously"
3822
3823 test_43A() { # was test_43
3824         test_mkdir $DIR/$tdir
3825         cp -p /bin/ls $DIR/$tdir/$tfile
3826         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3827         pid=$!
3828         # give multiop a chance to open
3829         sleep 1
3830
3831         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
3832         kill -USR1 $pid
3833 }
3834 run_test 43A "execution of file opened for write should return -ETXTBSY"
3835
3836 test_43a() {
3837         test_mkdir $DIR/$tdir
3838         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3839                 cp -p multiop $DIR/$tdir/multiop
3840         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3841                 error "multiop open $TMP/$tfile.junk failed"
3842         MULTIOP_PID=$!
3843         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3844         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3845         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3846 }
3847 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3848
3849 test_43b() {
3850         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3851         test_mkdir $DIR/$tdir
3852         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3853                 cp -p multiop $DIR/$tdir/multiop
3854         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3855                 error "multiop open $TMP/$tfile.junk failed"
3856         MULTIOP_PID=$!
3857         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3858         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3859         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3860 }
3861 run_test 43b "truncate of file being executed should return -ETXTBSY"
3862
3863 test_43c() {
3864         local testdir="$DIR/$tdir"
3865         test_mkdir $testdir
3866         cp $SHELL $testdir/
3867         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
3868                 ( cd $testdir && md5sum -c )
3869 }
3870 run_test 43c "md5sum of copy into lustre"
3871
3872 test_44A() { # was test_44
3873         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
3874         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3875         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3876 }
3877 run_test 44A "zero length read from a sparse stripe"
3878
3879 test_44a() {
3880         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3881                 awk '{ print $2 }')
3882         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3883         [[ $nstripe -gt $OSTCOUNT ]] &&
3884             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3885             return
3886         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3887                 awk '{ print $2 }')
3888         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3889                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3890                         awk '{ print $2 }')
3891         fi
3892
3893         OFFSETS="0 $((stride/2)) $((stride-1))"
3894         for offset in $OFFSETS; do
3895                 for i in $(seq 0 $((nstripe-1))); do
3896                         local GLOBALOFFSETS=""
3897                         # size in Bytes
3898                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3899                         local myfn=$DIR/d44a-$size
3900                         echo "--------writing $myfn at $size"
3901                         ll_sparseness_write $myfn $size ||
3902                                 error "ll_sparseness_write"
3903                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3904                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3905                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3906
3907                         for j in $(seq 0 $((nstripe-1))); do
3908                                 # size in Bytes
3909                                 size=$((((j + $nstripe )*$stride + $offset)))
3910                                 ll_sparseness_write $myfn $size ||
3911                                         error "ll_sparseness_write"
3912                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3913                         done
3914                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3915                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3916                         rm -f $myfn
3917                 done
3918         done
3919 }
3920 run_test 44a "test sparse pwrite ==============================="
3921
3922 dirty_osc_total() {
3923         tot=0
3924         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
3925                 tot=$(($tot + $d))
3926         done
3927         echo $tot
3928 }
3929 do_dirty_record() {
3930         before=`dirty_osc_total`
3931         echo executing "\"$*\""
3932         eval $*
3933         after=`dirty_osc_total`
3934         echo before $before, after $after
3935 }
3936 test_45() {
3937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3938         f="$DIR/f45"
3939         # Obtain grants from OST if it supports it
3940         echo blah > ${f}_grant
3941         stop_writeback
3942         sync
3943         do_dirty_record "echo blah > $f"
3944         [[ $before -eq $after ]] && error "write wasn't cached"
3945         do_dirty_record "> $f"
3946         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3947         do_dirty_record "echo blah > $f"
3948         [[ $before -eq $after ]] && error "write wasn't cached"
3949         do_dirty_record "sync"
3950         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3951         do_dirty_record "echo blah > $f"
3952         [[ $before -eq $after ]] && error "write wasn't cached"
3953         do_dirty_record "cancel_lru_locks osc"
3954         [[ $before -gt $after ]] ||
3955                 error "lock cancellation didn't lower dirty count"
3956         start_writeback
3957 }
3958 run_test 45 "osc io page accounting ============================"
3959
3960 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3961 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3962 # objects offset and an assert hit when an rpc was built with 1023's mapped
3963 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3964 test_46() {
3965         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3966         f="$DIR/f46"
3967         stop_writeback
3968         sync
3969         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3970         sync
3971         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3972         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3973         sync
3974         start_writeback
3975 }
3976 run_test 46 "dirtying a previously written page ================"
3977
3978 # test_47 is removed "Device nodes check" is moved to test_28
3979
3980 test_48a() { # bug 2399
3981         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3982         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3983                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3984                 return
3985         test_mkdir $DIR/$tdir
3986         cd $DIR/$tdir
3987         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3988         test_mkdir $DIR/$tdir
3989         touch foo || error "'touch foo' failed after recreating cwd"
3990         test_mkdir bar
3991         touch .foo || error "'touch .foo' failed after recreating cwd"
3992         test_mkdir .bar
3993         ls . > /dev/null || error "'ls .' failed after recreating cwd"
3994         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
3995         cd . || error "'cd .' failed after recreating cwd"
3996         mkdir . && error "'mkdir .' worked after recreating cwd"
3997         rmdir . && error "'rmdir .' worked after recreating cwd"
3998         ln -s . baz || error "'ln -s .' failed after recreating cwd"
3999         cd .. || error "'cd ..' failed after recreating cwd"
4000 }
4001 run_test 48a "Access renamed working dir (should return errors)="
4002
4003 test_48b() { # bug 2399
4004         rm -rf $DIR/$tdir
4005         test_mkdir $DIR/$tdir
4006         cd $DIR/$tdir
4007         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4008         touch foo && error "'touch foo' worked after removing cwd"
4009         mkdir foo && error "'mkdir foo' worked after removing cwd"
4010         touch .foo && error "'touch .foo' worked after removing cwd"
4011         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4012         ls . > /dev/null && error "'ls .' worked after removing cwd"
4013         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4014         mkdir . && error "'mkdir .' worked after removing cwd"
4015         rmdir . && error "'rmdir .' worked after removing cwd"
4016         ln -s . foo && error "'ln -s .' worked after removing cwd"
4017         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4018 }
4019 run_test 48b "Access removed working dir (should return errors)="
4020
4021 test_48c() { # bug 2350
4022         #lctl set_param debug=-1
4023         #set -vx
4024         rm -rf $DIR/$tdir
4025         test_mkdir -p $DIR/$tdir/dir
4026         cd $DIR/$tdir/dir
4027         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4028         $TRACE touch foo && error "touch foo worked after removing cwd"
4029         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4030         touch .foo && error "touch .foo worked after removing cwd"
4031         mkdir .foo && error "mkdir .foo worked after removing cwd"
4032         $TRACE ls . && error "'ls .' worked after removing cwd"
4033         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4034         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4035         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4036         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4037         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4038 }
4039 run_test 48c "Access removed working subdir (should return errors)"
4040
4041 test_48d() { # bug 2350
4042         #lctl set_param debug=-1
4043         #set -vx
4044         rm -rf $DIR/$tdir
4045         test_mkdir -p $DIR/$tdir/dir
4046         cd $DIR/$tdir/dir
4047         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4048         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4049         $TRACE touch foo && error "'touch foo' worked after removing parent"
4050         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4051         touch .foo && error "'touch .foo' worked after removing parent"
4052         mkdir .foo && error "mkdir .foo worked after removing parent"
4053         $TRACE ls . && error "'ls .' worked after removing parent"
4054         $TRACE ls .. && error "'ls ..' worked after removing parent"
4055         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4056         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4057         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4058         true
4059 }
4060 run_test 48d "Access removed parent subdir (should return errors)"
4061
4062 test_48e() { # bug 4134
4063         #lctl set_param debug=-1
4064         #set -vx
4065         rm -rf $DIR/$tdir
4066         test_mkdir -p $DIR/$tdir/dir
4067         cd $DIR/$tdir/dir
4068         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4069         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4070         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4071         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4072         # On a buggy kernel addition of "touch foo" after cd .. will
4073         # produce kernel oops in lookup_hash_it
4074         touch ../foo && error "'cd ..' worked after recreate parent"
4075         cd $DIR
4076         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4077 }
4078 run_test 48e "Access to recreated parent subdir (should return errors)"
4079
4080 test_49() { # LU-1030
4081         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4082         remote_ost_nodsh && skip "remote OST with nodsh" && return
4083         # get ost1 size - lustre-OST0000
4084         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4085                 awk '{ print $4 }')
4086         # write 800M at maximum
4087         [[ $ost1_size -lt 2 ]] && ost1_size=2
4088         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4089
4090         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4091         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4092         local dd_pid=$!
4093
4094         # change max_pages_per_rpc while writing the file
4095         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4096         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4097         # loop until dd process exits
4098         while ps ax -opid | grep -wq $dd_pid; do
4099                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4100                 sleep $((RANDOM % 5 + 1))
4101         done
4102         # restore original max_pages_per_rpc
4103         $LCTL set_param $osc1_mppc=$orig_mppc
4104         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4105 }
4106 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4107
4108 test_50() {
4109         # bug 1485
4110         test_mkdir $DIR/$tdir
4111         cd $DIR/$tdir
4112         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4113 }
4114 run_test 50 "special situations: /proc symlinks  ==============="
4115
4116 test_51a() {    # was test_51
4117         # bug 1516 - create an empty entry right after ".." then split dir
4118         test_mkdir -c1 $DIR/$tdir
4119         touch $DIR/$tdir/foo
4120         $MCREATE $DIR/$tdir/bar
4121         rm $DIR/$tdir/foo
4122         createmany -m $DIR/$tdir/longfile 201
4123         FNUM=202
4124         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4125                 $MCREATE $DIR/$tdir/longfile$FNUM
4126                 FNUM=$(($FNUM + 1))
4127                 echo -n "+"
4128         done
4129         echo
4130         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4131 }
4132 run_test 51a "special situations: split htree with empty entry =="
4133
4134 cleanup_print_lfs_df () {
4135         trap 0
4136         $LFS df
4137         $LFS df -i
4138 }
4139
4140 test_51b() {
4141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4142         local dir=$DIR/$tdir
4143
4144         local nrdirs=$((65536 + 100))
4145
4146         # cleanup the directory
4147         rm -fr $dir
4148
4149         test_mkdir -c1 $dir
4150
4151         $LFS df
4152         $LFS df -i
4153         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4154         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4155         [[ $numfree -lt $nrdirs ]] &&
4156                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4157                 return
4158
4159         # need to check free space for the directories as well
4160         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4161         numfree=$(( blkfree / $(fs_inode_ksize) ))
4162         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4163                 return
4164
4165         trap cleanup_print_lfs_df EXIT
4166
4167         # create files
4168         createmany -d $dir/d $nrdirs || {
4169                 unlinkmany $dir/d $nrdirs
4170                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4171         }
4172
4173         # really created :
4174         nrdirs=$(ls -U $dir | wc -l)
4175
4176         # unlink all but 100 subdirectories, then check it still works
4177         local left=100
4178         local delete=$((nrdirs - left))
4179
4180         $LFS df
4181         $LFS df -i
4182
4183         # for ldiskfs the nlink count should be 1, but this is OSD specific
4184         # and so this is listed for informational purposes only
4185         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4186         unlinkmany -d $dir/d $delete ||
4187                 error "unlink of first $delete subdirs failed"
4188
4189         echo "nlink between: $(stat -c %h $dir)"
4190         local found=$(ls -U $dir | wc -l)
4191         [ $found -ne $left ] &&
4192                 error "can't find subdirs: found only $found, expected $left"
4193
4194         unlinkmany -d $dir/d $delete $left ||
4195                 error "unlink of second $left subdirs failed"
4196         # regardless of whether the backing filesystem tracks nlink accurately
4197         # or not, the nlink count shouldn't be more than "." and ".." here
4198         local after=$(stat -c %h $dir)
4199         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4200                 echo "nlink after: $after"
4201
4202         cleanup_print_lfs_df
4203 }
4204 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4205
4206 test_51d() {
4207         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4208         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
4209         test_mkdir $DIR/$tdir
4210         createmany -o $DIR/$tdir/t- 1000
4211         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4212         for N in $(seq 0 $((OSTCOUNT - 1))); do
4213                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4214                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4215                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4216                         '($1 == '$N') { objs += 1 } \
4217                         END { printf("%0.0f", objs) }')
4218                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4219         done
4220         unlinkmany $DIR/$tdir/t- 1000
4221
4222         NLAST=0
4223         for N in $(seq 1 $((OSTCOUNT - 1))); do
4224                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4225                         error "OST $N has less objects vs OST $NLAST" \
4226                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4227                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4228                         error "OST $N has less objects vs OST $NLAST" \
4229                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4230
4231                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4232                         error "OST $N has less #0 objects vs OST $NLAST" \
4233                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4234                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4235                         error "OST $N has less #0 objects vs OST $NLAST" \
4236                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4237                 NLAST=$N
4238         done
4239         rm -f $TMP/$tfile
4240 }
4241 run_test 51d "check object distribution"
4242
4243 test_51e() {
4244         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4245                 skip "ldiskfs only test"
4246                 return
4247         fi
4248
4249         test_mkdir -c1 $DIR/$tdir
4250         test_mkdir -c1 $DIR/$tdir/d0
4251
4252         touch $DIR/$tdir/d0/foo
4253         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4254                 error "file exceed 65000 nlink limit!"
4255         unlinkmany $DIR/$tdir/d0/f- 65001
4256         return 0
4257 }
4258 run_test 51e "check file nlink limit"
4259
4260 test_51f() {
4261         test_mkdir $DIR/$tdir
4262
4263         local max=100000
4264         local ulimit_old=$(ulimit -n)
4265         local spare=20 # number of spare fd's for scripts/libraries, etc.
4266         local mdt=$(lfs getstripe -M $DIR/$tdir)
4267         local numfree=$(lfs df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4268
4269         echo "MDT$mdt numfree=$numfree, max=$max"
4270         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4271         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4272                 while ! ulimit -n $((numfree + spare)); do
4273                         numfree=$((numfree * 3 / 4))
4274                 done
4275                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4276         else
4277                 echo "left ulimit at $ulimit_old"
4278         fi
4279
4280         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4281                 unlinkmany $DIR/$tdir/f $numfree
4282                 error "create+open $numfree files in $DIR/$tdir failed"
4283         }
4284         ulimit -n $ulimit_old
4285
4286         # if createmany exits at 120s there will be fewer than $numfree files
4287         unlinkmany $DIR/$tdir/f $numfree || true
4288 }
4289 run_test 51f "check many open files limit"
4290
4291 test_52a() {
4292         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4293         test_mkdir $DIR/$tdir
4294         touch $DIR/$tdir/foo
4295         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4296         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4297         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4298         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4299         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4300                                         error "link worked"
4301         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4302         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4303         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4304                                                      error "lsattr"
4305         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4306         cp -r $DIR/$tdir $TMP/
4307         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4308 }
4309 run_test 52a "append-only flag test (should return errors)"
4310
4311 test_52b() {
4312         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4313         test_mkdir $DIR/$tdir
4314         touch $DIR/$tdir/foo
4315         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4316         cat test > $DIR/$tdir/foo && error "cat test worked"
4317         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4318         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4319         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4320                                         error "link worked"
4321         echo foo >> $DIR/$tdir/foo && error "echo worked"
4322         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4323         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4324         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4325         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4326                                                         error "lsattr"
4327         chattr -i $DIR/$tdir/foo || error "chattr failed"
4328
4329         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4330 }
4331 run_test 52b "immutable flag test (should return errors) ======="
4332
4333 test_53() {
4334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4335         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4336         remote_ost_nodsh && skip "remote OST with nodsh" && return
4337
4338         local param
4339         local param_seq
4340         local ostname
4341         local mds_last
4342         local mds_last_seq
4343         local ost_last
4344         local ost_last_seq
4345         local ost_last_id
4346         local ostnum
4347         local node
4348         local found=false
4349         local support_last_seq=true
4350
4351         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4352                 support_last_seq=false
4353
4354         # only test MDT0000
4355         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4356         local value
4357         for value in $(do_facet $SINGLEMDS \
4358                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4359                 param=$(echo ${value[0]} | cut -d "=" -f1)
4360                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4361
4362                 if $support_last_seq; then
4363                         param_seq=$(echo $param |
4364                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4365                         mds_last_seq=$(do_facet $SINGLEMDS \
4366                                        $LCTL get_param -n $param_seq)
4367                 fi
4368                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4369
4370                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4371                 node=$(facet_active_host ost$((ostnum+1)))
4372                 param="obdfilter.$ostname.last_id"
4373                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4374                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4375                         ost_last_id=$ost_last
4376
4377                         if $support_last_seq; then
4378                                 ost_last_id=$(echo $ost_last |
4379                                               awk -F':' '{print $2}' |
4380                                               sed -e "s/^0x//g")
4381                                 ost_last_seq=$(echo $ost_last |
4382                                                awk -F':' '{print $1}')
4383                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4384                         fi
4385
4386                         if [[ $ost_last_id != $mds_last ]]; then
4387                                 error "$ost_last_id != $mds_last"
4388                         else
4389                                 found=true
4390                                 break
4391                         fi
4392                 done
4393         done
4394         $found || error "can not match last_seq/last_id for $mdtosc"
4395         return 0
4396 }
4397 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4398
4399 test_54a() {
4400         perl -MSocket -e ';' || { skip "no Socket perl module installed" && return; }
4401
4402         $SOCKETSERVER $DIR/socket ||
4403                 error "$SOCKETSERVER $DIR/socket failed: $?"
4404         $SOCKETCLIENT $DIR/socket ||
4405                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4406         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4407 }
4408 run_test 54a "unix domain socket test =========================="
4409
4410 test_54b() {
4411         f="$DIR/f54b"
4412         mknod $f c 1 3
4413         chmod 0666 $f
4414         dd if=/dev/zero of=$f bs=$(page_size) count=1
4415 }
4416 run_test 54b "char device works in lustre ======================"
4417
4418 find_loop_dev() {
4419         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4420         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4421         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4422
4423         for i in $(seq 3 7); do
4424                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4425                 LOOPDEV=$LOOPBASE$i
4426                 LOOPNUM=$i
4427                 break
4428         done
4429 }
4430
4431 cleanup_54c() {
4432         local rc=0
4433         loopdev="$DIR/loop54c"
4434
4435         trap 0
4436         $UMOUNT $DIR/$tdir || rc=$?
4437         losetup -d $loopdev || true
4438         losetup -d $LOOPDEV || true
4439         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4440         return $rc
4441 }
4442
4443 test_54c() {
4444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4445         loopdev="$DIR/loop54c"
4446
4447         find_loop_dev
4448         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4449         trap cleanup_54c EXIT
4450         mknod $loopdev b 7 $LOOPNUM
4451         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4452         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4453         losetup $loopdev $DIR/$tfile ||
4454                 error "can't set up $loopdev for $DIR/$tfile"
4455         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4456         test_mkdir $DIR/$tdir
4457         mount -t ext2 $loopdev $DIR/$tdir ||
4458                 error "error mounting $loopdev on $DIR/$tdir"
4459         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4460                 error "dd write"
4461         df $DIR/$tdir
4462         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4463                 error "dd read"
4464         cleanup_54c
4465 }
4466 run_test 54c "block device works in lustre ====================="
4467
4468 test_54d() {
4469         f="$DIR/f54d"
4470         string="aaaaaa"
4471         mknod $f p
4472         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4473 }
4474 run_test 54d "fifo device works in lustre ======================"
4475
4476 test_54e() {
4477         f="$DIR/f54e"
4478         string="aaaaaa"
4479         cp -aL /dev/console $f
4480         echo $string > $f || error "echo $string to $f failed"
4481 }
4482 run_test 54e "console/tty device works in lustre ======================"
4483
4484 #The test_55 used to be iopen test and it was removed by bz#24037.
4485 #run_test 55 "check iopen_connect_dentry() ======================"
4486
4487 test_56a() {    # was test_56
4488         rm -rf $DIR/$tdir
4489         $SETSTRIPE -d $DIR
4490         test_mkdir -p $DIR/$tdir/dir
4491         NUMFILES=3
4492         NUMFILESx2=$(($NUMFILES * 2))
4493         for i in $(seq 1 $NUMFILES); do
4494                 touch $DIR/$tdir/file$i
4495                 touch $DIR/$tdir/dir/file$i
4496         done
4497
4498         # test lfs getstripe with --recursive
4499         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4500         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4501                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4502         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4503         [[ $FILENUM -eq $NUMFILES ]] ||
4504                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4505         echo "$GETSTRIPE --recursive passed."
4506
4507         # test lfs getstripe with file instead of dir
4508         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4509         [[ $FILENUM -eq 1 ]] ||
4510                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4511         echo "$GETSTRIPE file1 passed."
4512
4513         #test lfs getstripe with --verbose
4514         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4515                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4516                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4517         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4518                 error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4519
4520         #test lfs getstripe with -v prints lmm_fid
4521         [[ $($GETSTRIPE -v $DIR/$tdir | grep -c lmm_fid) -eq $NUMFILES ]] ||
4522                 error "$GETSTRIPE -v $DIR/$tdir: want $NUMFILES lmm_fid: lines"
4523         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_fid) -eq 0 ]] ||
4524                 error "$GETSTRIPE $DIR/$tdir: showed lmm_fid"
4525         echo "$GETSTRIPE --verbose passed."
4526
4527         #check for FID information
4528         local fid1=$($GETSTRIPE --fid $DIR/$tdir/file1)
4529         local fid2=$($GETSTRIPE --verbose $DIR/$tdir/file1 |
4530                        awk '/lmm_fid: / { print $2 }')
4531         local fid3=$($LFS path2fid $DIR/$tdir/file1)
4532         [ "$fid1" != "$fid2" ] &&
4533                 error "getstripe --fid $fid1 != getstripe --verbose $fid2"
4534         [ "$fid1" != "$fid3" ] &&
4535                 error "getstripe --fid $fid1 != lfs path2fid $fid3"
4536         echo "$GETSTRIPE --fid passed."
4537
4538         #test lfs getstripe with --obd
4539         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4540                 grep -q "unknown obduuid" ||
4541                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4542
4543         [[ $OSTCOUNT -lt 2 ]] &&
4544                 skip_env "skipping other $GETSTRIPE --obd test" && return
4545
4546         OSTIDX=1
4547         OBDUUID=$(ostuuid_from_index $OSTIDX)
4548         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4549         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4550         [[ $FOUND -eq $FILENUM ]] ||
4551                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4552         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4553                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4554                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4555                 error "$GETSTRIPE --obd: should not show file on other obd"
4556         echo "$GETSTRIPE --obd passed"
4557 }
4558 run_test 56a "check $GETSTRIPE"
4559
4560 test_56b() {
4561         test_mkdir $DIR/$tdir
4562         NUMDIRS=3
4563         for i in $(seq 1 $NUMDIRS); do
4564                 test_mkdir $DIR/$tdir/dir$i
4565         done
4566
4567         # test lfs getdirstripe default mode is non-recursion, which is
4568         # different from lfs getstripe
4569         dircnt=$($LFS getdirstripe $DIR/$tdir | grep -c lmv_stripe_count)
4570         [[ $dircnt -eq 1 ]] ||
4571                 error "$LFS getdirstripe: found $dircnt, not 1"
4572         dircnt=$($LFS getdirstripe --recursive $DIR/$tdir |
4573                 grep -c lmv_stripe_count)
4574         [[ $dircnt -eq $((NUMDIRS + 1)) ]] ||
4575                 error "$LFS getdirstripe --recursive: found $dircnt, \
4576                         not $((NUMDIRS + 1))"
4577 }
4578 run_test 56b "check $LFS getdirstripe"
4579
4580 test_56c() {
4581         local ost_idx=0
4582         local ost_name=$(ostname_from_index $ost_idx)
4583
4584         local old_status=$(ost_dev_status $ost_idx)
4585         [[ -z "$old_status" ]] ||
4586                 { skip_env "OST $ost_name is in $old_status status"; return 0; }
4587
4588         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4589         sleep_maxage
4590
4591         local new_status=$(ost_dev_status $ost_idx)
4592         [[ "$new_status" = "D" ]] ||
4593                 error "OST $ost_name is in status of '$new_status', not 'D'"
4594
4595         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4596         sleep_maxage
4597
4598         new_status=$(ost_dev_status $ost_idx)
4599         [[ -z "$new_status" ]] ||
4600                 error "OST $ost_name is in status of '$new_status', not ''"
4601 }
4602 run_test 56c "check 'lfs df' showing device status"
4603
4604 NUMFILES=3
4605 NUMDIRS=3
4606 setup_56() {
4607         local LOCAL_NUMFILES="$1"
4608         local LOCAL_NUMDIRS="$2"
4609         local MKDIR_PARAMS="$3"
4610         local DIR_STRIPE_PARAMS="$4"
4611
4612         if [ ! -d "$TDIR" ] ; then
4613                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4614                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4615                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4616                         touch $TDIR/file$i
4617                 done
4618                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4619                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4620                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4621                                 touch $TDIR/dir$i/file$j
4622                         done
4623                 done
4624         fi
4625 }
4626
4627 setup_56_special() {
4628         LOCAL_NUMFILES=$1
4629         LOCAL_NUMDIRS=$2
4630         setup_56 $1 $2
4631         if [ ! -e "$TDIR/loop1b" ] ; then
4632                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4633                         mknod $TDIR/loop${i}b b 7 $i
4634                         mknod $TDIR/null${i}c c 1 3
4635                         ln -s $TDIR/file1 $TDIR/link${i}l
4636                 done
4637                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4638                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4639                         mknod $TDIR/dir$i/null${i}c c 1 3
4640                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4641                 done
4642         fi
4643 }
4644
4645 test_56g() {
4646         $SETSTRIPE -d $DIR
4647
4648         TDIR=$DIR/${tdir}g
4649         setup_56 $NUMFILES $NUMDIRS
4650
4651         EXPECTED=$(($NUMDIRS + 2))
4652         # test lfs find with -name
4653         for i in $(seq 1 $NUMFILES) ; do
4654                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4655                 [ $NUMS -eq $EXPECTED ] ||
4656                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4657                               "found $NUMS, expected $EXPECTED"
4658         done
4659 }
4660 run_test 56g "check lfs find -name ============================="
4661
4662 test_56h() {
4663         $SETSTRIPE -d $DIR
4664
4665         TDIR=$DIR/${tdir}g
4666         setup_56 $NUMFILES $NUMDIRS
4667
4668         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4669         # test lfs find with ! -name
4670         for i in $(seq 1 $NUMFILES) ; do
4671                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4672                 [ $NUMS -eq $EXPECTED ] ||
4673                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4674                               "found $NUMS, expected $EXPECTED"
4675         done
4676 }
4677 run_test 56h "check lfs find ! -name"
4678
4679 test_56i() {
4680         tdir=${tdir}i
4681         test_mkdir $DIR/$tdir
4682         UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4683         CMD="$LFIND -ost $UUID $DIR/$tdir"
4684         OUT=$($CMD)
4685         [ -z "$OUT" ] || error "'$CMD' returned directory '$OUT'"
4686 }
4687 run_test 56i "check 'lfs find -ost UUID' skips directories"
4688
4689 test_56j() {
4690         TDIR=$DIR/${tdir}g
4691         setup_56_special $NUMFILES $NUMDIRS
4692
4693         EXPECTED=$((NUMDIRS + 1))
4694         CMD="$LFIND -type d $TDIR"
4695         NUMS=$($CMD | wc -l)
4696         [ $NUMS -eq $EXPECTED ] ||
4697                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4698 }
4699 run_test 56j "check lfs find -type d ============================="
4700
4701 test_56k() {
4702         TDIR=$DIR/${tdir}g
4703         setup_56_special $NUMFILES $NUMDIRS
4704
4705         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4706         CMD="$LFIND -type f $TDIR"
4707         NUMS=$($CMD | wc -l)
4708         [ $NUMS -eq $EXPECTED ] ||
4709                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4710 }
4711 run_test 56k "check lfs find -type f ============================="
4712
4713 test_56l() {
4714         TDIR=$DIR/${tdir}g
4715         setup_56_special $NUMFILES $NUMDIRS
4716
4717         EXPECTED=$((NUMDIRS + NUMFILES))
4718         CMD="$LFIND -type b $TDIR"
4719         NUMS=$($CMD | wc -l)
4720         [ $NUMS -eq $EXPECTED ] ||
4721                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4722 }
4723 run_test 56l "check lfs find -type b ============================="
4724
4725 test_56m() {
4726         TDIR=$DIR/${tdir}g
4727         setup_56_special $NUMFILES $NUMDIRS
4728
4729         EXPECTED=$((NUMDIRS + NUMFILES))
4730         CMD="$LFIND -type c $TDIR"
4731         NUMS=$($CMD | wc -l)
4732         [ $NUMS -eq $EXPECTED ] ||
4733                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4734 }
4735 run_test 56m "check lfs find -type c ============================="
4736
4737 test_56n() {
4738         TDIR=$DIR/${tdir}g
4739         setup_56_special $NUMFILES $NUMDIRS
4740
4741         EXPECTED=$((NUMDIRS + NUMFILES))
4742         CMD="$LFIND -type l $TDIR"
4743         NUMS=$($CMD | wc -l)
4744         [ $NUMS -eq $EXPECTED ] ||
4745                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4746 }
4747 run_test 56n "check lfs find -type l ============================="
4748
4749 test_56o() {
4750         TDIR=$DIR/${tdir}o
4751         setup_56 $NUMFILES $NUMDIRS
4752         utime $TDIR/file1 > /dev/null || error "utime (1)"
4753         utime $TDIR/file2 > /dev/null || error "utime (2)"
4754         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4755         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4756         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4757         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4758
4759         EXPECTED=4
4760         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4761         [ $NUMS -eq $EXPECTED ] || \
4762                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4763
4764         EXPECTED=12
4765         CMD="$LFIND -mtime 0 $TDIR"
4766         NUMS=$($CMD | wc -l)
4767         [ $NUMS -eq $EXPECTED ] ||
4768                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4769 }
4770 run_test 56o "check lfs find -mtime for old files =========================="
4771
4772 test_56p() {
4773         [ $RUNAS_ID -eq $UID ] &&
4774                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4775
4776         TDIR=$DIR/${tdir}p
4777         setup_56 $NUMFILES $NUMDIRS
4778
4779         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4780         EXPECTED=$NUMFILES
4781         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4782         NUMS=$($CMD | wc -l)
4783         [ $NUMS -eq $EXPECTED ] || \
4784                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4785
4786         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4787         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4788         NUMS=$($CMD | wc -l)
4789         [ $NUMS -eq $EXPECTED ] || \
4790                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4791 }
4792 run_test 56p "check lfs find -uid and ! -uid ==============================="
4793
4794 test_56q() {
4795         [ $RUNAS_ID -eq $UID ] &&
4796                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4797
4798         TDIR=$DIR/${tdir}q
4799         setup_56 $NUMFILES $NUMDIRS
4800
4801         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4802
4803         EXPECTED=$NUMFILES
4804         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4805         NUMS=$($CMD | wc -l)
4806         [ $NUMS -eq $EXPECTED ] ||
4807                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4808
4809         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4810         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4811         NUMS=$($CMD | wc -l)
4812         [ $NUMS -eq $EXPECTED ] ||
4813                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4814 }
4815 run_test 56q "check lfs find -gid and ! -gid ==============================="
4816
4817 test_56r() {
4818         TDIR=$DIR/${tdir}r
4819         setup_56 $NUMFILES $NUMDIRS
4820
4821         EXPECTED=12
4822         CMD="$LFIND -size 0 -type f $TDIR"
4823         NUMS=$($CMD | wc -l)
4824         [ $NUMS -eq $EXPECTED ] ||
4825                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4826         EXPECTED=0
4827         CMD="$LFIND ! -size 0 -type f $TDIR"
4828         NUMS=$($CMD | wc -l)
4829         [ $NUMS -eq $EXPECTED ] ||
4830                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4831         echo "test" > $TDIR/$tfile
4832         echo "test2" > $TDIR/$tfile.2 && sync
4833         EXPECTED=1
4834         CMD="$LFIND -size 5 -type f $TDIR"
4835         NUMS=$($CMD | wc -l)
4836         [ $NUMS -eq $EXPECTED ] ||
4837                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4838         EXPECTED=1
4839         CMD="$LFIND -size +5 -type f $TDIR"
4840         NUMS=$($CMD | wc -l)
4841         [ $NUMS -eq $EXPECTED ] ||
4842                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4843         EXPECTED=2
4844         CMD="$LFIND -size +0 -type f $TDIR"
4845         NUMS=$($CMD | wc -l)
4846         [ $NUMS -eq $EXPECTED ] ||
4847                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4848         EXPECTED=2
4849         CMD="$LFIND ! -size -5 -type f $TDIR"
4850         NUMS=$($CMD | wc -l)
4851         [ $NUMS -eq $EXPECTED ] ||
4852                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4853         EXPECTED=12
4854         CMD="$LFIND -size -5 -type f $TDIR"
4855         NUMS=$($CMD | wc -l)
4856         [ $NUMS -eq $EXPECTED ] ||
4857                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4858 }
4859 run_test 56r "check lfs find -size works =========================="
4860
4861 test_56s() { # LU-611
4862         TDIR=$DIR/${tdir}s
4863
4864         #LU-9369
4865         setup_56 0 $NUMDIRS
4866         for i in $(seq 1 $NUMDIRS); do
4867                 $SETSTRIPE -c $((OSTCOUNT + 1)) $TDIR/dir$i/$tfile
4868         done
4869         EXPECTED=$NUMDIRS
4870         CMD="$LFIND -c $OSTCOUNT $TDIR"
4871         NUMS=$($CMD | wc -l)
4872         [ $NUMS -eq $EXPECTED ] || {
4873                 $GETSTRIPE -R $TDIR
4874                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4875         }
4876         rm -rf $TDIR
4877
4878         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4879         if [[ $OSTCOUNT -gt 1 ]]; then
4880                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4881                 ONESTRIPE=4
4882                 EXTRA=4
4883         else
4884                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4885                 EXTRA=0
4886         fi
4887
4888         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4889         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4890         NUMS=$($CMD | wc -l)
4891         [ $NUMS -eq $EXPECTED ] || {
4892                 $GETSTRIPE -R $TDIR
4893                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4894         }
4895
4896         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4897         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4898         NUMS=$($CMD | wc -l)
4899         [ $NUMS -eq $EXPECTED ] || {
4900                 $GETSTRIPE -R $TDIR
4901                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4902         }
4903
4904         EXPECTED=$ONESTRIPE
4905         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4906         NUMS=$($CMD | wc -l)
4907         [ $NUMS -eq $EXPECTED ] || {
4908                 $GETSTRIPE -R $TDIR
4909                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4910         }
4911
4912         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4913         NUMS=$($CMD | wc -l)
4914         [ $NUMS -eq $EXPECTED ] || {
4915                 $GETSTRIPE -R $TDIR
4916                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4917         }
4918
4919         EXPECTED=0
4920         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4921         NUMS=$($CMD | wc -l)
4922         [ $NUMS -eq $EXPECTED ] || {
4923                 $GETSTRIPE -R $TDIR
4924                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4925         }
4926 }
4927 run_test 56s "check lfs find -stripe-count works"
4928
4929 test_56t() { # LU-611
4930         TDIR=$DIR/${tdir}t
4931
4932         #LU-9369
4933         setup_56 0 $NUMDIRS
4934         for i in $(seq 1 $NUMDIRS); do
4935                 $SETSTRIPE -S 4M $TDIR/dir$i/$tfile
4936         done
4937         EXPECTED=$NUMDIRS
4938         CMD="$LFIND -S 4M $TDIR"
4939         NUMS=$($CMD | wc -l)
4940         [ $NUMS -eq $EXPECTED ] || {
4941                 $GETSTRIPE -R $TDIR
4942                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4943         }
4944         rm -rf $TDIR
4945
4946         setup_56 $NUMFILES $NUMDIRS "--stripe-size 512k"
4947
4948         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4949
4950         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4951         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4952         NUMS=$($CMD | wc -l)
4953         [ $NUMS -eq $EXPECTED ] ||
4954                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4955
4956         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4957         NUMS=$($CMD | wc -l)
4958         [ $NUMS -eq $EXPECTED ] ||
4959                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4960
4961         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4962         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4963         NUMS=$($CMD | wc -l)
4964         [ $NUMS -eq $EXPECTED ] ||
4965                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4966
4967         CMD="$LFIND -stripe-size -640k -type f $TDIR"
4968         NUMS=$($CMD | wc -l)
4969         [ $NUMS -eq $EXPECTED ] ||
4970                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4971
4972         EXPECTED=4
4973         CMD="$LFIND -stripe-size 256k -type f $TDIR"
4974         NUMS=$($CMD | wc -l)
4975         [ $NUMS -eq $EXPECTED ] ||
4976                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4977
4978         CMD="$LFIND -stripe-size -320k -type f $TDIR"
4979         NUMS=$($CMD | wc -l)
4980         [ $NUMS -eq $EXPECTED ] ||
4981                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4982
4983         EXPECTED=0
4984         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
4985         NUMS=$($CMD | wc -l)
4986         [ $NUMS -eq $EXPECTED ] ||
4987                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4988 }
4989 run_test 56t "check lfs find -stripe-size works"
4990
4991 test_56u() { # LU-611
4992         TDIR=$DIR/${tdir}u
4993         setup_56 $NUMFILES $NUMDIRS "-i 0"
4994
4995         if [[ $OSTCOUNT -gt 1 ]]; then
4996                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
4997                 ONESTRIPE=4
4998         else
4999                 ONESTRIPE=0
5000         fi
5001
5002         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
5003         CMD="$LFIND -stripe-index 0 -type f $TDIR"
5004         NUMS=$($CMD | wc -l)
5005         [ $NUMS -eq $EXPECTED ] ||
5006                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5007
5008         EXPECTED=$ONESTRIPE
5009         CMD="$LFIND -stripe-index 1 -type f $TDIR"
5010         NUMS=$($CMD | wc -l)
5011         [ $NUMS -eq $EXPECTED ] ||
5012                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5013
5014         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
5015         NUMS=$($CMD | wc -l)
5016         [ $NUMS -eq $EXPECTED ] ||
5017                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5018
5019         EXPECTED=0
5020         # This should produce an error and not return any files
5021         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
5022         NUMS=$($CMD 2>/dev/null | wc -l)
5023         [ $NUMS -eq $EXPECTED ] ||
5024                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5025
5026         if [[ $OSTCOUNT -gt 1 ]]; then
5027                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
5028                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
5029                 NUMS=$($CMD | wc -l)
5030                 [ $NUMS -eq $EXPECTED ] ||
5031                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5032         fi
5033 }
5034 run_test 56u "check lfs find -stripe-index works"
5035
5036 test_56v() {
5037     local MDT_IDX=0
5038
5039     TDIR=$DIR/${tdir}v
5040     rm -rf $TDIR
5041     setup_56 $NUMFILES $NUMDIRS
5042
5043     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
5044     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
5045
5046     for file in $($LFIND -mdt $UUID $TDIR); do
5047         file_mdt_idx=$($GETSTRIPE -M $file)
5048         [ $file_mdt_idx -eq $MDT_IDX ] ||
5049             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
5050     done
5051 }
5052 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
5053
5054 test_56w() {
5055         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5056         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5057         TDIR=$DIR/${tdir}w
5058
5059         rm -rf $TDIR || error "remove $TDIR failed"
5060         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5061
5062         local stripe_size
5063         stripe_size=$($GETSTRIPE -S -d $TDIR) ||
5064                 error "$GETSTRIPE -S -d $TDIR failed"
5065         stripe_size=${stripe_size%% *}
5066
5067         local file_size=$((stripe_size * OSTCOUNT))
5068         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5069         local required_space=$((file_num * file_size))
5070
5071         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5072                            head -n1)
5073         [[ $free_space -le $((required_space / 1024)) ]] &&
5074                 skip_env "need $required_space bytes, have $free_space KB" &&
5075                 return
5076
5077         local dd_bs=65536
5078         local dd_count=$((file_size / dd_bs))
5079
5080         # write data into the files
5081         local i
5082         local j
5083         local file
5084         for i in $(seq 1 $NUMFILES); do
5085                 file=$TDIR/file$i
5086                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5087                         error "write data into $file failed"
5088         done
5089         for i in $(seq 1 $NUMDIRS); do
5090                 for j in $(seq 1 $NUMFILES); do
5091                         file=$TDIR/dir$i/file$j
5092                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5093                                 error "write data into $file failed"
5094                 done
5095         done
5096
5097         # $LFS_MIGRATE will fail if hard link migration is unsupported
5098         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5099                 createmany -l$TDIR/dir1/file1 $TDIR/dir1/link 200 ||
5100                         error "creating links to $TDIR/dir1/file1 failed"
5101         fi
5102
5103         local expected=-1
5104         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5105
5106         # lfs_migrate file
5107         local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
5108         echo "$cmd"
5109         eval $cmd || error "$cmd failed"
5110
5111         check_stripe_count $TDIR/file1 $expected
5112
5113         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5114         then
5115                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5116                 # OST 1 if it is on OST 0. This file is small enough to
5117                 # be on only one stripe.
5118                 file=$TDIR/migr_1_ost
5119                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5120                         error "write data into $file failed"
5121                 local obdidx=$($LFS getstripe -i $file)
5122                 local oldmd5=$(md5sum $file)
5123                 local newobdidx=0
5124                 [[ $obdidx -eq 0 ]] && newobdidx=1
5125                 cmd="$LFS migrate -i $newobdidx $file"
5126                 echo $cmd
5127                 eval $cmd || error "$cmd failed"
5128                 local realobdix=$($LFS getstripe -i $file)
5129                 local newmd5=$(md5sum $file)
5130                 [[ $newobdidx -ne $realobdix ]] &&
5131                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
5132                 [[ "$oldmd5" != "$newmd5" ]] &&
5133                         error "md5sum differ: $oldmd5, $newmd5"
5134         fi
5135
5136     # lfs_migrate dir
5137     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
5138     echo "$cmd"
5139     eval $cmd || error "$cmd failed"
5140
5141     for j in $(seq 1 $NUMFILES); do
5142         check_stripe_count $TDIR/dir1/file$j $expected
5143     done
5144
5145     # lfs_migrate works with lfs find
5146     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
5147          $LFS_MIGRATE -y -c $expected"
5148     echo "$cmd"
5149     eval $cmd || error "$cmd failed"
5150
5151     for i in $(seq 2 $NUMFILES); do
5152         check_stripe_count $TDIR/file$i $expected
5153     done
5154     for i in $(seq 2 $NUMDIRS); do
5155         for j in $(seq 1 $NUMFILES); do
5156             check_stripe_count $TDIR/dir$i/file$j $expected
5157         done
5158     done
5159 }
5160 run_test 56w "check lfs_migrate -c stripe_count works"
5161
5162 test_56x() {
5163         check_swap_layouts_support && return 0
5164         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5165
5166         local dir0=$DIR/$tdir
5167         local ref1=/etc/passwd
5168         local file1=$dir0/file1
5169
5170         test_mkdir $dir0 || error "creating dir $dir0"
5171         $LFS setstripe -c 2 $file1
5172         cp $ref1 $file1
5173         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5174         stripe=$($LFS getstripe -c $file1)
5175         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5176         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5177
5178         # clean up
5179         rm -f $file1
5180 }
5181 run_test 56x "lfs migration support"
5182
5183 test_56xa() {
5184         check_swap_layouts_support && return 0
5185         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5186
5187         local dir0=$DIR/$tdir/$testnum
5188         test_mkdir -p $dir0
5189
5190         local ref1=/etc/passwd
5191         local file1=$dir0/file1
5192
5193         $LFS setstripe -c 2 $file1
5194         cp $ref1 $file1
5195         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5196         local stripe=$($LFS getstripe -c $file1)
5197         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5198         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5199
5200         # clean up
5201         rm -f $file1
5202 }
5203 run_test 56xa "lfs migration --block support"
5204
5205 check_migrate_links() {
5206         local dir="$1"
5207         local file1="$dir/file1"
5208         local begin="$2"
5209         local count="$3"
5210         local total_count=$(($begin + $count - 1))
5211         local symlink_count=10
5212         local uniq_count=10
5213
5214         if [ ! -f "$file1" ]; then
5215                 echo -n "creating initial file..."
5216                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5217                         error "cannot setstripe initial file"
5218                 echo "done"
5219
5220                 echo -n "creating symlinks..."
5221                 for s in $(seq 1 $symlink_count); do
5222                         ln -s "$file1" "$dir/slink$s" ||
5223                                 error "cannot create symlinks"
5224                 done
5225                 echo "done"
5226
5227                 echo -n "creating nonlinked files..."
5228                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5229                         error "cannot create nonlinked files"
5230                 echo "done"
5231         fi
5232
5233         # create hard links
5234         if [ ! -f "$dir/file$total_count" ]; then
5235                 echo -n "creating hard links $begin:$total_count..."
5236                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5237                         /dev/null || error "cannot create hard links"
5238                 echo "done"
5239         fi
5240
5241         echo -n "checking number of hard links listed in xattrs..."
5242         local fid=$($LFS getstripe -F "$file1")
5243         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5244
5245         echo "${#paths[*]}"
5246         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5247                         echo "hard link list has unexpected size, skipping test"
5248                         return 0
5249         fi
5250         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5251                         error "link names should exceed xattrs size"
5252         fi
5253
5254         echo -n "migrating files..."
5255         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5256         local rc=$?
5257         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5258         echo "done"
5259
5260         # make sure all links have been properly migrated
5261         echo -n "verifying files..."
5262         fid=$($LFS getstripe -F "$file1") ||
5263                 error "cannot get fid for file $file1"
5264         for i in $(seq 2 $total_count); do
5265                 local fid2=$($LFS getstripe -F $dir/file$i)
5266                 [ "$fid2" == "$fid" ] ||
5267                         error "migrated hard link has mismatched FID"
5268         done
5269
5270         # make sure hard links were properly detected, and migration was
5271         # performed only once for the entire link set; nonlinked files should
5272         # also be migrated
5273         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5274         local expected=$(($uniq_count + 1))
5275         [ "$actual" -eq  "$expected" ] ||
5276                 error "hard links individually migrated ($actual != $expected)"
5277
5278         # make sure the correct number of hard links are present
5279         local hardlinks=$(stat -c '%h' "$file1")
5280         [ $hardlinks -eq $total_count ] ||
5281                 error "num hard links $hardlinks != $total_count"
5282         echo "done"
5283
5284         return 0
5285 }
5286
5287 test_56xb() {
5288         local dir0="$DIR/$tdir"
5289
5290         test_mkdir "$dir0" || error "cannot create dir $dir0"
5291
5292         echo "testing lfs migrate mode when all links fit within xattrs"
5293         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 2 99
5294
5295         echo "testing rsync mode when all links fit within xattrs"
5296         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 2 99
5297
5298         echo "testing lfs migrate mode when all links do not fit within xattrs"
5299         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 101 100
5300
5301         echo "testing rsync mode when all links do not fit within xattrs"
5302         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 101 100
5303
5304         # clean up
5305         rm -rf $dir0
5306 }
5307 run_test 56xb "lfs migration hard link support"
5308
5309 test_56y() {
5310         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5311                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5312                 return
5313
5314         local res=""
5315         local dir0=$DIR/$tdir/$testnum
5316         test_mkdir -p $dir0
5317         local f1=$dir0/file1
5318         local f2=$dir0/file2
5319
5320         touch $f1 || error "creating std file $f1"
5321         $MULTIOP $f2 H2c || error "creating released file $f2"
5322
5323         # a directory can be raid0, so ask only for files
5324         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5325         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5326
5327         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5328         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5329
5330         # only files can be released, so no need to force file search
5331         res=$($LFIND $dir0 -L released)
5332         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5333
5334         res=$($LFIND $dir0 \! -L released)
5335         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5336
5337 }
5338 run_test 56y "lfs find -L raid0|released"
5339
5340 test_56z() { # LU-4824
5341         # This checks to make sure 'lfs find' continues after errors
5342         # There are two classes of errors that should be caught:
5343         # - If multiple paths are provided, all should be searched even if one
5344         #   errors out
5345         # - If errors are encountered during the search, it should not terminate
5346         #   early
5347         local i
5348         test_mkdir $DIR/$tdir
5349         for i in d{0..9}; do
5350                 test_mkdir $DIR/$tdir/$i
5351         done
5352         touch $DIR/$tdir/d{0..9}/$tfile
5353         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5354                 error "$LFS find did not return an error"
5355         # Make a directory unsearchable. This should NOT be the last entry in
5356         # directory order.  Arbitrarily pick the 6th entry
5357         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5358         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5359         # The user should be able to see 10 directories and 9 files
5360         [ $count == 19 ] || error "$LFS find did not continue after error"
5361 }
5362 run_test 56z "lfs find should continue after an error"
5363
5364 test_56aa() { # LU-5937
5365         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5366
5367         mkdir $DIR/$tdir
5368         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5369
5370         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5371         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5372
5373         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5374 }
5375 run_test 56aa "lfs find --size under striped dir"
5376
5377 test_56ba() {
5378         # Create composite files with one component
5379         TDIR=$DIR/$tdir/1Mfiles
5380         setup_56 5 1 "--component-end 1M"
5381         # Create composite files with three components
5382         TDIR=$DIR/$tdir/2Mfiles
5383         setup_56 5 2 "-E 2M -E 4M -E 6M"
5384         TDIR=$DIR/$tdir
5385         # Create non-composite files
5386         createmany -o $TDIR/${tfile}- 10
5387
5388         local nfiles=$($LFIND --component-end 1M --type f $TDIR | wc -l)
5389         [[ $nfiles == 10 ]] ||
5390                 error "lfs find -E 1M found $nfiles != 10 files"
5391
5392         nfiles=$($LFIND ! -E 1M --type f $TDIR | wc -l)
5393         [[ $nfiles == 25 ]] ||
5394                 error "lfs find ! -E 1M found $nfiles != 25 files"
5395
5396         # All files have a component that starts at 0
5397         local nfiles=$($LFIND --component-start 0 --type f $TDIR | wc -l)
5398         [[ $nfiles == 35 ]] ||
5399                 error "lfs find --component-start 0 found $nfiles != 35 files"
5400
5401         nfiles=$($LFIND --component-start 2M --type f $TDIR | wc -l)
5402         [[ $nfiles == 15 ]] ||
5403                 error "$LFIND --component-start 2M found $nfiles != 15 files"
5404
5405         # All files created here have a componenet that does not starts at 2M
5406         nfiles=$($LFIND ! --component-start 2M --type f $TDIR | wc -l)
5407         [[ $nfiles == 35 ]] ||
5408                 error "$LFIND ! --component-start 2M found $nfiles != 35 files"
5409
5410         # Find files with a specified number of components
5411         local nfiles=$($LFIND --component-count 3 --type f $TDIR | wc -l)
5412         [[ $nfiles == 15 ]] ||
5413                 error "lfs find --component-count 3 found $nfiles != 15 files"
5414
5415         # Remember non-composite files have a component count of zero
5416         local nfiles=$($LFIND --component-count 0 --type f $TDIR | wc -l)
5417         [[ $nfiles == 10 ]] ||
5418                 error "lfs find --component-count 0 found $nfiles != 10 files"
5419
5420         nfiles=$($LFIND ! --component-count 3 --type f $TDIR | wc -l)
5421         [[ $nfiles == 20 ]] ||
5422                 error "$LFIND ! --component-count 3 found $nfiles != 20 files"
5423
5424         # All files have a flag called "init"
5425         local nfiles=$($LFIND --component-flags init --type f $TDIR | wc -l)
5426         [[ $nfiles == 35 ]] ||
5427                 error "$LFIND --component-flags init found $nfiles != 35 files"
5428
5429         # Multi-component files will have a component not initialized
5430         local nfiles=$($LFIND ! --component-flags init --type f $TDIR | wc -l)
5431         [[ $nfiles == 15 ]] ||
5432                 error "$LFIND !--component-flags init found $nfiles != 15 files"
5433
5434         rm -rf $TDIR
5435
5436 }
5437 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
5438
5439 test_57a() {
5440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5441         # note test will not do anything if MDS is not local
5442         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5443                 skip "ldiskfs only test"
5444                 return
5445         fi
5446
5447         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5448         local MNTDEV="osd*.*MDT*.mntdev"
5449         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5450         [ -z "$DEV" ] && error "can't access $MNTDEV"
5451         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5452                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5453                         error "can't access $DEV"
5454                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5455                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5456                 rm $TMP/t57a.dump
5457         done
5458 }
5459 run_test 57a "verify MDS filesystem created with large inodes =="
5460
5461 test_57b() {
5462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5463         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5464                 skip "ldiskfs only test"
5465                 return
5466         fi
5467
5468         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5469         local dir=$DIR/$tdir
5470
5471         local FILECOUNT=100
5472         local FILE1=$dir/f1
5473         local FILEN=$dir/f$FILECOUNT
5474
5475         rm -rf $dir || error "removing $dir"
5476         test_mkdir -c1 $dir
5477         local mdtidx=$($LFS getstripe -M $dir)
5478         local mdtname=MDT$(printf %04x $mdtidx)
5479         local facet=mds$((mdtidx + 1))
5480
5481         echo "mcreating $FILECOUNT files"
5482         createmany -m $dir/f 1 $FILECOUNT || \
5483                 error "creating files in $dir"
5484
5485         # verify that files do not have EAs yet
5486         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5487         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5488
5489         sync
5490         sleep 1
5491         df $dir  #make sure we get new statfs data
5492         local MDSFREE=$(do_facet $facet \
5493                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5494         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5495         echo "opening files to create objects/EAs"
5496         local FILE
5497         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5498                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5499         done
5500
5501         # verify that files have EAs now
5502         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5503         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5504
5505         sleep 1  #make sure we get new statfs data
5506         df $dir
5507         local MDSFREE2=$(do_facet $facet \
5508                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5509         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5510         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5511                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5512                         error "MDC before $MDCFREE != after $MDCFREE2"
5513                 else
5514                         echo "MDC before $MDCFREE != after $MDCFREE2"
5515                         echo "unable to confirm if MDS has large inodes"
5516                 fi
5517         fi
5518         rm -rf $dir
5519 }
5520 run_test 57b "default LOV EAs are stored inside large inodes ==="
5521
5522 test_58() {
5523         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5524         [ -z "$(which wiretest 2>/dev/null)" ] &&
5525                         skip_env "could not find wiretest" && return
5526         wiretest
5527 }
5528 run_test 58 "verify cross-platform wire constants =============="
5529
5530 test_59() {
5531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5532         echo "touch 130 files"
5533         createmany -o $DIR/f59- 130
5534         echo "rm 130 files"
5535         unlinkmany $DIR/f59- 130
5536         sync
5537         # wait for commitment of removal
5538         wait_delete_completed
5539 }
5540 run_test 59 "verify cancellation of llog records async ========="
5541
5542 TEST60_HEAD="test_60 run $RANDOM"
5543 test_60a() {
5544         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5545         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5546         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5547                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5548                         skip_env "missing subtest run-llog.sh" && return
5549
5550         log "$TEST60_HEAD - from kernel mode"
5551         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5552         do_facet mgs "sh run-llog.sh" || error "run-llog.sh failed"
5553         do_facet mgs $LCTL dk > $TMP/$tfile
5554
5555         # LU-6388: test llog_reader
5556         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5557         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5558         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5559                         skip_env "missing llog_reader" && return
5560         local fstype=$(facet_fstype mgs)
5561         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5562                 skip_env "Only for ldiskfs or zfs type mgs" && return
5563
5564         local mntpt=$(facet_mntpt mgs)
5565         local mgsdev=$(mgsdevname 1)
5566         local fid_list
5567         local fid
5568         local rec_list
5569         local rec
5570         local rec_type
5571         local obj_file
5572         local path
5573         local seq
5574         local oid
5575         local pass=true
5576
5577         #get fid and record list
5578         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5579                 tail -n 4))
5580         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5581                 tail -n 4))
5582         #remount mgs as ldiskfs or zfs type
5583         stop mgs || error "stop mgs failed"
5584         mount_fstype mgs || error "remount mgs failed"
5585         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5586                 fid=${fid_list[i]}
5587                 rec=${rec_list[i]}
5588                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5589                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5590                 oid=$((16#$oid))
5591
5592                 case $fstype in
5593                         ldiskfs )
5594                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5595                         zfs )
5596                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5597                 esac
5598                 echo "obj_file is $obj_file"
5599                 do_facet mgs $llog_reader $obj_file
5600
5601                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5602                         awk '{ print $3 }' | sed -e "s/^type=//g")
5603                 if [ $rec_type != $rec ]; then
5604                         echo "FAILED test_60a wrong record type $rec_type," \
5605                               "should be $rec"
5606                         pass=false
5607                         break
5608                 fi
5609
5610                 #check obj path if record type is LLOG_LOGID_MAGIC
5611                 if [ "$rec" == "1064553b" ]; then
5612                         path=$(do_facet mgs $llog_reader $obj_file |
5613                                 grep "path=" | awk '{ print $NF }' |
5614                                 sed -e "s/^path=//g")
5615                         if [ $obj_file != $mntpt/$path ]; then
5616                                 echo "FAILED test_60a wrong obj path" \
5617                                       "$montpt/$path, should be $obj_file"
5618                                 pass=false
5619                                 break
5620                         fi
5621                 fi
5622         done
5623         rm -f $TMP/$tfile
5624         #restart mgs before "error", otherwise it will block the next test
5625         stop mgs || error "stop mgs failed"
5626         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5627         $pass || error "test failed, see FAILED test_60a messages for specifics"
5628 }
5629 run_test 60a "llog_test run from kernel module and test llog_reader"
5630
5631 test_60aa() {
5632         # test old logid format
5633         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5634                 do_facet mgs $LCTL dl | grep MGS
5635                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5636                         error "old llog_print failed"
5637         fi
5638
5639         # test new logid format
5640         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5641                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5642                         error "new llog_print failed"
5643         fi
5644 }
5645 run_test 60aa "llog_print works with FIDs and simple names"
5646
5647 test_60b() { # bug 6411
5648         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5649         dmesg > $DIR/$tfile
5650         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5651                                 /llog.test/ {
5652                                         if (marker)
5653                                                 from_marker++
5654                                         from_begin++
5655                                 }
5656                                 END {
5657                                         if (marker)
5658                                                 print from_marker
5659                                         else
5660                                                 print from_begin
5661                                 }")
5662         [[ $LLOG_COUNT -gt 100 ]] &&
5663                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5664 }
5665 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5666
5667 test_60c() {
5668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5669         echo "create 5000 files"
5670         createmany -o $DIR/f60c- 5000
5671 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5672         lctl set_param fail_loc=0x80000137
5673         unlinkmany $DIR/f60c- 5000
5674         lctl set_param fail_loc=0
5675 }
5676 run_test 60c "unlink file when mds full"
5677
5678 test_60d() {
5679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5680         SAVEPRINTK=$(lctl get_param -n printk)
5681
5682         # verify "lctl mark" is even working"
5683         MESSAGE="test message ID $RANDOM $$"
5684         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5685         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5686
5687         lctl set_param printk=0 || error "set lnet.printk failed"
5688         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5689         MESSAGE="new test message ID $RANDOM $$"
5690         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5691         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5692         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5693
5694         lctl set_param -n printk="$SAVEPRINTK"
5695 }
5696 run_test 60d "test printk console message masking"
5697
5698 test_60e() {
5699         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5700         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5701         touch $DIR/$tfile
5702 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5703         do_facet mds1 lctl set_param fail_loc=0x15b
5704         rm $DIR/$tfile
5705 }
5706 run_test 60e "no space while new llog is being created"
5707
5708 test_61() {
5709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5710         f="$DIR/f61"
5711         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5712         cancel_lru_locks osc
5713         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5714         sync
5715 }
5716 run_test 61 "mmap() writes don't make sync hang ================"
5717
5718 # bug 2330 - insufficient obd_match error checking causes LBUG
5719 test_62() {
5720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5721         f="$DIR/f62"
5722         echo foo > $f
5723         cancel_lru_locks osc
5724         lctl set_param fail_loc=0x405
5725         cat $f && error "cat succeeded, expect -EIO"
5726         lctl set_param fail_loc=0
5727 }
5728 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5729 # match every page all of the time.
5730 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5731
5732 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5733 # Though this test is irrelevant anymore, it helped to reveal some
5734 # other grant bugs (LU-4482), let's keep it.
5735 test_63a() {   # was test_63
5736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5737         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5738         for i in `seq 10` ; do
5739                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5740                 sleep 5
5741                 kill $!
5742                 sleep 1
5743         done
5744
5745         rm -f $DIR/f63 || true
5746 }
5747 run_test 63a "Verify oig_wait interruption does not crash ======="
5748
5749 # bug 2248 - async write errors didn't return to application on sync
5750 # bug 3677 - async write errors left page locked
5751 test_63b() {
5752         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5753         debugsave
5754         lctl set_param debug=-1
5755
5756         # ensure we have a grant to do async writes
5757         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5758         rm $DIR/$tfile
5759
5760         sync    # sync lest earlier test intercept the fail_loc
5761
5762         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5763         lctl set_param fail_loc=0x80000406
5764         $MULTIOP $DIR/$tfile Owy && \
5765                 error "sync didn't return ENOMEM"
5766         sync; sleep 2; sync     # do a real sync this time to flush page
5767         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5768                 error "locked page left in cache after async error" || true
5769         debugrestore
5770 }
5771 run_test 63b "async write errors should be returned to fsync ==="
5772
5773 test_64a () {
5774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5775         df $DIR
5776         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5777 }
5778 run_test 64a "verify filter grant calculations (in kernel) ====="
5779
5780 test_64b () {
5781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5782         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5783 }
5784 run_test 64b "check out-of-space detection on client"
5785
5786 test_64c() {
5787         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5788 }
5789 run_test 64c "verify grant shrink"
5790
5791 # bug 1414 - set/get directories' stripe info
5792 test_65a() {
5793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5794         test_mkdir $DIR/$tdir
5795         touch $DIR/$tdir/f1
5796         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5797 }
5798 run_test 65a "directory with no stripe info"
5799
5800 test_65b() {
5801         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5802         test_mkdir $DIR/$tdir
5803         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5804
5805         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5806                                                 error "setstripe"
5807         touch $DIR/$tdir/f2
5808         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5809 }
5810 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
5811
5812 test_65c() {
5813         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5814         [ $OSTCOUNT -lt 2 ] && skip "need at least 2 OSTs" && return
5815         test_mkdir $DIR/$tdir
5816         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
5817
5818         $LFS setstripe -S $((stripesize * 4)) -i 1 \
5819                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5820         touch $DIR/$tdir/f3
5821         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5822 }
5823 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
5824
5825 test_65d() {
5826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5827         test_mkdir $DIR/$tdir
5828         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
5829         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5830
5831         if [[ $STRIPECOUNT -le 0 ]]; then
5832                 sc=1
5833         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5834 #LOV_MAX_STRIPE_COUNT is 2000
5835                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5836         else
5837                 sc=$(($STRIPECOUNT - 1))
5838         fi
5839         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5840         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5841         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5842                 error "lverify failed"
5843 }
5844 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
5845
5846 test_65e() {
5847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5848         test_mkdir $DIR/$tdir
5849
5850         $SETSTRIPE $DIR/$tdir || error "setstripe"
5851         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5852                                         error "no stripe info failed"
5853         touch $DIR/$tdir/f6
5854         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5855 }
5856 run_test 65e "directory setstripe defaults"
5857
5858 test_65f() {
5859         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5860         test_mkdir $DIR/${tdir}f
5861         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5862 }
5863 run_test 65f "dir setstripe permission (should return error) ==="
5864
5865 test_65g() {
5866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5867         test_mkdir $DIR/$tdir
5868         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5869
5870         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5871                 error "setstripe -S failed"
5872         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
5873         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
5874                 error "delete default stripe failed"
5875 }
5876 run_test 65g "directory setstripe -d"
5877
5878 test_65h() {
5879         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5880         test_mkdir $DIR/$tdir
5881         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5882
5883         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5884                 error "setstripe -S failed"
5885         test_mkdir $DIR/$tdir/dd1
5886         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5887                 error "stripe info inherit failed"
5888 }
5889 run_test 65h "directory stripe info inherit ===================="
5890
5891 test_65i() { # bug6367
5892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5893         $SETSTRIPE -S 65536 -c -1 $MOUNT
5894 }
5895 run_test 65i "set non-default striping on root directory (bug 6367)="
5896
5897 test_65ia() { # bug12836
5898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5899         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5900 }
5901 run_test 65ia "getstripe on -1 default directory striping"
5902
5903 test_65ib() { # bug12836
5904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5905         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5906 }
5907 run_test 65ib "getstripe -v on -1 default directory striping"
5908
5909 test_65ic() { # bug12836
5910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5911         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5912 }
5913 run_test 65ic "new find on -1 default directory striping"
5914
5915 test_65j() { # bug6367
5916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5917         sync; sleep 1
5918         # if we aren't already remounting for each test, do so for this test
5919         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5920                 cleanup || error "failed to unmount"
5921                 setup
5922         fi
5923         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5924 }
5925 run_test 65j "set default striping on root directory (bug 6367)="
5926
5927 test_65k() { # bug11679
5928         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5929         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5930         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5931
5932         local disable_precreate=true
5933         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
5934                 disable_precreate=false
5935
5936         echo "Check OST status: "
5937         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
5938                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
5939
5940         for OSC in $MDS_OSCS; do
5941                 echo $OSC "is active"
5942                 do_facet $SINGLEMDS lctl --device %$OSC activate
5943         done
5944
5945         for INACTIVE_OSC in $MDS_OSCS; do
5946                 local ost=$(osc_to_ost $INACTIVE_OSC)
5947                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
5948                                lov.*md*.target_obd |
5949                                awk -F: /$ost/'{ print $1 }' | head -n 1)
5950
5951                 mkdir -p $DIR/$tdir
5952                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
5953                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
5954
5955                 echo "Deactivate: " $INACTIVE_OSC
5956                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5957
5958                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
5959                               osp.$ost*MDT0000.create_count")
5960                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
5961                                   osp.$ost*MDT0000.max_create_count")
5962                 $disable_precreate &&
5963                         do_facet $SINGLEMDS "lctl set_param -n \
5964                                 osp.$ost*MDT0000.max_create_count=0"
5965
5966                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
5967                         [ -f $DIR/$tdir/$idx ] && continue
5968                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
5969                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
5970                                 error "setstripe $idx should succeed"
5971                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
5972                 done
5973                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
5974                 rmdir $DIR/$tdir
5975
5976                 do_facet $SINGLEMDS "lctl set_param -n \
5977                         osp.$ost*MDT0000.max_create_count=$max_count"
5978                 do_facet $SINGLEMDS "lctl set_param -n \
5979                         osp.$ost*MDT0000.create_count=$count"
5980                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5981                 echo $INACTIVE_OSC "is Activate"
5982
5983                 wait_osc_import_state mds ost$ostnum FULL
5984         done
5985 }
5986 run_test 65k "validate manual striping works properly with deactivated OSCs"
5987
5988 test_65l() { # bug 12836
5989         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5990         test_mkdir -p $DIR/$tdir/test_dir
5991         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5992         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5993 }
5994 run_test 65l "lfs find on -1 stripe dir ========================"
5995
5996 test_65m() {
5997         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
5998         true
5999 }
6000 run_test 65m "normal user can't set filesystem default stripe"
6001
6002 # bug 2543 - update blocks count on client
6003 test_66() {
6004         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6005         COUNT=${COUNT:-8}
6006         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6007         sync; sync_all_data; sync; sync_all_data
6008         cancel_lru_locks osc
6009         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6010         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6011 }
6012 run_test 66 "update inode blocks count on client ==============="
6013
6014 meminfo() {
6015         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6016 }
6017
6018 swap_used() {
6019         swapon -s | awk '($1 == "'$1'") { print $4 }'
6020 }
6021
6022 # bug5265, obdfilter oa2dentry return -ENOENT
6023 # #define OBD_FAIL_SRV_ENOENT 0x217
6024 test_69() {
6025         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6026         remote_ost_nodsh && skip "remote OST with nodsh" && return
6027
6028         f="$DIR/$tfile"
6029         $SETSTRIPE -c 1 -i 0 $f
6030
6031         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6032
6033         do_facet ost1 lctl set_param fail_loc=0x217
6034         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6035         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6036
6037         do_facet ost1 lctl set_param fail_loc=0
6038         $DIRECTIO write $f 0 2 || error "write error"
6039
6040         cancel_lru_locks osc
6041         $DIRECTIO read $f 0 1 || error "read error"
6042
6043         do_facet ost1 lctl set_param fail_loc=0x217
6044         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6045
6046         do_facet ost1 lctl set_param fail_loc=0
6047         rm -f $f
6048 }
6049 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6050
6051 test_71() {
6052         test_mkdir $DIR/$tdir
6053         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6054         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6055 }
6056 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6057
6058 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6059         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6060         [ "$RUNAS_ID" = "$UID" ] &&
6061                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6062
6063         # Check that testing environment is properly set up. Skip if not
6064         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
6065                 skip_env "User $RUNAS_ID does not exist - skipping"
6066                 return 0
6067         }
6068         touch $DIR/$tfile
6069         chmod 777 $DIR/$tfile
6070         chmod ug+s $DIR/$tfile
6071         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6072                 error "$RUNAS dd $DIR/$tfile failed"
6073         # See if we are still setuid/sgid
6074         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6075                 error "S/gid is not dropped on write"
6076         # Now test that MDS is updated too
6077         cancel_lru_locks mdc
6078         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6079                 error "S/gid is not dropped on MDS"
6080         rm -f $DIR/$tfile
6081 }
6082 run_test 72a "Test that remove suid works properly (bug5695) ===="
6083
6084 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6085         local perm
6086
6087         [ "$RUNAS_ID" = "$UID" ] && \
6088                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6089         [ "$RUNAS_ID" -eq 0 ] && \
6090                 skip_env "RUNAS_ID = 0 -- skipping" && return
6091
6092         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6093         # Check that testing environment is properly set up. Skip if not
6094         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
6095                 skip_env "User $RUNAS_ID does not exist - skipping"
6096                 return 0
6097         }
6098         touch $DIR/${tfile}-f{g,u}
6099         test_mkdir $DIR/${tfile}-dg
6100         test_mkdir $DIR/${tfile}-du
6101         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6102         chmod g+s $DIR/${tfile}-{f,d}g
6103         chmod u+s $DIR/${tfile}-{f,d}u
6104         for perm in 777 2777 4777; do
6105                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6106                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6107                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6108                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6109         done
6110         true
6111 }
6112 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6113
6114 # bug 3462 - multiple simultaneous MDC requests
6115 test_73() {
6116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6117         test_mkdir $DIR/d73-1
6118         test_mkdir $DIR/d73-2
6119         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6120         pid1=$!
6121
6122         lctl set_param fail_loc=0x80000129
6123         $MULTIOP $DIR/d73-1/f73-2 Oc &
6124         sleep 1
6125         lctl set_param fail_loc=0
6126
6127         $MULTIOP $DIR/d73-2/f73-3 Oc &
6128         pid3=$!
6129
6130         kill -USR1 $pid1
6131         wait $pid1 || return 1
6132
6133         sleep 25
6134
6135         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6136         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6137         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6138
6139         rm -rf $DIR/d73-*
6140 }
6141 run_test 73 "multiple MDC requests (should not deadlock)"
6142
6143 test_74a() { # bug 6149, 6184
6144         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6145         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6146         #
6147         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6148         # will spin in a tight reconnection loop
6149         touch $DIR/f74a
6150         $LCTL set_param fail_loc=0x8000030e
6151         # get any lock that won't be difficult - lookup works.
6152         ls $DIR/f74a
6153         $LCTL set_param fail_loc=0
6154         rm -f $DIR/f74a
6155         true
6156 }
6157 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6158
6159 test_74b() { # bug 13310
6160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6161         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6162         #
6163         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6164         # will spin in a tight reconnection loop
6165         $LCTL set_param fail_loc=0x8000030e
6166         # get a "difficult" lock
6167         touch $DIR/f74b
6168         $LCTL set_param fail_loc=0
6169         rm -f $DIR/f74b
6170         true
6171 }
6172 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6173
6174 test_74c() {
6175         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6176         #define OBD_FAIL_LDLM_NEW_LOCK
6177         $LCTL set_param fail_loc=0x319
6178         touch $DIR/$tfile && error "touch successful"
6179         $LCTL set_param fail_loc=0
6180         true
6181 }
6182 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6183
6184 num_inodes() {
6185         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6186 }
6187
6188 test_76() { # Now for bug 20433, added originally in bug 1443
6189         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6190         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6191         cancel_lru_locks osc
6192         BEFORE_INODES=$(num_inodes)
6193         echo "before inodes: $BEFORE_INODES"
6194         local COUNT=1000
6195         [ "$SLOW" = "no" ] && COUNT=100
6196         for i in $(seq $COUNT); do
6197                 touch $DIR/$tfile
6198                 rm -f $DIR/$tfile
6199         done
6200         cancel_lru_locks osc
6201         AFTER_INODES=$(num_inodes)
6202         echo "after inodes: $AFTER_INODES"
6203         local wait=0
6204         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6205                 sleep 2
6206                 AFTER_INODES=$(num_inodes)
6207                 wait=$((wait+2))
6208                 echo "wait $wait seconds inodes: $AFTER_INODES"
6209                 if [ $wait -gt 30 ]; then
6210                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6211                 fi
6212         done
6213 }
6214 run_test 76 "confirm clients recycle inodes properly ===="
6215
6216
6217 export ORIG_CSUM=""
6218 set_checksums()
6219 {
6220         # Note: in sptlrpc modes which enable its own bulk checksum, the
6221         # original crc32_le bulk checksum will be automatically disabled,
6222         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6223         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6224         # In this case set_checksums() will not be no-op, because sptlrpc
6225         # bulk checksum will be enabled all through the test.
6226
6227         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6228         lctl set_param -n osc.*.checksums $1
6229         return 0
6230 }
6231
6232 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6233                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6234 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6235 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6236 set_checksum_type()
6237 {
6238         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6239         log "set checksum type to $1"
6240         return 0
6241 }
6242 F77_TMP=$TMP/f77-temp
6243 F77SZ=8
6244 setup_f77() {
6245         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6246                 error "error writing to $F77_TMP"
6247 }
6248
6249 test_77a() { # bug 10889
6250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6251         $GSS && skip "could not run with gss" && return
6252         [ ! -f $F77_TMP ] && setup_f77
6253         set_checksums 1
6254         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6255         set_checksums 0
6256         rm -f $DIR/$tfile
6257 }
6258 run_test 77a "normal checksum read/write operation"
6259
6260 test_77b() { # bug 10889
6261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6262         $GSS && skip "could not run with gss" && return
6263         [ ! -f $F77_TMP ] && setup_f77
6264         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6265         $LCTL set_param fail_loc=0x80000409
6266         set_checksums 1
6267
6268         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6269                 error "dd error: $?"
6270         $LCTL set_param fail_loc=0
6271
6272         for algo in $CKSUM_TYPES; do
6273                 cancel_lru_locks osc
6274                 set_checksum_type $algo
6275                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6276                 $LCTL set_param fail_loc=0x80000408
6277                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6278                 $LCTL set_param fail_loc=0
6279         done
6280         set_checksums 0
6281         set_checksum_type $ORIG_CSUM_TYPE
6282         rm -f $DIR/$tfile
6283 }
6284 run_test 77b "checksum error on client write, read"
6285
6286 cleanup_77c() {
6287         trap 0
6288         set_checksums 0
6289         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
6290         $check_ost &&
6291                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
6292         [ -n $osc_file_prefix ] && rm -f ${osc_file_prefix}*
6293         $check_ost && [ -n $ost_file_prefix ] &&
6294                 do_facet ost1 rm -f ${ost_file_prefix}\*
6295 }
6296
6297 test_77c() {
6298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6299         $GSS && skip "could not run with gss" && return
6300
6301         local bad1
6302         local osc_file_prefix
6303         local osc_file
6304         local check_ost=false
6305         local ost_file_prefix
6306         local ost_file
6307         local orig_cksum
6308         local dump_cksum
6309         local fid
6310
6311         # ensure corruption will occur on first OSS/OST
6312         $LFS setstripe -i 0 $DIR/$tfile
6313
6314         [ ! -f $F77_TMP ] && setup_f77
6315         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6316                 error "dd write error: $?"
6317         fid=$($LFS path2fid $DIR/$tfile)
6318
6319         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
6320         then
6321                 check_ost=true
6322                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
6323                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
6324         else
6325                 echo "OSS do not support bulk pages dump upon error"
6326         fi
6327
6328         osc_file_prefix=$($LCTL get_param -n debug_path)
6329         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
6330
6331         trap cleanup_77c EXIT
6332
6333         set_checksums 1
6334         # enable bulk pages dump upon error on Client
6335         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
6336         # enable bulk pages dump upon error on OSS
6337         $check_ost &&
6338                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
6339
6340         # flush Client cache to allow next read to reach OSS
6341         cancel_lru_locks osc
6342
6343         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
6344         $LCTL set_param fail_loc=0x80000408
6345         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
6346         $LCTL set_param fail_loc=0
6347
6348         rm -f $DIR/$tfile
6349
6350         # check cksum dump on Client
6351         osc_file=$(ls ${osc_file_prefix}*)
6352         [ -n "$osc_file" ] || error "no checksum dump file on Client"
6353         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
6354         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
6355         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
6356         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
6357                      cksum)
6358         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
6359         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6360                 error "dump content does not match on Client"
6361
6362         $check_ost || skip "No need to check cksum dump on OSS"
6363
6364         # check cksum dump on OSS
6365         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
6366         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
6367         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
6368         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
6369         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6370                 error "dump content does not match on OSS"
6371
6372         cleanup_77c
6373 }
6374 run_test 77c "checksum error on client read with debug"
6375
6376 test_77d() { # bug 10889
6377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6378         $GSS && skip "could not run with gss" && return
6379         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6380         $LCTL set_param fail_loc=0x80000409
6381         set_checksums 1
6382         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6383                 error "direct write: rc=$?"
6384         $LCTL set_param fail_loc=0
6385         set_checksums 0
6386
6387         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6388         $LCTL set_param fail_loc=0x80000408
6389         set_checksums 1
6390         cancel_lru_locks osc
6391         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6392                 error "direct read: rc=$?"
6393         $LCTL set_param fail_loc=0
6394         set_checksums 0
6395 }
6396 run_test 77d "checksum error on OST direct write, read"
6397
6398 test_77f() { # bug 10889
6399         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6400         $GSS && skip "could not run with gss" && return
6401         set_checksums 1
6402         for algo in $CKSUM_TYPES; do
6403                 cancel_lru_locks osc
6404                 set_checksum_type $algo
6405                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6406                 $LCTL set_param fail_loc=0x409
6407                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6408                         error "direct write succeeded"
6409                 $LCTL set_param fail_loc=0
6410         done
6411         set_checksum_type $ORIG_CSUM_TYPE
6412         set_checksums 0
6413 }
6414 run_test 77f "repeat checksum error on write (expect error)"
6415
6416 test_77g() { # bug 10889
6417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6418         $GSS && skip "could not run with gss" && return
6419         remote_ost_nodsh && skip "remote OST with nodsh" && return
6420
6421         [ ! -f $F77_TMP ] && setup_f77
6422
6423         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6424         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6425         do_facet ost1 lctl set_param fail_loc=0x8000021a
6426         set_checksums 1
6427         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6428                 error "write error: rc=$?"
6429         do_facet ost1 lctl set_param fail_loc=0
6430         set_checksums 0
6431
6432         cancel_lru_locks osc
6433         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6434         do_facet ost1 lctl set_param fail_loc=0x8000021b
6435         set_checksums 1
6436         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6437         do_facet ost1 lctl set_param fail_loc=0
6438         set_checksums 0
6439 }
6440 run_test 77g "checksum error on OST write, read"
6441
6442 test_77j() { # bug 13805
6443         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6444         $GSS && skip "could not run with gss" && return
6445         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6446         lctl set_param fail_loc=0x40c
6447         remount_client $MOUNT
6448         lctl set_param fail_loc=0
6449         # wait async osc connect to finish and reflect updated state value
6450         local i
6451         for (( i=0; i < OSTCOUNT; i++ )) ; do
6452                 wait_osc_import_state client ost$((i+1)) FULL
6453         done
6454
6455         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6456                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6457                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6458                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6459         done
6460         remount_client $MOUNT
6461 }
6462 run_test 77j "client only supporting ADLER32"
6463
6464 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6465 rm -f $F77_TMP
6466 unset F77_TMP
6467
6468 cleanup_test_78() {
6469         trap 0
6470         rm -f $DIR/$tfile
6471 }
6472
6473 test_78() { # bug 10901
6474         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6475         remote_ost || { skip_env "local OST" && return; }
6476
6477         NSEQ=5
6478         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6479         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6480         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6481         echo "MemTotal: $MEMTOTAL"
6482
6483         # reserve 256MB of memory for the kernel and other running processes,
6484         # and then take 1/2 of the remaining memory for the read/write buffers.
6485         if [ $MEMTOTAL -gt 512 ] ;then
6486                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6487         else
6488                 # for those poor memory-starved high-end clusters...
6489                 MEMTOTAL=$((MEMTOTAL / 2))
6490         fi
6491         echo "Mem to use for directio: $MEMTOTAL"
6492
6493         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6494         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6495         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6496         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6497                 head -n1)
6498         echo "Smallest OST: $SMALLESTOST"
6499         [[ $SMALLESTOST -lt 10240 ]] &&
6500                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6501
6502         trap cleanup_test_78 EXIT
6503
6504         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6505                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6506
6507         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6508         echo "File size: $F78SIZE"
6509         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6510         for i in $(seq 1 $NSEQ); do
6511                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6512                 echo directIO rdwr round $i of $NSEQ
6513                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6514         done
6515
6516         cleanup_test_78
6517 }
6518 run_test 78 "handle large O_DIRECT writes correctly ============"
6519
6520 test_79() { # bug 12743
6521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6522         wait_delete_completed
6523
6524         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6525         BKFREE=$(calc_osc_kbytes kbytesfree)
6526         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6527
6528         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6529         DFTOTAL=`echo $STRING | cut -d, -f1`
6530         DFUSED=`echo $STRING  | cut -d, -f2`
6531         DFAVAIL=`echo $STRING | cut -d, -f3`
6532         DFFREE=$(($DFTOTAL - $DFUSED))
6533
6534         ALLOWANCE=$((64 * $OSTCOUNT))
6535
6536         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6537            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6538                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6539         fi
6540         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6541            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6542                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6543         fi
6544         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6545            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6546                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6547         fi
6548 }
6549 run_test 79 "df report consistency check ======================="
6550
6551 test_80() { # bug 10718
6552         remote_ost_nodsh && skip "remote OST with nodsh" && return
6553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6554         # relax strong synchronous semantics for slow backends like ZFS
6555         local soc="obdfilter.*.sync_on_lock_cancel"
6556         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6557         local hosts=
6558         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6559                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6560                           facet_active_host $host; done | sort -u)
6561                 do_nodes $hosts lctl set_param $soc=never
6562         fi
6563
6564         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6565         sync; sleep 1; sync
6566         local BEFORE=`date +%s`
6567         cancel_lru_locks osc
6568         local AFTER=`date +%s`
6569         local DIFF=$((AFTER-BEFORE))
6570         if [ $DIFF -gt 1 ] ; then
6571                 error "elapsed for 1M@1T = $DIFF"
6572         fi
6573
6574         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6575
6576         rm -f $DIR/$tfile
6577 }
6578 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6579
6580 test_81a() { # LU-456
6581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6582         remote_ost_nodsh && skip "remote OST with nodsh" && return
6583         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6584         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6585         do_facet ost1 lctl set_param fail_loc=0x80000228
6586
6587         # write should trigger a retry and success
6588         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6589         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6590         RC=$?
6591         if [ $RC -ne 0 ] ; then
6592                 error "write should success, but failed for $RC"
6593         fi
6594 }
6595 run_test 81a "OST should retry write when get -ENOSPC ==============="
6596
6597 test_81b() { # LU-456
6598         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6599         remote_ost_nodsh && skip "remote OST with nodsh" && return
6600         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6601         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6602         do_facet ost1 lctl set_param fail_loc=0x228
6603
6604         # write should retry several times and return -ENOSPC finally
6605         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6606         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6607         RC=$?
6608         ENOSPC=28
6609         if [ $RC -ne $ENOSPC ] ; then
6610                 error "dd should fail for -ENOSPC, but succeed."
6611         fi
6612 }
6613 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6614
6615 test_82() { # LU-1031
6616         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6617         local gid1=14091995
6618         local gid2=16022000
6619
6620         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6621         local MULTIPID1=$!
6622         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6623         local MULTIPID2=$!
6624         kill -USR1 $MULTIPID2
6625         sleep 2
6626         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6627                 error "First grouplock does not block second one"
6628         else
6629                 echo "Second grouplock blocks first one"
6630         fi
6631         kill -USR1 $MULTIPID1
6632         wait $MULTIPID1
6633         wait $MULTIPID2
6634 }
6635 run_test 82 "Basic grouplock test"
6636
6637 test_83() {
6638         local sfile="/boot/System.map-$(uname -r)"
6639         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
6640         $LCTL set_param fail_loc=0x140d
6641         cp $sfile $DIR/$tfile || error "write failed"
6642         diff -c $sfile $DIR/$tfile || error "files are different"
6643         $LCTL set_param fail_loc=0
6644         rm -f $DIR/$tfile
6645 }
6646 run_test 83 "Short write in ptask ==============================="
6647
6648 test_99() {
6649         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6650                 return
6651         test_mkdir $DIR/$tdir.cvsroot
6652         chown $RUNAS_ID $DIR/$tdir.cvsroot
6653
6654         cd $TMP
6655         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
6656
6657         cd /etc/init.d
6658         # some versions of cvs import exit(1) when asked to import links or
6659         # files they can't read.  ignore those files.
6660         local toignore=$(find . -type l -printf '-I %f\n' -o \
6661                          ! -perm /4 -printf '-I %f\n')
6662         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
6663                 $tdir.reposname vtag rtag
6664
6665         cd $DIR
6666         test_mkdir $DIR/$tdir.reposname
6667         chown $RUNAS_ID $DIR/$tdir.reposname
6668         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
6669
6670         cd $DIR/$tdir.reposname
6671         $RUNAS touch foo99
6672         $RUNAS cvs add -m 'addmsg' foo99
6673         $RUNAS cvs update
6674         $RUNAS cvs commit -m 'nomsg' foo99
6675         rm -fr $DIR/$tdir.cvsroot
6676 }
6677 run_test 99 "cvs strange file/directory operations"
6678
6679 test_100() {
6680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6681         [[ "$NETTYPE" =~ tcp ]] ||
6682                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" &&
6683                         return ; }
6684
6685         remote_ost_nodsh && skip "remote OST with nodsh" && return
6686         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6687         remote_servers ||
6688                 { skip "useless for local single node setup" && return; }
6689
6690         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6691                 [ "$PROT" != "tcp" ] && continue
6692                 RPORT=$(echo $REMOTE | cut -d: -f2)
6693                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6694
6695                 rc=0
6696                 LPORT=`echo $LOCAL | cut -d: -f2`
6697                 if [ $LPORT -ge 1024 ]; then
6698                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6699                         netstat -tna
6700                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6701                 fi
6702         done
6703         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6704 }
6705 run_test 100 "check local port using privileged port ==========="
6706
6707 function get_named_value()
6708 {
6709     local tag
6710
6711     tag=$1
6712     while read ;do
6713         line=$REPLY
6714         case $line in
6715         $tag*)
6716             echo $line | sed "s/^$tag[ ]*//"
6717             break
6718             ;;
6719         esac
6720     done
6721 }
6722
6723 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6724                    awk '/^max_cached_mb/ { print $2 }')
6725
6726 cleanup_101a() {
6727         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6728         trap 0
6729 }
6730
6731 test_101a() {
6732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6733         [ $MDSCOUNT -ge 2 ] && skip "needs < 2 MDTs" && return #LU-4322
6734         local s
6735         local discard
6736         local nreads=10000
6737         local cache_limit=32
6738
6739         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6740         trap cleanup_101a EXIT
6741         $LCTL set_param -n llite.*.read_ahead_stats 0
6742         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6743
6744         #
6745         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6746         #
6747         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6748         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6749
6750         discard=0
6751         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6752                 get_named_value 'read but discarded' | cut -d" " -f1); do
6753                         discard=$(($discard + $s))
6754         done
6755         cleanup_101a
6756
6757         if [[ $(($discard * 10)) -gt $nreads ]]; then
6758                 $LCTL get_param osc.*-osc*.rpc_stats
6759                 $LCTL get_param llite.*.read_ahead_stats
6760                 error "too many ($discard) discarded pages"
6761         fi
6762         rm -f $DIR/$tfile || true
6763 }
6764 run_test 101a "check read-ahead for random reads"
6765
6766 setup_test101bc() {
6767         test_mkdir $DIR/$tdir
6768         local STRIPE_SIZE=$1
6769         local FILE_LENGTH=$2
6770         STRIPE_OFFSET=0
6771
6772         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6773
6774         local list=$(comma_list $(osts_nodes))
6775         set_osd_param $list '' read_cache_enable 0
6776         set_osd_param $list '' writethrough_cache_enable 0
6777
6778         trap cleanup_test101bc EXIT
6779         # prepare the read-ahead file
6780         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6781
6782         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6783                                 count=$FILE_SIZE_MB 2> /dev/null
6784
6785 }
6786
6787 cleanup_test101bc() {
6788         trap 0
6789         rm -rf $DIR/$tdir
6790         rm -f $DIR/$tfile
6791
6792         local list=$(comma_list $(osts_nodes))
6793         set_osd_param $list '' read_cache_enable 1
6794         set_osd_param $list '' writethrough_cache_enable 1
6795 }
6796
6797 calc_total() {
6798         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6799 }
6800
6801 ra_check_101() {
6802         local READ_SIZE=$1
6803         local STRIPE_SIZE=$2
6804         local FILE_LENGTH=$3
6805         local RA_INC=1048576
6806         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6807         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6808                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6809         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6810                         get_named_value 'read but discarded' |
6811                         cut -d" " -f1 | calc_total)
6812         if [[ $DISCARD -gt $discard_limit ]]; then
6813                 $LCTL get_param llite.*.read_ahead_stats
6814                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6815         else
6816                 echo "Read-ahead success for size ${READ_SIZE}"
6817         fi
6818 }
6819
6820 test_101b() {
6821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6822         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6823         local STRIPE_SIZE=1048576
6824         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6825         if [ $SLOW == "yes" ]; then
6826                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6827         else
6828                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6829         fi
6830
6831         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6832
6833         # prepare the read-ahead file
6834         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6835         cancel_lru_locks osc
6836         for BIDX in 2 4 8 16 32 64 128 256
6837         do
6838                 local BSIZE=$((BIDX*4096))
6839                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6840                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6841                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6842                 $LCTL set_param -n llite.*.read_ahead_stats 0
6843                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6844                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6845                 cancel_lru_locks osc
6846                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6847         done
6848         cleanup_test101bc
6849         true
6850 }
6851 run_test 101b "check stride-io mode read-ahead ================="
6852
6853 test_101c() {
6854         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6855         local STRIPE_SIZE=1048576
6856         local FILE_LENGTH=$((STRIPE_SIZE*100))
6857         local nreads=10000
6858         local osc_rpc_stats
6859
6860         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6861
6862         cancel_lru_locks osc
6863         $LCTL set_param osc.*.rpc_stats 0
6864         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6865         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6866                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6867                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6868                 local size
6869
6870                 if [ $lines -le 20 ]; then
6871                         continue
6872                 fi
6873                 for size in 1 2 4 8; do
6874                         local rpc=$(echo "$stats" |
6875                                     awk '($1 == "'$size':") {print $2; exit; }')
6876                         [ $rpc != 0 ] &&
6877                                 error "Small $((size*4))k read IO $rpc !"
6878                 done
6879                 echo "$osc_rpc_stats check passed!"
6880         done
6881         cleanup_test101bc
6882         true
6883 }
6884 run_test 101c "check stripe_size aligned read-ahead ================="
6885
6886 set_read_ahead() {
6887         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6888         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6889 }
6890
6891 test_101d() {
6892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6893         local file=$DIR/$tfile
6894         local sz_MB=${FILESIZE_101d:-500}
6895         local ra_MB=${READAHEAD_MB:-40}
6896
6897         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6898         [ $free_MB -lt $sz_MB ] &&
6899                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6900
6901         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6902         $SETSTRIPE -c -1 $file || error "setstripe failed"
6903
6904         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6905         echo Cancel LRU locks on lustre client to flush the client cache
6906         cancel_lru_locks osc
6907
6908         echo Disable read-ahead
6909         local old_READAHEAD=$(set_read_ahead 0)
6910
6911         echo Reading the test file $file with read-ahead disabled
6912         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6913
6914         echo Cancel LRU locks on lustre client to flush the client cache
6915         cancel_lru_locks osc
6916         echo Enable read-ahead with ${ra_MB}MB
6917         set_read_ahead $ra_MB
6918
6919         echo Reading the test file $file with read-ahead enabled
6920         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6921
6922         echo "read-ahead disabled time read $raOFF"
6923         echo "read-ahead enabled  time read $raON"
6924
6925         set_read_ahead $old_READAHEAD
6926         rm -f $file
6927         wait_delete_completed
6928
6929         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6930                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6931 }
6932 run_test 101d "file read with and without read-ahead enabled"
6933
6934 test_101e() {
6935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6936         local file=$DIR/$tfile
6937         local size_KB=500  #KB
6938         local count=100
6939         local bsize=1024
6940
6941         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6942         local need_KB=$((count * size_KB))
6943         [[ $free_KB -le $need_KB ]] &&
6944                 skip_env "Need free space $need_KB, have $free_KB" && return
6945
6946         echo "Creating $count ${size_KB}K test files"
6947         for ((i = 0; i < $count; i++)); do
6948                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6949         done
6950
6951         echo "Cancel LRU locks on lustre client to flush the client cache"
6952         cancel_lru_locks $OSC
6953
6954         echo "Reset readahead stats"
6955         $LCTL set_param -n llite.*.read_ahead_stats 0
6956
6957         for ((i = 0; i < $count; i++)); do
6958                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6959         done
6960
6961         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6962                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6963
6964         for ((i = 0; i < $count; i++)); do
6965                 rm -rf $file.$i 2>/dev/null
6966         done
6967
6968         #10000 means 20% reads are missing in readahead
6969         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6970 }
6971 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6972
6973 test_101f() {
6974         which iozone || { skip "no iozone installed" && return; }
6975
6976         local old_debug=$($LCTL get_param debug)
6977         old_debug=${old_debug#*=}
6978         $LCTL set_param debug="reada mmap"
6979
6980         # create a test file
6981         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
6982
6983         echo Cancel LRU locks on lustre client to flush the client cache
6984         cancel_lru_locks osc
6985
6986         echo Reset readahead stats
6987         $LCTL set_param -n llite.*.read_ahead_stats 0
6988
6989         echo mmap read the file with small block size
6990         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
6991                 > /dev/null 2>&1
6992
6993         echo checking missing pages
6994         $LCTL get_param llite.*.read_ahead_stats
6995         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6996                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
6997
6998         $LCTL set_param debug="$old_debug"
6999         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7000         rm -f $DIR/$tfile
7001 }
7002 run_test 101f "check mmap read performance"
7003
7004 test_101g_brw_size_test() {
7005         local mb=$1
7006         local pages=$((mb * 1048576 / $(page_size)))
7007
7008         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7009                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7010         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7011                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7012                         return 2
7013         done
7014
7015         $LCTL set_param -n osc.*.rpc_stats=0
7016
7017         # 10 RPCs should be enough for the test
7018         local count=10
7019         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
7020                 { error "dd write ${mb} MB blocks failed"; return 3; }
7021         cancel_lru_locks osc
7022         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
7023                 { error "dd write ${mb} MB blocks failed"; return 4; }
7024
7025         # calculate number of full-sized read and write RPCs
7026         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7027                 sed -n '/pages per rpc/,/^$/p' |
7028                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7029                 END { print reads,writes }'))
7030         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7031                 return 5
7032         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7033                 return 6
7034
7035         return 0
7036 }
7037
7038 test_101g() {
7039         local rpcs
7040         local osts=$(get_facets OST)
7041         local list=$(comma_list $(osts_nodes))
7042         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7043         local brw_size="obdfilter.*.brw_size"
7044
7045         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7046
7047         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7048         if [ $(lustre_version_code ost1) -ge $(version_code 2.8.52) -a \
7049              $(lustre_version_code client) -ge $(version_code 2.8.52) ]; then
7050                 [ $(lustre_version_code ost1) -ge $(version_code 2.9.52) ] &&
7051                         suffix="M"
7052                 if [[ $orig_mb -lt 16 ]]; then
7053                         save_lustre_params $osts "$brw_size" > $p
7054                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7055                                 error "set 16MB RPC size failed"
7056
7057                         echo "remount client to enable new RPC size"
7058                         remount_client $MOUNT || error "remount_client failed"
7059                 fi
7060
7061                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7062                 # should be able to set brw_size=12, but no rpc_stats for that
7063                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7064         fi
7065
7066         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7067
7068         if [[ $orig_mb -lt 16 ]]; then
7069                 restore_lustre_params < $p
7070                 remount_client $MOUNT || error "remount_client restore failed"
7071         fi
7072
7073         rm -f $p $DIR/$tfile
7074 }
7075 run_test 101g "Big bulk(4/16 MiB) readahead"
7076
7077 setup_test102() {
7078         test_mkdir $DIR/$tdir
7079         chown $RUNAS_ID $DIR/$tdir
7080         STRIPE_SIZE=65536
7081         STRIPE_OFFSET=1
7082         STRIPE_COUNT=$OSTCOUNT
7083         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7084
7085         trap cleanup_test102 EXIT
7086         cd $DIR
7087         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7088         cd $DIR/$tdir
7089         for num in 1 2 3 4; do
7090                 for count in $(seq 1 $STRIPE_COUNT); do
7091                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7092                                 local size=`expr $STRIPE_SIZE \* $num`
7093                                 local file=file"$num-$idx-$count"
7094                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7095                         done
7096                 done
7097         done
7098
7099         cd $DIR
7100         $1 tar cf $TMP/f102.tar $tdir --xattrs
7101 }
7102
7103 cleanup_test102() {
7104         trap 0
7105         rm -f $TMP/f102.tar
7106         rm -rf $DIR/d0.sanity/d102
7107 }
7108
7109 test_102a() {
7110         local testfile=$DIR/$tfile
7111
7112         touch $testfile
7113
7114         [ "$UID" != 0 ] && skip_env "must run as root" && return
7115         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7116                 skip_env "must have user_xattr" && return
7117
7118         [ -z "$(which setfattr 2>/dev/null)" ] &&
7119                 skip_env "could not find setfattr" && return
7120
7121         echo "set/get xattr..."
7122         setfattr -n trusted.name1 -v value1 $testfile ||
7123                 error "setfattr -n trusted.name1=value1 $testfile failed"
7124         getfattr -n trusted.name1 $testfile 2> /dev/null |
7125           grep "trusted.name1=.value1" ||
7126                 error "$testfile missing trusted.name1=value1"
7127
7128         setfattr -n user.author1 -v author1 $testfile ||
7129                 error "setfattr -n user.author1=author1 $testfile failed"
7130         getfattr -n user.author1 $testfile 2> /dev/null |
7131           grep "user.author1=.author1" ||
7132                 error "$testfile missing trusted.author1=author1"
7133
7134         echo "listxattr..."
7135         setfattr -n trusted.name2 -v value2 $testfile ||
7136                 error "$testfile unable to set trusted.name2"
7137         setfattr -n trusted.name3 -v value3 $testfile ||
7138                 error "$testfile unable to set trusted.name3"
7139         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7140             grep "trusted.name" | wc -l) -eq 3 ] ||
7141                 error "$testfile missing 3 trusted.name xattrs"
7142
7143         setfattr -n user.author2 -v author2 $testfile ||
7144                 error "$testfile unable to set user.author2"
7145         setfattr -n user.author3 -v author3 $testfile ||
7146                 error "$testfile unable to set user.author3"
7147         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7148             grep "user.author" | wc -l) -eq 3 ] ||
7149                 error "$testfile missing 3 user.author xattrs"
7150
7151         echo "remove xattr..."
7152         setfattr -x trusted.name1 $testfile ||
7153                 error "$testfile error deleting trusted.name1"
7154         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7155                 error "$testfile did not delete trusted.name1 xattr"
7156
7157         setfattr -x user.author1 $testfile ||
7158                 error "$testfile error deleting user.author1"
7159         echo "set lustre special xattr ..."
7160         $LFS setstripe -c1 $testfile
7161         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7162                 awk -F "=" '/trusted.lov/ { print $2 }' )
7163         setfattr -n "trusted.lov" -v $lovea $testfile ||
7164                 error "$testfile doesn't ignore setting trusted.lov again"
7165         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7166                 error "$testfile allow setting invalid trusted.lov"
7167         rm -f $testfile
7168 }
7169 run_test 102a "user xattr test =================================="
7170
7171 test_102b() {
7172         [ -z "$(which setfattr 2>/dev/null)" ] &&
7173                 skip_env "could not find setfattr" && return
7174
7175         # b10930: get/set/list trusted.lov xattr
7176         echo "get/set/list trusted.lov xattr ..."
7177         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7178         local testfile=$DIR/$tfile
7179         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7180                 error "setstripe failed"
7181         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7182                 error "getstripe failed"
7183         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7184                 error "can't get trusted.lov from $testfile"
7185
7186         local testfile2=${testfile}2
7187         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7188                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7189
7190         $MCREATE $testfile2
7191         setfattr -n trusted.lov -v $value $testfile2
7192         local stripe_size=$($GETSTRIPE -S $testfile2)
7193         local stripe_count=$($GETSTRIPE -c $testfile2)
7194         [[ $stripe_size -eq 65536 ]] ||
7195                 error "stripe size $stripe_size != 65536"
7196         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7197                 error "stripe count $stripe_count != $STRIPECOUNT"
7198         rm -f $DIR/$tfile
7199 }
7200 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7201
7202 test_102c() {
7203         [ -z "$(which setfattr 2>/dev/null)" ] &&
7204                 skip_env "could not find setfattr" && return
7205
7206         # b10930: get/set/list lustre.lov xattr
7207         echo "get/set/list lustre.lov xattr ..."
7208         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7209         test_mkdir $DIR/$tdir
7210         chown $RUNAS_ID $DIR/$tdir
7211         local testfile=$DIR/$tdir/$tfile
7212         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7213                 error "setstripe failed"
7214         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7215                 error "getstripe failed"
7216         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7217         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7218
7219         local testfile2=${testfile}2
7220         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7221                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7222
7223         $RUNAS $MCREATE $testfile2
7224         $RUNAS setfattr -n lustre.lov -v $value $testfile2
7225         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
7226         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
7227         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
7228         [ $stripe_count -eq $STRIPECOUNT ] ||
7229                 error "stripe count $stripe_count != $STRIPECOUNT"
7230 }
7231 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
7232
7233 compare_stripe_info1() {
7234         local stripe_index_all_zero=true
7235
7236         for num in 1 2 3 4; do
7237                 for count in $(seq 1 $STRIPE_COUNT); do
7238                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
7239                                 local size=$((STRIPE_SIZE * num))
7240                                 local file=file"$num-$offset-$count"
7241                                 stripe_size=$($LFS getstripe -S $PWD/$file)
7242                                 [[ $stripe_size -ne $size ]] &&
7243                                     error "$file: size $stripe_size != $size"
7244                                 stripe_count=$($LFS getstripe -c $PWD/$file)
7245                                 # allow fewer stripes to be created, ORI-601
7246                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
7247                                     error "$file: count $stripe_count != $count"
7248                                 stripe_index=$($LFS getstripe -i $PWD/$file)
7249                                 [[ $stripe_index -ne 0 ]] &&
7250                                         stripe_index_all_zero=false
7251                         done
7252                 done
7253         done
7254         $stripe_index_all_zero &&
7255                 error "all files are being extracted starting from OST index 0"
7256         return 0
7257 }
7258
7259 have_xattrs_include() {
7260         tar --help | grep -q xattrs-include &&
7261                 echo --xattrs-include="lustre.*"
7262 }
7263
7264 test_102d() {
7265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7266         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7267         XINC=$(have_xattrs_include)
7268         setup_test102
7269         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7270         cd $DIR/$tdir/$tdir
7271         compare_stripe_info1
7272 }
7273 run_test 102d "tar restore stripe info from tarfile,not keep osts"
7274
7275 test_102f() {
7276         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7277         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7278         XINC=$(have_xattrs_include)
7279         setup_test102
7280         test_mkdir $DIR/$tdir.restore
7281         cd $DIR
7282         tar cf - --xattrs $tdir | tar xf - \
7283                 -C $DIR/$tdir.restore --xattrs $XINC
7284         cd $DIR/$tdir.restore/$tdir
7285         compare_stripe_info1
7286 }
7287 run_test 102f "tar copy files, not keep osts"
7288
7289 grow_xattr() {
7290         local xsize=${1:-1024}  # in bytes
7291         local file=$DIR/$tfile
7292
7293         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7294                 skip "must have user_xattr" && return 0
7295         [ -z "$(which setfattr 2>/dev/null)" ] &&
7296                 skip_env "could not find setfattr" && return 0
7297         [ -z "$(which getfattr 2>/dev/null)" ] &&
7298                 skip_env "could not find getfattr" && return 0
7299
7300         touch $file
7301
7302         local value="$(generate_string $xsize)"
7303
7304         local xbig=trusted.big
7305         log "save $xbig on $file"
7306         setfattr -n $xbig -v $value $file ||
7307                 error "saving $xbig on $file failed"
7308
7309         local orig=$(get_xattr_value $xbig $file)
7310         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7311
7312         local xsml=trusted.sml
7313         log "save $xsml on $file"
7314         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7315
7316         local new=$(get_xattr_value $xbig $file)
7317         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7318
7319         log "grow $xsml on $file"
7320         setfattr -n $xsml -v "$value" $file ||
7321                 error "growing $xsml on $file failed"
7322
7323         new=$(get_xattr_value $xbig $file)
7324         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7325         log "$xbig still valid after growing $xsml"
7326
7327         rm -f $file
7328 }
7329
7330 test_102h() { # bug 15777
7331         grow_xattr 1024
7332 }
7333 run_test 102h "grow xattr from inside inode to external block"
7334
7335 test_102ha() {
7336         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7337         grow_xattr $(max_xattr_size)
7338 }
7339 run_test 102ha "grow xattr from inside inode to external inode"
7340
7341 test_102i() { # bug 17038
7342         [ -z "$(which getfattr 2>/dev/null)" ] &&
7343                 skip "could not find getfattr" && return
7344         touch $DIR/$tfile
7345         ln -s $DIR/$tfile $DIR/${tfile}link
7346         getfattr -n trusted.lov $DIR/$tfile ||
7347                 error "lgetxattr on $DIR/$tfile failed"
7348         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7349                 grep -i "no such attr" ||
7350                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7351         rm -f $DIR/$tfile $DIR/${tfile}link
7352 }
7353 run_test 102i "lgetxattr test on symbolic link ============"
7354
7355 test_102j() {
7356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7357         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7358         XINC=$(have_xattrs_include)
7359         setup_test102 "$RUNAS"
7360         chown $RUNAS_ID $DIR/$tdir
7361         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7362         cd $DIR/$tdir/$tdir
7363         compare_stripe_info1 "$RUNAS"
7364 }
7365 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7366
7367 test_102k() {
7368         [ -z "$(which setfattr 2>/dev/null)" ] &&
7369                 skip "could not find setfattr" && return
7370         touch $DIR/$tfile
7371         # b22187 just check that does not crash for regular file.
7372         setfattr -n trusted.lov $DIR/$tfile
7373         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
7374         local test_kdir=$DIR/$tdir
7375         test_mkdir $test_kdir
7376         local default_size=$($LFS getstripe -S $test_kdir)
7377         local default_count=$($LFS getstripe -c $test_kdir)
7378         local default_offset=$($LFS getstripe -i $test_kdir)
7379         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7380                 error 'dir setstripe failed'
7381         setfattr -n trusted.lov $test_kdir
7382         local stripe_size=$($LFS getstripe -S $test_kdir)
7383         local stripe_count=$($LFS getstripe -c $test_kdir)
7384         local stripe_offset=$($LFS getstripe -i $test_kdir)
7385         [ $stripe_size -eq $default_size ] ||
7386                 error "stripe size $stripe_size != $default_size"
7387         [ $stripe_count -eq $default_count ] ||
7388                 error "stripe count $stripe_count != $default_count"
7389         [ $stripe_offset -eq $default_offset ] ||
7390                 error "stripe offset $stripe_offset != $default_offset"
7391         rm -rf $DIR/$tfile $test_kdir
7392 }
7393 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7394
7395 test_102l() {
7396         [ -z "$(which getfattr 2>/dev/null)" ] &&
7397                 skip "could not find getfattr" && return
7398
7399         # LU-532 trusted. xattr is invisible to non-root
7400         local testfile=$DIR/$tfile
7401
7402         touch $testfile
7403
7404         echo "listxattr as user..."
7405         chown $RUNAS_ID $testfile
7406         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7407             grep -q "trusted" &&
7408                 error "$testfile trusted xattrs are user visible"
7409
7410         return 0;
7411 }
7412 run_test 102l "listxattr size test =================================="
7413
7414 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7415         local path=$DIR/$tfile
7416         touch $path
7417
7418         listxattr_size_check $path || error "listattr_size_check $path failed"
7419 }
7420 run_test 102m "Ensure listxattr fails on small bufffer ========"
7421
7422 cleanup_test102
7423
7424 getxattr() { # getxattr path name
7425         # Return the base64 encoding of the value of xattr name on path.
7426         local path=$1
7427         local name=$2
7428
7429         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7430         # file: $path
7431         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7432         #
7433         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7434
7435         getfattr --absolute-names --encoding=base64 --name=$name $path |
7436                 awk -F= -v name=$name '$1 == name {
7437                         print substr($0, index($0, "=") + 1);
7438         }'
7439 }
7440
7441 test_102n() { # LU-4101 mdt: protect internal xattrs
7442         [ -z "$(which setfattr 2>/dev/null)" ] &&
7443                 skip "could not find setfattr" && return
7444         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7445         then
7446                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7447                 return
7448         fi
7449
7450         local file0=$DIR/$tfile.0
7451         local file1=$DIR/$tfile.1
7452         local xattr0=$TMP/$tfile.0
7453         local xattr1=$TMP/$tfile.1
7454         local namelist="lov lma lmv link fid version som hsm"
7455         local name
7456         local value
7457
7458         rm -rf $file0 $file1 $xattr0 $xattr1
7459         touch $file0 $file1
7460
7461         # Get 'before' xattrs of $file1.
7462         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7463
7464         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7465                 namelist+=" lfsck_namespace"
7466         for name in $namelist; do
7467                 # Try to copy xattr from $file0 to $file1.
7468                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7469
7470                 setfattr --name=trusted.$name --value="$value" $file1 ||
7471                         error "setxattr 'trusted.$name' failed"
7472
7473                 # Try to set a garbage xattr.
7474                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7475
7476                 if [[ x$name == "xlov" ]]; then
7477                         setfattr --name=trusted.lov --value="$value" $file1 &&
7478                         error "setxattr invalid 'trusted.lov' success"
7479                 else
7480                         setfattr --name=trusted.$name --value="$value" $file1 ||
7481                                 error "setxattr invalid 'trusted.$name' failed"
7482                 fi
7483
7484                 # Try to remove the xattr from $file1. We don't care if this
7485                 # appears to succeed or fail, we just don't want there to be
7486                 # any changes or crashes.
7487                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7488         done
7489
7490         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7491         then
7492                 name="lfsck_ns"
7493                 # Try to copy xattr from $file0 to $file1.
7494                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7495
7496                 setfattr --name=trusted.$name --value="$value" $file1 ||
7497                         error "setxattr 'trusted.$name' failed"
7498
7499                 # Try to set a garbage xattr.
7500                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7501
7502                 setfattr --name=trusted.$name --value="$value" $file1 ||
7503                         error "setxattr 'trusted.$name' failed"
7504
7505                 # Try to remove the xattr from $file1. We don't care if this
7506                 # appears to succeed or fail, we just don't want there to be
7507                 # any changes or crashes.
7508                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7509         fi
7510
7511         # Get 'after' xattrs of file1.
7512         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7513
7514         if ! diff $xattr0 $xattr1; then
7515                 error "before and after xattrs of '$file1' differ"
7516         fi
7517
7518         rm -rf $file0 $file1 $xattr0 $xattr1
7519
7520         return 0
7521 }
7522 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7523
7524 test_102p() { # LU-4703 setxattr did not check ownership
7525         local testfile=$DIR/$tfile
7526
7527         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7528                 skip "MDS needs to be at least 2.5.56" && return
7529
7530         touch $testfile
7531
7532         echo "setfacl as user..."
7533         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7534         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7535
7536         echo "setfattr as user..."
7537         setfacl -m "u:$RUNAS_ID:---" $testfile
7538         $RUNAS setfattr -x system.posix_acl_access $testfile
7539         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7540 }
7541 run_test 102p "check setxattr(2) correctly fails without permission"
7542
7543 test_102q() {
7544         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7545                 skip "MDS needs to be at least 2.6.92" && return
7546         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7547 }
7548 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7549
7550 test_102r() {
7551         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7552                 skip "MDS needs to be at least 2.6.93" && return
7553         touch $DIR/$tfile || error "touch"
7554         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7555         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7556         rm $DIR/$tfile || error "rm"
7557
7558         #normal directory
7559         mkdir -p $DIR/$tdir || error "mkdir"
7560         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7561         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7562         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7563                 error "$testfile error deleting user.author1"
7564         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7565                 grep "user.$(basename $tdir)" &&
7566                 error "$tdir did not delete user.$(basename $tdir)"
7567         rmdir $DIR/$tdir || error "rmdir"
7568
7569         #striped directory
7570         test_mkdir $DIR/$tdir
7571         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7572         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7573         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7574                 error "$testfile error deleting user.author1"
7575         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7576                 grep "user.$(basename $tdir)" &&
7577                 error "$tdir did not delete user.$(basename $tdir)"
7578         rmdir $DIR/$tdir || error "rm striped dir"
7579 }
7580 run_test 102r "set EAs with empty values"
7581
7582 run_acl_subtest()
7583 {
7584     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7585     return $?
7586 }
7587
7588 test_103a() {
7589         [ "$UID" != 0 ] && skip_env "must run as root" && return
7590         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7591                 skip "must have acl enabled" && return
7592         [ -z "$(which setfacl 2>/dev/null)" ] &&
7593                 skip_env "could not find setfacl" && return
7594         $GSS && skip "could not run under gss" && return
7595         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7596
7597         gpasswd -a daemon bin                           # LU-5641
7598         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7599
7600         declare -a identity_old
7601
7602         for num in $(seq $MDSCOUNT); do
7603                 switch_identity $num true || identity_old[$num]=$?
7604         done
7605
7606         SAVE_UMASK=$(umask)
7607         umask 0022
7608         mkdir -p $DIR/$tdir
7609         cd $DIR/$tdir
7610
7611         echo "performing cp ..."
7612         run_acl_subtest cp || error "run_acl_subtest cp failed"
7613         echo "performing getfacl-noacl..."
7614         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7615         echo "performing misc..."
7616         run_acl_subtest misc || error  "misc test failed"
7617         echo "performing permissions..."
7618         run_acl_subtest permissions || error "permissions failed"
7619         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7620         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7621              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7622              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7623         then
7624                 echo "performing permissions xattr..."
7625                 run_acl_subtest permissions_xattr ||
7626                         error "permissions_xattr failed"
7627         fi
7628         echo "performing setfacl..."
7629         run_acl_subtest setfacl || error  "setfacl test failed"
7630
7631         # inheritance test got from HP
7632         echo "performing inheritance..."
7633         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7634         chmod +x make-tree || error "chmod +x failed"
7635         run_acl_subtest inheritance || error "inheritance test failed"
7636         rm -f make-tree
7637
7638         echo "LU-974 ignore umask when acl is enabled..."
7639         run_acl_subtest 974 || error "LU-974 umask test failed"
7640         if [ $MDSCOUNT -ge 2 ]; then
7641                 run_acl_subtest 974_remote ||
7642                         error "LU-974 umask test failed under remote dir"
7643         fi
7644
7645         echo "LU-2561 newly created file is same size as directory..."
7646         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7647                 run_acl_subtest 2561 || error "LU-2561 test failed"
7648         else
7649                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7650         fi
7651
7652         run_acl_subtest 4924 || error "LU-4924 test failed"
7653
7654         cd $SAVE_PWD
7655         umask $SAVE_UMASK
7656
7657         for num in $(seq $MDSCOUNT); do
7658                 if [ "${identity_old[$num]}" = 1 ]; then
7659                         switch_identity $num false || identity_old[$num]=$?
7660                 fi
7661         done
7662 }
7663 run_test 103a "acl test ========================================="
7664
7665 test_103c() {
7666         mkdir -p $DIR/$tdir
7667         cp -rp $DIR/$tdir $DIR/$tdir.bak
7668
7669         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7670                 error "$DIR/$tdir shouldn't contain default ACL"
7671         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7672                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7673         true
7674 }
7675 run_test 103c "'cp -rp' won't set empty acl"
7676
7677 test_104a() {
7678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7679         touch $DIR/$tfile
7680         lfs df || error "lfs df failed"
7681         lfs df -ih || error "lfs df -ih failed"
7682         lfs df -h $DIR || error "lfs df -h $DIR failed"
7683         lfs df -i $DIR || error "lfs df -i $DIR failed"
7684         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7685         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7686
7687         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7688         lctl --device %$OSC deactivate
7689         lfs df || error "lfs df with deactivated OSC failed"
7690         lctl --device %$OSC activate
7691         # wait the osc back to normal
7692         wait_osc_import_state client ost FULL
7693
7694         lfs df || error "lfs df with reactivated OSC failed"
7695         rm -f $DIR/$tfile
7696 }
7697 run_test 104a "lfs df [-ih] [path] test ========================="
7698
7699 test_104b() {
7700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7701         [ $RUNAS_ID -eq $UID ] &&
7702                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7703         chmod 666 /dev/obd
7704         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7705                         grep "Permission denied" | wc -l)))
7706         if [ $denied_cnt -ne 0 ]; then
7707                 error "lfs check servers test failed"
7708         fi
7709 }
7710 run_test 104b "$RUNAS lfs check servers test ===================="
7711
7712 test_105a() {
7713         # doesn't work on 2.4 kernels
7714         touch $DIR/$tfile
7715         if $(flock_is_enabled); then
7716                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7717         else
7718                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7719         fi
7720         rm -f $DIR/$tfile
7721 }
7722 run_test 105a "flock when mounted without -o flock test ========"
7723
7724 test_105b() {
7725         touch $DIR/$tfile
7726         if $(flock_is_enabled); then
7727                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7728         else
7729                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7730         fi
7731         rm -f $DIR/$tfile
7732 }
7733 run_test 105b "fcntl when mounted without -o flock test ========"
7734
7735 test_105c() {
7736         touch $DIR/$tfile
7737         if $(flock_is_enabled); then
7738                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7739         else
7740                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7741         fi
7742         rm -f $DIR/$tfile
7743 }
7744 run_test 105c "lockf when mounted without -o flock test"
7745
7746 test_105d() { # bug 15924
7747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7748         test_mkdir $DIR/$tdir
7749         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7750         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7751         $LCTL set_param fail_loc=0x80000315
7752         flocks_test 2 $DIR/$tdir
7753 }
7754 run_test 105d "flock race (should not freeze) ========"
7755
7756 test_105e() { # bug 22660 && 22040
7757         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7758         touch $DIR/$tfile
7759         flocks_test 3 $DIR/$tfile
7760 }
7761 run_test 105e "Two conflicting flocks from same process"
7762
7763 test_106() { #bug 10921
7764         test_mkdir $DIR/$tdir
7765         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7766         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7767 }
7768 run_test 106 "attempt exec of dir followed by chown of that dir"
7769
7770 test_107() {
7771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7772         CDIR=`pwd`
7773         cd $DIR
7774
7775         local file=core
7776         rm -f $file
7777
7778         local save_pattern=$(sysctl -n kernel.core_pattern)
7779         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7780         sysctl -w kernel.core_pattern=$file
7781         sysctl -w kernel.core_uses_pid=0
7782
7783         ulimit -c unlimited
7784         sleep 60 &
7785         SLEEPPID=$!
7786
7787         sleep 1
7788
7789         kill -s 11 $SLEEPPID
7790         wait $SLEEPPID
7791         if [ -e $file ]; then
7792                 size=`stat -c%s $file`
7793                 [ $size -eq 0 ] && error "Fail to create core file $file"
7794         else
7795                 error "Fail to create core file $file"
7796         fi
7797         rm -f $file
7798         sysctl -w kernel.core_pattern=$save_pattern
7799         sysctl -w kernel.core_uses_pid=$save_uses_pid
7800         cd $CDIR
7801 }
7802 run_test 107 "Coredump on SIG"
7803
7804 test_110() {
7805         test_mkdir $DIR/$tdir
7806         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
7807         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
7808                 error "mkdir with 256 char should fail, but did not"
7809         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7810                 error "create with 255 char failed"
7811         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7812                 error "create with 256 char should fail, but did not"
7813
7814         ls -l $DIR/$tdir
7815         rm -rf $DIR/$tdir
7816 }
7817 run_test 110 "filename length checking"
7818
7819 #
7820 # Purpose: To verify dynamic thread (OSS) creation.
7821 #
7822 test_115() {
7823         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7824         remote_ost_nodsh && skip "remote OST with nodsh" && return
7825
7826         # Lustre does not stop service threads once they are started.
7827         # Reset number of running threads to default.
7828         stopall
7829         setupall
7830
7831         local OSTIO_pre
7832         local save_params="$TMP/sanity-$TESTNAME.parameters"
7833
7834         # Get ll_ost_io count before I/O
7835         OSTIO_pre=$(do_facet ost1 \
7836                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
7837         # Exit if lustre is not running (ll_ost_io not running).
7838         [ -z "$OSTIO_pre" ] && error "no OSS threads"
7839
7840         echo "Starting with $OSTIO_pre threads"
7841         local thread_max=$((OSTIO_pre * 2))
7842         local rpc_in_flight=$((thread_max * 2))
7843         # Number of I/O Process proposed to be started.
7844         local nfiles
7845         local facets=$(get_facets OST)
7846
7847         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
7848         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
7849
7850         # Set in_flight to $rpc_in_flight
7851         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
7852                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
7853         nfiles=${rpc_in_flight}
7854         # Set ost thread_max to $thread_max
7855         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
7856
7857         # 5 Minutes should be sufficient for max number of OSS
7858         # threads(thread_max) to be created.
7859         local timeout=300
7860
7861         # Start I/O.
7862         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
7863         test_mkdir $DIR/$tdir
7864         for i in $(seq $nfiles); do
7865                 local file=$DIR/$tdir/${tfile}-$i
7866                 $LFS setstripe -c -1 -i 0 $file
7867                 ($WTL $file $timeout)&
7868         done
7869
7870         # I/O Started - Wait for thread_started to reach thread_max or report
7871         # error if thread_started is more than thread_max.
7872         echo "Waiting for thread_started to reach thread_max"
7873         local thread_started=0
7874         local end_time=$((SECONDS + timeout))
7875
7876         while [ $SECONDS -le $end_time ] ; do
7877                 echo -n "."
7878                 # Get ost i/o thread_started count.
7879                 thread_started=$(do_facet ost1 \
7880                         "$LCTL get_param \
7881                         ost.OSS.ost_io.threads_started | cut -d= -f2")
7882                 # Break out if thread_started is equal/greater than thread_max
7883                 if [[ $thread_started -ge $thread_max ]]; then
7884                         echo ll_ost_io thread_started $thread_started, \
7885                                 equal/greater than thread_max $thread_max
7886                         break
7887                 fi
7888                 sleep 1
7889         done
7890
7891         # Cleanup - We have the numbers, Kill i/o jobs if running.
7892         jobcount=($(jobs -p))
7893         for i in $(seq 0 $((${#jobcount[@]}-1)))
7894         do
7895                 kill -9 ${jobcount[$i]}
7896                 if [ $? -ne 0 ] ; then
7897                         echo Warning: \
7898                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
7899                 fi
7900         done
7901
7902         # Cleanup files left by WTL binary.
7903         for i in $(seq $nfiles); do
7904                 local file=$DIR/$tdir/${tfile}-$i
7905                 rm -rf $file
7906                 if [ $? -ne 0 ] ; then
7907                         echo "Warning: Failed to delete file $file"
7908                 fi
7909         done
7910
7911         restore_lustre_params <$save_params
7912         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
7913
7914         # Error out if no new thread has started or Thread started is greater
7915         # than thread max.
7916         if [[ $thread_started -le $OSTIO_pre ||
7917                         $thread_started -gt $thread_max ]]; then
7918                 error "ll_ost_io: thread_started $thread_started" \
7919                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
7920                       "No new thread started or thread started greater " \
7921                       "than thread_max."
7922         fi
7923 }
7924 run_test 115 "verify dynamic thread creation===================="
7925
7926 free_min_max () {
7927         wait_delete_completed
7928         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
7929         echo "OST kbytes available: ${AVAIL[@]}"
7930         MAXV=${AVAIL[0]}
7931         MAXI=0
7932         MINV=${AVAIL[0]}
7933         MINI=0
7934         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
7935                 #echo OST $i: ${AVAIL[i]}kb
7936                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
7937                         MAXV=${AVAIL[i]}
7938                         MAXI=$i
7939                 fi
7940                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
7941                         MINV=${AVAIL[i]}
7942                         MINI=$i
7943                 fi
7944         done
7945         echo "Min free space: OST $MINI: $MINV"
7946         echo "Max free space: OST $MAXI: $MAXV"
7947 }
7948
7949 test_116a() { # was previously test_116()
7950         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7951         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7952
7953         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7954
7955         echo -n "Free space priority "
7956         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
7957                 head -n1
7958         declare -a AVAIL
7959         free_min_max
7960
7961         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
7962         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
7963                 && return
7964         trap simple_cleanup_common EXIT
7965
7966
7967         # Check if we need to generate uneven OSTs
7968         test_mkdir -p $DIR/$tdir/OST${MINI}
7969         local FILL=$((MINV / 4))
7970         local DIFF=$((MAXV - MINV))
7971         local DIFF2=$((DIFF * 100 / MINV))
7972
7973         local threshold=$(do_facet $SINGLEMDS \
7974                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
7975         threshold=${threshold%%%}
7976         echo -n "Check for uneven OSTs: "
7977         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
7978
7979         if [[ $DIFF2 -gt $threshold ]]; then
7980                 echo "ok"
7981                 echo "Don't need to fill OST$MINI"
7982         else
7983                 # generate uneven OSTs. Write 2% over the QOS threshold value
7984                 echo "no"
7985                 DIFF=$((threshold - DIFF2 + 2))
7986                 DIFF2=$((MINV * DIFF / 100))
7987                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
7988                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
7989                         error "setstripe failed"
7990                 DIFF=$((DIFF2 / 2048))
7991                 i=0
7992                 while [ $i -lt $DIFF ]; do
7993                         i=$((i + 1))
7994                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
7995                                 bs=2M count=1 2>/dev/null
7996                         echo -n .
7997                 done
7998                 echo .
7999                 sync
8000                 sleep_maxage
8001                 free_min_max
8002         fi
8003
8004         DIFF=$((MAXV - MINV))
8005         DIFF2=$((DIFF * 100 / MINV))
8006         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8007         if [ $DIFF2 -gt $threshold ]; then
8008                 echo "ok"
8009         else
8010                 echo "failed - QOS mode won't be used"
8011                 skip "QOS imbalance criteria not met"
8012                 simple_cleanup_common
8013                 return
8014         fi
8015
8016         MINI1=$MINI
8017         MINV1=$MINV
8018         MAXI1=$MAXI
8019         MAXV1=$MAXV
8020
8021         # now fill using QOS
8022         $SETSTRIPE -c 1 $DIR/$tdir
8023         FILL=$((FILL / 200))
8024         if [ $FILL -gt 600 ]; then
8025                 FILL=600
8026         fi
8027         echo "writing $FILL files to QOS-assigned OSTs"
8028         i=0
8029         while [ $i -lt $FILL ]; do
8030                 i=$((i + 1))
8031                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8032                         count=1 2>/dev/null
8033                 echo -n .
8034         done
8035         echo "wrote $i 200k files"
8036         sync
8037         sleep_maxage
8038
8039         echo "Note: free space may not be updated, so measurements might be off"
8040         free_min_max
8041         DIFF2=$((MAXV - MINV))
8042         echo "free space delta: orig $DIFF final $DIFF2"
8043         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8044         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8045         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8046         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8047         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8048         if [[ $DIFF -gt 0 ]]; then
8049                 FILL=$((DIFF2 * 100 / DIFF - 100))
8050                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8051         fi
8052
8053         # Figure out which files were written where
8054         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8055                awk '/'$MINI1': / {print $2; exit}')
8056         echo $UUID
8057         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8058         echo "$MINC files created on smaller OST $MINI1"
8059         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8060                awk '/'$MAXI1': / {print $2; exit}')
8061         echo $UUID
8062         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8063         echo "$MAXC files created on larger OST $MAXI1"
8064         if [[ $MINC -gt 0 ]]; then
8065                 FILL=$((MAXC * 100 / MINC - 100))
8066                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8067         fi
8068         [[ $MAXC -gt $MINC ]] ||
8069                 error_ignore LU-9 "stripe QOS didn't balance free space"
8070         simple_cleanup_common
8071 }
8072 run_test 116a "stripe QOS: free space balance ==================="
8073
8074 test_116b() { # LU-2093
8075         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8076         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8077
8078 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8079         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8080                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8081         [ -z "$old_rr" ] && skip "no QOS" && return 0
8082         do_facet $SINGLEMDS lctl set_param \
8083                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8084         mkdir -p $DIR/$tdir
8085         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8086         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8087         do_facet $SINGLEMDS lctl set_param fail_loc=0
8088         rm -rf $DIR/$tdir
8089         do_facet $SINGLEMDS lctl set_param \
8090                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8091 }
8092 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8093
8094 test_117() # bug 10891
8095 {
8096         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8097         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8098         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8099         lctl set_param fail_loc=0x21e
8100         > $DIR/$tfile || error "truncate failed"
8101         lctl set_param fail_loc=0
8102         echo "Truncate succeeded."
8103         rm -f $DIR/$tfile
8104 }
8105 run_test 117 "verify osd extend =========="
8106
8107 NO_SLOW_RESENDCOUNT=4
8108 export OLD_RESENDCOUNT=""
8109 set_resend_count () {
8110         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8111         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8112         lctl set_param -n $PROC_RESENDCOUNT $1
8113         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8114 }
8115
8116 # for reduce test_118* time (b=14842)
8117 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8118
8119 # Reset async IO behavior after error case
8120 reset_async() {
8121         FILE=$DIR/reset_async
8122
8123         # Ensure all OSCs are cleared
8124         $SETSTRIPE -c -1 $FILE
8125         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8126         sync
8127         rm $FILE
8128 }
8129
8130 test_118a() #bug 11710
8131 {
8132         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8133         reset_async
8134
8135         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8136         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8137         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8138
8139         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8140                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8141                 return 1;
8142         fi
8143         rm -f $DIR/$tfile
8144 }
8145 run_test 118a "verify O_SYNC works =========="
8146
8147 test_118b()
8148 {
8149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8150         remote_ost_nodsh && skip "remote OST with nodsh" && return
8151
8152         reset_async
8153
8154         #define OBD_FAIL_SRV_ENOENT 0x217
8155         set_nodes_failloc "$(osts_nodes)" 0x217
8156         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8157         RC=$?
8158         set_nodes_failloc "$(osts_nodes)" 0
8159         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8160         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8161                     grep -c writeback)
8162
8163         if [[ $RC -eq 0 ]]; then
8164                 error "Must return error due to dropped pages, rc=$RC"
8165                 return 1;
8166         fi
8167
8168         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8169                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8170                 return 1;
8171         fi
8172
8173         echo "Dirty pages not leaked on ENOENT"
8174
8175         # Due to the above error the OSC will issue all RPCs syncronously
8176         # until a subsequent RPC completes successfully without error.
8177         $MULTIOP $DIR/$tfile Ow4096yc
8178         rm -f $DIR/$tfile
8179
8180         return 0
8181 }
8182 run_test 118b "Reclaim dirty pages on fatal error =========="
8183
8184 test_118c()
8185 {
8186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8187
8188         # for 118c, restore the original resend count, LU-1940
8189         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
8190                                 set_resend_count $OLD_RESENDCOUNT
8191         remote_ost_nodsh && skip "remote OST with nodsh" && return
8192
8193         reset_async
8194
8195         #define OBD_FAIL_OST_EROFS               0x216
8196         set_nodes_failloc "$(osts_nodes)" 0x216
8197
8198         # multiop should block due to fsync until pages are written
8199         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8200         MULTIPID=$!
8201         sleep 1
8202
8203         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8204                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8205         fi
8206
8207         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8208                     grep -c writeback)
8209         if [[ $WRITEBACK -eq 0 ]]; then
8210                 error "No page in writeback, writeback=$WRITEBACK"
8211         fi
8212
8213         set_nodes_failloc "$(osts_nodes)" 0
8214         wait $MULTIPID
8215         RC=$?
8216         if [[ $RC -ne 0 ]]; then
8217                 error "Multiop fsync failed, rc=$RC"
8218         fi
8219
8220         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8221         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8222                     grep -c writeback)
8223         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8224                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8225         fi
8226
8227         rm -f $DIR/$tfile
8228         echo "Dirty pages flushed via fsync on EROFS"
8229         return 0
8230 }
8231 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
8232
8233 # continue to use small resend count to reduce test_118* time (b=14842)
8234 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8235
8236 test_118d()
8237 {
8238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8239         remote_ost_nodsh && skip "remote OST with nodsh" && return
8240
8241         reset_async
8242
8243         #define OBD_FAIL_OST_BRW_PAUSE_BULK
8244         set_nodes_failloc "$(osts_nodes)" 0x214
8245         # multiop should block due to fsync until pages are written
8246         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8247         MULTIPID=$!
8248         sleep 1
8249
8250         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8251                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8252         fi
8253
8254         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8255                     grep -c writeback)
8256         if [[ $WRITEBACK -eq 0 ]]; then
8257                 error "No page in writeback, writeback=$WRITEBACK"
8258         fi
8259
8260         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
8261         set_nodes_failloc "$(osts_nodes)" 0
8262
8263         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8264         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8265                     grep -c writeback)
8266         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8267                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8268         fi
8269
8270         rm -f $DIR/$tfile
8271         echo "Dirty pages gaurenteed flushed via fsync"
8272         return 0
8273 }
8274 run_test 118d "Fsync validation inject a delay of the bulk =========="
8275
8276 test_118f() {
8277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8278         reset_async
8279
8280         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
8281         lctl set_param fail_loc=0x8000040a
8282
8283         # Should simulate EINVAL error which is fatal
8284         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8285         RC=$?
8286         if [[ $RC -eq 0 ]]; then
8287                 error "Must return error due to dropped pages, rc=$RC"
8288         fi
8289
8290         lctl set_param fail_loc=0x0
8291
8292         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8293         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8294         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8295                     grep -c writeback)
8296         if [[ $LOCKED -ne 0 ]]; then
8297                 error "Locked pages remain in cache, locked=$LOCKED"
8298         fi
8299
8300         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8301                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8302         fi
8303
8304         rm -f $DIR/$tfile
8305         echo "No pages locked after fsync"
8306
8307         reset_async
8308         return 0
8309 }
8310 run_test 118f "Simulate unrecoverable OSC side error =========="
8311
8312 test_118g() {
8313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8314         reset_async
8315
8316         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8317         lctl set_param fail_loc=0x406
8318
8319         # simulate local -ENOMEM
8320         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8321         RC=$?
8322
8323         lctl set_param fail_loc=0
8324         if [[ $RC -eq 0 ]]; then
8325                 error "Must return error due to dropped pages, rc=$RC"
8326         fi
8327
8328         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8329         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8330         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8331                         grep -c writeback)
8332         if [[ $LOCKED -ne 0 ]]; then
8333                 error "Locked pages remain in cache, locked=$LOCKED"
8334         fi
8335
8336         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8337                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8338         fi
8339
8340         rm -f $DIR/$tfile
8341         echo "No pages locked after fsync"
8342
8343         reset_async
8344         return 0
8345 }
8346 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8347
8348 test_118h() {
8349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8350         remote_ost_nodsh && skip "remote OST with nodsh" && return
8351
8352         reset_async
8353
8354         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8355         set_nodes_failloc "$(osts_nodes)" 0x20e
8356         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8357         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8358         RC=$?
8359
8360         set_nodes_failloc "$(osts_nodes)" 0
8361         if [[ $RC -eq 0 ]]; then
8362                 error "Must return error due to dropped pages, rc=$RC"
8363         fi
8364
8365         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8366         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8367         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8368                     grep -c writeback)
8369         if [[ $LOCKED -ne 0 ]]; then
8370                 error "Locked pages remain in cache, locked=$LOCKED"
8371         fi
8372
8373         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8374                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8375         fi
8376
8377         rm -f $DIR/$tfile
8378         echo "No pages locked after fsync"
8379
8380         return 0
8381 }
8382 run_test 118h "Verify timeout in handling recoverables errors  =========="
8383
8384 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8385
8386 test_118i() {
8387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8388         remote_ost_nodsh && skip "remote OST with nodsh" && return
8389
8390         reset_async
8391
8392         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8393         set_nodes_failloc "$(osts_nodes)" 0x20e
8394
8395         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8396         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8397         PID=$!
8398         sleep 5
8399         set_nodes_failloc "$(osts_nodes)" 0
8400
8401         wait $PID
8402         RC=$?
8403         if [[ $RC -ne 0 ]]; then
8404                 error "got error, but should be not, rc=$RC"
8405         fi
8406
8407         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8408         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8409         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8410         if [[ $LOCKED -ne 0 ]]; then
8411                 error "Locked pages remain in cache, locked=$LOCKED"
8412         fi
8413
8414         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8415                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8416         fi
8417
8418         rm -f $DIR/$tfile
8419         echo "No pages locked after fsync"
8420
8421         return 0
8422 }
8423 run_test 118i "Fix error before timeout in recoverable error  =========="
8424
8425 [ "$SLOW" = "no" ] && set_resend_count 4
8426
8427 test_118j() {
8428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8429         remote_ost_nodsh && skip "remote OST with nodsh" && return
8430
8431         reset_async
8432
8433         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8434         set_nodes_failloc "$(osts_nodes)" 0x220
8435
8436         # return -EIO from OST
8437         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8438         RC=$?
8439         set_nodes_failloc "$(osts_nodes)" 0x0
8440         if [[ $RC -eq 0 ]]; then
8441                 error "Must return error due to dropped pages, rc=$RC"
8442         fi
8443
8444         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8445         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8446         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8447         if [[ $LOCKED -ne 0 ]]; then
8448                 error "Locked pages remain in cache, locked=$LOCKED"
8449         fi
8450
8451         # in recoverable error on OST we want resend and stay until it finished
8452         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8453                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8454         fi
8455
8456         rm -f $DIR/$tfile
8457         echo "No pages locked after fsync"
8458
8459         return 0
8460 }
8461 run_test 118j "Simulate unrecoverable OST side error =========="
8462
8463 test_118k()
8464 {
8465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8466         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8467
8468         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8469         set_nodes_failloc "$(osts_nodes)" 0x20e
8470         test_mkdir $DIR/$tdir
8471
8472         for ((i=0;i<10;i++)); do
8473                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8474                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8475                 SLEEPPID=$!
8476                 sleep 0.500s
8477                 kill $SLEEPPID
8478                 wait $SLEEPPID
8479         done
8480
8481         set_nodes_failloc "$(osts_nodes)" 0
8482         rm -rf $DIR/$tdir
8483 }
8484 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8485
8486 test_118l()
8487 {
8488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8489         # LU-646
8490         test_mkdir $DIR/$tdir
8491         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8492         rm -rf $DIR/$tdir
8493 }
8494 run_test 118l "fsync dir"
8495
8496 test_118m() # LU-3066
8497 {
8498         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8499         test_mkdir $DIR/$tdir
8500         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8501         rm -rf $DIR/$tdir
8502 }
8503 run_test 118m "fdatasync dir ========="
8504
8505 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8506
8507 test_119a() # bug 11737
8508 {
8509         BSIZE=$((512 * 1024))
8510         directio write $DIR/$tfile 0 1 $BSIZE
8511         # We ask to read two blocks, which is more than a file size.
8512         # directio will indicate an error when requested and actual
8513         # sizes aren't equeal (a normal situation in this case) and
8514         # print actual read amount.
8515         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8516         if [ "$NOB" != "$BSIZE" ]; then
8517                 error "read $NOB bytes instead of $BSIZE"
8518         fi
8519         rm -f $DIR/$tfile
8520 }
8521 run_test 119a "Short directIO read must return actual read amount"
8522
8523 test_119b() # bug 11737
8524 {
8525         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8526
8527         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8528         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8529         sync
8530         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8531                 error "direct read failed"
8532         rm -f $DIR/$tfile
8533 }
8534 run_test 119b "Sparse directIO read must return actual read amount"
8535
8536 test_119c() # bug 13099
8537 {
8538         BSIZE=1048576
8539         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8540         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8541         rm -f $DIR/$tfile
8542 }
8543 run_test 119c "Testing for direct read hitting hole"
8544
8545 test_119d() # bug 15950
8546 {
8547         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8548         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8549         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8550         BSIZE=1048576
8551         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8552         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8553         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8554         lctl set_param fail_loc=0x40d
8555         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8556         pid_dio=$!
8557         sleep 1
8558         cat $DIR/$tfile > /dev/null &
8559         lctl set_param fail_loc=0
8560         pid_reads=$!
8561         wait $pid_dio
8562         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8563         sleep 2
8564         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8565         error "the read rpcs have not completed in 2s"
8566         rm -f $DIR/$tfile
8567         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8568 }
8569 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8570
8571 test_120a() {
8572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8573         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8574         test_mkdir $DIR/$tdir
8575         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8576                 { skip "no early lock cancel on server"; return 0; }
8577
8578         lru_resize_disable mdc
8579         lru_resize_disable osc
8580         cancel_lru_locks mdc
8581         # asynchronous object destroy at MDT could cause bl ast to client
8582         cancel_lru_locks osc
8583
8584         stat $DIR/$tdir > /dev/null
8585         can1=$(do_facet $SINGLEMDS \
8586                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8587                awk '/ldlm_cancel/ {print $2}')
8588         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8589                awk '/ldlm_bl_callback/ {print $2}')
8590         test_mkdir -c1 $DIR/$tdir/d1
8591         can2=$(do_facet $SINGLEMDS \
8592                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8593                awk '/ldlm_cancel/ {print $2}')
8594         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8595                awk '/ldlm_bl_callback/ {print $2}')
8596         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8597         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8598         lru_resize_enable mdc
8599         lru_resize_enable osc
8600 }
8601 run_test 120a "Early Lock Cancel: mkdir test"
8602
8603 test_120b() {
8604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8605         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8606         test_mkdir $DIR/$tdir
8607         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8608                 { skip "no early lock cancel on server"; return 0; }
8609         lru_resize_disable mdc
8610         lru_resize_disable osc
8611         cancel_lru_locks mdc
8612         stat $DIR/$tdir > /dev/null
8613         can1=$(do_facet $SINGLEMDS \
8614                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8615                awk '/ldlm_cancel/ {print $2}')
8616         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8617                awk '/ldlm_bl_callback/ {print $2}')
8618         touch $DIR/$tdir/f1
8619         can2=$(do_facet $SINGLEMDS \
8620                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8621                awk '/ldlm_cancel/ {print $2}')
8622         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8623                awk '/ldlm_bl_callback/ {print $2}')
8624         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8625         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8626         lru_resize_enable mdc
8627         lru_resize_enable osc
8628 }
8629 run_test 120b "Early Lock Cancel: create test"
8630
8631 test_120c() {
8632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8633         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8634         test_mkdir -c1 $DIR/$tdir
8635         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8636                 { skip "no early lock cancel on server"; return 0; }
8637         lru_resize_disable mdc
8638         lru_resize_disable osc
8639         test_mkdir -c1 $DIR/$tdir/d1
8640         test_mkdir -c1 $DIR/$tdir/d2
8641         touch $DIR/$tdir/d1/f1
8642         cancel_lru_locks mdc
8643         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8644         can1=$(do_facet $SINGLEMDS \
8645                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8646                awk '/ldlm_cancel/ {print $2}')
8647         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8648                awk '/ldlm_bl_callback/ {print $2}')
8649         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8650         can2=$(do_facet $SINGLEMDS \
8651                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8652                awk '/ldlm_cancel/ {print $2}')
8653         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8654                awk '/ldlm_bl_callback/ {print $2}')
8655         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8656         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8657         lru_resize_enable mdc
8658         lru_resize_enable osc
8659 }
8660 run_test 120c "Early Lock Cancel: link test"
8661
8662 test_120d() {
8663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8664         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8665         test_mkdir -c1 $DIR/$tdir
8666         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8667                 { skip "no early lock cancel on server"; return 0; }
8668         lru_resize_disable mdc
8669         lru_resize_disable osc
8670         touch $DIR/$tdir
8671         cancel_lru_locks mdc
8672         stat $DIR/$tdir > /dev/null
8673         can1=$(do_facet $SINGLEMDS \
8674                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8675                awk '/ldlm_cancel/ {print $2}')
8676         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8677                awk '/ldlm_bl_callback/ {print $2}')
8678         chmod a+x $DIR/$tdir
8679         can2=$(do_facet $SINGLEMDS \
8680                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8681                awk '/ldlm_cancel/ {print $2}')
8682         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8683                awk '/ldlm_bl_callback/ {print $2}')
8684         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8685         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8686         lru_resize_enable mdc
8687         lru_resize_enable osc
8688 }
8689 run_test 120d "Early Lock Cancel: setattr test"
8690
8691 test_120e() {
8692         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8693         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8694                 { skip "no early lock cancel on server"; return 0; }
8695         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8696         local dlmtrace_set=false
8697
8698         test_mkdir -c1 $DIR/$tdir
8699         lru_resize_disable mdc
8700         lru_resize_disable osc
8701         ! $LCTL get_param debug | grep -q dlmtrace &&
8702                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
8703         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8704         cancel_lru_locks mdc
8705         cancel_lru_locks osc
8706         dd if=$DIR/$tdir/f1 of=/dev/null
8707         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8708         # XXX client can not do early lock cancel of OST lock
8709         # during unlink (LU-4206), so cancel osc lock now.
8710         sleep 2
8711         cancel_lru_locks osc
8712         can1=$(do_facet $SINGLEMDS \
8713                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8714                awk '/ldlm_cancel/ {print $2}')
8715         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8716                awk '/ldlm_bl_callback/ {print $2}')
8717         unlink $DIR/$tdir/f1
8718         sleep 5
8719         can2=$(do_facet $SINGLEMDS \
8720                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8721                awk '/ldlm_cancel/ {print $2}')
8722         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8723                awk '/ldlm_bl_callback/ {print $2}')
8724         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
8725                 $LCTL dk $TMP/cancel.debug.txt
8726         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
8727                 $LCTL dk $TMP/blocking.debug.txt
8728         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
8729         lru_resize_enable mdc
8730         lru_resize_enable osc
8731 }
8732 run_test 120e "Early Lock Cancel: unlink test"
8733
8734 test_120f() {
8735         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8736         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8737                 { skip "no early lock cancel on server"; return 0; }
8738         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8739         test_mkdir -c1 $DIR/$tdir
8740         lru_resize_disable mdc
8741         lru_resize_disable osc
8742         test_mkdir -c1 $DIR/$tdir/d1
8743         test_mkdir -c1 $DIR/$tdir/d2
8744         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8745         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8746         cancel_lru_locks mdc
8747         cancel_lru_locks osc
8748         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8749         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8750         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8751         # XXX client can not do early lock cancel of OST lock
8752         # during rename (LU-4206), so cancel osc lock now.
8753         sleep 2
8754         cancel_lru_locks osc
8755         can1=$(do_facet $SINGLEMDS \
8756                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8757                awk '/ldlm_cancel/ {print $2}')
8758         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8759                awk '/ldlm_bl_callback/ {print $2}')
8760         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8761         sleep 5
8762         can2=$(do_facet $SINGLEMDS \
8763                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8764                awk '/ldlm_cancel/ {print $2}')
8765         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8766                awk '/ldlm_bl_callback/ {print $2}')
8767         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8768         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8769         lru_resize_enable mdc
8770         lru_resize_enable osc
8771 }
8772 run_test 120f "Early Lock Cancel: rename test"
8773
8774 test_120g() {
8775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8776         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8777                 { skip "no early lock cancel on server"; return 0; }
8778         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8779         lru_resize_disable mdc
8780         lru_resize_disable osc
8781         count=10000
8782         echo create $count files
8783         test_mkdir $DIR/$tdir
8784         cancel_lru_locks mdc
8785         cancel_lru_locks osc
8786         t0=$(date +%s)
8787
8788         can0=$(do_facet $SINGLEMDS \
8789                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8790                awk '/ldlm_cancel/ {print $2}')
8791         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8792                awk '/ldlm_bl_callback/ {print $2}')
8793         createmany -o $DIR/$tdir/f $count
8794         sync
8795         can1=$(do_facet $SINGLEMDS \
8796                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8797                awk '/ldlm_cancel/ {print $2}')
8798         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8799                awk '/ldlm_bl_callback/ {print $2}')
8800         t1=$(date +%s)
8801         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8802         echo rm $count files
8803         rm -r $DIR/$tdir
8804         sync
8805         can2=$(do_facet $SINGLEMDS \
8806                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8807                awk '/ldlm_cancel/ {print $2}')
8808         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8809                awk '/ldlm_bl_callback/ {print $2}')
8810         t2=$(date +%s)
8811         echo total: $count removes in $((t2-t1))
8812         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8813         sleep 2
8814         # wait for commitment of removal
8815         lru_resize_enable mdc
8816         lru_resize_enable osc
8817 }
8818 run_test 120g "Early Lock Cancel: performance test"
8819
8820 test_121() { #bug #10589
8821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8822         rm -rf $DIR/$tfile
8823         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8824 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8825         lctl set_param fail_loc=0x310
8826         cancel_lru_locks osc > /dev/null
8827         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8828         lctl set_param fail_loc=0
8829         [[ $reads -eq $writes ]] ||
8830                 error "read $reads blocks, must be $writes blocks"
8831 }
8832 run_test 121 "read cancel race ========="
8833
8834 test_123a() { # was test 123, statahead(bug 11401)
8835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8836         SLOWOK=0
8837         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
8838                 log "testing UP system. Performance may be lower than expected."
8839                 SLOWOK=1
8840         fi
8841
8842         rm -rf $DIR/$tdir
8843         test_mkdir $DIR/$tdir
8844         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8845         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8846         MULT=10
8847         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8848                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8849
8850                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8851                 lctl set_param -n llite.*.statahead_max 0
8852                 lctl get_param llite.*.statahead_max
8853                 cancel_lru_locks mdc
8854                 cancel_lru_locks osc
8855                 stime=`date +%s`
8856                 time ls -l $DIR/$tdir | wc -l
8857                 etime=`date +%s`
8858                 delta=$((etime - stime))
8859                 log "ls $i files without statahead: $delta sec"
8860                 lctl set_param llite.*.statahead_max=$max
8861
8862                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8863                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8864                 cancel_lru_locks mdc
8865                 cancel_lru_locks osc
8866                 stime=`date +%s`
8867                 time ls -l $DIR/$tdir | wc -l
8868                 etime=`date +%s`
8869                 delta_sa=$((etime - stime))
8870                 log "ls $i files with statahead: $delta_sa sec"
8871                 lctl get_param -n llite.*.statahead_stats
8872                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8873
8874                 [[ $swrong -lt $ewrong ]] &&
8875                         log "statahead was stopped, maybe too many locks held!"
8876                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8877
8878                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8879                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8880                     lctl set_param -n llite.*.statahead_max 0
8881                     lctl get_param llite.*.statahead_max
8882                     cancel_lru_locks mdc
8883                     cancel_lru_locks osc
8884                     stime=`date +%s`
8885                     time ls -l $DIR/$tdir | wc -l
8886                     etime=`date +%s`
8887                     delta=$((etime - stime))
8888                     log "ls $i files again without statahead: $delta sec"
8889                     lctl set_param llite.*.statahead_max=$max
8890                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8891                         if [  $SLOWOK -eq 0 ]; then
8892                                 error "ls $i files is slower with statahead!"
8893                         else
8894                                 log "ls $i files is slower with statahead!"
8895                         fi
8896                         break
8897                     fi
8898                 fi
8899
8900                 [ $delta -gt 20 ] && break
8901                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8902                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8903         done
8904         log "ls done"
8905
8906         stime=`date +%s`
8907         rm -r $DIR/$tdir
8908         sync
8909         etime=`date +%s`
8910         delta=$((etime - stime))
8911         log "rm -r $DIR/$tdir/: $delta seconds"
8912         log "rm done"
8913         lctl get_param -n llite.*.statahead_stats
8914 }
8915 run_test 123a "verify statahead work"
8916
8917 test_123b () { # statahead(bug 15027)
8918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8919         test_mkdir $DIR/$tdir
8920         createmany -o $DIR/$tdir/$tfile-%d 1000
8921
8922         cancel_lru_locks mdc
8923         cancel_lru_locks osc
8924
8925 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
8926         lctl set_param fail_loc=0x80000803
8927         ls -lR $DIR/$tdir > /dev/null
8928         log "ls done"
8929         lctl set_param fail_loc=0x0
8930         lctl get_param -n llite.*.statahead_stats
8931         rm -r $DIR/$tdir
8932         sync
8933
8934 }
8935 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
8936
8937 test_124a() {
8938         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8939         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
8940                 { skip "no lru resize on server"; return 0; }
8941         local NR=2000
8942         test_mkdir $DIR/$tdir
8943
8944         log "create $NR files at $DIR/$tdir"
8945         createmany -o $DIR/$tdir/f $NR ||
8946                 error "failed to create $NR files in $DIR/$tdir"
8947
8948         cancel_lru_locks mdc
8949         ls -l $DIR/$tdir > /dev/null
8950
8951         local NSDIR=""
8952         local LRU_SIZE=0
8953         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
8954                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
8955                 LRU_SIZE=$($LCTL get_param -n $PARAM)
8956                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
8957                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
8958                         log "NSDIR=$NSDIR"
8959                         log "NS=$(basename $NSDIR)"
8960                         break
8961                 fi
8962         done
8963
8964         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
8965                 skip "Not enough cached locks created!"
8966                 return 0
8967         fi
8968         log "LRU=$LRU_SIZE"
8969
8970         local SLEEP=30
8971
8972         # We know that lru resize allows one client to hold $LIMIT locks
8973         # for 10h. After that locks begin to be killed by client.
8974         local MAX_HRS=10
8975         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
8976         log "LIMIT=$LIMIT"
8977         if [ $LIMIT -lt $LRU_SIZE ]; then
8978             skip "Limit is too small $LIMIT"
8979             return 0
8980         fi
8981
8982         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
8983         # killing locks. Some time was spent for creating locks. This means
8984         # that up to the moment of sleep finish we must have killed some of
8985         # them (10-100 locks). This depends on how fast ther were created.
8986         # Many of them were touched in almost the same moment and thus will
8987         # be killed in groups.
8988         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
8989
8990         # Use $LRU_SIZE_B here to take into account real number of locks
8991         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
8992         local LRU_SIZE_B=$LRU_SIZE
8993         log "LVF=$LVF"
8994         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
8995         log "OLD_LVF=$OLD_LVF"
8996         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
8997
8998         # Let's make sure that we really have some margin. Client checks
8999         # cached locks every 10 sec.
9000         SLEEP=$((SLEEP+20))
9001         log "Sleep ${SLEEP} sec"
9002         local SEC=0
9003         while ((SEC<$SLEEP)); do
9004                 echo -n "..."
9005                 sleep 5
9006                 SEC=$((SEC+5))
9007                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9008                 echo -n "$LRU_SIZE"
9009         done
9010         echo ""
9011         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9012         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9013
9014         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9015                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9016                 unlinkmany $DIR/$tdir/f $NR
9017                 return
9018         }
9019
9020         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9021         log "unlink $NR files at $DIR/$tdir"
9022         unlinkmany $DIR/$tdir/f $NR
9023 }
9024 run_test 124a "lru resize ======================================="
9025
9026 get_max_pool_limit()
9027 {
9028         local limit=$($LCTL get_param \
9029                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9030         local max=0
9031         for l in $limit; do
9032                 if [[ $l -gt $max ]]; then
9033                         max=$l
9034                 fi
9035         done
9036         echo $max
9037 }
9038
9039 test_124b() {
9040         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9041         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9042                 { skip "no lru resize on server"; return 0; }
9043
9044         LIMIT=$(get_max_pool_limit)
9045
9046         NR=$(($(default_lru_size)*20))
9047         if [[ $NR -gt $LIMIT ]]; then
9048                 log "Limit lock number by $LIMIT locks"
9049                 NR=$LIMIT
9050         fi
9051
9052         IFree=$(mdsrate_inodes_available)
9053         if [ $IFree -lt $NR ]; then
9054                 log "Limit lock number by $IFree inodes"
9055                 NR=$IFree
9056         fi
9057
9058         lru_resize_disable mdc
9059         test_mkdir -p $DIR/$tdir/disable_lru_resize
9060
9061         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9062         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9063         cancel_lru_locks mdc
9064         stime=`date +%s`
9065         PID=""
9066         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9067         PID="$PID $!"
9068         sleep 2
9069         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9070         PID="$PID $!"
9071         sleep 2
9072         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9073         PID="$PID $!"
9074         wait $PID
9075         etime=`date +%s`
9076         nolruresize_delta=$((etime-stime))
9077         log "ls -la time: $nolruresize_delta seconds"
9078         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9079         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9080
9081         lru_resize_enable mdc
9082         test_mkdir -p $DIR/$tdir/enable_lru_resize
9083
9084         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9085         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9086         cancel_lru_locks mdc
9087         stime=`date +%s`
9088         PID=""
9089         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9090         PID="$PID $!"
9091         sleep 2
9092         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9093         PID="$PID $!"
9094         sleep 2
9095         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9096         PID="$PID $!"
9097         wait $PID
9098         etime=`date +%s`
9099         lruresize_delta=$((etime-stime))
9100         log "ls -la time: $lruresize_delta seconds"
9101         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9102
9103         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9104                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9105         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9106                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9107         else
9108                 log "lru resize performs the same with no lru resize"
9109         fi
9110         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9111 }
9112 run_test 124b "lru resize (performance test) ======================="
9113
9114 test_124c() {
9115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9116         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9117                 { skip "no lru resize on server"; return 0; }
9118
9119         # cache ununsed locks on client
9120         local nr=100
9121         cancel_lru_locks mdc
9122         test_mkdir $DIR/$tdir
9123         createmany -o $DIR/$tdir/f $nr ||
9124                 error "failed to create $nr files in $DIR/$tdir"
9125         ls -l $DIR/$tdir > /dev/null
9126
9127         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9128         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9129         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9130         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9131         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9132
9133         # set lru_max_age to 1 sec
9134         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9135         echo "sleep $((recalc_p * 2)) seconds..."
9136         sleep $((recalc_p * 2))
9137
9138         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
9139         # restore lru_max_age
9140         $LCTL set_param -n $nsdir.lru_max_age $max_age
9141         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
9142         unlinkmany $DIR/$tdir/f $nr
9143 }
9144 run_test 124c "LRUR cancel very aged locks"
9145
9146 test_125() { # 13358
9147         $LCTL get_param -n llite.*.client_type | grep -q local ||
9148                 { skip "must run as local client"; return; }
9149         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
9150                 { skip "must have acl enabled"; return; }
9151         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9152         test_mkdir $DIR/$tdir
9153         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
9154         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
9155         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
9156 }
9157 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
9158
9159 test_126() { # bug 12829/13455
9160         $LCTL get_param -n llite.*.client_type | grep -q local ||
9161                 { skip "must run as local client"; return; }
9162         [ "$UID" != 0 ] && skip_env "$TESTNAME (must run as root)" && return
9163         $GSS && skip "must run as gss disabled" && return
9164
9165         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
9166         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
9167         rm -f $DIR/$tfile
9168         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
9169 }
9170 run_test 126 "check that the fsgid provided by the client is taken into account"
9171
9172 test_127a() { # bug 15521
9173         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9174         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
9175         $LCTL set_param osc.*.stats=0
9176         FSIZE=$((2048 * 1024))
9177         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9178         cancel_lru_locks osc
9179         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
9180
9181         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
9182         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9183                 echo "got $COUNT $NAME"
9184                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
9185                 eval $NAME=$COUNT || error "Wrong proc format"
9186
9187                 case $NAME in
9188                         read_bytes|write_bytes)
9189                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
9190                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
9191                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
9192                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
9193                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
9194                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
9195                                 error "sumsquare is too small: $SUMSQ"
9196                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
9197                                 error "sumsquare is too big: $SUMSQ"
9198                         ;;
9199                         *) ;;
9200                 esac
9201         done < $DIR/${tfile}.tmp
9202
9203         #check that we actually got some stats
9204         [ "$read_bytes" ] || error "Missing read_bytes stats"
9205         [ "$write_bytes" ] || error "Missing write_bytes stats"
9206         [ "$read_bytes" != 0 ] || error "no read done"
9207         [ "$write_bytes" != 0 ] || error "no write done"
9208 }
9209 run_test 127a "verify the client stats are sane"
9210
9211 test_127b() { # bug LU-333
9212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9213         $LCTL set_param llite.*.stats=0
9214         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
9215         # perform 2 reads and writes so MAX is different from SUM.
9216         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9217         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9218         cancel_lru_locks osc
9219         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9220         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9221
9222         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
9223         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9224                 echo "got $COUNT $NAME"
9225                 eval $NAME=$COUNT || error "Wrong proc format"
9226
9227         case $NAME in
9228                 read_bytes)
9229                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9230                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9231                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9232                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9233                         ;;
9234                 write_bytes)
9235                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9236                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9237                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9238                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9239                         ;;
9240                         *) ;;
9241                 esac
9242         done < $TMP/${tfile}.tmp
9243
9244         #check that we actually got some stats
9245         [ "$read_bytes" ] || error "Missing read_bytes stats"
9246         [ "$write_bytes" ] || error "Missing write_bytes stats"
9247         [ "$read_bytes" != 0 ] || error "no read done"
9248         [ "$write_bytes" != 0 ] || error "no write done"
9249
9250         rm -f $TMP/${tfile}.tmp
9251 }
9252 run_test 127b "verify the llite client stats are sane"
9253
9254 test_128() { # bug 15212
9255         touch $DIR/$tfile
9256         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
9257                 find $DIR/$tfile
9258                 find $DIR/$tfile
9259         EOF
9260
9261         result=$(grep error $TMP/$tfile.log)
9262         rm -f $DIR/$tfile $TMP/$tfile.log
9263         [ -z "$result" ] ||
9264                 error "consecutive find's under interactive lfs failed"
9265 }
9266 run_test 128 "interactive lfs for 2 consecutive find's"
9267
9268 set_dir_limits () {
9269         local mntdev
9270         local canondev
9271         local node
9272
9273         local ldproc=/proc/fs/ldiskfs
9274         local facets=$(get_facets MDS)
9275
9276         for facet in ${facets//,/ }; do
9277                 canondev=$(ldiskfs_canon \
9278                            *.$(convert_facet2label $facet).mntdev $facet)
9279                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
9280                         ldproc=/sys/fs/ldiskfs
9281                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
9282                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
9283         done
9284 }
9285
9286 check_mds_dmesg() {
9287         local facets=$(get_facets MDS)
9288         for facet in ${facets//,/ }; do
9289                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
9290         done
9291         return 1
9292 }
9293
9294 test_129() {
9295         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9296                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9297
9298         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9299         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9300                 skip "ldiskfs only test"
9301                 return
9302         fi
9303         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9304         local ENOSPC=28
9305         local EFBIG=27
9306         local has_warning=false
9307
9308         rm -rf $DIR/$tdir
9309         mkdir -p $DIR/$tdir
9310
9311         # block size of mds1
9312         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
9313         set_dir_limits $maxsize $maxsize
9314         local dirsize=$(stat -c%s "$DIR/$tdir")
9315         local nfiles=0
9316         while [[ $dirsize -le $maxsize ]]; do
9317                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
9318                 rc=$?
9319                 if ! $has_warning; then
9320                         check_mds_dmesg '"is approaching"' && has_warning=true
9321                 fi
9322                 # check two errors:
9323                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
9324                 # EFBIG for previous versions included in ldiskfs series
9325                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9326                         set_dir_limits 0 0
9327                         echo "return code $rc received as expected"
9328
9329                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
9330                                 error_exit "create failed w/o dir size limit"
9331
9332                         check_mds_dmesg '"has reached"' ||
9333                                 error_exit "reached message should be output"
9334
9335                         [ $has_warning -eq 0 ] &&
9336                                 error_exit "warning message should be output"
9337
9338                         dirsize=$(stat -c%s "$DIR/$tdir")
9339
9340                         [[ $dirsize -ge $maxsize ]] && return 0
9341                         error_exit "current dir size $dirsize, " \
9342                                    "previous limit $maxsize"
9343                 elif [ $rc -ne 0 ]; then
9344                         set_dir_limits 0 0
9345                         error_exit "return $rc received instead of expected " \
9346                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
9347                 fi
9348                 nfiles=$((nfiles + 1))
9349                 dirsize=$(stat -c%s "$DIR/$tdir")
9350         done
9351
9352         set_dir_limits 0 0
9353         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
9354 }
9355 run_test 129 "test directory size limit ========================"
9356
9357 OLDIFS="$IFS"
9358 cleanup_130() {
9359         trap 0
9360         IFS="$OLDIFS"
9361 }
9362
9363 test_130a() {
9364         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9365         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9366                 return
9367
9368         trap cleanup_130 EXIT RETURN
9369
9370         local fm_file=$DIR/$tfile
9371         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9372         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9373                 error "dd failed for $fm_file"
9374
9375         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9376         filefrag -ves $fm_file
9377         RC=$?
9378         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9379                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9380         [ $RC != 0 ] && error "filefrag $fm_file failed"
9381
9382         filefrag_op=$(filefrag -ve $fm_file |
9383                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9384         lun=$($GETSTRIPE -i $fm_file)
9385
9386         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9387         IFS=$'\n'
9388         tot_len=0
9389         for line in $filefrag_op
9390         do
9391                 frag_lun=`echo $line | cut -d: -f5`
9392                 ext_len=`echo $line | cut -d: -f4`
9393                 if (( $frag_lun != $lun )); then
9394                         cleanup_130
9395                         error "FIEMAP on 1-stripe file($fm_file) failed"
9396                         return
9397                 fi
9398                 (( tot_len += ext_len ))
9399         done
9400
9401         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9402                 cleanup_130
9403                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9404                 return
9405         fi
9406
9407         cleanup_130
9408
9409         echo "FIEMAP on single striped file succeeded"
9410 }
9411 run_test 130a "FIEMAP (1-stripe file)"
9412
9413 test_130b() {
9414         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
9415
9416         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9417         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9418                 return
9419
9420         trap cleanup_130 EXIT RETURN
9421
9422         local fm_file=$DIR/$tfile
9423         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9424                         error "setstripe on $fm_file"
9425         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9426                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9427
9428         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9429                 error "dd failed on $fm_file"
9430
9431         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9432         filefrag_op=$(filefrag -ve $fm_file |
9433                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9434
9435         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9436                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9437
9438         IFS=$'\n'
9439         tot_len=0
9440         num_luns=1
9441         for line in $filefrag_op
9442         do
9443                 frag_lun=$(echo $line | cut -d: -f5 |
9444                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9445                 ext_len=$(echo $line | cut -d: -f4)
9446                 if (( $frag_lun != $last_lun )); then
9447                         if (( tot_len != 1024 )); then
9448                                 cleanup_130
9449                                 error "FIEMAP on $fm_file failed; returned " \
9450                                 "len $tot_len for OST $last_lun instead of 1024"
9451                                 return
9452                         else
9453                                 (( num_luns += 1 ))
9454                                 tot_len=0
9455                         fi
9456                 fi
9457                 (( tot_len += ext_len ))
9458                 last_lun=$frag_lun
9459         done
9460         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9461                 cleanup_130
9462                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9463                         "luns or wrong len for OST $last_lun"
9464                 return
9465         fi
9466
9467         cleanup_130
9468
9469         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9470 }
9471 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9472
9473 test_130c() {
9474         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9475
9476         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9477         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9478                 return
9479
9480         trap cleanup_130 EXIT RETURN
9481
9482         local fm_file=$DIR/$tfile
9483         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9484         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9485                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9486
9487         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9488                         error "dd failed on $fm_file"
9489
9490         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9491         filefrag_op=$(filefrag -ve $fm_file |
9492                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9493
9494         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9495                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9496
9497         IFS=$'\n'
9498         tot_len=0
9499         num_luns=1
9500         for line in $filefrag_op
9501         do
9502                 frag_lun=$(echo $line | cut -d: -f5 |
9503                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9504                 ext_len=$(echo $line | cut -d: -f4)
9505                 if (( $frag_lun != $last_lun )); then
9506                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9507                         if (( logical != 512 )); then
9508                                 cleanup_130
9509                                 error "FIEMAP on $fm_file failed; returned " \
9510                                 "logical start for lun $logical instead of 512"
9511                                 return
9512                         fi
9513                         if (( tot_len != 512 )); then
9514                                 cleanup_130
9515                                 error "FIEMAP on $fm_file failed; returned " \
9516                                 "len $tot_len for OST $last_lun instead of 1024"
9517                                 return
9518                         else
9519                                 (( num_luns += 1 ))
9520                                 tot_len=0
9521                         fi
9522                 fi
9523                 (( tot_len += ext_len ))
9524                 last_lun=$frag_lun
9525         done
9526         if (( num_luns != 2 || tot_len != 512 )); then
9527                 cleanup_130
9528                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9529                         "luns or wrong len for OST $last_lun"
9530                 return
9531         fi
9532
9533         cleanup_130
9534
9535         echo "FIEMAP on 2-stripe file with hole succeeded"
9536 }
9537 run_test 130c "FIEMAP (2-stripe file with hole)"
9538
9539 test_130d() {
9540         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
9541
9542         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9543         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9544                 return
9545
9546         trap cleanup_130 EXIT RETURN
9547
9548         local fm_file=$DIR/$tfile
9549         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9550                         error "setstripe on $fm_file"
9551         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9552                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9553
9554         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
9555         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
9556                 error "dd failed on $fm_file"
9557
9558         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9559         filefrag_op=$(filefrag -ve $fm_file |
9560                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9561
9562         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9563                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9564
9565         IFS=$'\n'
9566         tot_len=0
9567         num_luns=1
9568         for line in $filefrag_op
9569         do
9570                 frag_lun=$(echo $line | cut -d: -f5 |
9571                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9572                 ext_len=$(echo $line | cut -d: -f4)
9573                 if (( $frag_lun != $last_lun )); then
9574                         if (( tot_len != 1024 )); then
9575                                 cleanup_130
9576                                 error "FIEMAP on $fm_file failed; returned " \
9577                                 "len $tot_len for OST $last_lun instead of 1024"
9578                                 return
9579                         else
9580                                 (( num_luns += 1 ))
9581                                 tot_len=0
9582                         fi
9583                 fi
9584                 (( tot_len += ext_len ))
9585                 last_lun=$frag_lun
9586         done
9587         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
9588                 cleanup_130
9589                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9590                         "luns or wrong len for OST $last_lun"
9591                 return
9592         fi
9593
9594         cleanup_130
9595
9596         echo "FIEMAP on N-stripe file succeeded"
9597 }
9598 run_test 130d "FIEMAP (N-stripe file)"
9599
9600 test_130e() {
9601         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9602
9603         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9604         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9605
9606         trap cleanup_130 EXIT RETURN
9607
9608         local fm_file=$DIR/$tfile
9609         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9610         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9611                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9612
9613         NUM_BLKS=512
9614         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9615         for ((i = 0; i < $NUM_BLKS; i++))
9616         do
9617                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9618         done
9619
9620         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9621         filefrag_op=$(filefrag -ve $fm_file |
9622                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9623
9624         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9625                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9626
9627         IFS=$'\n'
9628         tot_len=0
9629         num_luns=1
9630         for line in $filefrag_op
9631         do
9632                 frag_lun=$(echo $line | cut -d: -f5 |
9633                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9634                 ext_len=$(echo $line | cut -d: -f4)
9635                 if (( $frag_lun != $last_lun )); then
9636                         if (( tot_len != $EXPECTED_LEN )); then
9637                                 cleanup_130
9638                                 error "FIEMAP on $fm_file failed; returned " \
9639                                 "len $tot_len for OST $last_lun instead " \
9640                                 "of $EXPECTED_LEN"
9641                                 return
9642                         else
9643                                 (( num_luns += 1 ))
9644                                 tot_len=0
9645                         fi
9646                 fi
9647                 (( tot_len += ext_len ))
9648                 last_lun=$frag_lun
9649         done
9650         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9651                 cleanup_130
9652                 error "FIEMAP on $fm_file failed; returned wrong number " \
9653                         "of luns or wrong len for OST $last_lun"
9654                 return
9655         fi
9656
9657         cleanup_130
9658
9659         echo "FIEMAP with continuation calls succeeded"
9660 }
9661 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9662
9663 # Test for writev/readv
9664 test_131a() {
9665         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
9666                 error "writev test failed"
9667         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
9668                 error "readv failed"
9669         rm -f $DIR/$tfile
9670 }
9671 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9672
9673 test_131b() {
9674         local fsize=$((524288 + 1048576 + 1572864))
9675         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
9676                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9677                         error "append writev test failed"
9678
9679         ((fsize += 1572864 + 1048576))
9680         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
9681                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9682                         error "append writev test failed"
9683         rm -f $DIR/$tfile
9684 }
9685 run_test 131b "test append writev"
9686
9687 test_131c() {
9688         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9689         error "NOT PASS"
9690 }
9691 run_test 131c "test read/write on file w/o objects"
9692
9693 test_131d() {
9694         rwv -f $DIR/$tfile -w -n 1 1572864
9695         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9696         if [ "$NOB" != 1572864 ]; then
9697                 error "Short read filed: read $NOB bytes instead of 1572864"
9698         fi
9699         rm -f $DIR/$tfile
9700 }
9701 run_test 131d "test short read"
9702
9703 test_131e() {
9704         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9705         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9706         error "read hitting hole failed"
9707         rm -f $DIR/$tfile
9708 }
9709 run_test 131e "test read hitting hole"
9710
9711 check_stats() {
9712         local facet=$1
9713         local op=$2
9714         local want=${3:-0}
9715         local res
9716
9717         case $facet in
9718         mds*) res=$(do_facet $facet \
9719                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
9720                  ;;
9721         ost*) res=$(do_facet $facet \
9722                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
9723                  ;;
9724         *) error "Wrong facet '$facet'" ;;
9725         esac
9726         echo $res
9727         [ "$res" ] || error "The counter for $op on $facet was not incremented"
9728         # if the argument $3 is zero, it means any stat increment is ok.
9729         if [[ $want -gt 0 ]]; then
9730                 local count=$(echo $res | awk '{ print $2 }')
9731                 [[ $count -ne $want ]] &&
9732                         error "The $op counter on $facet is $count, not $want"
9733         fi
9734 }
9735
9736 test_133a() {
9737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9738         remote_ost_nodsh && skip "remote OST with nodsh" && return
9739         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9740
9741         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9742                 { skip "MDS doesn't support rename stats"; return; }
9743         local testdir=$DIR/${tdir}/stats_testdir
9744         mkdir -p $DIR/${tdir}
9745
9746         # clear stats.
9747         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9748         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9749
9750         # verify mdt stats first.
9751         mkdir ${testdir} || error "mkdir failed"
9752         check_stats $SINGLEMDS "mkdir" 1
9753         touch ${testdir}/${tfile} || error "touch failed"
9754         check_stats $SINGLEMDS "open" 1
9755         check_stats $SINGLEMDS "close" 1
9756         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
9757                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
9758                 check_stats $SINGLEMDS "mknod" 2
9759         }
9760         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
9761         check_stats $SINGLEMDS "unlink" 1
9762         rm -f ${testdir}/${tfile} || error "file remove failed"
9763         check_stats $SINGLEMDS "unlink" 2
9764
9765         # remove working dir and check mdt stats again.
9766         rmdir ${testdir} || error "rmdir failed"
9767         check_stats $SINGLEMDS "rmdir" 1
9768
9769         local testdir1=$DIR/${tdir}/stats_testdir1
9770         mkdir -p ${testdir}
9771         mkdir -p ${testdir1}
9772         touch ${testdir1}/test1
9773         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9774         check_stats $SINGLEMDS "crossdir_rename" 1
9775
9776         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9777         check_stats $SINGLEMDS "samedir_rename" 1
9778
9779         rm -rf $DIR/${tdir}
9780 }
9781 run_test 133a "Verifying MDT stats ========================================"
9782
9783 test_133b() {
9784         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9785         remote_ost_nodsh && skip "remote OST with nodsh" && return
9786         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9787         local testdir=$DIR/${tdir}/stats_testdir
9788         mkdir -p ${testdir} || error "mkdir failed"
9789         touch ${testdir}/${tfile} || error "touch failed"
9790         cancel_lru_locks mdc
9791
9792         # clear stats.
9793         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9794         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9795
9796         # extra mdt stats verification.
9797         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9798         check_stats $SINGLEMDS "setattr" 1
9799         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9800         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9801         then            # LU-1740
9802                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9803                 check_stats $SINGLEMDS "getattr" 1
9804         fi
9805         $LFS df || error "lfs failed"
9806         check_stats $SINGLEMDS "statfs" 1
9807
9808         rm -rf $DIR/${tdir}
9809 }
9810 run_test 133b "Verifying extra MDT stats =================================="
9811
9812 test_133c() {
9813         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9814         remote_ost_nodsh && skip "remote OST with nodsh" && return
9815         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9816         local testdir=$DIR/$tdir/stats_testdir
9817         test_mkdir -p $testdir
9818
9819         # verify obdfilter stats.
9820         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
9821         sync
9822         cancel_lru_locks osc
9823         wait_delete_completed
9824
9825         # clear stats.
9826         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9827         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9828
9829         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
9830                 error "dd failed"
9831         sync
9832         cancel_lru_locks osc
9833         check_stats ost1 "write" 1
9834
9835         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
9836         check_stats ost1 "read" 1
9837
9838         > $testdir/$tfile || error "truncate failed"
9839         check_stats ost1 "punch" 1
9840
9841         rm -f $testdir/$tfile || error "file remove failed"
9842         wait_delete_completed
9843         check_stats ost1 "destroy" 1
9844
9845         rm -rf $DIR/$tdir
9846 }
9847 run_test 133c "Verifying OST stats ========================================"
9848
9849 order_2() {
9850         local value=$1
9851         local orig=$value
9852         local order=1
9853
9854         while [ $value -ge 2 ]; do
9855                 order=$((order*2))
9856                 value=$((value/2))
9857         done
9858
9859         if [ $orig -gt $order ]; then
9860                 order=$((order*2))
9861         fi
9862         echo $order
9863 }
9864
9865 size_in_KMGT() {
9866     local value=$1
9867     local size=('K' 'M' 'G' 'T');
9868     local i=0
9869     local size_string=$value
9870
9871     while [ $value -ge 1024 ]; do
9872         if [ $i -gt 3 ]; then
9873             #T is the biggest unit we get here, if that is bigger,
9874             #just return XXXT
9875             size_string=${value}T
9876             break
9877         fi
9878         value=$((value >> 10))
9879         if [ $value -lt 1024 ]; then
9880             size_string=${value}${size[$i]}
9881             break
9882         fi
9883         i=$((i + 1))
9884     done
9885
9886     echo $size_string
9887 }
9888
9889 get_rename_size() {
9890     local size=$1
9891     local context=${2:-.}
9892     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
9893                 grep -A1 $context |
9894                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9895     echo $sample
9896 }
9897
9898 test_133d() {
9899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9900         remote_ost_nodsh && skip "remote OST with nodsh" && return
9901         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9902         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9903         { skip "MDS doesn't support rename stats"; return; }
9904
9905         local testdir1=$DIR/${tdir}/stats_testdir1
9906         local testdir2=$DIR/${tdir}/stats_testdir2
9907
9908         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9909
9910         mkdir -p ${testdir1} || error "mkdir failed"
9911         mkdir -p ${testdir2} || error "mkdir failed"
9912
9913         createmany -o $testdir1/test 512 || error "createmany failed"
9914
9915         # check samedir rename size
9916         mv ${testdir1}/test0 ${testdir1}/test_0
9917
9918         local testdir1_size=$(ls -l $DIR/${tdir} |
9919                 awk '/stats_testdir1/ {print $5}')
9920         local testdir2_size=$(ls -l $DIR/${tdir} |
9921                 awk '/stats_testdir2/ {print $5}')
9922
9923         testdir1_size=$(order_2 $testdir1_size)
9924         testdir2_size=$(order_2 $testdir2_size)
9925
9926         testdir1_size=$(size_in_KMGT $testdir1_size)
9927         testdir2_size=$(size_in_KMGT $testdir2_size)
9928
9929         echo "source rename dir size: ${testdir1_size}"
9930         echo "target rename dir size: ${testdir2_size}"
9931
9932         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
9933         eval $cmd || error "$cmd failed"
9934         local samedir=$($cmd | grep 'same_dir')
9935         local same_sample=$(get_rename_size $testdir1_size)
9936         [ -z "$samedir" ] && error "samedir_rename_size count error"
9937         [[ $same_sample -eq 1 ]] ||
9938                 error "samedir_rename_size error $same_sample"
9939         echo "Check same dir rename stats success"
9940
9941         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9942
9943         # check crossdir rename size
9944         mv ${testdir1}/test_0 ${testdir2}/test_0
9945
9946         testdir1_size=$(ls -l $DIR/${tdir} |
9947                 awk '/stats_testdir1/ {print $5}')
9948         testdir2_size=$(ls -l $DIR/${tdir} |
9949                 awk '/stats_testdir2/ {print $5}')
9950
9951         testdir1_size=$(order_2 $testdir1_size)
9952         testdir2_size=$(order_2 $testdir2_size)
9953
9954         testdir1_size=$(size_in_KMGT $testdir1_size)
9955         testdir2_size=$(size_in_KMGT $testdir2_size)
9956
9957         echo "source rename dir size: ${testdir1_size}"
9958         echo "target rename dir size: ${testdir2_size}"
9959
9960         eval $cmd || error "$cmd failed"
9961         local crossdir=$($cmd | grep 'crossdir')
9962         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
9963         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
9964         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
9965         [[ $src_sample -eq 1 ]] ||
9966                 error "crossdir_rename_size error $src_sample"
9967         [[ $tgt_sample -eq 1 ]] ||
9968                 error "crossdir_rename_size error $tgt_sample"
9969         echo "Check cross dir rename stats success"
9970         rm -rf $DIR/${tdir}
9971 }
9972 run_test 133d "Verifying rename_stats ========================================"
9973
9974 test_133e() {
9975         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9976         remote_ost_nodsh && skip "remote OST with nodsh" && return
9977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9978         local testdir=$DIR/${tdir}/stats_testdir
9979         local ctr f0 f1 bs=32768 count=42 sum
9980
9981         mkdir -p ${testdir} || error "mkdir failed"
9982
9983         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9984
9985         for ctr in {write,read}_bytes; do
9986                 sync
9987                 cancel_lru_locks osc
9988
9989                 do_facet ost1 $LCTL set_param -n \
9990                         "obdfilter.*.exports.clear=clear"
9991
9992                 if [ $ctr = write_bytes ]; then
9993                         f0=/dev/zero
9994                         f1=${testdir}/${tfile}
9995                 else
9996                         f0=${testdir}/${tfile}
9997                         f1=/dev/null
9998                 fi
9999
10000                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10001                         error "dd failed"
10002                 sync
10003                 cancel_lru_locks osc
10004
10005                 sum=$(do_facet ost1 $LCTL get_param \
10006                         "obdfilter.*.exports.*.stats" |
10007                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10008                                 $1 == ctr { sum += $7 }
10009                                 END { printf("%0.0f", sum) }')
10010
10011                 if ((sum != bs * count)); then
10012                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10013                 fi
10014         done
10015
10016         rm -rf $DIR/${tdir}
10017 }
10018 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10019
10020 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10021
10022 test_133f() {
10023         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10024         remote_ost_nodsh && skip "remote OST with nodsh" && return
10025         # First without trusting modes.
10026         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10027         echo "proc_dirs='$proc_dirs'"
10028         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10029         find $proc_dirs -exec cat '{}' \; &> /dev/null
10030
10031         # Second verifying readability.
10032         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10033
10034         # eventually, this can also be replaced with "lctl get_param -R",
10035         # but not until that option is always available on the server
10036         local facet
10037         for facet in mds1 ost1; do
10038                 local facet_proc_dirs=$(do_facet $facet \
10039                                         \\\ls -d $proc_regexp 2>/dev/null)
10040                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10041                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10042                 do_facet $facet find $facet_proc_dirs \
10043                         ! -name req_history \
10044                         -exec cat '{}' \\\; &> /dev/null
10045
10046                 do_facet $facet find $facet_proc_dirs \
10047                         ! -name req_history \
10048                         -type f \
10049                         -exec cat '{}' \\\; &> /dev/null ||
10050                                 error "proc file read failed"
10051         done
10052 }
10053 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
10054
10055 test_133g() {
10056         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10057         remote_ost_nodsh && skip "remote OST with nodsh" && return
10058         # Second verifying writability.
10059         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10060         echo "proc_dirs='$proc_dirs'"
10061         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10062         find $proc_dirs \
10063                 -type f \
10064                 -not -name force_lbug \
10065                 -not -name changelog_mask \
10066                 -exec badarea_io '{}' \; ||
10067                 error "find $proc_dirs failed"
10068
10069         local facet
10070         for facet in mds1 ost1; do
10071                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10072                         skip "Too old lustre on $facet" && continue
10073                 local facet_proc_dirs=$(do_facet $facet \
10074                                         \\\ls -d $proc_regexp 2> /dev/null)
10075                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10076                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10077                 do_facet $facet find $facet_proc_dirs \
10078                         -type f \
10079                         -not -name force_lbug \
10080                         -not -name changelog_mask \
10081                         -exec badarea_io '{}' \\\; ||
10082                                 error "$facet find $facet_proc_dirs failed"
10083         done
10084
10085         # remount the FS in case writes/reads /proc break the FS
10086         cleanup || error "failed to unmount"
10087         setup || error "failed to setup"
10088         true
10089 }
10090 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
10091
10092 test_133h() {
10093         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10094         remote_ost_nodsh && skip "remote OST with nodsh" && return
10095         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10096                 skip "Need MDS version at least 2.9.54" && return
10097
10098         local facet
10099         for facet in client mds1 ost1; do
10100                 local facet_proc_dirs=$(do_facet $facet \
10101                                         \\\ls -d $proc_regexp 2> /dev/null)
10102                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10103                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10104                 # Get the list of files that are missing the terminating newline
10105                 local missing=($(do_facet $facet \
10106                         find ${facet_proc_dirs} -type f \|              \
10107                                 while read F\; do                       \
10108                                         awk -v FS='\v' -v RS='\v\v'     \
10109                                         "'END { if(NR>0 &&              \
10110                                         \\\$NF !~ /.*\\\n\$/)           \
10111                                                 print FILENAME}'"       \
10112                                         '\$F'\;                         \
10113                                 done 2>/dev/null))
10114                 [ ${#missing[*]} -eq 0 ] ||
10115                         error "files do not end with newline: ${missing[*]}"
10116         done
10117 }
10118 run_test 133h "Proc files should end with newlines"
10119
10120 test_134a() {
10121         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10122         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10123                 skip "Need MDS version at least 2.7.54" && return
10124
10125         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10126         cancel_lru_locks mdc
10127
10128         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10129         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10130         [ $unused -eq 0 ] || error "$unused locks are not cleared"
10131
10132         local nr=1000
10133         createmany -o $DIR/$tdir/f $nr ||
10134                 error "failed to create $nr files in $DIR/$tdir"
10135         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10136
10137         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
10138         do_facet mds1 $LCTL set_param fail_loc=0x327
10139         do_facet mds1 $LCTL set_param fail_val=500
10140         touch $DIR/$tdir/m
10141
10142         echo "sleep 10 seconds ..."
10143         sleep 10
10144         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
10145
10146         do_facet mds1 $LCTL set_param fail_loc=0
10147         do_facet mds1 $LCTL set_param fail_val=0
10148         [ $lck_cnt -lt $unused ] ||
10149                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
10150
10151         rm $DIR/$tdir/m
10152         unlinkmany $DIR/$tdir/f $nr
10153 }
10154 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
10155
10156 test_134b() {
10157         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10158         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10159                 skip "Need MDS version at least 2.7.54" && return
10160
10161         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10162         cancel_lru_locks mdc
10163
10164         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10165                         ldlm.lock_reclaim_threshold_mb)
10166         # disable reclaim temporarily
10167         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10168
10169         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10170         do_facet mds1 $LCTL set_param fail_loc=0x328
10171         do_facet mds1 $LCTL set_param fail_val=500
10172
10173         $LCTL set_param debug=+trace
10174
10175         local nr=600
10176         createmany -o $DIR/$tdir/f $nr &
10177         local create_pid=$!
10178
10179         echo "Sleep $TIMEOUT seconds ..."
10180         sleep $TIMEOUT
10181         if ! ps -p $create_pid  > /dev/null 2>&1; then
10182                 do_facet mds1 $LCTL set_param fail_loc=0
10183                 do_facet mds1 $LCTL set_param fail_val=0
10184                 do_facet mds1 $LCTL set_param \
10185                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10186                 error "createmany finished incorrectly!"
10187         fi
10188         do_facet mds1 $LCTL set_param fail_loc=0
10189         do_facet mds1 $LCTL set_param fail_val=0
10190         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10191         wait $create_pid || return 1
10192
10193         unlinkmany $DIR/$tdir/f $nr
10194 }
10195 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10196
10197 test_140() { #bug-17379
10198         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10199         test_mkdir $DIR/$tdir
10200         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10201         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10202
10203         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10204         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10205         local i=0
10206         while i=$((i + 1)); do
10207                 test_mkdir $i
10208                 cd $i || error "Changing to $i"
10209                 ln -s ../stat stat || error "Creating stat symlink"
10210                 # Read the symlink until ELOOP present,
10211                 # not LBUGing the system is considered success,
10212                 # we didn't overrun the stack.
10213                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10214                 if [ $ret -ne 0 ]; then
10215                         if [ $ret -eq 40 ]; then
10216                                 break  # -ELOOP
10217                         else
10218                                 error "Open stat symlink"
10219                                         return
10220                         fi
10221                 fi
10222         done
10223         i=$((i - 1))
10224         echo "The symlink depth = $i"
10225         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10226                                         error "Invalid symlink depth"
10227
10228         # Test recursive symlink
10229         ln -s symlink_self symlink_self
10230         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10231         echo "open symlink_self returns $ret"
10232         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10233 }
10234 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10235
10236 test_150() {
10237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10238         local TF="$TMP/$tfile"
10239
10240         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10241         cp $TF $DIR/$tfile
10242         cancel_lru_locks $OSC
10243         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10244         remount_client $MOUNT
10245         df -P $MOUNT
10246         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10247
10248         $TRUNCATE $TF 6000
10249         $TRUNCATE $DIR/$tfile 6000
10250         cancel_lru_locks $OSC
10251         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10252
10253         echo "12345" >>$TF
10254         echo "12345" >>$DIR/$tfile
10255         cancel_lru_locks $OSC
10256         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10257
10258         echo "12345" >>$TF
10259         echo "12345" >>$DIR/$tfile
10260         cancel_lru_locks $OSC
10261         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10262
10263         rm -f $TF
10264         true
10265 }
10266 run_test 150 "truncate/append tests"
10267
10268 #LU-2902 roc_hit was not able to read all values from lproc
10269 function roc_hit_init() {
10270         local list=$(comma_list $(osts_nodes))
10271         local dir=$DIR/$tdir-check
10272         local file=$dir/$tfile
10273         local BEFORE
10274         local AFTER
10275         local idx
10276
10277         test_mkdir $dir
10278         #use setstripe to do a write to every ost
10279         for i in $(seq 0 $((OSTCOUNT-1))); do
10280                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10281                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10282                 idx=$(printf %04x $i)
10283                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10284                         awk '$1 == "cache_access" {sum += $7}
10285                                 END { printf("%0.0f", sum) }')
10286
10287                 cancel_lru_locks osc
10288                 cat $file >/dev/null
10289
10290                 AFTER=$(get_osd_param $list *OST*$idx stats |
10291                         awk '$1 == "cache_access" {sum += $7}
10292                                 END { printf("%0.0f", sum) }')
10293
10294                 echo BEFORE:$BEFORE AFTER:$AFTER
10295                 if ! let "AFTER - BEFORE == 4"; then
10296                         rm -rf $dir
10297                         error "roc_hit is not safe to use"
10298                 fi
10299                 rm $file
10300         done
10301
10302         rm -rf $dir
10303 }
10304
10305 function roc_hit() {
10306         local list=$(comma_list $(osts_nodes))
10307         echo $(get_osd_param $list '' stats |
10308                 awk '$1 == "cache_hit" {sum += $7}
10309                         END { printf("%0.0f", sum) }')
10310 }
10311
10312 function set_cache() {
10313         local on=1
10314
10315         if [ "$2" == "off" ]; then
10316                 on=0;
10317         fi
10318         local list=$(comma_list $(osts_nodes))
10319         set_osd_param $list '' $1_cache_enable $on
10320
10321         cancel_lru_locks osc
10322 }
10323
10324 test_151() {
10325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10326         remote_ost_nodsh && skip "remote OST with nodsh" && return
10327
10328         local CPAGES=3
10329         local list=$(comma_list $(osts_nodes))
10330
10331         # check whether obdfilter is cache capable at all
10332         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10333                 echo "not cache-capable obdfilter"
10334                 return 0
10335         fi
10336
10337         # check cache is enabled on all obdfilters
10338         if get_osd_param $list '' read_cache_enable | grep 0; then
10339                 echo "oss cache is disabled"
10340                 return 0
10341         fi
10342
10343         set_osd_param $list '' writethrough_cache_enable 1
10344
10345         # check write cache is enabled on all obdfilters
10346         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10347                 echo "oss write cache is NOT enabled"
10348                 return 0
10349         fi
10350
10351         roc_hit_init
10352
10353         #define OBD_FAIL_OBD_NO_LRU  0x609
10354         do_nodes $list $LCTL set_param fail_loc=0x609
10355
10356         # pages should be in the case right after write
10357         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10358                 error "dd failed"
10359
10360         local BEFORE=$(roc_hit)
10361         cancel_lru_locks osc
10362         cat $DIR/$tfile >/dev/null
10363         local AFTER=$(roc_hit)
10364
10365         do_nodes $list $LCTL set_param fail_loc=0
10366
10367         if ! let "AFTER - BEFORE == CPAGES"; then
10368                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10369         fi
10370
10371         # the following read invalidates the cache
10372         cancel_lru_locks osc
10373         set_osd_param $list '' read_cache_enable 0
10374         cat $DIR/$tfile >/dev/null
10375
10376         # now data shouldn't be found in the cache
10377         BEFORE=$(roc_hit)
10378         cancel_lru_locks osc
10379         cat $DIR/$tfile >/dev/null
10380         AFTER=$(roc_hit)
10381         if let "AFTER - BEFORE != 0"; then
10382                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10383         fi
10384
10385         set_osd_param $list '' read_cache_enable 1
10386         rm -f $DIR/$tfile
10387 }
10388 run_test 151 "test cache on oss and controls ==============================="
10389
10390 test_152() {
10391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10392         local TF="$TMP/$tfile"
10393
10394         # simulate ENOMEM during write
10395 #define OBD_FAIL_OST_NOMEM      0x226
10396         lctl set_param fail_loc=0x80000226
10397         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10398         cp $TF $DIR/$tfile
10399         sync || error "sync failed"
10400         lctl set_param fail_loc=0
10401
10402         # discard client's cache
10403         cancel_lru_locks osc
10404
10405         # simulate ENOMEM during read
10406         lctl set_param fail_loc=0x80000226
10407         cmp $TF $DIR/$tfile || error "cmp failed"
10408         lctl set_param fail_loc=0
10409
10410         rm -f $TF
10411 }
10412 run_test 152 "test read/write with enomem ============================"
10413
10414 test_153() {
10415         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10416 }
10417 run_test 153 "test if fdatasync does not crash ======================="
10418
10419 dot_lustre_fid_permission_check() {
10420         local fid=$1
10421         local ffid=$MOUNT/.lustre/fid/$fid
10422         local test_dir=$2
10423
10424         echo "stat fid $fid"
10425         stat $ffid > /dev/null || error "stat $ffid failed."
10426         echo "touch fid $fid"
10427         touch $ffid || error "touch $ffid failed."
10428         echo "write to fid $fid"
10429         cat /etc/hosts > $ffid || error "write $ffid failed."
10430         echo "read fid $fid"
10431         diff /etc/hosts $ffid || error "read $ffid failed."
10432         echo "append write to fid $fid"
10433         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10434         echo "rename fid $fid"
10435         mv $ffid $test_dir/$tfile.1 &&
10436                 error "rename $ffid to $tfile.1 should fail."
10437         touch $test_dir/$tfile.1
10438         mv $test_dir/$tfile.1 $ffid &&
10439                 error "rename $tfile.1 to $ffid should fail."
10440         rm -f $test_dir/$tfile.1
10441         echo "truncate fid $fid"
10442         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10443         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10444                 echo "link fid $fid"
10445                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10446         fi
10447         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10448                 echo "setfacl fid $fid"
10449                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10450                 echo "getfacl fid $fid"
10451                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10452         fi
10453         echo "unlink fid $fid"
10454         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10455         echo "mknod fid $fid"
10456         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10457
10458         fid=[0xf00000400:0x1:0x0]
10459         ffid=$MOUNT/.lustre/fid/$fid
10460
10461         echo "stat non-exist fid $fid"
10462         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10463         echo "write to non-exist fid $fid"
10464         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10465         echo "link new fid $fid"
10466         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10467
10468         mkdir -p $test_dir/$tdir
10469         touch $test_dir/$tdir/$tfile
10470         fid=$($LFS path2fid $test_dir/$tdir)
10471         rc=$?
10472         [ $rc -ne 0 ] &&
10473                 error "error: could not get fid for $test_dir/$dir/$tfile."
10474
10475         ffid=$MOUNT/.lustre/fid/$fid
10476
10477         echo "ls $fid"
10478         ls $ffid > /dev/null || error "ls $ffid failed."
10479         echo "touch $fid/$tfile.1"
10480         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10481
10482         echo "touch $MOUNT/.lustre/fid/$tfile"
10483         touch $MOUNT/.lustre/fid/$tfile && \
10484                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10485
10486         echo "setxattr to $MOUNT/.lustre/fid"
10487         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10488
10489         echo "listxattr for $MOUNT/.lustre/fid"
10490         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10491
10492         echo "delxattr from $MOUNT/.lustre/fid"
10493         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10494
10495         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10496         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10497                 error "touch invalid fid should fail."
10498
10499         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10500         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10501                 error "touch non-normal fid should fail."
10502
10503         echo "rename $tdir to $MOUNT/.lustre/fid"
10504         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10505                 error "rename to $MOUNT/.lustre/fid should fail."
10506
10507         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10508         then            # LU-3547
10509                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10510                 local new_obf_mode=777
10511
10512                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10513                 chmod $new_obf_mode $DIR/.lustre/fid ||
10514                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10515
10516                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10517                 [ $obf_mode -eq $new_obf_mode ] ||
10518                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10519
10520                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10521                 chmod $old_obf_mode $DIR/.lustre/fid ||
10522                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10523         fi
10524
10525         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10526         fid=$($LFS path2fid $test_dir/$tfile-2)
10527
10528         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10529         then # LU-5424
10530                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10531                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10532                         error "create lov data thru .lustre failed"
10533         fi
10534         echo "cp /etc/passwd $test_dir/$tfile-2"
10535         cp /etc/passwd $test_dir/$tfile-2 ||
10536                 error "copy to $test_dir/$tfile-2 failed."
10537         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10538         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10539                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10540
10541         rm -rf $test_dir/tfile.lnk
10542         rm -rf $test_dir/$tfile-2
10543 }
10544
10545 test_154A() {
10546         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10547                 skip "Need MDS version at least 2.4.1" && return
10548
10549         local tf=$DIR/$tfile
10550         touch $tf
10551
10552         local fid=$($LFS path2fid $tf)
10553         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10554
10555         # check that we get the same pathname back
10556         local found=$($LFS fid2path $MOUNT "$fid")
10557         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10558         [ "$found" == "$tf" ] ||
10559                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10560 }
10561 run_test 154A "lfs path2fid and fid2path basic checks"
10562
10563 test_154B() {
10564         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10565                 skip "Need MDS version at least 2.4.1" && return
10566
10567         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10568         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10569         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10570         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10571
10572         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10573         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10574
10575         # check that we get the same pathname
10576         echo "PFID: $PFID, name: $name"
10577         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10578         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10579         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10580                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10581
10582         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10583 }
10584 run_test 154B "verify the ll_decode_linkea tool"
10585
10586 test_154a() {
10587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10588         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10589                 { skip "Need MDS version at least 2.2.51"; return 0; }
10590         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10591         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10592
10593         cp /etc/hosts $DIR/$tfile
10594
10595         fid=$($LFS path2fid $DIR/$tfile)
10596         rc=$?
10597         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10598
10599         dot_lustre_fid_permission_check "$fid" $DIR ||
10600                 error "dot lustre permission check $fid failed"
10601
10602         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10603
10604         touch $MOUNT/.lustre/file &&
10605                 error "creation is not allowed under .lustre"
10606
10607         mkdir $MOUNT/.lustre/dir &&
10608                 error "mkdir is not allowed under .lustre"
10609
10610         rm -rf $DIR/$tfile
10611 }
10612 run_test 154a "Open-by-FID"
10613
10614 test_154b() {
10615         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10616         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10617                 { skip "Need MDS version at least 2.2.51"; return 0; }
10618         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10619
10620         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10621
10622         local remote_dir=$DIR/$tdir/remote_dir
10623         local MDTIDX=1
10624         local rc=0
10625
10626         mkdir -p $DIR/$tdir
10627         $LFS mkdir -i $MDTIDX $remote_dir ||
10628                 error "create remote directory failed"
10629
10630         cp /etc/hosts $remote_dir/$tfile
10631
10632         fid=$($LFS path2fid $remote_dir/$tfile)
10633         rc=$?
10634         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10635
10636         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10637                 error "dot lustre permission check $fid failed"
10638         rm -rf $DIR/$tdir
10639 }
10640 run_test 154b "Open-by-FID for remote directory"
10641
10642 test_154c() {
10643         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10644                 skip "Need MDS version at least 2.4.1" && return
10645
10646         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10647         local FID1=$($LFS path2fid $DIR/$tfile.1)
10648         local FID2=$($LFS path2fid $DIR/$tfile.2)
10649         local FID3=$($LFS path2fid $DIR/$tfile.3)
10650
10651         local N=1
10652         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10653                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10654                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10655                 local want=FID$N
10656                 [ "$FID" = "${!want}" ] ||
10657                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10658                 N=$((N + 1))
10659         done
10660
10661         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
10662         do
10663                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
10664                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
10665                 N=$((N + 1))
10666         done
10667 }
10668 run_test 154c "lfs path2fid and fid2path multiple arguments"
10669
10670 test_154d() {
10671         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10672         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10673                 skip "Need MDS version at least 2.5.53" && return
10674
10675         if remote_mds; then
10676                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10677         else
10678                 nid="0@lo"
10679         fi
10680         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10681         local fd
10682         local cmd
10683
10684         rm -f $DIR/$tfile
10685         touch $DIR/$tfile
10686
10687         local fid=$($LFS path2fid $DIR/$tfile)
10688         # Open the file
10689         fd=$(free_fd)
10690         cmd="exec $fd<$DIR/$tfile"
10691         eval $cmd
10692         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10693         echo "$fid_list" | grep "$fid"
10694         rc=$?
10695
10696         cmd="exec $fd>/dev/null"
10697         eval $cmd
10698         if [ $rc -ne 0 ]; then
10699                 error "FID $fid not found in open files list $fid_list"
10700         fi
10701 }
10702 run_test 154d "Verify open file fid"
10703
10704 test_154e()
10705 {
10706         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10707                 skip "Need MDS version at least 2.6.50" && return
10708
10709         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10710                 error ".lustre returned by readdir"
10711         fi
10712 }
10713 run_test 154e ".lustre is not returned by readdir"
10714
10715 test_154f() {
10716         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10717         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10718         test_mkdir -p -c1 $DIR/$tdir/d
10719         # test dirs inherit from its stripe
10720         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10721         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10722         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10723         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10724         touch $DIR/f
10725
10726         # get fid of parents
10727         local FID0=$($LFS path2fid $DIR/$tdir/d)
10728         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10729         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10730         local FID3=$($LFS path2fid $DIR)
10731
10732         # check that path2fid --parents returns expected <parent_fid>/name
10733         # 1) test for a directory (single parent)
10734         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10735         [ "$parent" == "$FID0/foo1" ] ||
10736                 error "expected parent: $FID0/foo1, got: $parent"
10737
10738         # 2) test for a file with nlink > 1 (multiple parents)
10739         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10740         echo "$parent" | grep -F "$FID1/$tfile" ||
10741                 error "$FID1/$tfile not returned in parent list"
10742         echo "$parent" | grep -F "$FID2/link" ||
10743                 error "$FID2/link not returned in parent list"
10744
10745         # 3) get parent by fid
10746         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10747         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10748         echo "$parent" | grep -F "$FID1/$tfile" ||
10749                 error "$FID1/$tfile not returned in parent list (by fid)"
10750         echo "$parent" | grep -F "$FID2/link" ||
10751                 error "$FID2/link not returned in parent list (by fid)"
10752
10753         # 4) test for entry in root directory
10754         parent=$($LFS path2fid --parents $DIR/f)
10755         echo "$parent" | grep -F "$FID3/f" ||
10756                 error "$FID3/f not returned in parent list"
10757
10758         # 5) test it on root directory
10759         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10760                 error "$MOUNT should not have parents"
10761
10762         # enable xattr caching and check that linkea is correctly updated
10763         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10764         save_lustre_params client "llite.*.xattr_cache" > $save
10765         lctl set_param llite.*.xattr_cache 1
10766
10767         # 6.1) linkea update on rename
10768         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10769
10770         # get parents by fid
10771         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10772         # foo1 should no longer be returned in parent list
10773         echo "$parent" | grep -F "$FID1" &&
10774                 error "$FID1 should no longer be in parent list"
10775         # the new path should appear
10776         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10777                 error "$FID2/$tfile.moved is not in parent list"
10778
10779         # 6.2) linkea update on unlink
10780         rm -f $DIR/$tdir/d/foo2/link
10781         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10782         # foo2/link should no longer be returned in parent list
10783         echo "$parent" | grep -F "$FID2/link" &&
10784                 error "$FID2/link should no longer be in parent list"
10785         true
10786
10787         rm -f $DIR/f
10788         restore_lustre_params < $save
10789         rm -f $save
10790 }
10791 run_test 154f "get parent fids by reading link ea"
10792
10793 test_154g()
10794 {
10795         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
10796            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
10797                 { skip "Need MDS version at least 2.6.92"; return 0; }
10798         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10799
10800         mkdir -p $DIR/$tdir
10801         llapi_fid_test -d $DIR/$tdir
10802 }
10803 run_test 154g "various llapi FID tests"
10804
10805 test_155_small_load() {
10806     local temp=$TMP/$tfile
10807     local file=$DIR/$tfile
10808
10809     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10810         error "dd of=$temp bs=6096 count=1 failed"
10811     cp $temp $file
10812     cancel_lru_locks $OSC
10813     cmp $temp $file || error "$temp $file differ"
10814
10815     $TRUNCATE $temp 6000
10816     $TRUNCATE $file 6000
10817     cmp $temp $file || error "$temp $file differ (truncate1)"
10818
10819     echo "12345" >>$temp
10820     echo "12345" >>$file
10821     cmp $temp $file || error "$temp $file differ (append1)"
10822
10823     echo "12345" >>$temp
10824     echo "12345" >>$file
10825     cmp $temp $file || error "$temp $file differ (append2)"
10826
10827     rm -f $temp $file
10828     true
10829 }
10830
10831 test_155_big_load() {
10832     remote_ost_nodsh && skip "remote OST with nodsh" && return
10833     local temp=$TMP/$tfile
10834     local file=$DIR/$tfile
10835
10836     free_min_max
10837     local cache_size=$(do_facet ost$((MAXI+1)) \
10838         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10839     local large_file_size=$((cache_size * 2))
10840
10841     echo "OSS cache size: $cache_size KB"
10842     echo "Large file size: $large_file_size KB"
10843
10844     [ $MAXV -le $large_file_size ] && \
10845         skip_env "max available OST size needs > $large_file_size KB" && \
10846         return 0
10847
10848     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10849
10850     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10851         error "dd of=$temp bs=$large_file_size count=1k failed"
10852     cp $temp $file
10853     ls -lh $temp $file
10854     cancel_lru_locks osc
10855     cmp $temp $file || error "$temp $file differ"
10856
10857     rm -f $temp $file
10858     true
10859 }
10860
10861 save_writethrough() {
10862         local facets=$(get_facets OST)
10863
10864         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
10865         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
10866 }
10867
10868 test_155a() {
10869         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10870         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10871         save_writethrough $p
10872
10873         set_cache read on
10874         set_cache writethrough on
10875         test_155_small_load
10876         restore_lustre_params < $p
10877         rm -f $p
10878 }
10879 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10880
10881 test_155b() {
10882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10883         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10884         save_writethrough $p
10885
10886         set_cache read on
10887         set_cache writethrough off
10888         test_155_small_load
10889         restore_lustre_params < $p
10890         rm -f $p
10891 }
10892 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10893
10894 test_155c() {
10895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10896         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10897         save_writethrough $p
10898
10899         set_cache read off
10900         set_cache writethrough on
10901         test_155_small_load
10902         restore_lustre_params < $p
10903         rm -f $p
10904 }
10905 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10906
10907 test_155d() {
10908         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10909         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10910         save_writethrough $p
10911
10912         set_cache read off
10913         set_cache writethrough off
10914         test_155_small_load
10915         restore_lustre_params < $p
10916         rm -f $p
10917 }
10918 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10919
10920 test_155e() {
10921         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10922         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10923         save_writethrough $p
10924
10925         set_cache read on
10926         set_cache writethrough on
10927         test_155_big_load
10928         restore_lustre_params < $p
10929         rm -f $p
10930 }
10931 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
10932
10933 test_155f() {
10934         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10935         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10936         save_writethrough $p
10937
10938         set_cache read on
10939         set_cache writethrough off
10940         test_155_big_load
10941         restore_lustre_params < $p
10942         rm -f $p
10943 }
10944 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
10945
10946 test_155g() {
10947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10948         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10949         save_writethrough $p
10950
10951         set_cache read off
10952         set_cache writethrough on
10953         test_155_big_load
10954         restore_lustre_params < $p
10955         rm -f $p
10956 }
10957 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
10958
10959 test_155h() {
10960         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10961         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10962         save_writethrough $p
10963
10964         set_cache read off
10965         set_cache writethrough off
10966         test_155_big_load
10967         restore_lustre_params < $p
10968         rm -f $p
10969 }
10970 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
10971
10972 test_156() {
10973         remote_ost_nodsh && skip "remote OST with nodsh" && return
10974         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10975         local CPAGES=3
10976         local BEFORE
10977         local AFTER
10978         local file="$DIR/$tfile"
10979         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10980
10981         [ "$(facet_fstype ost1)" = "zfs" -a \
10982            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
10983                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
10984                 return
10985
10986         save_writethrough $p
10987         roc_hit_init
10988
10989         log "Turn on read and write cache"
10990         set_cache read on
10991         set_cache writethrough on
10992
10993         log "Write data and read it back."
10994         log "Read should be satisfied from the cache."
10995         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
10996         BEFORE=$(roc_hit)
10997         cancel_lru_locks osc
10998         cat $file >/dev/null
10999         AFTER=$(roc_hit)
11000         if ! let "AFTER - BEFORE == CPAGES"; then
11001                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11002         else
11003                 log "cache hits:: before: $BEFORE, after: $AFTER"
11004         fi
11005
11006         log "Read again; it should be satisfied from the cache."
11007         BEFORE=$AFTER
11008         cancel_lru_locks osc
11009         cat $file >/dev/null
11010         AFTER=$(roc_hit)
11011         if ! let "AFTER - BEFORE == CPAGES"; then
11012                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11013         else
11014                 log "cache hits:: before: $BEFORE, after: $AFTER"
11015         fi
11016
11017         log "Turn off the read cache and turn on the write cache"
11018         set_cache read off
11019         set_cache writethrough on
11020
11021         log "Read again; it should be satisfied from the cache."
11022         BEFORE=$(roc_hit)
11023         cancel_lru_locks osc
11024         cat $file >/dev/null
11025         AFTER=$(roc_hit)
11026         if ! let "AFTER - BEFORE == CPAGES"; then
11027                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11028         else
11029                 log "cache hits:: before: $BEFORE, after: $AFTER"
11030         fi
11031
11032         log "Read again; it should not be satisfied from the cache."
11033         BEFORE=$AFTER
11034         cancel_lru_locks osc
11035         cat $file >/dev/null
11036         AFTER=$(roc_hit)
11037         if ! let "AFTER - BEFORE == 0"; then
11038                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11039         else
11040                 log "cache hits:: before: $BEFORE, after: $AFTER"
11041         fi
11042
11043         log "Write data and read it back."
11044         log "Read should be satisfied from the cache."
11045         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11046         BEFORE=$(roc_hit)
11047         cancel_lru_locks osc
11048         cat $file >/dev/null
11049         AFTER=$(roc_hit)
11050         if ! let "AFTER - BEFORE == CPAGES"; then
11051                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11052         else
11053                 log "cache hits:: before: $BEFORE, after: $AFTER"
11054         fi
11055
11056         log "Read again; it should not be satisfied from the cache."
11057         BEFORE=$AFTER
11058         cancel_lru_locks osc
11059         cat $file >/dev/null
11060         AFTER=$(roc_hit)
11061         if ! let "AFTER - BEFORE == 0"; then
11062                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11063         else
11064                 log "cache hits:: before: $BEFORE, after: $AFTER"
11065         fi
11066
11067         log "Turn off read and write cache"
11068         set_cache read off
11069         set_cache writethrough off
11070
11071         log "Write data and read it back"
11072         log "It should not be satisfied from the cache."
11073         rm -f $file
11074         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11075         cancel_lru_locks osc
11076         BEFORE=$(roc_hit)
11077         cat $file >/dev/null
11078         AFTER=$(roc_hit)
11079         if ! let "AFTER - BEFORE == 0"; then
11080                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11081         else
11082                 log "cache hits:: before: $BEFORE, after: $AFTER"
11083         fi
11084
11085         log "Turn on the read cache and turn off the write cache"
11086         set_cache read on
11087         set_cache writethrough off
11088
11089         log "Write data and read it back"
11090         log "It should not be satisfied from the cache."
11091         rm -f $file
11092         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11093         BEFORE=$(roc_hit)
11094         cancel_lru_locks osc
11095         cat $file >/dev/null
11096         AFTER=$(roc_hit)
11097         if ! let "AFTER - BEFORE == 0"; then
11098                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11099         else
11100                 log "cache hits:: before: $BEFORE, after: $AFTER"
11101         fi
11102
11103         log "Read again; it should be satisfied from the cache."
11104         BEFORE=$(roc_hit)
11105         cancel_lru_locks osc
11106         cat $file >/dev/null
11107         AFTER=$(roc_hit)
11108         if ! let "AFTER - BEFORE == CPAGES"; then
11109                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11110         else
11111                 log "cache hits:: before: $BEFORE, after: $AFTER"
11112         fi
11113
11114         rm -f $file
11115         restore_lustre_params < $p
11116         rm -f $p
11117 }
11118 run_test 156 "Verification of tunables"
11119
11120 #Changelogs
11121 cleanup_changelog () {
11122         trap 0
11123         echo "Deregistering changelog client $CL_USER"
11124         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11125 }
11126
11127 err17935 () {
11128         if [[ $MDSCOUNT -gt 1 ]]; then
11129                 error_ignore bz17935 $*
11130         else
11131                 error $*
11132         fi
11133 }
11134
11135 changelog_chmask()
11136 {
11137         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11138
11139         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
11140
11141         if [ $MASK -eq 1 ]; then
11142                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
11143         else
11144                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
11145         fi
11146 }
11147
11148 changelog_extract_field() {
11149         local mdt=$1
11150         local cltype=$2
11151         local file=$3
11152         local identifier=$4
11153
11154         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
11155                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
11156                 tail -1
11157 }
11158
11159 test_160a() {
11160         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11161         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11162         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11163                 { skip "Need MDS version at least 2.2.0"; return; }
11164
11165         local CL_USERS="mdd.$MDT0.changelog_users"
11166         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11167         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11168                 changelog_register -n)
11169         echo "Registered as changelog user $CL_USER"
11170         trap cleanup_changelog EXIT
11171         $GET_CL_USERS | grep -q $CL_USER ||
11172                 error "User $CL_USER not found in changelog_users"
11173
11174         # change something
11175         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11176         touch $DIR/$tdir/pics/2008/zachy/timestamp
11177         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11178         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11179         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11180         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11181         rm $DIR/$tdir/pics/desktop.jpg
11182
11183         $LFS changelog $MDT0 | tail -5
11184
11185         echo "verifying changelog mask"
11186         changelog_chmask "MKDIR"
11187         changelog_chmask "CLOSE"
11188
11189         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11190         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11191
11192         changelog_chmask "MKDIR"
11193         changelog_chmask "CLOSE"
11194
11195         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11196         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11197
11198         $LFS changelog $MDT0
11199         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11200         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11201         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11202         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11203
11204         # verify contents
11205         echo "verifying target fid"
11206         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11207         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11208         [ "$fidc" == "$fidf" ] ||
11209                 err17935 "fid in changelog $fidc != file fid $fidf"
11210         echo "verifying parent fid"
11211         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11212         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11213         [ "$fidc" == "$fidf" ] ||
11214                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11215
11216         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11217         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11218         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11219         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11220         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11221                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11222
11223         MIN_REC=$($GET_CL_USERS |
11224                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11225         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11226         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11227         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11228                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11229
11230         # LU-3446 changelog index reset on MDT restart
11231         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11232         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11233         $LFS changelog_clear $MDT0 $CL_USER 0
11234         stop $SINGLEMDS || error "Fail to stop MDT."
11235         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11236         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11237         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11238         [ $CUR_REC1 == $CUR_REC2 ] ||
11239                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11240
11241         echo "verifying user deregister"
11242         cleanup_changelog
11243         $GET_CL_USERS | grep -q $CL_USER &&
11244                 error "User $CL_USER still in changelog_users"
11245
11246         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11247         if [ $CL_USER -eq 0 ]; then
11248                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11249                 touch $DIR/$tdir/chloe
11250                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11251                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11252                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11253         else
11254                 echo "$CL_USER other changelog users; can't verify off"
11255         fi
11256 }
11257 run_test 160a "changelog sanity"
11258
11259 test_160b() { # LU-3587
11260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11261         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11262         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11263                 { skip "Need MDS version at least 2.2.0"; return; }
11264
11265         local CL_USERS="mdd.$MDT0.changelog_users"
11266         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11267         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11268                 changelog_register -n)
11269         echo "Registered as changelog user $CL_USER"
11270         trap cleanup_changelog EXIT
11271         $GET_CL_USERS | grep -q $CL_USER ||
11272                 error "User $CL_USER not found in changelog_users"
11273
11274         local LONGNAME1=$(str_repeat a 255)
11275         local LONGNAME2=$(str_repeat b 255)
11276
11277         cd $DIR
11278         echo "creating very long named file"
11279         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11280         echo "moving very long named file"
11281         mv $LONGNAME1 $LONGNAME2
11282
11283         $LFS changelog $MDT0 | grep RENME
11284         rm -f $LONGNAME2
11285         cleanup_changelog
11286 }
11287 run_test 160b "Verify that very long rename doesn't crash in changelog"
11288
11289 test_160c() {
11290         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11291
11292         local rc=0
11293         local server_version=$(lustre_version_code $SINGLEMDS)
11294
11295         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11296                 [[ $server_version -gt $(version_code 2.5.1) &&
11297                    $server_version -lt $(version_code 2.5.50) ]] ||
11298                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11300
11301         # Registration step
11302         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11303                 changelog_register -n)
11304         trap cleanup_changelog EXIT
11305
11306         rm -rf $DIR/$tdir
11307         mkdir -p $DIR/$tdir
11308         $MCREATE $DIR/$tdir/foo_160c
11309         changelog_chmask "TRUNC"
11310         $TRUNCATE $DIR/$tdir/foo_160c 200
11311         changelog_chmask "TRUNC"
11312         $TRUNCATE $DIR/$tdir/foo_160c 199
11313         $LFS changelog $MDT0
11314         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11315         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11316         $LFS changelog_clear $MDT0 $CL_USER 0
11317
11318         # Deregistration step
11319         cleanup_changelog
11320 }
11321 run_test 160c "verify that changelog log catch the truncate event"
11322
11323 test_160d() {
11324         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11325         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11326
11327         local server_version=$(lustre_version_code mds1)
11328         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11329
11330         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11331                 { skip "Need MDS version at least 2.7.60+"; return; }
11332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11333
11334         # Registration step
11335         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11336                 changelog_register -n)
11337
11338         trap cleanup_changelog EXIT
11339         mkdir -p $DIR/$tdir/migrate_dir
11340         $LFS changelog_clear $MDT0 $CL_USER 0
11341
11342         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11343         $LFS changelog $MDT0
11344         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11345         $LFS changelog_clear $MDT0 $CL_USER 0
11346         [ $MIGRATES -eq 1 ] ||
11347                 error "MIGRATE changelog mask count $MIGRATES != 1"
11348
11349         # Deregistration step
11350         cleanup_changelog
11351 }
11352 run_test 160d "verify that changelog log catch the migrate event"
11353
11354 test_160e() {
11355         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11356
11357         # Create a user
11358         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11359                 changelog_register -n)
11360         echo "Registered as changelog user $CL_USER"
11361         trap cleanup_changelog EXIT
11362
11363         # Delete a future user (expect fail)
11364         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11365         local rc=$?
11366
11367         if [ $rc -eq 0 ]; then
11368                 error "Deleted non-existant user cl77"
11369         elif [ $rc -ne 2 ]; then
11370                 error "changelog_deregister failed with $rc, " \
11371                         "expected 2 (ENOENT)"
11372         fi
11373
11374         # Clear to a bad index (1 billion should be safe)
11375         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11376         rc=$?
11377
11378         if [ $rc -eq 0 ]; then
11379                 error "Successfully cleared to invalid CL index"
11380         elif [ $rc -ne 22 ]; then
11381                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11382         fi
11383 }
11384 run_test 160e "changelog negative testing"
11385
11386 test_161a() {
11387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11388         test_mkdir -c1 $DIR/$tdir
11389         cp /etc/hosts $DIR/$tdir/$tfile
11390         test_mkdir -c1 $DIR/$tdir/foo1
11391         test_mkdir -c1 $DIR/$tdir/foo2
11392         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11393         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11394         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11395         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11396         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11397         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11398                 $LFS fid2path $DIR $FID
11399                 err17935 "bad link ea"
11400         fi
11401     # middle
11402     rm $DIR/$tdir/foo2/zachary
11403     # last
11404     rm $DIR/$tdir/foo2/thor
11405     # first
11406     rm $DIR/$tdir/$tfile
11407     # rename
11408     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11409     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11410         then
11411         $LFS fid2path $DIR $FID
11412         err17935 "bad link rename"
11413     fi
11414     rm $DIR/$tdir/foo2/maggie
11415
11416         # overflow the EA
11417         local longname=filename_avg_len_is_thirty_two_
11418         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11419                 error "failed to hardlink many files"
11420         links=$($LFS fid2path $DIR $FID | wc -l)
11421         echo -n "${links}/1000 links in link EA"
11422         [[ $links -gt 60 ]] ||
11423                 err17935 "expected at least 60 links in link EA"
11424         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11425                 error "failed to unlink many hardlinks"
11426 }
11427 run_test 161a "link ea sanity"
11428
11429 test_161b() {
11430         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11431         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11432         local MDTIDX=1
11433         local remote_dir=$DIR/$tdir/remote_dir
11434
11435         mkdir -p $DIR/$tdir
11436         $LFS mkdir -i $MDTIDX $remote_dir ||
11437                 error "create remote directory failed"
11438
11439         cp /etc/hosts $remote_dir/$tfile
11440         mkdir -p $remote_dir/foo1
11441         mkdir -p $remote_dir/foo2
11442         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11443         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11444         ln $remote_dir/$tfile $remote_dir/foo1/luna
11445         ln $remote_dir/$tfile $remote_dir/foo2/thor
11446
11447         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11448                      tr -d ']')
11449         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11450                 $LFS fid2path $DIR $FID
11451                 err17935 "bad link ea"
11452         fi
11453         # middle
11454         rm $remote_dir/foo2/zachary
11455         # last
11456         rm $remote_dir/foo2/thor
11457         # first
11458         rm $remote_dir/$tfile
11459         # rename
11460         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11461         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11462         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11463                 $LFS fid2path $DIR $FID
11464                 err17935 "bad link rename"
11465         fi
11466         rm $remote_dir/foo2/maggie
11467
11468         # overflow the EA
11469         local longname=filename_avg_len_is_thirty_two_
11470         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11471                 error "failed to hardlink many files"
11472         links=$($LFS fid2path $DIR $FID | wc -l)
11473         echo -n "${links}/1000 links in link EA"
11474         [[ ${links} -gt 60 ]] ||
11475                 err17935 "expected at least 60 links in link EA"
11476         unlinkmany $remote_dir/foo2/$longname 1000 ||
11477         error "failed to unlink many hardlinks"
11478 }
11479 run_test 161b "link ea sanity under remote directory"
11480
11481 test_161c() {
11482         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11484         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
11485                 skip "Need MDS version at least 2.1.5" && return
11486
11487         # define CLF_RENAME_LAST 0x0001
11488         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
11489         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11490                 changelog_register -n)
11491
11492         trap cleanup_changelog EXIT
11493         rm -rf $DIR/$tdir
11494         mkdir -p $DIR/$tdir
11495         touch $DIR/$tdir/foo_161c
11496         touch $DIR/$tdir/bar_161c
11497         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11498         $LFS changelog $MDT0 | grep RENME
11499         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
11500                 cut -f5 -d' ')
11501         $LFS changelog_clear $MDT0 $CL_USER 0
11502         if [ x$flags != "x0x1" ]; then
11503                 error "flag $flags is not 0x1"
11504         fi
11505         echo "rename overwrite a target having nlink = 1," \
11506                 "changelog record has flags of $flags"
11507
11508         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
11509         touch $DIR/$tdir/foo_161c
11510         touch $DIR/$tdir/bar_161c
11511         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11512         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11513         $LFS changelog $MDT0 | grep RENME
11514         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11515         $LFS changelog_clear $MDT0 $CL_USER 0
11516         if [ x$flags != "x0x0" ]; then
11517                 error "flag $flags is not 0x0"
11518         fi
11519         echo "rename overwrite a target having nlink > 1," \
11520                 "changelog record has flags of $flags"
11521
11522         # rename doesn't overwrite a target (changelog flag 0x0)
11523         touch $DIR/$tdir/foo_161c
11524         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
11525         $LFS changelog $MDT0 | grep RENME
11526         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11527         $LFS changelog_clear $MDT0 $CL_USER 0
11528         if [ x$flags != "x0x0" ]; then
11529                 error "flag $flags is not 0x0"
11530         fi
11531         echo "rename doesn't overwrite a target," \
11532                 "changelog record has flags of $flags"
11533
11534         # define CLF_UNLINK_LAST 0x0001
11535         # unlink a file having nlink = 1 (changelog flag 0x1)
11536         rm -f $DIR/$tdir/foo2_161c
11537         $LFS changelog $MDT0 | grep UNLNK
11538         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11539         $LFS changelog_clear $MDT0 $CL_USER 0
11540         if [ x$flags != "x0x1" ]; then
11541                 error "flag $flags is not 0x1"
11542         fi
11543         echo "unlink a file having nlink = 1," \
11544                 "changelog record has flags of $flags"
11545
11546         # unlink a file having nlink > 1 (changelog flag 0x0)
11547         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11548         rm -f $DIR/$tdir/foobar_161c
11549         $LFS changelog $MDT0 | grep UNLNK
11550         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11551         $LFS changelog_clear $MDT0 $CL_USER 0
11552         if [ x$flags != "x0x0" ]; then
11553                 error "flag $flags is not 0x0"
11554         fi
11555         echo "unlink a file having nlink > 1," \
11556                 "changelog record has flags of $flags"
11557         cleanup_changelog
11558 }
11559 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
11560
11561 test_161d() {
11562         local user
11563         local pid
11564         local fid
11565
11566         # cleanup previous run
11567         rm -rf $DIR/$tdir/$tfile
11568
11569         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11570                 changelog_register -n)
11571         [[ $? -eq 0 ]] || error "changelog_register failed"
11572
11573         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
11574         # interfer with $MOUNT/.lustre/fid/ access
11575         mkdir $DIR/$tdir
11576         [[ $? -eq 0 ]] || error "mkdir failed"
11577
11578         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
11579         $LCTL set_param fail_loc=0x8000140c
11580         # 5s pause
11581         $LCTL set_param fail_val=5
11582
11583         # create file
11584         echo foofoo > $DIR/$tdir/$tfile &
11585         pid=$!
11586
11587         # wait for create to be delayed
11588         sleep 2
11589
11590         ps -p $pid
11591         [[ $? -eq 0 ]] || error "create should be blocked"
11592
11593         local tempfile=$(mktemp)
11594         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
11595         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
11596         # some delay may occur during ChangeLog publishing and file read just
11597         # above, that could allow file write to happen finally
11598         [[ -s $tempfile ]] && echo "file should be empty"
11599
11600         $LCTL set_param fail_loc=0
11601
11602         wait $pid
11603         [[ $? -eq 0 ]] || error "create failed"
11604
11605         $LFS changelog_clear $MDT0 $user 0
11606         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
11607 }
11608 run_test 161d "create with concurrent .lustre/fid access"
11609
11610 check_path() {
11611     local expected=$1
11612     shift
11613     local fid=$2
11614
11615     local path=$(${LFS} fid2path $*)
11616     # Remove the '//' indicating a remote directory
11617     path=$(echo $path | sed 's#//#/#g')
11618     RC=$?
11619
11620     if [ $RC -ne 0 ]; then
11621         err17935 "path looked up of $expected failed. Error $RC"
11622         return $RC
11623     elif [ "${path}" != "${expected}" ]; then
11624         err17935 "path looked up \"${path}\" instead of \"${expected}\""
11625         return 2
11626     fi
11627     echo "fid $fid resolves to path $path (expected $expected)"
11628 }
11629
11630 test_162a() { # was test_162
11631         # Make changes to filesystem
11632         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11633         test_mkdir -p -c1 $DIR/$tdir/d2
11634         touch $DIR/$tdir/d2/$tfile
11635         touch $DIR/$tdir/d2/x1
11636         touch $DIR/$tdir/d2/x2
11637         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
11638         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
11639         # regular file
11640         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
11641         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
11642                 error "check path $tdir/d2/$tfile failed"
11643
11644         # softlink
11645         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
11646         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
11647         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
11648                 error "check path $tdir/d2/p/q/r/slink failed"
11649
11650         # softlink to wrong file
11651         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
11652         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
11653         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
11654                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
11655
11656         # hardlink
11657         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
11658         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
11659         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
11660         # fid2path dir/fsname should both work
11661         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
11662                 error "check path $tdir/d2/a/b/c/new_file failed"
11663         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
11664                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
11665
11666         # hardlink count: check that there are 2 links
11667         # Doesnt work with CMD yet: 17935
11668         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
11669                 err17935 "expected 2 links"
11670
11671         # hardlink indexing: remove the first link
11672         rm $DIR/$tdir/d2/p/q/r/hlink
11673         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
11674                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
11675
11676         return 0
11677 }
11678 run_test 162a "path lookup sanity"
11679
11680 test_162b() {
11681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11682         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11683
11684         mkdir $DIR/$tdir
11685         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
11686                                 error "create striped dir failed"
11687
11688         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
11689                                         tail -n 1 | awk '{print $2}')
11690         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
11691
11692         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
11693         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
11694
11695         # regular file
11696         for ((i=0;i<5;i++)); do
11697                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
11698                         error "get fid for f$i failed"
11699                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
11700                         error "check path $tdir/striped_dir/f$i failed"
11701
11702                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
11703                         error "get fid for d$i failed"
11704                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
11705                         error "check path $tdir/striped_dir/d$i failed"
11706         done
11707
11708         return 0
11709 }
11710 run_test 162b "striped directory path lookup sanity"
11711
11712 # LU-4239: Verify fid2path works with paths 100 or more directories deep
11713 test_162c() {
11714         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
11715                 skip "Need MDS version at least 2.7.51" && return
11716         test_mkdir $DIR/$tdir.local
11717         test_mkdir $DIR/$tdir.remote
11718         local lpath=$tdir.local
11719         local rpath=$tdir.remote
11720
11721         for ((i = 0; i <= 101; i++)); do
11722                 lpath="$lpath/$i"
11723                 mkdir $DIR/$lpath
11724                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
11725                         error "get fid for local directory $DIR/$lpath failed"
11726                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
11727                         error "check path for local directory $DIR/$lpath failed"
11728
11729                 rpath="$rpath/$i"
11730                 test_mkdir $DIR/$rpath
11731                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
11732                         error "get fid for remote directory $DIR/$rpath failed"
11733                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
11734                         error "check path for remote directory $DIR/$rpath failed"
11735         done
11736
11737         return 0
11738 }
11739 run_test 162c "fid2path works with paths 100 or more directories deep"
11740
11741 test_169() {
11742         # do directio so as not to populate the page cache
11743         log "creating a 10 Mb file"
11744         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11745         log "starting reads"
11746         dd if=$DIR/$tfile of=/dev/null bs=4096 &
11747         log "truncating the file"
11748         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11749         log "killing dd"
11750         kill %+ || true # reads might have finished
11751         echo "wait until dd is finished"
11752         wait
11753         log "removing the temporary file"
11754         rm -rf $DIR/$tfile || error "tmp file removal failed"
11755 }
11756 run_test 169 "parallel read and truncate should not deadlock"
11757
11758 test_170() {
11759         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11760         $LCTL clear     # bug 18514
11761         $LCTL debug_daemon start $TMP/${tfile}_log_good
11762         touch $DIR/$tfile
11763         $LCTL debug_daemon stop
11764         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
11765                error "sed failed to read log_good"
11766
11767         $LCTL debug_daemon start $TMP/${tfile}_log_good
11768         rm -rf $DIR/$tfile
11769         $LCTL debug_daemon stop
11770
11771         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
11772                error "lctl df log_bad failed"
11773
11774         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11775         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11776
11777         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
11778         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
11779
11780         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
11781                 error "bad_line good_line1 good_line2 are empty"
11782
11783         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11784         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
11785         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11786
11787         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
11788         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11789         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11790
11791         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
11792                 error "bad_line_new good_line_new are empty"
11793
11794         local expected_good=$((good_line1 + good_line2*2))
11795
11796         rm -f $TMP/${tfile}*
11797         # LU-231, short malformed line may not be counted into bad lines
11798         if [ $bad_line -ne $bad_line_new ] &&
11799                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
11800                 error "expected $bad_line bad lines, but got $bad_line_new"
11801                 return 1
11802         fi
11803
11804         if [ $expected_good -ne $good_line_new ]; then
11805                 error "expected $expected_good good lines, but got $good_line_new"
11806                 return 2
11807         fi
11808         true
11809 }
11810 run_test 170 "test lctl df to handle corrupted log ====================="
11811
11812 test_171() { # bug20592
11813         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11814 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11815         $LCTL set_param fail_loc=0x50e
11816         $LCTL set_param fail_val=3000
11817         multiop_bg_pause $DIR/$tfile O_s || true
11818         local MULTIPID=$!
11819         kill -USR1 $MULTIPID
11820         # cause log dump
11821         sleep 3
11822         wait $MULTIPID
11823         if dmesg | grep "recursive fault"; then
11824                 error "caught a recursive fault"
11825         fi
11826         $LCTL set_param fail_loc=0
11827         true
11828 }
11829 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11830
11831 # it would be good to share it with obdfilter-survey/iokit-libecho code
11832 setup_obdecho_osc () {
11833         local rc=0
11834         local ost_nid=$1
11835         local obdfilter_name=$2
11836         echo "Creating new osc for $obdfilter_name on $ost_nid"
11837         # make sure we can find loopback nid
11838         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11839
11840         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11841                            ${obdfilter_name}_osc_UUID || rc=2; }
11842         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11843                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11844         return $rc
11845 }
11846
11847 cleanup_obdecho_osc () {
11848         local obdfilter_name=$1
11849         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11850         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11851         return 0
11852 }
11853
11854 obdecho_test() {
11855         local OBD=$1
11856         local node=$2
11857         local pages=${3:-64}
11858         local rc=0
11859         local id
11860
11861         local count=10
11862         local obd_size=$(get_obd_size $node $OBD)
11863         local page_size=$(get_page_size $node)
11864         if [[ -n "$obd_size" ]]; then
11865                 local new_count=$((obd_size / (pages * page_size / 1024)))
11866                 [[ $new_count -ge $count ]] || count=$new_count
11867         fi
11868
11869         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11870         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11871                            rc=2; }
11872         if [ $rc -eq 0 ]; then
11873             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11874             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11875         fi
11876         echo "New object id is $id"
11877         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11878                            rc=4; }
11879         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11880                            "test_brw $count w v $pages $id" || rc=4; }
11881         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11882                            rc=4; }
11883         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11884                                         "cleanup" || rc=5; }
11885         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11886                                         "detach" || rc=6; }
11887         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11888         return $rc
11889 }
11890
11891 test_180a() {
11892         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11893         remote_ost_nodsh && skip "remote OST with nodsh" && return
11894         local rc=0
11895         local rmmod_local=0
11896
11897         if ! module_loaded obdecho; then
11898             load_module obdecho/obdecho
11899             rmmod_local=1
11900         fi
11901
11902         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11903         local host=$(lctl get_param -n osc.$osc.import |
11904                              awk '/current_connection:/ {print $2}' )
11905         local target=$(lctl get_param -n osc.$osc.import |
11906                              awk '/target:/ {print $2}' )
11907         target=${target%_UUID}
11908
11909         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11910         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11911         [[ -n $target ]] && cleanup_obdecho_osc $target
11912         [ $rmmod_local -eq 1 ] && rmmod obdecho
11913         return $rc
11914 }
11915 run_test 180a "test obdecho on osc"
11916
11917 test_180b() {
11918         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11919         remote_ost_nodsh && skip "remote OST with nodsh" && return
11920         local rc=0
11921         local rmmod_remote=0
11922
11923         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11924                 rmmod_remote=true || error "failed to load module obdecho"
11925         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
11926         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
11927         $rmmod_remote && do_facet ost1 "rmmod obdecho"
11928         return $rc
11929 }
11930 run_test 180b "test obdecho directly on obdfilter"
11931
11932 test_180c() { # LU-2598
11933         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11934         remote_ost_nodsh && skip "remote OST with nodsh" && return
11935         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
11936                 skip "Need MDS version at least 2.4.0" && return
11937
11938         local rc=0
11939         local rmmod_remote=false
11940         local pages=16384 # 64MB bulk I/O RPC size
11941         local target
11942
11943         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
11944                 rmmod_remote=true || error "failed to load module obdecho"
11945
11946         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
11947                 head -n1)
11948         if [ -n "$target" ]; then
11949                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
11950         else
11951                 echo "there is no obdfilter target on ost1"
11952                 rc=2
11953         fi
11954         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
11955         return $rc
11956 }
11957 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
11958
11959 test_181() { # bug 22177
11960         test_mkdir $DIR/$tdir
11961         # create enough files to index the directory
11962         createmany -o $DIR/$tdir/foobar 4000
11963         # print attributes for debug purpose
11964         lsattr -d .
11965         # open dir
11966         multiop_bg_pause $DIR/$tdir D_Sc || return 1
11967         MULTIPID=$!
11968         # remove the files & current working dir
11969         unlinkmany $DIR/$tdir/foobar 4000
11970         rmdir $DIR/$tdir
11971         kill -USR1 $MULTIPID
11972         wait $MULTIPID
11973         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
11974         return 0
11975 }
11976 run_test 181 "Test open-unlinked dir ========================"
11977
11978 test_182() {
11979         local fcount=1000
11980         local tcount=10
11981
11982         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
11983
11984         $LCTL set_param mdc.*.rpc_stats=clear
11985
11986         for (( i = 0; i < $tcount; i++ )) ; do
11987                 mkdir $DIR/$tdir/$i
11988         done
11989
11990         for (( i = 0; i < $tcount; i++ )) ; do
11991                 createmany -o $DIR/$tdir/$i/f- $fcount &
11992         done
11993         wait
11994
11995         for (( i = 0; i < $tcount; i++ )) ; do
11996                 unlinkmany $DIR/$tdir/$i/f- $fcount &
11997         done
11998         wait
11999
12000         $LCTL get_param mdc.*.rpc_stats
12001
12002         rm -rf $DIR/$tdir
12003 }
12004 run_test 182 "Test parallel modify metadata operations ================"
12005
12006 test_183() { # LU-2275
12007         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12008         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
12009                 skip "Need MDS version at least 2.3.56" && return
12010
12011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12012         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12013         echo aaa > $DIR/$tdir/$tfile
12014
12015 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
12016         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
12017
12018         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
12019         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
12020
12021         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
12022
12023         # Flush negative dentry cache
12024         touch $DIR/$tdir/$tfile
12025
12026         # We are not checking for any leaked references here, they'll
12027         # become evident next time we do cleanup with module unload.
12028         rm -rf $DIR/$tdir
12029 }
12030 run_test 183 "No crash or request leak in case of strange dispositions ========"
12031
12032 # test suite 184 is for LU-2016, LU-2017
12033 test_184a() {
12034         check_swap_layouts_support && return 0
12035
12036         dir0=$DIR/$tdir/$testnum
12037         test_mkdir -p -c1 $dir0
12038         ref1=/etc/passwd
12039         ref2=/etc/group
12040         file1=$dir0/f1
12041         file2=$dir0/f2
12042         $SETSTRIPE -c1 $file1
12043         cp $ref1 $file1
12044         $SETSTRIPE -c2 $file2
12045         cp $ref2 $file2
12046         gen1=$($GETSTRIPE -g $file1)
12047         gen2=$($GETSTRIPE -g $file2)
12048
12049         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
12050         gen=$($GETSTRIPE -g $file1)
12051         [[ $gen1 != $gen ]] ||
12052                 "Layout generation on $file1 does not change"
12053         gen=$($GETSTRIPE -g $file2)
12054         [[ $gen2 != $gen ]] ||
12055                 "Layout generation on $file2 does not change"
12056
12057         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
12058         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12059 }
12060 run_test 184a "Basic layout swap"
12061
12062 test_184b() {
12063         check_swap_layouts_support && return 0
12064
12065         dir0=$DIR/$tdir/$testnum
12066         mkdir -p $dir0 || error "creating dir $dir0"
12067         file1=$dir0/f1
12068         file2=$dir0/f2
12069         file3=$dir0/f3
12070         dir1=$dir0/d1
12071         dir2=$dir0/d2
12072         mkdir $dir1 $dir2
12073         $SETSTRIPE -c1 $file1
12074         $SETSTRIPE -c2 $file2
12075         $SETSTRIPE -c1 $file3
12076         chown $RUNAS_ID $file3
12077         gen1=$($GETSTRIPE -g $file1)
12078         gen2=$($GETSTRIPE -g $file2)
12079
12080         $LFS swap_layouts $dir1 $dir2 &&
12081                 error "swap of directories layouts should fail"
12082         $LFS swap_layouts $dir1 $file1 &&
12083                 error "swap of directory and file layouts should fail"
12084         $RUNAS $LFS swap_layouts $file1 $file2 &&
12085                 error "swap of file we cannot write should fail"
12086         $LFS swap_layouts $file1 $file3 &&
12087                 error "swap of file with different owner should fail"
12088         /bin/true # to clear error code
12089 }
12090 run_test 184b "Forbidden layout swap (will generate errors)"
12091
12092 test_184c() {
12093         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
12094         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
12095         check_swap_layouts_support && return 0
12096
12097         local dir0=$DIR/$tdir/$testnum
12098         mkdir -p $dir0 || error "creating dir $dir0"
12099
12100         local ref1=$dir0/ref1
12101         local ref2=$dir0/ref2
12102         local file1=$dir0/file1
12103         local file2=$dir0/file2
12104         # create a file large enough for the concurrent test
12105         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
12106         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
12107         echo "ref file size: ref1($(stat -c %s $ref1))," \
12108              "ref2($(stat -c %s $ref2))"
12109
12110         cp $ref2 $file2
12111         dd if=$ref1 of=$file1 bs=16k &
12112         local DD_PID=$!
12113
12114         # Make sure dd starts to copy file
12115         while [ ! -f $file1 ]; do sleep 0.1; done
12116
12117         $LFS swap_layouts $file1 $file2
12118         local rc=$?
12119         wait $DD_PID
12120         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
12121         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
12122
12123         # how many bytes copied before swapping layout
12124         local copied=$(stat -c %s $file2)
12125         local remaining=$(stat -c %s $ref1)
12126         remaining=$((remaining - copied))
12127         echo "Copied $copied bytes before swapping layout..."
12128
12129         cmp -n $copied $file1 $ref2 | grep differ &&
12130                 error "Content mismatch [0, $copied) of ref2 and file1"
12131         cmp -n $copied $file2 $ref1 ||
12132                 error "Content mismatch [0, $copied) of ref1 and file2"
12133         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
12134                 error "Content mismatch [$copied, EOF) of ref1 and file1"
12135
12136         # clean up
12137         rm -f $ref1 $ref2 $file1 $file2
12138 }
12139 run_test 184c "Concurrent write and layout swap"
12140
12141 test_184d() {
12142         check_swap_layouts_support && return 0
12143         [ -z "$(which getfattr 2>/dev/null)" ] &&
12144                 skip "no getfattr command" && return 0
12145
12146         local file1=$DIR/$tdir/$tfile-1
12147         local file2=$DIR/$tdir/$tfile-2
12148         local file3=$DIR/$tdir/$tfile-3
12149         local lovea1
12150         local lovea2
12151
12152         mkdir -p $DIR/$tdir
12153         touch $file1 || error "create $file1 failed"
12154         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12155                 error "create $file2 failed"
12156         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12157                 error "create $file3 failed"
12158         lovea1=$(get_layout_param $file1)
12159
12160         $LFS swap_layouts $file2 $file3 ||
12161                 error "swap $file2 $file3 layouts failed"
12162         $LFS swap_layouts $file1 $file2 ||
12163                 error "swap $file1 $file2 layouts failed"
12164
12165         lovea2=$(get_layout_param $file2)
12166         echo "$lovea1"
12167         echo "$lovea2"
12168         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12169
12170         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12171         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12172 }
12173 run_test 184d "allow stripeless layouts swap"
12174
12175 test_184e() {
12176         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12177                 { skip "Need MDS version at least 2.6.94"; return 0; }
12178         check_swap_layouts_support && return 0
12179         [ -z "$(which getfattr 2>/dev/null)" ] &&
12180                 skip "no getfattr command" && return 0
12181
12182         local file1=$DIR/$tdir/$tfile-1
12183         local file2=$DIR/$tdir/$tfile-2
12184         local file3=$DIR/$tdir/$tfile-3
12185         local lovea
12186
12187         mkdir -p $DIR/$tdir
12188         touch $file1 || error "create $file1 failed"
12189         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12190                 error "create $file2 failed"
12191         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12192                 error "create $file3 failed"
12193
12194         $LFS swap_layouts $file1 $file2 ||
12195                 error "swap $file1 $file2 layouts failed"
12196
12197         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12198         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12199
12200         echo 123 > $file1 || error "Should be able to write into $file1"
12201
12202         $LFS swap_layouts $file1 $file3 ||
12203                 error "swap $file1 $file3 layouts failed"
12204
12205         echo 123 > $file1 || error "Should be able to write into $file1"
12206
12207         rm -rf $file1 $file2 $file3
12208 }
12209 run_test 184e "Recreate layout after stripeless layout swaps"
12210
12211 test_185() { # LU-2441
12212         # LU-3553 - no volatile file support in old servers
12213         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12214                 { skip "Need MDS version at least 2.3.60"; return 0; }
12215
12216         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12217         touch $DIR/$tdir/spoo
12218         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12219         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12220                 error "cannot create/write a volatile file"
12221         [ "$FILESET" == "" ] &&
12222         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12223                 error "FID is still valid after close"
12224
12225         multiop_bg_pause $DIR/$tdir vVw4096_c
12226         local multi_pid=$!
12227
12228         local OLD_IFS=$IFS
12229         IFS=":"
12230         local fidv=($fid)
12231         IFS=$OLD_IFS
12232         # assume that the next FID for this client is sequential, since stdout
12233         # is unfortunately eaten by multiop_bg_pause
12234         local n=$((${fidv[1]} + 1))
12235         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12236         if [ "$FILESET" == "" ]; then
12237                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12238                         error "FID is missing before close"
12239         fi
12240         kill -USR1 $multi_pid
12241         # 1 second delay, so if mtime change we will see it
12242         sleep 1
12243         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12244         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12245 }
12246 run_test 185 "Volatile file support"
12247
12248 test_187a() {
12249         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12250         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12251                 skip "Need MDS version at least 2.3.0" && return
12252
12253         local dir0=$DIR/$tdir/$testnum
12254         mkdir -p $dir0 || error "creating dir $dir0"
12255
12256         local file=$dir0/file1
12257         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12258         local dv1=$($LFS data_version $file)
12259         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12260         local dv2=$($LFS data_version $file)
12261         [[ $dv1 != $dv2 ]] ||
12262                 error "data version did not change on write $dv1 == $dv2"
12263
12264         # clean up
12265         rm -f $file1
12266 }
12267 run_test 187a "Test data version change"
12268
12269 test_187b() {
12270         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12271         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12272                 skip "Need MDS version at least 2.3.0" && return
12273
12274         local dir0=$DIR/$tdir/$testnum
12275         mkdir -p $dir0 || error "creating dir $dir0"
12276
12277         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12278         [[ ${DV[0]} != ${DV[1]} ]] ||
12279                 error "data version did not change on write"\
12280                       " ${DV[0]} == ${DV[1]}"
12281
12282         # clean up
12283         rm -f $file1
12284 }
12285 run_test 187b "Test data version change on volatile file"
12286
12287 test_200() {
12288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12289         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12290         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12291
12292         local POOL=${POOL:-cea1}
12293         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12294         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12295         # Pool OST targets
12296         local first_ost=0
12297         local last_ost=$(($OSTCOUNT - 1))
12298         local ost_step=2
12299         local ost_list=$(seq $first_ost $ost_step $last_ost)
12300         local ost_range="$first_ost $last_ost $ost_step"
12301         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12302         local file_dir=$POOL_ROOT/file_tst
12303         local subdir=$test_path/subdir
12304         local rc=0
12305
12306         if ! combined_mgs_mds ; then
12307                 mount_mgs_client
12308         fi
12309
12310         while : ; do
12311                 # former test_200a test_200b
12312                 pool_add $POOL                          || { rc=$? ; break; }
12313                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12314                 # former test_200c test_200d
12315                 mkdir -p $test_path
12316                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12317                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12318                 mkdir -p $subdir
12319                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12320                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12321                                                         || { rc=$? ; break; }
12322                 # former test_200e test_200f
12323                 local files=$((OSTCOUNT*3))
12324                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12325                                                         || { rc=$? ; break; }
12326                 pool_create_files $POOL $file_dir $files "$ost_list" \
12327                                                         || { rc=$? ; break; }
12328                 # former test_200g test_200h
12329                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12330                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12331
12332                 # former test_201a test_201b test_201c
12333                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12334
12335                 local f=$test_path/$tfile
12336                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12337                 pool_remove $POOL $f                    || { rc=$? ; break; }
12338                 break
12339         done
12340
12341         destroy_test_pools
12342
12343         if ! combined_mgs_mds ; then
12344                 umount_mgs_client
12345         fi
12346         return $rc
12347 }
12348 run_test 200 "OST pools"
12349
12350 # usage: default_attr <count | size | offset>
12351 default_attr() {
12352         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12353 }
12354
12355 # usage: check_default_stripe_attr
12356 check_default_stripe_attr() {
12357         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12358         case $1 in
12359         --stripe-count|-c)
12360                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12361         --stripe-size|-S)
12362                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12363         --stripe-index|-i)
12364                 EXPECTED=-1;;
12365         *)
12366                 error "unknown getstripe attr '$1'"
12367         esac
12368
12369         [ $ACTUAL == $EXPECTED ] ||
12370                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12371 }
12372
12373 test_204a() {
12374         test_mkdir $DIR/$tdir
12375         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12376
12377         check_default_stripe_attr --stripe-count
12378         check_default_stripe_attr --stripe-size
12379         check_default_stripe_attr --stripe-index
12380 }
12381 run_test 204a "Print default stripe attributes"
12382
12383 test_204b() {
12384         test_mkdir $DIR/$tdir
12385         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12386
12387         check_default_stripe_attr --stripe-size
12388         check_default_stripe_attr --stripe-index
12389 }
12390 run_test 204b "Print default stripe size and offset"
12391
12392 test_204c() {
12393         test_mkdir $DIR/$tdir
12394         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12395
12396         check_default_stripe_attr --stripe-count
12397         check_default_stripe_attr --stripe-index
12398 }
12399 run_test 204c "Print default stripe count and offset"
12400
12401 test_204d() {
12402         test_mkdir $DIR/$tdir
12403         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12404
12405         check_default_stripe_attr --stripe-count
12406         check_default_stripe_attr --stripe-size
12407 }
12408 run_test 204d "Print default stripe count and size"
12409
12410 test_204e() {
12411         test_mkdir $DIR/$tdir
12412         $SETSTRIPE -d $DIR/$tdir
12413
12414         check_default_stripe_attr --stripe-count --raw
12415         check_default_stripe_attr --stripe-size --raw
12416         check_default_stripe_attr --stripe-index --raw
12417 }
12418 run_test 204e "Print raw stripe attributes"
12419
12420 test_204f() {
12421         test_mkdir $DIR/$tdir
12422         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12423
12424         check_default_stripe_attr --stripe-size --raw
12425         check_default_stripe_attr --stripe-index --raw
12426 }
12427 run_test 204f "Print raw stripe size and offset"
12428
12429 test_204g() {
12430         test_mkdir $DIR/$tdir
12431         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12432
12433         check_default_stripe_attr --stripe-count --raw
12434         check_default_stripe_attr --stripe-index --raw
12435 }
12436 run_test 204g "Print raw stripe count and offset"
12437
12438 test_204h() {
12439         test_mkdir $DIR/$tdir
12440         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12441
12442         check_default_stripe_attr --stripe-count --raw
12443         check_default_stripe_attr --stripe-size --raw
12444 }
12445 run_test 204h "Print raw stripe count and size"
12446
12447 # Figure out which job scheduler is being used, if any,
12448 # or use a fake one
12449 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12450         JOBENV=SLURM_JOB_ID
12451 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12452         JOBENV=LSB_JOBID
12453 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12454         JOBENV=PBS_JOBID
12455 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12456         JOBENV=LOADL_STEP_ID
12457 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12458         JOBENV=JOB_ID
12459 else
12460         $LCTL list_param jobid_name > /dev/null 2>&1
12461         if [ $? -eq 0 ]; then
12462                 JOBENV=nodelocal
12463         else
12464                 JOBENV=FAKE_JOBID
12465         fi
12466 fi
12467
12468 verify_jobstats() {
12469         local cmd=($1)
12470         shift
12471         local facets="$@"
12472
12473 # we don't really need to clear the stats for this test to work, since each
12474 # command has a unique jobid, but it makes debugging easier if needed.
12475 #       for facet in $facets; do
12476 #               local dev=$(convert_facet2label $facet)
12477 #               # clear old jobstats
12478 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
12479 #       done
12480
12481         # use a new JobID for each test, or we might see an old one
12482         [ "$JOBENV" = "FAKE_JOBID" ] &&
12483                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12484
12485         JOBVAL=${!JOBENV}
12486
12487         [ "$JOBENV" = "nodelocal" ] && {
12488                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12489                 $LCTL set_param jobid_name=$FAKE_JOBID
12490                 JOBVAL=$FAKE_JOBID
12491         }
12492
12493         log "Test: ${cmd[*]}"
12494         log "Using JobID environment variable $JOBENV=$JOBVAL"
12495
12496         if [ $JOBENV = "FAKE_JOBID" ]; then
12497                 FAKE_JOBID=$JOBVAL ${cmd[*]}
12498         else
12499                 ${cmd[*]}
12500         fi
12501
12502         # all files are created on OST0000
12503         for facet in $facets; do
12504                 local stats="*.$(convert_facet2label $facet).job_stats"
12505                 if [ $(do_facet $facet lctl get_param $stats |
12506                        grep -c $JOBVAL) -ne 1 ]; then
12507                         do_facet $facet lctl get_param $stats
12508                         error "No jobstats for $JOBVAL found on $facet::$stats"
12509                 fi
12510         done
12511 }
12512
12513 jobstats_set() {
12514         trap 0
12515         NEW_JOBENV=${1:-$OLD_JOBENV}
12516         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
12517         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
12518 }
12519
12520 cleanup_205() {
12521         trap 0
12522         do_facet $SINGLEMDS \
12523                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
12524         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
12525         cleanup_changelog
12526 }
12527
12528 test_205() { # Job stats
12529         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
12530                 { skip "Need MDS version with at least 2.7.1"; return 0; }
12531
12532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12533         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12534         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12535         remote_ost_nodsh && skip "remote OST with nodsh" && return
12536
12537         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
12538                 skip "Server doesn't support jobstats" && return 0
12539         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
12540
12541         OLD_JOBENV=$($LCTL get_param -n jobid_var)
12542         if [ $OLD_JOBENV != $JOBENV ]; then
12543                 jobstats_set $JOBENV
12544                 trap cleanup_205 EXIT
12545         fi
12546
12547         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
12548         echo "Registered as changelog user $CL_USER"
12549
12550         OLD_INTERVAL=$(do_facet $SINGLEMDS \
12551                        lctl get_param -n mdt.*.job_cleanup_interval)
12552         local interval_new=5
12553         do_facet $SINGLEMDS \
12554                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
12555         local start=$SECONDS
12556
12557         local cmd
12558         # mkdir
12559         cmd="mkdir $DIR/$tdir"
12560         verify_jobstats "$cmd" "$SINGLEMDS"
12561         # rmdir
12562         cmd="rmdir $DIR/$tdir"
12563         verify_jobstats "$cmd" "$SINGLEMDS"
12564         # mkdir on secondary MDT
12565         if [ $MDSCOUNT -gt 1 ]; then
12566                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
12567                 verify_jobstats "$cmd" "mds2"
12568         fi
12569         # mknod
12570         cmd="mknod $DIR/$tfile c 1 3"
12571         verify_jobstats "$cmd" "$SINGLEMDS"
12572         # unlink
12573         cmd="rm -f $DIR/$tfile"
12574         verify_jobstats "$cmd" "$SINGLEMDS"
12575         # create all files on OST0000 so verify_jobstats can find OST stats
12576         # open & close
12577         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
12578         verify_jobstats "$cmd" "$SINGLEMDS"
12579         # setattr
12580         cmd="touch $DIR/$tfile"
12581         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12582         # write
12583         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
12584         verify_jobstats "$cmd" "ost1"
12585         # read
12586         cancel_lru_locks osc
12587         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
12588         verify_jobstats "$cmd" "ost1"
12589         # truncate
12590         cmd="$TRUNCATE $DIR/$tfile 0"
12591         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12592         # rename
12593         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
12594         verify_jobstats "$cmd" "$SINGLEMDS"
12595         # jobstats expiry - sleep until old stats should be expired
12596         local left=$((interval_new + 5 - (SECONDS - start)))
12597         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
12598                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
12599                         "0" $left
12600         cmd="mkdir $DIR/$tdir.expire"
12601         verify_jobstats "$cmd" "$SINGLEMDS"
12602         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
12603             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
12604
12605         # Ensure that jobid are present in changelog (if supported by MDS)
12606         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
12607         then
12608                 $LFS changelog $MDT0 | tail -9
12609                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
12610                 [ $jobids -eq 9 ] ||
12611                         error "Wrong changelog jobid count $jobids != 9"
12612
12613                 # LU-5862
12614                 JOBENV="disable"
12615                 jobstats_set $JOBENV
12616                 touch $DIR/$tfile
12617                 $LFS changelog $MDT0 | tail -1
12618                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
12619                 [ $jobids -eq 0 ] ||
12620                         error "Unexpected jobids when jobid_var=$JOBENV"
12621         fi
12622
12623         cleanup_205
12624 }
12625 run_test 205 "Verify job stats"
12626
12627 # LU-1480, LU-1773 and LU-1657
12628 test_206() {
12629         mkdir -p $DIR/$tdir
12630         $SETSTRIPE -c -1 $DIR/$tdir
12631 #define OBD_FAIL_LOV_INIT 0x1403
12632         $LCTL set_param fail_loc=0xa0001403
12633         $LCTL set_param fail_val=1
12634         touch $DIR/$tdir/$tfile || true
12635 }
12636 run_test 206 "fail lov_init_raid0() doesn't lbug"
12637
12638 test_207a() {
12639         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12640         local fsz=`stat -c %s $DIR/$tfile`
12641         cancel_lru_locks mdc
12642
12643         # do not return layout in getattr intent
12644 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
12645         $LCTL set_param fail_loc=0x170
12646         local sz=`stat -c %s $DIR/$tfile`
12647
12648         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
12649
12650         rm -rf $DIR/$tfile
12651 }
12652 run_test 207a "can refresh layout at glimpse"
12653
12654 test_207b() {
12655         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12656         local cksum=`md5sum $DIR/$tfile`
12657         local fsz=`stat -c %s $DIR/$tfile`
12658         cancel_lru_locks mdc
12659         cancel_lru_locks osc
12660
12661         # do not return layout in getattr intent
12662 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
12663         $LCTL set_param fail_loc=0x171
12664
12665         # it will refresh layout after the file is opened but before read issues
12666         echo checksum is "$cksum"
12667         echo "$cksum" |md5sum -c --quiet || error "file differs"
12668
12669         rm -rf $DIR/$tfile
12670 }
12671 run_test 207b "can refresh layout at open"
12672
12673 test_208() {
12674         # FIXME: in this test suite, only RD lease is used. This is okay
12675         # for now as only exclusive open is supported. After generic lease
12676         # is done, this test suite should be revised. - Jinshan
12677
12678         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12679         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
12680                 { skip "Need MDS version at least 2.4.52"; return 0; }
12681
12682         echo "==== test 1: verify get lease work"
12683         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
12684
12685         echo "==== test 2: verify lease can be broken by upcoming open"
12686         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12687         local PID=$!
12688         sleep 1
12689
12690         $MULTIOP $DIR/$tfile oO_RDONLY:c
12691         kill -USR1 $PID && wait $PID || error "break lease error"
12692
12693         echo "==== test 3: verify lease can't be granted if an open already exists"
12694         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
12695         local PID=$!
12696         sleep 1
12697
12698         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
12699         kill -USR1 $PID && wait $PID || error "open file error"
12700
12701         echo "==== test 4: lease can sustain over recovery"
12702         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
12703         PID=$!
12704         sleep 1
12705
12706         fail mds1
12707
12708         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
12709
12710         echo "==== test 5: lease broken can't be regained by replay"
12711         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12712         PID=$!
12713         sleep 1
12714
12715         # open file to break lease and then recovery
12716         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
12717         fail mds1
12718
12719         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
12720
12721         rm -f $DIR/$tfile
12722 }
12723 run_test 208 "Exclusive open"
12724
12725 test_209() {
12726         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
12727                 skip_env "must have disp_stripe" && return
12728
12729         touch $DIR/$tfile
12730         sync; sleep 5; sync;
12731
12732         echo 3 > /proc/sys/vm/drop_caches
12733         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12734
12735         # open/close 500 times
12736         for i in $(seq 500); do
12737                 cat $DIR/$tfile
12738         done
12739
12740         echo 3 > /proc/sys/vm/drop_caches
12741         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12742
12743         echo "before: $req_before, after: $req_after"
12744         [ $((req_after - req_before)) -ge 300 ] &&
12745                 error "open/close requests are not freed"
12746         return 0
12747 }
12748 run_test 209 "read-only open/close requests should be freed promptly"
12749
12750 test_212() {
12751         size=`date +%s`
12752         size=$((size % 8192 + 1))
12753         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
12754         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
12755         rm -f $DIR/f212 $DIR/f212.xyz
12756 }
12757 run_test 212 "Sendfile test ============================================"
12758
12759 test_213() {
12760         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
12761         cancel_lru_locks osc
12762         lctl set_param fail_loc=0x8000040f
12763         # generate a read lock
12764         cat $DIR/$tfile > /dev/null
12765         # write to the file, it will try to cancel the above read lock.
12766         cat /etc/hosts >> $DIR/$tfile
12767 }
12768 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
12769
12770 test_214() { # for bug 20133
12771         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
12772         for (( i=0; i < 340; i++ )) ; do
12773                 touch $DIR/$tdir/d214c/a$i
12774         done
12775
12776         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
12777         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
12778         ls $DIR/d214c || error "ls $DIR/d214c failed"
12779         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
12780         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
12781 }
12782 run_test 214 "hash-indexed directory test - bug 20133"
12783
12784 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
12785 create_lnet_proc_files() {
12786         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
12787 }
12788
12789 # counterpart of create_lnet_proc_files
12790 remove_lnet_proc_files() {
12791         rm -f $TMP/lnet_$1.sys
12792 }
12793
12794 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12795 # 3rd arg as regexp for body
12796 check_lnet_proc_stats() {
12797         local l=$(cat "$TMP/lnet_$1" |wc -l)
12798         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
12799
12800         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
12801 }
12802
12803 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12804 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
12805 # optional and can be regexp for 2nd line (lnet.routes case)
12806 check_lnet_proc_entry() {
12807         local blp=2          # blp stands for 'position of 1st line of body'
12808         [ -z "$5" ] || blp=3 # lnet.routes case
12809
12810         local l=$(cat "$TMP/lnet_$1" |wc -l)
12811         # subtracting one from $blp because the body can be empty
12812         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12813
12814         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12815                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12816
12817         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12818                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12819
12820         # bail out if any unexpected line happened
12821         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12822         [ "$?" != 0 ] || error "$2 misformatted"
12823 }
12824
12825 test_215() { # for bugs 18102, 21079, 21517
12826         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12827         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12828         local P='[1-9][0-9]*'           # positive numeric
12829         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12830         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12831         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12832         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12833
12834         local L1 # regexp for 1st line
12835         local L2 # regexp for 2nd line (optional)
12836         local BR # regexp for the rest (body)
12837
12838         # lnet.stats should look as 11 space-separated non-negative numerics
12839         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12840         create_lnet_proc_files "stats"
12841         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12842         remove_lnet_proc_files "stats"
12843
12844         # lnet.routes should look like this:
12845         # Routing disabled/enabled
12846         # net hops priority state router
12847         # where net is a string like tcp0, hops > 0, priority >= 0,
12848         # state is up/down,
12849         # router is a string like 192.168.1.1@tcp2
12850         L1="^Routing (disabled|enabled)$"
12851         L2="^net +hops +priority +state +router$"
12852         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12853         create_lnet_proc_files "routes"
12854         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12855         remove_lnet_proc_files "routes"
12856
12857         # lnet.routers should look like this:
12858         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12859         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12860         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12861         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12862         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12863         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12864         create_lnet_proc_files "routers"
12865         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12866         remove_lnet_proc_files "routers"
12867
12868         # lnet.peers should look like this:
12869         # nid refs state last max rtr min tx min queue
12870         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12871         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12872         # numeric (0 or >0 or <0), queue >= 0.
12873         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12874         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12875         create_lnet_proc_files "peers"
12876         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12877         remove_lnet_proc_files "peers"
12878
12879         # lnet.buffers  should look like this:
12880         # pages count credits min
12881         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12882         L1="^pages +count +credits +min$"
12883         BR="^ +$N +$N +$I +$I$"
12884         create_lnet_proc_files "buffers"
12885         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12886         remove_lnet_proc_files "buffers"
12887
12888         # lnet.nis should look like this:
12889         # nid status alive refs peer rtr max tx min
12890         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12891         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12892         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12893         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12894         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12895         create_lnet_proc_files "nis"
12896         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12897         remove_lnet_proc_files "nis"
12898
12899         # can we successfully write to lnet.stats?
12900         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12901 }
12902 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
12903
12904 test_216() { # bug 20317
12905         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12906         remote_ost_nodsh && skip "remote OST with nodsh" && return
12907
12908         local node
12909         local facets=$(get_facets OST)
12910         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
12911
12912         save_lustre_params client "osc.*.contention_seconds" > $p
12913         save_lustre_params $facets \
12914                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
12915         save_lustre_params $facets \
12916                 "ldlm.namespaces.filter-*.contended_locks" >> $p
12917         save_lustre_params $facets \
12918                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
12919         clear_stats osc.*.osc_stats
12920
12921         # agressive lockless i/o settings
12922         do_nodes $(comma_list $(osts_nodes)) \
12923                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
12924                         ldlm.namespaces.filter-*.contended_locks=0 \
12925                         ldlm.namespaces.filter-*.contention_seconds=60"
12926         lctl set_param -n osc.*.contention_seconds=60
12927
12928         $DIRECTIO write $DIR/$tfile 0 10 4096
12929         $CHECKSTAT -s 40960 $DIR/$tfile
12930
12931         # disable lockless i/o
12932         do_nodes $(comma_list $(osts_nodes)) \
12933                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
12934                         ldlm.namespaces.filter-*.contended_locks=32 \
12935                         ldlm.namespaces.filter-*.contention_seconds=0"
12936         lctl set_param -n osc.*.contention_seconds=0
12937         clear_stats osc.*.osc_stats
12938
12939         dd if=/dev/zero of=$DIR/$tfile count=0
12940         $CHECKSTAT -s 0 $DIR/$tfile
12941
12942         restore_lustre_params <$p
12943         rm -f $p
12944         rm $DIR/$tfile
12945 }
12946 run_test 216 "check lockless direct write updates file size and kms correctly"
12947
12948 test_217() { # bug 22430
12949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12950         local node
12951         local nid
12952
12953         for node in $(nodes_list); do
12954                 nid=$(host_nids_address $node $NETTYPE)
12955                 if [[ $nid = *-* ]] ; then
12956                         echo "lctl ping $(h2nettype $nid)"
12957                         lctl ping $(h2nettype $nid)
12958                 else
12959                         echo "skipping $node (no hyphen detected)"
12960                 fi
12961         done
12962 }
12963 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
12964
12965 test_218() {
12966        # do directio so as not to populate the page cache
12967        log "creating a 10 Mb file"
12968        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12969        log "starting reads"
12970        dd if=$DIR/$tfile of=/dev/null bs=4096 &
12971        log "truncating the file"
12972        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12973        log "killing dd"
12974        kill %+ || true # reads might have finished
12975        echo "wait until dd is finished"
12976        wait
12977        log "removing the temporary file"
12978        rm -rf $DIR/$tfile || error "tmp file removal failed"
12979 }
12980 run_test 218 "parallel read and truncate should not deadlock ======================="
12981
12982 test_219() {
12983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12984         # write one partial page
12985         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
12986         # set no grant so vvp_io_commit_write will do sync write
12987         $LCTL set_param fail_loc=0x411
12988         # write a full page at the end of file
12989         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
12990
12991         $LCTL set_param fail_loc=0
12992         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
12993         $LCTL set_param fail_loc=0x411
12994         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
12995
12996         # LU-4201
12997         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
12998         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
12999 }
13000 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
13001
13002 test_220() { #LU-325
13003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13004         remote_ost_nodsh && skip "remote OST with nodsh" && return
13005         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13006         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13007         local OSTIDX=0
13008
13009         # create on MDT0000 so the last_id and next_id are correct
13010         mkdir $DIR/$tdir
13011         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
13012         OST=${OST%_UUID}
13013
13014         # on the mdt's osc
13015         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
13016         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
13017                         osc.$mdtosc_proc1.prealloc_last_id)
13018         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
13019                         osc.$mdtosc_proc1.prealloc_next_id)
13020
13021         $LFS df -i
13022
13023         if ! combined_mgs_mds ; then
13024                 mount_mgs_client
13025         fi
13026
13027         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
13028         #define OBD_FAIL_OST_ENOINO              0x229
13029         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
13030         create_pool $FSNAME.$TESTNAME || return 1
13031         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
13032
13033         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
13034
13035         MDSOBJS=$((last_id - next_id))
13036         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
13037
13038         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
13039         echo "OST still has $count kbytes free"
13040
13041         echo "create $MDSOBJS files @next_id..."
13042         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
13043
13044         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13045                         osc.$mdtosc_proc1.prealloc_last_id)
13046         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13047                         osc.$mdtosc_proc1.prealloc_next_id)
13048
13049         echo "after creation, last_id=$last_id2, next_id=$next_id2"
13050         $LFS df -i
13051
13052         echo "cleanup..."
13053
13054         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
13055         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
13056
13057         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
13058                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
13059         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
13060                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
13061         echo "unlink $MDSOBJS files @$next_id..."
13062         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
13063
13064         if ! combined_mgs_mds ; then
13065                 umount_mgs_client
13066         fi
13067 }
13068 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
13069
13070 test_221() {
13071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13072         dd if=`which date` of=$MOUNT/date oflag=sync
13073         chmod +x $MOUNT/date
13074
13075         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
13076         $LCTL set_param fail_loc=0x80001401
13077
13078         $MOUNT/date > /dev/null
13079         rm -f $MOUNT/date
13080 }
13081 run_test 221 "make sure fault and truncate race to not cause OOM"
13082
13083 test_222a () {
13084         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13085         rm -rf $DIR/$tdir
13086         test_mkdir $DIR/$tdir
13087         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13088         createmany -o $DIR/$tdir/$tfile 10
13089         cancel_lru_locks mdc
13090         cancel_lru_locks osc
13091         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13092         $LCTL set_param fail_loc=0x31a
13093         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
13094         $LCTL set_param fail_loc=0
13095         rm -r $DIR/$tdir
13096 }
13097 run_test 222a "AGL for ls should not trigger CLIO lock failure"
13098
13099 test_222b () {
13100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13101         rm -rf $DIR/$tdir
13102         test_mkdir $DIR/$tdir
13103         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13104         createmany -o $DIR/$tdir/$tfile 10
13105         cancel_lru_locks mdc
13106         cancel_lru_locks osc
13107         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13108         $LCTL set_param fail_loc=0x31a
13109         rm -r $DIR/$tdir || error "AGL for rmdir failed"
13110         $LCTL set_param fail_loc=0
13111 }
13112 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
13113
13114 test_223 () {
13115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13116         rm -rf $DIR/$tdir
13117         test_mkdir $DIR/$tdir
13118         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13119         createmany -o $DIR/$tdir/$tfile 10
13120         cancel_lru_locks mdc
13121         cancel_lru_locks osc
13122         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
13123         $LCTL set_param fail_loc=0x31b
13124         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
13125         $LCTL set_param fail_loc=0
13126         rm -r $DIR/$tdir
13127 }
13128 run_test 223 "osc reenqueue if without AGL lock granted ======================="
13129
13130 test_224a() { # LU-1039, MRP-303
13131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13132         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
13133         $LCTL set_param fail_loc=0x508
13134         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
13135         $LCTL set_param fail_loc=0
13136         df $DIR
13137 }
13138 run_test 224a "Don't panic on bulk IO failure"
13139
13140 test_224b() { # LU-1039, MRP-303
13141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13142         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
13143         cancel_lru_locks osc
13144         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
13145         $LCTL set_param fail_loc=0x515
13146         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
13147         $LCTL set_param fail_loc=0
13148         df $DIR
13149 }
13150 run_test 224b "Don't panic on bulk IO failure"
13151
13152 test_224c() { # LU-6441
13153         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13154         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13155
13156         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13157         save_writethrough $p
13158         set_cache writethrough on
13159
13160         local pages_per_rpc=$($LCTL get_param \
13161                                 osc.*.max_pages_per_rpc)
13162         local at_max=$($LCTL get_param -n at_max)
13163         local timeout=$($LCTL get_param -n timeout)
13164         local test_at="$LCTL get_param -n at_max"
13165         local param_at="$FSNAME.sys.at_max"
13166         local test_timeout="$LCTL get_param -n timeout"
13167         local param_timeout="$FSNAME.sys.timeout"
13168
13169         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
13170
13171         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13172                 error "conf_param at_max=0 failed"
13173         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13174                 error "conf_param timeout=5 failed"
13175
13176         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13177         do_facet ost1 $LCTL set_param fail_loc=0x520
13178         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13179         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13180         sync
13181         do_facet ost1 $LCTL set_param fail_loc=0
13182
13183         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13184                 error "conf_param at_max=$at_max failed"
13185         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13186                 $timeout || error "conf_param timeout=$timeout failed"
13187
13188         $LCTL set_param -n $pages_per_rpc
13189         restore_lustre_params < $p
13190         rm -f $p
13191 }
13192 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13193
13194 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13195 test_225a () {
13196         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13198         if [ -z ${MDSSURVEY} ]; then
13199               skip_env "mds-survey not found" && return
13200         fi
13201
13202         [ $MDSCOUNT -ge 2 ] &&
13203                 skip "skipping now for more than one MDT" && return
13204
13205        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13206             { skip "Need MDS version at least 2.2.51"; return; }
13207
13208        local mds=$(facet_host $SINGLEMDS)
13209        local target=$(do_nodes $mds 'lctl dl' | \
13210                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13211
13212        local cmd1="file_count=1000 thrhi=4"
13213        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13214        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13215        local cmd="$cmd1 $cmd2 $cmd3"
13216
13217        rm -f ${TMP}/mds_survey*
13218        echo + $cmd
13219        eval $cmd || error "mds-survey with zero-stripe failed"
13220        cat ${TMP}/mds_survey*
13221        rm -f ${TMP}/mds_survey*
13222 }
13223 run_test 225a "Metadata survey sanity with zero-stripe"
13224
13225 test_225b () {
13226         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13227         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13228         if [ -z ${MDSSURVEY} ]; then
13229               skip_env "mds-survey not found" && return
13230         fi
13231         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13232             { skip "Need MDS version at least 2.2.51"; return; }
13233
13234         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13235               skip_env "Need to mount OST to test" && return
13236         fi
13237
13238        local mds=$(facet_host $SINGLEMDS)
13239        local target=$(do_nodes $mds 'lctl dl' | \
13240                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13241
13242        local cmd1="file_count=1000 thrhi=4"
13243        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13244        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13245        local cmd="$cmd1 $cmd2 $cmd3"
13246
13247        rm -f ${TMP}/mds_survey*
13248        echo + $cmd
13249        eval $cmd || error "mds-survey with stripe_count failed"
13250        cat ${TMP}/mds_survey*
13251        rm -f ${TMP}/mds_survey*
13252 }
13253 run_test 225b "Metadata survey sanity with stripe_count = 1"
13254
13255 mcreate_path2fid () {
13256         local mode=$1
13257         local major=$2
13258         local minor=$3
13259         local name=$4
13260         local desc=$5
13261         local path=$DIR/$tdir/$name
13262         local fid
13263         local rc
13264         local fid_path
13265
13266         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13267                 error "cannot create $desc"
13268
13269         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13270         rc=$?
13271         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13272
13273         fid_path=$($LFS fid2path $MOUNT $fid)
13274         rc=$?
13275         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13276
13277         [ "$path" == "$fid_path" ] ||
13278                 error "fid2path returned $fid_path, expected $path"
13279
13280         echo "pass with $path and $fid"
13281 }
13282
13283 test_226a () {
13284         rm -rf $DIR/$tdir
13285         mkdir -p $DIR/$tdir
13286
13287         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13288         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13289         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13290         mcreate_path2fid 0040666 0 0 dir "directory"
13291         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13292         mcreate_path2fid 0100666 0 0 file "regular file"
13293         mcreate_path2fid 0120666 0 0 link "symbolic link"
13294         mcreate_path2fid 0140666 0 0 sock "socket"
13295 }
13296 run_test 226a "call path2fid and fid2path on files of all type"
13297
13298 test_226b () {
13299         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13300         rm -rf $DIR/$tdir
13301         local MDTIDX=1
13302
13303         mkdir -p $DIR/$tdir
13304         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13305                 error "create remote directory failed"
13306         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13307         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13308                                 "character special file (null)"
13309         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13310                                 "character special file (no device)"
13311         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13312         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13313                                 "block special file (loop)"
13314         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13315         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13316         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13317 }
13318 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13319
13320 # LU-1299 Executing or running ldd on a truncated executable does not
13321 # cause an out-of-memory condition.
13322 test_227() {
13323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13324         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13325         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13326         chmod +x $MOUNT/date
13327
13328         $MOUNT/date > /dev/null
13329         ldd $MOUNT/date > /dev/null
13330         rm -f $MOUNT/date
13331 }
13332 run_test 227 "running truncated executable does not cause OOM"
13333
13334 # LU-1512 try to reuse idle OI blocks
13335 test_228a() {
13336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13337         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13338         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13339                 skip "ldiskfs only test" && return
13340
13341         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13342         local myDIR=$DIR/$tdir
13343
13344         mkdir -p $myDIR
13345         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13346         $LCTL set_param fail_loc=0x80001002
13347         createmany -o $myDIR/t- 10000
13348         $LCTL set_param fail_loc=0
13349         # The guard is current the largest FID holder
13350         touch $myDIR/guard
13351         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13352                     tr -d '[')
13353         local IDX=$(($SEQ % 64))
13354
13355         do_facet $SINGLEMDS sync
13356         # Make sure journal flushed.
13357         sleep 6
13358         local blk1=$(do_facet $SINGLEMDS \
13359                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13360                      grep Blockcount | awk '{print $4}')
13361
13362         # Remove old files, some OI blocks will become idle.
13363         unlinkmany $myDIR/t- 10000
13364         # Create new files, idle OI blocks should be reused.
13365         createmany -o $myDIR/t- 2000
13366         do_facet $SINGLEMDS sync
13367         # Make sure journal flushed.
13368         sleep 6
13369         local blk2=$(do_facet $SINGLEMDS \
13370                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13371                      grep Blockcount | awk '{print $4}')
13372
13373         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13374 }
13375 run_test 228a "try to reuse idle OI blocks"
13376
13377 test_228b() {
13378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13379         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13380         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13381                 skip "ldiskfs only test" && return
13382
13383         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13384         local myDIR=$DIR/$tdir
13385
13386         mkdir -p $myDIR
13387         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13388         $LCTL set_param fail_loc=0x80001002
13389         createmany -o $myDIR/t- 10000
13390         $LCTL set_param fail_loc=0
13391         # The guard is current the largest FID holder
13392         touch $myDIR/guard
13393         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13394                     tr -d '[')
13395         local IDX=$(($SEQ % 64))
13396
13397         do_facet $SINGLEMDS sync
13398         # Make sure journal flushed.
13399         sleep 6
13400         local blk1=$(do_facet $SINGLEMDS \
13401                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13402                      grep Blockcount | awk '{print $4}')
13403
13404         # Remove old files, some OI blocks will become idle.
13405         unlinkmany $myDIR/t- 10000
13406
13407         # stop the MDT
13408         stop $SINGLEMDS || error "Fail to stop MDT."
13409         # remount the MDT
13410         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13411
13412         df $MOUNT || error "Fail to df."
13413         # Create new files, idle OI blocks should be reused.
13414         createmany -o $myDIR/t- 2000
13415         do_facet $SINGLEMDS sync
13416         # Make sure journal flushed.
13417         sleep 6
13418         local blk2=$(do_facet $SINGLEMDS \
13419                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13420                      grep Blockcount | awk '{print $4}')
13421
13422         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13423 }
13424 run_test 228b "idle OI blocks can be reused after MDT restart"
13425
13426 #LU-1881
13427 test_228c() {
13428         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13429         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13430         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13431                 skip "ldiskfs only test" && return
13432
13433         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13434         local myDIR=$DIR/$tdir
13435
13436         mkdir -p $myDIR
13437         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13438         $LCTL set_param fail_loc=0x80001002
13439         # 20000 files can guarantee there are index nodes in the OI file
13440         createmany -o $myDIR/t- 20000
13441         $LCTL set_param fail_loc=0
13442         # The guard is current the largest FID holder
13443         touch $myDIR/guard
13444         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13445                     tr -d '[')
13446         local IDX=$(($SEQ % 64))
13447
13448         do_facet $SINGLEMDS sync
13449         # Make sure journal flushed.
13450         sleep 6
13451         local blk1=$(do_facet $SINGLEMDS \
13452                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13453                      grep Blockcount | awk '{print $4}')
13454
13455         # Remove old files, some OI blocks will become idle.
13456         unlinkmany $myDIR/t- 20000
13457         rm -f $myDIR/guard
13458         # The OI file should become empty now
13459
13460         # Create new files, idle OI blocks should be reused.
13461         createmany -o $myDIR/t- 2000
13462         do_facet $SINGLEMDS sync
13463         # Make sure journal flushed.
13464         sleep 6
13465         local blk2=$(do_facet $SINGLEMDS \
13466                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13467                      grep Blockcount | awk '{print $4}')
13468
13469         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13470 }
13471 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13472
13473 test_229() { # LU-2482, LU-3448
13474         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13475                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13476                 return
13477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13478         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
13479
13480         rm -f $DIR/$tfile
13481
13482         # Create a file with a released layout and stripe count 2.
13483         $MULTIOP $DIR/$tfile H2c ||
13484                 error "failed to create file with released layout"
13485
13486         $GETSTRIPE -v $DIR/$tfile
13487
13488         local pattern=$($GETSTRIPE -L $DIR/$tfile)
13489         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
13490
13491         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
13492         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
13493         stat $DIR/$tfile || error "failed to stat released file"
13494
13495         chown $RUNAS_ID $DIR/$tfile ||
13496                 error "chown $RUNAS_ID $DIR/$tfile failed"
13497
13498         chgrp $RUNAS_ID $DIR/$tfile ||
13499                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
13500
13501         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
13502         rm $DIR/$tfile || error "failed to remove released file"
13503 }
13504 run_test 229 "getstripe/stat/rm/attr changes work on released files"
13505
13506 test_230a() {
13507         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13508         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13509         local MDTIDX=1
13510
13511         test_mkdir $DIR/$tdir
13512         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
13513         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
13514         [ $mdt_idx -ne 0 ] &&
13515                 error "create local directory on wrong MDT $mdt_idx"
13516
13517         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
13518                         error "create remote directory failed"
13519         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
13520         [ $mdt_idx -ne $MDTIDX ] &&
13521                 error "create remote directory on wrong MDT $mdt_idx"
13522
13523         createmany -o $DIR/$tdir/test_230/t- 10 ||
13524                 error "create files on remote directory failed"
13525         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
13526         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
13527         rm -r $DIR/$tdir || error "unlink remote directory failed"
13528 }
13529 run_test 230a "Create remote directory and files under the remote directory"
13530
13531 test_230b() {
13532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13533         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13534         local MDTIDX=1
13535         local mdt_index
13536         local i
13537         local file
13538         local pid
13539         local stripe_count
13540         local migrate_dir=$DIR/$tdir/migrate_dir
13541         local other_dir=$DIR/$tdir/other_dir
13542
13543         test_mkdir $DIR/$tdir
13544         test_mkdir -i0 -c1 $migrate_dir
13545         test_mkdir -i0 -c1 $other_dir
13546         for ((i=0; i<10; i++)); do
13547                 mkdir -p $migrate_dir/dir_${i}
13548                 createmany -o $migrate_dir/dir_${i}/f 10 ||
13549                         error "create files under remote dir failed $i"
13550         done
13551
13552         cp /etc/passwd $migrate_dir/$tfile
13553         cp /etc/passwd $other_dir/$tfile
13554         chattr +SAD $migrate_dir
13555         chattr +SAD $migrate_dir/$tfile
13556
13557         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13558         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13559         local old_dir_mode=$(stat -c%f $migrate_dir)
13560         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
13561
13562         mkdir -p $migrate_dir/dir_default_stripe2
13563         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
13564         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
13565
13566         mkdir -p $other_dir
13567         ln $migrate_dir/$tfile $other_dir/luna
13568         ln $migrate_dir/$tfile $migrate_dir/sofia
13569         ln $other_dir/$tfile $migrate_dir/david
13570         ln -s $migrate_dir/$tfile $other_dir/zachary
13571         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
13572         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
13573
13574         $LFS migrate -m $MDTIDX $migrate_dir ||
13575                 error "fails on migrating remote dir to MDT1"
13576
13577         echo "migratate to MDT1, then checking.."
13578         for ((i = 0; i < 10; i++)); do
13579                 for file in $(find $migrate_dir/dir_${i}); do
13580                         mdt_index=$($LFS getstripe -M $file)
13581                         [ $mdt_index == $MDTIDX ] ||
13582                                 error "$file is not on MDT${MDTIDX}"
13583                 done
13584         done
13585
13586         # the multiple link file should still in MDT0
13587         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
13588         [ $mdt_index == 0 ] ||
13589                 error "$file is not on MDT${MDTIDX}"
13590
13591         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13592         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13593                 error " expect $old_dir_flag get $new_dir_flag"
13594
13595         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13596         [ "$old_file_flag" = "$new_file_flag" ] ||
13597                 error " expect $old_file_flag get $new_file_flag"
13598
13599         local new_dir_mode=$(stat -c%f $migrate_dir)
13600         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13601                 error "expect mode $old_dir_mode get $new_dir_mode"
13602
13603         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13604         [ "$old_file_mode" = "$new_file_mode" ] ||
13605                 error "expect mode $old_file_mode get $new_file_mode"
13606
13607         diff /etc/passwd $migrate_dir/$tfile ||
13608                 error "$tfile different after migration"
13609
13610         diff /etc/passwd $other_dir/luna ||
13611                 error "luna different after migration"
13612
13613         diff /etc/passwd $migrate_dir/sofia ||
13614                 error "sofia different after migration"
13615
13616         diff /etc/passwd $migrate_dir/david ||
13617                 error "david different after migration"
13618
13619         diff /etc/passwd $other_dir/zachary ||
13620                 error "zachary different after migration"
13621
13622         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13623                 error "${tfile}_ln different after migration"
13624
13625         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13626                 error "${tfile}_ln_other different after migration"
13627
13628         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
13629         [ $stripe_count = 2 ] ||
13630                 error "dir strpe_count $d != 2 after migration."
13631
13632         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
13633         [ $stripe_count = 2 ] ||
13634                 error "file strpe_count $d != 2 after migration."
13635
13636         #migrate back to MDT0
13637         MDTIDX=0
13638
13639         $LFS migrate -m $MDTIDX $migrate_dir ||
13640                 error "fails on migrating remote dir to MDT0"
13641
13642         echo "migrate back to MDT0, checking.."
13643         for file in $(find $migrate_dir); do
13644                 mdt_index=$($LFS getstripe -M $file)
13645                 [ $mdt_index == $MDTIDX ] ||
13646                         error "$file is not on MDT${MDTIDX}"
13647         done
13648
13649         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13650         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13651                 error " expect $old_dir_flag get $new_dir_flag"
13652
13653         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13654         [ "$old_file_flag" = "$new_file_flag" ] ||
13655                 error " expect $old_file_flag get $new_file_flag"
13656
13657         local new_dir_mode=$(stat -c%f $migrate_dir)
13658         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13659                 error "expect mode $old_dir_mode get $new_dir_mode"
13660
13661         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13662         [ "$old_file_mode" = "$new_file_mode" ] ||
13663                 error "expect mode $old_file_mode get $new_file_mode"
13664
13665         diff /etc/passwd ${migrate_dir}/$tfile ||
13666                 error "$tfile different after migration"
13667
13668         diff /etc/passwd ${other_dir}/luna ||
13669                 error "luna different after migration"
13670
13671         diff /etc/passwd ${migrate_dir}/sofia ||
13672                 error "sofia different after migration"
13673
13674         diff /etc/passwd ${other_dir}/zachary ||
13675                 error "zachary different after migration"
13676
13677         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13678                 error "${tfile}_ln different after migration"
13679
13680         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13681                 error "${tfile}_ln_other different after migration"
13682
13683         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
13684         [ $stripe_count = 2 ] ||
13685                 error "dir strpe_count $d != 2 after migration."
13686
13687         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
13688         [ $stripe_count = 2 ] ||
13689                 error "file strpe_count $d != 2 after migration."
13690
13691         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13692 }
13693 run_test 230b "migrate directory"
13694
13695 test_230c() {
13696         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13697         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13698         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13699         local MDTIDX=1
13700         local mdt_index
13701         local file
13702         local migrate_dir=$DIR/$tdir/migrate_dir
13703
13704         #If migrating directory fails in the middle, all entries of
13705         #the directory is still accessiable.
13706         test_mkdir $DIR/$tdir
13707         test_mkdir -i0 -c1 $migrate_dir
13708         stat $migrate_dir
13709         createmany -o $migrate_dir/f 10 ||
13710                 error "create files under ${migrate_dir} failed"
13711
13712         #failed after migrating 5 entries
13713         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
13714         do_facet mds1 lctl set_param fail_loc=0x20001801
13715         do_facet mds1 lctl  set_param fail_val=5
13716         local t=$(ls $migrate_dir | wc -l)
13717         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
13718                 error "migrate should fail after 5 entries"
13719
13720         mkdir $migrate_dir/dir &&
13721                 error "mkdir succeeds under migrating directory"
13722         touch $migrate_dir/file &&
13723                 error "touch file succeeds under migrating directory"
13724
13725         local u=$(ls $migrate_dir | wc -l)
13726         [ "$u" == "$t" ] || error "$u != $t during migration"
13727
13728         for file in $(find $migrate_dir); do
13729                 stat $file || error "stat $file failed"
13730         done
13731
13732         do_facet mds1 lctl set_param fail_loc=0
13733         do_facet mds1 lctl set_param fail_val=0
13734
13735         $LFS migrate -m $MDTIDX $migrate_dir ||
13736                 error "migrate open files should failed with open files"
13737
13738         echo "Finish migration, then checking.."
13739         for file in $(find $migrate_dir); do
13740                 mdt_index=$($LFS getstripe -M $file)
13741                 [ $mdt_index == $MDTIDX ] ||
13742                         error "$file is not on MDT${MDTIDX}"
13743         done
13744
13745         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13746 }
13747 run_test 230c "check directory accessiblity if migration is failed"
13748
13749 test_230d() {
13750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13751         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13752         local MDTIDX=1
13753         local mdt_index
13754         local migrate_dir=$DIR/$tdir/migrate_dir
13755         local i
13756         local j
13757
13758         test_mkdir $DIR/$tdir
13759         test_mkdir -i0 -c1 $migrate_dir
13760
13761         for ((i=0; i<100; i++)); do
13762                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
13763                 createmany -o $migrate_dir/dir_${i}/f 100 ||
13764                         error "create files under remote dir failed $i"
13765         done
13766
13767         $LFS migrate -m $MDTIDX $migrate_dir ||
13768                 error "migrate remote dir error"
13769
13770         echo "Finish migration, then checking.."
13771         for file in $(find $migrate_dir); do
13772                 mdt_index=$($LFS getstripe -M $file)
13773                 [ $mdt_index == $MDTIDX ] ||
13774                         error "$file is not on MDT${MDTIDX}"
13775         done
13776
13777         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13778 }
13779 run_test 230d "check migrate big directory"
13780
13781 test_230e() {
13782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13783         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13784         local i
13785         local j
13786         local a_fid
13787         local b_fid
13788
13789         mkdir -p $DIR/$tdir
13790         mkdir $DIR/$tdir/migrate_dir
13791         mkdir $DIR/$tdir/other_dir
13792         touch $DIR/$tdir/migrate_dir/a
13793         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
13794         ls $DIR/$tdir/other_dir
13795
13796         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13797                 error "migrate dir fails"
13798
13799         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13800         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13801
13802         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13803         [ $mdt_index == 0 ] || error "a is not on MDT0"
13804
13805         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
13806                 error "migrate dir fails"
13807
13808         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
13809         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
13810
13811         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13812         [ $mdt_index == 1 ] || error "a is not on MDT1"
13813
13814         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
13815         [ $mdt_index == 1 ] || error "b is not on MDT1"
13816
13817         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13818         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
13819
13820         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
13821
13822         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13823 }
13824 run_test 230e "migrate mulitple local link files"
13825
13826 test_230f() {
13827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13828         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13829         local a_fid
13830         local ln_fid
13831
13832         mkdir -p $DIR/$tdir
13833         mkdir $DIR/$tdir/migrate_dir
13834         $LFS mkdir -i1 $DIR/$tdir/other_dir
13835         touch $DIR/$tdir/migrate_dir/a
13836         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13837         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13838         ls $DIR/$tdir/other_dir
13839
13840         # a should be migrated to MDT1, since no other links on MDT0
13841         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13842                 error "#1 migrate dir fails"
13843         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13844         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13845         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13846         [ $mdt_index == 1 ] || error "a is not on MDT1"
13847
13848         # a should stay on MDT1, because it is a mulitple link file
13849         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13850                 error "#2 migrate dir fails"
13851         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13852         [ $mdt_index == 1 ] || error "a is not on MDT1"
13853
13854         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13855                 error "#3 migrate dir fails"
13856
13857         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13858         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13859         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13860
13861         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13862         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13863
13864         # a should be migrated to MDT0, since no other links on MDT1
13865         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13866                 error "#4 migrate dir fails"
13867         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13868         [ $mdt_index == 0 ] || error "a is not on MDT0"
13869
13870         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13871 }
13872 run_test 230f "migrate mulitple remote link files"
13873
13874 test_230g() {
13875         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13876         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13877
13878         mkdir -p $DIR/$tdir/migrate_dir
13879
13880         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
13881                 error "migrating dir to non-exist MDT succeeds"
13882         true
13883 }
13884 run_test 230g "migrate dir to non-exist MDT"
13885
13886 test_230h() {
13887         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13888         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13889         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
13890                 skip "Need MDS version at least 2.7.64" && return
13891         local mdt_index
13892
13893         mkdir -p $DIR/$tdir/migrate_dir
13894
13895         $LFS migrate -m1 $DIR &&
13896                 error "migrating mountpoint1 should fail"
13897
13898         $LFS migrate -m1 $DIR/$tdir/.. &&
13899                 error "migrating mountpoint2 should fail"
13900
13901         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
13902                 error "migrating $tdir fail"
13903
13904         mdt_index=$($LFS getstripe -M $DIR/$tdir)
13905         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13906
13907         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13908         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
13909
13910 }
13911 run_test 230h "migrate .. and root"
13912
13913 test_230i() {
13914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13915         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13916
13917         mkdir -p $DIR/$tdir/migrate_dir
13918
13919         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
13920                 error "migration fails with a tailing slash"
13921
13922         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
13923                 error "migration fails with two tailing slashes"
13924 }
13925 run_test 230i "lfs migrate -m tolerates trailing slashes"
13926
13927 test_231a()
13928 {
13929         # For simplicity this test assumes that max_pages_per_rpc
13930         # is the same across all OSCs
13931         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
13932         local bulk_size=$((max_pages * 4096))
13933         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
13934                                        head -n 1)
13935
13936         mkdir -p $DIR/$tdir
13937         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
13938                 error "failed to set stripe with -S ${brw_size}M option"
13939
13940         # clear the OSC stats
13941         $LCTL set_param osc.*.stats=0 &>/dev/null
13942         stop_writeback
13943
13944         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
13945         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
13946                 oflag=direct &>/dev/null || error "dd failed"
13947
13948         sync; sleep 1; sync # just to be safe
13949         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
13950         if [ x$nrpcs != "x1" ]; then
13951                 $LCTL get_param osc.*.stats
13952                 error "found $nrpcs ost_write RPCs, not 1 as expected"
13953         fi
13954
13955         start_writeback
13956         # Drop the OSC cache, otherwise we will read from it
13957         cancel_lru_locks osc
13958
13959         # clear the OSC stats
13960         $LCTL set_param osc.*.stats=0 &>/dev/null
13961
13962         # Client reads $bulk_size.
13963         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
13964                 iflag=direct &>/dev/null || error "dd failed"
13965
13966         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
13967         if [ x$nrpcs != "x1" ]; then
13968                 $LCTL get_param osc.*.stats
13969                 error "found $nrpcs ost_read RPCs, not 1 as expected"
13970         fi
13971 }
13972 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
13973
13974 test_231b() {
13975         mkdir -p $DIR/$tdir
13976         local i
13977         for i in {0..1023}; do
13978                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
13979                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
13980                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
13981         done
13982         sync
13983 }
13984 run_test 231b "must not assert on fully utilized OST request buffer"
13985
13986 test_232() {
13987         mkdir -p $DIR/$tdir
13988         #define OBD_FAIL_LDLM_OST_LVB            0x31c
13989         $LCTL set_param fail_loc=0x31c
13990
13991         # ignore dd failure
13992         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
13993
13994         $LCTL set_param fail_loc=0
13995         umount_client $MOUNT || error "umount failed"
13996         mount_client $MOUNT || error "mount failed"
13997 }
13998 run_test 232 "failed lock should not block umount"
13999
14000 test_233a() {
14001         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
14002         { skip "Need MDS version at least 2.3.64"; return; }
14003         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14004
14005         local fid=$($LFS path2fid $MOUNT)
14006         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14007                 error "cannot access $MOUNT using its FID '$fid'"
14008 }
14009 run_test 233a "checking that OBF of the FS root succeeds"
14010
14011 test_233b() {
14012         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
14013         { skip "Need MDS version at least 2.5.90"; return; }
14014         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14015
14016         local fid=$($LFS path2fid $MOUNT/.lustre)
14017         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14018                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
14019
14020         fid=$($LFS path2fid $MOUNT/.lustre/fid)
14021         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14022                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
14023 }
14024 run_test 233b "checking that OBF of the FS .lustre succeeds"
14025
14026 test_234() {
14027         local p="$TMP/sanityN-$TESTNAME.parameters"
14028         save_lustre_params client "llite.*.xattr_cache" > $p
14029         lctl set_param llite.*.xattr_cache 1 ||
14030                 { skip "xattr cache is not supported"; return 0; }
14031
14032         mkdir -p $DIR/$tdir || error "mkdir failed"
14033         touch $DIR/$tdir/$tfile || error "touch failed"
14034         # OBD_FAIL_LLITE_XATTR_ENOMEM
14035         $LCTL set_param fail_loc=0x1405
14036         # output of the form: attr 2 4 44 3 fc13 x86_64
14037         V=($(IFS=".-" rpm -q attr))
14038         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
14039               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
14040                 # attr pre-2.4.44-7 had a bug with rc
14041                 # LU-3703 - SLES 11 and FC13 clients have older attr
14042                 getfattr -n user.attr $DIR/$tdir/$tfile &&
14043                         error "getfattr should have failed with ENOMEM"
14044         else
14045                 skip "LU-3703: attr version $(getfattr --version) too old"
14046         fi
14047         $LCTL set_param fail_loc=0x0
14048         rm -rf $DIR/$tdir
14049
14050         restore_lustre_params < $p
14051         rm -f $p
14052 }
14053 run_test 234 "xattr cache should not crash on ENOMEM"
14054
14055 test_235() {
14056         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
14057                 skip "Need MDS version at least 2.4.52" && return
14058         flock_deadlock $DIR/$tfile
14059         local RC=$?
14060         case $RC in
14061                 0)
14062                 ;;
14063                 124) error "process hangs on a deadlock"
14064                 ;;
14065                 *) error "error executing flock_deadlock $DIR/$tfile"
14066                 ;;
14067         esac
14068 }
14069 run_test 235 "LU-1715: flock deadlock detection does not work properly"
14070
14071 #LU-2935
14072 test_236() {
14073         check_swap_layouts_support && return 0
14074         test_mkdir -c1 $DIR/$tdir
14075
14076         local ref1=/etc/passwd
14077         local ref2=/etc/group
14078         local file1=$DIR/$tdir/f1
14079         local file2=$DIR/$tdir/f2
14080
14081         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
14082         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
14083         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
14084         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
14085         local fd=$(free_fd)
14086         local cmd="exec $fd<>$file2"
14087         eval $cmd
14088         rm $file2
14089         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
14090                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
14091         cmd="exec $fd>&-"
14092         eval $cmd
14093         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14094
14095         #cleanup
14096         rm -rf $DIR/$tdir
14097 }
14098 run_test 236 "Layout swap on open unlinked file"
14099
14100 # test to verify file handle related system calls
14101 # (name_to_handle_at/open_by_handle_at)
14102 # The new system calls are supported in glibc >= 2.14.
14103
14104 test_237() {
14105         echo "Test file_handle syscalls" > $DIR/$tfile ||
14106                 error "write failed"
14107         check_fhandle_syscalls $DIR/$tfile ||
14108                 error "check_fhandle_syscalls failed"
14109 }
14110 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
14111
14112 # LU-4659 linkea consistency
14113 test_238() {
14114         local server_version=$(lustre_version_code $SINGLEMDS)
14115
14116         [[ $server_version -gt $(version_code 2.5.57) ]] ||
14117                 [[ $server_version -gt $(version_code 2.5.1) &&
14118                    $server_version -lt $(version_code 2.5.50) ]] ||
14119                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
14120
14121         touch $DIR/$tfile
14122         ln $DIR/$tfile $DIR/$tfile.lnk
14123         touch $DIR/$tfile.new
14124         mv $DIR/$tfile.new $DIR/$tfile
14125         local fid1=$($LFS path2fid $DIR/$tfile)
14126         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
14127         local path1=$($LFS fid2path $FSNAME "$fid1")
14128         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
14129         local path2=$($LFS fid2path $FSNAME "$fid2")
14130         [ $tfile.lnk == $path2 ] ||
14131                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
14132         rm -f $DIR/$tfile*
14133 }
14134 run_test 238 "Verify linkea consistency"
14135
14136 test_239() {
14137         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
14138                 skip "Need MDS version at least 2.5.60" && return
14139         local list=$(comma_list $(mdts_nodes))
14140
14141         mkdir -p $DIR/$tdir
14142         createmany -o $DIR/$tdir/f- 5000
14143         unlinkmany $DIR/$tdir/f- 5000
14144         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.53) ] &&
14145                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
14146         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
14147                         osp.*MDT*.sync_in_flight" | calc_sum)
14148         [ "$changes" -eq 0 ] || error "$changes not synced"
14149 }
14150 run_test 239 "osp_sync test"
14151
14152 test_239a() { #LU-5297
14153         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14154         touch $DIR/$tfile
14155         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
14156         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
14157         chgrp $RUNAS_GID $DIR/$tfile
14158         wait_delete_completed
14159 }
14160 run_test 239a "process invalid osp sync record correctly"
14161
14162 test_239b() { #LU-5297
14163         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14164         touch $DIR/$tfile1
14165         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
14166         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
14167         chgrp $RUNAS_GID $DIR/$tfile1
14168         wait_delete_completed
14169         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14170         touch $DIR/$tfile2
14171         chgrp $RUNAS_GID $DIR/$tfile2
14172         wait_delete_completed
14173 }
14174 run_test 239b "process osp sync record with ENOMEM error correctly"
14175
14176 test_240() {
14177         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14178         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14179
14180         mkdir -p $DIR/$tdir
14181
14182         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14183                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14184         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14185                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14186
14187         umount_client $MOUNT || error "umount failed"
14188         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14189         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14190         mount_client $MOUNT || error "failed to mount client"
14191
14192         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14193         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14194 }
14195 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14196
14197 test_241_bio() {
14198         for LOOP in $(seq $1); do
14199                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14200                 cancel_lru_locks $OSC || true
14201         done
14202 }
14203
14204 test_241_dio() {
14205         for LOOP in $(seq $1); do
14206                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14207                                                 iflag=direct 2>/dev/null
14208         done
14209 }
14210
14211 test_241a() { # was test_241
14212         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14213         ls -la $DIR/$tfile
14214         cancel_lru_locks $OSC
14215         test_241_bio 1000 &
14216         PID=$!
14217         test_241_dio 1000
14218         wait $PID
14219 }
14220 run_test 241a "bio vs dio"
14221
14222 test_241b() {
14223         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14224         ls -la $DIR/$tfile
14225         test_241_dio 1000 &
14226         PID=$!
14227         test_241_dio 1000
14228         wait $PID
14229 }
14230 run_test 241b "dio vs dio"
14231
14232 test_242() {
14233         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14234         mkdir -p $DIR/$tdir
14235         touch $DIR/$tdir/$tfile
14236
14237         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14238         do_facet mds1 lctl set_param fail_loc=0x105
14239         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14240
14241         do_facet mds1 lctl set_param fail_loc=0
14242         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14243 }
14244 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14245
14246 test_243()
14247 {
14248         test_mkdir $DIR/$tdir
14249         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14250 }
14251 run_test 243 "various group lock tests"
14252
14253 test_244()
14254 {
14255         test_mkdir $DIR/$tdir
14256         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14257         sendfile_grouplock $DIR/$tdir/$tfile || \
14258                 error "sendfile+grouplock failed"
14259         rm -rf $DIR/$tdir
14260 }
14261 run_test 244 "sendfile with group lock tests"
14262
14263 test_245() {
14264         local flagname="multi_mod_rpcs"
14265         local connect_data_name="max_mod_rpcs"
14266         local out
14267
14268         # check if multiple modify RPCs flag is set
14269         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14270                 grep "connect_flags:")
14271         echo "$out"
14272
14273         echo "$out" | grep -qw $flagname
14274         if [ $? -ne 0 ]; then
14275                 echo "connect flag $flagname is not set"
14276                 return
14277         fi
14278
14279         # check if multiple modify RPCs data is set
14280         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14281         echo "$out"
14282
14283         echo "$out" | grep -qw $connect_data_name ||
14284                 error "import should have connect data $connect_data_name"
14285 }
14286 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14287
14288 test_246() { # LU-7371
14289         remote_ost_nodsh && skip "remote OST with nodsh" && return
14290         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14291                 skip "Need OST version >= 2.7.62" && return 0
14292         do_facet ost1 $LCTL set_param fail_val=4095
14293 #define OBD_FAIL_OST_READ_SIZE          0x234
14294         do_facet ost1 $LCTL set_param fail_loc=0x234
14295         $LFS setstripe $DIR/$tfile -i 0 -c 1
14296         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14297         cancel_lru_locks $FSNAME-OST0000
14298         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14299 }
14300 run_test 246 "Read file of size 4095 should return right length"
14301
14302 test_247a() {
14303         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14304                 grep -q subtree ||
14305                 { skip "Fileset feature is not supported"; return; }
14306
14307         local submount=${MOUNT}_$tdir
14308
14309         mkdir $MOUNT/$tdir
14310         mkdir -p $submount || error "mkdir $submount failed"
14311         FILESET="$FILESET/$tdir" mount_client $submount ||
14312                 error "mount $submount failed"
14313         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14314         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14315                 error "read $MOUNT/$tdir/$tfile failed"
14316         umount_client $submount || error "umount $submount failed"
14317         rmdir $submount
14318 }
14319 run_test 247a "mount subdir as fileset"
14320
14321 test_247b() {
14322         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14323                 { skip "Fileset feature is not supported"; return; }
14324
14325         local submount=${MOUNT}_$tdir
14326
14327         rm -rf $MOUNT/$tdir
14328         mkdir -p $submount || error "mkdir $submount failed"
14329         SKIP_FILESET=1
14330         FILESET="$FILESET/$tdir" mount_client $submount &&
14331                 error "mount $submount should fail"
14332         rmdir $submount
14333 }
14334 run_test 247b "mount subdir that dose not exist"
14335
14336 test_247c() {
14337         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14338                 { skip "Fileset feature is not supported"; return; }
14339
14340         local submount=${MOUNT}_$tdir
14341
14342         mkdir -p $MOUNT/$tdir/dir1
14343         mkdir -p $submount || error "mkdir $submount failed"
14344         FILESET="$FILESET/$tdir" mount_client $submount ||
14345                 error "mount $submount failed"
14346         local fid=$($LFS path2fid $MOUNT/)
14347         $LFS fid2path $submount $fid && error "fid2path should fail"
14348         umount_client $submount || error "umount $submount failed"
14349         rmdir $submount
14350 }
14351 run_test 247c "running fid2path outside root"
14352
14353 test_247d() {
14354         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14355                 { skip "Fileset feature is not supported"; return; }
14356
14357         local submount=${MOUNT}_$tdir
14358
14359         mkdir -p $MOUNT/$tdir/dir1
14360         mkdir -p $submount || error "mkdir $submount failed"
14361         FILESET="$FILESET/$tdir" mount_client $submount ||
14362                 error "mount $submount failed"
14363         local fid=$($LFS path2fid $submount/dir1)
14364         $LFS fid2path $submount $fid || error "fid2path should succeed"
14365         umount_client $submount || error "umount $submount failed"
14366         rmdir $submount
14367 }
14368 run_test 247d "running fid2path inside root"
14369
14370 # LU-8037
14371 test_247e() {
14372         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14373                 grep -q subtree ||
14374                 { skip "Fileset feature is not supported"; return; }
14375
14376         local submount=${MOUNT}_$tdir
14377
14378         mkdir $MOUNT/$tdir
14379         mkdir -p $submount || error "mkdir $submount failed"
14380         FILESET="$FILESET/.." mount_client $submount &&
14381                 error "mount $submount should fail"
14382         rmdir $submount
14383 }
14384 run_test 247e "mount .. as fileset"
14385
14386 test_248() {
14387         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14388         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14389
14390         # create a large file for fast read verification
14391         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14392
14393         # make sure the file is created correctly
14394         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14395                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14396
14397         echo "Test 1: verify that fast read is 4 times faster on cache read"
14398
14399         # small read with fast read enabled
14400         $LCTL set_param -n llite.*.fast_read=1
14401         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14402                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14403                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14404         # small read with fast read disabled
14405         $LCTL set_param -n llite.*.fast_read=0
14406         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14407                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14408                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14409
14410         # verify that fast read is 4 times faster for cache read
14411         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
14412                 error_not_in_vm "fast read was not 4 times faster: " \
14413                            "$t_fast vs $t_slow"
14414
14415         echo "Test 2: verify the performance between big and small read"
14416         $LCTL set_param -n llite.*.fast_read=1
14417
14418         # 1k non-cache read
14419         cancel_lru_locks osc
14420         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14421                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14422                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14423
14424         # 1M non-cache read
14425         cancel_lru_locks osc
14426         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14427                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14428                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14429
14430         # verify that big IO is not 4 times faster than small IO
14431         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
14432                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
14433
14434         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
14435         rm -f $DIR/$tfile
14436 }
14437 run_test 248 "fast read verification"
14438
14439 test_249() { # LU-7890
14440         rm -f $DIR/$tfile
14441         $SETSTRIPE -c 1 $DIR/$tfile
14442
14443         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
14444         skip "Need at least version 2.8.54"
14445
14446         # Offset 2T == 4k * 512M
14447         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
14448                 error "dd to 2T offset failed"
14449 }
14450 run_test 249 "Write above 2T file size"
14451
14452 test_250() {
14453         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
14454          && skip "no 16TB file size limit on ZFS" && return
14455
14456         $SETSTRIPE -c 1 $DIR/$tfile
14457         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
14458         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
14459         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
14460         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
14461                 conv=notrunc,fsync && error "append succeeded"
14462         return 0
14463 }
14464 run_test 250 "Write above 16T limit"
14465
14466 test_251() {
14467         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
14468
14469         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
14470         #Skip once - writing the first stripe will succeed
14471         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14472         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
14473                 error "short write happened"
14474
14475         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14476         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
14477                 error "short read happened"
14478
14479         rm -f $DIR/$tfile
14480 }
14481 run_test 251 "Handling short read and write correctly"
14482
14483 test_252() {
14484         local tgt
14485         local dev
14486         local out
14487         local uuid
14488         local num
14489         local gen
14490
14491         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14492         remote_ost_nodsh && skip "remote OST with nodsh" && return
14493         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
14494              "$(facet_fstype mds1)" != "ldiskfs" ]; then
14495                 skip "ldiskfs only test"
14496                 return
14497         fi
14498
14499         # check lr_reader on OST0000
14500         tgt=ost1
14501         dev=$(facet_device $tgt)
14502         out=$(do_facet $tgt $LR_READER $dev)
14503         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14504         echo "$out"
14505         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
14506         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
14507                 error "Invalid uuid returned by $LR_READER on target $tgt"
14508         echo -e "uuid returned by $LR_READER is '$uuid'\n"
14509
14510         # check lr_reader -c on MDT0000
14511         tgt=mds1
14512         dev=$(facet_device $tgt)
14513         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
14514                 echo "$LR_READER does not support additional options"
14515                 return 0
14516         fi
14517         out=$(do_facet $tgt $LR_READER -c $dev)
14518         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14519         echo "$out"
14520         num=$(echo "$out" | grep -c "mdtlov")
14521         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
14522                 error "Invalid number of mdtlov clients returned by $LR_READER"
14523         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
14524
14525         # check lr_reader -cr on MDT0000
14526         out=$(do_facet $tgt $LR_READER -cr $dev)
14527         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14528         echo "$out"
14529         echo "$out" | grep -q "^reply_data:$" ||
14530                 error "$LR_READER should have returned 'reply_data' section"
14531         num=$(echo "$out" | grep -c "client_generation")
14532         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
14533 }
14534 run_test 252 "check lr_reader tool"
14535
14536 test_253_fill_ost() {
14537         local size_mb #how many MB should we write to pass watermark
14538         local lwm=$3  #low watermark
14539         local free_10mb #10% of free space
14540
14541         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14542         size_mb=$((free_kb / 1024 - lwm))
14543         free_10mb=$((free_kb / 10240))
14544         #If 10% of free space cross low watermark use it
14545         if (( free_10mb > size_mb )); then
14546                 size_mb=$free_10mb
14547         else
14548                 #At least we need to store 1.1 of difference between
14549                 #free space and low watermark
14550                 size_mb=$((size_mb + size_mb / 10))
14551         fi
14552         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
14553                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
14554                          oflag=append conv=notrunc
14555         fi
14556
14557         sleep_maxage
14558
14559         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14560         echo "OST still has $((free_kb / 1024)) mbytes free"
14561 }
14562
14563 test_253() {
14564         local ostidx=0
14565         local rc=0
14566
14567         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14568         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14569         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
14570
14571         local ost_name=$($LFS osts |
14572                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
14573         # on the mdt's osc
14574         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
14575         do_facet $SINGLEMDS $LCTL get_param -n \
14576                 osp.$mdtosc_proc1.reserved_mb_high ||
14577                 { skip  "remote MDS does not support reserved_mb_high" &&
14578                   return; }
14579
14580         rm -rf $DIR/$tdir
14581         wait_mds_ost_sync
14582         wait_delete_completed
14583         mkdir $DIR/$tdir
14584
14585         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
14586                         osp.$mdtosc_proc1.reserved_mb_high)
14587         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
14588                         osp.$mdtosc_proc1.reserved_mb_low)
14589         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
14590
14591         if ! combined_mgs_mds ; then
14592                 mount_mgs_client
14593         fi
14594         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
14595         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
14596                 error "Adding $ost_name to pool failed"
14597
14598         # Wait for client to see a OST at pool
14599         wait_update $HOSTNAME "$LCTL get_param -n
14600                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
14601                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
14602                 error "Client can not see the pool"
14603         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
14604                 error "Setstripe failed"
14605
14606         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
14607         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
14608         echo "OST still has $((blocks/1024)) mbytes free"
14609
14610         local new_lwm=$((blocks/1024-10))
14611         do_facet $SINGLEMDS $LCTL set_param \
14612                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
14613         do_facet $SINGLEMDS $LCTL set_param \
14614                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
14615
14616         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14617
14618         #First enospc could execute orphan deletion so repeat.
14619         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14620
14621         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14622                         osp.$mdtosc_proc1.prealloc_status)
14623         echo "prealloc_status $oa_status"
14624
14625         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
14626                 error "File creation should fail"
14627         #object allocation was stopped, but we still able to append files
14628         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
14629                 error "Append failed"
14630         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
14631
14632         wait_delete_completed
14633
14634         sleep_maxage
14635
14636         for i in $(seq 10 12); do
14637                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
14638                         error "File creation failed after rm";
14639         done
14640
14641         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14642                         osp.$mdtosc_proc1.prealloc_status)
14643         echo "prealloc_status $oa_status"
14644
14645         if (( oa_status != 0 )); then
14646                 error "Object allocation still disable after rm"
14647         fi
14648         do_facet $SINGLEMDS $LCTL set_param \
14649                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
14650         do_facet $SINGLEMDS $LCTL set_param \
14651                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
14652
14653
14654         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
14655                 error "Remove $ost_name from pool failed"
14656         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14657                 error "Pool destroy fialed"
14658
14659         if ! combined_mgs_mds ; then
14660                 umount_mgs_client
14661         fi
14662 }
14663 run_test 253 "Check object allocation limit"
14664
14665 test_254() {
14666         local cl_user
14667
14668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14669         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14670         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
14671                 { skip "MDS does not support changelog_size" && return; }
14672
14673         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14674         echo "Registered as changelog user $cl_user"
14675
14676         $LFS changelog_clear $MDT0 $cl_user 0
14677
14678         local size1=$(do_facet mds1 \
14679                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14680         echo "Changelog size $size1"
14681
14682         rm -rf $DIR/$tdir
14683         $LFS mkdir -i 0 $DIR/$tdir
14684         # change something
14685         mkdir -p $DIR/$tdir/pics/2008/zachy
14686         touch $DIR/$tdir/pics/2008/zachy/timestamp
14687         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
14688         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14689         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14690         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14691         rm $DIR/$tdir/pics/desktop.jpg
14692
14693         local size2=$(do_facet mds1 \
14694                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14695         echo "Changelog size after work $size2"
14696
14697         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14698
14699         if (( size2 <= size1 )); then
14700                 error "Changelog size after work should be greater than original"
14701         fi
14702         return 0
14703 }
14704 run_test 254 "Check changelog size"
14705
14706 ladvise_no_type()
14707 {
14708         local type=$1
14709         local file=$2
14710
14711         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
14712                 awk -F: '{print $2}' | grep $type > /dev/null
14713         if [ $? -ne 0 ]; then
14714                 return 0
14715         fi
14716         return 1
14717 }
14718
14719 ladvise_no_ioctl()
14720 {
14721         local file=$1
14722
14723         lfs ladvise -a willread $file > /dev/null 2>&1
14724         if [ $? -eq 0 ]; then
14725                 return 1
14726         fi
14727
14728         lfs ladvise -a willread $file 2>&1 |
14729                 grep "Inappropriate ioctl for device" > /dev/null
14730         if [ $? -eq 0 ]; then
14731                 return 0
14732         fi
14733         return 1
14734 }
14735
14736 percent() {
14737         bc <<<"scale=2; ($1 - $2) * 100 / $2"
14738 }
14739
14740 # run a random read IO workload
14741 # usage: random_read_iops <filename> <filesize> <iosize>
14742 random_read_iops() {
14743         local file=$1
14744         local fsize=$2
14745         local iosize=${3:-4096}
14746
14747         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
14748                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
14749 }
14750
14751 drop_file_oss_cache() {
14752         local file="$1"
14753         local nodes="$2"
14754
14755         $LFS ladvise -a dontneed $file 2>/dev/null ||
14756                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
14757 }
14758
14759 ladvise_willread_performance()
14760 {
14761         local repeat=10
14762         local average_origin=0
14763         local average_cache=0
14764         local average_ladvise=0
14765
14766         for ((i = 1; i <= $repeat; i++)); do
14767                 echo "Iter $i/$repeat: reading without willread hint"
14768                 cancel_lru_locks osc
14769                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14770                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
14771                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
14772                 average_origin=$(bc <<<"$average_origin + $speed_origin")
14773
14774                 cancel_lru_locks osc
14775                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
14776                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
14777                 average_cache=$(bc <<<"$average_cache + $speed_cache")
14778
14779                 cancel_lru_locks osc
14780                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14781                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
14782                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
14783                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
14784                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
14785         done
14786         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
14787         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
14788         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
14789
14790         speedup_cache=$(percent $average_cache $average_origin)
14791         speedup_ladvise=$(percent $average_ladvise $average_origin)
14792
14793         echo "Average uncached read: $average_origin"
14794         echo "Average speedup with OSS cached read: " \
14795                 "$average_cache = +$speedup_cache%"
14796         echo "Average speedup with ladvise willread: " \
14797                 "$average_ladvise = +$speedup_ladvise%"
14798
14799         local lowest_speedup=20
14800         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
14801                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
14802                         "got $average_cache%. Skipping ladvise willread check."
14803                 return 0
14804         fi
14805
14806         # the test won't work on ZFS until it supports 'ladvise dontneed', but
14807         # it is still good to run until then to exercise 'ladvise willread'
14808         ! $LFS ladvise -a dontneed $DIR/$tfile &&
14809                 [ "$(facet_fstype ost1)" = "zfs" ] &&
14810                 echo "osd-zfs does not support dontneed or drop_caches" &&
14811                 return 0
14812
14813         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
14814         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
14815                 error_not_in_vm "Speedup with willread is less than " \
14816                         "$lowest_speedup%, got $average_ladvise%"
14817 }
14818
14819 test_255a() {
14820         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14821                 skip "lustre < 2.8.54 does not support ladvise " && return
14822         remote_ost_nodsh && skip "remote OST with nodsh" && return
14823
14824         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
14825
14826         ladvise_no_type willread $DIR/$tfile &&
14827                 skip "willread ladvise is not supported" && return
14828
14829         ladvise_no_ioctl $DIR/$tfile &&
14830                 skip "ladvise ioctl is not supported" && return
14831
14832         local size_mb=100
14833         local size=$((size_mb * 1048576))
14834         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14835                 error "dd to $DIR/$tfile failed"
14836
14837         lfs ladvise -a willread $DIR/$tfile ||
14838                 error "Ladvise failed with no range argument"
14839
14840         lfs ladvise -a willread -s 0 $DIR/$tfile ||
14841                 error "Ladvise failed with no -l or -e argument"
14842
14843         lfs ladvise -a willread -e 1 $DIR/$tfile ||
14844                 error "Ladvise failed with only -e argument"
14845
14846         lfs ladvise -a willread -l 1 $DIR/$tfile ||
14847                 error "Ladvise failed with only -l argument"
14848
14849         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
14850                 error "End offset should not be smaller than start offset"
14851
14852         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
14853                 error "End offset should not be equal to start offset"
14854
14855         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
14856                 error "Ladvise failed with overflowing -s argument"
14857
14858         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
14859                 error "Ladvise failed with overflowing -e argument"
14860
14861         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
14862                 error "Ladvise failed with overflowing -l argument"
14863
14864         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
14865                 error "Ladvise succeeded with conflicting -l and -e arguments"
14866
14867         echo "Synchronous ladvise should wait"
14868         local delay=4
14869 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
14870         do_nodes $(comma_list $(osts_nodes)) \
14871                 $LCTL set_param fail_val=$delay fail_loc=0x237
14872
14873         local start_ts=$SECONDS
14874         lfs ladvise -a willread $DIR/$tfile ||
14875                 error "Ladvise failed with no range argument"
14876         local end_ts=$SECONDS
14877         local inteval_ts=$((end_ts - start_ts))
14878
14879         if [ $inteval_ts -lt $(($delay - 1)) ]; then
14880                 error "Synchronous advice didn't wait reply"
14881         fi
14882
14883         echo "Asynchronous ladvise shouldn't wait"
14884         local start_ts=$SECONDS
14885         lfs ladvise -a willread -b $DIR/$tfile ||
14886                 error "Ladvise failed with no range argument"
14887         local end_ts=$SECONDS
14888         local inteval_ts=$((end_ts - start_ts))
14889
14890         if [ $inteval_ts -gt $(($delay / 2)) ]; then
14891                 error "Asynchronous advice blocked"
14892         fi
14893
14894         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
14895         ladvise_willread_performance
14896 }
14897 run_test 255a "check 'lfs ladvise -a willread'"
14898
14899 facet_meminfo() {
14900         local facet=$1
14901         local info=$2
14902
14903         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
14904 }
14905
14906 test_255b() {
14907         remote_ost_nodsh && skip "remote OST with nodsh" && return
14908
14909         lfs setstripe -c 1 -i 0 $DIR/$tfile
14910
14911         ladvise_no_type dontneed $DIR/$tfile &&
14912                 skip "dontneed ladvise is not supported" && return
14913
14914         ladvise_no_ioctl $DIR/$tfile &&
14915                 skip "ladvise ioctl is not supported" && return
14916
14917         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14918                 skip "lustre < 2.8.54 does not support ladvise" && return
14919
14920         ! $LFS ladvise -a dontneed $DIR/$tfile &&
14921                 [ "$(facet_fstype ost1)" = "zfs" ] &&
14922                 skip "zfs-osd does not support 'ladvise dontneed'" && return
14923
14924         local size_mb=100
14925         local size=$((size_mb * 1048576))
14926         # In order to prevent disturbance of other processes, only check 3/4
14927         # of the memory usage
14928         local kibibytes=$((size_mb * 1024 * 3 / 4))
14929
14930         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14931                 error "dd to $DIR/$tfile failed"
14932
14933         local total=$(facet_meminfo ost1 MemTotal)
14934         echo "Total memory: $total KiB"
14935
14936         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
14937         local before_read=$(facet_meminfo ost1 Cached)
14938         echo "Cache used before read: $before_read KiB"
14939
14940         lfs ladvise -a willread $DIR/$tfile ||
14941                 error "Ladvise willread failed"
14942         local after_read=$(facet_meminfo ost1 Cached)
14943         echo "Cache used after read: $after_read KiB"
14944
14945         lfs ladvise -a dontneed $DIR/$tfile ||
14946                 error "Ladvise dontneed again failed"
14947         local no_read=$(facet_meminfo ost1 Cached)
14948         echo "Cache used after dontneed ladvise: $no_read KiB"
14949
14950         if [ $total -lt $((before_read + kibibytes)) ]; then
14951                 echo "Memory is too small, abort checking"
14952                 return 0
14953         fi
14954
14955         if [ $((before_read + kibibytes)) -gt $after_read ]; then
14956                 error "Ladvise willread should use more memory" \
14957                         "than $kibibytes KiB"
14958         fi
14959
14960         if [ $((no_read + kibibytes)) -gt $after_read ]; then
14961                 error "Ladvise dontneed should release more memory" \
14962                         "than $kibibytes KiB"
14963         fi
14964 }
14965 run_test 255b "check 'lfs ladvise -a dontneed'"
14966
14967 test_255c() {
14968         local count
14969         local new_count
14970         local difference
14971         local i
14972         local rc
14973
14974         [ $(lustre_version_code ost1) -lt $(version_code 2.10.50) ] &&
14975                 skip "lustre < 2.10.53 does not support lockahead" && return
14976
14977         test_mkdir -p $DIR/$tdir
14978         $SETSTRIPE -i 0 $DIR/$tdir
14979
14980         #test 10 returns only success/failure
14981         i=10
14982         lockahead_test -d $DIR/$tdir -t $i
14983         rc=$?
14984         if [ $rc -eq 255 ]; then
14985                 error "Ladvise test${i} failed, ${rc}"
14986         fi
14987
14988         #test 11 counts lock enqueue requests, all others count new locks
14989         i=11
14990         count=$(do_facet ost1 \
14991                 $LCTL get_param -n ost.OSS.ost.stats)
14992         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
14993
14994         lockahead_test -d $DIR/$tdir -t $i
14995         rc=$?
14996         if [ $rc -eq 255 ]; then
14997                 error "Ladvise test${i} failed, ${rc}"
14998         fi
14999
15000         new_count=$(do_facet ost1 \
15001                 $LCTL get_param -n ost.OSS.ost.stats)
15002         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
15003                    awk '{ print $2 }')
15004
15005         difference="$((new_count - count))"
15006         if [ $difference -ne $rc ]; then
15007                 error "Ladvise test${i}, bad enqueue count, returned " \
15008                       "${rc}, actual ${difference}"
15009         fi
15010
15011         for i in $(seq 12 21); do
15012                 # If we do not do this, we run the risk of having too many
15013                 # locks and starting lock cancellation while we are checking
15014                 # lock counts.
15015                 cancel_lru_locks osc
15016
15017                 count=$($LCTL get_param -n \
15018                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15019
15020                 lockahead_test -d $DIR/$tdir -t $i
15021                 rc=$?
15022                 if [ $rc -eq 255 ]; then
15023                         error "Ladvise test ${i} failed, ${rc}"
15024                 fi
15025
15026                 new_count=$($LCTL get_param -n \
15027                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15028                 difference="$((new_count - count))"
15029
15030                 # Test 15 output is divided by 100 to map down to valid return
15031                 if [ $i -eq 15 ]; then
15032                         rc="$((rc * 100))"
15033                 fi
15034
15035                 if [ $difference -ne $rc ]; then
15036                         error "Ladvise test ${i}, bad lock count, returned " \
15037                               "${rc}, actual ${difference}"
15038                 fi
15039         done
15040
15041         #test 22 returns only success/failure
15042         i=22
15043         lockahead_test -d $DIR/$tdir -t $i
15044         rc=$?
15045         if [ $rc -eq 255 ]; then
15046                 error "Ladvise test${i} failed, ${rc}"
15047         fi
15048
15049 }
15050 run_test 255c "suite of ladvise lockahead tests"
15051
15052 test_256() {
15053         local cl_user
15054         local cat_sl
15055         local mdt_dev
15056
15057         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15058         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15059         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
15060                 skip "ldiskfs only test" && return
15061
15062         mdt_dev=$(mdsdevname 1)
15063         echo $mdt_dev
15064         cl_user=$(do_facet mds1 \
15065         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
15066         if [[ -n $cl_user ]]; then
15067                 skip "active changelog user"
15068                 return
15069         fi
15070
15071         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15072         echo "Registered as changelog user $cl_user"
15073
15074         rm -rf $DIR/$tdir
15075         mkdir -p $DIR/$tdir
15076
15077         $LFS changelog_clear $MDT0 $cl_user 0
15078
15079         # change something
15080         touch $DIR/$tdir/{1..10}
15081
15082         # stop the MDT
15083         stop mds1 || error "Fail to stop MDT."
15084
15085         # remount the MDT
15086         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
15087
15088         #after mount new plainllog is used
15089         touch $DIR/$tdir/{11..19}
15090         do_facet mds1 sync
15091         local TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15092         cat_sl=$(do_facet mds1 \
15093         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15094          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15095         do_facet mds1 rm $TEMP256FILE
15096
15097         if (( cat_sl != 2 )); then
15098                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15099                 error "Changelog catalog has wrong number of slots $cat_sl"
15100         fi
15101
15102         $LFS changelog_clear $MDT0 $cl_user 0
15103
15104         do_facet mds1 sync
15105         TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15106         cat_sl=$(do_facet mds1 \
15107         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15108          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15109         do_facet mds1 rm $TEMP256FILE
15110
15111         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15112
15113         if (( cat_sl == 2 )); then
15114                 error "Empty plain llog was not deleted from changelog catalog"
15115         fi
15116         if (( cat_sl != 1 )); then
15117                 error "Active plain llog shouldn\`t be deleted from catalog"
15118         fi
15119 }
15120 run_test 256 "Check llog delete for empty and not full state"
15121
15122 test_257() {
15123         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15124         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15125                 skip "Need MDS version at least 2.8.55" && return
15126
15127         test_mkdir $DIR/$tdir
15128
15129         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
15130                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
15131         stat $DIR/$tdir
15132
15133 #define OBD_FAIL_MDS_XATTR_REP                  0x161
15134         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15135         local facet=mds$((mdtidx + 1))
15136         set_nodes_failloc $(facet_active_host $facet) 0x80000161
15137         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
15138
15139         stop $facet || error "stop MDS failed"
15140         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
15141                 error "start MDS fail"
15142 }
15143 run_test 257 "xattr locks are not lost"
15144
15145 # Verify we take the i_mutex when security requires it
15146 test_258a() {
15147 #define OBD_FAIL_IMUTEX_SEC 0x141c
15148         $LCTL set_param fail_loc=0x141c
15149         touch $DIR/$tfile
15150         chmod u+s $DIR/$tfile
15151         chmod a+rwx $DIR/$tfile
15152         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15153         RC=$?
15154         if [ $RC -ne 0 ]; then
15155                 error "error, failed to take i_mutex, rc=$?"
15156         fi
15157         rm -f $DIR/$tfile
15158 }
15159 run_test 258a
15160
15161 # Verify we do NOT take the i_mutex in the normal case
15162 test_258b() {
15163 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
15164         $LCTL set_param fail_loc=0x141d
15165         touch $DIR/$tfile
15166         chmod a+rwx $DIR
15167         chmod a+rw $DIR/$tfile
15168         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15169         RC=$?
15170         if [ $RC -ne 0 ]; then
15171                 error "error, took i_mutex unnecessarily, rc=$?"
15172         fi
15173         rm -f $DIR/$tfile
15174
15175 }
15176 run_test 258b "verify i_mutex security behavior"
15177
15178 test_260() {
15179 #define OBD_FAIL_MDC_CLOSE               0x806
15180         $LCTL set_param fail_loc=0x80000806
15181         touch $DIR/$tfile
15182
15183 }
15184 run_test 260 "Check mdc_close fail"
15185
15186 ### Data-on-MDT sanity tests ###
15187 test_270a() {
15188         # create DoM file
15189         local dom=$DIR/$tdir/dom_file
15190         local tmp=$DIR/$tdir/tmp_file
15191
15192         mkdir -p $DIR/$tdir
15193
15194         # basic checks for DoM component creation
15195         $LFS setstripe -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
15196                 error "Can set MDT layout to non-first entry"
15197
15198         $LFS setstripe -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
15199                 error "Can define multiple entries as MDT layout"
15200
15201         $LFS setstripe -E 1M -L mdt $dom ||
15202                 error "Can't create DoM layout"
15203
15204         [ $($LFS getstripe -L $dom) == 100 ] || error "bad pattern"
15205         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
15206         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
15207
15208         local mdtidx=$($GETSTRIPE -M $dom)
15209         local mdtname=MDT$(printf %04x $mdtidx)
15210         local facet=mds$((mdtidx + 1))
15211         local space_check=1
15212
15213         # Skip free space checks with ZFS
15214         if [ "$(facet_fstype $facet)" == "zfs" ]; then
15215                 space_check=0
15216         fi
15217
15218         # write
15219         sync
15220         local mdtfree1=$(do_facet $facet \
15221                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15222         dd if=/dev/urandom of=$tmp bs=1024 count=100
15223         # check also direct IO along write
15224         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
15225         sync
15226         cmp $tmp $dom || error "file data is different"
15227         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
15228         if [ $space_check == 1 ]; then
15229                 local mdtfree2=$(do_facet $facet \
15230                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15231                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15232                         error "MDT free space is wrong after write"
15233         fi
15234
15235         # truncate
15236         $TRUNCATE $dom 10000
15237         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
15238         if [ $space_check == 1 ]; then
15239                 mdtfree1=$(do_facet $facet \
15240                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15241                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
15242                         error "MDT free space is wrong after truncate"
15243         fi
15244
15245         # append
15246         cat $tmp >> $dom
15247         sync
15248         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
15249         if [ $space_check == 1 ]; then
15250                 mdtfree2=$(do_facet $facet \
15251                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15252                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15253                         error "MDT free space is wrong after append"
15254         fi
15255
15256         # delete
15257         rm $dom
15258         if [ $space_check == 1 ]; then
15259                 mdtfree1=$(do_facet $facet \
15260                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15261                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
15262                         error "MDT free space is wrong after removal"
15263         fi
15264
15265         # combined striping
15266         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
15267                 error "Can't create DoM + OST striping"
15268
15269         dd if=/dev/urandom of=$tmp bs=1024 count=2000
15270         # check also direct IO along write
15271         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
15272         sync
15273         cmp $tmp $dom || error "file data is different"
15274         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
15275         rm $dom
15276         rm $tmp
15277
15278         return 0
15279 }
15280 run_test 270a "DoM: basic functionality tests"
15281
15282 test_270b() {
15283         local dom=$DIR/$tdir/dom_file
15284         local max_size=1048576
15285
15286         mkdir -p $DIR/$tdir
15287         $LFS setstripe -E $max_size -L mdt $dom
15288
15289         # truncate over the limit
15290         $TRUNCATE $dom $(($max_size + 1)) &&
15291                 error "successful truncate over the maximum size"
15292         # write over the limit
15293         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
15294                 error "successful write over the maximum size"
15295         # append over the limit
15296         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
15297         echo "12345" >> $dom && error "successful append over the maximum size"
15298         rm $dom
15299
15300         return 0
15301 }
15302 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
15303
15304 test_270c() {
15305         mkdir -p $DIR/$tdir
15306         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15307
15308         # check files inherit DoM EA
15309         touch $DIR/$tdir/first
15310         [ $($GETSTRIPE -L $DIR/$tdir/first) == 100 ] ||
15311                 error "bad pattern"
15312         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
15313                 error "bad stripe count"
15314         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
15315                 error "bad stripe size"
15316
15317         # check directory inherits DoM EA and uses it as default
15318         mkdir $DIR/$tdir/subdir
15319         touch $DIR/$tdir/subdir/second
15320         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == 100 ] ||
15321                 error "bad pattern in sub-directory"
15322         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
15323                 error "bad stripe count in sub-directory"
15324         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
15325                 error "bad stripe size in sub-directory"
15326         return 0
15327 }
15328 run_test 270c "DoM: DoM EA inheritance tests"
15329
15330 test_270d() {
15331         mkdir -p $DIR/$tdir
15332         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15333
15334         # inherit default DoM striping
15335         mkdir $DIR/$tdir/subdir
15336         touch $DIR/$tdir/subdir/f1
15337
15338         # change default directory striping
15339         $LFS setstripe -c 1 $DIR/$tdir/subdir
15340         touch $DIR/$tdir/subdir/f2
15341         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
15342                 error "wrong default striping in file 2"
15343         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == 1 ] ||
15344                 error "bad pattern in file 2"
15345         return 0
15346 }
15347 run_test 270d "DoM: change striping from DoM to RAID0"
15348
15349 test_270e() {
15350         mkdir -p $DIR/$tdir/dom
15351         mkdir -p $DIR/$tdir/norm
15352         DOMFILES=20
15353         NORMFILES=10
15354         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
15355         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
15356
15357         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
15358         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
15359
15360         # find DoM files by layout
15361         NUM=$($LFIND -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
15362         [ $NUM -eq  $DOMFILES ] ||
15363                 error "lfs find -L: found $NUM, expected $DOMFILES"
15364         echo "Test 1: lfs find 20 DOM files by layout: OK"
15365
15366         # there should be 1 dir with default DOM striping
15367         NUM=$($LFIND -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
15368         [ $NUM -eq  1 ] ||
15369                 error "lfs find -L: found $NUM, expected 1 dir"
15370         echo "Test 2: lfs find 1 DOM dir by layout: OK"
15371
15372         # find DoM files by stripe size
15373         NUM=$($LFIND -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
15374         [ $NUM -eq  $DOMFILES ] ||
15375                 error "lfs find -S: found $NUM, expected $DOMFILES"
15376         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
15377
15378         # find files by stripe offset except DoM files
15379         NUM=$($LFIND -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
15380         [ $NUM -eq  $NORMFILES ] ||
15381                 error "lfs find -i: found $NUM, expected $NORMFILES"
15382         echo "Test 5: lfs find no DOM files by stripe index: OK"
15383         return 0
15384 }
15385 run_test 270e "DoM: lfs find with DoM files test"
15386
15387 test_270f() {
15388         local mdtname=${FSNAME}-MDT0000-mdtlov
15389         local dom=$DIR/$tdir/dom_file
15390         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
15391                                                 lod.$mdtname.dom_stripesize)
15392         local dom_limit=131072
15393
15394         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
15395         local dom_current=$(do_facet mds1 $LCTL get_param -n \
15396                                                 lod.$mdtname.dom_stripesize)
15397         [ ${dom_limit} -eq ${dom_current} ] ||
15398                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
15399
15400         $LFS mkdir -i 0 -c 1 $DIR/$tdir
15401         $LFS setstripe -d $DIR/$tdir
15402         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
15403                 error "Can't set directory default striping"
15404
15405         # exceed maximum stripe size
15406         $LFS setstripe -E $(($dom_limit * 2)) -L mdt $dom &&
15407                 error "Able to create DoM component size more than LOD limit"
15408
15409         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
15410         dom_current=$(do_facet mds1 $LCTL get_param -n \
15411                                                 lod.$mdtname.dom_stripesize)
15412         [ 0 -eq ${dom_current} ] ||
15413                 error "Can't set zero DoM stripe limit"
15414
15415         # too low values to be aligned with smallest stripe size 64K
15416         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
15417         dom_current=$(do_facet mds1 $LCTL get_param -n \
15418                                                 lod.$mdtname.dom_stripesize)
15419         [ 30000 -eq ${dom_current} ] &&
15420                 error "Can set too small DoM stripe limit"
15421
15422         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
15423         dom_current=$(do_facet mds1 $LCTL get_param -n \
15424                                                 lod.$mdtname.dom_stripesize)
15425         echo $dom_current
15426         [ 2147483648 -eq ${dom_current} ] &&
15427                 error "Can set too large DoM stripe limit"
15428
15429         do_facet mds1 $LCTL set_param -n \
15430                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
15431         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
15432                 error "Can't create DoM component size after limit change"
15433         do_facet mds1 $LCTL set_param -n \
15434                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
15435         $LFS setstripe -E $dom_limit -L mdt ${dom}_big &&
15436                 error "Can create big DoM component after limit decrease"
15437         touch ${dom}_def ||
15438                 error "Can't create file with old default layout"
15439
15440         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
15441         return 0
15442 }
15443 run_test 270f "DoM: maximum DoM stripe size checks"
15444
15445 test_271a() {
15446         local dom=$DIR/$tdir/dom
15447
15448         mkdir -p $DIR/$tdir
15449
15450         $LFS setstripe -E 1024K -L mdt $dom
15451
15452         lctl set_param -n mdc.*.stats=clear
15453         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
15454         cat $dom > /dev/null
15455         local reads=$(lctl get_param -n mdc.*.stats |
15456                         awk '/ost_read/ {print $2}')
15457         [ -z $reads ] || error "Unexpected $reads READ RPCs"
15458         ls $dom
15459         rm -f $dom
15460 }
15461 run_test 271a "DoM: data is cached for read after write"
15462
15463 test_271b() {
15464         local dom=$DIR/$tdir/dom
15465
15466         mkdir -p $DIR/$tdir
15467
15468         $LFS setstripe -E 1024K -L mdt -E EOF $dom
15469
15470         lctl set_param -n mdc.*.stats=clear
15471         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
15472         cancel_lru_locks mdc
15473         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
15474         # second stat to check size is cached on client
15475         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
15476         local gls=$(lctl get_param -n mdc.*.stats |
15477                         awk '/ldlm_glimpse/ {print $2}')
15478         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
15479         rm -f $dom
15480 }
15481 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
15482
15483 test_271ba() {
15484         local dom=$DIR/$tdir/dom
15485
15486         mkdir -p $DIR/$tdir
15487
15488         $LFS setstripe -E 1024K -L mdt -E EOF $dom
15489
15490         lctl set_param -n mdc.*.stats=clear
15491         lctl set_param -n osc.*.stats=clear
15492         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
15493         cancel_lru_locks mdc
15494         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
15495         # second stat to check size is cached on client
15496         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
15497         local gls=$(lctl get_param -n mdc.*.stats |
15498                         awk '/ldlm_glimpse/ {print $2}')
15499         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
15500         local gls=$(lctl get_param -n osc.*.stats |
15501                         awk '/ldlm_glimpse/ {print $2}')
15502         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
15503         rm -f $dom
15504 }
15505 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
15506
15507 test_271c() {
15508         # test to be enabled with lock_convert
15509         skip "skipped until lock convert will be implemented" && return
15510
15511         local dom=$DIR/$tdir/dom
15512
15513         mkdir -p $DIR/$tdir
15514
15515         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15516
15517         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15518         local facet=mds$((mdtidx + 1))
15519
15520         cancel_lru_locks mdc
15521         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
15522         createmany -o $dom 1000
15523         lctl set_param -n mdc.*.stats=clear
15524         smalliomany -w $dom 1000 200
15525         lctl get_param -n mdc.*.stats
15526         local enq=$(lctl get_param -n mdc.*.stats |
15527                         awk '/ldlm_ibits_enqueue/ {print $2}')
15528         # Each file has 1 open, 1 IO enqueues, total 2000
15529         # but now we have also +1 getxattr for security.capability, total 3000
15530         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
15531         unlinkmany $dom 1000
15532
15533         cancel_lru_locks mdc
15534         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
15535         createmany -o $dom 1000
15536         lctl set_param -n mdc.*.stats=clear
15537         smalliomany -w $dom 1000 200
15538         lctl get_param -n mdc.*.stats
15539         local enq_2=$(lctl get_param -n mdc.*.stats |
15540                         awk '/ldlm_ibits_enqueue/ {print $2}')
15541         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
15542         # for OPEN and IO lock.
15543         [ $((enq - enq_2)) -ge 1000 ] ||
15544                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
15545         unlinkmany $dom 1000
15546         return 0
15547 }
15548 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
15549
15550 cleanup_test_300() {
15551         trap 0
15552         umask $SAVE_UMASK
15553 }
15554 test_striped_dir() {
15555         local mdt_index=$1
15556         local stripe_count
15557         local stripe_index
15558
15559         mkdir -p $DIR/$tdir
15560
15561         SAVE_UMASK=$(umask)
15562         trap cleanup_test_300 RETURN EXIT
15563
15564         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
15565                                                 $DIR/$tdir/striped_dir ||
15566                 error "set striped dir error"
15567
15568         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
15569         [ "$mode" = "755" ] || error "expect 755 got $mode"
15570
15571         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
15572                 error "getdirstripe failed"
15573         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
15574         if [ "$stripe_count" != "2" ]; then
15575                 error "1:stripe_count is $stripe_count, expect 2"
15576         fi
15577         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
15578         if [ "$stripe_count" != "2" ]; then
15579                 error "2:stripe_count is $stripe_count, expect 2"
15580         fi
15581
15582         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
15583         if [ "$stripe_index" != "$mdt_index" ]; then
15584                 error "stripe_index is $stripe_index, expect $mdt_index"
15585         fi
15586
15587         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15588                 error "nlink error after create striped dir"
15589
15590         mkdir $DIR/$tdir/striped_dir/a
15591         mkdir $DIR/$tdir/striped_dir/b
15592
15593         stat $DIR/$tdir/striped_dir/a ||
15594                 error "create dir under striped dir failed"
15595         stat $DIR/$tdir/striped_dir/b ||
15596                 error "create dir under striped dir failed"
15597
15598         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
15599                 error "nlink error after mkdir"
15600
15601         rmdir $DIR/$tdir/striped_dir/a
15602         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
15603                 error "nlink error after rmdir"
15604
15605         rmdir $DIR/$tdir/striped_dir/b
15606         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15607                 error "nlink error after rmdir"
15608
15609         chattr +i $DIR/$tdir/striped_dir
15610         createmany -o $DIR/$tdir/striped_dir/f 10 &&
15611                 error "immutable flags not working under striped dir!"
15612         chattr -i $DIR/$tdir/striped_dir
15613
15614         rmdir $DIR/$tdir/striped_dir ||
15615                 error "rmdir striped dir error"
15616
15617         cleanup_test_300
15618
15619         true
15620 }
15621
15622 test_300a() {
15623         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15624                 skip "skipped for lustre < 2.7.0" && return
15625         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15626         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15627
15628         test_striped_dir 0 || error "failed on striped dir on MDT0"
15629         test_striped_dir 1 || error "failed on striped dir on MDT0"
15630 }
15631 run_test 300a "basic striped dir sanity test"
15632
15633 test_300b() {
15634         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15635                 skip "skipped for lustre < 2.7.0" && return
15636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15637         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15638         local i
15639         local mtime1
15640         local mtime2
15641         local mtime3
15642
15643         test_mkdir $DIR/$tdir
15644         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15645                 error "set striped dir error"
15646         for ((i=0; i<10; i++)); do
15647                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
15648                 sleep 1
15649                 touch $DIR/$tdir/striped_dir/file_$i ||
15650                                         error "touch error $i"
15651                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
15652                 [ $mtime1 -eq $mtime2 ] &&
15653                         error "mtime not change after create"
15654                 sleep 1
15655                 rm -f $DIR/$tdir/striped_dir/file_$i ||
15656                                         error "unlink error $i"
15657                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
15658                 [ $mtime2 -eq $mtime3 ] &&
15659                         error "mtime did not change after unlink"
15660         done
15661         true
15662 }
15663 run_test 300b "check ctime/mtime for striped dir"
15664
15665 test_300c() {
15666         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15667                 skip "skipped for lustre < 2.7.0" && return
15668         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15669         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15670         local file_count
15671
15672         mkdir -p $DIR/$tdir
15673         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
15674                 error "set striped dir error"
15675
15676         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
15677                 error "chown striped dir failed"
15678
15679         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
15680                 error "create 5k files failed"
15681
15682         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
15683
15684         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
15685
15686         rm -rf $DIR/$tdir
15687 }
15688 run_test 300c "chown && check ls under striped directory"
15689
15690 test_300d() {
15691         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15692                 skip "skipped for lustre < 2.7.0" && return
15693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15694         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15695         local stripe_count
15696         local file
15697
15698         mkdir -p $DIR/$tdir
15699         $SETSTRIPE -c 2 $DIR/$tdir
15700
15701         #local striped directory
15702         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15703                 error "set striped dir error"
15704         createmany -o $DIR/$tdir/striped_dir/f 10 ||
15705                 error "create 10 files failed"
15706
15707         #remote striped directory
15708         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
15709                 error "set striped dir error"
15710         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
15711                 error "create 10 files failed"
15712
15713         for file in $(find $DIR/$tdir); do
15714                 stripe_count=$($GETSTRIPE -c $file)
15715                 [ $stripe_count -eq 2 ] ||
15716                         error "wrong stripe $stripe_count for $file"
15717         done
15718
15719         rm -rf $DIR/$tdir
15720 }
15721 run_test 300d "check default stripe under striped directory"
15722
15723 test_300e() {
15724         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15725                 skip "Need MDS version at least 2.7.55" && return
15726         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15727         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15728         local stripe_count
15729         local file
15730
15731         mkdir -p $DIR/$tdir
15732
15733         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15734                 error "set striped dir error"
15735
15736         touch $DIR/$tdir/striped_dir/a
15737         touch $DIR/$tdir/striped_dir/b
15738         touch $DIR/$tdir/striped_dir/c
15739
15740         mkdir $DIR/$tdir/striped_dir/dir_a
15741         mkdir $DIR/$tdir/striped_dir/dir_b
15742         mkdir $DIR/$tdir/striped_dir/dir_c
15743
15744         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
15745                 error "set striped adir under striped dir error"
15746
15747         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
15748                 error "set striped bdir under striped dir error"
15749
15750         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
15751                 error "set striped cdir under striped dir error"
15752
15753         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
15754                 error "rename dir under striped dir fails"
15755
15756         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
15757                 error "rename dir under different stripes fails"
15758
15759         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
15760                 error "rename file under striped dir should succeed"
15761
15762         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
15763                 error "rename dir under striped dir should succeed"
15764
15765         rm -rf $DIR/$tdir
15766 }
15767 run_test 300e "check rename under striped directory"
15768
15769 test_300f() {
15770         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15771                 skip "Need MDS version at least 2.7.55" && return
15772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15773         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15774         local stripe_count
15775         local file
15776
15777         rm -rf $DIR/$tdir
15778         mkdir -p $DIR/$tdir
15779
15780         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15781                 error "set striped dir error"
15782
15783         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
15784                 error "set striped dir error"
15785
15786         touch $DIR/$tdir/striped_dir/a
15787         mkdir $DIR/$tdir/striped_dir/dir_a
15788         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
15789                 error "create striped dir under striped dir fails"
15790
15791         touch $DIR/$tdir/striped_dir1/b
15792         mkdir $DIR/$tdir/striped_dir1/dir_b
15793         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
15794                 error "create striped dir under striped dir fails"
15795
15796         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
15797                 error "rename dir under different striped dir should fail"
15798
15799         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
15800                 error "rename striped dir under diff striped dir should fail"
15801
15802         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
15803                 error "rename file under diff striped dirs fails"
15804
15805         rm -rf $DIR/$tdir
15806 }
15807 run_test 300f "check rename cross striped directory"
15808
15809 test_300_check_default_striped_dir()
15810 {
15811         local dirname=$1
15812         local default_count=$2
15813         local default_index=$3
15814         local stripe_count
15815         local stripe_index
15816         local dir_stripe_index
15817         local dir
15818
15819         echo "checking $dirname $default_count $default_index"
15820         $LFS setdirstripe -D -c $default_count -i $default_index \
15821                                 -t all_char $DIR/$tdir/$dirname ||
15822                 error "set default stripe on striped dir error"
15823         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
15824         [ $stripe_count -eq $default_count ] ||
15825                 error "expect $default_count get $stripe_count for $dirname"
15826
15827         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
15828         [ $stripe_index -eq $default_index ] ||
15829                 error "expect $default_index get $stripe_index for $dirname"
15830
15831         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
15832                                                 error "create dirs failed"
15833
15834         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
15835         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
15836         for dir in $(find $DIR/$tdir/$dirname/*); do
15837                 stripe_count=$($LFS getdirstripe -c $dir)
15838                 [ $stripe_count -eq $default_count ] ||
15839                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
15840                 error "stripe count $default_count != $stripe_count for $dir"
15841
15842                 stripe_index=$($LFS getdirstripe -i $dir)
15843                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
15844                         error "$stripe_index != $default_index for $dir"
15845
15846                 #check default stripe
15847                 stripe_count=$($LFS getdirstripe -D -c $dir)
15848                 [ $stripe_count -eq $default_count ] ||
15849                 error "default count $default_count != $stripe_count for $dir"
15850
15851                 stripe_index=$($LFS getdirstripe -D -i $dir)
15852                 [ $stripe_index -eq $default_index ] ||
15853                 error "default index $default_index != $stripe_index for $dir"
15854         done
15855         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
15856 }
15857
15858 test_300g() {
15859         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15860                 skip "Need MDS version at least 2.7.55" && return
15861         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15862         local dir
15863         local stripe_count
15864         local stripe_index
15865
15866         mkdir $DIR/$tdir
15867         mkdir $DIR/$tdir/normal_dir
15868
15869         #Checking when client cache stripe index
15870         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
15871         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
15872                 error "create striped_dir failed"
15873
15874         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
15875                 error "create dir0 fails"
15876         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
15877         [ $stripe_index -eq 0 ] ||
15878                 error "dir0 expect index 0 got $stripe_index"
15879
15880         mkdir $DIR/$tdir/striped_dir/dir1 ||
15881                 error "create dir1 fails"
15882         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
15883         [ $stripe_index -eq 1 ] ||
15884                 error "dir1 expect index 1 got $stripe_index"
15885
15886         #check default stripe count/stripe index
15887         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
15888         test_300_check_default_striped_dir normal_dir 1 0
15889         test_300_check_default_striped_dir normal_dir 2 1
15890         test_300_check_default_striped_dir normal_dir 2 -1
15891
15892         #delete default stripe information
15893         echo "delete default stripeEA"
15894         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
15895                 error "set default stripe on striped dir error"
15896
15897         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
15898         for dir in $(find $DIR/$tdir/normal_dir/*); do
15899                 stripe_count=$($LFS getdirstripe -c $dir)
15900                 [ $stripe_count -eq 0 ] ||
15901                         error "expect 1 get $stripe_count for $dir"
15902                 stripe_index=$($LFS getdirstripe -i $dir)
15903                 [ $stripe_index -eq 0 ] ||
15904                         error "expect 0 get $stripe_index for $dir"
15905         done
15906 }
15907 run_test 300g "check default striped directory for normal directory"
15908
15909 test_300h() {
15910         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15911                 skip "Need MDS version at least 2.7.55" && return
15912         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15913         local dir
15914         local stripe_count
15915
15916         mkdir $DIR/$tdir
15917         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
15918                                         $DIR/$tdir/striped_dir ||
15919                 error "set striped dir error"
15920
15921         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
15922         test_300_check_default_striped_dir striped_dir 1 0
15923         test_300_check_default_striped_dir striped_dir 2 1
15924         test_300_check_default_striped_dir striped_dir 2 -1
15925
15926         #delete default stripe information
15927         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
15928                 error "set default stripe on striped dir error"
15929
15930         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
15931         for dir in $(find $DIR/$tdir/striped_dir/*); do
15932                 stripe_count=$($LFS getdirstripe -c $dir)
15933                 [ $stripe_count -eq 0 ] ||
15934                         error "expect 1 get $stripe_count for $dir"
15935         done
15936 }
15937 run_test 300h "check default striped directory for striped directory"
15938
15939 test_300i() {
15940         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15941                 skip "Need MDS version at least 2.7.55" && return
15942         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15943         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15944         local stripe_count
15945         local file
15946
15947         mkdir $DIR/$tdir
15948
15949         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15950                 error "set striped dir error"
15951
15952         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15953                 error "create files under striped dir failed"
15954
15955         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
15956                 error "set striped hashdir error"
15957
15958         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
15959                 error "create dir0 under hash dir failed"
15960         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
15961                 error "create dir1 under hash dir failed"
15962
15963         # unfortunately, we need to umount to clear dir layout cache for now
15964         # once we fully implement dir layout, we can drop this
15965         umount_client $MOUNT || error "umount failed"
15966         mount_client $MOUNT || error "mount failed"
15967
15968         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
15969         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
15970         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
15971
15972         #set the stripe to be unknown hash type
15973         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
15974         $LCTL set_param fail_loc=0x1901
15975         for ((i = 0; i < 10; i++)); do
15976                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
15977                         error "stat f-$i failed"
15978                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
15979         done
15980
15981         touch $DIR/$tdir/striped_dir/f0 &&
15982                 error "create under striped dir with unknown hash should fail"
15983
15984         $LCTL set_param fail_loc=0
15985
15986         umount_client $MOUNT || error "umount failed"
15987         mount_client $MOUNT || error "mount failed"
15988
15989         return 0
15990 }
15991 run_test 300i "client handle unknown hash type striped directory"
15992
15993 test_300j() {
15994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15995         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15996                 skip "Need MDS version at least 2.7.55" && return
15997         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15998         local stripe_count
15999         local file
16000
16001         mkdir $DIR/$tdir
16002
16003         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
16004         $LCTL set_param fail_loc=0x1702
16005         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16006                 error "set striped dir error"
16007
16008         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16009                 error "create files under striped dir failed"
16010
16011         $LCTL set_param fail_loc=0
16012
16013         rm -rf $DIR/$tdir || error "unlink striped dir fails"
16014
16015         return 0
16016 }
16017 run_test 300j "test large update record"
16018
16019 test_300k() {
16020         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16021                 skip "Need MDS version at least 2.7.55" && return
16022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16023         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16024         local stripe_count
16025         local file
16026
16027         mkdir $DIR/$tdir
16028
16029         #define OBD_FAIL_LARGE_STRIPE   0x1703
16030         $LCTL set_param fail_loc=0x1703
16031         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
16032                 error "set striped dir error"
16033         $LCTL set_param fail_loc=0
16034
16035         $LFS getdirstripe $DIR/$tdir/striped_dir ||
16036                 error "getstripeddir fails"
16037         rm -rf $DIR/$tdir/striped_dir ||
16038                 error "unlink striped dir fails"
16039
16040         return 0
16041 }
16042 run_test 300k "test large striped directory"
16043
16044 test_300l() {
16045         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16046                 skip "Need MDS version at least 2.7.55" && return
16047         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16048         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16049         local stripe_index
16050
16051         test_mkdir -p $DIR/$tdir/striped_dir
16052         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
16053                         error "chown $RUNAS_ID failed"
16054         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
16055                 error "set default striped dir failed"
16056
16057         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
16058         $LCTL set_param fail_loc=0x80000158
16059         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
16060
16061         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
16062         [ $stripe_index -eq 1 ] ||
16063                 error "expect 1 get $stripe_index for $dir"
16064 }
16065 run_test 300l "non-root user to create dir under striped dir with stale layout"
16066
16067 test_300m() {
16068         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16069                 skip "Need MDS version at least 2.7.55" && return
16070         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16071         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
16072
16073         mkdir -p $DIR/$tdir/striped_dir
16074         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
16075                 error "set default stripes dir error"
16076
16077         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
16078
16079         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
16080         [ $stripe_count -eq 0 ] ||
16081                         error "expect 0 get $stripe_count for a"
16082
16083         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
16084                 error "set default stripes dir error"
16085
16086         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
16087
16088         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
16089         [ $stripe_count -eq 0 ] ||
16090                         error "expect 0 get $stripe_count for b"
16091
16092         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
16093                 error "set default stripes dir error"
16094
16095         mkdir $DIR/$tdir/striped_dir/c &&
16096                 error "default stripe_index is invalid, mkdir c should fails"
16097
16098         rm -rf $DIR/$tdir || error "rmdir fails"
16099 }
16100 run_test 300m "setstriped directory on single MDT FS"
16101
16102 cleanup_300n() {
16103         local list=$(comma_list $(mdts_nodes))
16104
16105         trap 0
16106         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16107 }
16108
16109 test_300n() {
16110         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16111                 skip "Need MDS version at least 2.7.55" && return
16112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16113         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16114         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16115
16116         local stripe_index
16117         local list=$(comma_list $(mdts_nodes))
16118
16119         trap cleanup_300n RETURN EXIT
16120         mkdir -p $DIR/$tdir
16121         chmod 777 $DIR/$tdir
16122         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
16123                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16124                 error "create striped dir succeeds with gid=0"
16125
16126         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16127         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
16128                 error "create striped dir fails with gid=-1"
16129
16130         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16131         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
16132                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16133                 error "set default striped dir succeeds with gid=0"
16134
16135
16136         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16137         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
16138                 error "set default striped dir fails with gid=-1"
16139
16140
16141         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16142         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
16143                                         error "create test_dir fails"
16144         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
16145                                         error "create test_dir1 fails"
16146         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
16147                                         error "create test_dir2 fails"
16148         cleanup_300n
16149 }
16150 run_test 300n "non-root user to create dir under striped dir with default EA"
16151
16152 test_300o() {
16153         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16154                 skip "Need MDS version at least 2.7.55" && return
16155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16156         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16157         local numfree1
16158         local numfree2
16159
16160         mkdir -p $DIR/$tdir
16161
16162         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
16163         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
16164         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
16165                 skip "not enough free inodes $numfree1 $numfree2"
16166                 return
16167         fi
16168
16169         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
16170         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
16171         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
16172                 skip "not enough free space $numfree1 $numfree2"
16173                 return
16174         fi
16175
16176         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
16177                 error "setdirstripe fails"
16178
16179         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
16180                 error "create dirs fails"
16181
16182         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
16183         ls $DIR/$tdir/striped_dir > /dev/null ||
16184                 error "ls striped dir fails"
16185         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
16186                 error "unlink big striped dir fails"
16187 }
16188 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
16189
16190 test_300p() {
16191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16192         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16193         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16194
16195         mkdir -p $DIR/$tdir
16196
16197         #define OBD_FAIL_OUT_ENOSPC     0x1704
16198         do_facet mds2 lctl set_param fail_loc=0x80001704
16199         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
16200                         error "create striped directory should fail"
16201
16202         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
16203
16204         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
16205         true
16206 }
16207 run_test 300p "create striped directory without space"
16208
16209 test_300q() {
16210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16211         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16212
16213         local fd=$(free_fd)
16214         local cmd="exec $fd<$tdir"
16215         cd $DIR
16216         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
16217         eval $cmd
16218         cmd="exec $fd<&-"
16219         trap "eval $cmd" EXIT
16220         cd $tdir || error "cd $tdir fails"
16221         rmdir  ../$tdir || error "rmdir $tdir fails"
16222         mkdir local_dir && error "create dir succeeds"
16223         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
16224         eval $cmd
16225         return 0
16226 }
16227 run_test 300q "create remote directory under orphan directory"
16228
16229 prepare_remote_file() {
16230         mkdir $DIR/$tdir/src_dir ||
16231                 error "create remote source failed"
16232
16233         cp /etc/hosts $DIR/$tdir/src_dir/a || error
16234         touch $DIR/$tdir/src_dir/a
16235
16236         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
16237                 error "create remote target dir failed"
16238
16239         touch $DIR/$tdir/tgt_dir/b
16240
16241         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
16242                 error "rename dir cross MDT failed!"
16243
16244         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
16245                 error "src_child still exists after rename"
16246
16247         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
16248                 error "missing file(a) after rename"
16249
16250         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
16251                 error "diff after rename"
16252 }
16253
16254 test_310a() {
16255         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16257         local remote_file=$DIR/$tdir/tgt_dir/b
16258
16259         mkdir -p $DIR/$tdir
16260
16261         prepare_remote_file || error "prepare remote file failed"
16262
16263         #open-unlink file
16264         $OPENUNLINK $remote_file $remote_file || error
16265         $CHECKSTAT -a $remote_file || error
16266 }
16267 run_test 310a "open unlink remote file"
16268
16269 test_310b() {
16270         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16272         local remote_file=$DIR/$tdir/tgt_dir/b
16273
16274         mkdir -p $DIR/$tdir
16275
16276         prepare_remote_file || error "prepare remote file failed"
16277
16278         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16279         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
16280         $CHECKSTAT -t file $remote_file || error "check file failed"
16281 }
16282 run_test 310b "unlink remote file with multiple links while open"
16283
16284 test_310c() {
16285         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
16286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16287         local remote_file=$DIR/$tdir/tgt_dir/b
16288
16289         mkdir -p $DIR/$tdir
16290
16291         prepare_remote_file || error "prepare remote file failed"
16292
16293         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16294         multiop_bg_pause $remote_file O_uc ||
16295                         error "mulitop failed for remote file"
16296         MULTIPID=$!
16297         $MULTIOP $DIR/$tfile Ouc
16298         kill -USR1 $MULTIPID
16299         wait $MULTIPID
16300 }
16301 run_test 310c "open-unlink remote file with multiple links"
16302
16303 #LU-4825
16304 test_311() {
16305         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16306                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16307         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16308         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16309
16310         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16311
16312         mkdir -p $DIR/$tdir
16313         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16314         createmany -o $DIR/$tdir/$tfile. 1000
16315
16316         # statfs data is not real time, let's just calculate it
16317         old_iused=$((old_iused + 1000))
16318
16319         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16320                         osp.*OST0000*MDT0000.create_count")
16321         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16322                                 osp.*OST0000*MDT0000.max_create_count")
16323         for idx in $(seq $MDSCOUNT); do
16324                 do_facet mds$idx "lctl set_param -n \
16325                         osp.*OST0000*MDT000?.max_create_count=0"
16326         done
16327
16328         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16329         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16330         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16331
16332         unlinkmany $DIR/$tdir/$tfile. 1000
16333
16334         for idx in $(seq $MDSCOUNT); do
16335                 do_facet mds$idx "lctl set_param -n \
16336                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16337                 do_facet mds$idx "lctl set_param -n \
16338                         osp.*OST0000*MDT000?.create_count=$count"
16339         done
16340
16341         local new_iused
16342         for i in $(seq 120); do
16343                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16344                 # system may be too busy to destroy all objs in time, use
16345                 # a somewhat small value to not fail autotest
16346                 [ $((old_iused - new_iused)) -gt 400 ] && break
16347                 sleep 1
16348         done
16349
16350         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16351         [ $((old_iused - new_iused)) -gt 400 ] ||
16352                 error "objs not destroyed after unlink"
16353 }
16354 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16355
16356 zfs_oid_to_objid()
16357 {
16358         local ost=$1
16359         local objid=$2
16360
16361         local vdevdir=$(dirname $(facet_vdevice $ost))
16362         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
16363         local zfs_zapid=$(do_facet $ost $cmd |
16364                           grep -w "/O/0/d$((objid%32))" -C 5 |
16365                           awk '/Object/{getline; print $1}')
16366         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
16367                           awk "/$objid = /"'{printf $3}')
16368
16369         echo $zfs_objid
16370 }
16371
16372 zfs_object_blksz() {
16373         local ost=$1
16374         local objid=$2
16375
16376         local vdevdir=$(dirname $(facet_vdevice $ost))
16377         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
16378         local blksz=$(do_facet $ost $cmd $objid |
16379                       awk '/dblk/{getline; printf $4}')
16380
16381         case "${blksz: -1}" in
16382                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
16383                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
16384                 *) ;;
16385         esac
16386
16387         echo $blksz
16388 }
16389
16390 test_312() { # LU-4856
16391         remote_ost_nodsh && skip "remote OST with nodsh" && return
16392
16393         [ $(facet_fstype ost1) = "zfs" ] ||
16394                 { skip "the test only applies to zfs" && return; }
16395
16396         local max_blksz=$(do_facet ost1 \
16397                           $ZFS get -p recordsize $(facet_device ost1) |
16398                           awk '!/VALUE/{print $3}')
16399         local min_blksz=$(getconf PAGE_SIZE)
16400
16401         # to make life a little bit easier
16402         $LFS mkdir -c 1 -i 0 $DIR/$tdir
16403         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16404
16405         local tf=$DIR/$tdir/$tfile
16406         touch $tf
16407         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16408
16409         # Get ZFS object id
16410         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16411
16412         # block size change by sequential over write
16413         local blksz
16414         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
16415                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
16416
16417                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16418                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
16419         done
16420         rm -f $tf
16421
16422         # block size change by sequential append write
16423         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
16424         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16425         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16426
16427         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
16428                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
16429                         oflag=sync conv=notrunc
16430
16431                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16432                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
16433                         error "blksz error, actual $blksz, "    \
16434                                 "expected: 2 * $count * $min_blksz"
16435         done
16436         rm -f $tf
16437
16438         # random write
16439         touch $tf
16440         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16441         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16442
16443         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
16444         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16445         [ $blksz -eq $min_blksz ] ||
16446                 error "blksz error: $blksz, expected: $min_blksz"
16447
16448         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
16449         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16450         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
16451
16452         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
16453         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16454         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
16455 }
16456 run_test 312 "make sure ZFS adjusts its block size by write pattern"
16457
16458 test_313() {
16459         remote_ost_nodsh && skip "remote OST with nodsh" && return
16460
16461         local file=$DIR/$tfile
16462         rm -f $file
16463         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
16464
16465         # define OBD_FAIL_TGT_RCVD_EIO           0x720
16466         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16467         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
16468                 error "write should failed"
16469         do_facet ost1 "$LCTL set_param fail_loc=0"
16470         rm -f $file
16471 }
16472 run_test 313 "io should fail after last_rcvd update fail"
16473
16474 test_fake_rw() {
16475         local read_write=$1
16476         if [ "$read_write" = "write" ]; then
16477                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
16478         elif [ "$read_write" = "read" ]; then
16479                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
16480         else
16481                 error "argument error"
16482         fi
16483
16484         # turn off debug for performance testing
16485         local saved_debug=$($LCTL get_param -n debug)
16486         $LCTL set_param debug=0
16487
16488         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
16489
16490         # get ost1 size - lustre-OST0000
16491         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
16492         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
16493         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
16494
16495         if [ "$read_write" = "read" ]; then
16496                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
16497         fi
16498
16499         local start_time=$(date +%s.%N)
16500         $dd_cmd bs=1M count=$blocks oflag=sync ||
16501                 error "real dd $read_write error"
16502         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
16503
16504         if [ "$read_write" = "write" ]; then
16505                 rm -f $DIR/$tfile
16506         fi
16507
16508         # define OBD_FAIL_OST_FAKE_RW           0x238
16509         do_facet ost1 $LCTL set_param fail_loc=0x238
16510
16511         local start_time=$(date +%s.%N)
16512         $dd_cmd bs=1M count=$blocks oflag=sync ||
16513                 error "fake dd $read_write error"
16514         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
16515
16516         if [ "$read_write" = "write" ]; then
16517                 # verify file size
16518                 cancel_lru_locks osc
16519                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
16520                         error "$tfile size not $blocks MB"
16521         fi
16522         do_facet ost1 $LCTL set_param fail_loc=0
16523
16524         echo "fake $read_write $duration_fake vs. normal $read_write" \
16525                 "$duration in seconds"
16526         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
16527                 error_not_in_vm "fake write is slower"
16528
16529         $LCTL set_param -n debug="$saved_debug"
16530         rm -f $DIR/$tfile
16531 }
16532 test_399a() { # LU-7655 for OST fake write
16533         remote_ost_nodsh && skip "remote OST with nodsh" && return
16534
16535         test_fake_rw write
16536 }
16537 run_test 399a "fake write should not be slower than normal write"
16538
16539 test_399b() { # LU-8726 for OST fake read
16540         remote_ost_nodsh && skip "remote OST with nodsh" && return
16541
16542         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
16543                 skip "ldiskfs only test" && return 0
16544         fi
16545         test_fake_rw read
16546 }
16547 run_test 399b "fake read should not be slower than normal read"
16548
16549 test_400a() { # LU-1606, was conf-sanity test_74
16550         local extra_flags=''
16551         local out=$TMP/$tfile
16552         local prefix=/usr/include/lustre
16553         local prog
16554
16555         if ! which $CC > /dev/null 2>&1; then
16556                 skip_env "$CC is not installed"
16557                 return 0
16558         fi
16559
16560         if ! [[ -d $prefix ]]; then
16561                 # Assume we're running in tree and fixup the include path.
16562                 extra_flags+=" -I$LUSTRE/include"
16563                 extra_flags+=" -L$LUSTRE/utils"
16564         fi
16565
16566         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
16567                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
16568                         error "client api broken"
16569         done
16570         rm -f $out
16571 }
16572 run_test 400a "Lustre client api program can compile and link"
16573
16574 test_400b() { # LU-1606, LU-5011
16575         local header
16576         local out=$TMP/$tfile
16577         local prefix=/usr/include/linux/lustre
16578
16579         # We use a hard coded prefix so that this test will not fail
16580         # when run in tree. There are headers in lustre/include/lustre/
16581         # that are not packaged (like lustre_idl.h) and have more
16582         # complicated include dependencies (like config.h and lnet/types.h).
16583         # Since this test about correct packaging we just skip them when
16584         # they don't exist (see below) rather than try to fixup cppflags.
16585
16586         if ! which $CC > /dev/null 2>&1; then
16587                 skip_env "$CC is not installed"
16588                 return 0
16589         fi
16590
16591         for header in $prefix/*.h; do
16592                 if ! [[ -f "$header" ]]; then
16593                         continue
16594                 fi
16595
16596                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
16597                         continue # lustre_ioctl.h is internal header
16598                 fi
16599
16600                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
16601                         error "cannot compile '$header'"
16602         done
16603         rm -f $out
16604 }
16605 run_test 400b "packaged headers can be compiled"
16606
16607 test_401a() { #LU-7437
16608         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
16609         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
16610                 return
16611         #count the number of parameters by "list_param -R"
16612         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
16613         #count the number of parameters by listing proc files
16614         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
16615         echo "proc_dirs='$proc_dirs'"
16616         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
16617         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
16618                       sort -u | wc -l)
16619
16620         [ $params -eq $procs ] ||
16621                 error "found $params parameters vs. $procs proc files"
16622
16623         # test the list_param -D option only returns directories
16624         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
16625         #count the number of parameters by listing proc directories
16626         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
16627                 sort -u | wc -l)
16628
16629         [ $params -eq $procs ] ||
16630                 error "found $params parameters vs. $procs proc files"
16631 }
16632 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
16633
16634 test_401b() {
16635         local save=$($LCTL get_param -n jobid_var)
16636         local tmp=testing
16637
16638         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
16639                 error "no error returned when setting bad parameters"
16640
16641         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
16642         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
16643
16644         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
16645         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
16646         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
16647 }
16648 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
16649
16650 test_401c() {
16651         local jobid_var_old=$($LCTL get_param -n jobid_var)
16652         local jobid_var_new
16653
16654         $LCTL set_param jobid_var= &&
16655                 error "no error returned for 'set_param a='"
16656
16657         jobid_var_new=$($LCTL get_param -n jobid_var)
16658         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16659                 error "jobid_var was changed by setting without value"
16660
16661         $LCTL set_param jobid_var &&
16662                 error "no error returned for 'set_param a'"
16663
16664         jobid_var_new=$($LCTL get_param -n jobid_var)
16665         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16666                 error "jobid_var was changed by setting without value"
16667 }
16668 run_test 401c "Verify 'lctl set_param' without value fails in either format."
16669
16670 test_401d() {
16671         local jobid_var_old=$($LCTL get_param -n jobid_var)
16672         local jobid_var_new
16673         local new_value="foo=bar"
16674
16675         $LCTL set_param jobid_var=$new_value ||
16676                 error "'set_param a=b' did not accept a value containing '='"
16677
16678         jobid_var_new=$($LCTL get_param -n jobid_var)
16679         [[ "$jobid_var_new" == "$new_value" ]] ||
16680                 error "'set_param a=b' failed on a value containing '='"
16681
16682         # Reset the jobid_var to test the other format
16683         $LCTL set_param jobid_var=$jobid_var_old
16684         jobid_var_new=$($LCTL get_param -n jobid_var)
16685         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16686                 error "failed to reset jobid_var"
16687
16688         $LCTL set_param jobid_var $new_value ||
16689                 error "'set_param a b' did not accept a value containing '='"
16690
16691         jobid_var_new=$($LCTL get_param -n jobid_var)
16692         [[ "$jobid_var_new" == "$new_value" ]] ||
16693                 error "'set_param a b' failed on a value containing '='"
16694
16695         $LCTL set_param jobid_var $jobid_var_old
16696         jobid_var_new=$($LCTL get_param -n jobid_var)
16697         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16698                 error "failed to reset jobid_var"
16699 }
16700 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
16701
16702 test_402() {
16703         local server_version=$(lustre_version_code $SINGLEMDS)
16704         [[ $server_version -ge $(version_code 2.7.66) ]] ||
16705         [[ $server_version -ge $(version_code 2.7.18.4) &&
16706                 $server_version -lt $(version_code 2.7.50) ]] ||
16707         [[ $server_version -ge $(version_code 2.7.2) &&
16708                 $server_version -lt $(version_code 2.7.11) ]] ||
16709                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
16710                         return; }
16711         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16712         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
16713 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
16714         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
16715         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
16716                 echo "Touch failed - OK"
16717 }
16718 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
16719
16720 test_403() {
16721         local file1=$DIR/$tfile.1
16722         local file2=$DIR/$tfile.2
16723         local tfile=$TMP/$tfile
16724
16725         rm -f $file1 $file2 $tfile
16726
16727         touch $file1
16728         ln $file1 $file2
16729
16730         # 30 sec OBD_TIMEOUT in ll_getattr()
16731         # right before populating st_nlink
16732         $LCTL set_param fail_loc=0x80001409
16733         stat -c %h $file1 > $tfile &
16734
16735         # create an alias, drop all locks and reclaim the dentry
16736         < $file2
16737         cancel_lru_locks mdc
16738         cancel_lru_locks osc
16739         sysctl -w vm.drop_caches=2
16740
16741         wait
16742
16743         [ `cat $tfile` -gt 0 ] || error "wrong nlink count: `cat $tfile`"
16744
16745         rm -f $tfile $file1 $file2
16746 }
16747 run_test 403 "i_nlink should not drop to zero due to aliasing"
16748
16749 test_404() { # LU-6601
16750         local server_version=$(lustre_version_code $SINGLEMDS)
16751         [[ $server_version -ge $(version_code 2.8.53) ]] ||
16752                 { skip "Need server version newer than 2.8.52"; return 0; }
16753
16754         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16755         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
16756                 awk '/osp .*-osc-MDT/ { print $4}')
16757
16758         local osp
16759         for osp in $mosps; do
16760                 echo "Deactivate: " $osp
16761                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
16762                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16763                         awk -vp=$osp '$4 == p { print $2 }')
16764                 [ $stat = IN ] || {
16765                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16766                         error "deactivate error"
16767                 }
16768                 echo "Activate: " $osp
16769                 do_facet $SINGLEMDS $LCTL --device %$osp activate
16770                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16771                         awk -vp=$osp '$4 == p { print $2 }')
16772                 [ $stat = UP ] || {
16773                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16774                         error "activate error"
16775                 }
16776         done
16777 }
16778 run_test 404 "validate manual {de}activated works properly for OSPs"
16779
16780 test_405() {
16781         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
16782         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
16783                 skip "Layout swap lock is not supported" && return
16784
16785         check_swap_layouts_support && return 0
16786
16787         test_mkdir $DIR/$tdir
16788         swap_lock_test -d $DIR/$tdir ||
16789                 error "One layout swap locked test failed"
16790 }
16791 run_test 405 "Various layout swap lock tests"
16792
16793 test_406() {
16794         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16795         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16796         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
16797         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16798         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
16799                 skip "Need MDS version at least 2.8.50" && return
16800
16801         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
16802         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
16803         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
16804         local def_pool=$($GETSTRIPE -p $MOUNT)
16805         local test_pool=$TESTNAME
16806
16807         if ! combined_mgs_mds ; then
16808                 mount_mgs_client
16809         fi
16810         pool_add $test_pool || error "pool_add failed"
16811         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
16812                 error "pool_add_targets failed"
16813
16814         # parent set default stripe count only, child will stripe from both
16815         # parent and fs default
16816         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
16817                 error "setstripe $MOUNT failed"
16818         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
16819         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
16820         for i in $(seq 10); do
16821                 local f=$DIR/$tdir/$tfile.$i
16822                 touch $f || error "touch failed"
16823                 local count=$($GETSTRIPE -c $f)
16824                 [ $count -eq $OSTCOUNT ] ||
16825                         error "$f stripe count $count != $OSTCOUNT"
16826                 local offset=$($GETSTRIPE -i $f)
16827                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
16828                 local size=$($GETSTRIPE -S $f)
16829                 [ $size -eq $((def_stripe_size * 2)) ] ||
16830                         error "$f stripe size $size != $((def_stripe_size * 2))"
16831                 local pool=$($GETSTRIPE -p $f)
16832                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
16833         done
16834
16835         # change fs default striping, delete parent default striping, now child
16836         # will stripe from new fs default striping only
16837         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
16838                 error "change $MOUNT default stripe failed"
16839         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
16840         for i in $(seq 11 20); do
16841                 local f=$DIR/$tdir/$tfile.$i
16842                 touch $f || error "touch $f failed"
16843                 local count=$($GETSTRIPE -c $f)
16844                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
16845                 local offset=$($GETSTRIPE -i $f)
16846                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
16847                 local size=$($GETSTRIPE -S $f)
16848                 [ $size -eq $def_stripe_size ] ||
16849                         error "$f stripe size $size != $def_stripe_size"
16850                 local pool=$($GETSTRIPE -p $f)
16851                 [ "#$pool" == "#" ] || error "$f pool $pool is set"
16852
16853         done
16854
16855         unlinkmany $DIR/$tdir/$tfile. 1 20
16856
16857         # restore FS default striping
16858         if [ -z $def_pool ]; then
16859                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16860                         -i $def_stripe_offset $MOUNT ||
16861                         error "restore default striping failed"
16862         else
16863                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16864                         -i $def_stripe_offset -p $def_pool $MOUNT ||
16865                         error "restore default striping with $def_pool failed"
16866         fi
16867
16868         local f=$DIR/$tdir/$tfile
16869         pool_remove_all_targets $test_pool $f
16870         pool_remove $test_pool $f
16871
16872         if ! combined_mgs_mds ; then
16873                 umount_mgs_client
16874         fi
16875 }
16876 run_test 406 "DNE support fs default striping"
16877
16878 test_407() {
16879         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16880         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
16881                 skip "Need MDS version at least 2.8.55" && return
16882         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16883
16884         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
16885                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
16886         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
16887                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
16888         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
16889
16890         #define OBD_FAIL_DT_TXN_STOP    0x2019
16891         for idx in $(seq $MDSCOUNT); do
16892                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
16893         done
16894         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
16895         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
16896                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
16897         true
16898 }
16899 run_test 407 "transaction fail should cause operation fail"
16900
16901 test_408() {
16902         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
16903
16904         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
16905         lctl set_param fail_loc=0x8000040a
16906         # let ll_prepare_partial_page() fail
16907         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
16908
16909         rm -f $DIR/$tfile
16910
16911         # create at least 100 unused inodes so that
16912         # shrink_icache_memory(0) should not return 0
16913         touch $DIR/$tfile-{0..100}
16914         rm -f $DIR/$tfile-{0..100}
16915         sync
16916
16917         echo 2 > /proc/sys/vm/drop_caches
16918 }
16919 run_test 408 "drop_caches should not hang due to page leaks"
16920
16921 test_409()
16922 {
16923         [ $MDSCOUNT -lt 2 ] &&
16924                 skip "We need at least 2 MDTs for this test" && return
16925
16926         check_mount_and_prep
16927
16928         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
16929         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
16930         touch $DIR/$tdir/guard || error "(2) Fail to create"
16931
16932         local PREFIX=$(str_repeat 'A' 128)
16933         echo "Create 1K hard links start at $(date)"
16934         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16935                 error "(3) Fail to hard link"
16936
16937         echo "Links count should be right although linkEA overflow"
16938         stat $DIR/$tdir/guard || error "(4) Fail to stat"
16939         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
16940         [ $linkcount -eq 1001 ] ||
16941                 error "(5) Unexpected hard links count: $linkcount"
16942
16943         echo "List all links start at $(date)"
16944         ls -l $DIR/$tdir/foo > /dev/null ||
16945                 error "(6) Fail to list $DIR/$tdir/foo"
16946
16947         echo "Unlink hard links start at $(date)"
16948         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16949                 error "(7) Fail to unlink"
16950 }
16951 run_test 409 "Large amount of cross-MDTs hard links on the same file"
16952
16953 test_410()
16954 {
16955         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
16956                 skip "Need client version at least 2.9.59" && return
16957
16958         # Create a file, and stat it from the kernel
16959         local testfile=$DIR/$tfile
16960         touch $testfile
16961
16962         local run_id=$RANDOM
16963         local my_ino=$(stat --format "%i" $testfile)
16964
16965         # Try to insert the module. This will always fail as the
16966         # module is designed to not be inserted.
16967         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
16968             &> /dev/null
16969
16970         # Anything but success is a test failure
16971         dmesg | grep -q \
16972             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
16973             error "no inode match"
16974 }
16975 run_test 410 "Test inode number returned from kernel thread"
16976
16977 cleanup_test411_cgroup() {
16978         trap 0
16979         rmdir "$1"
16980 }
16981
16982 test_411() {
16983         local cg_basedir=/sys/fs/cgroup/memory
16984         # LU-9966
16985         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
16986                 { skip "no setup for cgroup"; return; }
16987
16988         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
16989                 error "test file creation failed"
16990         cancel_lru_locks osc
16991
16992         # Create a very small memory cgroup to force a slab allocation error
16993         local cgdir=$cg_basedir/osc_slab_alloc
16994         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
16995         trap "cleanup_test411_cgroup $cgdir" EXIT
16996         echo 2M > $cgdir/memory.kmem.limit_in_bytes
16997         echo 1M > $cgdir/memory.limit_in_bytes
16998
16999         # Should not LBUG, just be killed by oom-killer
17000         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
17001                 error "fail to trigger a memory allocation error"
17002
17003         cleanup_test411_cgroup $cgdir
17004
17005         return 0
17006 }
17007 run_test 411 "Slab allocation error with cgroup does not LBUG"
17008
17009 prep_801() {
17010         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17011         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17012                 skip "Need server version at least 2.9.55" & exit 0
17013         start_full_debug_logging
17014 }
17015
17016 post_801() {
17017         stop_full_debug_logging
17018 }
17019
17020 barrier_stat() {
17021         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17022                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17023                            awk '/The barrier for/ { print $7 }')
17024                 echo $st
17025         else
17026                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
17027                 echo \'$st\'
17028         fi
17029 }
17030
17031 barrier_expired() {
17032         local expired
17033
17034         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17035                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17036                           awk '/will be expired/ { print $7 }')
17037         else
17038                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
17039         fi
17040
17041         echo $expired
17042 }
17043
17044 test_801a() {
17045         prep_801
17046
17047         echo "Start barrier_freeze at: $(date)"
17048         #define OBD_FAIL_BARRIER_DELAY          0x2202
17049         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17050         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
17051
17052         sleep 2
17053         local b_status=$(barrier_stat)
17054         echo "Got barrier status at: $(date)"
17055         [ "$b_status" = "'freezing_p1'" ] ||
17056                 error "(1) unexpected barrier status $b_status"
17057
17058         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17059         wait
17060         b_status=$(barrier_stat)
17061         [ "$b_status" = "'frozen'" ] ||
17062                 error "(2) unexpected barrier status $b_status"
17063
17064         local expired=$(barrier_expired)
17065         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
17066         sleep $((expired + 3))
17067
17068         b_status=$(barrier_stat)
17069         [ "$b_status" = "'expired'" ] ||
17070                 error "(3) unexpected barrier status $b_status"
17071
17072         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
17073                 error "(4) fail to freeze barrier"
17074
17075         b_status=$(barrier_stat)
17076         [ "$b_status" = "'frozen'" ] ||
17077                 error "(5) unexpected barrier status $b_status"
17078
17079         echo "Start barrier_thaw at: $(date)"
17080         #define OBD_FAIL_BARRIER_DELAY          0x2202
17081         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17082         do_facet mgs $LCTL barrier_thaw $FSNAME &
17083
17084         sleep 2
17085         b_status=$(barrier_stat)
17086         echo "Got barrier status at: $(date)"
17087         [ "$b_status" = "'thawing'" ] ||
17088                 error "(6) unexpected barrier status $b_status"
17089
17090         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17091         wait
17092         b_status=$(barrier_stat)
17093         [ "$b_status" = "'thawed'" ] ||
17094                 error "(7) unexpected barrier status $b_status"
17095
17096         #define OBD_FAIL_BARRIER_FAILURE        0x2203
17097         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
17098         do_facet mgs $LCTL barrier_freeze $FSNAME
17099
17100         b_status=$(barrier_stat)
17101         [ "$b_status" = "'failed'" ] ||
17102                 error "(8) unexpected barrier status $b_status"
17103
17104         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17105         do_facet mgs $LCTL barrier_thaw $FSNAME
17106
17107         post_801
17108 }
17109 run_test 801a "write barrier user interfaces and stat machine"
17110
17111 test_801b() {
17112         prep_801
17113
17114         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17115         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
17116         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
17117         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
17118         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
17119
17120         cancel_lru_locks mdc
17121
17122         # 180 seconds should be long enough
17123         do_facet mgs $LCTL barrier_freeze $FSNAME 180
17124
17125         local b_status=$(barrier_stat)
17126         [ "$b_status" = "'frozen'" ] ||
17127                 error "(6) unexpected barrier status $b_status"
17128
17129         mkdir $DIR/$tdir/d0/d10 &
17130         mkdir_pid=$!
17131
17132         touch $DIR/$tdir/d1/f13 &
17133         touch_pid=$!
17134
17135         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
17136         ln_pid=$!
17137
17138         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
17139         mv_pid=$!
17140
17141         rm -f $DIR/$tdir/d4/f12 &
17142         rm_pid=$!
17143
17144         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
17145
17146         # To guarantee taht the 'stat' is not blocked
17147         b_status=$(barrier_stat)
17148         [ "$b_status" = "'frozen'" ] ||
17149                 error "(8) unexpected barrier status $b_status"
17150
17151         # let above commands to run at background
17152         sleep 5
17153
17154         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
17155         ps -p $touch_pid || error "(10) touch should be blocked"
17156         ps -p $ln_pid || error "(11) link should be blocked"
17157         ps -p $mv_pid || error "(12) rename should be blocked"
17158         ps -p $rm_pid || error "(13) unlink should be blocked"
17159
17160         b_status=$(barrier_stat)
17161         [ "$b_status" = "'frozen'" ] ||
17162                 error "(14) unexpected barrier status $b_status"
17163
17164         do_facet mgs $LCTL barrier_thaw $FSNAME
17165         b_status=$(barrier_stat)
17166         [ "$b_status" = "'thawed'" ] ||
17167                 error "(15) unexpected barrier status $b_status"
17168
17169         wait $mkdir_pid || error "(16) mkdir should succeed"
17170         wait $touch_pid || error "(17) touch should succeed"
17171         wait $ln_pid || error "(18) link should succeed"
17172         wait $mv_pid || error "(19) rename should succeed"
17173         wait $rm_pid || error "(20) unlink should succeed"
17174
17175         post_801
17176 }
17177 run_test 801b "modification will be blocked by write barrier"
17178
17179 test_801c() {
17180         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17181
17182         prep_801
17183
17184         stop mds2 || error "(1) Fail to stop mds2"
17185
17186         do_facet mgs $LCTL barrier_freeze $FSNAME 30
17187
17188         local b_status=$(barrier_stat)
17189         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
17190                 do_facet mgs $LCTL barrier_thaw $FSNAME
17191                 error "(2) unexpected barrier status $b_status"
17192         }
17193
17194         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17195                 error "(3) Fail to rescan barrier bitmap"
17196
17197         do_facet mgs $LCTL barrier_freeze $FSNAME 10
17198
17199         b_status=$(barrier_stat)
17200         [ "$b_status" = "'frozen'" ] ||
17201                 error "(4) unexpected barrier status $b_status"
17202
17203         do_facet mgs $LCTL barrier_thaw $FSNAME
17204         b_status=$(barrier_stat)
17205         [ "$b_status" = "'thawed'" ] ||
17206                 error "(5) unexpected barrier status $b_status"
17207
17208         local devname=$(mdsdevname 2)
17209
17210         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
17211
17212         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17213                 error "(7) Fail to rescan barrier bitmap"
17214
17215         post_801
17216 }
17217 run_test 801c "rescan barrier bitmap"
17218
17219 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
17220 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
17221 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
17222
17223 cleanup_802() {
17224         trap 0
17225
17226         stopall
17227         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
17228         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
17229         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
17230         setupall
17231 }
17232
17233 test_802() {
17234
17235         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17236         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17237                 skip "Need server version at least 2.9.55" & exit 0
17238
17239         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17240
17241         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
17242                 error "(2) Fail to copy"
17243
17244         trap cleanup_802 EXIT
17245
17246         # sync by force before remount as readonly
17247         sync; sync_all_data; sleep 3; sync_all_data
17248
17249         stopall
17250
17251         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
17252         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
17253         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
17254
17255         echo "Mount the server as read only"
17256         setupall server_only || error "(3) Fail to start servers"
17257
17258         echo "Mount client without ro should fail"
17259         mount_client $MOUNT &&
17260                 error "(4) Mount client without 'ro' should fail"
17261
17262         echo "Mount client with ro should succeed"
17263         mount_client $MOUNT ro ||
17264                 error "(5) Mount client with 'ro' should succeed"
17265
17266         echo "Modify should be refused"
17267         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
17268
17269         echo "Read should be allowed"
17270         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
17271                 error "(7) Read should succeed under ro mode"
17272
17273         cleanup_802
17274 }
17275 run_test 802 "simulate readonly device"
17276
17277 #
17278 # tests that do cleanup/setup should be run at the end
17279 #
17280
17281 test_900() {
17282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17283         local ls
17284         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
17285         $LCTL set_param fail_loc=0x903
17286
17287         cancel_lru_locks MGC
17288
17289         FAIL_ON_ERROR=true cleanup
17290         FAIL_ON_ERROR=true setup
17291 }
17292 run_test 900 "umount should not race with any mgc requeue thread"
17293
17294 complete $SECONDS
17295 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
17296 check_and_cleanup_lustre
17297 if [ "$I_MOUNTED" != "yes" ]; then
17298         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
17299 fi
17300 exit_status