Whamcloud - gitweb
66a32d0467c85555dca71393e8e42151c3291400
[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
12 ALWAYS_EXCEPT="$SANITY_EXCEPT  42a     42b     42c"
13 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
14
15 # skipped tests: LU-8411 LU-9096 LU-9054
16 ALWAYS_EXCEPT="  407     253     312 $ALWAYS_EXCEPT"
17
18 # Check Grants after these tests
19 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c"
20
21 SRCDIR=$(cd $(dirname $0); echo $PWD)
22 export PATH=$PATH:/sbin
23
24 TMP=${TMP:-/tmp}
25
26 CC=${CC:-cc}
27 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
28 CREATETEST=${CREATETEST:-createtest}
29 LFS=${LFS:-lfs}
30 LFIND=${LFIND:-"$LFS find"}
31 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
32 LCTL=${LCTL:-lctl}
33 OPENFILE=${OPENFILE:-openfile}
34 OPENUNLINK=${OPENUNLINK:-openunlink}
35 export MULTIOP=${MULTIOP:-multiop}
36 READS=${READS:-"reads"}
37 MUNLINK=${MUNLINK:-munlink}
38 SOCKETSERVER=${SOCKETSERVER:-socketserver}
39 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
40 MEMHOG=${MEMHOG:-memhog}
41 DIRECTIO=${DIRECTIO:-directio}
42 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
43 STRIPES_PER_OBJ=-1
44 CHECK_GRANT=${CHECK_GRANT:-"yes"}
45 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
46 export PARALLEL=${PARALLEL:-"no"}
47
48 export NAME=${NAME:-local}
49
50 SAVE_PWD=$PWD
51
52 CLEANUP=${CLEANUP:-:}
53 SETUP=${SETUP:-:}
54 TRACE=${TRACE:-""}
55 LUSTRE=${LUSTRE:-$(cd $(dirname $0)/..; echo $PWD)}
56 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
57 . $LUSTRE/tests/test-framework.sh
58 init_test_env $@
59 . ${CONFIG:=$LUSTRE/tests/cfg/${NAME}.sh}
60 init_logging
61
62 #                                  5          12          (min)"
63 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 300o"
64
65 if [ $(facet_fstype $SINGLEMDS) = "zfs" ]; then
66         # bug number for skipped test: LU-4536 LU-1957
67         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  65ic    180"
68         #                                               13    (min)"
69         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
70 fi
71
72
73 is_sles11()                                             # LU-4341
74 {
75         if [ -r /etc/SuSE-release ]
76         then
77                 local vers=$(grep VERSION /etc/SuSE-release | awk '{print $3}')
78                 local patchlev=$(grep PATCHLEVEL /etc/SuSE-release |
79                         awk '{ print $3 }')
80                 if [ $vers -eq 11 ] && [ $patchlev -ge 3 ]; then
81                         return 0
82                 fi
83         fi
84         return 1
85 }
86
87 # Check if we are running on Ubuntu or SLES so we can make decisions on
88 # what tests to run
89 if is_sles11; then
90         # bug number for skipped test: LU-4341
91         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
92 elif [ -r /etc/os-release ]; then
93         if grep -qi ubuntu /etc/os-release; then
94                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
95                                                 -e 's/^VERSION=//p' \
96                                                 /etc/os-release |
97                                                 awk '{ print $1 }'))
98
99                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
100                         # bug number for skipped test:
101                         #                LU-10334 LU-10335 LU-10335 LU-10335
102                         ALWAYS_EXCEPT+=" 103a     130a     130b     130c"
103                         #                LU-10335 LU-10335 LU-10365 LU-10366
104                         ALWAYS_EXCEPT+=" 130d     130e     400a     410"
105                 fi
106         fi
107 fi
108
109 FAIL_ON_ERROR=false
110
111 cleanup() {
112         echo -n "cln.."
113         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
114         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
115 }
116 setup() {
117         echo -n "mnt.."
118         load_modules
119         setupall || exit 10
120         echo "done"
121 }
122
123 check_swap_layouts_support()
124 {
125         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
126                 { skip "Does not support layout lock."; return 0; }
127         return 1
128 }
129
130 check_and_setup_lustre
131 DIR=${DIR:-$MOUNT}
132 assert_DIR
133
134 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
135         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
136 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
137
138 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
139 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
140 rm -rf $DIR/[Rdfs][0-9]*
141
142 # $RUNAS_ID may get set incorrectly somewhere else
143 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
144
145 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
146
147 build_test_filter
148
149 if [ "${ONLY}" = "MOUNT" ] ; then
150         echo "Lustre is up, please go on"
151         exit
152 fi
153
154 echo "preparing for tests involving mounts"
155 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
156 touch $EXT2_DEV
157 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
158 echo # add a newline after mke2fs.
159
160 umask 077
161
162 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
163 lctl set_param debug=-1 2> /dev/null || true
164 test_0a() {
165         touch $DIR/$tfile
166         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
167         rm $DIR/$tfile
168         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
169 }
170 run_test 0a "touch; rm ====================="
171
172 test_0b() {
173         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
174         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
175 }
176 run_test 0b "chmod 0755 $DIR ============================="
177
178 test_0c() {
179         $LCTL get_param mdc.*.import | grep "state: FULL" ||
180                 error "import not FULL"
181         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
182                 error "bad target"
183 }
184 run_test 0c "check import proc ============================="
185
186 test_1() {
187         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
188         test_mkdir -p $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
189         test_mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
190         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
191         rmdir $DIR/$tdir/d2
192         rmdir $DIR/$tdir
193         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
194 }
195 run_test 1 "mkdir; remkdir; rmdir =============================="
196
197 test_2() {
198         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
199         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
200         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
201         rm -r $DIR/$tdir
202         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
203 }
204 run_test 2 "mkdir; touch; rmdir; check file ===================="
205
206 test_3() {
207         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
208         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
209         touch $DIR/$tdir/$tfile
210         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
211         rm -r $DIR/$tdir
212         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
213 }
214 run_test 3 "mkdir; touch; rmdir; check dir ====================="
215
216 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
217 test_4() {
218         local MDTIDX=1
219
220         test_mkdir $DIR/$tdir ||
221                 error "Create remote directory failed"
222
223         touch $DIR/$tdir/$tfile ||
224                 error "Create file under remote directory failed"
225
226         rmdir $DIR/$tdir &&
227                 error "Expect error removing in-use dir $DIR/$tdir"
228
229         test -d $DIR/$tdir || error "Remote directory disappeared"
230
231         rm -rf $DIR/$tdir || error "remove remote dir error"
232 }
233 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
234
235 test_5() {
236         test_mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
237         test_mkdir $DIR/$tdir/d2 || error "mkdir $tdir/d2 failed"
238         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
239         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
240         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
241 }
242 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2 ============"
243
244 test_6a() {
245         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
246         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
247         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
248                 error "$tfile does not have perm 0666 or UID $UID"
249         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
250         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
251                 error "$tfile should be 0666 and owned by UID $UID"
252 }
253 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
254
255 test_6c() {
256         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
257         touch $DIR/$tfile
258         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
259         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
260                 error "$tfile should be owned by UID $RUNAS_ID"
261         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
262         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
263                 error "$tfile should be owned by UID $RUNAS_ID"
264 }
265 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
266
267 test_6e() {
268         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
269         touch $DIR/$tfile
270         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
271         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
272                 error "$tfile should be owned by GID $UID"
273         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
274         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
275                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
276 }
277 run_test 6e "touch f6e; chgrp f6e; $RUNAS chgrp f6e (should return error) =="
278
279 test_6g() {
280         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
281         test_mkdir $DIR/$tdir || error "mkdir $tfile failed"
282         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
283         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
284         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
285         test_mkdir $DIR/$tdir/d/subdir || error "mkdir $tdir/d/subdir failed"
286         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
287                 error "$tdir/d/subdir should be GID $RUNAS_GID"
288         if [[ $MDSCOUNT -gt 1 ]]; then
289                 # check remote dir sgid inherite
290                 $LFS mkdir -i 0 $DIR/$tdir.local ||
291                         error "mkdir $tdir.local failed"
292                 chmod g+s $DIR/$tdir.local ||
293                         error "chmod $tdir.local failed"
294                 chgrp $RUNAS_GID $DIR/$tdir.local ||
295                         error "chgrp $tdir.local failed"
296                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
297                         error "mkdir $tdir.remote failed"
298                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
299                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
300                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
301                         error "$tdir.remote should be mode 02755"
302         fi
303 }
304 run_test 6g "Is new dir in sgid dir inheriting group?"
305
306 test_6h() { # bug 7331
307         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
308         touch $DIR/$tfile || error "touch failed"
309         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
310         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
311                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
312         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
313                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
314 }
315 run_test 6h "$RUNAS chown RUNAS_ID.0 .../f6h (should return error)"
316
317 test_7a() {
318         test_mkdir $DIR/$tdir
319         $MCREATE $DIR/$tdir/$tfile
320         chmod 0666 $DIR/$tdir/$tfile
321         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
322                 error "$tdir/$tfile should be mode 0666"
323 }
324 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
325
326 test_7b() {
327         if [ ! -d $DIR/$tdir ]; then
328                 test_mkdir $DIR/$tdir
329         fi
330         $MCREATE $DIR/$tdir/$tfile
331         echo -n foo > $DIR/$tdir/$tfile
332         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
333         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
334 }
335 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
336
337 test_8() {
338         test_mkdir $DIR/$tdir
339         touch $DIR/$tdir/$tfile
340         chmod 0666 $DIR/$tdir/$tfile
341         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
342                 error "$tfile mode not 0666"
343 }
344 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
345
346 test_9() {
347         test_mkdir $DIR/$tdir
348         test_mkdir $DIR/$tdir/d2
349         test_mkdir $DIR/$tdir/d2/d3
350         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
351 }
352 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
353
354 test_10() {
355         test_mkdir $DIR/$tdir
356         test_mkdir $DIR/$tdir/d2
357         touch $DIR/$tdir/d2/$tfile
358         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
359                 error "$tdir/d2/$tfile not a file"
360 }
361 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
362
363 test_11() {
364         test_mkdir $DIR/$tdir
365         test_mkdir $DIR/$tdir/d2
366         chmod 0666 $DIR/$tdir/d2
367         chmod 0705 $DIR/$tdir/d2
368         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
369                 error "$tdir/d2 mode not 0705"
370 }
371 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
372
373 test_12() {
374         test_mkdir $DIR/$tdir
375         touch $DIR/$tdir/$tfile
376         chmod 0666 $DIR/$tdir/$tfile
377         chmod 0654 $DIR/$tdir/$tfile
378         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
379                 error "$tdir/d2 mode not 0654"
380 }
381 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
382
383 test_13() {
384         test_mkdir $DIR/$tdir
385         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
386         >  $DIR/$tdir/$tfile
387         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
388                 error "$tdir/$tfile size not 0 after truncate"
389 }
390 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
391
392 test_14() {
393         test_mkdir $DIR/$tdir
394         touch $DIR/$tdir/$tfile
395         rm $DIR/$tdir/$tfile
396         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
397 }
398 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
399
400 test_15() {
401         test_mkdir $DIR/$tdir
402         touch $DIR/$tdir/$tfile
403         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
404         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
405                 error "$tdir/${tfile_2} not a file after rename"
406         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
407 }
408 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
409
410 test_16() {
411         test_mkdir $DIR/$tdir
412         touch $DIR/$tdir/$tfile
413         rm -rf $DIR/$tdir/$tfile
414         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
415 }
416 run_test 16 "touch .../d16/f; rm -rf .../d16/f ================="
417
418 test_17a() {
419         test_mkdir -p $DIR/$tdir
420         touch $DIR/$tdir/$tfile
421         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
422         ls -l $DIR/$tdir
423         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
424                 error "$tdir/l-exist not a symlink"
425         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
426                 error "$tdir/l-exist not referencing a file"
427         rm -f $DIR/$tdir/l-exist
428         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
429 }
430 run_test 17a "symlinks: create, remove (real) =================="
431
432 test_17b() {
433         test_mkdir -p $DIR/$tdir
434         ln -s no-such-file $DIR/$tdir/l-dangle
435         ls -l $DIR/$tdir
436         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
437                 error "$tdir/l-dangle not referencing no-such-file"
438         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
439                 error "$tdir/l-dangle not referencing non-existent file"
440         rm -f $DIR/$tdir/l-dangle
441         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
442 }
443 run_test 17b "symlinks: create, remove (dangling) =============="
444
445 test_17c() { # bug 3440 - don't save failed open RPC for replay
446         test_mkdir -p $DIR/$tdir
447         ln -s foo $DIR/$tdir/$tfile
448         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
449 }
450 run_test 17c "symlinks: open dangling (should return error) ===="
451
452 test_17d() {
453         test_mkdir -p $DIR/$tdir
454         ln -s foo $DIR/$tdir/$tfile
455         touch $DIR/$tdir/$tfile || error "creating to new symlink"
456 }
457 run_test 17d "symlinks: create dangling ========================"
458
459 test_17e() {
460         test_mkdir -p $DIR/$tdir
461         local foo=$DIR/$tdir/$tfile
462         ln -s $foo $foo || error "create symlink failed"
463         ls -l $foo || error "ls -l failed"
464         ls $foo && error "ls not failed" || true
465 }
466 run_test 17e "symlinks: create recursive symlink (should return error) ===="
467
468 test_17f() {
469         test_mkdir -p $DIR/$tdir
470         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
471         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
472         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
473         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
474         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
475         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
476         ls -l  $DIR/$tdir
477 }
478 run_test 17f "symlinks: long and very long symlink name ========================"
479
480 # str_repeat(S, N) generate a string that is string S repeated N times
481 str_repeat() {
482         local s=$1
483         local n=$2
484         local ret=''
485         while [ $((n -= 1)) -ge 0 ]; do
486                 ret=$ret$s
487         done
488         echo $ret
489 }
490
491 # Long symlinks and LU-2241
492 test_17g() {
493         test_mkdir -p $DIR/$tdir
494         local TESTS="59 60 61 4094 4095"
495
496         # Fix for inode size boundary in 2.1.4
497         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
498                 TESTS="4094 4095"
499
500         # Patch not applied to 2.2 or 2.3 branches
501         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
502         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
503                 TESTS="4094 4095"
504
505         # skip long symlink name for rhel6.5.
506         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
507         grep -q '6.5' /etc/redhat-release &>/dev/null &&
508                 TESTS="59 60 61 4062 4063"
509
510         for i in $TESTS; do
511                 local SYMNAME=$(str_repeat 'x' $i)
512                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
513                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
514         done
515 }
516 run_test 17g "symlinks: really long symlink name and inode boundaries"
517
518 test_17h() { #bug 17378
519         remote_mds_nodsh && skip "remote MDS with nodsh" && return
520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
521         local mdt_idx
522         test_mkdir -p $DIR/$tdir
523         if [[ $MDSCOUNT -gt 1 ]]; then
524                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
525         else
526                 mdt_idx=0
527         fi
528         $SETSTRIPE -c -1 $DIR/$tdir
529 #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
530         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
531         touch $DIR/$tdir/$tfile || true
532 }
533 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
534
535 test_17i() { #bug 20018
536         remote_mds_nodsh && skip "remote MDS with nodsh" && return
537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
538         test_mkdir -c1 $DIR/$tdir
539         local foo=$DIR/$tdir/$tfile
540         local mdt_idx
541         if [[ $MDSCOUNT -gt 1 ]]; then
542                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
543         else
544                 mdt_idx=0
545         fi
546         ln -s $foo $foo || error "create symlink failed"
547 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
548         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
549         ls -l $foo && error "error not detected"
550         return 0
551 }
552 run_test 17i "don't panic on short symlink"
553
554 test_17k() { #bug 22301
555         [[ -z "$(which rsync 2>/dev/null)" ]] &&
556                 skip "no rsync command" && return 0
557         rsync --help | grep -q xattr ||
558                 skip_env "$(rsync --version | head -n1) does not support xattrs"
559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
560         test_mkdir -p $DIR/$tdir
561         test_mkdir -p $DIR/$tdir.new
562         touch $DIR/$tdir/$tfile
563         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
564         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
565                 error "rsync failed with xattrs enabled"
566 }
567 run_test 17k "symlinks: rsync with xattrs enabled ========================="
568
569 test_17l() { # LU-279
570         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
571                 skip "no getfattr command" && return 0
572         test_mkdir -p $DIR/$tdir
573         touch $DIR/$tdir/$tfile
574         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
575         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
576                 # -h to not follow symlinks. -m '' to list all the xattrs.
577                 # grep to remove first line: '# file: $path'.
578                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
579                 do
580                         lgetxattr_size_check $path $xattr ||
581                                 error "lgetxattr_size_check $path $xattr failed"
582                 done
583         done
584 }
585 run_test 17l "Ensure lgetxattr's returned xattr size is consistent ========"
586
587 # LU-1540
588 test_17m() {
589         local short_sym="0123456789"
590         local WDIR=$DIR/${tdir}m
591         local i
592
593         remote_mds_nodsh && skip "remote MDS with nodsh" && return
594         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
595         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
596                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
597
598         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
599                 skip "ldiskfs only test" && return 0
600
601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
602
603         test_mkdir -p $WDIR
604         long_sym=$short_sym
605         # create a long symlink file
606         for ((i = 0; i < 4; ++i)); do
607                 long_sym=${long_sym}${long_sym}
608         done
609
610         echo "create 512 short and long symlink files under $WDIR"
611         for ((i = 0; i < 256; ++i)); do
612                 ln -sf ${long_sym}"a5a5" $WDIR/long-$i
613                 ln -sf ${short_sym}"a5a5" $WDIR/short-$i
614         done
615
616         echo "erase them"
617         rm -f $WDIR/*
618         sync
619         wait_delete_completed
620
621         echo "recreate the 512 symlink files with a shorter string"
622         for ((i = 0; i < 512; ++i)); do
623                 # rewrite the symlink file with a shorter string
624                 ln -sf ${long_sym} $WDIR/long-$i || error "long_sym failed"
625                 ln -sf ${short_sym} $WDIR/short-$i || error "short_sym failed"
626         done
627
628         local mds_index=$(($($LFS getstripe -M $WDIR) + 1))
629         local devname=$(mdsdevname $mds_index)
630
631         echo "stop and checking mds${mds_index}:"
632         # e2fsck should not return error
633         stop mds${mds_index}
634         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
635         rc=$?
636
637         start mds${mds_index} $devname $MDS_MOUNT_OPTS || error "start failed"
638         df $MOUNT > /dev/null 2>&1
639         [ $rc -eq 0 ] ||
640                 error "e2fsck detected error for short/long symlink: rc=$rc"
641 }
642 run_test 17m "run e2fsck against MDT which contains short/long symlink"
643
644 check_fs_consistency_17n() {
645         local mdt_index
646         local rc=0
647
648         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
649         # so it only check MDT1/MDT2 instead of all of MDTs.
650         for mdt_index in 1 2; do
651                 local devname=$(mdsdevname $mdt_index)
652                 # e2fsck should not return error
653                 stop mds${mdt_index}
654                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
655                         rc=$((rc + $?))
656
657                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
658                         error "mount mds$mdt_index failed"
659                 df $MOUNT > /dev/null 2>&1
660         done
661         return $rc
662 }
663
664 test_17n() {
665         local i
666
667         remote_mds_nodsh && skip "remote MDS with nodsh" && return
668         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
669         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
670                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
671
672         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
673                 skip "ldiskfs only test" && return 0
674
675         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
676
677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
678
679         test_mkdir $DIR/$tdir
680         for ((i=0; i<10; i++)); do
681                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
682                         error "create remote dir error $i"
683                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
684                         error "create files under remote dir failed $i"
685         done
686
687         check_fs_consistency_17n ||
688                 error "e2fsck report error after create files under remote dir"
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 ||
696                 error "e2fsck report error after unlink files under remote dir"
697
698         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
699                 skip "lustre < 2.4.50 does not support migrate mv " && return
700
701         for ((i = 0; i < 10; i++)); do
702                 mkdir -p $DIR/$tdir/remote_dir_${i}
703                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
704                         error "create files under remote dir failed $i"
705                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
706                         error "migrate remote dir error $i"
707         done
708         check_fs_consistency_17n || error "e2fsck report error after migration"
709
710         for ((i = 0; i < 10; i++)); do
711                 rm -rf $DIR/$tdir/remote_dir_${i} ||
712                         error "destroy remote dir error $i"
713         done
714
715         check_fs_consistency_17n || error "e2fsck report error after unlink"
716 }
717 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
718
719 test_17o() {
720         remote_mds_nodsh && skip "remote MDS with nodsh" && return
721         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
722                 skip "Need MDS version at least 2.3.64" && return
723
724         local WDIR=$DIR/${tdir}o
725         local mdt_index
726         local rc=0
727
728         test_mkdir -p $WDIR
729         touch $WDIR/$tfile
730         mdt_index=$($LFS getstripe -M $WDIR/$tfile)
731         mdt_index=$((mdt_index+1))
732
733         cancel_lru_locks mdc
734         #fail mds will wait the failover finish then set
735         #following fail_loc to avoid interfer the recovery process.
736         fail mds${mdt_index}
737
738         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
739         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
740         ls -l $WDIR/$tfile && rc=1
741         do_facet mds${mdt_index} lctl set_param fail_loc=0
742         [[ $rc -ne 0 ]] && error "stat file should fail"
743         true
744 }
745 run_test 17o "stat file with incompat LMA feature"
746
747 test_18() {
748         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
749         ls $DIR || error "Failed to ls $DIR: $?"
750 }
751 run_test 18 "touch .../f ; ls ... =============================="
752
753 test_19a() {
754         touch $DIR/$tfile
755         ls -l $DIR
756         rm $DIR/$tfile
757         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
758 }
759 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
760
761 test_19b() {
762         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
763 }
764 run_test 19b "ls -l .../f19 (should return error) =============="
765
766 test_19c() {
767         [ $RUNAS_ID -eq $UID ] &&
768                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
769         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
770 }
771 run_test 19c "$RUNAS touch .../f19 (should return error) =="
772
773 test_19d() {
774         cat $DIR/f19 && error || true
775 }
776 run_test 19d "cat .../f19 (should return error) =============="
777
778 test_20() {
779         touch $DIR/$tfile
780         rm $DIR/$tfile
781         touch $DIR/$tfile
782         rm $DIR/$tfile
783         touch $DIR/$tfile
784         rm $DIR/$tfile
785         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
786 }
787 run_test 20 "touch .../f ; ls -l ... ==========================="
788
789 test_21() {
790         test_mkdir -p $DIR/$tdir
791         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
792         ln -s dangle $DIR/$tdir/link
793         echo foo >> $DIR/$tdir/link
794         cat $DIR/$tdir/dangle
795         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
796         $CHECKSTAT -f -t file $DIR/$tdir/link ||
797                 error "$tdir/link not linked to a file"
798 }
799 run_test 21 "write to dangling link ============================"
800
801 test_22() {
802         WDIR=$DIR/$tdir
803         test_mkdir -p $DIR/$tdir
804         chown $RUNAS_ID:$RUNAS_GID $WDIR
805         (cd $WDIR || error "cd $WDIR failed";
806         $RUNAS tar cf - /etc/hosts /etc/sysconfig/network | \
807         $RUNAS tar xf -)
808         ls -lR $WDIR/etc || error "ls -lR $WDIR/etc failed"
809         $CHECKSTAT -t dir $WDIR/etc || error "checkstat -t dir failed"
810         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $WDIR/etc || error "checkstat -u failed"
811 }
812 run_test 22 "unpack tar archive as non-root user ==============="
813
814 # was test_23
815 test_23a() {
816         test_mkdir -p $DIR/$tdir
817         local file=$DIR/$tdir/$tfile
818
819         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
820         openfile -f O_CREAT:O_EXCL $file &&
821                 error "$file recreate succeeded" || true
822 }
823 run_test 23a "O_CREAT|O_EXCL in subdir =========================="
824
825 test_23b() { # bug 18988
826         test_mkdir -p $DIR/$tdir
827         local file=$DIR/$tdir/$tfile
828
829         rm -f $file
830         echo foo > $file || error "write filed"
831         echo bar >> $file || error "append filed"
832         $CHECKSTAT -s 8 $file || error "wrong size"
833         rm $file
834 }
835 run_test 23b "O_APPEND check =========================="
836
837 # LU-11069 file offset is correct after appending writes
838 test_23d() {
839         local file=$DIR/$tfile
840         local offset
841
842         echo CentaurHauls > $file
843         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
844         if ((offset != 26)); then
845                 error "wrong offset, expected 26, got '$offset'"
846         fi
847 }
848 run_test 23d "file offset is correct after appending writes"
849
850 # rename sanity
851 test_24a() {
852         echo '-- same directory rename'
853         test_mkdir $DIR/$tdir
854         touch $DIR/$tdir/$tfile.1
855         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
856         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
857 }
858 run_test 24a "rename file to non-existent target"
859
860 test_24b() {
861         test_mkdir $DIR/$tdir
862         touch $DIR/$tdir/$tfile.{1,2}
863         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
864         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
865         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
866 }
867 run_test 24b "rename file to existing target"
868
869 test_24c() {
870         test_mkdir $DIR/$tdir
871         test_mkdir $DIR/$tdir/d$testnum.1
872         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
873         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
874         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
875 }
876 run_test 24c "rename directory to non-existent target"
877
878 test_24d() {
879         test_mkdir -c1 $DIR/$tdir
880         test_mkdir -c1 $DIR/$tdir/d$testnum.1
881         test_mkdir -c1 $DIR/$tdir/d$testnum.2
882         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
883         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
884         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
885 }
886 run_test 24d "rename directory to existing target"
887
888 test_24e() {
889         echo '-- cross directory renames --'
890         test_mkdir $DIR/R5a
891         test_mkdir $DIR/R5b
892         touch $DIR/R5a/f
893         mv $DIR/R5a/f $DIR/R5b/g
894         $CHECKSTAT -a $DIR/R5a/f || error
895         $CHECKSTAT -t file $DIR/R5b/g || error
896 }
897 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
898
899 test_24f() {
900         test_mkdir $DIR/R6a
901         test_mkdir $DIR/R6b
902         touch $DIR/R6a/f $DIR/R6b/g
903         mv $DIR/R6a/f $DIR/R6b/g
904         $CHECKSTAT -a $DIR/R6a/f || error
905         $CHECKSTAT -t file $DIR/R6b/g || error
906 }
907 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
908
909 test_24g() {
910         test_mkdir $DIR/R7a
911         test_mkdir $DIR/R7b
912         test_mkdir $DIR/R7a/d
913         mv $DIR/R7a/d $DIR/R7b/e
914         $CHECKSTAT -a $DIR/R7a/d || error
915         $CHECKSTAT -t dir $DIR/R7b/e || error
916 }
917 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
918
919 test_24h() {
920         test_mkdir -c1 $DIR/R8a
921         test_mkdir -c1 $DIR/R8b
922         test_mkdir -c1 $DIR/R8a/d
923         test_mkdir -c1 $DIR/R8b/e
924         mrename $DIR/R8a/d $DIR/R8b/e
925         $CHECKSTAT -a $DIR/R8a/d || error
926         $CHECKSTAT -t dir $DIR/R8b/e || error
927 }
928 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
929
930 test_24i() {
931         echo "-- rename error cases"
932         test_mkdir $DIR/R9
933         test_mkdir $DIR/R9/a
934         touch $DIR/R9/f
935         mrename $DIR/R9/f $DIR/R9/a
936         $CHECKSTAT -t file $DIR/R9/f || error
937         $CHECKSTAT -t dir  $DIR/R9/a || error
938         $CHECKSTAT -a $DIR/R9/a/f || error
939 }
940 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
941
942 test_24j() {
943         test_mkdir $DIR/R10
944         mrename $DIR/R10/f $DIR/R10/g
945         $CHECKSTAT -t dir $DIR/R10 || error
946         $CHECKSTAT -a $DIR/R10/f || error
947         $CHECKSTAT -a $DIR/R10/g || error
948 }
949 run_test 24j "source does not exist ============================"
950
951 test_24k() {
952         test_mkdir $DIR/R11a
953         test_mkdir $DIR/R11a/d
954         touch $DIR/R11a/f
955         mv $DIR/R11a/f $DIR/R11a/d
956         $CHECKSTAT -a $DIR/R11a/f || error
957         $CHECKSTAT -t file $DIR/R11a/d/f || error
958 }
959 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
960
961 # bug 2429 - rename foo foo foo creates invalid file
962 test_24l() {
963         f="$DIR/f24l"
964         $MULTIOP $f OcNs || error
965 }
966 run_test 24l "Renaming a file to itself ========================"
967
968 test_24m() {
969         f="$DIR/f24m"
970         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
971         # on ext3 this does not remove either the source or target files
972         # though the "expected" operation would be to remove the source
973         $CHECKSTAT -t file ${f} || error "${f} missing"
974         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
975 }
976 run_test 24m "Renaming a file to a hard link to itself ========="
977
978 test_24n() {
979     f="$DIR/f24n"
980     # this stats the old file after it was renamed, so it should fail
981     touch ${f}
982     $CHECKSTAT ${f}
983     mv ${f} ${f}.rename
984     $CHECKSTAT ${f}.rename
985     $CHECKSTAT -a ${f}
986 }
987 run_test 24n "Statting the old file after renaming (Posix rename 2)"
988
989 test_24o() {
990         test_mkdir -p $DIR/d24o
991         rename_many -s random -v -n 10 $DIR/d24o
992 }
993 run_test 24o "rename of files during htree split ==============="
994
995 test_24p() {
996         test_mkdir $DIR/R12a
997         test_mkdir $DIR/R12b
998         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
999         mrename $DIR/R12a $DIR/R12b
1000         $CHECKSTAT -a $DIR/R12a || error
1001         $CHECKSTAT -t dir $DIR/R12b || error
1002         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1003         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1004 }
1005 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1006
1007 cleanup_multiop_pause() {
1008         trap 0
1009         kill -USR1 $MULTIPID
1010 }
1011
1012 test_24q() {
1013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1014         test_mkdir $DIR/R13a
1015         test_mkdir $DIR/R13b
1016         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1017         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1018         MULTIPID=$!
1019
1020         trap cleanup_multiop_pause EXIT
1021         mrename $DIR/R13a $DIR/R13b
1022         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1023         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1024         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1025         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1026         cleanup_multiop_pause
1027         wait $MULTIPID || error "multiop close failed"
1028 }
1029 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1030
1031 test_24r() { #bug 3789
1032         test_mkdir $DIR/R14a
1033         test_mkdir $DIR/R14a/b
1034         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1035         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1036         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1037 }
1038 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1039
1040 test_24s() {
1041         test_mkdir $DIR/R15a
1042         test_mkdir $DIR/R15a/b
1043         test_mkdir $DIR/R15a/b/c
1044         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1045         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1046         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1047 }
1048 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1049 test_24t() {
1050         test_mkdir $DIR/R16a
1051         test_mkdir $DIR/R16a/b
1052         test_mkdir $DIR/R16a/b/c
1053         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1054         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1055         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1056 }
1057 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1058
1059 test_24u() { # bug12192
1060         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1061         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1062 }
1063 run_test 24u "create stripe file"
1064
1065 page_size() {
1066         local size
1067         size=$(getconf PAGE_SIZE 2>/dev/null)
1068         echo -n ${size:-4096}
1069 }
1070
1071 simple_cleanup_common() {
1072         local rc=0
1073         trap 0
1074         [ -z "$DIR" -o -z "$tdir" ] && return 0
1075
1076         local start=$SECONDS
1077         rm -rf $DIR/$tdir
1078         rc=$?
1079         wait_delete_completed
1080         echo "cleanup time $((SECONDS - start))"
1081         return $rc
1082 }
1083
1084 max_pages_per_rpc() {
1085         local mdtname="$(printf "MDT%04x" ${1:-0})"
1086         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1087 }
1088
1089 test_24v() {
1090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1091         local nrfiles=${COUNT:-100000}
1092         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1093         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && nrfiles=${COUNT:-10000}
1094
1095         local fname="$DIR/$tdir/$tfile"
1096         test_mkdir "$(dirname $fname)"
1097         # assume MDT0000 has the fewest inodes
1098         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1099         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1100         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1101
1102         trap simple_cleanup_common EXIT
1103
1104         createmany -m "$fname" $nrfiles
1105
1106         cancel_lru_locks mdc
1107         lctl set_param mdc.*.stats clear
1108
1109         # was previously test_24D: LU-6101
1110         # readdir() returns correct number of entries after cursor reload
1111         local num_ls=$(ls $DIR/$tdir | wc -l)
1112         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1113         local num_all=$(ls -a $DIR/$tdir | wc -l)
1114         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1115              $num_all -ne $((nrfiles + 2)) ]; then
1116                 error "Expected $nrfiles files, got $num_ls " \
1117                         "($num_uniq unique $num_all .&..)"
1118         fi
1119         # LU-5 large readdir
1120         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1121         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1122         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1123         # take into account of overhead in lu_dirpage header and end mark in
1124         # each page, plus one in rpc_num calculation.
1125         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1126         local page_entries=$((($(page_size) - 24) / dirent_size))
1127         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1128         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1129         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1130         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1131         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1132         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1133                 error "large readdir doesn't take effect: " \
1134                       "$mds_readpage should be about $rpc_max"
1135
1136         simple_cleanup_common
1137 }
1138 run_test 24v "list large directory (test hash collision, b=17560)"
1139
1140 test_24w() { # bug21506
1141         SZ1=234852
1142         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1143         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1144         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1145         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1146         [[ "$SZ1" -eq "$SZ2" ]] ||
1147                 error "Error reading at the end of the file $tfile"
1148 }
1149 run_test 24w "Reading a file larger than 4Gb"
1150
1151 test_24x() {
1152         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1153
1154         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1155                 skip "Need MDS version at least 2.7.56" && return
1156
1157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1158         local MDTIDX=1
1159         local remote_dir=$DIR/$tdir/remote_dir
1160
1161         test_mkdir -p $DIR/$tdir
1162         $LFS mkdir -i $MDTIDX $remote_dir ||
1163                 error "create remote directory failed"
1164
1165         test_mkdir -p $DIR/$tdir/src_dir
1166         touch $DIR/$tdir/src_file
1167         test_mkdir -p $remote_dir/tgt_dir
1168         touch $remote_dir/tgt_file
1169
1170         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1171                 error "rename dir cross MDT failed!"
1172
1173         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1174                 error "rename file cross MDT failed!"
1175
1176         touch $DIR/$tdir/ln_file
1177         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1178                 error "ln file cross MDT failed"
1179
1180         rm -rf $DIR/$tdir || error "Can not delete directories"
1181 }
1182 run_test 24x "cross MDT rename/link"
1183
1184 test_24y() {
1185         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1186         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1187         local MDTIDX=1
1188         local remote_dir=$DIR/$tdir/remote_dir
1189
1190         test_mkdir -p $DIR/$tdir
1191         $LFS mkdir -i $MDTIDX $remote_dir ||
1192                    error "create remote directory failed"
1193
1194         test_mkdir -p $remote_dir/src_dir
1195         touch $remote_dir/src_file
1196         test_mkdir -p $remote_dir/tgt_dir
1197         touch $remote_dir/tgt_file
1198
1199         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1200                 error "rename subdir in the same remote dir failed!"
1201
1202         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1203                 error "rename files in the same remote dir failed!"
1204
1205         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1206                 error "link files in the same remote dir failed!"
1207
1208         rm -rf $DIR/$tdir || error "Can not delete directories"
1209 }
1210 run_test 24y "rename/link on the same dir should succeed"
1211
1212 test_24A() { # LU-3182
1213         local NFILES=5000
1214
1215         rm -rf $DIR/$tdir
1216         test_mkdir -p $DIR/$tdir
1217         trap simple_cleanup_common EXIT
1218         createmany -m $DIR/$tdir/$tfile $NFILES
1219         local t=$(ls $DIR/$tdir | wc -l)
1220         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1221         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1222         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1223                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1224         fi
1225
1226         simple_cleanup_common || error "Can not delete directories"
1227 }
1228 run_test 24A "readdir() returns correct number of entries."
1229
1230 test_24B() { # LU-4805
1231         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1232         local count
1233
1234         test_mkdir $DIR/$tdir
1235         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1236                 error "create striped dir failed"
1237
1238         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1239         [ $count -eq 2 ] || error "Expected 2, got $count"
1240
1241         touch $DIR/$tdir/striped_dir/a
1242
1243         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1244         [ $count -eq 3 ] || error "Expected 3, got $count"
1245
1246         touch $DIR/$tdir/striped_dir/.f
1247
1248         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1249         [ $count -eq 4 ] || error "Expected 4, got $count"
1250
1251         rm -rf $DIR/$tdir || error "Can not delete directories"
1252 }
1253 run_test 24B "readdir for striped dir return correct number of entries"
1254
1255 test_24C() {
1256         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1257
1258         mkdir $DIR/$tdir
1259         mkdir $DIR/$tdir/d0
1260         mkdir $DIR/$tdir/d1
1261
1262         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1263                 error "create striped dir failed"
1264
1265         cd $DIR/$tdir/d0/striped_dir
1266
1267         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1268         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1269         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1270
1271         [ "$d0_ino" = "$parent_ino" ] ||
1272                 error ".. wrong, expect $d0_ino, get $parent_ino"
1273
1274         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1275                 error "mv striped dir failed"
1276
1277         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1278
1279         [ "$d1_ino" = "$parent_ino" ] ||
1280                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1281 }
1282 run_test 24C "check .. in striped dir"
1283
1284 test_24E() {
1285         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1287
1288         mkdir -p $DIR/$tdir
1289         mkdir $DIR/$tdir/src_dir
1290         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1291                 error "create remote source failed"
1292
1293         touch $DIR/$tdir/src_dir/src_child/a
1294
1295         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1296                 error "create remote target dir failed"
1297
1298         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1299                 error "create remote target child failed"
1300
1301         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1302                 error "rename dir cross MDT failed!"
1303
1304         find $DIR/$tdir
1305
1306         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1307                 error "src_child still exists after rename"
1308
1309         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1310                 error "missing file(a) after rename"
1311
1312         rm -rf $DIR/$tdir || error "Can not delete directories"
1313 }
1314 run_test 24E "cross MDT rename/link"
1315
1316 test_25a() {
1317         echo '== symlink sanity ============================================='
1318
1319         test_mkdir $DIR/d25
1320         ln -s d25 $DIR/s25
1321         touch $DIR/s25/foo || error
1322 }
1323 run_test 25a "create file in symlinked directory ==============="
1324
1325 test_25b() {
1326         [ ! -d $DIR/d25 ] && test_25a
1327         $CHECKSTAT -t file $DIR/s25/foo || error
1328 }
1329 run_test 25b "lookup file in symlinked directory ==============="
1330
1331 test_26a() {
1332         test_mkdir $DIR/d26
1333         test_mkdir $DIR/d26/d26-2
1334         ln -s d26/d26-2 $DIR/s26
1335         touch $DIR/s26/foo || error
1336 }
1337 run_test 26a "multiple component symlink ======================="
1338
1339 test_26b() {
1340         test_mkdir -p $DIR/d26b/d26-2
1341         ln -s d26b/d26-2/foo $DIR/s26-2
1342         touch $DIR/s26-2 || error
1343 }
1344 run_test 26b "multiple component symlink at end of lookup ======"
1345
1346 test_26c() {
1347         test_mkdir $DIR/d26.2
1348         touch $DIR/d26.2/foo
1349         ln -s d26.2 $DIR/s26.2-1
1350         ln -s s26.2-1 $DIR/s26.2-2
1351         ln -s s26.2-2 $DIR/s26.2-3
1352         chmod 0666 $DIR/s26.2-3/foo
1353 }
1354 run_test 26c "chain of symlinks ================================"
1355
1356 # recursive symlinks (bug 439)
1357 test_26d() {
1358         ln -s d26-3/foo $DIR/d26-3
1359 }
1360 run_test 26d "create multiple component recursive symlink ======"
1361
1362 test_26e() {
1363         [ ! -h $DIR/d26-3 ] && test_26d
1364         rm $DIR/d26-3
1365 }
1366 run_test 26e "unlink multiple component recursive symlink ======"
1367
1368 # recursive symlinks (bug 7022)
1369 test_26f() {
1370         test_mkdir -p $DIR/$tdir
1371         test_mkdir $DIR/$tdir/$tfile   || error "mkdir $DIR/$tdir/$tfile failed"
1372         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1373         test_mkdir -p lndir/bar1      || error "mkdir lndir/bar1 failed"
1374         test_mkdir $DIR/$tdir/$tfile/$tfile   || error "mkdir $tfile failed"
1375         cd $tfile                || error "cd $tfile failed"
1376         ln -s .. dotdot          || error "ln dotdot failed"
1377         ln -s dotdot/lndir lndir || error "ln lndir failed"
1378         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1379         output=`ls $tfile/$tfile/lndir/bar1`
1380         [ "$output" = bar1 ] && error "unexpected output"
1381         rm -r $tfile             || error "rm $tfile failed"
1382         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1383 }
1384 run_test 26f "rm -r of a directory which has recursive symlink"
1385
1386 test_27a() {
1387         test_mkdir -p $DIR/d27 || error "mkdir failed"
1388         $LFS getstripe $DIR/d27
1389         $LFS setstripe -c 1 $DIR/d27/f0 || error "setstripe failed"
1390         $CHECKSTAT -t file $DIR/d27/f0 || error "checkstat failed"
1391         cp /etc/hosts $DIR/d27/f0 || error
1392 }
1393 run_test 27a "one stripe file"
1394
1395 test_27b() {
1396         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1397         test_mkdir -p $DIR/d27
1398         $LFS setstripe -c 2 $DIR/d27/f01 || error "setstripe failed"
1399         $LFS getstripe -c $DIR/d27/f01
1400         [ $($LFS getstripe -c $DIR/d27/f01) -eq 2 ] ||
1401                 error "two-stripe file doesn't have two stripes"
1402
1403         dd if=/dev/zero of=$DIR/d27/f01 bs=4k count=4 || error "dd failed"
1404 }
1405 run_test 27b "create and write to two stripe file"
1406
1407 test_27d() {
1408         test_mkdir -p $DIR/d27
1409         $LFS setstripe -c 0 -i -1 -S 0 $DIR/d27/fdef || error "setstripe failed"
1410         $CHECKSTAT -t file $DIR/d27/fdef || error "checkstat failed"
1411         dd if=/dev/zero of=$DIR/d27/fdef bs=4k count=4 || error
1412 }
1413 run_test 27d "create file with default settings"
1414
1415 test_27e() {
1416         # LU-5839 adds check for existed layout before setting it
1417         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1418                 skip "Need MDS version at least 2.7.56" && return
1419         test_mkdir -p $DIR/d27
1420         $LFS setstripe -c 2 $DIR/d27/f12 || error "setstripe failed"
1421         $LFS setstripe -c 2 $DIR/d27/f12 && error "setstripe succeeded twice"
1422         $CHECKSTAT -t file $DIR/d27/f12 || error "checkstat failed"
1423 }
1424 run_test 27e "setstripe existing file (should return error)"
1425
1426 test_27f() {
1427         test_mkdir $DIR/$tdir
1428         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1429                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1430         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1431                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1432         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1433         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1434 }
1435 run_test 27f "setstripe with bad stripe size (should return error)"
1436
1437 test_27g() {
1438         test_mkdir -p $DIR/d27
1439         $MCREATE $DIR/d27/fnone || error "mcreate failed"
1440         $LFS getstripe $DIR/d27/fnone 2>&1 | grep "no stripe info" ||
1441                 error "$DIR/d27/fnone has object"
1442 }
1443 run_test 27g "$LFS getstripe with no objects"
1444
1445 test_27i() {
1446         test_mkdir $DIR/$tdir
1447         touch $DIR/$tdir/$tfile || error "touch failed"
1448         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1449                 error "missing objects"
1450 }
1451 run_test 27i "$LFS getstripe with some objects"
1452
1453 test_27j() {
1454         test_mkdir -p $DIR/d27
1455         $LFS setstripe -i $OSTCOUNT $DIR/d27/f27j &&
1456                 error "setstripe failed" || true
1457 }
1458 run_test 27j "setstripe with bad stripe offset (should return error)"
1459
1460 test_27k() { # bug 2844
1461         test_mkdir -p $DIR/d27
1462         FILE=$DIR/d27/f27k
1463         LL_MAX_BLKSIZE=$((4 * 1024 * 1024))
1464         [ ! -d $DIR/d27 ] && test_mkdir -p $DIR d27
1465         $LFS setstripe -S 67108864 $FILE || error "setstripe failed"
1466         BLKSIZE=$(stat $FILE | awk '/IO Block:/ { print $7 }')
1467         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "1:$BLKSIZE > $LL_MAX_BLKSIZE"
1468         dd if=/dev/zero of=$FILE bs=4k count=1
1469         BLKSIZE=$(stat $FILE | awk '/IO Block:/ { print $7 }')
1470         [ $BLKSIZE -le $LL_MAX_BLKSIZE ] || error "2:$BLKSIZE > $LL_MAX_BLKSIZE"
1471 }
1472 run_test 27k "limit i_blksize for broken user apps"
1473
1474 test_27l() {
1475         test_mkdir -p $DIR/d27
1476         mcreate $DIR/f27l || error "creating file"
1477         $RUNAS $SETSTRIPE -c 1 $DIR/f27l &&
1478                 error "setstripe should have failed" || true
1479 }
1480 run_test 27l "check setstripe permissions (should return error)"
1481
1482 test_27m() {
1483         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1484
1485         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1486                    head -n1)
1487         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1488                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1489                 return
1490         fi
1491         trap simple_cleanup_common EXIT
1492         test_mkdir -p $DIR/$tdir
1493         $LFS setstripe -i 0 -c 1 $DIR/$tdir/f27m_1
1494         dd if=/dev/zero of=$DIR/$tdir/f27m_1 bs=1024 count=$MAXFREE &&
1495                 error "dd should fill OST0"
1496         i=2
1497         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/f27m_$i; do
1498                 i=$((i + 1))
1499                 [ $i -gt 256 ] && break
1500         done
1501         i=$((i + 1))
1502         touch $DIR/$tdir/f27m_$i
1503         [ $($LFS getstripe $DIR/$tdir/f27m_$i | grep -A 10 obdidx |
1504             awk '{print $1}' | grep -w "0") ] &&
1505                 error "OST0 was full but new created file still use it"
1506         i=$((i + 1))
1507         touch $DIR/$tdir/f27m_$i
1508         [ $($LFS getstripe $DIR/$tdir/f27m_$i | grep -A 10 obdidx |
1509             awk '{print $1}'| grep -w "0") ] &&
1510                 error "OST0 was full but new created file still use it"
1511         simple_cleanup_common
1512 }
1513 run_test 27m "create file while OST0 was full"
1514
1515 sleep_maxage() {
1516         local DELAY=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1517                       head -n 1 | awk '{print $1 * 2}')
1518         sleep $DELAY
1519 }
1520
1521 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1522 # if the OST isn't full anymore.
1523 reset_enospc() {
1524         local OSTIDX=${1:-""}
1525
1526         local list=$(comma_list $(osts_nodes))
1527         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1528
1529         do_nodes $list lctl set_param fail_loc=0
1530         sync    # initiate all OST_DESTROYs from MDS to OST
1531         sleep_maxage
1532 }
1533
1534 exhaust_precreations() {
1535         local OSTIDX=$1
1536         local FAILLOC=$2
1537         local FAILIDX=${3:-$OSTIDX}
1538         local ofacet=ost$((OSTIDX + 1))
1539
1540         test_mkdir -p -c1 $DIR/$tdir
1541         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1542         local mfacet=mds$((mdtidx + 1))
1543         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1544
1545         local OST=$(ostname_from_index $OSTIDX)
1546
1547         # on the mdt's osc
1548         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1549         local last_id=$(do_facet $mfacet lctl get_param -n \
1550                         osc.$mdtosc_proc1.prealloc_last_id)
1551         local next_id=$(do_facet $mfacet lctl get_param -n \
1552                         osc.$mdtosc_proc1.prealloc_next_id)
1553
1554         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1555         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1556
1557         test_mkdir -p $DIR/$tdir/${OST}
1558         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1559 #define OBD_FAIL_OST_ENOSPC              0x215
1560         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1561         echo "Creating to objid $last_id on ost $OST..."
1562         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1563         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1564         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1565         sleep_maxage
1566 }
1567
1568 exhaust_all_precreations() {
1569         local i
1570         for (( i=0; i < OSTCOUNT; i++ )) ; do
1571                 exhaust_precreations $i $1 -1
1572         done
1573 }
1574
1575 test_27n() {
1576         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1577         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1578         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1579         remote_ost_nodsh && skip "remote OST with nodsh" && return
1580
1581         reset_enospc
1582         rm -f $DIR/$tdir/$tfile
1583         exhaust_precreations 0 0x80000215
1584         $LFS setstripe -c -1 $DIR/$tdir
1585         touch $DIR/$tdir/$tfile || error
1586         $LFS getstripe $DIR/$tdir/$tfile
1587         reset_enospc
1588 }
1589 run_test 27n "create file with some full OSTs"
1590
1591 test_27o() {
1592         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1593         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1594         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1595         remote_ost_nodsh && skip "remote OST with nodsh" && return
1596
1597         reset_enospc
1598         rm -f $DIR/$tdir/$tfile
1599         exhaust_all_precreations 0x215
1600
1601         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1602
1603         reset_enospc
1604         rm -rf $DIR/$tdir/*
1605 }
1606 run_test 27o "create file with all full OSTs (should error)"
1607
1608 test_27p() {
1609         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1610         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1611         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1612         remote_ost_nodsh && skip "remote OST with nodsh" && return
1613
1614         reset_enospc
1615         rm -f $DIR/$tdir/$tfile
1616         test_mkdir -p $DIR/$tdir
1617
1618         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1619         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1620         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1621
1622         exhaust_precreations 0 0x80000215
1623         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1624         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1625         $LFS getstripe $DIR/$tdir/$tfile
1626
1627         reset_enospc
1628 }
1629 run_test 27p "append to a truncated file with some full OSTs"
1630
1631 test_27q() {
1632         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1633         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1634         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1635         remote_ost_nodsh && skip "remote OST with nodsh" && return
1636
1637         reset_enospc
1638         rm -f $DIR/$tdir/$tfile
1639
1640         test_mkdir -p $DIR/$tdir
1641         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1642         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1643                 error "truncate $DIR/$tdir/$tfile failed"
1644         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1645
1646         exhaust_all_precreations 0x215
1647
1648         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1649         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1650
1651         reset_enospc
1652 }
1653 run_test 27q "append to truncated file with all OSTs full (should error)"
1654
1655 test_27r() {
1656         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1658         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1659         remote_ost_nodsh && skip "remote OST with nodsh" && return
1660
1661         reset_enospc
1662         rm -f $DIR/$tdir/$tfile
1663         exhaust_precreations 0 0x80000215
1664
1665         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile # && error
1666
1667         reset_enospc
1668 }
1669 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1670
1671 test_27s() { # bug 10725
1672         test_mkdir -p $DIR/$tdir
1673         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1674         local stripe_count=0
1675         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1676         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1677                 error "stripe width >= 2^32 succeeded" || true
1678
1679 }
1680 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1681
1682 test_27t() { # bug 10864
1683         WDIR=$(pwd)
1684         WLFS=$(which lfs)
1685         cd $DIR
1686         touch $tfile
1687         $WLFS getstripe $tfile
1688         cd $WDIR
1689 }
1690 run_test 27t "check that utils parse path correctly"
1691
1692 test_27u() { # bug 4900
1693         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1694         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1695         local index
1696         local list=$(comma_list $(mdts_nodes))
1697
1698 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1699         do_nodes $list $LCTL set_param fail_loc=0x139
1700         test_mkdir -p $DIR/$tdir
1701         trap simple_cleanup_common EXIT
1702         createmany -o $DIR/$tdir/t- 1000
1703         do_nodes $list $LCTL set_param fail_loc=0
1704
1705         TLOG=$TMP/$tfile.getstripe
1706         $LFS getstripe $DIR/$tdir > $TLOG
1707         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1708         unlinkmany $DIR/$tdir/t- 1000
1709         trap 0
1710         [[ $OBJS -gt 0 ]] &&
1711                 error "$OBJS objects created on OST-0. See $TLOG" || pass
1712 }
1713 run_test 27u "skip object creation on OSC w/o objects"
1714
1715 test_27v() { # bug 4900
1716         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1718         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1719         remote_ost_nodsh && skip "remote OST with nodsh" && return
1720
1721         exhaust_all_precreations 0x215
1722         reset_enospc
1723
1724         test_mkdir $DIR/$tdir
1725         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1726
1727         touch $DIR/$tdir/$tfile
1728         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1729         # all except ost1
1730         for (( i=1; i < OSTCOUNT; i++ )); do
1731                 do_facet ost$i lctl set_param fail_loc=0x705
1732         done
1733         local START=`date +%s`
1734         createmany -o $DIR/$tdir/$tfile 32
1735
1736         local FINISH=`date +%s`
1737         local TIMEOUT=`lctl get_param -n timeout`
1738         local PROCESS=$((FINISH - START))
1739         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1740                error "$FINISH - $START >= $TIMEOUT / 2"
1741         sleep $((TIMEOUT / 2 - PROCESS))
1742         reset_enospc
1743 }
1744 run_test 27v "skip object creation on slow OST"
1745
1746 test_27w() { # bug 10997
1747         test_mkdir $DIR/$tdir || error "mkdir failed"
1748         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1749         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1750                 error "stripe size $size != 65536" || true
1751         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1752                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1753 }
1754 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1755
1756 test_27wa() {
1757         [[ $OSTCOUNT -lt 2 ]] &&
1758                 skip_env "skipping multiple stripe count/offset test" && return
1759
1760         test_mkdir $DIR/$tdir || error "mkdir failed"
1761         for i in $(seq 1 $OSTCOUNT); do
1762                 offset=$((i - 1))
1763                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1764                         error "setstripe -c $i -i $offset failed"
1765                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1766                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1767                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1768                 [ $index -ne $offset ] &&
1769                         error "stripe offset $index != $offset" || true
1770         done
1771 }
1772 run_test 27wa "check $LFS setstripe -c -i options"
1773
1774 test_27x() {
1775         remote_ost_nodsh && skip "remote OST with nodsh" && return
1776         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1777         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1778         OFFSET=$(($OSTCOUNT - 1))
1779         OSTIDX=0
1780         local OST=$(ostname_from_index $OSTIDX)
1781
1782         test_mkdir -p $DIR/$tdir
1783         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1784         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1785         sleep_maxage
1786         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1787         for i in $(seq 0 $OFFSET); do
1788                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1789                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1790                 error "OST0 was degraded but new created file still use it"
1791         done
1792         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1793 }
1794 run_test 27x "create files while OST0 is degraded"
1795
1796 test_27y() {
1797         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1798         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1799         remote_ost_nodsh && skip "remote OST with nodsh" && return
1800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1801
1802         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1803         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1804                 osc.$mdtosc.prealloc_last_id)
1805         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1806                 osc.$mdtosc.prealloc_next_id)
1807         local fcount=$((last_id - next_id))
1808         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1809         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1810
1811         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1812                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1813         local OST_DEACTIVE_IDX=-1
1814         local OSC
1815         local OSTIDX
1816         local OST
1817
1818         for OSC in $MDS_OSCS; do
1819                 OST=$(osc_to_ost $OSC)
1820                 OSTIDX=$(index_from_ostuuid $OST)
1821                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1822                         OST_DEACTIVE_IDX=$OSTIDX
1823                 fi
1824                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1825                         echo $OSC "is Deactivated:"
1826                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1827                 fi
1828         done
1829
1830         OSTIDX=$(index_from_ostuuid $OST)
1831         test_mkdir -p $DIR/$tdir
1832         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1833
1834         for OSC in $MDS_OSCS; do
1835                 OST=$(osc_to_ost $OSC)
1836                 OSTIDX=$(index_from_ostuuid $OST)
1837                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1838                         echo $OST "is degraded:"
1839                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1840                                                 obdfilter.$OST.degraded=1
1841                 fi
1842         done
1843
1844         sleep_maxage
1845         createmany -o $DIR/$tdir/$tfile $fcount
1846
1847         for OSC in $MDS_OSCS; do
1848                 OST=$(osc_to_ost $OSC)
1849                 OSTIDX=$(index_from_ostuuid $OST)
1850                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1851                         echo $OST "is recovered from degraded:"
1852                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1853                                                 obdfilter.$OST.degraded=0
1854                 else
1855                         do_facet $SINGLEMDS lctl --device %$OSC activate
1856                 fi
1857         done
1858
1859         # all osp devices get activated, hence -1 stripe count restored
1860         local stripe_count=0
1861
1862         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1863         # devices get activated.
1864         sleep_maxage
1865         $LFS setstripe -c -1 $DIR/$tfile
1866         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1867         rm -f $DIR/$tfile
1868         [ $stripe_count -ne $OSTCOUNT ] &&
1869                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1870         return 0
1871 }
1872 run_test 27y "create files while OST0 is degraded and the rest inactive"
1873
1874 check_seq_oid()
1875 {
1876         log "check file $1"
1877
1878         lmm_count=$($GETSTRIPE -c $1)
1879         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1880         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1881
1882         local old_ifs="$IFS"
1883         IFS=$'[:]'
1884         fid=($($LFS path2fid $1))
1885         IFS="$old_ifs"
1886
1887         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1888         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1889
1890         # compare lmm_seq and lu_fid->f_seq
1891         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1892         # compare lmm_object_id and lu_fid->oid
1893         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1894
1895         # check the trusted.fid attribute of the OST objects of the file
1896         local have_obdidx=false
1897         local stripe_nr=0
1898         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1899                 # skip lines up to and including "obdidx"
1900                 [ -z "$obdidx" ] && break
1901                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1902                 $have_obdidx || continue
1903
1904                 local ost=$((obdidx + 1))
1905                 local dev=$(ostdevname $ost)
1906                 local oid_hex
1907
1908                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1909
1910                 seq=$(echo $seq | sed -e "s/^0x//g")
1911                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1912                         oid_hex=$(echo $oid)
1913                 else
1914                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1915                 fi
1916                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1917
1918                 local ff=""
1919                 #
1920                 # Don't unmount/remount the OSTs if we don't need to do that.
1921                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1922                 # update too, until that use mount/ll_decode_filter_fid/mount.
1923                 # Re-enable when debugfs will understand new filter_fid.
1924                 #
1925                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
1926                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1927                                 $dev 2>/dev/null" | grep "parent=")
1928                 fi
1929                 if [ -z "$ff" ]; then
1930                         stop ost$ost
1931                         mount_fstype ost$ost
1932                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1933                                 $(facet_mntpt ost$ost)/$obj_file)
1934                         unmount_fstype ost$ost
1935                         start ost$ost $dev $OST_MOUNT_OPTS
1936                         clients_up
1937                 fi
1938
1939                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1940
1941                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1942
1943                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1944                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1945                 #
1946                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
1947                 #       stripe_size=1048576 component_id=1 component_start=0 \
1948                 #       component_end=33554432
1949                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
1950                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
1951                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
1952                 local ff_pstripe
1953                 if grep -q 'stripe=' <<<$ff; then
1954                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
1955                 else
1956                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1957                         # into f_ver in this case.  See comment on ff_parent.
1958                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
1959                 fi
1960
1961                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1962                 [ $ff_pseq = $lmm_seq ] ||
1963                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1964                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1965                 [ $ff_poid = $lmm_oid ] ||
1966                         error "FF parent OID $ff_poid != $lmm_oid"
1967                 (($ff_pstripe == $stripe_nr)) ||
1968                         error "FF stripe $ff_pstripe != $stripe_nr"
1969
1970                 stripe_nr=$((stripe_nr + 1))
1971                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
1972                         continue
1973                 if grep -q 'stripe_count=' <<<$ff; then
1974                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
1975                                             -e 's/ .*//' <<<$ff)
1976                         [ $lmm_count = $ff_scnt ] ||
1977                                 error "FF stripe count $lmm_count != $ff_scnt"
1978                 fi
1979         done
1980 }
1981
1982 test_27z() {
1983         remote_ost_nodsh && skip "remote OST with nodsh" && return
1984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1985         test_mkdir -p $DIR/$tdir
1986
1987         $SETSTRIPE -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1988                 { error "setstripe -c -1 failed"; return 1; }
1989         # We need to send a write to every object to get parent FID info set.
1990         # This _should_ also work for setattr, but does not currently.
1991         # touch $DIR/$tdir/$tfile-1 ||
1992         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1993                 { error "dd $tfile-1 failed"; return 2; }
1994         $SETSTRIPE -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1995                 { error "setstripe -c -1 failed"; return 3; }
1996         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1997                 { error "dd $tfile-2 failed"; return 4; }
1998
1999         # make sure write RPCs have been sent to OSTs
2000         sync; sleep 5; sync
2001
2002         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2003         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2004 }
2005 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2006
2007 test_27A() { # b=19102
2008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2009         local restore_size=$($GETSTRIPE -S $MOUNT)
2010         local restore_count=$($GETSTRIPE -c $MOUNT)
2011         local restore_offset=$($GETSTRIPE -i $MOUNT)
2012         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
2013         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2014                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
2015         local default_size=$($GETSTRIPE -S $MOUNT)
2016         local default_offset=$($GETSTRIPE -i $MOUNT)
2017         local dsize=$((1024 * 1024))
2018         [ $default_size -eq $dsize ] ||
2019                 error "stripe size $default_size != $dsize"
2020         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
2021         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
2022 }
2023 run_test 27A "check filesystem-wide default LOV EA values"
2024
2025 test_27B() { # LU-2523
2026         test_mkdir -p $DIR/$tdir
2027         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2028         touch $DIR/$tdir/f0
2029         # open f1 with O_LOV_DELAY_CREATE
2030         # rename f0 onto f1
2031         # call setstripe ioctl on open file descriptor for f1
2032         # close
2033         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2034                 $DIR/$tdir/f0
2035
2036         rm -f $DIR/$tdir/f1
2037         # open f1 with O_LOV_DELAY_CREATE
2038         # unlink f1
2039         # call setstripe ioctl on open file descriptor for f1
2040         # close
2041         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2042
2043         # Allow multiop to fail in imitation of NFS's busted semantics.
2044         true
2045 }
2046 run_test 27B "call setstripe on open unlinked file/rename victim"
2047
2048 test_27C() { #LU-2871
2049         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2050
2051         declare -a ost_idx
2052         local index
2053         local found
2054         local i
2055         local j
2056
2057         test_mkdir -p $DIR/$tdir
2058         cd $DIR/$tdir
2059         for i in $(seq 0 $((OSTCOUNT - 1))); do
2060                 # set stripe across all OSTs starting from OST$i
2061                 $SETSTRIPE -i $i -c -1 $tfile$i
2062                 # get striping information
2063                 ost_idx=($($GETSTRIPE $tfile$i |
2064                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2065                 echo ${ost_idx[@]}
2066
2067                 # check the layout
2068                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2069                         error "${#ost_idx[@]} != $OSTCOUNT"
2070
2071                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2072                         found=0
2073                         for j in $(echo ${ost_idx[@]}); do
2074                                 if [ $index -eq $j ]; then
2075                                         found=1
2076                                         break
2077                                 fi
2078                         done
2079                         [ $found = 1 ] ||
2080                                 error "Can not find $index in ${ost_idx[@]}"
2081                 done
2082         done
2083 }
2084 run_test 27C "check full striping across all OSTs"
2085
2086 test_27D() {
2087         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2088         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2089         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2090         local POOL=${POOL:-testpool}
2091         local first_ost=0
2092         local last_ost=$(($OSTCOUNT - 1))
2093         local ost_step=1
2094         local ost_list=$(seq $first_ost $ost_step $last_ost)
2095         local ost_range="$first_ost $last_ost $ost_step"
2096
2097         if ! combined_mgs_mds ; then
2098                 mount_mgs_client
2099         fi
2100
2101         test_mkdir -p $DIR/$tdir
2102         pool_add $POOL || error "pool_add failed"
2103         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2104
2105         local skip27D
2106         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2107                 skip27D+="-s 29"
2108         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2109           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2110                 skip27D+=" -s 30,31"
2111         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2112                 error "llapi_layout_test failed"
2113
2114         destroy_test_pools || error "destroy test pools failed"
2115
2116         if ! combined_mgs_mds ; then
2117                 umount_mgs_client
2118         fi
2119 }
2120 run_test 27D "validate llapi_layout API"
2121
2122 # Verify that default_easize is increased from its initial value after
2123 # accessing a widely striped file.
2124 test_27E() {
2125         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2126         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2127                 skip "client does not have LU-3338 fix" && return
2128
2129         # 72 bytes is the minimum space required to store striping
2130         # information for a file striped across one OST:
2131         # (sizeof(struct lov_user_md_v3) +
2132         #  sizeof(struct lov_user_ost_data_v1))
2133         local min_easize=72
2134         $LCTL set_param -n llite.*.default_easize $min_easize ||
2135                 error "lctl set_param failed"
2136         local easize=$($LCTL get_param -n llite.*.default_easize)
2137
2138         [ $easize -eq $min_easize ] ||
2139                 error "failed to set default_easize"
2140
2141         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2142                 error "setstripe failed"
2143         cat $DIR/$tfile
2144         rm $DIR/$tfile
2145
2146         easize=$($LCTL get_param -n llite.*.default_easize)
2147
2148         [ $easize -gt $min_easize ] ||
2149                 error "default_easize not updated"
2150 }
2151 run_test 27E "check that default extended attribute size properly increases"
2152
2153 test_27F() { # LU-5346/LU-7975
2154         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2155         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2156         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2157                 skip "Need MDS version at least 2.8.51" && return
2158         remote_ost_nodsh && skip "remote OST with nodsh" && return
2159
2160         test_mkdir -p $DIR/$tdir
2161         rm -f $DIR/$tdir/f0
2162         $SETSTRIPE -c 2 $DIR/$tdir
2163
2164         # stop all OSTs to reproduce situation for LU-7975 ticket
2165         for num in $(seq $OSTCOUNT); do
2166                 stop ost$num
2167         done
2168
2169         # open/create f0 with O_LOV_DELAY_CREATE
2170         # truncate f0 to a non-0 size
2171         # close
2172         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2173
2174         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2175         # open/write it again to force delayed layout creation
2176         cat /etc/hosts > $DIR/$tdir/f0 &
2177         catpid=$!
2178
2179         # restart OSTs
2180         for num in $(seq $OSTCOUNT); do
2181                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2182                         error "ost$num failed to start"
2183         done
2184
2185         wait $catpid || error "cat failed"
2186
2187         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2188         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2189
2190 }
2191 run_test 27F "Client resend delayed layout creation with non-zero size"
2192
2193 test_27G() { #LU-10629
2194         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2195         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2196         local POOL=${POOL:-testpool}
2197         local ostrange="0 0 1"
2198
2199         test_mkdir $DIR/$tdir
2200         pool_add $POOL || error "pool_add failed"
2201         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2202         $LFS setstripe -p $POOL $DIR/$tdir
2203
2204         local pool=$($LFS getstripe -p $DIR/$tdir)
2205
2206         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2207
2208         $LFS setstripe -d $DIR/$tdir
2209
2210         pool=$($LFS getstripe -p $DIR/$tdir)
2211
2212         rmdir $DIR/$tdir
2213
2214         [ -z "$pool" ] || error "'$pool' is not empty"
2215 }
2216 run_test 27G "Clear OST pool from stripe"
2217
2218 # createtest also checks that device nodes are created and
2219 # then visible correctly (#2091)
2220 test_28() { # bug 2091
2221         test_mkdir $DIR/d28
2222         $CREATETEST $DIR/d28/ct || error
2223 }
2224 run_test 28 "create/mknod/mkdir with bad file types ============"
2225
2226 test_29() {
2227         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2228         sync; sleep 1; sync # flush out any dirty pages from previous tests
2229         cancel_lru_locks
2230         test_mkdir $DIR/d29
2231         touch $DIR/d29/foo
2232         log 'first d29'
2233         ls -l $DIR/d29
2234
2235         declare -i LOCKCOUNTORIG=0
2236         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2237                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2238         done
2239         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2240
2241         declare -i LOCKUNUSEDCOUNTORIG=0
2242         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2243                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2244         done
2245
2246         log 'second d29'
2247         ls -l $DIR/d29
2248         log 'done'
2249
2250         declare -i LOCKCOUNTCURRENT=0
2251         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2252                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2253         done
2254
2255         declare -i LOCKUNUSEDCOUNTCURRENT=0
2256         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2257                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2258         done
2259
2260         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2261                 $LCTL set_param -n ldlm.dump_namespaces ""
2262                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2263                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2264                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2265                 return 2
2266         fi
2267         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2268                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2269                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2270                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2271                 return 3
2272         fi
2273 }
2274 run_test 29 "IT_GETATTR regression  ============================"
2275
2276 test_30a() { # was test_30
2277         cp $(which ls) $DIR || cp /bin/ls $DIR
2278         $DIR/ls / || error
2279         rm $DIR/ls
2280 }
2281 run_test 30a "execute binary from Lustre (execve) =============="
2282
2283 test_30b() {
2284         cp `which ls` $DIR || cp /bin/ls $DIR
2285         chmod go+rx $DIR/ls
2286         $RUNAS $DIR/ls / || error
2287         rm $DIR/ls
2288 }
2289 run_test 30b "execute binary from Lustre as non-root ==========="
2290
2291 test_30c() { # b=22376
2292         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2293         cp `which ls` $DIR || cp /bin/ls $DIR
2294         chmod a-rw $DIR/ls
2295         cancel_lru_locks mdc
2296         cancel_lru_locks osc
2297         $RUNAS $DIR/ls / || error
2298         rm -f $DIR/ls
2299 }
2300 run_test 30c "execute binary from Lustre without read perms ===="
2301
2302 test_31a() {
2303         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2304         $CHECKSTAT -a $DIR/f31 || error
2305 }
2306 run_test 31a "open-unlink file =================================="
2307
2308 test_31b() {
2309         touch $DIR/f31 || error
2310         ln $DIR/f31 $DIR/f31b || error
2311         $MULTIOP $DIR/f31b Ouc || error
2312         $CHECKSTAT -t file $DIR/f31 || error
2313 }
2314 run_test 31b "unlink file with multiple links while open ======="
2315
2316 test_31c() {
2317         touch $DIR/f31 || error
2318         ln $DIR/f31 $DIR/f31c || error
2319         multiop_bg_pause $DIR/f31 O_uc || return 1
2320         MULTIPID=$!
2321         $MULTIOP $DIR/f31c Ouc
2322         kill -USR1 $MULTIPID
2323         wait $MULTIPID
2324 }
2325 run_test 31c "open-unlink file with multiple links ============="
2326
2327 test_31d() {
2328         opendirunlink $DIR/d31d $DIR/d31d || error
2329         $CHECKSTAT -a $DIR/d31d || error
2330 }
2331 run_test 31d "remove of open directory ========================="
2332
2333 test_31e() { # bug 2904
2334         openfilleddirunlink $DIR/d31e || error
2335 }
2336 run_test 31e "remove of open non-empty directory ==============="
2337
2338 test_31f() { # bug 4554
2339         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2340         set -vx
2341         test_mkdir $DIR/d31f
2342         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2343         cp /etc/hosts $DIR/d31f
2344         ls -l $DIR/d31f
2345         $GETSTRIPE $DIR/d31f/hosts
2346         multiop_bg_pause $DIR/d31f D_c || return 1
2347         MULTIPID=$!
2348
2349         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2350         test_mkdir $DIR/d31f
2351         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2352         cp /etc/hosts $DIR/d31f
2353         ls -l $DIR/d31f
2354         $GETSTRIPE $DIR/d31f/hosts
2355         multiop_bg_pause $DIR/d31f D_c || return 1
2356         MULTIPID2=$!
2357
2358         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2359         wait $MULTIPID || error "first opendir $MULTIPID failed"
2360
2361         sleep 6
2362
2363         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2364         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2365         set +vx
2366 }
2367 run_test 31f "remove of open directory with open-unlink file ==="
2368
2369 test_31g() {
2370         echo "-- cross directory link --"
2371         test_mkdir -c1 $DIR/${tdir}ga
2372         test_mkdir -c1 $DIR/${tdir}gb
2373         touch $DIR/${tdir}ga/f
2374         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2375         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2376         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2377         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2378         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2379 }
2380 run_test 31g "cross directory link==============="
2381
2382 test_31h() {
2383         echo "-- cross directory link --"
2384         test_mkdir -c1 $DIR/${tdir}
2385         test_mkdir -c1 $DIR/${tdir}/dir
2386         touch $DIR/${tdir}/f
2387         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2388         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2389         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2390         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2391         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2392 }
2393 run_test 31h "cross directory link under child==============="
2394
2395 test_31i() {
2396         echo "-- cross directory link --"
2397         test_mkdir -c1 $DIR/$tdir
2398         test_mkdir -c1 $DIR/$tdir/dir
2399         touch $DIR/$tdir/dir/f
2400         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2401         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2402         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2403         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2404         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2405 }
2406 run_test 31i "cross directory link under parent==============="
2407
2408 test_31j() {
2409         test_mkdir -c1 -p $DIR/$tdir
2410         test_mkdir -c1 -p $DIR/$tdir/dir1
2411         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2412         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2413         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2414         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2415         return 0
2416 }
2417 run_test 31j "link for directory==============="
2418
2419 test_31k() {
2420         test_mkdir -c1 -p $DIR/$tdir
2421         touch $DIR/$tdir/s
2422         touch $DIR/$tdir/exist
2423         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2424         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2425         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2426         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2427         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2428         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2429         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2430         return 0
2431 }
2432 run_test 31k "link to file: the same, non-existing, dir==============="
2433
2434 test_31m() {
2435         mkdir $DIR/d31m
2436         touch $DIR/d31m/s
2437         mkdir $DIR/d31m2
2438         touch $DIR/d31m2/exist
2439         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2440         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2441         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2442         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2443         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2444         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2445         return 0
2446 }
2447 run_test 31m "link to file: the same, non-existing, dir==============="
2448
2449 test_31n() {
2450         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2451         nlink=$(stat --format=%h $DIR/$tfile)
2452         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2453         local fd=$(free_fd)
2454         local cmd="exec $fd<$DIR/$tfile"
2455         eval $cmd
2456         cmd="exec $fd<&-"
2457         trap "eval $cmd" EXIT
2458         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2459         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2460         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2461         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2462         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2463         eval $cmd
2464 }
2465 run_test 31n "check link count of unlinked file"
2466
2467 link_one() {
2468         local TEMPNAME=$(mktemp $1_XXXXXX)
2469         mlink $TEMPNAME $1 2> /dev/null &&
2470                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2471         munlink $TEMPNAME
2472 }
2473
2474 test_31o() { # LU-2901
2475         test_mkdir -p $DIR/$tdir
2476         for LOOP in $(seq 100); do
2477                 rm -f $DIR/$tdir/$tfile*
2478                 for THREAD in $(seq 8); do
2479                         link_one $DIR/$tdir/$tfile.$LOOP &
2480                 done
2481                 wait
2482                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2483                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2484                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2485                         break || true
2486         done
2487 }
2488 run_test 31o "duplicate hard links with same filename"
2489
2490 test_31p() {
2491         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2492
2493         test_mkdir $DIR/$tdir
2494         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2495         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2496
2497         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2498                 error "open unlink test1 failed"
2499         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2500                 error "open unlink test2 failed"
2501
2502         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2503                 error "test1 still exists"
2504         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2505                 error "test2 still exists"
2506 }
2507 run_test 31p "remove of open striped directory"
2508
2509 cleanup_test32_mount() {
2510         local rc=0
2511         trap 0
2512         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2513         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2514         losetup -d $loopdev || true
2515         rm -rf $DIR/$tdir
2516         return $rc
2517 }
2518
2519 test_32a() {
2520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2521         echo "== more mountpoints and symlinks ================="
2522         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2523         trap cleanup_test32_mount EXIT
2524         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2525         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2526         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2527         cleanup_test32_mount
2528 }
2529 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2530
2531 test_32b() {
2532         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2533         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2534         trap cleanup_test32_mount EXIT
2535         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2536         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2537         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2538         cleanup_test32_mount
2539 }
2540 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2541
2542 test_32c() {
2543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2544         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2545         trap cleanup_test32_mount EXIT
2546         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2547         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2548         test_mkdir -p $DIR/$tdir/d2/test_dir
2549         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2550         cleanup_test32_mount
2551 }
2552 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2553
2554 test_32d() {
2555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2556         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2557         trap cleanup_test32_mount EXIT
2558         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2559         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2560         test_mkdir -p $DIR/$tdir/d2/test_dir
2561         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2562         cleanup_test32_mount
2563 }
2564 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2565
2566 test_32e() {
2567         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2568         test_mkdir -p $DIR/d32e/tmp
2569         TMP_DIR=$DIR/d32e/tmp
2570         ln -s $DIR/d32e $TMP_DIR/symlink11
2571         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2572         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2573         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2574 }
2575 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2576
2577 test_32f() {
2578         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2579         test_mkdir -p $DIR/d32f/tmp
2580         TMP_DIR=$DIR/d32f/tmp
2581         ln -s $DIR/d32f $TMP_DIR/symlink11
2582         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2583         ls $DIR/d32f/tmp/symlink11  || error
2584         ls $DIR/d32f/symlink01 || error
2585 }
2586 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2587
2588 test_32g() {
2589         TMP_DIR=$DIR/$tdir/tmp
2590         test_mkdir -p $DIR/$tdir/tmp
2591         test_mkdir $DIR/${tdir}2
2592         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2593         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2594         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2595         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2596         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2597         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2598 }
2599 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2600
2601 test_32h() {
2602         rm -fr $DIR/$tdir $DIR/${tdir}2
2603         TMP_DIR=$DIR/$tdir/tmp
2604         test_mkdir -p $DIR/$tdir/tmp
2605         test_mkdir $DIR/${tdir}2
2606         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2607         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2608         ls $TMP_DIR/symlink12 || error
2609         ls $DIR/$tdir/symlink02  || error
2610 }
2611 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2612
2613 test_32i() {
2614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2615         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2616         trap cleanup_test32_mount EXIT
2617         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2618         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2619         touch $DIR/$tdir/test_file
2620         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2621         cleanup_test32_mount
2622 }
2623 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2624
2625 test_32j() {
2626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2627         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2628         trap cleanup_test32_mount EXIT
2629         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2630         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2631         touch $DIR/$tdir/test_file
2632         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2633         cleanup_test32_mount
2634 }
2635 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2636
2637 test_32k() {
2638         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2639         rm -fr $DIR/$tdir
2640         trap cleanup_test32_mount EXIT
2641         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2642         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2643         test_mkdir -p $DIR/$tdir/d2
2644         touch $DIR/$tdir/d2/test_file || error
2645         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2646         cleanup_test32_mount
2647 }
2648 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2649
2650 test_32l() {
2651         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2652         rm -fr $DIR/$tdir
2653         trap cleanup_test32_mount EXIT
2654         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2655         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2656         test_mkdir -p $DIR/$tdir/d2
2657         touch $DIR/$tdir/d2/test_file
2658         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2659         cleanup_test32_mount
2660 }
2661 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2662
2663 test_32m() {
2664         rm -fr $DIR/d32m
2665         test_mkdir -p $DIR/d32m/tmp
2666         TMP_DIR=$DIR/d32m/tmp
2667         ln -s $DIR $TMP_DIR/symlink11
2668         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2669         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2670         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2671 }
2672 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2673
2674 test_32n() {
2675         rm -fr $DIR/d32n
2676         test_mkdir -p $DIR/d32n/tmp
2677         TMP_DIR=$DIR/d32n/tmp
2678         ln -s $DIR $TMP_DIR/symlink11
2679         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2680         ls -l $DIR/d32n/tmp/symlink11  || error
2681         ls -l $DIR/d32n/symlink01 || error
2682 }
2683 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2684
2685 test_32o() {
2686         touch $DIR/$tfile
2687         test_mkdir -p $DIR/d32o/tmp
2688         TMP_DIR=$DIR/d32o/tmp
2689         ln -s $DIR/$tfile $TMP_DIR/symlink12
2690         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2691         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2692         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2693         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2694         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2695 }
2696 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2697
2698 test_32p() {
2699         log 32p_1
2700         rm -fr $DIR/d32p
2701         log 32p_2
2702         rm -f $DIR/$tfile
2703         log 32p_3
2704         touch $DIR/$tfile
2705         log 32p_4
2706         test_mkdir -p $DIR/d32p/tmp
2707         log 32p_5
2708         TMP_DIR=$DIR/d32p/tmp
2709         log 32p_6
2710         ln -s $DIR/$tfile $TMP_DIR/symlink12
2711         log 32p_7
2712         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2713         log 32p_8
2714         cat $DIR/d32p/tmp/symlink12 || error
2715         log 32p_9
2716         cat $DIR/d32p/symlink02 || error
2717         log 32p_10
2718 }
2719 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2720
2721 test_32q() {
2722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2723         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2724         trap cleanup_test32_mount EXIT
2725         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2726         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2727         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2728         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2729         cleanup_test32_mount
2730 }
2731 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2732
2733 test_32r() {
2734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2735         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2736         trap cleanup_test32_mount EXIT
2737         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2738         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2739         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2740         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2741         cleanup_test32_mount
2742 }
2743 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2744
2745 test_33aa() {
2746         rm -f $DIR/$tfile
2747         touch $DIR/$tfile
2748         chmod 444 $DIR/$tfile
2749         chown $RUNAS_ID $DIR/$tfile
2750         log 33_1
2751         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2752         log 33_2
2753 }
2754 run_test 33aa "write file with mode 444 (should return error) ===="
2755
2756 test_33a() {
2757         rm -fr $DIR/d33
2758         test_mkdir -p $DIR/d33
2759         chown $RUNAS_ID $DIR/d33
2760         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2761         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2762                 error "open RDWR" || true
2763 }
2764 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2765
2766 test_33b() {
2767         rm -fr $DIR/d33
2768         test_mkdir -p $DIR/d33
2769         chown $RUNAS_ID $DIR/d33
2770         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2771 }
2772 run_test 33b "test open file with malformed flags (No panic)"
2773
2774 test_33c() {
2775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2776         local ostnum
2777         local ostname
2778         local write_bytes
2779         local all_zeros
2780
2781         remote_ost_nodsh && skip "remote OST with nodsh" && return
2782         all_zeros=:
2783         rm -fr $DIR/d33
2784         test_mkdir -p $DIR/d33
2785         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2786
2787         sync
2788         for ostnum in $(seq $OSTCOUNT); do
2789                 # test-framework's OST numbering is one-based, while Lustre's
2790                 # is zero-based
2791                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2792                 # Parsing llobdstat's output sucks; we could grep the /proc
2793                 # path, but that's likely to not be as portable as using the
2794                 # llobdstat utility.  So we parse lctl output instead.
2795                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2796                         obdfilter/$ostname/stats |
2797                         awk '/^write_bytes/ {print $7}' )
2798                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2799                 if (( ${write_bytes:-0} > 0 ))
2800                 then
2801                         all_zeros=false
2802                         break;
2803                 fi
2804         done
2805
2806         $all_zeros || return 0
2807
2808         # Write four bytes
2809         echo foo > $DIR/d33/bar
2810         # Really write them
2811         sync
2812
2813         # Total up write_bytes after writing.  We'd better find non-zeros.
2814         for ostnum in $(seq $OSTCOUNT); do
2815                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2816                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2817                         obdfilter/$ostname/stats |
2818                         awk '/^write_bytes/ {print $7}' )
2819                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2820                 if (( ${write_bytes:-0} > 0 ))
2821                 then
2822                         all_zeros=false
2823                         break;
2824                 fi
2825         done
2826
2827         if $all_zeros
2828         then
2829                 for ostnum in $(seq $OSTCOUNT); do
2830                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2831                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2832                         do_facet ost$ostnum lctl get_param -n \
2833                                 obdfilter/$ostname/stats
2834                 done
2835                 error "OST not keeping write_bytes stats (b22312)"
2836         fi
2837 }
2838 run_test 33c "test llobdstat and write_bytes"
2839
2840 test_33d() {
2841         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2843         local MDTIDX=1
2844         local remote_dir=$DIR/$tdir/remote_dir
2845
2846         test_mkdir -p $DIR/$tdir
2847         $LFS mkdir -i $MDTIDX $remote_dir ||
2848                 error "create remote directory failed"
2849
2850         touch $remote_dir/$tfile
2851         chmod 444 $remote_dir/$tfile
2852         chown $RUNAS_ID $remote_dir/$tfile
2853
2854         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2855
2856         chown $RUNAS_ID $remote_dir
2857         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2858                                         error "create" || true
2859         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2860                                     error "open RDWR" || true
2861         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2862 }
2863 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2864
2865 test_33e() {
2866         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2867
2868         mkdir $DIR/$tdir
2869
2870         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2871         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2872         mkdir $DIR/$tdir/local_dir
2873
2874         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2875         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2876         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2877
2878         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2879                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2880
2881         rmdir $DIR/$tdir/* || error "rmdir failed"
2882
2883         umask 777
2884         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2885         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2886         mkdir $DIR/$tdir/local_dir
2887
2888         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2889         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2890         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2891
2892         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2893                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2894
2895         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2896
2897         umask 000
2898         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2899         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2900         mkdir $DIR/$tdir/local_dir
2901
2902         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2903         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2904         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2905
2906         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2907                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2908 }
2909 run_test 33e "mkdir and striped directory should have same mode"
2910
2911 cleanup_33f() {
2912         trap 0
2913         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2914 }
2915
2916 test_33f() {
2917         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2918         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2919
2920         mkdir $DIR/$tdir
2921         chmod go+rwx $DIR/$tdir
2922         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2923         trap cleanup_33f EXIT
2924
2925         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2926                 error "cannot create striped directory"
2927
2928         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2929                 error "cannot create files in striped directory"
2930
2931         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2932                 error "cannot remove files in striped directory"
2933
2934         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2935                 error "cannot remove striped directory"
2936
2937         cleanup_33f
2938 }
2939 run_test 33f "nonroot user can create, access, and remove a striped directory"
2940
2941 test_33g() {
2942         mkdir -p $DIR/$tdir/dir2
2943
2944         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2945         echo $err
2946         [[ $err =~ "exists" ]] || error "Not exists error"
2947 }
2948 run_test 33g "nonroot user create already existing root created file"
2949
2950 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2951 test_34a() {
2952         rm -f $DIR/f34
2953         $MCREATE $DIR/f34 || error
2954         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2955         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2956         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2957         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2958 }
2959 run_test 34a "truncate file that has not been opened ==========="
2960
2961 test_34b() {
2962         [ ! -f $DIR/f34 ] && test_34a
2963         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2964         $OPENFILE -f O_RDONLY $DIR/f34
2965         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2966         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2967 }
2968 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2969
2970 test_34c() {
2971         [ ! -f $DIR/f34 ] && test_34a
2972         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2973         $OPENFILE -f O_RDWR $DIR/f34
2974         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2975         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2976 }
2977 run_test 34c "O_RDWR opening file-with-size works =============="
2978
2979 test_34d() {
2980         [ ! -f $DIR/f34 ] && test_34a
2981         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2982         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2983         rm $DIR/f34
2984 }
2985 run_test 34d "write to sparse file ============================="
2986
2987 test_34e() {
2988         rm -f $DIR/f34e
2989         $MCREATE $DIR/f34e || error
2990         $TRUNCATE $DIR/f34e 1000 || error
2991         $CHECKSTAT -s 1000 $DIR/f34e || error
2992         $OPENFILE -f O_RDWR $DIR/f34e
2993         $CHECKSTAT -s 1000 $DIR/f34e || error
2994 }
2995 run_test 34e "create objects, some with size and some without =="
2996
2997 test_34f() { # bug 6242, 6243
2998         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2999         SIZE34F=48000
3000         rm -f $DIR/f34f
3001         $MCREATE $DIR/f34f || error
3002         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3003         dd if=$DIR/f34f of=$TMP/f34f
3004         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3005         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3006         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3007         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3008         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3009 }
3010 run_test 34f "read from a file with no objects until EOF ======="
3011
3012 test_34g() {
3013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3014         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
3015         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
3016         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
3017         cancel_lru_locks osc
3018         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
3019                 error "wrong size after lock cancel"
3020
3021         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
3022         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
3023                 error "expanding truncate failed"
3024         cancel_lru_locks osc
3025         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
3026                 error "wrong expanded size after lock cancel"
3027 }
3028 run_test 34g "truncate long file ==============================="
3029
3030 test_34h() {
3031         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3032         local gid=10
3033         local sz=1000
3034
3035         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
3036         sync # Flush the cache so that multiop below does not block on cache
3037              # flush when getting the group lock
3038         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3039         MULTIPID=$!
3040
3041         # Since just timed wait is not good enough, let's do a sync write
3042         # that way we are sure enough time for a roundtrip + processing
3043         # passed + 2 seconds of extra margin.
3044         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
3045         rm $DIR/${tfile}-1
3046         sleep 2
3047
3048         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3049                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3050                 kill -9 $MULTIPID
3051         fi
3052         wait $MULTIPID
3053         local nsz=`stat -c %s $DIR/$tfile`
3054         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3055 }
3056 run_test 34h "ftruncate file under grouplock should not block"
3057
3058 test_35a() {
3059         cp /bin/sh $DIR/f35a
3060         chmod 444 $DIR/f35a
3061         chown $RUNAS_ID $DIR/f35a
3062         $RUNAS $DIR/f35a && error || true
3063         rm $DIR/f35a
3064 }
3065 run_test 35a "exec file with mode 444 (should return and not leak) ====="
3066
3067 test_36a() {
3068         rm -f $DIR/f36
3069         utime $DIR/f36 || error
3070 }
3071 run_test 36a "MDS utime check (mknod, utime) ==================="
3072
3073 test_36b() {
3074         echo "" > $DIR/f36
3075         utime $DIR/f36 || error
3076 }
3077 run_test 36b "OST utime check (open, utime) ===================="
3078
3079 test_36c() {
3080         rm -f $DIR/d36/f36
3081         test_mkdir $DIR/d36
3082         chown $RUNAS_ID $DIR/d36
3083         $RUNAS utime $DIR/d36/f36 || error
3084 }
3085 run_test 36c "non-root MDS utime check (mknod, utime) =========="
3086
3087 test_36d() {
3088         [ ! -d $DIR/d36 ] && test_36c
3089         echo "" > $DIR/d36/f36
3090         $RUNAS utime $DIR/d36/f36 || error
3091 }
3092 run_test 36d "non-root OST utime check (open, utime) ==========="
3093
3094 test_36e() {
3095         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3096         test_mkdir -p $DIR/$tdir
3097         touch $DIR/$tdir/$tfile
3098         $RUNAS utime $DIR/$tdir/$tfile && \
3099                 error "utime worked, expected failure" || true
3100 }
3101 run_test 36e "utime on non-owned file (should return error) ===="
3102
3103 subr_36fh() {
3104         local fl="$1"
3105         local LANG_SAVE=$LANG
3106         local LC_LANG_SAVE=$LC_LANG
3107         export LANG=C LC_LANG=C # for date language
3108
3109         DATESTR="Dec 20  2000"
3110         test_mkdir -p $DIR/$tdir
3111         lctl set_param fail_loc=$fl
3112         date; date +%s
3113         cp /etc/hosts $DIR/$tdir/$tfile
3114         sync & # write RPC generated with "current" inode timestamp, but delayed
3115         sleep 1
3116         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3117         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3118         cancel_lru_locks osc
3119         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3120         date; date +%s
3121         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3122                 echo "BEFORE: $LS_BEFORE" && \
3123                 echo "AFTER : $LS_AFTER" && \
3124                 echo "WANT  : $DATESTR" && \
3125                 error "$DIR/$tdir/$tfile timestamps changed" || true
3126
3127         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3128 }
3129
3130 test_36f() {
3131         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3132         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3133         subr_36fh "0x80000214"
3134 }
3135 run_test 36f "utime on file racing with OST BRW write =========="
3136
3137 test_36g() {
3138         remote_ost_nodsh && skip "remote OST with nodsh" && return
3139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3140         local fmd_max_age
3141         local fmd_before
3142         local fmd_after
3143
3144         test_mkdir -p $DIR/$tdir
3145         fmd_max_age=$(do_facet ost1 \
3146                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3147                 head -n 1")
3148
3149         fmd_before=$(do_facet ost1 \
3150                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3151         touch $DIR/$tdir/$tfile
3152         sleep $((fmd_max_age + 12))
3153         fmd_after=$(do_facet ost1 \
3154                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3155
3156         echo "fmd_before: $fmd_before"
3157         echo "fmd_after: $fmd_after"
3158         [[ $fmd_after -gt $fmd_before ]] &&
3159                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3160                 error "fmd didn't expire after ping" || true
3161 }
3162 run_test 36g "filter mod data cache expiry ====================="
3163
3164 test_36h() {
3165         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3166         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3167         subr_36fh "0x80000227"
3168 }
3169 run_test 36h "utime on file racing with OST BRW write =========="
3170
3171 test_36i() {
3172         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3173
3174         test_mkdir $DIR/$tdir
3175         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3176
3177         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3178         local new_mtime=$((mtime + 200))
3179
3180         #change Modify time of striped dir
3181         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3182                         error "change mtime failed"
3183
3184         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3185
3186         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3187 }
3188 run_test 36i "change mtime on striped directory"
3189
3190 # test_37 - duplicate with tests 32q 32r
3191
3192 test_38() {
3193         local file=$DIR/$tfile
3194         touch $file
3195         openfile -f O_DIRECTORY $file
3196         local RC=$?
3197         local ENOTDIR=20
3198         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3199         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3200 }
3201 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3202
3203 test_39a() { # was test_39
3204         touch $DIR/$tfile
3205         touch $DIR/${tfile}2
3206 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3207 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3208 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3209         sleep 2
3210         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3211         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3212                 echo "mtime"
3213                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3214                 echo "atime"
3215                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3216                 echo "ctime"
3217                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3218                 error "O_TRUNC didn't change timestamps"
3219         fi
3220 }
3221 run_test 39a "mtime changed on create ==========================="
3222
3223 test_39b() {
3224         test_mkdir -p -c1 $DIR/$tdir
3225         cp -p /etc/passwd $DIR/$tdir/fopen
3226         cp -p /etc/passwd $DIR/$tdir/flink
3227         cp -p /etc/passwd $DIR/$tdir/funlink
3228         cp -p /etc/passwd $DIR/$tdir/frename
3229         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3230
3231         sleep 1
3232         echo "aaaaaa" >> $DIR/$tdir/fopen
3233         echo "aaaaaa" >> $DIR/$tdir/flink
3234         echo "aaaaaa" >> $DIR/$tdir/funlink
3235         echo "aaaaaa" >> $DIR/$tdir/frename
3236
3237         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3238         local link_new=`stat -c %Y $DIR/$tdir/flink`
3239         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3240         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3241
3242         cat $DIR/$tdir/fopen > /dev/null
3243         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3244         rm -f $DIR/$tdir/funlink2
3245         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3246
3247         for (( i=0; i < 2; i++ )) ; do
3248                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3249                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3250                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3251                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3252
3253                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3254                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3255                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3256                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3257
3258                 cancel_lru_locks osc
3259                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3260         done
3261 }
3262 run_test 39b "mtime change on open, link, unlink, rename  ======"
3263
3264 # this should be set to past
3265 TEST_39_MTIME=`date -d "1 year ago" +%s`
3266
3267 # bug 11063
3268 test_39c() {
3269         touch $DIR1/$tfile
3270         sleep 2
3271         local mtime0=`stat -c %Y $DIR1/$tfile`
3272
3273         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3274         local mtime1=`stat -c %Y $DIR1/$tfile`
3275         [ "$mtime1" = $TEST_39_MTIME ] || \
3276                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3277
3278         local d1=`date +%s`
3279         echo hello >> $DIR1/$tfile
3280         local d2=`date +%s`
3281         local mtime2=`stat -c %Y $DIR1/$tfile`
3282         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3283                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3284
3285         mv $DIR1/$tfile $DIR1/$tfile-1
3286
3287         for (( i=0; i < 2; i++ )) ; do
3288                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3289                 [ "$mtime2" = "$mtime3" ] || \
3290                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3291
3292                 cancel_lru_locks osc
3293                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3294         done
3295 }
3296 run_test 39c "mtime change on rename ==========================="
3297
3298 # bug 21114
3299 test_39d() {
3300         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3301         touch $DIR1/$tfile
3302
3303         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3304
3305         for (( i=0; i < 2; i++ )) ; do
3306                 local mtime=`stat -c %Y $DIR1/$tfile`
3307                 [ $mtime = $TEST_39_MTIME ] || \
3308                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3309
3310                 cancel_lru_locks osc
3311                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3312         done
3313 }
3314 run_test 39d "create, utime, stat =============================="
3315
3316 # bug 21114
3317 test_39e() {
3318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3319         touch $DIR1/$tfile
3320         local mtime1=`stat -c %Y $DIR1/$tfile`
3321
3322         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3323
3324         for (( i=0; i < 2; i++ )) ; do
3325                 local mtime2=`stat -c %Y $DIR1/$tfile`
3326                 [ $mtime2 = $TEST_39_MTIME ] || \
3327                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3328
3329                 cancel_lru_locks osc
3330                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3331         done
3332 }
3333 run_test 39e "create, stat, utime, stat ========================"
3334
3335 # bug 21114
3336 test_39f() {
3337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3338         touch $DIR1/$tfile
3339         mtime1=`stat -c %Y $DIR1/$tfile`
3340
3341         sleep 2
3342         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3343
3344         for (( i=0; i < 2; i++ )) ; do
3345                 local mtime2=`stat -c %Y $DIR1/$tfile`
3346                 [ $mtime2 = $TEST_39_MTIME ] || \
3347                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3348
3349                 cancel_lru_locks osc
3350                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3351         done
3352 }
3353 run_test 39f "create, stat, sleep, utime, stat ================="
3354
3355 # bug 11063
3356 test_39g() {
3357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3358         echo hello >> $DIR1/$tfile
3359         local mtime1=`stat -c %Y $DIR1/$tfile`
3360
3361         sleep 2
3362         chmod o+r $DIR1/$tfile
3363
3364         for (( i=0; i < 2; i++ )) ; do
3365                 local mtime2=`stat -c %Y $DIR1/$tfile`
3366                 [ "$mtime1" = "$mtime2" ] || \
3367                         error "lost mtime: $mtime2, should be $mtime1"
3368
3369                 cancel_lru_locks osc
3370                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3371         done
3372 }
3373 run_test 39g "write, chmod, stat ==============================="
3374
3375 # bug 11063
3376 test_39h() {
3377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3378         touch $DIR1/$tfile
3379         sleep 1
3380
3381         local d1=`date`
3382         echo hello >> $DIR1/$tfile
3383         local mtime1=`stat -c %Y $DIR1/$tfile`
3384
3385         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3386         local d2=`date`
3387         if [ "$d1" != "$d2" ]; then
3388                 echo "write and touch not within one second"
3389         else
3390                 for (( i=0; i < 2; i++ )) ; do
3391                         local mtime2=`stat -c %Y $DIR1/$tfile`
3392                         [ "$mtime2" = $TEST_39_MTIME ] || \
3393                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3394
3395                         cancel_lru_locks osc
3396                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3397                 done
3398         fi
3399 }
3400 run_test 39h "write, utime within one second, stat ============="
3401
3402 test_39i() {
3403         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3404         touch $DIR1/$tfile
3405         sleep 1
3406
3407         echo hello >> $DIR1/$tfile
3408         local mtime1=`stat -c %Y $DIR1/$tfile`
3409
3410         mv $DIR1/$tfile $DIR1/$tfile-1
3411
3412         for (( i=0; i < 2; i++ )) ; do
3413                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3414
3415                 [ "$mtime1" = "$mtime2" ] || \
3416                         error "lost mtime: $mtime2, should be $mtime1"
3417
3418                 cancel_lru_locks osc
3419                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3420         done
3421 }
3422 run_test 39i "write, rename, stat =============================="
3423
3424 test_39j() {
3425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3426         start_full_debug_logging
3427         touch $DIR1/$tfile
3428         sleep 1
3429
3430         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3431         lctl set_param fail_loc=0x80000412
3432         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3433                 error "multiop failed"
3434         local multipid=$!
3435         local mtime1=`stat -c %Y $DIR1/$tfile`
3436
3437         mv $DIR1/$tfile $DIR1/$tfile-1
3438
3439         kill -USR1 $multipid
3440         wait $multipid || error "multiop close failed"
3441
3442         for (( i=0; i < 2; i++ )) ; do
3443                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3444                 [ "$mtime1" = "$mtime2" ] ||
3445                         error "mtime is lost on close: $mtime2, " \
3446                               "should be $mtime1"
3447
3448                 cancel_lru_locks osc
3449                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3450         done
3451         lctl set_param fail_loc=0
3452         stop_full_debug_logging
3453 }
3454 run_test 39j "write, rename, close, stat ======================="
3455
3456 test_39k() {
3457         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3458         touch $DIR1/$tfile
3459         sleep 1
3460
3461         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3462         local multipid=$!
3463         local mtime1=`stat -c %Y $DIR1/$tfile`
3464
3465         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3466
3467         kill -USR1 $multipid
3468         wait $multipid || error "multiop close failed"
3469
3470         for (( i=0; i < 2; i++ )) ; do
3471                 local mtime2=`stat -c %Y $DIR1/$tfile`
3472
3473                 [ "$mtime2" = $TEST_39_MTIME ] || \
3474                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3475
3476                 cancel_lru_locks osc
3477                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3478         done
3479 }
3480 run_test 39k "write, utime, close, stat ========================"
3481
3482 # this should be set to future
3483 TEST_39_ATIME=`date -d "1 year" +%s`
3484
3485 test_39l() {
3486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3487         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3488         local atime_diff=$(do_facet $SINGLEMDS \
3489                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3490         rm -rf $DIR/$tdir
3491         mkdir -p $DIR/$tdir
3492
3493         # test setting directory atime to future
3494         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3495         local atime=$(stat -c %X $DIR/$tdir)
3496         [ "$atime" = $TEST_39_ATIME ] ||
3497                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3498
3499         # test setting directory atime from future to now
3500         local now=$(date +%s)
3501         touch -a -d @$now $DIR/$tdir
3502
3503         atime=$(stat -c %X $DIR/$tdir)
3504         [ "$atime" -eq "$now"  ] ||
3505                 error "atime is not updated from future: $atime, $now"
3506
3507         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3508         sleep 3
3509
3510         # test setting directory atime when now > dir atime + atime_diff
3511         local d1=$(date +%s)
3512         ls $DIR/$tdir
3513         local d2=$(date +%s)
3514         cancel_lru_locks mdc
3515         atime=$(stat -c %X $DIR/$tdir)
3516         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3517                 error "atime is not updated  : $atime, should be $d2"
3518
3519         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3520         sleep 3
3521
3522         # test not setting directory atime when now < dir atime + atime_diff
3523         ls $DIR/$tdir
3524         cancel_lru_locks mdc
3525         atime=$(stat -c %X $DIR/$tdir)
3526         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3527                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3528
3529         do_facet $SINGLEMDS \
3530                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3531 }
3532 run_test 39l "directory atime update ==========================="
3533
3534 test_39m() {
3535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3536         touch $DIR1/$tfile
3537         sleep 2
3538         local far_past_mtime=$(date -d "May 29 1953" +%s)
3539         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3540
3541         touch -m -d @$far_past_mtime $DIR1/$tfile
3542         touch -a -d @$far_past_atime $DIR1/$tfile
3543
3544         for (( i=0; i < 2; i++ )) ; do
3545                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3546                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3547                         error "atime or mtime set incorrectly"
3548
3549                 cancel_lru_locks osc
3550                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3551         done
3552 }
3553 run_test 39m "test atime and mtime before 1970"
3554
3555 test_39n() { # LU-3832
3556         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3557         local atime_diff=$(do_facet $SINGLEMDS \
3558                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3559         local atime0
3560         local atime1
3561         local atime2
3562
3563         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3564
3565         rm -rf $DIR/$tfile
3566         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3567         atime0=$(stat -c %X $DIR/$tfile)
3568
3569         sleep 5
3570         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3571         atime1=$(stat -c %X $DIR/$tfile)
3572
3573         sleep 5
3574         cancel_lru_locks mdc
3575         cancel_lru_locks osc
3576         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3577         atime2=$(stat -c %X $DIR/$tfile)
3578
3579         do_facet $SINGLEMDS \
3580                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3581
3582         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3583         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3584 }
3585 run_test 39n "check that O_NOATIME is honored"
3586
3587 test_39o() {
3588         TESTDIR=$DIR/$tdir/$tfile
3589         [ -e $TESTDIR ] && rm -rf $TESTDIR
3590         mkdir -p $TESTDIR
3591         cd $TESTDIR
3592         links1=2
3593         ls
3594         mkdir a b
3595         ls
3596         links2=$(stat -c %h .)
3597         [ $(($links1 + 2)) != $links2 ] &&
3598                 error "wrong links count $(($links1 + 2)) != $links2"
3599         rmdir b
3600         links3=$(stat -c %h .)
3601         [ $(($links1 + 1)) != $links3 ] &&
3602                 error "wrong links count $links1 != $links3"
3603         return 0
3604 }
3605 run_test 39o "directory cached attributes updated after create ========"
3606
3607 test_39p() {
3608         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3609         local MDTIDX=1
3610         TESTDIR=$DIR/$tdir/$tfile
3611         [ -e $TESTDIR ] && rm -rf $TESTDIR
3612         test_mkdir -p $TESTDIR
3613         cd $TESTDIR
3614         links1=2
3615         ls
3616         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3617         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3618         ls
3619         links2=$(stat -c %h .)
3620         [ $(($links1 + 2)) != $links2 ] &&
3621                 error "wrong links count $(($links1 + 2)) != $links2"
3622         rmdir remote_dir2
3623         links3=$(stat -c %h .)
3624         [ $(($links1 + 1)) != $links3 ] &&
3625                 error "wrong links count $links1 != $links3"
3626         return 0
3627 }
3628 run_test 39p "remote directory cached attributes updated after create ========"
3629
3630
3631 test_39q() { # LU-8041
3632         local testdir=$DIR/$tdir
3633         mkdir -p $testdir
3634         multiop_bg_pause $testdir D_c || error "multiop failed"
3635         local multipid=$!
3636         cancel_lru_locks mdc
3637         kill -USR1 $multipid
3638         local atime=$(stat -c %X $testdir)
3639         [ "$atime" -ne 0 ] || error "atime is zero"
3640 }
3641 run_test 39q "close won't zero out atime"
3642
3643 test_40() {
3644         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3645         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3646                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3647         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3648                 error "$tfile is not 4096 bytes in size"
3649 }
3650 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3651
3652 test_41() {
3653         # bug 1553
3654         small_write $DIR/f41 18
3655 }
3656 run_test 41 "test small file write + fstat ====================="
3657
3658 count_ost_writes() {
3659         lctl get_param -n osc.*.stats |
3660                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3661                         END { printf("%0.0f", writes) }'
3662 }
3663
3664 # decent default
3665 WRITEBACK_SAVE=500
3666 DIRTY_RATIO_SAVE=40
3667 MAX_DIRTY_RATIO=50
3668 BG_DIRTY_RATIO_SAVE=10
3669 MAX_BG_DIRTY_RATIO=25
3670
3671 start_writeback() {
3672         trap 0
3673         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3674         # dirty_ratio, dirty_background_ratio
3675         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3676                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3677                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3678                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3679         else
3680                 # if file not here, we are a 2.4 kernel
3681                 kill -CONT `pidof kupdated`
3682         fi
3683 }
3684
3685 stop_writeback() {
3686         # setup the trap first, so someone cannot exit the test at the
3687         # exact wrong time and mess up a machine
3688         trap start_writeback EXIT
3689         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3690         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3691                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3692                 sysctl -w vm.dirty_writeback_centisecs=0
3693                 sysctl -w vm.dirty_writeback_centisecs=0
3694                 # save and increase /proc/sys/vm/dirty_ratio
3695                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3696                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3697                 # save and increase /proc/sys/vm/dirty_background_ratio
3698                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3699                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3700         else
3701                 # if file not here, we are a 2.4 kernel
3702                 kill -STOP `pidof kupdated`
3703         fi
3704 }
3705
3706 # ensure that all stripes have some grant before we test client-side cache
3707 setup_test42() {
3708         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3709                 dd if=/dev/zero of=$i bs=4k count=1
3710                 rm $i
3711         done
3712 }
3713
3714 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3715 # file truncation, and file removal.
3716 test_42a() {
3717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3718         setup_test42
3719         cancel_lru_locks osc
3720         stop_writeback
3721         sync; sleep 1; sync # just to be safe
3722         BEFOREWRITES=`count_ost_writes`
3723         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3724         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3725         AFTERWRITES=`count_ost_writes`
3726         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3727                 error "$BEFOREWRITES < $AFTERWRITES"
3728         start_writeback
3729 }
3730 run_test 42a "ensure that we don't flush on close"
3731
3732 test_42b() {
3733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3734         setup_test42
3735         cancel_lru_locks osc
3736         stop_writeback
3737         sync
3738         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3739         BEFOREWRITES=$(count_ost_writes)
3740         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3741         AFTERWRITES=$(count_ost_writes)
3742         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3743                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3744         fi
3745         BEFOREWRITES=$(count_ost_writes)
3746         sync || error "sync: $?"
3747         AFTERWRITES=$(count_ost_writes)
3748         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3749                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3750         fi
3751         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3752         start_writeback
3753         return 0
3754 }
3755 run_test 42b "test destroy of file with cached dirty data ======"
3756
3757 # if these tests just want to test the effect of truncation,
3758 # they have to be very careful.  consider:
3759 # - the first open gets a {0,EOF}PR lock
3760 # - the first write conflicts and gets a {0, count-1}PW
3761 # - the rest of the writes are under {count,EOF}PW
3762 # - the open for truncate tries to match a {0,EOF}PR
3763 #   for the filesize and cancels the PWs.
3764 # any number of fixes (don't get {0,EOF} on open, match
3765 # composite locks, do smarter file size management) fix
3766 # this, but for now we want these tests to verify that
3767 # the cancellation with truncate intent works, so we
3768 # start the file with a full-file pw lock to match against
3769 # until the truncate.
3770 trunc_test() {
3771         test=$1
3772         file=$DIR/$test
3773         offset=$2
3774         cancel_lru_locks osc
3775         stop_writeback
3776         # prime the file with 0,EOF PW to match
3777         touch $file
3778         $TRUNCATE $file 0
3779         sync; sync
3780         # now the real test..
3781         dd if=/dev/zero of=$file bs=1024 count=100
3782         BEFOREWRITES=`count_ost_writes`
3783         $TRUNCATE $file $offset
3784         cancel_lru_locks osc
3785         AFTERWRITES=`count_ost_writes`
3786         start_writeback
3787 }
3788
3789 test_42c() {
3790         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3791         trunc_test 42c 1024
3792         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3793             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3794         rm $file
3795 }
3796 run_test 42c "test partial truncate of file with cached dirty data"
3797
3798 test_42d() {
3799         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3800         trunc_test 42d 0
3801         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3802             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3803         rm $file
3804 }
3805 run_test 42d "test complete truncate of file with cached dirty data"
3806
3807 test_42e() { # bug22074
3808         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3809         local TDIR=$DIR/${tdir}e
3810         local pagesz=$(page_size)
3811         local pages=16 # hardcoded 16 pages, don't change it.
3812         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3813         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3814         local max_dirty_mb
3815         local warmup_files
3816
3817         test_mkdir -p $DIR/${tdir}e
3818         $SETSTRIPE -c 1 $TDIR
3819         createmany -o $TDIR/f $files
3820
3821         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3822
3823         # we assume that with $OSTCOUNT files, at least one of them will
3824         # be allocated on OST0.
3825         warmup_files=$((OSTCOUNT * max_dirty_mb))
3826         createmany -o $TDIR/w $warmup_files
3827
3828         # write a large amount of data into one file and sync, to get good
3829         # avail_grant number from OST.
3830         for ((i=0; i<$warmup_files; i++)); do
3831                 idx=$($GETSTRIPE -i $TDIR/w$i)
3832                 [ $idx -ne 0 ] && continue
3833                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3834                 break
3835         done
3836         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3837         sync
3838         $LCTL get_param $proc_osc0/cur_dirty_bytes
3839         $LCTL get_param $proc_osc0/cur_grant_bytes
3840
3841         # create as much dirty pages as we can while not to trigger the actual
3842         # RPCs directly. but depends on the env, VFS may trigger flush during this
3843         # period, hopefully we are good.
3844         for ((i=0; i<$warmup_files; i++)); do
3845                 idx=$($GETSTRIPE -i $TDIR/w$i)
3846                 [ $idx -ne 0 ] && continue
3847                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3848         done
3849         $LCTL get_param $proc_osc0/cur_dirty_bytes
3850         $LCTL get_param $proc_osc0/cur_grant_bytes
3851
3852         # perform the real test
3853         $LCTL set_param $proc_osc0/rpc_stats 0
3854         for ((;i<$files; i++)); do
3855                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3856                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3857         done
3858         sync
3859         $LCTL get_param $proc_osc0/rpc_stats
3860
3861         local percent=0
3862         local have_ppr=false
3863         $LCTL get_param $proc_osc0/rpc_stats |
3864                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3865                         # skip lines until we are at the RPC histogram data
3866                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3867                         $have_ppr || continue
3868
3869                         # we only want the percent stat for < 16 pages
3870                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3871
3872                         percent=$((percent + WPCT))
3873                         if [[ $percent -gt 15 ]]; then
3874                                 error "less than 16-pages write RPCs" \
3875                                       "$percent% > 15%"
3876                                 break
3877                         fi
3878                 done
3879         rm -rf $TDIR
3880 }
3881 run_test 42e "verify sub-RPC writes are not done synchronously"
3882
3883 test_43A() { # was test_43
3884         test_mkdir -p $DIR/$tdir
3885         cp -p /bin/ls $DIR/$tdir/$tfile
3886         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3887         pid=$!
3888         # give multiop a chance to open
3889         sleep 1
3890
3891         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
3892         kill -USR1 $pid
3893 }
3894 run_test 43A "execution of file opened for write should return -ETXTBSY"
3895
3896 test_43a() {
3897         test_mkdir $DIR/$tdir
3898         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3899                 cp -p multiop $DIR/$tdir/multiop
3900         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3901                 error "multiop open $TMP/$tfile.junk failed"
3902         MULTIOP_PID=$!
3903         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3904         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3905         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3906 }
3907 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3908
3909 test_43b() {
3910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3911         test_mkdir $DIR/$tdir
3912         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3913                 cp -p multiop $DIR/$tdir/multiop
3914         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3915                 error "multiop open $TMP/$tfile.junk failed"
3916         MULTIOP_PID=$!
3917         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3918         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3919         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3920 }
3921 run_test 43b "truncate of file being executed should return -ETXTBSY"
3922
3923 test_43c() {
3924         local testdir="$DIR/$tdir"
3925         test_mkdir $testdir
3926         cp $SHELL $testdir/
3927         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
3928                 ( cd $testdir && md5sum -c )
3929 }
3930 run_test 43c "md5sum of copy into lustre"
3931
3932 test_44A() { # was test_44
3933         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
3934         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3935         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3936 }
3937 run_test 44A "zero length read from a sparse stripe"
3938
3939 test_44a() {
3940         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3941                 awk '{ print $2 }')
3942         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3943         [[ $nstripe -gt $OSTCOUNT ]] &&
3944             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3945             return
3946         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3947                 awk '{ print $2 }')
3948         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3949                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3950                         awk '{ print $2 }')
3951         fi
3952
3953         OFFSETS="0 $((stride/2)) $((stride-1))"
3954         for offset in $OFFSETS; do
3955                 for i in $(seq 0 $((nstripe-1))); do
3956                         local GLOBALOFFSETS=""
3957                         # size in Bytes
3958                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3959                         local myfn=$DIR/d44a-$size
3960                         echo "--------writing $myfn at $size"
3961                         ll_sparseness_write $myfn $size ||
3962                                 error "ll_sparseness_write"
3963                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3964                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3965                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3966
3967                         for j in $(seq 0 $((nstripe-1))); do
3968                                 # size in Bytes
3969                                 size=$((((j + $nstripe )*$stride + $offset)))
3970                                 ll_sparseness_write $myfn $size ||
3971                                         error "ll_sparseness_write"
3972                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3973                         done
3974                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3975                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3976                         rm -f $myfn
3977                 done
3978         done
3979 }
3980 run_test 44a "test sparse pwrite ==============================="
3981
3982 dirty_osc_total() {
3983         tot=0
3984         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3985                 tot=$(($tot + $d))
3986         done
3987         echo $tot
3988 }
3989 do_dirty_record() {
3990         before=`dirty_osc_total`
3991         echo executing "\"$*\""
3992         eval $*
3993         after=`dirty_osc_total`
3994         echo before $before, after $after
3995 }
3996 test_45() {
3997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3998         f="$DIR/f45"
3999         # Obtain grants from OST if it supports it
4000         echo blah > ${f}_grant
4001         stop_writeback
4002         sync
4003         do_dirty_record "echo blah > $f"
4004         [[ $before -eq $after ]] && error "write wasn't cached"
4005         do_dirty_record "> $f"
4006         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4007         do_dirty_record "echo blah > $f"
4008         [[ $before -eq $after ]] && error "write wasn't cached"
4009         do_dirty_record "sync"
4010         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4011         do_dirty_record "echo blah > $f"
4012         [[ $before -eq $after ]] && error "write wasn't cached"
4013         do_dirty_record "cancel_lru_locks osc"
4014         [[ $before -gt $after ]] ||
4015                 error "lock cancellation didn't lower dirty count"
4016         start_writeback
4017 }
4018 run_test 45 "osc io page accounting ============================"
4019
4020 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4021 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4022 # objects offset and an assert hit when an rpc was built with 1023's mapped
4023 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4024 test_46() {
4025         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4026         f="$DIR/f46"
4027         stop_writeback
4028         sync
4029         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4030         sync
4031         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
4032         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4033         sync
4034         start_writeback
4035 }
4036 run_test 46 "dirtying a previously written page ================"
4037
4038 # test_47 is removed "Device nodes check" is moved to test_28
4039
4040 test_48a() { # bug 2399
4041         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
4042         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
4043                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
4044                 return
4045         test_mkdir $DIR/$tdir
4046         cd $DIR/$tdir
4047         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4048         test_mkdir $DIR/$tdir || error "recreate directory failed"
4049         touch foo || error "'touch foo' failed after recreating cwd"
4050         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
4051         touch .foo || error "'touch .foo' failed after recreating cwd"
4052         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
4053         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4054         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4055         cd . || error "'cd .' failed after recreating cwd"
4056         test_mkdir . && error "'mkdir .' worked after recreating cwd"
4057         rmdir . && error "'rmdir .' worked after recreating cwd"
4058         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4059         cd .. || error "'cd ..' failed after recreating cwd"
4060 }
4061 run_test 48a "Access renamed working dir (should return errors)="
4062
4063 test_48b() { # bug 2399
4064         rm -rf $DIR/$tdir
4065         test_mkdir $DIR/$tdir
4066         cd $DIR/$tdir
4067         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4068         touch foo && error "'touch foo' worked after removing cwd"
4069         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
4070         touch .foo && error "'touch .foo' worked after removing cwd"
4071         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4072         ls . > /dev/null && error "'ls .' worked after removing cwd"
4073         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4074         test_mkdir . && error "'mkdir .' worked after removing cwd"
4075         rmdir . && error "'rmdir .' worked after removing cwd"
4076         ln -s . foo && error "'ln -s .' worked after removing cwd"
4077         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4078 }
4079 run_test 48b "Access removed working dir (should return errors)="
4080
4081 test_48c() { # bug 2350
4082         #lctl set_param debug=-1
4083         #set -vx
4084         rm -rf $DIR/$tdir
4085         test_mkdir -p $DIR/$tdir/dir
4086         cd $DIR/$tdir/dir
4087         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4088         $TRACE touch foo && error "touch foo worked after removing cwd"
4089         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
4090         touch .foo && error "touch .foo worked after removing cwd"
4091         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
4092         $TRACE ls . && error "'ls .' worked after removing cwd"
4093         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4094         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
4095         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4096         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4097         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4098 }
4099 run_test 48c "Access removed working subdir (should return errors)"
4100
4101 test_48d() { # bug 2350
4102         #lctl set_param debug=-1
4103         #set -vx
4104         rm -rf $DIR/$tdir
4105         test_mkdir -p $DIR/$tdir/dir
4106         cd $DIR/$tdir/dir
4107         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4108         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4109         $TRACE touch foo && error "'touch foo' worked after removing parent"
4110         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
4111         touch .foo && error "'touch .foo' worked after removing parent"
4112         test_mkdir .foo && error "mkdir .foo worked after removing parent"
4113         $TRACE ls . && error "'ls .' worked after removing parent"
4114         $TRACE ls .. && error "'ls ..' worked after removing parent"
4115         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
4116         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4117         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4118         true
4119 }
4120 run_test 48d "Access removed parent subdir (should return errors)"
4121
4122 test_48e() { # bug 4134
4123         #lctl set_param debug=-1
4124         #set -vx
4125         rm -rf $DIR/$tdir
4126         test_mkdir -p $DIR/$tdir/dir
4127         cd $DIR/$tdir/dir
4128         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4129         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4130         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4131         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4132         # On a buggy kernel addition of "touch foo" after cd .. will
4133         # produce kernel oops in lookup_hash_it
4134         touch ../foo && error "'cd ..' worked after recreate parent"
4135         cd $DIR
4136         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4137 }
4138 run_test 48e "Access to recreated parent subdir (should return errors)"
4139
4140 test_49() { # LU-1030
4141         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4142         remote_ost_nodsh && skip "remote OST with nodsh" && return
4143         # get ost1 size - lustre-OST0000
4144         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4145                 awk '{ print $4 }')
4146         # write 800M at maximum
4147         [[ $ost1_size -lt 2 ]] && ost1_size=2
4148         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4149
4150         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4151         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4152         local dd_pid=$!
4153
4154         # change max_pages_per_rpc while writing the file
4155         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4156         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4157         # loop until dd process exits
4158         while ps ax -opid | grep -wq $dd_pid; do
4159                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4160                 sleep $((RANDOM % 5 + 1))
4161         done
4162         # restore original max_pages_per_rpc
4163         $LCTL set_param $osc1_mppc=$orig_mppc
4164         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4165 }
4166 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4167
4168 test_50() {
4169         # bug 1485
4170         test_mkdir $DIR/$tdir
4171         cd $DIR/$tdir
4172         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4173 }
4174 run_test 50 "special situations: /proc symlinks  ==============="
4175
4176 test_51a() {    # was test_51
4177         # bug 1516 - create an empty entry right after ".." then split dir
4178         test_mkdir -c1 $DIR/$tdir
4179         touch $DIR/$tdir/foo
4180         $MCREATE $DIR/$tdir/bar
4181         rm $DIR/$tdir/foo
4182         createmany -m $DIR/$tdir/longfile 201
4183         FNUM=202
4184         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4185                 $MCREATE $DIR/$tdir/longfile$FNUM
4186                 FNUM=$(($FNUM + 1))
4187                 echo -n "+"
4188         done
4189         echo
4190         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4191 }
4192 run_test 51a "special situations: split htree with empty entry =="
4193
4194 cleanup_print_lfs_df () {
4195         trap 0
4196         $LFS df
4197         $LFS df -i
4198 }
4199
4200 test_51b() {
4201         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4202         local dir=$DIR/$tdir
4203
4204         local nrdirs=$((65536 + 100))
4205
4206         # cleanup the directory
4207         rm -fr $dir
4208
4209         test_mkdir -p -c1 $dir
4210
4211         $LFS df
4212         $LFS df -i
4213         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4214         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4215         [[ $numfree -lt $nrdirs ]] &&
4216                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4217                 return
4218
4219         # need to check free space for the directories as well
4220         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4221         numfree=$(( blkfree / $(fs_inode_ksize) ))
4222         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4223                 return
4224
4225         trap cleanup_print_lfs_df EXIT
4226
4227         # create files
4228         createmany -d $dir/d $nrdirs || {
4229                 unlinkmany $dir/d $nrdirs
4230                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4231         }
4232
4233         # really created :
4234         nrdirs=$(ls -U $dir | wc -l)
4235
4236         # unlink all but 100 subdirectories, then check it still works
4237         local left=100
4238         local delete=$((nrdirs - left))
4239
4240         $LFS df
4241         $LFS df -i
4242
4243         # for ldiskfs the nlink count should be 1, but this is OSD specific
4244         # and so this is listed for informational purposes only
4245         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4246         unlinkmany -d $dir/d $delete ||
4247                 error "unlink of first $delete subdirs failed"
4248
4249         echo "nlink between: $(stat -c %h $dir)"
4250         local found=$(ls -U $dir | wc -l)
4251         [ $found -ne $left ] &&
4252                 error "can't find subdirs: found only $found, expected $left"
4253
4254         unlinkmany -d $dir/d $delete $left ||
4255                 error "unlink of second $left subdirs failed"
4256         # regardless of whether the backing filesystem tracks nlink accurately
4257         # or not, the nlink count shouldn't be more than "." and ".." here
4258         local after=$(stat -c %h $dir)
4259         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4260                 echo "nlink after: $after"
4261
4262         cleanup_print_lfs_df
4263 }
4264 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4265
4266 test_51d() {
4267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4268         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
4269         test_mkdir -p $DIR/$tdir
4270         createmany -o $DIR/$tdir/t- 1000
4271         $GETSTRIPE $DIR/$tdir > $TMP/$tfile
4272         for N in $(seq 0 $((OSTCOUNT - 1))); do
4273                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4274                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4275                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4276                         '($1 == '$N') { objs += 1 } \
4277                         END { printf("%0.0f", objs) }')
4278                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4279         done
4280         unlinkmany $DIR/$tdir/t- 1000
4281
4282         NLAST=0
4283         for N in $(seq 1 $((OSTCOUNT - 1))); do
4284                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4285                         error "OST $N has less objects vs OST $NLAST" \
4286                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4287                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4288                         error "OST $N has less objects vs OST $NLAST" \
4289                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4290
4291                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4292                         error "OST $N has less #0 objects vs OST $NLAST" \
4293                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4294                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4295                         error "OST $N has less #0 objects vs OST $NLAST" \
4296                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4297                 NLAST=$N
4298         done
4299         rm -f $TMP/$tfile
4300 }
4301 run_test 51d "check object distribution"
4302
4303 test_51e() {
4304         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4305                 skip "ldiskfs only test"
4306                 return
4307         fi
4308
4309         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4310         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4311
4312         touch $DIR/$tdir/d0/foo
4313         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4314                 error "file exceed 65000 nlink limit!"
4315         unlinkmany $DIR/$tdir/d0/f- 65001
4316         return 0
4317 }
4318 run_test 51e "check file nlink limit"
4319
4320 test_51f() {
4321         test_mkdir $DIR/$tdir
4322
4323         local max=100000
4324         local ulimit_old=$(ulimit -n)
4325         local spare=20 # number of spare fd's for scripts/libraries, etc.
4326         local mdt=$(lfs getstripe -M $DIR/$tdir)
4327         local numfree=$(lfs df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4328
4329         echo "MDT$mdt numfree=$numfree, max=$max"
4330         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4331         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4332                 while ! ulimit -n $((numfree + spare)); do
4333                         numfree=$((numfree * 3 / 4))
4334                 done
4335                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4336         else
4337                 echo "left ulimit at $ulimit_old"
4338         fi
4339
4340         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4341                 unlinkmany $DIR/$tdir/f $numfree
4342                 error "create+open $numfree files in $DIR/$tdir failed"
4343         }
4344         ulimit -n $ulimit_old
4345
4346         # if createmany exits at 120s there will be fewer than $numfree files
4347         unlinkmany $DIR/$tdir/f $numfree || true
4348 }
4349 run_test 51f "check many open files limit"
4350
4351 test_52a() {
4352         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4353         test_mkdir -p $DIR/$tdir
4354         touch $DIR/$tdir/foo
4355         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4356         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4357         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4358         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4359         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4360                                         error "link worked"
4361         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4362         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4363         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4364                                                      error "lsattr"
4365         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4366         cp -r $DIR/$tdir $TMP/
4367         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4368 }
4369 run_test 52a "append-only flag test (should return errors)"
4370
4371 test_52b() {
4372         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4373         test_mkdir -p $DIR/$tdir
4374         touch $DIR/$tdir/foo
4375         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4376         cat test > $DIR/$tdir/foo && error "cat test worked"
4377         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4378         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4379         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4380                                         error "link worked"
4381         echo foo >> $DIR/$tdir/foo && error "echo worked"
4382         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4383         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4384         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4385         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4386                                                         error "lsattr"
4387         chattr -i $DIR/$tdir/foo || error "chattr failed"
4388
4389         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4390 }
4391 run_test 52b "immutable flag test (should return errors) ======="
4392
4393 test_53() {
4394         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4395         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4396         remote_ost_nodsh && skip "remote OST with nodsh" && return
4397
4398         local param
4399         local param_seq
4400         local ostname
4401         local mds_last
4402         local mds_last_seq
4403         local ost_last
4404         local ost_last_seq
4405         local ost_last_id
4406         local ostnum
4407         local node
4408         local found=false
4409         local support_last_seq=true
4410
4411         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4412                 support_last_seq=false
4413
4414         # only test MDT0000
4415         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4416         local value
4417         for value in $(do_facet $SINGLEMDS \
4418                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4419                 param=$(echo ${value[0]} | cut -d "=" -f1)
4420                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4421
4422                 if $support_last_seq; then
4423                         param_seq=$(echo $param |
4424                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4425                         mds_last_seq=$(do_facet $SINGLEMDS \
4426                                        $LCTL get_param -n $param_seq)
4427                 fi
4428                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4429
4430                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4431                 node=$(facet_active_host ost$((ostnum+1)))
4432                 param="obdfilter.$ostname.last_id"
4433                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4434                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4435                         ost_last_id=$ost_last
4436
4437                         if $support_last_seq; then
4438                                 ost_last_id=$(echo $ost_last |
4439                                               awk -F':' '{print $2}' |
4440                                               sed -e "s/^0x//g")
4441                                 ost_last_seq=$(echo $ost_last |
4442                                                awk -F':' '{print $1}')
4443                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4444                         fi
4445
4446                         if [[ $ost_last_id != $mds_last ]]; then
4447                                 error "$ost_last_id != $mds_last"
4448                         else
4449                                 found=true
4450                                 break
4451                         fi
4452                 done
4453         done
4454         $found || error "can not match last_seq/last_id for $mdtosc"
4455         return 0
4456 }
4457 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4458
4459 test_54a() {
4460         perl -MSocket -e ';' || { skip "no Socket perl module installed" && return; }
4461
4462         $SOCKETSERVER $DIR/socket ||
4463                 error "$SOCKETSERVER $DIR/socket failed: $?"
4464         $SOCKETCLIENT $DIR/socket ||
4465                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4466         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4467 }
4468 run_test 54a "unix domain socket test =========================="
4469
4470 test_54b() {
4471         f="$DIR/f54b"
4472         mknod $f c 1 3
4473         chmod 0666 $f
4474         dd if=/dev/zero of=$f bs=$(page_size) count=1
4475 }
4476 run_test 54b "char device works in lustre ======================"
4477
4478 find_loop_dev() {
4479         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4480         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4481         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4482
4483         for i in $(seq 3 7); do
4484                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4485                 LOOPDEV=$LOOPBASE$i
4486                 LOOPNUM=$i
4487                 break
4488         done
4489 }
4490
4491 cleanup_54c() {
4492         local rc=0
4493         loopdev="$DIR/loop54c"
4494
4495         trap 0
4496         $UMOUNT $DIR/$tdir || rc=$?
4497         losetup -d $loopdev || true
4498         losetup -d $LOOPDEV || true
4499         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4500         return $rc
4501 }
4502
4503 test_54c() {
4504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4505         loopdev="$DIR/loop54c"
4506
4507         find_loop_dev
4508         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4509         trap cleanup_54c EXIT
4510         mknod $loopdev b 7 $LOOPNUM
4511         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4512         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4513         losetup $loopdev $DIR/$tfile ||
4514                 error "can't set up $loopdev for $DIR/$tfile"
4515         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4516         test_mkdir -p $DIR/$tdir
4517         mount -t ext2 $loopdev $DIR/$tdir ||
4518                 error "error mounting $loopdev on $DIR/$tdir"
4519         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4520                 error "dd write"
4521         df $DIR/$tdir
4522         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4523                 error "dd read"
4524         cleanup_54c
4525 }
4526 run_test 54c "block device works in lustre ====================="
4527
4528 test_54d() {
4529         f="$DIR/f54d"
4530         string="aaaaaa"
4531         mknod $f p
4532         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4533 }
4534 run_test 54d "fifo device works in lustre ======================"
4535
4536 test_54e() {
4537         f="$DIR/f54e"
4538         string="aaaaaa"
4539         cp -aL /dev/console $f
4540         echo $string > $f || error "echo $string to $f failed"
4541 }
4542 run_test 54e "console/tty device works in lustre ======================"
4543
4544 #The test_55 used to be iopen test and it was removed by bz#24037.
4545 #run_test 55 "check iopen_connect_dentry() ======================"
4546
4547 test_56a() {    # was test_56
4548         rm -rf $DIR/$tdir
4549         $SETSTRIPE -d $DIR
4550         test_mkdir -p $DIR/$tdir/dir
4551         NUMFILES=3
4552         NUMFILESx2=$(($NUMFILES * 2))
4553         for i in $(seq 1 $NUMFILES); do
4554                 touch $DIR/$tdir/file$i
4555                 touch $DIR/$tdir/dir/file$i
4556         done
4557
4558         # test lfs getstripe with --recursive
4559         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4560         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4561                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4562         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4563         [[ $FILENUM -eq $NUMFILES ]] ||
4564                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4565         echo "$GETSTRIPE --recursive passed."
4566
4567         # test lfs getstripe with file instead of dir
4568         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4569         [[ $FILENUM -eq 1 ]] ||
4570                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4571         echo "$GETSTRIPE file1 passed."
4572
4573         #test lfs getstripe with --verbose
4574         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4575                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4576                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4577         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4578                 error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4579
4580         #test lfs getstripe with -v prints lmm_fid
4581         [[ $($GETSTRIPE -v $DIR/$tdir | grep -c lmm_fid) -eq $NUMFILES ]] ||
4582                 error "$GETSTRIPE -v $DIR/$tdir: want $NUMFILES lmm_fid: lines"
4583         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_fid) -eq 0 ]] ||
4584                 error "$GETSTRIPE $DIR/$tdir: showed lmm_fid"
4585         echo "$GETSTRIPE --verbose passed."
4586
4587         #check for FID information
4588         local fid1=$($GETSTRIPE --fid $DIR/$tdir/file1)
4589         local fid2=$($GETSTRIPE --verbose $DIR/$tdir/file1 |
4590                        awk '/lmm_fid: / { print $2 }')
4591         local fid3=$($LFS path2fid $DIR/$tdir/file1)
4592         [ "$fid1" != "$fid2" ] &&
4593                 error "getstripe --fid $fid1 != getstripe --verbose $fid2"
4594         [ "$fid1" != "$fid3" ] &&
4595                 error "getstripe --fid $fid1 != lfs path2fid $fid3"
4596         echo "$GETSTRIPE --fid passed."
4597
4598         #test lfs getstripe with --obd
4599         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4600                 grep -q "unknown obduuid" ||
4601                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4602
4603         [[ $OSTCOUNT -lt 2 ]] &&
4604                 skip_env "skipping other $GETSTRIPE --obd test" && return
4605
4606         OSTIDX=1
4607         OBDUUID=$(ostuuid_from_index $OSTIDX)
4608         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4609         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4610         [[ $FOUND -eq $FILENUM ]] ||
4611                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4612         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4613                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4614                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4615                 error "$GETSTRIPE --obd: should not show file on other obd"
4616         echo "$GETSTRIPE --obd passed"
4617 }
4618 run_test 56a "check $GETSTRIPE"
4619
4620 test_56b() {
4621         test_mkdir $DIR/$tdir
4622         NUMDIRS=3
4623         for i in $(seq 1 $NUMDIRS); do
4624                 test_mkdir $DIR/$tdir/dir$i
4625         done
4626
4627         # test lfs getdirstripe default mode is non-recursion, which is
4628         # different from lfs getstripe
4629         dircnt=$($LFS getdirstripe $DIR/$tdir | grep -c lmv_stripe_count)
4630         [[ $dircnt -eq 1 ]] ||
4631                 error "$LFS getdirstripe: found $dircnt, not 1"
4632         dircnt=$($LFS getdirstripe --recursive $DIR/$tdir |
4633                 grep -c lmv_stripe_count)
4634         [[ $dircnt -eq $((NUMDIRS + 1)) ]] ||
4635                 error "$LFS getdirstripe --recursive: found $dircnt, \
4636                         not $((NUMDIRS + 1))"
4637 }
4638 run_test 56b "check $LFS getdirstripe"
4639
4640 test_56c() {
4641         local ost_idx=0
4642         local ost_name=$(ostname_from_index $ost_idx)
4643
4644         local old_status=$(ost_dev_status $ost_idx)
4645         [[ -z "$old_status" ]] ||
4646                 { skip_env "OST $ost_name is in $old_status status"; return 0; }
4647
4648         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4649         sleep_maxage
4650
4651         local new_status=$(ost_dev_status $ost_idx)
4652         [[ "$new_status" = "D" ]] ||
4653                 error "OST $ost_name is in status of '$new_status', not 'D'"
4654
4655         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4656         sleep_maxage
4657
4658         new_status=$(ost_dev_status $ost_idx)
4659         [[ -z "$new_status" ]] ||
4660                 error "OST $ost_name is in status of '$new_status', not ''"
4661 }
4662 run_test 56c "check 'lfs df' showing device status"
4663
4664 NUMFILES=3
4665 NUMDIRS=3
4666 setup_56() {
4667         local LOCAL_NUMFILES="$1"
4668         local LOCAL_NUMDIRS="$2"
4669         local MKDIR_PARAMS="$3"
4670         local DIR_STRIPE_PARAMS="$4"
4671
4672         if [ ! -d "$TDIR" ] ; then
4673                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4674                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4675                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4676                         touch $TDIR/file$i
4677                 done
4678                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4679                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4680                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4681                                 touch $TDIR/dir$i/file$j
4682                         done
4683                 done
4684         fi
4685 }
4686
4687 setup_56_special() {
4688         LOCAL_NUMFILES=$1
4689         LOCAL_NUMDIRS=$2
4690         setup_56 $1 $2
4691         if [ ! -e "$TDIR/loop1b" ] ; then
4692                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4693                         mknod $TDIR/loop${i}b b 7 $i
4694                         mknod $TDIR/null${i}c c 1 3
4695                         ln -s $TDIR/file1 $TDIR/link${i}l
4696                 done
4697                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4698                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4699                         mknod $TDIR/dir$i/null${i}c c 1 3
4700                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4701                 done
4702         fi
4703 }
4704
4705 test_56g() {
4706         $SETSTRIPE -d $DIR
4707
4708         TDIR=$DIR/${tdir}g
4709         setup_56 $NUMFILES $NUMDIRS
4710
4711         EXPECTED=$(($NUMDIRS + 2))
4712         # test lfs find with -name
4713         for i in $(seq 1 $NUMFILES) ; do
4714                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4715                 [ $NUMS -eq $EXPECTED ] ||
4716                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4717                               "found $NUMS, expected $EXPECTED"
4718         done
4719 }
4720 run_test 56g "check lfs find -name ============================="
4721
4722 test_56h() {
4723         $SETSTRIPE -d $DIR
4724
4725         TDIR=$DIR/${tdir}g
4726         setup_56 $NUMFILES $NUMDIRS
4727
4728         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4729         # test lfs find with ! -name
4730         for i in $(seq 1 $NUMFILES) ; do
4731                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4732                 [ $NUMS -eq $EXPECTED ] ||
4733                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4734                               "found $NUMS, expected $EXPECTED"
4735         done
4736 }
4737 run_test 56h "check lfs find ! -name ============================="
4738
4739 test_56i() {
4740        tdir=${tdir}i
4741        test_mkdir -p $DIR/$tdir
4742        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4743        CMD="$LFIND -ost $UUID $DIR/$tdir"
4744        OUT=$($CMD)
4745        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4746 }
4747 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4748
4749 test_56j() {
4750         TDIR=$DIR/${tdir}g
4751         setup_56_special $NUMFILES $NUMDIRS
4752
4753         EXPECTED=$((NUMDIRS + 1))
4754         CMD="$LFIND -type d $TDIR"
4755         NUMS=$($CMD | wc -l)
4756         [ $NUMS -eq $EXPECTED ] ||
4757                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4758 }
4759 run_test 56j "check lfs find -type d ============================="
4760
4761 test_56k() {
4762         TDIR=$DIR/${tdir}g
4763         setup_56_special $NUMFILES $NUMDIRS
4764
4765         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4766         CMD="$LFIND -type f $TDIR"
4767         NUMS=$($CMD | wc -l)
4768         [ $NUMS -eq $EXPECTED ] ||
4769                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4770 }
4771 run_test 56k "check lfs find -type f ============================="
4772
4773 test_56l() {
4774         TDIR=$DIR/${tdir}g
4775         setup_56_special $NUMFILES $NUMDIRS
4776
4777         EXPECTED=$((NUMDIRS + NUMFILES))
4778         CMD="$LFIND -type b $TDIR"
4779         NUMS=$($CMD | wc -l)
4780         [ $NUMS -eq $EXPECTED ] ||
4781                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4782 }
4783 run_test 56l "check lfs find -type b ============================="
4784
4785 test_56m() {
4786         TDIR=$DIR/${tdir}g
4787         setup_56_special $NUMFILES $NUMDIRS
4788
4789         EXPECTED=$((NUMDIRS + NUMFILES))
4790         CMD="$LFIND -type c $TDIR"
4791         NUMS=$($CMD | wc -l)
4792         [ $NUMS -eq $EXPECTED ] ||
4793                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4794 }
4795 run_test 56m "check lfs find -type c ============================="
4796
4797 test_56n() {
4798         TDIR=$DIR/${tdir}g
4799         setup_56_special $NUMFILES $NUMDIRS
4800
4801         EXPECTED=$((NUMDIRS + NUMFILES))
4802         CMD="$LFIND -type l $TDIR"
4803         NUMS=$($CMD | wc -l)
4804         [ $NUMS -eq $EXPECTED ] ||
4805                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4806 }
4807 run_test 56n "check lfs find -type l ============================="
4808
4809 test_56o() {
4810         TDIR=$DIR/${tdir}o
4811         setup_56 $NUMFILES $NUMDIRS
4812         utime $TDIR/file1 > /dev/null || error "utime (1)"
4813         utime $TDIR/file2 > /dev/null || error "utime (2)"
4814         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4815         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4816         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4817         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4818
4819         EXPECTED=4
4820         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4821         [ $NUMS -eq $EXPECTED ] || \
4822                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4823
4824         EXPECTED=12
4825         CMD="$LFIND -mtime 0 $TDIR"
4826         NUMS=$($CMD | wc -l)
4827         [ $NUMS -eq $EXPECTED ] ||
4828                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4829 }
4830 run_test 56o "check lfs find -mtime for old files =========================="
4831
4832 test_56p() {
4833         [ $RUNAS_ID -eq $UID ] &&
4834                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4835
4836         TDIR=$DIR/${tdir}p
4837         setup_56 $NUMFILES $NUMDIRS
4838
4839         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4840         EXPECTED=$NUMFILES
4841         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4842         NUMS=$($CMD | wc -l)
4843         [ $NUMS -eq $EXPECTED ] || \
4844                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4845
4846         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4847         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4848         NUMS=$($CMD | wc -l)
4849         [ $NUMS -eq $EXPECTED ] || \
4850                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4851 }
4852 run_test 56p "check lfs find -uid and ! -uid ==============================="
4853
4854 test_56q() {
4855         [ $RUNAS_ID -eq $UID ] &&
4856                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4857
4858         TDIR=$DIR/${tdir}q
4859         setup_56 $NUMFILES $NUMDIRS
4860
4861         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4862
4863         EXPECTED=$NUMFILES
4864         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4865         NUMS=$($CMD | wc -l)
4866         [ $NUMS -eq $EXPECTED ] ||
4867                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4868
4869         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4870         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4871         NUMS=$($CMD | wc -l)
4872         [ $NUMS -eq $EXPECTED ] ||
4873                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4874 }
4875 run_test 56q "check lfs find -gid and ! -gid ==============================="
4876
4877 test_56r() {
4878         TDIR=$DIR/${tdir}r
4879         setup_56 $NUMFILES $NUMDIRS
4880
4881         EXPECTED=12
4882         CMD="$LFIND -size 0 -type f $TDIR"
4883         NUMS=$($CMD | wc -l)
4884         [ $NUMS -eq $EXPECTED ] ||
4885                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4886         EXPECTED=0
4887         CMD="$LFIND ! -size 0 -type f $TDIR"
4888         NUMS=$($CMD | wc -l)
4889         [ $NUMS -eq $EXPECTED ] ||
4890                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4891         echo "test" > $TDIR/$tfile
4892         echo "test2" > $TDIR/$tfile.2 && sync
4893         EXPECTED=1
4894         CMD="$LFIND -size 5 -type f $TDIR"
4895         NUMS=$($CMD | wc -l)
4896         [ $NUMS -eq $EXPECTED ] ||
4897                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4898         EXPECTED=1
4899         CMD="$LFIND -size +5 -type f $TDIR"
4900         NUMS=$($CMD | wc -l)
4901         [ $NUMS -eq $EXPECTED ] ||
4902                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4903         EXPECTED=2
4904         CMD="$LFIND -size +0 -type f $TDIR"
4905         NUMS=$($CMD | wc -l)
4906         [ $NUMS -eq $EXPECTED ] ||
4907                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4908         EXPECTED=2
4909         CMD="$LFIND ! -size -5 -type f $TDIR"
4910         NUMS=$($CMD | wc -l)
4911         [ $NUMS -eq $EXPECTED ] ||
4912                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4913         EXPECTED=12
4914         CMD="$LFIND -size -5 -type f $TDIR"
4915         NUMS=$($CMD | wc -l)
4916         [ $NUMS -eq $EXPECTED ] ||
4917                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4918 }
4919 run_test 56r "check lfs find -size works =========================="
4920
4921 test_56s() { # LU-611
4922         TDIR=$DIR/${tdir}s
4923
4924         #LU-9369
4925         setup_56 0 $NUMDIRS
4926         for i in $(seq 1 $NUMDIRS); do
4927                 $SETSTRIPE -c $((OSTCOUNT + 1)) $TDIR/dir$i/$tfile
4928         done
4929         EXPECTED=$NUMDIRS
4930         CMD="$LFIND -c $OSTCOUNT $TDIR"
4931         NUMS=$($CMD | wc -l)
4932         [ $NUMS -eq $EXPECTED ] || {
4933                 $GETSTRIPE -R $TDIR
4934                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4935         }
4936         rm -rf $TDIR
4937
4938         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4939         if [[ $OSTCOUNT -gt 1 ]]; then
4940                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4941                 ONESTRIPE=4
4942                 EXTRA=4
4943         else
4944                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4945                 EXTRA=0
4946         fi
4947
4948         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4949         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4950         NUMS=$($CMD | wc -l)
4951         [ $NUMS -eq $EXPECTED ] || {
4952                 $GETSTRIPE -R $TDIR
4953                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4954         }
4955
4956         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4957         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4958         NUMS=$($CMD | wc -l)
4959         [ $NUMS -eq $EXPECTED ] || {
4960                 $GETSTRIPE -R $TDIR
4961                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4962         }
4963
4964         EXPECTED=$ONESTRIPE
4965         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4966         NUMS=$($CMD | wc -l)
4967         [ $NUMS -eq $EXPECTED ] || {
4968                 $GETSTRIPE -R $TDIR
4969                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4970         }
4971
4972         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4973         NUMS=$($CMD | wc -l)
4974         [ $NUMS -eq $EXPECTED ] || {
4975                 $GETSTRIPE -R $TDIR
4976                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4977         }
4978
4979         EXPECTED=0
4980         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4981         NUMS=$($CMD | wc -l)
4982         [ $NUMS -eq $EXPECTED ] || {
4983                 $GETSTRIPE -R $TDIR
4984                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4985         }
4986 }
4987 run_test 56s "check lfs find -stripe-count works"
4988
4989 test_56t() { # LU-611
4990         TDIR=$DIR/${tdir}t
4991
4992         #LU-9369
4993         setup_56 0 $NUMDIRS
4994         for i in $(seq 1 $NUMDIRS); do
4995                 $SETSTRIPE -S 4M $TDIR/dir$i/$tfile
4996         done
4997         EXPECTED=$NUMDIRS
4998         CMD="$LFIND -S 4M $TDIR"
4999         NUMS=$($CMD | wc -l)
5000         [ $NUMS -eq $EXPECTED ] || {
5001                 $GETSTRIPE -R $TDIR
5002                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5003         }
5004         rm -rf $TDIR
5005
5006         setup_56 $NUMFILES $NUMDIRS "--stripe-size 512k"
5007
5008         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
5009
5010         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
5011         CMD="$LFIND -stripe-size 512k -type f $TDIR"
5012         NUMS=$($CMD | wc -l)
5013         [ $NUMS -eq $EXPECTED ] ||
5014                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5015
5016         CMD="$LFIND -stripe-size +320k -type f $TDIR"
5017         NUMS=$($CMD | wc -l)
5018         [ $NUMS -eq $EXPECTED ] ||
5019                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5020
5021         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
5022         CMD="$LFIND -stripe-size +200k -type f $TDIR"
5023         NUMS=$($CMD | wc -l)
5024         [ $NUMS -eq $EXPECTED ] ||
5025                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5026
5027         CMD="$LFIND -stripe-size -640k -type f $TDIR"
5028         NUMS=$($CMD | wc -l)
5029         [ $NUMS -eq $EXPECTED ] ||
5030                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5031
5032         EXPECTED=4
5033         CMD="$LFIND -stripe-size 256k -type f $TDIR"
5034         NUMS=$($CMD | wc -l)
5035         [ $NUMS -eq $EXPECTED ] ||
5036                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5037
5038         CMD="$LFIND -stripe-size -320k -type f $TDIR"
5039         NUMS=$($CMD | wc -l)
5040         [ $NUMS -eq $EXPECTED ] ||
5041                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5042
5043         EXPECTED=0
5044         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
5045         NUMS=$($CMD | wc -l)
5046         [ $NUMS -eq $EXPECTED ] ||
5047                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5048 }
5049 run_test 56t "check lfs find -stripe-size works"
5050
5051 test_56u() { # LU-611
5052         TDIR=$DIR/${tdir}u
5053         setup_56 $NUMFILES $NUMDIRS "-i 0"
5054
5055         if [[ $OSTCOUNT -gt 1 ]]; then
5056                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
5057                 ONESTRIPE=4
5058         else
5059                 ONESTRIPE=0
5060         fi
5061
5062         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
5063         CMD="$LFIND -stripe-index 0 -type f $TDIR"
5064         NUMS=$($CMD | wc -l)
5065         [ $NUMS -eq $EXPECTED ] ||
5066                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5067
5068         EXPECTED=$ONESTRIPE
5069         CMD="$LFIND -stripe-index 1 -type f $TDIR"
5070         NUMS=$($CMD | wc -l)
5071         [ $NUMS -eq $EXPECTED ] ||
5072                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5073
5074         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
5075         NUMS=$($CMD | wc -l)
5076         [ $NUMS -eq $EXPECTED ] ||
5077                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5078
5079         EXPECTED=0
5080         # This should produce an error and not return any files
5081         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
5082         NUMS=$($CMD 2>/dev/null | wc -l)
5083         [ $NUMS -eq $EXPECTED ] ||
5084                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5085
5086         if [[ $OSTCOUNT -gt 1 ]]; then
5087                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
5088                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
5089                 NUMS=$($CMD | wc -l)
5090                 [ $NUMS -eq $EXPECTED ] ||
5091                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5092         fi
5093 }
5094 run_test 56u "check lfs find -stripe-index works"
5095
5096 test_56v() {
5097     local MDT_IDX=0
5098
5099     TDIR=$DIR/${tdir}v
5100     rm -rf $TDIR
5101     setup_56 $NUMFILES $NUMDIRS
5102
5103     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
5104     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
5105
5106     for file in $($LFIND -mdt $UUID $TDIR); do
5107         file_mdt_idx=$($GETSTRIPE -M $file)
5108         [ $file_mdt_idx -eq $MDT_IDX ] ||
5109             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
5110     done
5111 }
5112 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
5113
5114 test_56w() {
5115         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5117         TDIR=$DIR/${tdir}w
5118
5119         rm -rf $TDIR || error "remove $TDIR failed"
5120         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5121
5122         local stripe_size
5123         stripe_size=$($GETSTRIPE -S -d $TDIR) ||
5124                 error "$GETSTRIPE -S -d $TDIR failed"
5125         stripe_size=${stripe_size%% *}
5126
5127         local file_size=$((stripe_size * OSTCOUNT))
5128         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5129         local required_space=$((file_num * file_size))
5130
5131         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5132                            head -n1)
5133         [[ $free_space -le $((required_space / 1024)) ]] &&
5134                 skip_env "need $required_space bytes, have $free_space KB" &&
5135                 return
5136
5137         local dd_bs=65536
5138         local dd_count=$((file_size / dd_bs))
5139
5140         # write data into the files
5141         local i
5142         local j
5143         local file
5144         for i in $(seq 1 $NUMFILES); do
5145                 file=$TDIR/file$i
5146                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5147                         error "write data into $file failed"
5148         done
5149         for i in $(seq 1 $NUMDIRS); do
5150                 for j in $(seq 1 $NUMFILES); do
5151                         file=$TDIR/dir$i/file$j
5152                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5153                                 error "write data into $file failed"
5154                 done
5155         done
5156
5157         # $LFS_MIGRATE will fail if hard link migration is unsupported
5158         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5159                 createmany -l$TDIR/dir1/file1 $TDIR/dir1/link 200 ||
5160                         error "creating links to $TDIR/dir1/file1 failed"
5161         fi
5162
5163         local expected=-1
5164         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5165
5166         # lfs_migrate file
5167         local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
5168         echo "$cmd"
5169         eval $cmd || error "$cmd failed"
5170
5171         check_stripe_count $TDIR/file1 $expected
5172
5173         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5174         then
5175                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5176                 # OST 1 if it is on OST 0. This file is small enough to
5177                 # be on only one stripe.
5178                 file=$TDIR/migr_1_ost
5179                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5180                         error "write data into $file failed"
5181                 local obdidx=$($LFS getstripe -i $file)
5182                 local oldmd5=$(md5sum $file)
5183                 local newobdidx=0
5184                 [[ $obdidx -eq 0 ]] && newobdidx=1
5185                 cmd="$LFS migrate -i $newobdidx $file"
5186                 echo $cmd
5187                 eval $cmd || error "$cmd failed"
5188                 local realobdix=$($LFS getstripe -i $file)
5189                 local newmd5=$(md5sum $file)
5190                 [[ $newobdidx -ne $realobdix ]] &&
5191                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
5192                 [[ "$oldmd5" != "$newmd5" ]] &&
5193                         error "md5sum differ: $oldmd5, $newmd5"
5194         fi
5195
5196     # lfs_migrate dir
5197     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
5198     echo "$cmd"
5199     eval $cmd || error "$cmd failed"
5200
5201     for j in $(seq 1 $NUMFILES); do
5202         check_stripe_count $TDIR/dir1/file$j $expected
5203     done
5204
5205     # lfs_migrate works with lfs find
5206     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
5207          $LFS_MIGRATE -y -c $expected"
5208     echo "$cmd"
5209     eval $cmd || error "$cmd failed"
5210
5211     for i in $(seq 2 $NUMFILES); do
5212         check_stripe_count $TDIR/file$i $expected
5213     done
5214     for i in $(seq 2 $NUMDIRS); do
5215         for j in $(seq 1 $NUMFILES); do
5216             check_stripe_count $TDIR/dir$i/file$j $expected
5217         done
5218     done
5219 }
5220 run_test 56w "check lfs_migrate -c stripe_count works"
5221
5222 test_56x() {
5223         check_swap_layouts_support && return 0
5224         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5225
5226         local dir0=$DIR/$tdir
5227         local ref1=/etc/passwd
5228         local file1=$dir0/file1
5229
5230         test_mkdir $dir0 || error "creating dir $dir0"
5231         $LFS setstripe -c 2 $file1
5232         cp $ref1 $file1
5233         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5234         stripe=$($LFS getstripe -c $file1)
5235         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5236         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5237
5238         # clean up
5239         rm -f $file1
5240 }
5241 run_test 56x "lfs migration support"
5242
5243 test_56xa() {
5244         check_swap_layouts_support && return 0
5245         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5246
5247         local dir0=$DIR/$tdir/$testnum
5248         test_mkdir -p $dir0 || error "creating dir $dir0"
5249
5250         local ref1=/etc/passwd
5251         local file1=$dir0/file1
5252
5253         $LFS setstripe -c 2 $file1
5254         cp $ref1 $file1
5255         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5256         local stripe=$($LFS getstripe -c $file1)
5257         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5258         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5259
5260         # clean up
5261         rm -f $file1
5262 }
5263 run_test 56xa "lfs migration --block support"
5264
5265 check_migrate_links() {
5266         local dir="$1"
5267         local file1="$dir/file1"
5268         local begin="$2"
5269         local count="$3"
5270         local total_count=$(($begin + $count - 1))
5271         local symlink_count=10
5272         local uniq_count=10
5273
5274         if [ ! -f "$file1" ]; then
5275                 echo -n "creating initial file..."
5276                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5277                         error "cannot setstripe initial file"
5278                 echo "done"
5279
5280                 echo -n "creating symlinks..."
5281                 for s in $(seq 1 $symlink_count); do
5282                         ln -s "$file1" "$dir/slink$s" ||
5283                                 error "cannot create symlinks"
5284                 done
5285                 echo "done"
5286
5287                 echo -n "creating nonlinked files..."
5288                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5289                         error "cannot create nonlinked files"
5290                 echo "done"
5291         fi
5292
5293         # create hard links
5294         if [ ! -f "$dir/file$total_count" ]; then
5295                 echo -n "creating hard links $begin:$total_count..."
5296                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5297                         /dev/null || error "cannot create hard links"
5298                 echo "done"
5299         fi
5300
5301         echo -n "checking number of hard links listed in xattrs..."
5302         local fid=$($LFS getstripe -F "$file1")
5303         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5304
5305         echo "${#paths[*]}"
5306         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5307                         echo "hard link list has unexpected size, skipping test"
5308                         return 0
5309         fi
5310         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5311                         error "link names should exceed xattrs size"
5312         fi
5313
5314         echo -n "migrating files..."
5315         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5316         local rc=$?
5317         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5318         echo "done"
5319
5320         # make sure all links have been properly migrated
5321         echo -n "verifying files..."
5322         fid=$($LFS getstripe -F "$file1") ||
5323                 error "cannot get fid for file $file1"
5324         for i in $(seq 2 $total_count); do
5325                 local fid2=$($LFS getstripe -F $dir/file$i)
5326                 [ "$fid2" == "$fid" ] ||
5327                         error "migrated hard link has mismatched FID"
5328         done
5329
5330         # make sure hard links were properly detected, and migration was
5331         # performed only once for the entire link set; nonlinked files should
5332         # also be migrated
5333         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5334         local expected=$(($uniq_count + 1))
5335         [ "$actual" -eq  "$expected" ] ||
5336                 error "hard links individually migrated ($actual != $expected)"
5337
5338         # make sure the correct number of hard links are present
5339         local hardlinks=$(stat -c '%h' "$file1")
5340         [ $hardlinks -eq $total_count ] ||
5341                 error "num hard links $hardlinks != $total_count"
5342         echo "done"
5343
5344         return 0
5345 }
5346
5347 test_56xb() {
5348         local dir0="$DIR/$tdir"
5349
5350         test_mkdir "$dir0" || error "cannot create dir $dir0"
5351
5352         echo "testing lfs migrate mode when all links fit within xattrs"
5353         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 2 99
5354
5355         echo "testing rsync mode when all links fit within xattrs"
5356         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 2 99
5357
5358         echo "testing lfs migrate mode when all links do not fit within xattrs"
5359         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 101 100
5360
5361         echo "testing rsync mode when all links do not fit within xattrs"
5362         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 101 100
5363
5364         # clean up
5365         rm -rf $dir0
5366 }
5367 run_test 56xb "lfs migration hard link support"
5368
5369 test_56y() {
5370         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5371                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5372                 return
5373
5374         local res=""
5375         local dir0=$DIR/$tdir/$testnum
5376         test_mkdir -p $dir0 || error "creating dir $dir0"
5377         local f1=$dir0/file1
5378         local f2=$dir0/file2
5379
5380         touch $f1 || error "creating std file $f1"
5381         $MULTIOP $f2 H2c || error "creating released file $f2"
5382
5383         # a directory can be raid0, so ask only for files
5384         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5385         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5386
5387         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5388         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5389
5390         # only files can be released, so no need to force file search
5391         res=$($LFIND $dir0 -L released)
5392         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5393
5394         res=$($LFIND $dir0 \! -L released)
5395         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5396
5397 }
5398 run_test 56y "lfs find -L raid0|released"
5399
5400 test_56z() { # LU-4824
5401         # This checks to make sure 'lfs find' continues after errors
5402         # There are two classes of errors that should be caught:
5403         # - If multiple paths are provided, all should be searched even if one
5404         #   errors out
5405         # - If errors are encountered during the search, it should not terminate
5406         #   early
5407         local i
5408         test_mkdir $DIR/$tdir
5409         for i in d{0..9}; do
5410                 test_mkdir $DIR/$tdir/$i
5411         done
5412         touch $DIR/$tdir/d{0..9}/$tfile
5413         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5414                 error "$LFS find did not return an error"
5415         # Make a directory unsearchable. This should NOT be the last entry in
5416         # directory order.  Arbitrarily pick the 6th entry
5417         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5418         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5419         # The user should be able to see 10 directories and 9 files
5420         [ $count == 19 ] || error "$LFS find did not continue after error"
5421 }
5422 run_test 56z "lfs find should continue after an error"
5423
5424 test_56aa() { # LU-5937
5425         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5426
5427         mkdir $DIR/$tdir
5428         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5429
5430         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5431         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5432
5433         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5434 }
5435 run_test 56aa "lfs find --size under striped dir"
5436
5437 test_57a() {
5438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5439         # note test will not do anything if MDS is not local
5440         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5441                 skip "ldiskfs only test"
5442                 return
5443         fi
5444
5445         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5446         local MNTDEV="osd*.*MDT*.mntdev"
5447         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5448         [ -z "$DEV" ] && error "can't access $MNTDEV"
5449         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5450                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5451                         error "can't access $DEV"
5452                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5453                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5454                 rm $TMP/t57a.dump
5455         done
5456 }
5457 run_test 57a "verify MDS filesystem created with large inodes =="
5458
5459 test_57b() {
5460         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5461         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5462                 skip "ldiskfs only test"
5463                 return
5464         fi
5465
5466         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5467         local dir=$DIR/$tdir
5468
5469         local FILECOUNT=100
5470         local FILE1=$dir/f1
5471         local FILEN=$dir/f$FILECOUNT
5472
5473         rm -rf $dir || error "removing $dir"
5474         test_mkdir -p -c1 $dir || error "creating $dir"
5475         local mdtidx=$($LFS getstripe -M $dir)
5476         local mdtname=MDT$(printf %04x $mdtidx)
5477         local facet=mds$((mdtidx + 1))
5478
5479         echo "mcreating $FILECOUNT files"
5480         createmany -m $dir/f 1 $FILECOUNT || \
5481                 error "creating files in $dir"
5482
5483         # verify that files do not have EAs yet
5484         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5485         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5486
5487         sync
5488         sleep 1
5489         df $dir  #make sure we get new statfs data
5490         local MDSFREE=$(do_facet $facet \
5491                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5492         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5493         echo "opening files to create objects/EAs"
5494         local FILE
5495         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5496                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5497         done
5498
5499         # verify that files have EAs now
5500         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5501         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5502
5503         sleep 1  #make sure we get new statfs data
5504         df $dir
5505         local MDSFREE2=$(do_facet $facet \
5506                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5507         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5508         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5509                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5510                         error "MDC before $MDCFREE != after $MDCFREE2"
5511                 else
5512                         echo "MDC before $MDCFREE != after $MDCFREE2"
5513                         echo "unable to confirm if MDS has large inodes"
5514                 fi
5515         fi
5516         rm -rf $dir
5517 }
5518 run_test 57b "default LOV EAs are stored inside large inodes ==="
5519
5520 test_58() {
5521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5522         [ -z "$(which wiretest 2>/dev/null)" ] &&
5523                         skip_env "could not find wiretest" && return
5524         wiretest
5525 }
5526 run_test 58 "verify cross-platform wire constants =============="
5527
5528 test_59() {
5529         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5530         echo "touch 130 files"
5531         createmany -o $DIR/f59- 130
5532         echo "rm 130 files"
5533         unlinkmany $DIR/f59- 130
5534         sync
5535         # wait for commitment of removal
5536         wait_delete_completed
5537 }
5538 run_test 59 "verify cancellation of llog records async ========="
5539
5540 TEST60_HEAD="test_60 run $RANDOM"
5541 test_60a() {
5542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5543         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5544         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5545                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5546                         skip_env "missing subtest run-llog.sh" && return
5547
5548         log "$TEST60_HEAD - from kernel mode"
5549         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5550         do_facet mgs sh run-llog.sh
5551         do_facet mgs $LCTL dk > $TMP/$tfile
5552
5553         # LU-6388: test llog_reader
5554         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5555         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5556         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5557                         skip_env "missing llog_reader" && return
5558         local fstype=$(facet_fstype mgs)
5559         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5560                 skip_env "Only for ldiskfs or zfs type mgs" && return
5561
5562         local mntpt=$(facet_mntpt mgs)
5563         local mgsdev=$(mgsdevname 1)
5564         local fid_list
5565         local fid
5566         local rec_list
5567         local rec
5568         local rec_type
5569         local obj_file
5570         local path
5571         local seq
5572         local oid
5573         local pass=true
5574
5575         #get fid and record list
5576         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5577                 tail -n 4))
5578         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5579                 tail -n 4))
5580         #remount mgs as ldiskfs or zfs type
5581         stop mgs || error "stop mgs failed"
5582         mount_fstype mgs || error "remount mgs failed"
5583         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5584                 fid=${fid_list[i]}
5585                 rec=${rec_list[i]}
5586                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5587                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5588                 oid=$((16#$oid))
5589
5590                 case $fstype in
5591                         ldiskfs )
5592                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5593                         zfs )
5594                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5595                 esac
5596                 echo "obj_file is $obj_file"
5597                 do_facet mgs $llog_reader $obj_file
5598
5599                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5600                         awk '{ print $3 }' | sed -e "s/^type=//g")
5601                 if [ $rec_type != $rec ]; then
5602                         echo "FAILED test_60a wrong record type $rec_type," \
5603                               "should be $rec"
5604                         pass=false
5605                         break
5606                 fi
5607
5608                 #check obj path if record type is LLOG_LOGID_MAGIC
5609                 if [ "$rec" == "1064553b" ]; then
5610                         path=$(do_facet mgs $llog_reader $obj_file |
5611                                 grep "path=" | awk '{ print $NF }' |
5612                                 sed -e "s/^path=//g")
5613                         if [ $obj_file != $mntpt/$path ]; then
5614                                 echo "FAILED test_60a wrong obj path" \
5615                                       "$montpt/$path, should be $obj_file"
5616                                 pass=false
5617                                 break
5618                         fi
5619                 fi
5620         done
5621         rm -f $TMP/$tfile
5622         #restart mgs before "error", otherwise it will block the next test
5623         stop mgs || error "stop mgs failed"
5624         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5625         $pass || error "test failed, see FAILED test_60a messages for specifics"
5626 }
5627 run_test 60a "llog_test run from kernel module and test llog_reader"
5628
5629 test_60aa() {
5630         # test old logid format
5631         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5632                 do_facet mgs $LCTL dl | grep MGS
5633                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5634                         error "old llog_print failed"
5635         fi
5636
5637         # test new logid format
5638         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5639                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5640                         error "new llog_print failed"
5641         fi
5642 }
5643 run_test 60aa "llog_print works with FIDs and simple names"
5644
5645 test_60b() { # bug 6411
5646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5647         dmesg > $DIR/$tfile
5648         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5649                                 /llog.test/ {
5650                                         if (marker)
5651                                                 from_marker++
5652                                         from_begin++
5653                                 }
5654                                 END {
5655                                         if (marker)
5656                                                 print from_marker
5657                                         else
5658                                                 print from_begin
5659                                 }")
5660         [[ $LLOG_COUNT -gt 100 ]] &&
5661                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5662 }
5663 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5664
5665 test_60c() {
5666         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5667         echo "create 5000 files"
5668         createmany -o $DIR/f60c- 5000
5669 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5670         lctl set_param fail_loc=0x80000137
5671         unlinkmany $DIR/f60c- 5000
5672         lctl set_param fail_loc=0
5673 }
5674 run_test 60c "unlink file when mds full"
5675
5676 test_60d() {
5677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5678         SAVEPRINTK=$(lctl get_param -n printk)
5679
5680         # verify "lctl mark" is even working"
5681         MESSAGE="test message ID $RANDOM $$"
5682         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5683         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5684
5685         lctl set_param printk=0 || error "set lnet.printk failed"
5686         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5687         MESSAGE="new test message ID $RANDOM $$"
5688         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5689         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5690         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5691
5692         lctl set_param -n printk="$SAVEPRINTK"
5693 }
5694 run_test 60d "test printk console message masking"
5695
5696 test_60e() {
5697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5698         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5699         touch $DIR/$tfile
5700 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5701         do_facet mds1 lctl set_param fail_loc=0x15b
5702         rm $DIR/$tfile
5703 }
5704 run_test 60e "no space while new llog is being created"
5705
5706 test_61() {
5707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5708         f="$DIR/f61"
5709         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5710         cancel_lru_locks osc
5711         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5712         sync
5713 }
5714 run_test 61 "mmap() writes don't make sync hang ================"
5715
5716 # bug 2330 - insufficient obd_match error checking causes LBUG
5717 test_62() {
5718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5719         f="$DIR/f62"
5720         echo foo > $f
5721         cancel_lru_locks osc
5722         lctl set_param fail_loc=0x405
5723         cat $f && error "cat succeeded, expect -EIO"
5724         lctl set_param fail_loc=0
5725 }
5726 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5727 # match every page all of the time.
5728 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5729
5730 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5731 # Though this test is irrelevant anymore, it helped to reveal some
5732 # other grant bugs (LU-4482), let's keep it.
5733 test_63a() {   # was test_63
5734         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5735         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5736         for i in `seq 10` ; do
5737                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5738                 sleep 5
5739                 kill $!
5740                 sleep 1
5741         done
5742
5743         rm -f $DIR/f63 || true
5744 }
5745 run_test 63a "Verify oig_wait interruption does not crash ======="
5746
5747 # bug 2248 - async write errors didn't return to application on sync
5748 # bug 3677 - async write errors left page locked
5749 test_63b() {
5750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5751         debugsave
5752         lctl set_param debug=-1
5753
5754         # ensure we have a grant to do async writes
5755         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5756         rm $DIR/$tfile
5757
5758         sync    # sync lest earlier test intercept the fail_loc
5759
5760         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5761         lctl set_param fail_loc=0x80000406
5762         $MULTIOP $DIR/$tfile Owy && \
5763                 error "sync didn't return ENOMEM"
5764         sync; sleep 2; sync     # do a real sync this time to flush page
5765         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5766                 error "locked page left in cache after async error" || true
5767         debugrestore
5768 }
5769 run_test 63b "async write errors should be returned to fsync ==="
5770
5771 test_64a () {
5772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5773         df $DIR
5774         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5775 }
5776 run_test 64a "verify filter grant calculations (in kernel) ====="
5777
5778 test_64b () {
5779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5780         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5781 }
5782 run_test 64b "check out-of-space detection on client ==========="
5783
5784 test_64c() {
5785         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5786 }
5787 run_test 64c "verify grant shrink ========================------"
5788
5789 # bug 1414 - set/get directories' stripe info
5790 test_65a() {
5791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5792         test_mkdir -p $DIR/$tdir
5793         touch $DIR/$tdir/f1
5794         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5795 }
5796 run_test 65a "directory with no stripe info ===================="
5797
5798 test_65b() {
5799         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5800         test_mkdir -p $DIR/$tdir
5801         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5802
5803         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5804                                                 error "setstripe"
5805         touch $DIR/$tdir/f2
5806         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5807 }
5808 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
5809
5810 test_65c() {
5811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5812         if [[ $OSTCOUNT -gt 1 ]]; then
5813                 test_mkdir -p $DIR/$tdir
5814                 local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5815
5816                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5817                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5818                 touch $DIR/$tdir/f3
5819                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5820         fi
5821 }
5822 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
5823
5824 test_65d() {
5825         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5826         test_mkdir -p $DIR/$tdir
5827         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
5828         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5829
5830         if [[ $STRIPECOUNT -le 0 ]]; then
5831                 sc=1
5832         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5833 #LOV_MAX_STRIPE_COUNT is 2000
5834                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5835         else
5836                 sc=$(($STRIPECOUNT - 1))
5837         fi
5838         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5839         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5840         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5841                 error "lverify failed"
5842 }
5843 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
5844
5845 test_65e() {
5846         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5847         test_mkdir -p $DIR/$tdir
5848
5849         $SETSTRIPE $DIR/$tdir || error "setstripe"
5850         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5851                                         error "no stripe info failed"
5852         touch $DIR/$tdir/f6
5853         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5854 }
5855 run_test 65e "directory setstripe defaults ======================="
5856
5857 test_65f() {
5858         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5859         test_mkdir -p $DIR/${tdir}f
5860         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5861 }
5862 run_test 65f "dir setstripe permission (should return error) ==="
5863
5864 test_65g() {
5865         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5866         test_mkdir -p $DIR/$tdir
5867         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5868
5869         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5870                                                         error "setstripe"
5871         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5872         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5873                 error "delete default stripe failed"
5874 }
5875 run_test 65g "directory setstripe -d ==========================="
5876
5877 test_65h() {
5878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5879         test_mkdir -p $DIR/$tdir
5880         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5881
5882         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5883                                                         error "setstripe"
5884         test_mkdir -p $DIR/$tdir/dd1
5885         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5886                 error "stripe info inherit failed"
5887 }
5888 run_test 65h "directory stripe info inherit ===================="
5889
5890 test_65i() { # bug6367
5891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5892         $SETSTRIPE -S 65536 -c -1 $MOUNT
5893 }
5894 run_test 65i "set non-default striping on root directory (bug 6367)="
5895
5896 test_65ia() { # bug12836
5897         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5898         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5899 }
5900 run_test 65ia "getstripe on -1 default directory striping"
5901
5902 test_65ib() { # bug12836
5903         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5904         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5905 }
5906 run_test 65ib "getstripe -v on -1 default directory striping"
5907
5908 test_65ic() { # bug12836
5909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5910         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5911 }
5912 run_test 65ic "new find on -1 default directory striping"
5913
5914 test_65j() { # bug6367
5915         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5916         sync; sleep 1
5917         # if we aren't already remounting for each test, do so for this test
5918         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5919                 cleanup || error "failed to unmount"
5920                 setup
5921         fi
5922         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5923 }
5924 run_test 65j "set default striping on root directory (bug 6367)="
5925
5926 test_65k() { # bug11679
5927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5928         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5929         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5930
5931         local disable_precreate=true
5932         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
5933                 disable_precreate=false
5934
5935         echo "Check OST status: "
5936         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
5937                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
5938
5939         for OSC in $MDS_OSCS; do
5940                 echo $OSC "is active"
5941                 do_facet $SINGLEMDS lctl --device %$OSC activate
5942         done
5943
5944         for INACTIVE_OSC in $MDS_OSCS; do
5945                 local ost=$(osc_to_ost $INACTIVE_OSC)
5946                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
5947                                lov.*md*.target_obd |
5948                                awk -F: /$ost/'{ print $1 }' | head -n 1)
5949
5950                 mkdir -p $DIR/$tdir
5951                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
5952                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
5953
5954                 echo "Deactivate: " $INACTIVE_OSC
5955                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5956
5957                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
5958                               osp.$ost*MDT0000.create_count")
5959                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
5960                                   osp.$ost*MDT0000.max_create_count")
5961                 $disable_precreate &&
5962                         do_facet $SINGLEMDS "lctl set_param -n \
5963                                 osp.$ost*MDT0000.max_create_count=0"
5964
5965                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
5966                         [ -f $DIR/$tdir/$idx ] && continue
5967                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
5968                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
5969                                 error "setstripe $idx should succeed"
5970                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
5971                 done
5972                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
5973                 rmdir $DIR/$tdir
5974
5975                 do_facet $SINGLEMDS "lctl set_param -n \
5976                         osp.$ost*MDT0000.max_create_count=$max_count"
5977                 do_facet $SINGLEMDS "lctl set_param -n \
5978                         osp.$ost*MDT0000.create_count=$count"
5979                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5980                 echo $INACTIVE_OSC "is Activate"
5981
5982                 wait_osc_import_state mds ost$ostnum FULL
5983         done
5984 }
5985 run_test 65k "validate manual striping works properly with deactivated OSCs"
5986
5987 test_65l() { # bug 12836
5988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5989         test_mkdir -p $DIR/$tdir/test_dir
5990         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5991         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5992 }
5993 run_test 65l "lfs find on -1 stripe dir ========================"
5994
5995 test_65m() {
5996         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
5997         true
5998 }
5999 run_test 65m "normal user can't set filesystem default stripe"
6000
6001 # bug 2543 - update blocks count on client
6002 test_66() {
6003         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6004         COUNT=${COUNT:-8}
6005         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6006         sync; sync_all_data; sync; sync_all_data
6007         cancel_lru_locks osc
6008         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6009         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6010 }
6011 run_test 66 "update inode blocks count on client ==============="
6012
6013 meminfo() {
6014         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6015 }
6016
6017 swap_used() {
6018         swapon -s | awk '($1 == "'$1'") { print $4 }'
6019 }
6020
6021 # bug5265, obdfilter oa2dentry return -ENOENT
6022 # #define OBD_FAIL_SRV_ENOENT 0x217
6023 test_69() {
6024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6025         remote_ost_nodsh && skip "remote OST with nodsh" && return
6026
6027         f="$DIR/$tfile"
6028         $SETSTRIPE -c 1 -i 0 $f
6029
6030         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6031
6032         do_facet ost1 lctl set_param fail_loc=0x217
6033         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6034         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6035
6036         do_facet ost1 lctl set_param fail_loc=0
6037         $DIRECTIO write $f 0 2 || error "write error"
6038
6039         cancel_lru_locks osc
6040         $DIRECTIO read $f 0 1 || error "read error"
6041
6042         do_facet ost1 lctl set_param fail_loc=0x217
6043         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6044
6045         do_facet ost1 lctl set_param fail_loc=0
6046         rm -f $f
6047 }
6048 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6049
6050 test_71() {
6051         test_mkdir -p $DIR/$tdir
6052         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6053         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6054 }
6055 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6056
6057 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6058         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6059         [ "$RUNAS_ID" = "$UID" ] &&
6060                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6061
6062         # Check that testing environment is properly set up. Skip if not
6063         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
6064                 skip_env "User $RUNAS_ID does not exist - skipping"
6065                 return 0
6066         }
6067         touch $DIR/$tfile
6068         chmod 777 $DIR/$tfile
6069         chmod ug+s $DIR/$tfile
6070         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6071                 error "$RUNAS dd $DIR/$tfile failed"
6072         # See if we are still setuid/sgid
6073         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6074                 error "S/gid is not dropped on write"
6075         # Now test that MDS is updated too
6076         cancel_lru_locks mdc
6077         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6078                 error "S/gid is not dropped on MDS"
6079         rm -f $DIR/$tfile
6080 }
6081 run_test 72a "Test that remove suid works properly (bug5695) ===="
6082
6083 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6084         local perm
6085
6086         [ "$RUNAS_ID" = "$UID" ] && \
6087                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6088         [ "$RUNAS_ID" -eq 0 ] && \
6089                 skip_env "RUNAS_ID = 0 -- skipping" && return
6090
6091         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6092         # Check that testing environment is properly set up. Skip if not
6093         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
6094                 skip_env "User $RUNAS_ID does not exist - skipping"
6095                 return 0
6096         }
6097         touch $DIR/${tfile}-f{g,u}
6098         test_mkdir $DIR/${tfile}-dg
6099         test_mkdir $DIR/${tfile}-du
6100         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6101         chmod g+s $DIR/${tfile}-{f,d}g
6102         chmod u+s $DIR/${tfile}-{f,d}u
6103         for perm in 777 2777 4777; do
6104                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6105                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6106                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6107                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6108         done
6109         true
6110 }
6111 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6112
6113 # bug 3462 - multiple simultaneous MDC requests
6114 test_73() {
6115         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6116         test_mkdir $DIR/d73-1
6117         test_mkdir $DIR/d73-2
6118         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6119         pid1=$!
6120
6121         lctl set_param fail_loc=0x80000129
6122         $MULTIOP $DIR/d73-1/f73-2 Oc &
6123         sleep 1
6124         lctl set_param fail_loc=0
6125
6126         $MULTIOP $DIR/d73-2/f73-3 Oc &
6127         pid3=$!
6128
6129         kill -USR1 $pid1
6130         wait $pid1 || return 1
6131
6132         sleep 25
6133
6134         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6135         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6136         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6137
6138         rm -rf $DIR/d73-*
6139 }
6140 run_test 73 "multiple MDC requests (should not deadlock)"
6141
6142 test_74a() { # bug 6149, 6184
6143         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6144         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6145         #
6146         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6147         # will spin in a tight reconnection loop
6148         touch $DIR/f74a
6149         $LCTL set_param fail_loc=0x8000030e
6150         # get any lock that won't be difficult - lookup works.
6151         ls $DIR/f74a
6152         $LCTL set_param fail_loc=0
6153         rm -f $DIR/f74a
6154         true
6155 }
6156 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6157
6158 test_74b() { # bug 13310
6159         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6160         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6161         #
6162         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6163         # will spin in a tight reconnection loop
6164         $LCTL set_param fail_loc=0x8000030e
6165         # get a "difficult" lock
6166         touch $DIR/f74b
6167         $LCTL set_param fail_loc=0
6168         rm -f $DIR/f74b
6169         true
6170 }
6171 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6172
6173 test_74c() {
6174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6175         #define OBD_FAIL_LDLM_NEW_LOCK
6176         $LCTL set_param fail_loc=0x319
6177         touch $DIR/$tfile && error "touch successful"
6178         $LCTL set_param fail_loc=0
6179         true
6180 }
6181 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6182
6183 num_inodes() {
6184         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6185 }
6186
6187 test_76() { # Now for bug 20433, added originally in bug 1443
6188         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6189         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6190         cancel_lru_locks osc
6191         BEFORE_INODES=$(num_inodes)
6192         echo "before inodes: $BEFORE_INODES"
6193         local COUNT=1000
6194         [ "$SLOW" = "no" ] && COUNT=100
6195         for i in $(seq $COUNT); do
6196                 touch $DIR/$tfile
6197                 rm -f $DIR/$tfile
6198         done
6199         cancel_lru_locks osc
6200         AFTER_INODES=$(num_inodes)
6201         echo "after inodes: $AFTER_INODES"
6202         local wait=0
6203         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6204                 sleep 2
6205                 AFTER_INODES=$(num_inodes)
6206                 wait=$((wait+2))
6207                 echo "wait $wait seconds inodes: $AFTER_INODES"
6208                 if [ $wait -gt 30 ]; then
6209                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6210                 fi
6211         done
6212 }
6213 run_test 76 "confirm clients recycle inodes properly ===="
6214
6215
6216 export ORIG_CSUM=""
6217 set_checksums()
6218 {
6219         # Note: in sptlrpc modes which enable its own bulk checksum, the
6220         # original crc32_le bulk checksum will be automatically disabled,
6221         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6222         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6223         # In this case set_checksums() will not be no-op, because sptlrpc
6224         # bulk checksum will be enabled all through the test.
6225
6226         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6227         lctl set_param -n osc.*.checksums $1
6228         return 0
6229 }
6230
6231 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6232                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6233 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6234 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6235 set_checksum_type()
6236 {
6237         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6238         log "set checksum type to $1"
6239         return 0
6240 }
6241 F77_TMP=$TMP/f77-temp
6242 F77SZ=8
6243 setup_f77() {
6244         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6245                 error "error writing to $F77_TMP"
6246 }
6247
6248 test_77a() { # bug 10889
6249         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6250         $GSS && skip "could not run with gss" && return
6251         [ ! -f $F77_TMP ] && setup_f77
6252         set_checksums 1
6253         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6254         set_checksums 0
6255         rm -f $DIR/$tfile
6256 }
6257 run_test 77a "normal checksum read/write operation"
6258
6259 test_77b() { # bug 10889
6260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6261         $GSS && skip "could not run with gss" && return
6262         [ ! -f $F77_TMP ] && setup_f77
6263         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6264         $LCTL set_param fail_loc=0x80000409
6265         set_checksums 1
6266
6267         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6268                 error "dd error: $?"
6269         $LCTL set_param fail_loc=0
6270
6271         for algo in $CKSUM_TYPES; do
6272                 cancel_lru_locks osc
6273                 set_checksum_type $algo
6274                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6275                 $LCTL set_param fail_loc=0x80000408
6276                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6277                 $LCTL set_param fail_loc=0
6278         done
6279         set_checksums 0
6280         set_checksum_type $ORIG_CSUM_TYPE
6281         rm -f $DIR/$tfile
6282 }
6283 run_test 77b "checksum error on client write, read"
6284
6285 cleanup_77c() {
6286         trap 0
6287         set_checksums 0
6288         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
6289         $check_ost &&
6290                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
6291         [ -n $osc_file_prefix ] && rm -f ${osc_file_prefix}*
6292         $check_ost && [ -n $ost_file_prefix ] &&
6293                 do_facet ost1 rm -f ${ost_file_prefix}\*
6294 }
6295
6296 test_77c() {
6297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6298         $GSS && skip "could not run with gss" && return
6299
6300         local bad1
6301         local osc_file_prefix
6302         local osc_file
6303         local check_ost=false
6304         local ost_file_prefix
6305         local ost_file
6306         local orig_cksum
6307         local dump_cksum
6308         local fid
6309
6310         # ensure corruption will occur on first OSS/OST
6311         $LFS setstripe -i 0 $DIR/$tfile
6312
6313         [ ! -f $F77_TMP ] && setup_f77
6314         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6315                 error "dd write error: $?"
6316         fid=$($LFS path2fid $DIR/$tfile)
6317
6318         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
6319         then
6320                 check_ost=true
6321                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
6322                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
6323         else
6324                 echo "OSS do not support bulk pages dump upon error"
6325         fi
6326
6327         osc_file_prefix=$($LCTL get_param -n debug_path)
6328         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
6329
6330         trap cleanup_77c EXIT
6331
6332         set_checksums 1
6333         # enable bulk pages dump upon error on Client
6334         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
6335         # enable bulk pages dump upon error on OSS
6336         $check_ost &&
6337                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
6338
6339         # flush Client cache to allow next read to reach OSS
6340         cancel_lru_locks osc
6341
6342         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
6343         $LCTL set_param fail_loc=0x80000408
6344         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
6345         $LCTL set_param fail_loc=0
6346
6347         rm -f $DIR/$tfile
6348
6349         # check cksum dump on Client
6350         osc_file=$(ls ${osc_file_prefix}*)
6351         [ -n "$osc_file" ] || error "no checksum dump file on Client"
6352         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
6353         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
6354         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
6355         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
6356                      cksum)
6357         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
6358         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6359                 error "dump content does not match on Client"
6360
6361         $check_ost || { skip "No need to check cksum dump on OSS"; return 0; }
6362
6363         # check cksum dump on OSS
6364         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
6365         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
6366         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
6367         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
6368         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6369                 error "dump content does not match on OSS"
6370
6371         cleanup_77c
6372 }
6373 run_test 77c "checksum error on client read with debug"
6374
6375 test_77d() { # bug 10889
6376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6377         $GSS && skip "could not run with gss" && return
6378         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6379         $LCTL set_param fail_loc=0x80000409
6380         set_checksums 1
6381         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6382                 error "direct write: rc=$?"
6383         $LCTL set_param fail_loc=0
6384         set_checksums 0
6385
6386         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6387         $LCTL set_param fail_loc=0x80000408
6388         set_checksums 1
6389         cancel_lru_locks osc
6390         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6391                 error "direct read: rc=$?"
6392         $LCTL set_param fail_loc=0
6393         set_checksums 0
6394 }
6395 run_test 77d "checksum error on OST direct write, read"
6396
6397 test_77f() { # bug 10889
6398         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6399         $GSS && skip "could not run with gss" && return
6400         set_checksums 1
6401         for algo in $CKSUM_TYPES; do
6402                 cancel_lru_locks osc
6403                 set_checksum_type $algo
6404                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6405                 $LCTL set_param fail_loc=0x409
6406                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6407                         error "direct write succeeded"
6408                 $LCTL set_param fail_loc=0
6409         done
6410         set_checksum_type $ORIG_CSUM_TYPE
6411         set_checksums 0
6412 }
6413 run_test 77f "repeat checksum error on write (expect error)"
6414
6415 test_77g() { # bug 10889
6416         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6417         $GSS && skip "could not run with gss" && return
6418         remote_ost_nodsh && skip "remote OST with nodsh" && return
6419
6420         [ ! -f $F77_TMP ] && setup_f77
6421
6422         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6423         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6424         do_facet ost1 lctl set_param fail_loc=0x8000021a
6425         set_checksums 1
6426         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6427                 error "write error: rc=$?"
6428         do_facet ost1 lctl set_param fail_loc=0
6429         set_checksums 0
6430
6431         cancel_lru_locks osc
6432         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6433         do_facet ost1 lctl set_param fail_loc=0x8000021b
6434         set_checksums 1
6435         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6436         do_facet ost1 lctl set_param fail_loc=0
6437         set_checksums 0
6438 }
6439 run_test 77g "checksum error on OST write, read"
6440
6441 test_77j() { # bug 13805
6442         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6443         $GSS && skip "could not run with gss" && return
6444         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6445         lctl set_param fail_loc=0x40c
6446         remount_client $MOUNT
6447         lctl set_param fail_loc=0
6448         # wait async osc connect to finish and reflect updated state value
6449         local i
6450         for (( i=0; i < OSTCOUNT; i++ )) ; do
6451                 wait_osc_import_state client ost$((i+1)) FULL
6452         done
6453
6454         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6455                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6456                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6457                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6458         done
6459         remount_client $MOUNT
6460 }
6461 run_test 77j "client only supporting ADLER32"
6462
6463 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6464 rm -f $F77_TMP
6465 unset F77_TMP
6466
6467 cleanup_test_78() {
6468         trap 0
6469         rm -f $DIR/$tfile
6470 }
6471
6472 test_78() { # bug 10901
6473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6474         remote_ost || { skip_env "local OST" && return; }
6475
6476         NSEQ=5
6477         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6478         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6479         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6480         echo "MemTotal: $MEMTOTAL"
6481
6482         # reserve 256MB of memory for the kernel and other running processes,
6483         # and then take 1/2 of the remaining memory for the read/write buffers.
6484         if [ $MEMTOTAL -gt 512 ] ;then
6485                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6486         else
6487                 # for those poor memory-starved high-end clusters...
6488                 MEMTOTAL=$((MEMTOTAL / 2))
6489         fi
6490         echo "Mem to use for directio: $MEMTOTAL"
6491
6492         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6493         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6494         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6495         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6496                 head -n1)
6497         echo "Smallest OST: $SMALLESTOST"
6498         [[ $SMALLESTOST -lt 10240 ]] &&
6499                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6500
6501         trap cleanup_test_78 EXIT
6502
6503         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6504                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6505
6506         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6507         echo "File size: $F78SIZE"
6508         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6509         for i in $(seq 1 $NSEQ); do
6510                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6511                 echo directIO rdwr round $i of $NSEQ
6512                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6513         done
6514
6515         cleanup_test_78
6516 }
6517 run_test 78 "handle large O_DIRECT writes correctly ============"
6518
6519 test_79() { # bug 12743
6520         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6521         wait_delete_completed
6522
6523         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6524         BKFREE=$(calc_osc_kbytes kbytesfree)
6525         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6526
6527         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6528         DFTOTAL=`echo $STRING | cut -d, -f1`
6529         DFUSED=`echo $STRING  | cut -d, -f2`
6530         DFAVAIL=`echo $STRING | cut -d, -f3`
6531         DFFREE=$(($DFTOTAL - $DFUSED))
6532
6533         ALLOWANCE=$((64 * $OSTCOUNT))
6534
6535         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6536            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6537                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6538         fi
6539         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6540            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6541                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6542         fi
6543         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6544            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6545                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6546         fi
6547 }
6548 run_test 79 "df report consistency check ======================="
6549
6550 test_80() { # bug 10718
6551         remote_ost_nodsh && skip "remote OST with nodsh" && return
6552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6553         # relax strong synchronous semantics for slow backends like ZFS
6554         local soc="obdfilter.*.sync_on_lock_cancel"
6555         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6556         local hosts=
6557         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6558                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6559                           facet_active_host $host; done | sort -u)
6560                 do_nodes $hosts lctl set_param $soc=never
6561         fi
6562
6563         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6564         sync; sleep 1; sync
6565         local BEFORE=`date +%s`
6566         cancel_lru_locks osc
6567         local AFTER=`date +%s`
6568         local DIFF=$((AFTER-BEFORE))
6569         if [ $DIFF -gt 1 ] ; then
6570                 error "elapsed for 1M@1T = $DIFF"
6571         fi
6572
6573         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6574
6575         rm -f $DIR/$tfile
6576 }
6577 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6578
6579 test_81a() { # LU-456
6580         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6581         remote_ost_nodsh && skip "remote OST with nodsh" && return
6582         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6583         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6584         do_facet ost1 lctl set_param fail_loc=0x80000228
6585
6586         # write should trigger a retry and success
6587         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6588         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6589         RC=$?
6590         if [ $RC -ne 0 ] ; then
6591                 error "write should success, but failed for $RC"
6592         fi
6593 }
6594 run_test 81a "OST should retry write when get -ENOSPC ==============="
6595
6596 test_81b() { # LU-456
6597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6598         remote_ost_nodsh && skip "remote OST with nodsh" && return
6599         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6600         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6601         do_facet ost1 lctl set_param fail_loc=0x228
6602
6603         # write should retry several times and return -ENOSPC finally
6604         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6605         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6606         RC=$?
6607         ENOSPC=28
6608         if [ $RC -ne $ENOSPC ] ; then
6609                 error "dd should fail for -ENOSPC, but succeed."
6610         fi
6611 }
6612 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6613
6614 test_82() { # LU-1031
6615         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6616         local gid1=14091995
6617         local gid2=16022000
6618
6619         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6620         local MULTIPID1=$!
6621         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6622         local MULTIPID2=$!
6623         kill -USR1 $MULTIPID2
6624         sleep 2
6625         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6626                 error "First grouplock does not block second one"
6627         else
6628                 echo "Second grouplock blocks first one"
6629         fi
6630         kill -USR1 $MULTIPID1
6631         wait $MULTIPID1
6632         wait $MULTIPID2
6633 }
6634 run_test 82 "Basic grouplock test ==============================="
6635
6636 test_83() {
6637         local sfile="/boot/System.map-$(uname -r)"
6638         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
6639         $LCTL set_param fail_loc=0x140d
6640         cp $sfile $DIR/$tfile || error "write failed"
6641         diff -c $sfile $DIR/$tfile || error "files are different"
6642         $LCTL set_param fail_loc=0
6643         rm -f $DIR/$tfile
6644 }
6645 run_test 83 "Short write in ptask ==============================="
6646
6647 test_99a() {
6648         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6649                 return
6650         test_mkdir -p $DIR/d99cvsroot
6651         chown $RUNAS_ID $DIR/d99cvsroot
6652         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6653         cd $TMP
6654
6655         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6656         cd $oldPWD
6657 }
6658 run_test 99a "cvs init ========================================="
6659
6660 test_99b() {
6661         [ -z "$(which cvs 2>/dev/null)" ] &&
6662                 skip_env "could not find cvs" && return
6663         [ ! -d $DIR/d99cvsroot ] && test_99a
6664         cd /etc/init.d
6665         # some versions of cvs import exit(1) when asked to import links or
6666         # files they can't read.  ignore those files.
6667         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6668                         ! -perm /4 -printf '-I %f\n')
6669         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6670                 d99reposname vtag rtag
6671 }
6672 run_test 99b "cvs import ======================================="
6673
6674 test_99c() {
6675         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6676         [ ! -d $DIR/d99cvsroot ] && test_99b
6677         cd $DIR
6678         test_mkdir -p $DIR/d99reposname
6679         chown $RUNAS_ID $DIR/d99reposname
6680         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6681 }
6682 run_test 99c "cvs checkout ====================================="
6683
6684 test_99d() {
6685         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6686         [ ! -d $DIR/d99cvsroot ] && test_99c
6687         cd $DIR/d99reposname
6688         $RUNAS touch foo99
6689         $RUNAS cvs add -m 'addmsg' foo99
6690 }
6691 run_test 99d "cvs add =========================================="
6692
6693 test_99e() {
6694         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6695         [ ! -d $DIR/d99cvsroot ] && test_99c
6696         cd $DIR/d99reposname
6697         $RUNAS cvs update
6698 }
6699 run_test 99e "cvs update ======================================="
6700
6701 test_99f() {
6702         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6703         [ ! -d $DIR/d99cvsroot ] && test_99d
6704         cd $DIR/d99reposname
6705         $RUNAS cvs commit -m 'nomsg' foo99
6706     rm -fr $DIR/d99cvsroot
6707 }
6708 run_test 99f "cvs commit ======================================="
6709
6710 test_100() {
6711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6712         [[ "$NETTYPE" =~ tcp ]] ||
6713                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" &&
6714                         return ; }
6715
6716         remote_ost_nodsh && skip "remote OST with nodsh" && return
6717         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6718         remote_servers ||
6719                 { skip "useless for local single node setup" && return; }
6720
6721         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6722                 [ "$PROT" != "tcp" ] && continue
6723                 RPORT=$(echo $REMOTE | cut -d: -f2)
6724                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6725
6726                 rc=0
6727                 LPORT=`echo $LOCAL | cut -d: -f2`
6728                 if [ $LPORT -ge 1024 ]; then
6729                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6730                         netstat -tna
6731                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6732                 fi
6733         done
6734         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6735 }
6736 run_test 100 "check local port using privileged port ==========="
6737
6738 function get_named_value()
6739 {
6740     local tag
6741
6742     tag=$1
6743     while read ;do
6744         line=$REPLY
6745         case $line in
6746         $tag*)
6747             echo $line | sed "s/^$tag[ ]*//"
6748             break
6749             ;;
6750         esac
6751     done
6752 }
6753
6754 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6755                    awk '/^max_cached_mb/ { print $2 }')
6756
6757 cleanup_101a() {
6758         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6759         trap 0
6760 }
6761
6762 test_101a() {
6763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6764         [ $MDSCOUNT -ge 2 ] && skip "needs < 2 MDTs" && return #LU-4322
6765         local s
6766         local discard
6767         local nreads=10000
6768         local cache_limit=32
6769
6770         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6771         trap cleanup_101a EXIT
6772         $LCTL set_param -n llite.*.read_ahead_stats 0
6773         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6774
6775         #
6776         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6777         #
6778         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6779         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6780
6781         discard=0
6782         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6783                 get_named_value 'read but discarded' | cut -d" " -f1); do
6784                         discard=$(($discard + $s))
6785         done
6786         cleanup_101a
6787
6788         if [[ $(($discard * 10)) -gt $nreads ]]; then
6789                 $LCTL get_param osc.*-osc*.rpc_stats
6790                 $LCTL get_param llite.*.read_ahead_stats
6791                 error "too many ($discard) discarded pages"
6792         fi
6793         rm -f $DIR/$tfile || true
6794 }
6795 run_test 101a "check read-ahead for random reads ================"
6796
6797 setup_test101bc() {
6798         test_mkdir -p $DIR/$tdir
6799         local STRIPE_SIZE=$1
6800         local FILE_LENGTH=$2
6801         STRIPE_OFFSET=0
6802
6803         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6804
6805         local list=$(comma_list $(osts_nodes))
6806         set_osd_param $list '' read_cache_enable 0
6807         set_osd_param $list '' writethrough_cache_enable 0
6808
6809         trap cleanup_test101bc EXIT
6810         # prepare the read-ahead file
6811         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6812
6813         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6814                                 count=$FILE_SIZE_MB 2> /dev/null
6815
6816 }
6817
6818 cleanup_test101bc() {
6819         trap 0
6820         rm -rf $DIR/$tdir
6821         rm -f $DIR/$tfile
6822
6823         local list=$(comma_list $(osts_nodes))
6824         set_osd_param $list '' read_cache_enable 1
6825         set_osd_param $list '' writethrough_cache_enable 1
6826 }
6827
6828 calc_total() {
6829         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6830 }
6831
6832 ra_check_101() {
6833         local READ_SIZE=$1
6834         local STRIPE_SIZE=$2
6835         local FILE_LENGTH=$3
6836         local RA_INC=1048576
6837         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6838         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6839                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6840         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6841                         get_named_value 'read but discarded' |
6842                         cut -d" " -f1 | calc_total)
6843         if [[ $DISCARD -gt $discard_limit ]]; then
6844                 $LCTL get_param llite.*.read_ahead_stats
6845                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6846         else
6847                 echo "Read-ahead success for size ${READ_SIZE}"
6848         fi
6849 }
6850
6851 test_101b() {
6852         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6853         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6854         local STRIPE_SIZE=1048576
6855         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6856         if [ $SLOW == "yes" ]; then
6857                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6858         else
6859                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6860         fi
6861
6862         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6863
6864         # prepare the read-ahead file
6865         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6866         cancel_lru_locks osc
6867         for BIDX in 2 4 8 16 32 64 128 256
6868         do
6869                 local BSIZE=$((BIDX*4096))
6870                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6871                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6872                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6873                 $LCTL set_param -n llite.*.read_ahead_stats 0
6874                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6875                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6876                 cancel_lru_locks osc
6877                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6878         done
6879         cleanup_test101bc
6880         true
6881 }
6882 run_test 101b "check stride-io mode read-ahead ================="
6883
6884 test_101c() {
6885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6886         local STRIPE_SIZE=1048576
6887         local FILE_LENGTH=$((STRIPE_SIZE*100))
6888         local nreads=10000
6889         local osc_rpc_stats
6890
6891         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6892
6893         cancel_lru_locks osc
6894         $LCTL set_param osc.*.rpc_stats 0
6895         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6896         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6897                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6898                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6899                 local size
6900
6901                 if [ $lines -le 20 ]; then
6902                         continue
6903                 fi
6904                 for size in 1 2 4 8; do
6905                         local rpc=$(echo "$stats" |
6906                                     awk '($1 == "'$size':") {print $2; exit; }')
6907                         [ $rpc != 0 ] &&
6908                                 error "Small $((size*4))k read IO $rpc !"
6909                 done
6910                 echo "$osc_rpc_stats check passed!"
6911         done
6912         cleanup_test101bc
6913         true
6914 }
6915 run_test 101c "check stripe_size aligned read-ahead ================="
6916
6917 set_read_ahead() {
6918         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6919         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6920 }
6921
6922 test_101d() {
6923         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6924         local file=$DIR/$tfile
6925         local sz_MB=${FILESIZE_101d:-500}
6926         local ra_MB=${READAHEAD_MB:-40}
6927
6928         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6929         [ $free_MB -lt $sz_MB ] &&
6930                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6931
6932         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6933         $SETSTRIPE -c -1 $file || error "setstripe failed"
6934
6935         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6936         echo Cancel LRU locks on lustre client to flush the client cache
6937         cancel_lru_locks osc
6938
6939         echo Disable read-ahead
6940         local old_READAHEAD=$(set_read_ahead 0)
6941
6942         echo Reading the test file $file with read-ahead disabled
6943         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6944
6945         echo Cancel LRU locks on lustre client to flush the client cache
6946         cancel_lru_locks osc
6947         echo Enable read-ahead with ${ra_MB}MB
6948         set_read_ahead $ra_MB
6949
6950         echo Reading the test file $file with read-ahead enabled
6951         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6952
6953         echo "read-ahead disabled time read $raOFF"
6954         echo "read-ahead enabled  time read $raON"
6955
6956         set_read_ahead $old_READAHEAD
6957         rm -f $file
6958         wait_delete_completed
6959
6960         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6961                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6962 }
6963 run_test 101d "file read with and without read-ahead enabled"
6964
6965 test_101e() {
6966         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6967         local file=$DIR/$tfile
6968         local size_KB=500  #KB
6969         local count=100
6970         local bsize=1024
6971
6972         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6973         local need_KB=$((count * size_KB))
6974         [[ $free_KB -le $need_KB ]] &&
6975                 skip_env "Need free space $need_KB, have $free_KB" && return
6976
6977         echo "Creating $count ${size_KB}K test files"
6978         for ((i = 0; i < $count; i++)); do
6979                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6980         done
6981
6982         echo "Cancel LRU locks on lustre client to flush the client cache"
6983         cancel_lru_locks osc
6984
6985         echo "Reset readahead stats"
6986         $LCTL set_param -n llite.*.read_ahead_stats 0
6987
6988         for ((i = 0; i < $count; i++)); do
6989                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6990         done
6991
6992         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6993                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6994
6995         for ((i = 0; i < $count; i++)); do
6996                 rm -rf $file.$i 2>/dev/null
6997         done
6998
6999         #10000 means 20% reads are missing in readahead
7000         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
7001 }
7002 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
7003
7004 test_101f() {
7005         which iozone || { skip "no iozone installed" && return; }
7006
7007         local old_debug=$($LCTL get_param debug)
7008         old_debug=${old_debug#*=}
7009         $LCTL set_param debug="reada mmap"
7010
7011         # create a test file
7012         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
7013
7014         echo Cancel LRU locks on lustre client to flush the client cache
7015         cancel_lru_locks osc
7016
7017         echo Reset readahead stats
7018         $LCTL set_param -n llite.*.read_ahead_stats 0
7019
7020         echo mmap read the file with small block size
7021         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
7022                 > /dev/null 2>&1
7023
7024         echo checking missing pages
7025         $LCTL get_param llite.*.read_ahead_stats
7026         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7027                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7028
7029         $LCTL set_param debug="$old_debug"
7030         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7031         rm -f $DIR/$tfile
7032 }
7033 run_test 101f "check mmap read performance"
7034
7035 test_101g_brw_size_test() {
7036         local mb=$1
7037         local pages=$((mb * 1048576 / $(page_size)))
7038
7039         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7040                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7041         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7042                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7043                         return 2
7044         done
7045
7046         $LCTL set_param -n osc.*.rpc_stats=0
7047
7048         # 10 RPCs should be enough for the test
7049         local count=10
7050         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
7051                 { error "dd write ${mb} MB blocks failed"; return 3; }
7052         cancel_lru_locks osc
7053         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
7054                 { error "dd write ${mb} MB blocks failed"; return 4; }
7055
7056         # calculate number of full-sized read and write RPCs
7057         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7058                 sed -n '/pages per rpc/,/^$/p' |
7059                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7060                 END { print reads,writes }'))
7061         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7062                 return 5
7063         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7064                 return 6
7065
7066         return 0
7067 }
7068
7069 test_101g() {
7070         local rpcs
7071         local osts=$(get_facets OST)
7072         local list=$(comma_list $(osts_nodes))
7073         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7074         local brw_size="obdfilter.*.brw_size"
7075         local ostver=$(lustre_version_code ost1)
7076
7077         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7078
7079         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7080         if [ $ostver -ge $(version_code 2.8.52) ] ||
7081            [ $ostver -ge $(version_code 2.7.17) -a \
7082              $ostver -lt $(version_code 2.7.50) ]; then
7083                 [ $ostver -ge $(version_code 2.9.52) ] && suffix="M"
7084                 if [[ $orig_mb -lt 16 ]]; then
7085                         save_lustre_params $osts "$brw_size" > $p
7086                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7087                                 error "set 16MB RPC size failed"
7088
7089                         echo "remount client to enable new RPC size"
7090                         remount_client $MOUNT || error "remount_client failed"
7091                 fi
7092
7093                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7094                 # should be able to set brw_size=12, but no rpc_stats for that
7095                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7096         fi
7097
7098         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7099
7100         if [[ $orig_mb -lt 16 ]]; then
7101                 restore_lustre_params < $p
7102                 remount_client $MOUNT || error "remount_client restore failed"
7103         fi
7104
7105         rm -f $p $DIR/$tfile
7106 }
7107 run_test 101g "Big bulk(4/16 MiB) readahead"
7108
7109 setup_test102() {
7110         test_mkdir $DIR/$tdir
7111         chown $RUNAS_ID $DIR/$tdir
7112         STRIPE_SIZE=65536
7113         STRIPE_OFFSET=1
7114         STRIPE_COUNT=$OSTCOUNT
7115         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7116
7117         trap cleanup_test102 EXIT
7118         cd $DIR
7119         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7120         cd $DIR/$tdir
7121         for num in 1 2 3 4; do
7122                 for count in $(seq 1 $STRIPE_COUNT); do
7123                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7124                                 local size=`expr $STRIPE_SIZE \* $num`
7125                                 local file=file"$num-$idx-$count"
7126                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7127                         done
7128                 done
7129         done
7130
7131         cd $DIR
7132         $1 tar cf $TMP/f102.tar $tdir --xattrs
7133 }
7134
7135 cleanup_test102() {
7136         trap 0
7137         rm -f $TMP/f102.tar
7138         rm -rf $DIR/d0.sanity/d102
7139 }
7140
7141 test_102a() {
7142         local testfile=$DIR/$tfile
7143
7144         touch $testfile
7145
7146         [ "$UID" != 0 ] && skip_env "must run as root" && return
7147         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7148                 skip_env "must have user_xattr" && return
7149
7150         [ -z "$(which setfattr 2>/dev/null)" ] &&
7151                 skip_env "could not find setfattr" && return
7152
7153         echo "set/get xattr..."
7154         setfattr -n trusted.name1 -v value1 $testfile ||
7155                 error "setfattr -n trusted.name1=value1 $testfile failed"
7156         getfattr -n trusted.name1 $testfile 2> /dev/null |
7157           grep "trusted.name1=.value1" ||
7158                 error "$testfile missing trusted.name1=value1"
7159
7160         setfattr -n user.author1 -v author1 $testfile ||
7161                 error "setfattr -n user.author1=author1 $testfile failed"
7162         getfattr -n user.author1 $testfile 2> /dev/null |
7163           grep "user.author1=.author1" ||
7164                 error "$testfile missing trusted.author1=author1"
7165
7166         echo "listxattr..."
7167         setfattr -n trusted.name2 -v value2 $testfile ||
7168                 error "$testfile unable to set trusted.name2"
7169         setfattr -n trusted.name3 -v value3 $testfile ||
7170                 error "$testfile unable to set trusted.name3"
7171         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7172             grep "trusted.name" | wc -l) -eq 3 ] ||
7173                 error "$testfile missing 3 trusted.name xattrs"
7174
7175         setfattr -n user.author2 -v author2 $testfile ||
7176                 error "$testfile unable to set user.author2"
7177         setfattr -n user.author3 -v author3 $testfile ||
7178                 error "$testfile unable to set user.author3"
7179         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7180             grep "user.author" | wc -l) -eq 3 ] ||
7181                 error "$testfile missing 3 user.author xattrs"
7182
7183         echo "remove xattr..."
7184         setfattr -x trusted.name1 $testfile ||
7185                 error "$testfile error deleting trusted.name1"
7186         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7187                 error "$testfile did not delete trusted.name1 xattr"
7188
7189         setfattr -x user.author1 $testfile ||
7190                 error "$testfile error deleting user.author1"
7191         echo "set lustre special xattr ..."
7192         $LFS setstripe -c1 $testfile
7193         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7194                 awk -F "=" '/trusted.lov/ { print $2 }' )
7195         setfattr -n "trusted.lov" -v $lovea $testfile ||
7196                 error "$testfile doesn't ignore setting trusted.lov again"
7197         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7198                 error "$testfile allow setting invalid trusted.lov"
7199         rm -f $testfile
7200 }
7201 run_test 102a "user xattr test =================================="
7202
7203 test_102b() {
7204         [ -z "$(which setfattr 2>/dev/null)" ] &&
7205                 skip_env "could not find setfattr" && return
7206
7207         # b10930: get/set/list trusted.lov xattr
7208         echo "get/set/list trusted.lov xattr ..."
7209         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7210         local testfile=$DIR/$tfile
7211         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7212                 error "setstripe failed"
7213         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7214                 error "getstripe failed"
7215         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7216                 error "can't get trusted.lov from $testfile"
7217
7218         local testfile2=${testfile}2
7219         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7220                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7221
7222         $MCREATE $testfile2
7223         setfattr -n trusted.lov -v $value $testfile2
7224         local stripe_size=$($GETSTRIPE -S $testfile2)
7225         local stripe_count=$($GETSTRIPE -c $testfile2)
7226         [[ $stripe_size -eq 65536 ]] ||
7227                 error "stripe size $stripe_size != 65536"
7228         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7229                 error "stripe count $stripe_count != $STRIPECOUNT"
7230         rm -f $DIR/$tfile
7231 }
7232 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7233
7234 test_102c() {
7235         [ -z "$(which setfattr 2>/dev/null)" ] &&
7236                 skip_env "could not find setfattr" && return
7237
7238         # b10930: get/set/list lustre.lov xattr
7239         echo "get/set/list lustre.lov xattr ..."
7240         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7241         test_mkdir $DIR/$tdir
7242         chown $RUNAS_ID $DIR/$tdir
7243         local testfile=$DIR/$tdir/$tfile
7244         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7245                 error "setstripe failed"
7246         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7247                 error "getstripe failed"
7248         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7249         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7250
7251         local testfile2=${testfile}2
7252         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7253                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7254
7255         $RUNAS $MCREATE $testfile2
7256         $RUNAS setfattr -n lustre.lov -v $value $testfile2
7257         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
7258         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
7259         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
7260         [ $stripe_count -eq $STRIPECOUNT ] ||
7261                 error "stripe count $stripe_count != $STRIPECOUNT"
7262 }
7263 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
7264
7265 compare_stripe_info1() {
7266         local stripe_index_all_zero=true
7267
7268         for num in 1 2 3 4; do
7269                 for count in $(seq 1 $STRIPE_COUNT); do
7270                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
7271                                 local size=$((STRIPE_SIZE * num))
7272                                 local file=file"$num-$offset-$count"
7273                                 stripe_size=$($LFS getstripe -S $PWD/$file)
7274                                 [[ $stripe_size -ne $size ]] &&
7275                                     error "$file: size $stripe_size != $size"
7276                                 stripe_count=$($LFS getstripe -c $PWD/$file)
7277                                 # allow fewer stripes to be created, ORI-601
7278                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
7279                                     error "$file: count $stripe_count != $count"
7280                                 stripe_index=$($LFS getstripe -i $PWD/$file)
7281                                 [[ $stripe_index -ne 0 ]] &&
7282                                         stripe_index_all_zero=false
7283                         done
7284                 done
7285         done
7286         $stripe_index_all_zero &&
7287                 error "all files are being extracted starting from OST index 0"
7288         return 0
7289 }
7290
7291 have_xattrs_include() {
7292         tar --help | grep -q xattrs-include &&
7293                 echo --xattrs-include="lustre.*"
7294 }
7295
7296 test_102d() {
7297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7298         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7299         local xinc=$(have_xattrs_include)
7300         setup_test102
7301         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $xinc
7302         cd $DIR/$tdir/$tdir
7303         compare_stripe_info1
7304 }
7305 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
7306
7307 test_102f() {
7308         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7309         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7310         local xinc=$(have_xattrs_include)
7311         setup_test102
7312         test_mkdir $DIR/$tdir.restore
7313         cd $DIR
7314         tar cf - --xattrs $tdir | tar xf - \
7315                 -C $DIR/$tdir.restore --xattrs $xinc
7316         cd $DIR/$tdir.restore/$tdir
7317         compare_stripe_info1
7318 }
7319 run_test 102f "tar copy files, not keep osts ==========="
7320
7321 grow_xattr() {
7322         local xsize=${1:-1024}  # in bytes
7323         local file=$DIR/$tfile
7324
7325         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7326                 skip "must have user_xattr" && return 0
7327         [ -z "$(which setfattr 2>/dev/null)" ] &&
7328                 skip_env "could not find setfattr" && return 0
7329         [ -z "$(which getfattr 2>/dev/null)" ] &&
7330                 skip_env "could not find getfattr" && return 0
7331
7332         touch $file
7333
7334         local value="$(generate_string $xsize)"
7335
7336         local xbig=trusted.big
7337         log "save $xbig on $file"
7338         setfattr -n $xbig -v $value $file ||
7339                 error "saving $xbig on $file failed"
7340
7341         local orig=$(get_xattr_value $xbig $file)
7342         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7343
7344         local xsml=trusted.sml
7345         log "save $xsml on $file"
7346         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7347
7348         local new=$(get_xattr_value $xbig $file)
7349         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7350
7351         log "grow $xsml on $file"
7352         setfattr -n $xsml -v "$value" $file ||
7353                 error "growing $xsml on $file failed"
7354
7355         new=$(get_xattr_value $xbig $file)
7356         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7357         log "$xbig still valid after growing $xsml"
7358
7359         rm -f $file
7360 }
7361
7362 test_102h() { # bug 15777
7363         grow_xattr 1024
7364 }
7365 run_test 102h "grow xattr from inside inode to external block"
7366
7367 test_102ha() {
7368         large_xattr_enabled || { skip "ea_inode feature disabled" && return; }
7369         grow_xattr $(max_xattr_size)
7370 }
7371 run_test 102ha "grow xattr from inside inode to external inode"
7372
7373 test_102i() { # bug 17038
7374         [ -z "$(which getfattr 2>/dev/null)" ] &&
7375                 skip "could not find getfattr" && return
7376         touch $DIR/$tfile
7377         ln -s $DIR/$tfile $DIR/${tfile}link
7378         getfattr -n trusted.lov $DIR/$tfile ||
7379                 error "lgetxattr on $DIR/$tfile failed"
7380         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7381                 grep -i "no such attr" ||
7382                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7383         rm -f $DIR/$tfile $DIR/${tfile}link
7384 }
7385 run_test 102i "lgetxattr test on symbolic link ============"
7386
7387 test_102j() {
7388         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7389         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7390         local xinc=$(have_xattrs_include)
7391         setup_test102 "$RUNAS"
7392         chown $RUNAS_ID $DIR/$tdir
7393         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $xinc
7394         cd $DIR/$tdir/$tdir
7395         compare_stripe_info1 "$RUNAS"
7396 }
7397 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7398
7399 test_102k() {
7400         [ -z "$(which setfattr 2>/dev/null)" ] &&
7401                 skip "could not find setfattr" && return
7402         touch $DIR/$tfile
7403         # b22187 just check that does not crash for regular file.
7404         setfattr -n trusted.lov $DIR/$tfile
7405         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
7406         local test_kdir=$DIR/$tdir
7407         test_mkdir $test_kdir
7408         local default_size=`$GETSTRIPE -S $test_kdir`
7409         local default_count=`$GETSTRIPE -c $test_kdir`
7410         local default_offset=`$GETSTRIPE -i $test_kdir`
7411         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7412                 error 'dir setstripe failed'
7413         setfattr -n trusted.lov $test_kdir
7414         local stripe_size=`$GETSTRIPE -S $test_kdir`
7415         local stripe_count=`$GETSTRIPE -c $test_kdir`
7416         local stripe_offset=`$GETSTRIPE -i $test_kdir`
7417         [ $stripe_size -eq $default_size ] ||
7418                 error "stripe size $stripe_size != $default_size"
7419         [ $stripe_count -eq $default_count ] ||
7420                 error "stripe count $stripe_count != $default_count"
7421         [ $stripe_offset -eq $default_offset ] ||
7422                 error "stripe offset $stripe_offset != $default_offset"
7423         rm -rf $DIR/$tfile $test_kdir
7424 }
7425 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7426
7427 test_102l() {
7428         [ -z "$(which getfattr 2>/dev/null)" ] &&
7429                 skip "could not find getfattr" && return
7430
7431         # LU-532 trusted. xattr is invisible to non-root
7432         local testfile=$DIR/$tfile
7433
7434         touch $testfile
7435
7436         echo "listxattr as user..."
7437         chown $RUNAS_ID $testfile
7438         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7439             grep -q "trusted" &&
7440                 error "$testfile trusted xattrs are user visible"
7441
7442         return 0;
7443 }
7444 run_test 102l "listxattr size test =================================="
7445
7446 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7447         local path=$DIR/$tfile
7448         touch $path
7449
7450         listxattr_size_check $path || error "listattr_size_check $path failed"
7451 }
7452 run_test 102m "Ensure listxattr fails on small bufffer ========"
7453
7454 cleanup_test102
7455
7456 getxattr() { # getxattr path name
7457         # Return the base64 encoding of the value of xattr name on path.
7458         local path=$1
7459         local name=$2
7460
7461         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7462         # file: $path
7463         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7464         #
7465         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7466
7467         getfattr --absolute-names --encoding=base64 --name=$name $path |
7468                 awk -F= -v name=$name '$1 == name {
7469                         print substr($0, index($0, "=") + 1);
7470         }'
7471 }
7472
7473 test_102n() { # LU-4101 mdt: protect internal xattrs
7474         [ -z "$(which setfattr 2>/dev/null)" ] &&
7475                 skip "could not find setfattr" && return
7476         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7477         then
7478                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7479                 return
7480         fi
7481
7482         local file0=$DIR/$tfile.0
7483         local file1=$DIR/$tfile.1
7484         local xattr0=$TMP/$tfile.0
7485         local xattr1=$TMP/$tfile.1
7486         local namelist="lov lma lmv link fid version som hsm"
7487         local name
7488         local value
7489
7490         rm -rf $file0 $file1 $xattr0 $xattr1
7491         touch $file0 $file1
7492
7493         # Get 'before' xattrs of $file1.
7494         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7495
7496         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7497                 namelist+=" lfsck_namespace"
7498         for name in $namelist; do
7499                 # Try to copy xattr from $file0 to $file1.
7500                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7501
7502                 setfattr --name=trusted.$name --value="$value" $file1 ||
7503                         error "setxattr 'trusted.$name' failed"
7504
7505                 # Try to set a garbage xattr.
7506                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7507
7508                 if [[ x$name == "xlov" ]]; then
7509                         setfattr --name=trusted.lov --value="$value" $file1 &&
7510                         error "setxattr invalid 'trusted.lov' success"
7511                 else
7512                         setfattr --name=trusted.$name --value="$value" $file1 ||
7513                                 error "setxattr invalid 'trusted.$name' failed"
7514                 fi
7515
7516                 # Try to remove the xattr from $file1. We don't care if this
7517                 # appears to succeed or fail, we just don't want there to be
7518                 # any changes or crashes.
7519                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7520         done
7521
7522         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7523         then
7524                 name="lfsck_ns"
7525                 # Try to copy xattr from $file0 to $file1.
7526                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7527
7528                 setfattr --name=trusted.$name --value="$value" $file1 ||
7529                         error "setxattr 'trusted.$name' failed"
7530
7531                 # Try to set a garbage xattr.
7532                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7533
7534                 setfattr --name=trusted.$name --value="$value" $file1 ||
7535                         error "setxattr 'trusted.$name' failed"
7536
7537                 # Try to remove the xattr from $file1. We don't care if this
7538                 # appears to succeed or fail, we just don't want there to be
7539                 # any changes or crashes.
7540                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7541         fi
7542
7543         # Get 'after' xattrs of file1.
7544         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7545
7546         if ! diff $xattr0 $xattr1; then
7547                 error "before and after xattrs of '$file1' differ"
7548         fi
7549
7550         rm -rf $file0 $file1 $xattr0 $xattr1
7551
7552         return 0
7553 }
7554 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7555
7556 test_102p() { # LU-4703 setxattr did not check ownership
7557         local testfile=$DIR/$tfile
7558
7559         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7560                 skip "MDS needs to be at least 2.5.56" && return
7561
7562         touch $testfile
7563
7564         echo "setfacl as user..."
7565         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7566         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7567
7568         echo "setfattr as user..."
7569         setfacl -m "u:$RUNAS_ID:---" $testfile
7570         $RUNAS setfattr -x system.posix_acl_access $testfile
7571         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7572 }
7573 run_test 102p "check setxattr(2) correctly fails without permission"
7574
7575 test_102q() {
7576         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7577                 skip "MDS needs to be at least 2.6.92" && return
7578         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7579 }
7580 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7581
7582 test_102r() {
7583         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7584                 skip "MDS needs to be at least 2.6.93" && return
7585         touch $DIR/$tfile || error "touch"
7586         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7587         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7588         rm $DIR/$tfile || error "rm"
7589
7590         #normal directory
7591         mkdir -p $DIR/$tdir || error "mkdir"
7592         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7593         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7594         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7595                 error "$testfile error deleting user.author1"
7596         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7597                 grep "user.$(basename $tdir)" &&
7598                 error "$tdir did not delete user.$(basename $tdir)"
7599         rmdir $DIR/$tdir || error "rmdir"
7600
7601         #striped directory
7602         test_mkdir $DIR/$tdir || error "make striped dir"
7603         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7604         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7605         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7606                 error "$testfile error deleting user.author1"
7607         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7608                 grep "user.$(basename $tdir)" &&
7609                 error "$tdir did not delete user.$(basename $tdir)"
7610         rmdir $DIR/$tdir || error "rm striped dir"
7611 }
7612 run_test 102r "set EAs with empty values"
7613
7614 test_102s() {
7615         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.52) ] &&
7616                 skip "MDS needs to be at least 2.11.52"
7617
7618         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
7619
7620         save_lustre_params client "llite.*.xattr_cache" > $save
7621
7622         for cache in 0 1; do
7623                 lctl set_param llite.*.xattr_cache=$cache
7624
7625                 rm -f $DIR/$tfile
7626                 touch $DIR/$tfile || error "touch"
7627                 for prefix in lustre security system trusted user; do
7628                         # Note getxattr() may fail with 'Operation not
7629                         # supported' or 'No such attribute' depending
7630                         # on prefix and cache.
7631                         getfattr -n $prefix.n102s $DIR/$tfile &&
7632                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
7633                 done
7634         done
7635
7636         restore_lustre_params < $save
7637 }
7638 run_test 102s "getting nonexistent xattrs should fail"
7639
7640 run_acl_subtest()
7641 {
7642     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7643     return $?
7644 }
7645
7646 test_103a() {
7647         [ "$UID" != 0 ] && skip_env "must run as root" && return
7648         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7649                 skip "must have acl enabled" && return
7650         [ -z "$(which setfacl 2>/dev/null)" ] &&
7651                 skip_env "could not find setfacl" && return
7652         $GSS && skip "could not run under gss" && return
7653         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7654
7655         gpasswd -a daemon bin                           # LU-5641
7656         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7657
7658         declare -a identity_old
7659
7660         for num in $(seq $MDSCOUNT); do
7661                 switch_identity $num true || identity_old[$num]=$?
7662         done
7663
7664         SAVE_UMASK=$(umask)
7665         umask 0022
7666         mkdir -p $DIR/$tdir
7667         cd $DIR/$tdir
7668
7669         echo "performing cp ..."
7670         run_acl_subtest cp || error "run_acl_subtest cp failed"
7671         echo "performing getfacl-noacl..."
7672         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7673         echo "performing misc..."
7674         run_acl_subtest misc || error  "misc test failed"
7675         echo "performing permissions..."
7676         run_acl_subtest permissions || error "permissions failed"
7677         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7678         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7679              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7680              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7681         then
7682                 echo "performing permissions xattr..."
7683                 run_acl_subtest permissions_xattr ||
7684                         error "permissions_xattr failed"
7685         fi
7686         echo "performing setfacl..."
7687         run_acl_subtest setfacl || error  "setfacl test failed"
7688
7689         # inheritance test got from HP
7690         echo "performing inheritance..."
7691         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7692         chmod +x make-tree || error "chmod +x failed"
7693         run_acl_subtest inheritance || error "inheritance test failed"
7694         rm -f make-tree
7695
7696         echo "LU-974 ignore umask when acl is enabled..."
7697         run_acl_subtest 974 || error "LU-974 umask test failed"
7698         if [ $MDSCOUNT -ge 2 ]; then
7699                 run_acl_subtest 974_remote ||
7700                         error "LU-974 umask test failed under remote dir"
7701         fi
7702
7703         echo "LU-2561 newly created file is same size as directory..."
7704         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7705                 run_acl_subtest 2561 || error "LU-2561 test failed"
7706         else
7707                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7708         fi
7709
7710         run_acl_subtest 4924 || error "LU-4924 test failed"
7711
7712         cd $SAVE_PWD
7713         umask $SAVE_UMASK
7714
7715         for num in $(seq $MDSCOUNT); do
7716                 if [ "${identity_old[$num]}" = 1 ]; then
7717                         switch_identity $num false || identity_old[$num]=$?
7718                 fi
7719         done
7720 }
7721 run_test 103a "acl test ========================================="
7722
7723 test_103b() {
7724         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7725         local noacl=false
7726         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7727         local mountopts=$MDS_MOUNT_OPTS
7728
7729         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7730                 noacl=true
7731         else
7732                 # stop the MDT
7733                 stop $SINGLEMDS || error "failed to stop MDT."
7734                 # remount the MDT
7735                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7736                         MDS_MOUNT_OPTS="-o noacl"
7737                 else
7738                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7739                 fi
7740                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7741                         error "failed to start MDT."
7742                 MDS_MOUNT_OPTS=$mountopts
7743         fi
7744
7745         touch $DIR/$tfile
7746         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7747
7748         if ! $noacl; then
7749                 # stop the MDT
7750                 stop $SINGLEMDS || error "failed to stop MDT."
7751                 # remount the MDT
7752                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7753                         error "failed to start MDT."
7754         fi
7755
7756         true
7757 }
7758 run_test 103b "MDS mount option 'noacl'"
7759
7760 test_103c() {
7761         mkdir -p $DIR/$tdir
7762         cp -rp $DIR/$tdir $DIR/$tdir.bak
7763
7764         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7765                 error "$DIR/$tdir shouldn't contain default ACL"
7766         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7767                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7768         true
7769 }
7770 run_test 103c "'cp -rp' won't set empty acl"
7771
7772 test_104a() {
7773         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7774         touch $DIR/$tfile
7775         lfs df || error "lfs df failed"
7776         lfs df -ih || error "lfs df -ih failed"
7777         lfs df -h $DIR || error "lfs df -h $DIR failed"
7778         lfs df -i $DIR || error "lfs df -i $DIR failed"
7779         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7780         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7781
7782         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7783         lctl --device %$OSC deactivate
7784         lfs df || error "lfs df with deactivated OSC failed"
7785         lctl --device %$OSC activate
7786         # wait the osc back to normal
7787         wait_osc_import_state client ost FULL
7788
7789         lfs df || error "lfs df with reactivated OSC failed"
7790         rm -f $DIR/$tfile
7791 }
7792 run_test 104a "lfs df [-ih] [path] test ========================="
7793
7794 test_104b() {
7795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7796         [ $RUNAS_ID -eq $UID ] &&
7797                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7798         chmod 666 /dev/obd
7799         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7800                         grep "Permission denied" | wc -l)))
7801         if [ $denied_cnt -ne 0 ]; then
7802                 error "lfs check servers test failed"
7803         fi
7804 }
7805 run_test 104b "$RUNAS lfs check servers test ===================="
7806
7807 test_105a() {
7808         # doesn't work on 2.4 kernels
7809         touch $DIR/$tfile
7810         if $(flock_is_enabled); then
7811                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7812         else
7813                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7814         fi
7815         rm -f $DIR/$tfile
7816 }
7817 run_test 105a "flock when mounted without -o flock test ========"
7818
7819 test_105b() {
7820         touch $DIR/$tfile
7821         if $(flock_is_enabled); then
7822                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7823         else
7824                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7825         fi
7826         rm -f $DIR/$tfile
7827 }
7828 run_test 105b "fcntl when mounted without -o flock test ========"
7829
7830 test_105c() {
7831         touch $DIR/$tfile
7832         if $(flock_is_enabled); then
7833                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7834         else
7835                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7836         fi
7837         rm -f $DIR/$tfile
7838 }
7839 run_test 105c "lockf when mounted without -o flock test ========"
7840
7841 test_105d() { # bug 15924
7842         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7843         test_mkdir -p $DIR/$tdir
7844         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7845         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7846         $LCTL set_param fail_loc=0x80000315
7847         flocks_test 2 $DIR/$tdir
7848 }
7849 run_test 105d "flock race (should not freeze) ========"
7850
7851 test_105e() { # bug 22660 && 22040
7852         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7853         touch $DIR/$tfile
7854         flocks_test 3 $DIR/$tfile
7855 }
7856 run_test 105e "Two conflicting flocks from same process ======="
7857
7858 test_106() { #bug 10921
7859         test_mkdir -p $DIR/$tdir
7860         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7861         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7862 }
7863 run_test 106 "attempt exec of dir followed by chown of that dir"
7864
7865 test_107() {
7866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7867         CDIR=`pwd`
7868         cd $DIR
7869
7870         local file=core
7871         rm -f $file
7872
7873         local save_pattern=$(sysctl -n kernel.core_pattern)
7874         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7875         sysctl -w kernel.core_pattern=$file
7876         sysctl -w kernel.core_uses_pid=0
7877
7878         ulimit -c unlimited
7879         sleep 60 &
7880         SLEEPPID=$!
7881
7882         sleep 1
7883
7884         kill -s 11 $SLEEPPID
7885         wait $SLEEPPID
7886         if [ -e $file ]; then
7887                 size=`stat -c%s $file`
7888                 [ $size -eq 0 ] && error "Fail to create core file $file"
7889         else
7890                 error "Fail to create core file $file"
7891         fi
7892         rm -f $file
7893         sysctl -w kernel.core_pattern=$save_pattern
7894         sysctl -w kernel.core_uses_pid=$save_uses_pid
7895         cd $CDIR
7896 }
7897 run_test 107 "Coredump on SIG"
7898
7899 test_110() {
7900         test_mkdir -p $DIR/$tdir
7901         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7902                 error "mkdir with 255 char failed"
7903         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7904                 error "mkdir with 256 char should fail, but did not"
7905         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7906                 error "create with 255 char failed"
7907         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7908                 error "create with 256 char should fail, but did not"
7909
7910         ls -l $DIR/$tdir
7911         rm -rf $DIR/$tdir
7912 }
7913 run_test 110 "filename length checking"
7914
7915 #
7916 # Purpose: To verify dynamic thread (OSS) creation.
7917 #
7918 test_115() {
7919         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7920         remote_ost_nodsh && skip "remote OST with nodsh" && return
7921
7922         # Lustre does not stop service threads once they are started.
7923         # Reset number of running threads to default.
7924         stopall
7925         setupall
7926
7927         local OSTIO_pre
7928         local save_params="$TMP/sanity-$TESTNAME.parameters"
7929
7930         # Get ll_ost_io count before I/O
7931         OSTIO_pre=$(do_facet ost1 \
7932                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
7933         # Exit if lustre is not running (ll_ost_io not running).
7934         [ -z "$OSTIO_pre" ] && error "no OSS threads"
7935
7936         echo "Starting with $OSTIO_pre threads"
7937         local thread_max=$((OSTIO_pre * 2))
7938         local rpc_in_flight=$((thread_max * 2))
7939         # Number of I/O Process proposed to be started.
7940         local nfiles
7941         local facets=$(get_facets OST)
7942
7943         save_lustre_params client \
7944                 "osc.*OST*.max_rpcs_in_flight" > $save_params
7945         save_lustre_params $facets \
7946                 "ost.OSS.ost_io.threads_max" >> $save_params
7947
7948         # Set in_flight to $rpc_in_flight
7949         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
7950                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
7951         nfiles=${rpc_in_flight}
7952         # Set ost thread_max to $thread_max
7953         do_facet ost1 \
7954                 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
7955
7956         # 5 Minutes should be sufficient for max number of OSS
7957         # threads(thread_max) to be created.
7958         local timeout=300
7959
7960         # Start I/O.
7961         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
7962         mkdir -p $DIR/$tdir
7963         for i in $(seq $nfiles); do
7964                 local file=$DIR/$tdir/${tfile}-$i
7965                 $LFS setstripe -c -1 -i 0 $file
7966                 ($WTL $file $timeout)&
7967         done
7968
7969         # I/O Started - Wait for thread_started to reach thread_max or report
7970         # error if thread_started is more than thread_max.
7971         echo "Waiting for thread_started to reach thread_max"
7972         local thread_started=0
7973         local end_time=$((SECONDS + timeout))
7974
7975         while [ $SECONDS -le $end_time ] ; do
7976                 echo -n "."
7977                 # Get ost i/o thread_started count.
7978                 thread_started=$(do_facet ost1 \
7979                         "$LCTL get_param \
7980                         ost.OSS.ost_io.threads_started | cut -d= -f2")
7981                 # Break out if thread_started is equal/greater than thread_max
7982                 if [[ $thread_started -ge $thread_max ]]; then
7983                         echo ll_ost_io thread_started $thread_started, \
7984                                 equal/greater than thread_max $thread_max
7985                         break
7986                 fi
7987                 sleep 1
7988         done
7989
7990         # Cleanup - We have the numbers, Kill i/o jobs if running.
7991         jobcount=($(jobs -p))
7992         for i in $(seq 0 $((${#jobcount[@]}-1)))
7993         do
7994                 kill -9 ${jobcount[$i]}
7995                 if [ $? -ne 0 ] ; then
7996                         echo Warning: \
7997                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
7998                 fi
7999         done
8000
8001         # Cleanup files left by WTL binary.
8002         for i in $(seq $nfiles); do
8003                 local file=$DIR/$tdir/${tfile}-$i
8004                 rm -rf $file
8005                 if [ $? -ne 0 ] ; then
8006                         echo "Warning: Failed to delete file $file"
8007                 fi
8008         done
8009
8010         restore_lustre_params <$save_params
8011         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
8012
8013         # Error out if no new thread has started or Thread started is greater
8014         # than thread max.
8015         if [[ $thread_started -le $OSTIO_pre ||
8016                         $thread_started -gt $thread_max ]]; then
8017                 error "ll_ost_io: thread_started $thread_started" \
8018                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
8019                       "No new thread started or thread started greater " \
8020                       "than thread_max."
8021         fi
8022 }
8023 run_test 115 "verify dynamic thread creation===================="
8024
8025 free_min_max () {
8026         wait_delete_completed
8027         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8028         echo "OST kbytes available: ${AVAIL[@]}"
8029         MAXV=${AVAIL[0]}
8030         MAXI=0
8031         MINV=${AVAIL[0]}
8032         MINI=0
8033         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8034                 #echo OST $i: ${AVAIL[i]}kb
8035                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8036                         MAXV=${AVAIL[i]}
8037                         MAXI=$i
8038                 fi
8039                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8040                         MINV=${AVAIL[i]}
8041                         MINI=$i
8042                 fi
8043         done
8044         echo "Min free space: OST $MINI: $MINV"
8045         echo "Max free space: OST $MAXI: $MAXV"
8046 }
8047
8048 test_116a() { # was previously test_116()
8049         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8050         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8051
8052         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8053
8054         echo -n "Free space priority "
8055         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
8056                 head -n1
8057         declare -a AVAIL
8058         free_min_max
8059
8060         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
8061         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
8062                 && return
8063         trap simple_cleanup_common EXIT
8064
8065
8066         # Check if we need to generate uneven OSTs
8067         test_mkdir -p $DIR/$tdir/OST${MINI}
8068         local FILL=$((MINV / 4))
8069         local DIFF=$((MAXV - MINV))
8070         local DIFF2=$((DIFF * 100 / MINV))
8071
8072         local threshold=$(do_facet $SINGLEMDS \
8073                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
8074         threshold=${threshold%%%}
8075         echo -n "Check for uneven OSTs: "
8076         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
8077
8078         if [[ $DIFF2 -gt $threshold ]]; then
8079                 echo "ok"
8080                 echo "Don't need to fill OST$MINI"
8081         else
8082                 # generate uneven OSTs. Write 2% over the QOS threshold value
8083                 echo "no"
8084                 DIFF=$((threshold - DIFF2 + 2))
8085                 DIFF2=$((MINV * DIFF / 100))
8086                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
8087                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
8088                         error "setstripe failed"
8089                 DIFF=$((DIFF2 / 2048))
8090                 i=0
8091                 while [ $i -lt $DIFF ]; do
8092                         i=$((i + 1))
8093                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
8094                                 bs=2M count=1 2>/dev/null
8095                         echo -n .
8096                 done
8097                 echo .
8098                 sync
8099                 sleep_maxage
8100                 free_min_max
8101         fi
8102
8103         DIFF=$((MAXV - MINV))
8104         DIFF2=$((DIFF * 100 / MINV))
8105         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8106         if [ $DIFF2 -gt $threshold ]; then
8107                 echo "ok"
8108         else
8109                 echo "failed - QOS mode won't be used"
8110                 skip "QOS imbalance criteria not met"
8111                 simple_cleanup_common
8112                 return
8113         fi
8114
8115         MINI1=$MINI
8116         MINV1=$MINV
8117         MAXI1=$MAXI
8118         MAXV1=$MAXV
8119
8120         # now fill using QOS
8121         $SETSTRIPE -c 1 $DIR/$tdir
8122         FILL=$((FILL / 200))
8123         if [ $FILL -gt 600 ]; then
8124                 FILL=600
8125         fi
8126         echo "writing $FILL files to QOS-assigned OSTs"
8127         i=0
8128         while [ $i -lt $FILL ]; do
8129                 i=$((i + 1))
8130                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8131                         count=1 2>/dev/null
8132                 echo -n .
8133         done
8134         echo "wrote $i 200k files"
8135         sync
8136         sleep_maxage
8137
8138         echo "Note: free space may not be updated, so measurements might be off"
8139         free_min_max
8140         DIFF2=$((MAXV - MINV))
8141         echo "free space delta: orig $DIFF final $DIFF2"
8142         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8143         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8144         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8145         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8146         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8147         if [[ $DIFF -gt 0 ]]; then
8148                 FILL=$((DIFF2 * 100 / DIFF - 100))
8149                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8150         fi
8151
8152         # Figure out which files were written where
8153         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8154                awk '/'$MINI1': / {print $2; exit}')
8155         echo $UUID
8156         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8157         echo "$MINC files created on smaller OST $MINI1"
8158         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8159                awk '/'$MAXI1': / {print $2; exit}')
8160         echo $UUID
8161         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8162         echo "$MAXC files created on larger OST $MAXI1"
8163         if [[ $MINC -gt 0 ]]; then
8164                 FILL=$((MAXC * 100 / MINC - 100))
8165                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8166         fi
8167         [[ $MAXC -gt $MINC ]] ||
8168                 error_ignore LU-9 "stripe QOS didn't balance free space"
8169         simple_cleanup_common
8170 }
8171 run_test 116a "stripe QOS: free space balance ==================="
8172
8173 test_116b() { # LU-2093
8174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8175         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8176
8177 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8178         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8179                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8180         [ -z "$old_rr" ] && skip "no QOS" && return 0
8181         do_facet $SINGLEMDS lctl set_param \
8182                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8183         mkdir -p $DIR/$tdir
8184         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8185         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8186         do_facet $SINGLEMDS lctl set_param fail_loc=0
8187         rm -rf $DIR/$tdir
8188         do_facet $SINGLEMDS lctl set_param \
8189                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8190 }
8191 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8192
8193 test_117() # bug 10891
8194 {
8195         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8196         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8197         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8198         lctl set_param fail_loc=0x21e
8199         > $DIR/$tfile || error "truncate failed"
8200         lctl set_param fail_loc=0
8201         echo "Truncate succeeded."
8202         rm -f $DIR/$tfile
8203 }
8204 run_test 117 "verify osd extend =========="
8205
8206 NO_SLOW_RESENDCOUNT=4
8207 export OLD_RESENDCOUNT=""
8208 set_resend_count () {
8209         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8210         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8211         lctl set_param -n $PROC_RESENDCOUNT $1
8212         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8213 }
8214
8215 # for reduce test_118* time (b=14842)
8216 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8217
8218 # Reset async IO behavior after error case
8219 reset_async() {
8220         FILE=$DIR/reset_async
8221
8222         # Ensure all OSCs are cleared
8223         $SETSTRIPE -c -1 $FILE
8224         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8225         sync
8226         rm $FILE
8227 }
8228
8229 test_118a() #bug 11710
8230 {
8231         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8232         reset_async
8233
8234         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8235         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8236         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8237
8238         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8239                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8240                 return 1;
8241         fi
8242         rm -f $DIR/$tfile
8243 }
8244 run_test 118a "verify O_SYNC works =========="
8245
8246 test_118b()
8247 {
8248         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8249         remote_ost_nodsh && skip "remote OST with nodsh" && return
8250
8251         reset_async
8252
8253         #define OBD_FAIL_SRV_ENOENT 0x217
8254         set_nodes_failloc "$(osts_nodes)" 0x217
8255         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8256         RC=$?
8257         set_nodes_failloc "$(osts_nodes)" 0
8258         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8259         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8260                     grep -c writeback)
8261
8262         if [[ $RC -eq 0 ]]; then
8263                 error "Must return error due to dropped pages, rc=$RC"
8264                 return 1;
8265         fi
8266
8267         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8268                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8269                 return 1;
8270         fi
8271
8272         echo "Dirty pages not leaked on ENOENT"
8273
8274         # Due to the above error the OSC will issue all RPCs syncronously
8275         # until a subsequent RPC completes successfully without error.
8276         $MULTIOP $DIR/$tfile Ow4096yc
8277         rm -f $DIR/$tfile
8278
8279         return 0
8280 }
8281 run_test 118b "Reclaim dirty pages on fatal error =========="
8282
8283 test_118c()
8284 {
8285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8286
8287         # for 118c, restore the original resend count, LU-1940
8288         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
8289                                 set_resend_count $OLD_RESENDCOUNT
8290         remote_ost_nodsh && skip "remote OST with nodsh" && return
8291
8292         reset_async
8293
8294         #define OBD_FAIL_OST_EROFS               0x216
8295         set_nodes_failloc "$(osts_nodes)" 0x216
8296
8297         # multiop should block due to fsync until pages are written
8298         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8299         MULTIPID=$!
8300         sleep 1
8301
8302         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8303                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8304         fi
8305
8306         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8307                     grep -c writeback)
8308         if [[ $WRITEBACK -eq 0 ]]; then
8309                 error "No page in writeback, writeback=$WRITEBACK"
8310         fi
8311
8312         set_nodes_failloc "$(osts_nodes)" 0
8313         wait $MULTIPID
8314         RC=$?
8315         if [[ $RC -ne 0 ]]; then
8316                 error "Multiop fsync failed, rc=$RC"
8317         fi
8318
8319         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8320         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8321                     grep -c writeback)
8322         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8323                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8324         fi
8325
8326         rm -f $DIR/$tfile
8327         echo "Dirty pages flushed via fsync on EROFS"
8328         return 0
8329 }
8330 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
8331
8332 # continue to use small resend count to reduce test_118* time (b=14842)
8333 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8334
8335 test_118d()
8336 {
8337         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8338         remote_ost_nodsh && skip "remote OST with nodsh" && return
8339
8340         reset_async
8341
8342         #define OBD_FAIL_OST_BRW_PAUSE_BULK
8343         set_nodes_failloc "$(osts_nodes)" 0x214
8344         # multiop should block due to fsync until pages are written
8345         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8346         MULTIPID=$!
8347         sleep 1
8348
8349         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8350                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8351         fi
8352
8353         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8354                     grep -c writeback)
8355         if [[ $WRITEBACK -eq 0 ]]; then
8356                 error "No page in writeback, writeback=$WRITEBACK"
8357         fi
8358
8359         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
8360         set_nodes_failloc "$(osts_nodes)" 0
8361
8362         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8363         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8364                     grep -c writeback)
8365         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8366                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8367         fi
8368
8369         rm -f $DIR/$tfile
8370         echo "Dirty pages gaurenteed flushed via fsync"
8371         return 0
8372 }
8373 run_test 118d "Fsync validation inject a delay of the bulk =========="
8374
8375 test_118f() {
8376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8377         reset_async
8378
8379         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
8380         lctl set_param fail_loc=0x8000040a
8381
8382         # Should simulate EINVAL error which is fatal
8383         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8384         RC=$?
8385         if [[ $RC -eq 0 ]]; then
8386                 error "Must return error due to dropped pages, rc=$RC"
8387         fi
8388
8389         lctl set_param fail_loc=0x0
8390
8391         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8392         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8393         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8394                     grep -c writeback)
8395         if [[ $LOCKED -ne 0 ]]; then
8396                 error "Locked pages remain in cache, locked=$LOCKED"
8397         fi
8398
8399         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8400                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8401         fi
8402
8403         rm -f $DIR/$tfile
8404         echo "No pages locked after fsync"
8405
8406         reset_async
8407         return 0
8408 }
8409 run_test 118f "Simulate unrecoverable OSC side error =========="
8410
8411 test_118g() {
8412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8413         reset_async
8414
8415         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8416         lctl set_param fail_loc=0x406
8417
8418         # simulate local -ENOMEM
8419         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8420         RC=$?
8421
8422         lctl set_param fail_loc=0
8423         if [[ $RC -eq 0 ]]; then
8424                 error "Must return error due to dropped pages, rc=$RC"
8425         fi
8426
8427         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8428         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8429         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8430                         grep -c writeback)
8431         if [[ $LOCKED -ne 0 ]]; then
8432                 error "Locked pages remain in cache, locked=$LOCKED"
8433         fi
8434
8435         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8436                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8437         fi
8438
8439         rm -f $DIR/$tfile
8440         echo "No pages locked after fsync"
8441
8442         reset_async
8443         return 0
8444 }
8445 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8446
8447 test_118h() {
8448         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8449         remote_ost_nodsh && skip "remote OST with nodsh" && return
8450
8451         reset_async
8452
8453         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8454         set_nodes_failloc "$(osts_nodes)" 0x20e
8455         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8456         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8457         RC=$?
8458
8459         set_nodes_failloc "$(osts_nodes)" 0
8460         if [[ $RC -eq 0 ]]; then
8461                 error "Must return error due to dropped pages, rc=$RC"
8462         fi
8463
8464         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8465         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8466         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8467                     grep -c writeback)
8468         if [[ $LOCKED -ne 0 ]]; then
8469                 error "Locked pages remain in cache, locked=$LOCKED"
8470         fi
8471
8472         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8473                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8474         fi
8475
8476         rm -f $DIR/$tfile
8477         echo "No pages locked after fsync"
8478
8479         return 0
8480 }
8481 run_test 118h "Verify timeout in handling recoverables errors  =========="
8482
8483 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8484
8485 test_118i() {
8486         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8487         remote_ost_nodsh && skip "remote OST with nodsh" && return
8488
8489         reset_async
8490
8491         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8492         set_nodes_failloc "$(osts_nodes)" 0x20e
8493
8494         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8495         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8496         PID=$!
8497         sleep 5
8498         set_nodes_failloc "$(osts_nodes)" 0
8499
8500         wait $PID
8501         RC=$?
8502         if [[ $RC -ne 0 ]]; then
8503                 error "got error, but should be not, rc=$RC"
8504         fi
8505
8506         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8507         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8508         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8509         if [[ $LOCKED -ne 0 ]]; then
8510                 error "Locked pages remain in cache, locked=$LOCKED"
8511         fi
8512
8513         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8514                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8515         fi
8516
8517         rm -f $DIR/$tfile
8518         echo "No pages locked after fsync"
8519
8520         return 0
8521 }
8522 run_test 118i "Fix error before timeout in recoverable error  =========="
8523
8524 [ "$SLOW" = "no" ] && set_resend_count 4
8525
8526 test_118j() {
8527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8528         remote_ost_nodsh && skip "remote OST with nodsh" && return
8529
8530         reset_async
8531
8532         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8533         set_nodes_failloc "$(osts_nodes)" 0x220
8534
8535         # return -EIO from OST
8536         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8537         RC=$?
8538         set_nodes_failloc "$(osts_nodes)" 0x0
8539         if [[ $RC -eq 0 ]]; then
8540                 error "Must return error due to dropped pages, rc=$RC"
8541         fi
8542
8543         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8544         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8545         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8546         if [[ $LOCKED -ne 0 ]]; then
8547                 error "Locked pages remain in cache, locked=$LOCKED"
8548         fi
8549
8550         # in recoverable error on OST we want resend and stay until it finished
8551         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8552                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8553         fi
8554
8555         rm -f $DIR/$tfile
8556         echo "No pages locked after fsync"
8557
8558         return 0
8559 }
8560 run_test 118j "Simulate unrecoverable OST side error =========="
8561
8562 test_118k()
8563 {
8564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8565         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8566
8567         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8568         set_nodes_failloc "$(osts_nodes)" 0x20e
8569         test_mkdir -p $DIR/$tdir
8570
8571         for ((i=0;i<10;i++)); do
8572                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8573                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8574                 SLEEPPID=$!
8575                 sleep 0.500s
8576                 kill $SLEEPPID
8577                 wait $SLEEPPID
8578         done
8579
8580         set_nodes_failloc "$(osts_nodes)" 0
8581         rm -rf $DIR/$tdir
8582 }
8583 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8584
8585 test_118l()
8586 {
8587         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8588         # LU-646
8589         test_mkdir -p $DIR/$tdir
8590         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8591         rm -rf $DIR/$tdir
8592 }
8593 run_test 118l "fsync dir ========="
8594
8595 test_118m() # LU-3066
8596 {
8597         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8598         test_mkdir -p $DIR/$tdir
8599         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8600         rm -rf $DIR/$tdir
8601 }
8602 run_test 118m "fdatasync dir ========="
8603
8604 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8605
8606 test_119a() # bug 11737
8607 {
8608         BSIZE=$((512 * 1024))
8609         directio write $DIR/$tfile 0 1 $BSIZE
8610         # We ask to read two blocks, which is more than a file size.
8611         # directio will indicate an error when requested and actual
8612         # sizes aren't equeal (a normal situation in this case) and
8613         # print actual read amount.
8614         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8615         if [ "$NOB" != "$BSIZE" ]; then
8616                 error "read $NOB bytes instead of $BSIZE"
8617         fi
8618         rm -f $DIR/$tfile
8619 }
8620 run_test 119a "Short directIO read must return actual read amount"
8621
8622 test_119b() # bug 11737
8623 {
8624         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8625
8626         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8627         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8628         sync
8629         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8630                 error "direct read failed"
8631         rm -f $DIR/$tfile
8632 }
8633 run_test 119b "Sparse directIO read must return actual read amount"
8634
8635 test_119c() # bug 13099
8636 {
8637         BSIZE=1048576
8638         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8639         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8640         rm -f $DIR/$tfile
8641 }
8642 run_test 119c "Testing for direct read hitting hole"
8643
8644 test_119d() # bug 15950
8645 {
8646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8647         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8648         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8649         BSIZE=1048576
8650         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8651         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8652         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8653         lctl set_param fail_loc=0x40d
8654         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8655         pid_dio=$!
8656         sleep 1
8657         cat $DIR/$tfile > /dev/null &
8658         lctl set_param fail_loc=0
8659         pid_reads=$!
8660         wait $pid_dio
8661         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8662         sleep 2
8663         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8664         error "the read rpcs have not completed in 2s"
8665         rm -f $DIR/$tfile
8666         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8667 }
8668 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8669
8670 test_120a() {
8671         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8672         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8673         test_mkdir -p $DIR/$tdir
8674         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8675                skip "no early lock cancel on server" && return 0
8676
8677         lru_resize_disable mdc
8678         lru_resize_disable osc
8679         cancel_lru_locks mdc
8680         # asynchronous object destroy at MDT could cause bl ast to client
8681         cancel_lru_locks osc
8682
8683         stat $DIR/$tdir > /dev/null
8684         can1=$(do_facet $SINGLEMDS \
8685                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8686                awk '/ldlm_cancel/ {print $2}')
8687         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8688                awk '/ldlm_bl_callback/ {print $2}')
8689         test_mkdir -c1 $DIR/$tdir/d1
8690         can2=$(do_facet $SINGLEMDS \
8691                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8692                awk '/ldlm_cancel/ {print $2}')
8693         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8694                awk '/ldlm_bl_callback/ {print $2}')
8695         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8696         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8697         lru_resize_enable mdc
8698         lru_resize_enable osc
8699 }
8700 run_test 120a "Early Lock Cancel: mkdir test"
8701
8702 test_120b() {
8703         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8704         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8705         test_mkdir $DIR/$tdir
8706         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8707                skip "no early lock cancel on server" && return 0
8708         lru_resize_disable mdc
8709         lru_resize_disable osc
8710         cancel_lru_locks mdc
8711         stat $DIR/$tdir > /dev/null
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         touch $DIR/$tdir/f1
8718         can2=$(do_facet $SINGLEMDS \
8719                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8720                awk '/ldlm_cancel/ {print $2}')
8721         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8722                awk '/ldlm_bl_callback/ {print $2}')
8723         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8724         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8725         lru_resize_enable mdc
8726         lru_resize_enable osc
8727 }
8728 run_test 120b "Early Lock Cancel: create test"
8729
8730 test_120c() {
8731         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8732         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8733         test_mkdir -c1 $DIR/$tdir
8734         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8735                skip "no early lock cancel on server" && return 0
8736         lru_resize_disable mdc
8737         lru_resize_disable osc
8738         test_mkdir -p -c1 $DIR/$tdir/d1
8739         test_mkdir -p -c1 $DIR/$tdir/d2
8740         touch $DIR/$tdir/d1/f1
8741         cancel_lru_locks mdc
8742         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8743         can1=$(do_facet $SINGLEMDS \
8744                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8745                awk '/ldlm_cancel/ {print $2}')
8746         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8747                awk '/ldlm_bl_callback/ {print $2}')
8748         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8749         can2=$(do_facet $SINGLEMDS \
8750                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8751                awk '/ldlm_cancel/ {print $2}')
8752         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8753                awk '/ldlm_bl_callback/ {print $2}')
8754         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8755         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8756         lru_resize_enable mdc
8757         lru_resize_enable osc
8758 }
8759 run_test 120c "Early Lock Cancel: link test"
8760
8761 test_120d() {
8762         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8763         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8764         test_mkdir -p -c1 $DIR/$tdir
8765         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8766                skip "no early lock cancel on server" && return 0
8767         lru_resize_disable mdc
8768         lru_resize_disable osc
8769         touch $DIR/$tdir
8770         cancel_lru_locks mdc
8771         stat $DIR/$tdir > /dev/null
8772         can1=$(do_facet $SINGLEMDS \
8773                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8774                awk '/ldlm_cancel/ {print $2}')
8775         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8776                awk '/ldlm_bl_callback/ {print $2}')
8777         chmod a+x $DIR/$tdir
8778         can2=$(do_facet $SINGLEMDS \
8779                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8780                awk '/ldlm_cancel/ {print $2}')
8781         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8782                awk '/ldlm_bl_callback/ {print $2}')
8783         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8784         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8785         lru_resize_enable mdc
8786         lru_resize_enable osc
8787 }
8788 run_test 120d "Early Lock Cancel: setattr test"
8789
8790 test_120e() {
8791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8792         ! $($LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_can) &&
8793                 skip "no early lock cancel on server" && return 0
8794         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8795         local dlmtrace_set=false
8796
8797         test_mkdir -p -c1 $DIR/$tdir
8798         lru_resize_disable mdc
8799         lru_resize_disable osc
8800         ! $LCTL get_param debug | grep -q dlmtrace &&
8801                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
8802         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8803         cancel_lru_locks mdc
8804         cancel_lru_locks osc
8805         dd if=$DIR/$tdir/f1 of=/dev/null
8806         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8807         # XXX client can not do early lock cancel of OST lock
8808         # during unlink (LU-4206), so cancel osc lock now.
8809         sleep 2
8810         cancel_lru_locks osc
8811         can1=$(do_facet $SINGLEMDS \
8812                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8813                awk '/ldlm_cancel/ {print $2}')
8814         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8815                awk '/ldlm_bl_callback/ {print $2}')
8816         unlink $DIR/$tdir/f1
8817         sleep 5
8818         can2=$(do_facet $SINGLEMDS \
8819                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8820                awk '/ldlm_cancel/ {print $2}')
8821         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8822                awk '/ldlm_bl_callback/ {print $2}')
8823         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
8824                 $LCTL dk $TMP/cancel.debug.txt
8825         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
8826                 $LCTL dk $TMP/blocking.debug.txt
8827         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
8828         lru_resize_enable mdc
8829         lru_resize_enable osc
8830 }
8831 run_test 120e "Early Lock Cancel: unlink test"
8832
8833 test_120f() {
8834         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8835         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8836                skip "no early lock cancel on server" && return 0
8837         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8838         test_mkdir -p -c1 $DIR/$tdir
8839         lru_resize_disable mdc
8840         lru_resize_disable osc
8841         test_mkdir -p -c1 $DIR/$tdir/d1
8842         test_mkdir -p -c1 $DIR/$tdir/d2
8843         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8844         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8845         cancel_lru_locks mdc
8846         cancel_lru_locks osc
8847         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8848         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8849         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8850         # XXX client can not do early lock cancel of OST lock
8851         # during rename (LU-4206), so cancel osc lock now.
8852         sleep 2
8853         cancel_lru_locks osc
8854         can1=$(do_facet $SINGLEMDS \
8855                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8856                awk '/ldlm_cancel/ {print $2}')
8857         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8858                awk '/ldlm_bl_callback/ {print $2}')
8859         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8860         sleep 5
8861         can2=$(do_facet $SINGLEMDS \
8862                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8863                awk '/ldlm_cancel/ {print $2}')
8864         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8865                awk '/ldlm_bl_callback/ {print $2}')
8866         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8867         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8868         lru_resize_enable mdc
8869         lru_resize_enable osc
8870 }
8871 run_test 120f "Early Lock Cancel: rename test"
8872
8873 test_120g() {
8874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8875         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8876                skip "no early lock cancel on server" && return 0
8877         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8878         lru_resize_disable mdc
8879         lru_resize_disable osc
8880         count=10000
8881         echo create $count files
8882         test_mkdir -p $DIR/$tdir
8883         cancel_lru_locks mdc
8884         cancel_lru_locks osc
8885         t0=`date +%s`
8886
8887         can0=$(do_facet $SINGLEMDS \
8888                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8889                awk '/ldlm_cancel/ {print $2}')
8890         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8891                awk '/ldlm_bl_callback/ {print $2}')
8892         createmany -o $DIR/$tdir/f $count
8893         sync
8894         can1=$(do_facet $SINGLEMDS \
8895                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8896                awk '/ldlm_cancel/ {print $2}')
8897         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8898                awk '/ldlm_bl_callback/ {print $2}')
8899         t1=$(date +%s)
8900         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8901         echo rm $count files
8902         rm -r $DIR/$tdir
8903         sync
8904         can2=$(do_facet $SINGLEMDS \
8905                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8906                awk '/ldlm_cancel/ {print $2}')
8907         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8908                awk '/ldlm_bl_callback/ {print $2}')
8909         t2=$(date +%s)
8910         echo total: $count removes in $((t2-t1))
8911         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8912         sleep 2
8913         # wait for commitment of removal
8914         lru_resize_enable mdc
8915         lru_resize_enable osc
8916 }
8917 run_test 120g "Early Lock Cancel: performance test"
8918
8919 test_121() { #bug #10589
8920         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8921         rm -rf $DIR/$tfile
8922         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8923 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8924         lctl set_param fail_loc=0x310
8925         cancel_lru_locks osc > /dev/null
8926         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8927         lctl set_param fail_loc=0
8928         [[ $reads -eq $writes ]] ||
8929                 error "read $reads blocks, must be $writes blocks"
8930 }
8931 run_test 121 "read cancel race ========="
8932
8933 test_123a() { # was test 123, statahead(bug 11401)
8934         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8935         SLOWOK=0
8936         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8937             log "testing on UP system. Performance may be not as good as expected."
8938                         SLOWOK=1
8939         fi
8940
8941         rm -rf $DIR/$tdir
8942         test_mkdir -p $DIR/$tdir
8943         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8944         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8945         MULT=10
8946         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8947                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8948
8949                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8950                 lctl set_param -n llite.*.statahead_max 0
8951                 lctl get_param llite.*.statahead_max
8952                 cancel_lru_locks mdc
8953                 cancel_lru_locks osc
8954                 stime=`date +%s`
8955                 time ls -l $DIR/$tdir | wc -l
8956                 etime=`date +%s`
8957                 delta=$((etime - stime))
8958                 log "ls $i files without statahead: $delta sec"
8959                 lctl set_param llite.*.statahead_max=$max
8960
8961                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8962                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8963                 cancel_lru_locks mdc
8964                 cancel_lru_locks osc
8965                 stime=`date +%s`
8966                 time ls -l $DIR/$tdir | wc -l
8967                 etime=`date +%s`
8968                 delta_sa=$((etime - stime))
8969                 log "ls $i files with statahead: $delta_sa sec"
8970                 lctl get_param -n llite.*.statahead_stats
8971                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8972
8973                 [[ $swrong -lt $ewrong ]] &&
8974                         log "statahead was stopped, maybe too many locks held!"
8975                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8976
8977                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8978                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8979                     lctl set_param -n llite.*.statahead_max 0
8980                     lctl get_param llite.*.statahead_max
8981                     cancel_lru_locks mdc
8982                     cancel_lru_locks osc
8983                     stime=`date +%s`
8984                     time ls -l $DIR/$tdir | wc -l
8985                     etime=`date +%s`
8986                     delta=$((etime - stime))
8987                     log "ls $i files again without statahead: $delta sec"
8988                     lctl set_param llite.*.statahead_max=$max
8989                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8990                         if [  $SLOWOK -eq 0 ]; then
8991                                 error "ls $i files is slower with statahead!"
8992                         else
8993                                 log "ls $i files is slower with statahead!"
8994                         fi
8995                         break
8996                     fi
8997                 fi
8998
8999                 [ $delta -gt 20 ] && break
9000                 [ $delta -gt 8 ] && MULT=$((50 / delta))
9001                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
9002         done
9003         log "ls done"
9004
9005         stime=`date +%s`
9006         rm -r $DIR/$tdir
9007         sync
9008         etime=`date +%s`
9009         delta=$((etime - stime))
9010         log "rm -r $DIR/$tdir/: $delta seconds"
9011         log "rm done"
9012         lctl get_param -n llite.*.statahead_stats
9013 }
9014 run_test 123a "verify statahead work"
9015
9016 test_123b () { # statahead(bug 15027)
9017         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9018         test_mkdir -p $DIR/$tdir
9019         createmany -o $DIR/$tdir/$tfile-%d 1000
9020
9021         cancel_lru_locks mdc
9022         cancel_lru_locks osc
9023
9024 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
9025         lctl set_param fail_loc=0x80000803
9026         ls -lR $DIR/$tdir > /dev/null
9027         log "ls done"
9028         lctl set_param fail_loc=0x0
9029         lctl get_param -n llite.*.statahead_stats
9030         rm -r $DIR/$tdir
9031         sync
9032
9033 }
9034 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
9035
9036 test_124a() {
9037         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9038         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
9039                 skip "no lru resize on server" && return 0
9040         local NR=2000
9041         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9042
9043         log "create $NR files at $DIR/$tdir"
9044         createmany -o $DIR/$tdir/f $NR ||
9045                 error "failed to create $NR files in $DIR/$tdir"
9046
9047         cancel_lru_locks mdc
9048         ls -l $DIR/$tdir > /dev/null
9049
9050         local NSDIR=""
9051         local LRU_SIZE=0
9052         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
9053                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
9054                 LRU_SIZE=$($LCTL get_param -n $PARAM)
9055                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
9056                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
9057                         log "NSDIR=$NSDIR"
9058                         log "NS=$(basename $NSDIR)"
9059                         break
9060                 fi
9061         done
9062
9063         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
9064                 skip "Not enough cached locks created!"
9065                 return 0
9066         fi
9067         log "LRU=$LRU_SIZE"
9068
9069         local SLEEP=30
9070
9071         # We know that lru resize allows one client to hold $LIMIT locks
9072         # for 10h. After that locks begin to be killed by client.
9073         local MAX_HRS=10
9074         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
9075         log "LIMIT=$LIMIT"
9076         if [ $LIMIT -lt $LRU_SIZE ]; then
9077             skip "Limit is too small $LIMIT"
9078             return 0
9079         fi
9080
9081         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
9082         # killing locks. Some time was spent for creating locks. This means
9083         # that up to the moment of sleep finish we must have killed some of
9084         # them (10-100 locks). This depends on how fast ther were created.
9085         # Many of them were touched in almost the same moment and thus will
9086         # be killed in groups.
9087         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
9088
9089         # Use $LRU_SIZE_B here to take into account real number of locks
9090         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
9091         local LRU_SIZE_B=$LRU_SIZE
9092         log "LVF=$LVF"
9093         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
9094         log "OLD_LVF=$OLD_LVF"
9095         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
9096
9097         # Let's make sure that we really have some margin. Client checks
9098         # cached locks every 10 sec.
9099         SLEEP=$((SLEEP+20))
9100         log "Sleep ${SLEEP} sec"
9101         local SEC=0
9102         while ((SEC<$SLEEP)); do
9103                 echo -n "..."
9104                 sleep 5
9105                 SEC=$((SEC+5))
9106                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9107                 echo -n "$LRU_SIZE"
9108         done
9109         echo ""
9110         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9111         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9112
9113         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9114                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9115                 unlinkmany $DIR/$tdir/f $NR
9116                 return
9117         }
9118
9119         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9120         log "unlink $NR files at $DIR/$tdir"
9121         unlinkmany $DIR/$tdir/f $NR
9122 }
9123 run_test 124a "lru resize ======================================="
9124
9125 get_max_pool_limit()
9126 {
9127         local limit=$($LCTL get_param \
9128                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9129         local max=0
9130         for l in $limit; do
9131                 if [[ $l -gt $max ]]; then
9132                         max=$l
9133                 fi
9134         done
9135         echo $max
9136 }
9137
9138 test_124b() {
9139         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9140         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
9141                 skip "no lru resize on server" && return 0
9142
9143         LIMIT=$(get_max_pool_limit)
9144
9145         NR=$(($(default_lru_size)*20))
9146         if [[ $NR -gt $LIMIT ]]; then
9147                 log "Limit lock number by $LIMIT locks"
9148                 NR=$LIMIT
9149         fi
9150
9151         IFree=$(mdsrate_inodes_available)
9152         if [ $IFree -lt $NR ]; then
9153                 log "Limit lock number by $IFree inodes"
9154                 NR=$IFree
9155         fi
9156
9157         lru_resize_disable mdc
9158         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
9159                 error "failed to create $DIR/$tdir/disable_lru_resize"
9160
9161         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9162         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9163         cancel_lru_locks mdc
9164         stime=`date +%s`
9165         PID=""
9166         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9167         PID="$PID $!"
9168         sleep 2
9169         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9170         PID="$PID $!"
9171         sleep 2
9172         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9173         PID="$PID $!"
9174         wait $PID
9175         etime=`date +%s`
9176         nolruresize_delta=$((etime-stime))
9177         log "ls -la time: $nolruresize_delta seconds"
9178         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9179         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9180
9181         lru_resize_enable mdc
9182         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
9183                 error "failed to create $DIR/$tdir/enable_lru_resize"
9184
9185         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9186         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9187         cancel_lru_locks mdc
9188         stime=`date +%s`
9189         PID=""
9190         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9191         PID="$PID $!"
9192         sleep 2
9193         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9194         PID="$PID $!"
9195         sleep 2
9196         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9197         PID="$PID $!"
9198         wait $PID
9199         etime=`date +%s`
9200         lruresize_delta=$((etime-stime))
9201         log "ls -la time: $lruresize_delta seconds"
9202         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9203
9204         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9205                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9206         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9207                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9208         else
9209                 log "lru resize performs the same with no lru resize"
9210         fi
9211         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9212 }
9213 run_test 124b "lru resize (performance test) ======================="
9214
9215 test_124c() {
9216         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9217         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
9218                 skip "no lru resize on server" && return 0
9219
9220         # cache ununsed locks on client
9221         local nr=100
9222         cancel_lru_locks mdc
9223         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9224         createmany -o $DIR/$tdir/f $nr ||
9225                 error "failed to create $nr files in $DIR/$tdir"
9226         ls -l $DIR/$tdir > /dev/null
9227
9228         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9229         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9230         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9231         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9232         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9233
9234         # set lru_max_age to 1 sec
9235         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9236         echo "sleep $((recalc_p * 2)) seconds..."
9237         sleep $((recalc_p * 2))
9238
9239         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
9240         # restore lru_max_age
9241         $LCTL set_param -n $nsdir.lru_max_age $max_age
9242         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
9243         unlinkmany $DIR/$tdir/f $nr
9244 }
9245 run_test 124c "LRUR cancel very aged locks"
9246
9247 test_125() { # 13358
9248         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
9249         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
9250         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9251         test_mkdir -p $DIR/d125 || error "mkdir failed"
9252         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
9253         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
9254         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
9255 }
9256 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
9257
9258 test_126() { # bug 12829/13455
9259         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
9260         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
9261         $GSS && skip "must run as gss disabled" && return
9262
9263         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
9264         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
9265         rm -f $DIR/$tfile
9266         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
9267 }
9268 run_test 126 "check that the fsgid provided by the client is taken into account"
9269
9270 test_127a() { # bug 15521
9271         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9272         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
9273         $LCTL set_param osc.*.stats=0
9274         FSIZE=$((2048 * 1024))
9275         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9276         cancel_lru_locks osc
9277         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
9278
9279         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
9280         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9281                 echo "got $COUNT $NAME"
9282                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
9283                 eval $NAME=$COUNT || error "Wrong proc format"
9284
9285                 case $NAME in
9286                         read_bytes|write_bytes)
9287                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
9288                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
9289                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
9290                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
9291                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
9292                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
9293                                 error "sumsquare is too small: $SUMSQ"
9294                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
9295                                 error "sumsquare is too big: $SUMSQ"
9296                         ;;
9297                         *) ;;
9298                 esac
9299         done < $DIR/${tfile}.tmp
9300
9301         #check that we actually got some stats
9302         [ "$read_bytes" ] || error "Missing read_bytes stats"
9303         [ "$write_bytes" ] || error "Missing write_bytes stats"
9304         [ "$read_bytes" != 0 ] || error "no read done"
9305         [ "$write_bytes" != 0 ] || error "no write done"
9306 }
9307 run_test 127a "verify the client stats are sane"
9308
9309 test_127b() { # bug LU-333
9310         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9311         $LCTL set_param llite.*.stats=0
9312         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
9313         # perform 2 reads and writes so MAX is different from SUM.
9314         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9315         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9316         cancel_lru_locks osc
9317         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9318         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9319
9320         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
9321         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9322                 echo "got $COUNT $NAME"
9323                 eval $NAME=$COUNT || error "Wrong proc format"
9324
9325         case $NAME in
9326                 read_bytes)
9327                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9328                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9329                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9330                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9331                         ;;
9332                 write_bytes)
9333                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9334                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9335                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9336                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9337                         ;;
9338                         *) ;;
9339                 esac
9340         done < $TMP/${tfile}.tmp
9341
9342         #check that we actually got some stats
9343         [ "$read_bytes" ] || error "Missing read_bytes stats"
9344         [ "$write_bytes" ] || error "Missing write_bytes stats"
9345         [ "$read_bytes" != 0 ] || error "no read done"
9346         [ "$write_bytes" != 0 ] || error "no write done"
9347
9348         rm -f $TMP/${tfile}.tmp
9349 }
9350 run_test 127b "verify the llite client stats are sane"
9351
9352 test_128() { # bug 15212
9353         touch $DIR/$tfile
9354         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
9355                 find $DIR/$tfile
9356                 find $DIR/$tfile
9357         EOF
9358
9359         result=$(grep error $TMP/$tfile.log)
9360         rm -f $DIR/$tfile $TMP/$tfile.log
9361         [ -z "$result" ] ||
9362                 error "consecutive find's under interactive lfs failed"
9363 }
9364 run_test 128 "interactive lfs for 2 consecutive find's"
9365
9366 set_dir_limits () {
9367         local mntdev
9368         local canondev
9369         local node
9370
9371         local ldproc=/proc/fs/ldiskfs
9372         local facets=$(get_facets MDS)
9373
9374         for facet in ${facets//,/ }; do
9375                 canondev=$(ldiskfs_canon \
9376                            *.$(convert_facet2label $facet).mntdev $facet)
9377                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
9378                         ldproc=/sys/fs/ldiskfs
9379                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
9380                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
9381         done
9382 }
9383
9384 check_mds_dmesg() {
9385         local facets=$(get_facets MDS)
9386         for facet in ${facets//,/ }; do
9387                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
9388         done
9389         return 1
9390 }
9391
9392 test_129() {
9393         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9394                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9395
9396         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9397         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9398                 skip "ldiskfs only test"
9399                 return
9400         fi
9401         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9402         local ENOSPC=28
9403         local EFBIG=27
9404         local has_warning=false
9405
9406         rm -rf $DIR/$tdir
9407         mkdir -p $DIR/$tdir
9408
9409         # block size of mds1
9410         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
9411         set_dir_limits $maxsize $maxsize
9412         local dirsize=$(stat -c%s "$DIR/$tdir")
9413         local nfiles=0
9414         while [[ $dirsize -le $maxsize ]]; do
9415                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
9416                 rc=$?
9417                 if ! $has_warning; then
9418                         check_mds_dmesg '"is approaching"' && has_warning=true
9419                 fi
9420                 # check two errors:
9421                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
9422                 # EFBIG for previous versions included in ldiskfs series
9423                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9424                         set_dir_limits 0 0
9425                         echo "return code $rc received as expected"
9426
9427                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
9428                                 error_exit "create failed w/o dir size limit"
9429
9430                         check_mds_dmesg '"has reached"' ||
9431                                 error_exit "reached message should be output"
9432
9433                         [ $has_warning = "false" ] &&
9434                                 error_exit "warning message should be output"
9435
9436                         dirsize=$(stat -c%s "$DIR/$tdir")
9437
9438                         [[ $dirsize -ge $maxsize ]] && return 0
9439                         error_exit "current dir size $dirsize, " \
9440                                    "previous limit $maxsize"
9441                 elif [ $rc -ne 0 ]; then
9442                         set_dir_limits 0 0
9443                         error_exit "return $rc received instead of expected " \
9444                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
9445                 fi
9446                 nfiles=$((nfiles + 1))
9447                 dirsize=$(stat -c%s "$DIR/$tdir")
9448         done
9449
9450         set_dir_limits 0 0
9451         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
9452 }
9453 run_test 129 "test directory size limit ========================"
9454
9455 OLDIFS="$IFS"
9456 cleanup_130() {
9457         trap 0
9458         IFS="$OLDIFS"
9459 }
9460
9461 test_130a() {
9462         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9463         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9464                 return
9465
9466         trap cleanup_130 EXIT RETURN
9467
9468         local fm_file=$DIR/$tfile
9469         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9470         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9471                 error "dd failed for $fm_file"
9472
9473         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9474         filefrag -ves $fm_file
9475         RC=$?
9476         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9477                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9478         [ $RC != 0 ] && error "filefrag $fm_file failed"
9479
9480         filefrag_op=$(filefrag -ve $fm_file |
9481                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9482         lun=$($GETSTRIPE -i $fm_file)
9483
9484         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9485         IFS=$'\n'
9486         tot_len=0
9487         for line in $filefrag_op
9488         do
9489                 frag_lun=`echo $line | cut -d: -f5`
9490                 ext_len=`echo $line | cut -d: -f4`
9491                 if (( $frag_lun != $lun )); then
9492                         cleanup_130
9493                         error "FIEMAP on 1-stripe file($fm_file) failed"
9494                         return
9495                 fi
9496                 (( tot_len += ext_len ))
9497         done
9498
9499         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9500                 cleanup_130
9501                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9502                 return
9503         fi
9504
9505         cleanup_130
9506
9507         echo "FIEMAP on single striped file succeeded"
9508 }
9509 run_test 130a "FIEMAP (1-stripe file)"
9510
9511 test_130b() {
9512         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
9513
9514         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9515         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9516                 return
9517
9518         trap cleanup_130 EXIT RETURN
9519
9520         local fm_file=$DIR/$tfile
9521         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9522                         error "setstripe on $fm_file"
9523         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9524                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9525
9526         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9527                 error "dd failed on $fm_file"
9528
9529         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9530         filefrag_op=$(filefrag -ve $fm_file |
9531                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9532
9533         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9534                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9535
9536         IFS=$'\n'
9537         tot_len=0
9538         num_luns=1
9539         for line in $filefrag_op
9540         do
9541                 frag_lun=$(echo $line | cut -d: -f5 |
9542                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9543                 ext_len=$(echo $line | cut -d: -f4)
9544                 if (( $frag_lun != $last_lun )); then
9545                         if (( tot_len != 1024 )); then
9546                                 cleanup_130
9547                                 error "FIEMAP on $fm_file failed; returned " \
9548                                 "len $tot_len for OST $last_lun instead of 1024"
9549                                 return
9550                         else
9551                                 (( num_luns += 1 ))
9552                                 tot_len=0
9553                         fi
9554                 fi
9555                 (( tot_len += ext_len ))
9556                 last_lun=$frag_lun
9557         done
9558         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9559                 cleanup_130
9560                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9561                         "luns or wrong len for OST $last_lun"
9562                 return
9563         fi
9564
9565         cleanup_130
9566
9567         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9568 }
9569 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9570
9571 test_130c() {
9572         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9573
9574         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9575         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9576                 return
9577
9578         trap cleanup_130 EXIT RETURN
9579
9580         local fm_file=$DIR/$tfile
9581         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9582         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9583                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9584
9585         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9586                         error "dd failed on $fm_file"
9587
9588         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9589         filefrag_op=$(filefrag -ve $fm_file |
9590                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9591
9592         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9593                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9594
9595         IFS=$'\n'
9596         tot_len=0
9597         num_luns=1
9598         for line in $filefrag_op
9599         do
9600                 frag_lun=$(echo $line | cut -d: -f5 |
9601                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9602                 ext_len=$(echo $line | cut -d: -f4)
9603                 if (( $frag_lun != $last_lun )); then
9604                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9605                         if (( logical != 512 )); then
9606                                 cleanup_130
9607                                 error "FIEMAP on $fm_file failed; returned " \
9608                                 "logical start for lun $logical instead of 512"
9609                                 return
9610                         fi
9611                         if (( tot_len != 512 )); then
9612                                 cleanup_130
9613                                 error "FIEMAP on $fm_file failed; returned " \
9614                                 "len $tot_len for OST $last_lun instead of 1024"
9615                                 return
9616                         else
9617                                 (( num_luns += 1 ))
9618                                 tot_len=0
9619                         fi
9620                 fi
9621                 (( tot_len += ext_len ))
9622                 last_lun=$frag_lun
9623         done
9624         if (( num_luns != 2 || tot_len != 512 )); then
9625                 cleanup_130
9626                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9627                         "luns or wrong len for OST $last_lun"
9628                 return
9629         fi
9630
9631         cleanup_130
9632
9633         echo "FIEMAP on 2-stripe file with hole succeeded"
9634 }
9635 run_test 130c "FIEMAP (2-stripe file with hole)"
9636
9637 test_130d() {
9638         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
9639
9640         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9641         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9642                 return
9643
9644         trap cleanup_130 EXIT RETURN
9645
9646         local fm_file=$DIR/$tfile
9647         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9648                         error "setstripe on $fm_file"
9649         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9650                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9651
9652         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
9653         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
9654                 error "dd failed on $fm_file"
9655
9656         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9657         filefrag_op=$(filefrag -ve $fm_file |
9658                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9659
9660         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9661                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9662
9663         IFS=$'\n'
9664         tot_len=0
9665         num_luns=1
9666         for line in $filefrag_op
9667         do
9668                 frag_lun=$(echo $line | cut -d: -f5 |
9669                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9670                 ext_len=$(echo $line | cut -d: -f4)
9671                 if (( $frag_lun != $last_lun )); then
9672                         if (( tot_len != 1024 )); then
9673                                 cleanup_130
9674                                 error "FIEMAP on $fm_file failed; returned " \
9675                                 "len $tot_len for OST $last_lun instead of 1024"
9676                                 return
9677                         else
9678                                 (( num_luns += 1 ))
9679                                 tot_len=0
9680                         fi
9681                 fi
9682                 (( tot_len += ext_len ))
9683                 last_lun=$frag_lun
9684         done
9685         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
9686                 cleanup_130
9687                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9688                         "luns or wrong len for OST $last_lun"
9689                 return
9690         fi
9691
9692         cleanup_130
9693
9694         echo "FIEMAP on N-stripe file succeeded"
9695 }
9696 run_test 130d "FIEMAP (N-stripe file)"
9697
9698 test_130e() {
9699         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9700
9701         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9702         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9703
9704         trap cleanup_130 EXIT RETURN
9705
9706         local fm_file=$DIR/$tfile
9707         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9708         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9709                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9710
9711         NUM_BLKS=512
9712         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9713         for ((i = 0; i < $NUM_BLKS; i++))
9714         do
9715                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9716         done
9717
9718         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9719         filefrag_op=$(filefrag -ve $fm_file |
9720                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9721
9722         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9723                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9724
9725         IFS=$'\n'
9726         tot_len=0
9727         num_luns=1
9728         for line in $filefrag_op
9729         do
9730                 frag_lun=$(echo $line | cut -d: -f5 |
9731                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9732                 ext_len=$(echo $line | cut -d: -f4)
9733                 if (( $frag_lun != $last_lun )); then
9734                         if (( tot_len != $EXPECTED_LEN )); then
9735                                 cleanup_130
9736                                 error "FIEMAP on $fm_file failed; returned " \
9737                                 "len $tot_len for OST $last_lun instead " \
9738                                 "of $EXPECTED_LEN"
9739                                 return
9740                         else
9741                                 (( num_luns += 1 ))
9742                                 tot_len=0
9743                         fi
9744                 fi
9745                 (( tot_len += ext_len ))
9746                 last_lun=$frag_lun
9747         done
9748         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9749                 cleanup_130
9750                 error "FIEMAP on $fm_file failed; returned wrong number " \
9751                         "of luns or wrong len for OST $last_lun"
9752                 return
9753         fi
9754
9755         cleanup_130
9756
9757         echo "FIEMAP with continuation calls succeeded"
9758 }
9759 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9760
9761 # Test for writev/readv
9762 test_131a() {
9763         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
9764                 error "writev test failed"
9765         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
9766                 error "readv failed"
9767         rm -f $DIR/$tfile
9768 }
9769 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9770
9771 test_131b() {
9772         local fsize=$((524288 + 1048576 + 1572864))
9773         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
9774                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9775                         error "append writev test failed"
9776
9777         ((fsize += 1572864 + 1048576))
9778         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
9779                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9780                         error "append writev test failed"
9781         rm -f $DIR/$tfile
9782 }
9783 run_test 131b "test append writev"
9784
9785 test_131c() {
9786         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9787         error "NOT PASS"
9788 }
9789 run_test 131c "test read/write on file w/o objects"
9790
9791 test_131d() {
9792         rwv -f $DIR/$tfile -w -n 1 1572864
9793         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9794         if [ "$NOB" != 1572864 ]; then
9795                 error "Short read filed: read $NOB bytes instead of 1572864"
9796         fi
9797         rm -f $DIR/$tfile
9798 }
9799 run_test 131d "test short read"
9800
9801 test_131e() {
9802         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9803         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9804         error "read hitting hole failed"
9805         rm -f $DIR/$tfile
9806 }
9807 run_test 131e "test read hitting hole"
9808
9809 check_stats() {
9810         local facet=$1
9811         local op=$2
9812         local want=${3:-0}
9813         local res
9814
9815         case $facet in
9816         mds*) res=$(do_facet $facet \
9817                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
9818                  ;;
9819         ost*) res=$(do_facet $facet \
9820                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
9821                  ;;
9822         *) error "Wrong facet '$facet'" ;;
9823         esac
9824         echo $res
9825         [ "$res" ] || error "The counter for $op on $facet was not incremented"
9826         # if the argument $3 is zero, it means any stat increment is ok.
9827         if [[ $want -gt 0 ]]; then
9828                 local count=$(echo $res | awk '{ print $2 }')
9829                 [[ $count -ne $want ]] &&
9830                         error "The $op counter on $facet is $count, not $want"
9831         fi
9832 }
9833
9834 test_133a() {
9835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9836         remote_ost_nodsh && skip "remote OST with nodsh" && return
9837         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9838
9839         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9840                 { skip "MDS doesn't support rename stats"; return; }
9841         local testdir=$DIR/${tdir}/stats_testdir
9842         mkdir -p $DIR/${tdir}
9843
9844         # clear stats.
9845         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9846         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9847
9848         # verify mdt stats first.
9849         mkdir ${testdir} || error "mkdir failed"
9850         check_stats $SINGLEMDS "mkdir" 1
9851         touch ${testdir}/${tfile} || error "touch failed"
9852         check_stats $SINGLEMDS "open" 1
9853         check_stats $SINGLEMDS "close" 1
9854         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
9855                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
9856                 check_stats $SINGLEMDS "mknod" 2
9857         }
9858         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
9859         check_stats $SINGLEMDS "unlink" 1
9860         rm -f ${testdir}/${tfile} || error "file remove failed"
9861         check_stats $SINGLEMDS "unlink" 2
9862
9863         # remove working dir and check mdt stats again.
9864         rmdir ${testdir} || error "rmdir failed"
9865         check_stats $SINGLEMDS "rmdir" 1
9866
9867         local testdir1=$DIR/${tdir}/stats_testdir1
9868         mkdir -p ${testdir}
9869         mkdir -p ${testdir1}
9870         touch ${testdir1}/test1
9871         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9872         check_stats $SINGLEMDS "crossdir_rename" 1
9873
9874         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9875         check_stats $SINGLEMDS "samedir_rename" 1
9876
9877         rm -rf $DIR/${tdir}
9878 }
9879 run_test 133a "Verifying MDT stats ========================================"
9880
9881 test_133b() {
9882         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9883         remote_ost_nodsh && skip "remote OST with nodsh" && return
9884         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9885         local testdir=$DIR/${tdir}/stats_testdir
9886         mkdir -p ${testdir} || error "mkdir failed"
9887         touch ${testdir}/${tfile} || error "touch failed"
9888         cancel_lru_locks mdc
9889
9890         # clear stats.
9891         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9892         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9893
9894         # extra mdt stats verification.
9895         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9896         check_stats $SINGLEMDS "setattr" 1
9897         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9898         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9899         then            # LU-1740
9900                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9901                 check_stats $SINGLEMDS "getattr" 1
9902         fi
9903         $LFS df || error "lfs failed"
9904         check_stats $SINGLEMDS "statfs" 1
9905
9906         rm -rf $DIR/${tdir}
9907 }
9908 run_test 133b "Verifying extra MDT stats =================================="
9909
9910 test_133c() {
9911         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9912         remote_ost_nodsh && skip "remote OST with nodsh" && return
9913         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9914         local testdir=$DIR/${tdir}/stats_testdir
9915         test_mkdir -p ${testdir} || error "mkdir failed"
9916
9917         # verify obdfilter stats.
9918         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9919         sync
9920         cancel_lru_locks osc
9921         wait_delete_completed
9922
9923         # clear stats.
9924         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9925         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9926
9927         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9928         sync
9929         cancel_lru_locks osc
9930         check_stats ost1 "write" 1
9931
9932         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9933         check_stats ost1 "read" 1
9934
9935         > ${testdir}/${tfile} || error "truncate failed"
9936         check_stats ost1 "punch" 1
9937
9938         rm -f ${testdir}/${tfile} || error "file remove failed"
9939         wait_delete_completed
9940         check_stats ost1 "destroy" 1
9941
9942         rm -rf $DIR/${tdir}
9943 }
9944 run_test 133c "Verifying OST stats ========================================"
9945
9946 order_2() {
9947         local value=$1
9948         local orig=$value
9949         local order=1
9950
9951         while [ $value -ge 2 ]; do
9952                 order=$((order*2))
9953                 value=$((value/2))
9954         done
9955
9956         if [ $orig -gt $order ]; then
9957                 order=$((order*2))
9958         fi
9959         echo $order
9960 }
9961
9962 size_in_KMGT() {
9963     local value=$1
9964     local size=('K' 'M' 'G' 'T');
9965     local i=0
9966     local size_string=$value
9967
9968     while [ $value -ge 1024 ]; do
9969         if [ $i -gt 3 ]; then
9970             #T is the biggest unit we get here, if that is bigger,
9971             #just return XXXT
9972             size_string=${value}T
9973             break
9974         fi
9975         value=$((value >> 10))
9976         if [ $value -lt 1024 ]; then
9977             size_string=${value}${size[$i]}
9978             break
9979         fi
9980         i=$((i + 1))
9981     done
9982
9983     echo $size_string
9984 }
9985
9986 get_rename_size() {
9987         local size=$1
9988         local context=${2:-.}
9989         local sample=$(do_facet $SINGLEMDS $LCTL \
9990                 get_param mdt.$FSNAME-MDT0000.rename_stats |
9991                 grep -A1 $context |
9992                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9993         echo $sample
9994 }
9995
9996 test_133d() {
9997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9998         remote_ost_nodsh && skip "remote OST with nodsh" && return
9999         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10000         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10001         { skip "MDS doesn't support rename stats"; return; }
10002
10003         local testdir1=$DIR/${tdir}/stats_testdir1
10004         local testdir2=$DIR/${tdir}/stats_testdir2
10005         mkdir -p $DIR/${tdir}
10006
10007         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10008
10009         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
10010         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
10011
10012         createmany -o $testdir1/test 512 || error "createmany failed"
10013
10014         # check samedir rename size
10015         mv ${testdir1}/test0 ${testdir1}/test_0
10016
10017         local testdir1_size=$(ls -l $DIR/${tdir} |
10018                 awk '/stats_testdir1/ {print $5}')
10019         local testdir2_size=$(ls -l $DIR/${tdir} |
10020                 awk '/stats_testdir2/ {print $5}')
10021
10022         testdir1_size=$(order_2 $testdir1_size)
10023         testdir2_size=$(order_2 $testdir2_size)
10024
10025         testdir1_size=$(size_in_KMGT $testdir1_size)
10026         testdir2_size=$(size_in_KMGT $testdir2_size)
10027
10028         echo "source rename dir size: ${testdir1_size}"
10029         echo "target rename dir size: ${testdir2_size}"
10030
10031         local cmd="do_facet $SINGLEMDS $LCTL "
10032         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
10033
10034         eval $cmd || error "$cmd failed"
10035         local samedir=$($cmd | grep 'same_dir')
10036         local same_sample=$(get_rename_size $testdir1_size)
10037         [ -z "$samedir" ] && error "samedir_rename_size count error"
10038         [[ $same_sample -eq 1 ]] ||
10039                 error "samedir_rename_size error $same_sample"
10040         echo "Check same dir rename stats success"
10041
10042         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10043
10044         # check crossdir rename size
10045         mv ${testdir1}/test_0 ${testdir2}/test_0
10046
10047         testdir1_size=$(ls -l $DIR/${tdir} |
10048                 awk '/stats_testdir1/ {print $5}')
10049         testdir2_size=$(ls -l $DIR/${tdir} |
10050                 awk '/stats_testdir2/ {print $5}')
10051
10052         testdir1_size=$(order_2 $testdir1_size)
10053         testdir2_size=$(order_2 $testdir2_size)
10054
10055         testdir1_size=$(size_in_KMGT $testdir1_size)
10056         testdir2_size=$(size_in_KMGT $testdir2_size)
10057
10058         echo "source rename dir size: ${testdir1_size}"
10059         echo "target rename dir size: ${testdir2_size}"
10060
10061         eval $cmd || error "$cmd failed"
10062         local crossdir=$($cmd | grep 'crossdir')
10063         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10064         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10065         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10066         [[ $src_sample -eq 1 ]] ||
10067                 error "crossdir_rename_size error $src_sample"
10068         [[ $tgt_sample -eq 1 ]] ||
10069                 error "crossdir_rename_size error $tgt_sample"
10070         echo "Check cross dir rename stats success"
10071         rm -rf $DIR/${tdir}
10072 }
10073 run_test 133d "Verifying rename_stats ========================================"
10074
10075 test_133e() {
10076         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10077         remote_ost_nodsh && skip "remote OST with nodsh" && return
10078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10079         local testdir=$DIR/${tdir}/stats_testdir
10080         local ctr f0 f1 bs=32768 count=42 sum
10081
10082         mkdir -p ${testdir} || error "mkdir failed"
10083
10084         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10085
10086         for ctr in {write,read}_bytes; do
10087                 sync
10088                 cancel_lru_locks osc
10089
10090                 do_facet ost1 $LCTL set_param -n \
10091                         "obdfilter.*.exports.clear=clear"
10092
10093                 if [ $ctr = write_bytes ]; then
10094                         f0=/dev/zero
10095                         f1=${testdir}/${tfile}
10096                 else
10097                         f0=${testdir}/${tfile}
10098                         f1=/dev/null
10099                 fi
10100
10101                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10102                         error "dd failed"
10103                 sync
10104                 cancel_lru_locks osc
10105
10106                 sum=$(do_facet ost1 $LCTL get_param \
10107                         "obdfilter.*.exports.*.stats" |
10108                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10109                                 $1 == ctr { sum += $7 }
10110                                 END { printf("%0.0f", sum) }')
10111
10112                 if ((sum != bs * count)); then
10113                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10114                 fi
10115         done
10116
10117         rm -rf $DIR/${tdir}
10118 }
10119 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10120
10121 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10122
10123 test_133f() {
10124         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10125         remote_ost_nodsh && skip "remote OST with nodsh" && return
10126         # First without trusting modes.
10127         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10128         echo "proc_dirs='$proc_dirs'"
10129         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10130         find $proc_dirs -exec cat '{}' \; &> /dev/null
10131
10132         # Second verifying readability.
10133         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10134
10135         # eventually, this can also be replaced with "lctl get_param -R",
10136         # but not until that option is always available on the server
10137         local facet
10138         for facet in mds1 ost1; do
10139                 local facet_proc_dirs=$(do_facet $facet \
10140                                         \\\ls -d $proc_regexp 2>/dev/null)
10141                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10142                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10143                 do_facet $facet find $facet_proc_dirs \
10144                         ! -name req_history \
10145                         -exec cat '{}' \\\; &> /dev/null
10146
10147                 do_facet $facet find $facet_proc_dirs \
10148                         ! -name req_history \
10149                         -type f \
10150                         -exec cat '{}' \\\; &> /dev/null ||
10151                                 error "proc file read failed"
10152         done
10153 }
10154 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
10155
10156 test_133g() {
10157         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10158         remote_ost_nodsh && skip "remote OST with nodsh" && return
10159         # Second verifying writability.
10160         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10161         echo "proc_dirs='$proc_dirs'"
10162         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10163         find $proc_dirs \
10164                 -type f \
10165                 -not -name force_lbug \
10166                 -not -name changelog_mask \
10167                 -exec badarea_io '{}' \; &> /dev/null ||
10168                 error "find $proc_dirs failed"
10169
10170         local facet
10171         for facet in mds1 ost1; do
10172                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10173                         skip "Too old lustre on $facet" && continue
10174                 local facet_proc_dirs=$(do_facet $facet \
10175                                         \\\ls -d $proc_regexp 2> /dev/null)
10176                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10177                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10178                 do_facet $facet find $facet_proc_dirs \
10179                         -type f \
10180                         -not -name force_lbug \
10181                         -not -name changelog_mask \
10182                         -exec badarea_io '{}' \\\; &> /dev/null ||
10183                                 error "$facet find $facet_proc_dirs failed"
10184         done
10185
10186         # remount the FS in case writes/reads /proc break the FS
10187         cleanup || error "failed to unmount"
10188         setup || error "failed to setup"
10189         true
10190 }
10191 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
10192
10193 test_133h() {
10194         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10195         remote_ost_nodsh && skip "remote OST with nodsh" && return
10196         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10197                 skip "Need MDS version at least 2.9.54" && return
10198
10199         local facet
10200         for facet in client mds1 ost1; do
10201                 local facet_proc_dirs=$(do_facet $facet \
10202                                         \\\ls -d $proc_regexp 2> /dev/null)
10203                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10204                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10205                 # Get the list of files that are missing the terminating newline
10206                 local missing=($(do_facet $facet \
10207                         find ${facet_proc_dirs} -type f \|              \
10208                                 while read F\; do                       \
10209                                         awk -v FS='\v' -v RS='\v\v'     \
10210                                         "'END { if(NR>0 &&              \
10211                                         \\\$NF !~ /.*\\\n\$/)           \
10212                                                 print FILENAME}'"       \
10213                                         '\$F'\;                         \
10214                                 done 2>/dev/null))
10215                 [ ${#missing[*]} -eq 0 ] ||
10216                         error "files do not end with newline: ${missing[*]}"
10217         done
10218 }
10219 run_test 133h "Proc files should end with newlines"
10220
10221 test_134a() {
10222         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10223         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10224                 skip "Need MDS version at least 2.7.54" && return
10225
10226         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10227         cancel_lru_locks mdc
10228
10229         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10230         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10231         [ $unused -eq 0 ] || error "$unused locks are not cleared"
10232
10233         local nr=1000
10234         createmany -o $DIR/$tdir/f $nr ||
10235                 error "failed to create $nr files in $DIR/$tdir"
10236         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10237
10238         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
10239         do_facet mds1 $LCTL set_param fail_loc=0x327
10240         do_facet mds1 $LCTL set_param fail_val=500
10241         touch $DIR/$tdir/m
10242
10243         echo "sleep 10 seconds ..."
10244         sleep 10
10245         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
10246
10247         do_facet mds1 $LCTL set_param fail_loc=0
10248         do_facet mds1 $LCTL set_param fail_val=0
10249         [ $lck_cnt -lt $unused ] ||
10250                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
10251
10252         rm $DIR/$tdir/m
10253         unlinkmany $DIR/$tdir/f $nr
10254 }
10255 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
10256
10257 test_134b() {
10258         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10259         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10260                 skip "Need MDS version at least 2.7.54" && return
10261
10262         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10263         cancel_lru_locks mdc
10264
10265         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10266                         ldlm.lock_reclaim_threshold_mb)
10267         # disable reclaim temporarily
10268         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10269
10270         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10271         do_facet mds1 $LCTL set_param fail_loc=0x328
10272         do_facet mds1 $LCTL set_param fail_val=500
10273
10274         $LCTL set_param debug=+trace
10275
10276         local nr=600
10277         createmany -o $DIR/$tdir/f $nr &
10278         local create_pid=$!
10279
10280         echo "Sleep $TIMEOUT seconds ..."
10281         sleep $TIMEOUT
10282         if ! ps -p $create_pid  > /dev/null 2>&1; then
10283                 do_facet mds1 $LCTL set_param fail_loc=0
10284                 do_facet mds1 $LCTL set_param fail_val=0
10285                 do_facet mds1 $LCTL set_param \
10286                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10287                 error "createmany finished incorrectly!"
10288         fi
10289         do_facet mds1 $LCTL set_param fail_loc=0
10290         do_facet mds1 $LCTL set_param fail_val=0
10291         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10292         wait $create_pid || return 1
10293
10294         unlinkmany $DIR/$tdir/f $nr
10295 }
10296 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10297
10298 test_140() { #bug-17379
10299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10300         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
10301         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10302         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10303
10304         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10305         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10306         local i=0
10307         while i=`expr $i + 1`; do
10308                 test_mkdir -p $i || error "Creating dir $i"
10309                 cd $i || error "Changing to $i"
10310                 ln -s ../stat stat || error "Creating stat symlink"
10311                 # Read the symlink until ELOOP present,
10312                 # not LBUGing the system is considered success,
10313                 # we didn't overrun the stack.
10314                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10315                 [ $ret -ne 0 ] && {
10316                         if [ $ret -eq 40 ]; then
10317                                 break  # -ELOOP
10318                         else
10319                                 error "Open stat symlink"
10320                                 return
10321                         fi
10322                 }
10323         done
10324         i=`expr $i - 1`
10325         echo "The symlink depth = $i"
10326         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10327                                         error "Invalid symlink depth"
10328
10329         # Test recursive symlink
10330         ln -s symlink_self symlink_self
10331         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10332         echo "open symlink_self returns $ret"
10333         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10334 }
10335 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10336
10337 test_150() {
10338         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10339         local TF="$TMP/$tfile"
10340
10341         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10342         cp $TF $DIR/$tfile
10343         cancel_lru_locks osc
10344         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10345         remount_client $MOUNT
10346         df -P $MOUNT
10347         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10348
10349         $TRUNCATE $TF 6000
10350         $TRUNCATE $DIR/$tfile 6000
10351         cancel_lru_locks osc
10352         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10353
10354         echo "12345" >>$TF
10355         echo "12345" >>$DIR/$tfile
10356         cancel_lru_locks osc
10357         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10358
10359         echo "12345" >>$TF
10360         echo "12345" >>$DIR/$tfile
10361         cancel_lru_locks osc
10362         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10363
10364         rm -f $TF
10365         true
10366 }
10367 run_test 150 "truncate/append tests"
10368
10369 #LU-2902 roc_hit was not able to read all values from lproc
10370 function roc_hit_init() {
10371         local list=$(comma_list $(osts_nodes))
10372         local dir=$DIR/$tdir-check
10373         local file=$dir/file
10374         local BEFORE
10375         local AFTER
10376         local idx
10377
10378         test_mkdir -p $dir
10379         #use setstripe to do a write to every ost
10380         for i in $(seq 0 $((OSTCOUNT-1))); do
10381                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10382                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10383                 idx=$(printf %04x $i)
10384                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10385                         awk '$1 == "cache_access" {sum += $7}
10386                                 END { printf("%0.0f", sum) }')
10387
10388                 cancel_lru_locks osc
10389                 cat $file >/dev/null
10390
10391                 AFTER=$(get_osd_param $list *OST*$idx stats |
10392                         awk '$1 == "cache_access" {sum += $7}
10393                                 END { printf("%0.0f", sum) }')
10394
10395                 echo BEFORE:$BEFORE AFTER:$AFTER
10396                 if ! let "AFTER - BEFORE == 4"; then
10397                         rm -rf $dir
10398                         error "roc_hit is not safe to use"
10399                 fi
10400                 rm $file
10401         done
10402
10403         rm -rf $dir
10404 }
10405
10406 function roc_hit() {
10407         local list=$(comma_list $(osts_nodes))
10408         echo $(get_osd_param $list '' stats |
10409                 awk '$1 == "cache_hit" {sum += $7}
10410                         END { printf("%0.0f", sum) }')
10411 }
10412
10413 function set_cache() {
10414         local on=1
10415
10416         if [ "$2" == "off" ]; then
10417                 on=0;
10418         fi
10419         local list=$(comma_list $(osts_nodes))
10420         set_osd_param $list '' $1_cache_enable $on
10421
10422         cancel_lru_locks osc
10423 }
10424
10425 test_151() {
10426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10427         remote_ost_nodsh && skip "remote OST with nodsh" && return
10428
10429         local CPAGES=3
10430         local list=$(comma_list $(osts_nodes))
10431
10432         # check whether obdfilter is cache capable at all
10433         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10434                 echo "not cache-capable obdfilter"
10435                 return 0
10436         fi
10437
10438         # check cache is enabled on all obdfilters
10439         if get_osd_param $list '' read_cache_enable | grep 0; then
10440                 echo "oss cache is disabled"
10441                 return 0
10442         fi
10443
10444         set_osd_param $list '' writethrough_cache_enable 1
10445
10446         # check write cache is enabled on all obdfilters
10447         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10448                 echo "oss write cache is NOT enabled"
10449                 return 0
10450         fi
10451
10452         roc_hit_init
10453
10454         #define OBD_FAIL_OBD_NO_LRU  0x609
10455         do_nodes $list $LCTL set_param fail_loc=0x609
10456
10457         # pages should be in the case right after write
10458         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10459                 error "dd failed"
10460
10461         local BEFORE=$(roc_hit)
10462         cancel_lru_locks osc
10463         cat $DIR/$tfile >/dev/null
10464         local AFTER=$(roc_hit)
10465
10466         do_nodes $list $LCTL set_param fail_loc=0
10467
10468         if ! let "AFTER - BEFORE == CPAGES"; then
10469                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10470         fi
10471
10472         # the following read invalidates the cache
10473         cancel_lru_locks osc
10474         set_osd_param $list '' read_cache_enable 0
10475         cat $DIR/$tfile >/dev/null
10476
10477         # now data shouldn't be found in the cache
10478         BEFORE=$(roc_hit)
10479         cancel_lru_locks osc
10480         cat $DIR/$tfile >/dev/null
10481         AFTER=$(roc_hit)
10482         if let "AFTER - BEFORE != 0"; then
10483                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10484         fi
10485
10486         set_osd_param $list '' read_cache_enable 1
10487         rm -f $DIR/$tfile
10488 }
10489 run_test 151 "test cache on oss and controls ==============================="
10490
10491 test_152() {
10492         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10493         local TF="$TMP/$tfile"
10494
10495         # simulate ENOMEM during write
10496 #define OBD_FAIL_OST_NOMEM      0x226
10497         lctl set_param fail_loc=0x80000226
10498         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10499         cp $TF $DIR/$tfile
10500         sync || error "sync failed"
10501         lctl set_param fail_loc=0
10502
10503         # discard client's cache
10504         cancel_lru_locks osc
10505
10506         # simulate ENOMEM during read
10507         lctl set_param fail_loc=0x80000226
10508         cmp $TF $DIR/$tfile || error "cmp failed"
10509         lctl set_param fail_loc=0
10510
10511         rm -f $TF
10512 }
10513 run_test 152 "test read/write with enomem ============================"
10514
10515 test_153() {
10516         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10517 }
10518 run_test 153 "test if fdatasync does not crash ======================="
10519
10520 dot_lustre_fid_permission_check() {
10521         local fid=$1
10522         local ffid=$MOUNT/.lustre/fid/$fid
10523         local test_dir=$2
10524
10525         echo "stat fid $fid"
10526         stat $ffid > /dev/null || error "stat $ffid failed."
10527         echo "touch fid $fid"
10528         touch $ffid || error "touch $ffid failed."
10529         echo "write to fid $fid"
10530         cat /etc/hosts > $ffid || error "write $ffid failed."
10531         echo "read fid $fid"
10532         diff /etc/hosts $ffid || error "read $ffid failed."
10533         echo "append write to fid $fid"
10534         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10535         echo "rename fid $fid"
10536         mv $ffid $test_dir/$tfile.1 &&
10537                 error "rename $ffid to $tfile.1 should fail."
10538         touch $test_dir/$tfile.1
10539         mv $test_dir/$tfile.1 $ffid &&
10540                 error "rename $tfile.1 to $ffid should fail."
10541         rm -f $test_dir/$tfile.1
10542         echo "truncate fid $fid"
10543         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10544         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10545                 echo "link fid $fid"
10546                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10547         fi
10548         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10549                 echo "setfacl fid $fid"
10550                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10551                 echo "getfacl fid $fid"
10552                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10553         fi
10554         echo "unlink fid $fid"
10555         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10556         echo "mknod fid $fid"
10557         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10558
10559         fid=[0xf00000400:0x1:0x0]
10560         ffid=$MOUNT/.lustre/fid/$fid
10561
10562         echo "stat non-exist fid $fid"
10563         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10564         echo "write to non-exist fid $fid"
10565         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10566         echo "link new fid $fid"
10567         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10568
10569         mkdir -p $test_dir/$tdir
10570         touch $test_dir/$tdir/$tfile
10571         fid=$($LFS path2fid $test_dir/$tdir)
10572         rc=$?
10573         [ $rc -ne 0 ] &&
10574                 error "error: could not get fid for $test_dir/$dir/$tfile."
10575
10576         ffid=$MOUNT/.lustre/fid/$fid
10577
10578         echo "ls $fid"
10579         ls $ffid > /dev/null || error "ls $ffid failed."
10580         echo "touch $fid/$tfile.1"
10581         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10582
10583         echo "touch $MOUNT/.lustre/fid/$tfile"
10584         touch $MOUNT/.lustre/fid/$tfile && \
10585                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10586
10587         echo "setxattr to $MOUNT/.lustre/fid"
10588         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10589
10590         echo "listxattr for $MOUNT/.lustre/fid"
10591         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10592
10593         echo "delxattr from $MOUNT/.lustre/fid"
10594         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10595
10596         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10597         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10598                 error "touch invalid fid should fail."
10599
10600         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10601         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10602                 error "touch non-normal fid should fail."
10603
10604         echo "rename $tdir to $MOUNT/.lustre/fid"
10605         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10606                 error "rename to $MOUNT/.lustre/fid should fail."
10607
10608         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10609         then            # LU-3547
10610                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10611                 local new_obf_mode=777
10612
10613                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10614                 chmod $new_obf_mode $DIR/.lustre/fid ||
10615                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10616
10617                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10618                 [ $obf_mode -eq $new_obf_mode ] ||
10619                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10620
10621                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10622                 chmod $old_obf_mode $DIR/.lustre/fid ||
10623                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10624         fi
10625
10626         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10627         fid=$($LFS path2fid $test_dir/$tfile-2)
10628
10629         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10630         then # LU-5424
10631                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10632                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10633                         error "create lov data thru .lustre failed"
10634         fi
10635         echo "cp /etc/passwd $test_dir/$tfile-2"
10636         cp /etc/passwd $test_dir/$tfile-2 ||
10637                 error "copy to $test_dir/$tfile-2 failed."
10638         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10639         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10640                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10641
10642         rm -rf $test_dir/tfile.lnk
10643         rm -rf $test_dir/$tfile-2
10644 }
10645
10646 test_154A() {
10647         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10648                 skip "Need MDS version at least 2.4.1" && return
10649
10650         local tf=$DIR/$tfile
10651         touch $tf
10652
10653         local fid=$($LFS path2fid $tf)
10654         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10655
10656         # check that we get the same pathname back
10657         local found=$($LFS fid2path $MOUNT "$fid")
10658         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10659         [ "$found" == "$tf" ] ||
10660                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10661 }
10662 run_test 154A "lfs path2fid and fid2path basic checks"
10663
10664 test_154B() {
10665         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10666                 skip "Need MDS version at least 2.4.1" && return
10667
10668         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10669         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10670         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10671         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10672
10673         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10674         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10675
10676         # check that we get the same pathname
10677         echo "PFID: $PFID, name: $name"
10678         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10679         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10680         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10681                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10682
10683         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10684 }
10685 run_test 154B "verify the ll_decode_linkea tool"
10686
10687 test_154a() {
10688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10689         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10690                 { skip "Need MDS version at least 2.2.51"; return 0; }
10691         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10692         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10693
10694         cp /etc/hosts $DIR/$tfile
10695
10696         fid=$($LFS path2fid $DIR/$tfile)
10697         rc=$?
10698         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10699
10700         dot_lustre_fid_permission_check "$fid" $DIR ||
10701                 error "dot lustre permission check $fid failed"
10702
10703         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10704
10705         touch $MOUNT/.lustre/file &&
10706                 error "creation is not allowed under .lustre"
10707
10708         mkdir $MOUNT/.lustre/dir &&
10709                 error "mkdir is not allowed under .lustre"
10710
10711         rm -rf $DIR/$tfile
10712 }
10713 run_test 154a "Open-by-FID"
10714
10715 test_154b() {
10716         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10717         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10718                 { skip "Need MDS version at least 2.2.51"; return 0; }
10719         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10720
10721         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10722
10723         local remote_dir=$DIR/$tdir/remote_dir
10724         local MDTIDX=1
10725         local rc=0
10726
10727         mkdir -p $DIR/$tdir
10728         $LFS mkdir -i $MDTIDX $remote_dir ||
10729                 error "create remote directory failed"
10730
10731         cp /etc/hosts $remote_dir/$tfile
10732
10733         fid=$($LFS path2fid $remote_dir/$tfile)
10734         rc=$?
10735         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10736
10737         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10738                 error "dot lustre permission check $fid failed"
10739         rm -rf $DIR/$tdir
10740 }
10741 run_test 154b "Open-by-FID for remote directory"
10742
10743 test_154c() {
10744         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10745                 skip "Need MDS version at least 2.4.1" && return
10746
10747         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10748         local FID1=$($LFS path2fid $DIR/$tfile.1)
10749         local FID2=$($LFS path2fid $DIR/$tfile.2)
10750         local FID3=$($LFS path2fid $DIR/$tfile.3)
10751
10752         local N=1
10753         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10754                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10755                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10756                 local want=FID$N
10757                 [ "$FID" = "${!want}" ] ||
10758                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10759                 N=$((N + 1))
10760         done
10761
10762         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
10763         do
10764                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
10765                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
10766                 N=$((N + 1))
10767         done
10768 }
10769 run_test 154c "lfs path2fid and fid2path multiple arguments"
10770
10771 test_154d() {
10772         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10773         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10774                 skip "Need MDS version at least 2.5.53" && return
10775
10776         if remote_mds; then
10777                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10778         else
10779                 nid="0@lo"
10780         fi
10781         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10782         local fd
10783         local cmd
10784
10785         rm -f $DIR/$tfile
10786         touch $DIR/$tfile
10787
10788         local fid=$($LFS path2fid $DIR/$tfile)
10789         # Open the file
10790         fd=$(free_fd)
10791         cmd="exec $fd<$DIR/$tfile"
10792         eval $cmd
10793         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10794         echo "$fid_list" | grep "$fid"
10795         rc=$?
10796
10797         cmd="exec $fd>/dev/null"
10798         eval $cmd
10799         if [ $rc -ne 0 ]; then
10800                 error "FID $fid not found in open files list $fid_list"
10801         fi
10802 }
10803 run_test 154d "Verify open file fid"
10804
10805 test_154e()
10806 {
10807         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10808                 skip "Need MDS version at least 2.6.50" && return
10809
10810         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10811                 error ".lustre returned by readdir"
10812         fi
10813 }
10814 run_test 154e ".lustre is not returned by readdir"
10815
10816 test_154f() {
10817         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10818         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10819         test_mkdir -p -c1 $DIR/$tdir/d
10820         # test dirs inherit from its stripe
10821         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10822         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10823         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10824         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10825         touch $DIR/f
10826
10827         # get fid of parents
10828         local FID0=$($LFS path2fid $DIR/$tdir/d)
10829         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10830         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10831         local FID3=$($LFS path2fid $DIR)
10832
10833         # check that path2fid --parents returns expected <parent_fid>/name
10834         # 1) test for a directory (single parent)
10835         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10836         [ "$parent" == "$FID0/foo1" ] ||
10837                 error "expected parent: $FID0/foo1, got: $parent"
10838
10839         # 2) test for a file with nlink > 1 (multiple parents)
10840         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10841         echo "$parent" | grep -F "$FID1/$tfile" ||
10842                 error "$FID1/$tfile not returned in parent list"
10843         echo "$parent" | grep -F "$FID2/link" ||
10844                 error "$FID2/link not returned in parent list"
10845
10846         # 3) get parent by fid
10847         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10848         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10849         echo "$parent" | grep -F "$FID1/$tfile" ||
10850                 error "$FID1/$tfile not returned in parent list (by fid)"
10851         echo "$parent" | grep -F "$FID2/link" ||
10852                 error "$FID2/link not returned in parent list (by fid)"
10853
10854         # 4) test for entry in root directory
10855         parent=$($LFS path2fid --parents $DIR/f)
10856         echo "$parent" | grep -F "$FID3/f" ||
10857                 error "$FID3/f not returned in parent list"
10858
10859         # 5) test it on root directory
10860         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10861                 error "$MOUNT should not have parents"
10862
10863         # enable xattr caching and check that linkea is correctly updated
10864         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10865         save_lustre_params client "llite.*.xattr_cache" > $save
10866         lctl set_param llite.*.xattr_cache 1
10867
10868         # 6.1) linkea update on rename
10869         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10870
10871         # get parents by fid
10872         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10873         # foo1 should no longer be returned in parent list
10874         echo "$parent" | grep -F "$FID1" &&
10875                 error "$FID1 should no longer be in parent list"
10876         # the new path should appear
10877         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10878                 error "$FID2/$tfile.moved is not in parent list"
10879
10880         # 6.2) linkea update on unlink
10881         rm -f $DIR/$tdir/d/foo2/link
10882         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10883         # foo2/link should no longer be returned in parent list
10884         echo "$parent" | grep -F "$FID2/link" &&
10885                 error "$FID2/link should no longer be in parent list"
10886         true
10887
10888         rm -f $DIR/f
10889         restore_lustre_params < $save
10890         rm -f $save
10891 }
10892 run_test 154f "get parent fids by reading link ea"
10893
10894 test_154g()
10895 {
10896         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
10897            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
10898                 { skip "Need MDS version at least 2.6.92"; return 0; }
10899         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10900
10901         mkdir -p $DIR/$tdir
10902         llapi_fid_test -d $DIR/$tdir
10903 }
10904 run_test 154g "various llapi FID tests"
10905
10906 test_155_small_load() {
10907     local temp=$TMP/$tfile
10908     local file=$DIR/$tfile
10909
10910     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10911         error "dd of=$temp bs=6096 count=1 failed"
10912     cp $temp $file
10913     cancel_lru_locks osc
10914     cmp $temp $file || error "$temp $file differ"
10915
10916     $TRUNCATE $temp 6000
10917     $TRUNCATE $file 6000
10918     cmp $temp $file || error "$temp $file differ (truncate1)"
10919
10920     echo "12345" >>$temp
10921     echo "12345" >>$file
10922     cmp $temp $file || error "$temp $file differ (append1)"
10923
10924     echo "12345" >>$temp
10925     echo "12345" >>$file
10926     cmp $temp $file || error "$temp $file differ (append2)"
10927
10928     rm -f $temp $file
10929     true
10930 }
10931
10932 test_155_big_load() {
10933     remote_ost_nodsh && skip "remote OST with nodsh" && return
10934     local temp=$TMP/$tfile
10935     local file=$DIR/$tfile
10936
10937     free_min_max
10938     local cache_size=$(do_facet ost$((MAXI+1)) \
10939         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10940     local large_file_size=$((cache_size * 2))
10941
10942     echo "OSS cache size: $cache_size KB"
10943     echo "Large file size: $large_file_size KB"
10944
10945     [ $MAXV -le $large_file_size ] && \
10946         skip_env "max available OST size needs > $large_file_size KB" && \
10947         return 0
10948
10949     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10950
10951     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10952         error "dd of=$temp bs=$large_file_size count=1k failed"
10953     cp $temp $file
10954     ls -lh $temp $file
10955     cancel_lru_locks osc
10956     cmp $temp $file || error "$temp $file differ"
10957
10958     rm -f $temp $file
10959     true
10960 }
10961
10962 save_writethrough() {
10963         local facets=$(get_facets OST)
10964
10965         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
10966         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
10967 }
10968
10969 test_155a() {
10970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10971         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10972         save_writethrough $p
10973
10974         set_cache read on
10975         set_cache writethrough on
10976         test_155_small_load
10977         restore_lustre_params < $p
10978         rm -f $p
10979 }
10980 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10981
10982 test_155b() {
10983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10984         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10985         save_writethrough $p
10986
10987         set_cache read on
10988         set_cache writethrough off
10989         test_155_small_load
10990         restore_lustre_params < $p
10991         rm -f $p
10992 }
10993 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10994
10995 test_155c() {
10996         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10997         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10998         save_writethrough $p
10999
11000         set_cache read off
11001         set_cache writethrough on
11002         test_155_small_load
11003         restore_lustre_params < $p
11004         rm -f $p
11005 }
11006 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
11007
11008 test_155d() {
11009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11010         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11011         save_writethrough $p
11012
11013         set_cache read off
11014         set_cache writethrough off
11015         test_155_small_load
11016         restore_lustre_params < $p
11017         rm -f $p
11018 }
11019 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
11020
11021 test_155e() {
11022         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11023         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11024         save_writethrough $p
11025
11026         set_cache read on
11027         set_cache writethrough on
11028         test_155_big_load
11029         restore_lustre_params < $p
11030         rm -f $p
11031 }
11032 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11033
11034 test_155f() {
11035         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11036         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11037         save_writethrough $p
11038
11039         set_cache read on
11040         set_cache writethrough off
11041         test_155_big_load
11042         restore_lustre_params < $p
11043         rm -f $p
11044 }
11045 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11046
11047 test_155g() {
11048         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11049         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11050         save_writethrough $p
11051
11052         set_cache read off
11053         set_cache writethrough on
11054         test_155_big_load
11055         restore_lustre_params < $p
11056         rm -f $p
11057 }
11058 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11059
11060 test_155h() {
11061         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11062         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11063         save_writethrough $p
11064
11065         set_cache read off
11066         set_cache writethrough off
11067         test_155_big_load
11068         restore_lustre_params < $p
11069         rm -f $p
11070 }
11071 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11072
11073 test_156() {
11074         remote_ost_nodsh && skip "remote OST with nodsh" && return
11075         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11076         local CPAGES=3
11077         local BEFORE
11078         local AFTER
11079         local file="$DIR/$tfile"
11080         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11081
11082         [ "$(facet_fstype ost1)" = "zfs" -a \
11083            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11084                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
11085                 return
11086
11087         save_writethrough $p
11088         roc_hit_init
11089
11090         log "Turn on read and write cache"
11091         set_cache read on
11092         set_cache writethrough on
11093
11094         log "Write data and read it back."
11095         log "Read should be satisfied from the cache."
11096         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11097         BEFORE=$(roc_hit)
11098         cancel_lru_locks osc
11099         cat $file >/dev/null
11100         AFTER=$(roc_hit)
11101         if ! let "AFTER - BEFORE == CPAGES"; then
11102                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11103         else
11104                 log "cache hits:: before: $BEFORE, after: $AFTER"
11105         fi
11106
11107         log "Read again; it should be satisfied from the cache."
11108         BEFORE=$AFTER
11109         cancel_lru_locks osc
11110         cat $file >/dev/null
11111         AFTER=$(roc_hit)
11112         if ! let "AFTER - BEFORE == CPAGES"; then
11113                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11114         else
11115                 log "cache hits:: before: $BEFORE, after: $AFTER"
11116         fi
11117
11118         log "Turn off the read cache and turn on the write cache"
11119         set_cache read off
11120         set_cache writethrough on
11121
11122         log "Read again; it should be satisfied from the cache."
11123         BEFORE=$(roc_hit)
11124         cancel_lru_locks osc
11125         cat $file >/dev/null
11126         AFTER=$(roc_hit)
11127         if ! let "AFTER - BEFORE == CPAGES"; then
11128                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11129         else
11130                 log "cache hits:: before: $BEFORE, after: $AFTER"
11131         fi
11132
11133         log "Read again; it should not be satisfied from the cache."
11134         BEFORE=$AFTER
11135         cancel_lru_locks osc
11136         cat $file >/dev/null
11137         AFTER=$(roc_hit)
11138         if ! let "AFTER - BEFORE == 0"; then
11139                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11140         else
11141                 log "cache hits:: before: $BEFORE, after: $AFTER"
11142         fi
11143
11144         log "Write data and read it back."
11145         log "Read should be satisfied from the cache."
11146         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11147         BEFORE=$(roc_hit)
11148         cancel_lru_locks osc
11149         cat $file >/dev/null
11150         AFTER=$(roc_hit)
11151         if ! let "AFTER - BEFORE == CPAGES"; then
11152                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11153         else
11154                 log "cache hits:: before: $BEFORE, after: $AFTER"
11155         fi
11156
11157         log "Read again; it should not be satisfied from the cache."
11158         BEFORE=$AFTER
11159         cancel_lru_locks osc
11160         cat $file >/dev/null
11161         AFTER=$(roc_hit)
11162         if ! let "AFTER - BEFORE == 0"; then
11163                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11164         else
11165                 log "cache hits:: before: $BEFORE, after: $AFTER"
11166         fi
11167
11168         log "Turn off read and write cache"
11169         set_cache read off
11170         set_cache writethrough off
11171
11172         log "Write data and read it back"
11173         log "It should not be satisfied from the cache."
11174         rm -f $file
11175         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11176         cancel_lru_locks osc
11177         BEFORE=$(roc_hit)
11178         cat $file >/dev/null
11179         AFTER=$(roc_hit)
11180         if ! let "AFTER - BEFORE == 0"; then
11181                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11182         else
11183                 log "cache hits:: before: $BEFORE, after: $AFTER"
11184         fi
11185
11186         log "Turn on the read cache and turn off the write cache"
11187         set_cache read on
11188         set_cache writethrough off
11189
11190         log "Write data and read it back"
11191         log "It should not be satisfied from the cache."
11192         rm -f $file
11193         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11194         BEFORE=$(roc_hit)
11195         cancel_lru_locks osc
11196         cat $file >/dev/null
11197         AFTER=$(roc_hit)
11198         if ! let "AFTER - BEFORE == 0"; then
11199                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11200         else
11201                 log "cache hits:: before: $BEFORE, after: $AFTER"
11202         fi
11203
11204         log "Read again; it should be satisfied from the cache."
11205         BEFORE=$(roc_hit)
11206         cancel_lru_locks osc
11207         cat $file >/dev/null
11208         AFTER=$(roc_hit)
11209         if ! let "AFTER - BEFORE == CPAGES"; then
11210                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11211         else
11212                 log "cache hits:: before: $BEFORE, after: $AFTER"
11213         fi
11214
11215         rm -f $file
11216         restore_lustre_params < $p
11217         rm -f $p
11218 }
11219 run_test 156 "Verification of tunables"
11220
11221 #Changelogs
11222 cleanup_changelog () {
11223         trap 0
11224         echo "Deregistering changelog client $CL_USER"
11225         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11226 }
11227
11228 err17935 () {
11229         if [[ $MDSCOUNT -gt 1 ]]; then
11230                 error_ignore bz17935 $*
11231         else
11232                 error $*
11233         fi
11234 }
11235
11236 changelog_chmask()
11237 {
11238         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11239
11240         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
11241
11242         if [ $MASK -eq 1 ]; then
11243                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
11244         else
11245                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
11246         fi
11247 }
11248
11249 changelog_extract_field() {
11250         local mdt=$1
11251         local cltype=$2
11252         local file=$3
11253         local identifier=$4
11254
11255         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
11256                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
11257                 tail -1
11258 }
11259
11260 test_160a() {
11261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11262         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11263         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11264                 { skip "Need MDS version at least 2.2.0"; return; }
11265
11266         local CL_USERS="mdd.$MDT0.changelog_users"
11267         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11268         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11269                 changelog_register -n)
11270         echo "Registered as changelog user $CL_USER"
11271         trap cleanup_changelog EXIT
11272         $GET_CL_USERS | grep -q $CL_USER ||
11273                 error "User $CL_USER not found in changelog_users"
11274
11275         # change something
11276         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11277         touch $DIR/$tdir/pics/2008/zachy/timestamp
11278         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11279         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11280         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11281         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11282         rm $DIR/$tdir/pics/desktop.jpg
11283
11284         $LFS changelog $MDT0 | tail -5
11285
11286         echo "verifying changelog mask"
11287         changelog_chmask "MKDIR"
11288         changelog_chmask "CLOSE"
11289
11290         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11291         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11292
11293         changelog_chmask "MKDIR"
11294         changelog_chmask "CLOSE"
11295
11296         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11297         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11298
11299         $LFS changelog $MDT0
11300         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11301         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11302         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11303         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11304
11305         # verify contents
11306         echo "verifying target fid"
11307         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11308         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11309         [ "$fidc" == "$fidf" ] ||
11310                 err17935 "fid in changelog $fidc != file fid $fidf"
11311         echo "verifying parent fid"
11312         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11313         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11314         [ "$fidc" == "$fidf" ] ||
11315                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11316
11317         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11318         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11319         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11320         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11321         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11322                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11323
11324         MIN_REC=$($GET_CL_USERS |
11325                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11326         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11327         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11328         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11329                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11330
11331         # LU-3446 changelog index reset on MDT restart
11332         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11333         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11334         $LFS changelog_clear $MDT0 $CL_USER 0
11335         stop $SINGLEMDS || error "Fail to stop MDT."
11336         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11337         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11338         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11339         [ $CUR_REC1 == $CUR_REC2 ] ||
11340                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11341
11342         echo "verifying user deregister"
11343         cleanup_changelog
11344         $GET_CL_USERS | grep -q $CL_USER &&
11345                 error "User $CL_USER still in changelog_users"
11346
11347         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11348         if [ $CL_USER -eq 0 ]; then
11349                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11350                 touch $DIR/$tdir/chloe
11351                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11352                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11353                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11354         else
11355                 echo "$CL_USER other changelog users; can't verify off"
11356         fi
11357 }
11358 run_test 160a "changelog sanity"
11359
11360 test_160b() { # LU-3587
11361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11362         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11363         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11364                 { skip "Need MDS version at least 2.2.0"; return; }
11365
11366         local CL_USERS="mdd.$MDT0.changelog_users"
11367         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11368         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11369                 changelog_register -n)
11370         echo "Registered as changelog user $CL_USER"
11371         trap cleanup_changelog EXIT
11372         $GET_CL_USERS | grep -q $CL_USER ||
11373                 error "User $CL_USER not found in changelog_users"
11374
11375         local LONGNAME1=$(str_repeat a 255)
11376         local LONGNAME2=$(str_repeat b 255)
11377
11378         cd $DIR
11379         echo "creating very long named file"
11380         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11381         echo "moving very long named file"
11382         mv $LONGNAME1 $LONGNAME2
11383
11384         $LFS changelog $MDT0 | grep RENME
11385         rm -f $LONGNAME2
11386         cleanup_changelog
11387 }
11388 run_test 160b "Verify that very long rename doesn't crash in changelog"
11389
11390 test_160c() {
11391         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11392
11393         local rc=0
11394         local server_version=$(lustre_version_code $SINGLEMDS)
11395
11396         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11397                 [[ $server_version -gt $(version_code 2.5.1) &&
11398                    $server_version -lt $(version_code 2.5.50) ]] ||
11399                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11401
11402         # Registration step
11403         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11404                 changelog_register -n)
11405         trap cleanup_changelog EXIT
11406
11407         rm -rf $DIR/$tdir
11408         mkdir -p $DIR/$tdir
11409         $MCREATE $DIR/$tdir/foo_160c
11410         changelog_chmask "TRUNC"
11411         $TRUNCATE $DIR/$tdir/foo_160c 200
11412         changelog_chmask "TRUNC"
11413         $TRUNCATE $DIR/$tdir/foo_160c 199
11414         $LFS changelog $MDT0
11415         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11416         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11417         $LFS changelog_clear $MDT0 $CL_USER 0
11418
11419         # Deregistration step
11420         cleanup_changelog
11421 }
11422 run_test 160c "verify that changelog log catch the truncate event"
11423
11424 test_160d() {
11425         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11426         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11427
11428         local server_version=$(lustre_version_code mds1)
11429         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11430
11431         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11432                 { skip "Need MDS version at least 2.7.60+"; return; }
11433         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11434
11435         # Registration step
11436         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11437                 changelog_register -n)
11438
11439         trap cleanup_changelog EXIT
11440         mkdir -p $DIR/$tdir/migrate_dir
11441         $LFS changelog_clear $MDT0 $CL_USER 0
11442
11443         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11444         $LFS changelog $MDT0
11445         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11446         $LFS changelog_clear $MDT0 $CL_USER 0
11447         [ $MIGRATES -eq 1 ] ||
11448                 error "MIGRATE changelog mask count $MIGRATES != 1"
11449
11450         # Deregistration step
11451         cleanup_changelog
11452 }
11453 run_test 160d "verify that changelog log catch the migrate event"
11454
11455 test_160e() {
11456         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11457
11458         # Create a user
11459         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11460                 changelog_register -n)
11461         echo "Registered as changelog user $CL_USER"
11462         trap cleanup_changelog EXIT
11463
11464         # Delete a future user (expect fail)
11465         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11466         local rc=$?
11467
11468         if [ $rc -eq 0 ]; then
11469                 error "Deleted non-existant user cl77"
11470         elif [ $rc -ne 2 ]; then
11471                 error "changelog_deregister failed with $rc, " \
11472                         "expected 2 (ENOENT)"
11473         fi
11474
11475         # Clear to a bad index (1 billion should be safe)
11476         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11477         rc=$?
11478
11479         if [ $rc -eq 0 ]; then
11480                 error "Successfully cleared to invalid CL index"
11481         elif [ $rc -ne 22 ]; then
11482                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11483         fi
11484 }
11485 run_test 160e "changelog negative testing"
11486
11487 test_161a() {
11488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11489         test_mkdir -p -c1 $DIR/$tdir
11490         cp /etc/hosts $DIR/$tdir/$tfile
11491         test_mkdir -c1 $DIR/$tdir/foo1
11492         test_mkdir -c1 $DIR/$tdir/foo2
11493         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11494         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11495         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11496         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11497         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11498         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11499                 $LFS fid2path $DIR $FID
11500                 err17935 "bad link ea"
11501         fi
11502     # middle
11503     rm $DIR/$tdir/foo2/zachary
11504     # last
11505     rm $DIR/$tdir/foo2/thor
11506     # first
11507     rm $DIR/$tdir/$tfile
11508     # rename
11509     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11510     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11511         then
11512         $LFS fid2path $DIR $FID
11513         err17935 "bad link rename"
11514     fi
11515     rm $DIR/$tdir/foo2/maggie
11516
11517         # overflow the EA
11518         local longname=filename_avg_len_is_thirty_two_
11519         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11520                 error "failed to hardlink many files"
11521         links=$($LFS fid2path $DIR $FID | wc -l)
11522         echo -n "${links}/1000 links in link EA"
11523         [[ $links -gt 60 ]] ||
11524                 err17935 "expected at least 60 links in link EA"
11525         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11526                 error "failed to unlink many hardlinks"
11527 }
11528 run_test 161a "link ea sanity"
11529
11530 test_161b() {
11531         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11532         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11533         local MDTIDX=1
11534         local remote_dir=$DIR/$tdir/remote_dir
11535
11536         mkdir -p $DIR/$tdir
11537         $LFS mkdir -i $MDTIDX $remote_dir ||
11538                 error "create remote directory failed"
11539
11540         cp /etc/hosts $remote_dir/$tfile
11541         mkdir -p $remote_dir/foo1
11542         mkdir -p $remote_dir/foo2
11543         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11544         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11545         ln $remote_dir/$tfile $remote_dir/foo1/luna
11546         ln $remote_dir/$tfile $remote_dir/foo2/thor
11547
11548         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11549                      tr -d ']')
11550         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11551                 $LFS fid2path $DIR $FID
11552                 err17935 "bad link ea"
11553         fi
11554         # middle
11555         rm $remote_dir/foo2/zachary
11556         # last
11557         rm $remote_dir/foo2/thor
11558         # first
11559         rm $remote_dir/$tfile
11560         # rename
11561         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11562         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11563         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11564                 $LFS fid2path $DIR $FID
11565                 err17935 "bad link rename"
11566         fi
11567         rm $remote_dir/foo2/maggie
11568
11569         # overflow the EA
11570         local longname=filename_avg_len_is_thirty_two_
11571         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11572                 error "failed to hardlink many files"
11573         links=$($LFS fid2path $DIR $FID | wc -l)
11574         echo -n "${links}/1000 links in link EA"
11575         [[ ${links} -gt 60 ]] ||
11576                 err17935 "expected at least 60 links in link EA"
11577         unlinkmany $remote_dir/foo2/$longname 1000 ||
11578         error "failed to unlink many hardlinks"
11579 }
11580 run_test 161b "link ea sanity under remote directory"
11581
11582 test_161c() {
11583         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11584         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11585         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
11586                 skip "Need MDS version at least 2.1.5" && return
11587
11588         # define CLF_RENAME_LAST 0x0001
11589         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
11590         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11591                 changelog_register -n)
11592
11593         trap cleanup_changelog EXIT
11594         rm -rf $DIR/$tdir
11595         mkdir -p $DIR/$tdir
11596         touch $DIR/$tdir/foo_161c
11597         touch $DIR/$tdir/bar_161c
11598         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11599         $LFS changelog $MDT0 | grep RENME
11600         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
11601                 cut -f5 -d' ')
11602         $LFS changelog_clear $MDT0 $CL_USER 0
11603         if [ x$flags != "x0x1" ]; then
11604                 error "flag $flags is not 0x1"
11605         fi
11606         echo "rename overwrite a target having nlink = 1," \
11607                 "changelog record has flags of $flags"
11608
11609         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
11610         touch $DIR/$tdir/foo_161c
11611         touch $DIR/$tdir/bar_161c
11612         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11613         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11614         $LFS changelog $MDT0 | grep RENME
11615         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11616         $LFS changelog_clear $MDT0 $CL_USER 0
11617         if [ x$flags != "x0x0" ]; then
11618                 error "flag $flags is not 0x0"
11619         fi
11620         echo "rename overwrite a target having nlink > 1," \
11621                 "changelog record has flags of $flags"
11622
11623         # rename doesn't overwrite a target (changelog flag 0x0)
11624         touch $DIR/$tdir/foo_161c
11625         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
11626         $LFS changelog $MDT0 | grep RENME
11627         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11628         $LFS changelog_clear $MDT0 $CL_USER 0
11629         if [ x$flags != "x0x0" ]; then
11630                 error "flag $flags is not 0x0"
11631         fi
11632         echo "rename doesn't overwrite a target," \
11633                 "changelog record has flags of $flags"
11634
11635         # define CLF_UNLINK_LAST 0x0001
11636         # unlink a file having nlink = 1 (changelog flag 0x1)
11637         rm -f $DIR/$tdir/foo2_161c
11638         $LFS changelog $MDT0 | grep UNLNK
11639         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11640         $LFS changelog_clear $MDT0 $CL_USER 0
11641         if [ x$flags != "x0x1" ]; then
11642                 error "flag $flags is not 0x1"
11643         fi
11644         echo "unlink a file having nlink = 1," \
11645                 "changelog record has flags of $flags"
11646
11647         # unlink a file having nlink > 1 (changelog flag 0x0)
11648         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11649         rm -f $DIR/$tdir/foobar_161c
11650         $LFS changelog $MDT0 | grep UNLNK
11651         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11652         $LFS changelog_clear $MDT0 $CL_USER 0
11653         if [ x$flags != "x0x0" ]; then
11654                 error "flag $flags is not 0x0"
11655         fi
11656         echo "unlink a file having nlink > 1," \
11657                 "changelog record has flags of $flags"
11658         cleanup_changelog
11659 }
11660 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
11661
11662 test_161d() {
11663         local user
11664         local pid
11665         local fid
11666
11667         # cleanup previous run
11668         rm -rf $DIR/$tdir/$tfile
11669
11670         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11671                 changelog_register -n)
11672         [[ $? -eq 0 ]] || error "changelog_register failed"
11673
11674         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
11675         # interfer with $MOUNT/.lustre/fid/ access
11676         mkdir $DIR/$tdir
11677         [[ $? -eq 0 ]] || error "mkdir failed"
11678
11679         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
11680         $LCTL set_param fail_loc=0x8000140c
11681         # 5s pause
11682         $LCTL set_param fail_val=5
11683
11684         # create file
11685         echo foofoo > $DIR/$tdir/$tfile &
11686         pid=$!
11687
11688         # wait for create to be delayed
11689         sleep 2
11690
11691         ps -p $pid
11692         [[ $? -eq 0 ]] || error "create should be blocked"
11693
11694         local tempfile=$(mktemp)
11695         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
11696         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
11697         # some delay may occur during ChangeLog publishing and file read just
11698         # above, that could allow file write to happen finally
11699         [[ -s $tempfile ]] && echo "file should be empty"
11700
11701         $LCTL set_param fail_loc=0
11702
11703         wait $pid
11704         [[ $? -eq 0 ]] || error "create failed"
11705
11706         $LFS changelog_clear $MDT0 $user 0
11707         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
11708 }
11709 run_test 161d "create with concurrent .lustre/fid access"
11710
11711 check_path() {
11712     local expected=$1
11713     shift
11714     local fid=$2
11715
11716     local path=$(${LFS} fid2path $*)
11717     # Remove the '//' indicating a remote directory
11718     path=$(echo $path | sed 's#//#/#g')
11719     RC=$?
11720
11721     if [ $RC -ne 0 ]; then
11722         err17935 "path looked up of $expected failed. Error $RC"
11723         return $RC
11724     elif [ "${path}" != "${expected}" ]; then
11725         err17935 "path looked up \"${path}\" instead of \"${expected}\""
11726         return 2
11727     fi
11728     echo "fid $fid resolves to path $path (expected $expected)"
11729 }
11730
11731 test_162a() { # was test_162
11732         # Make changes to filesystem
11733         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11734         test_mkdir -p -c1 $DIR/$tdir/d2
11735         touch $DIR/$tdir/d2/$tfile
11736         touch $DIR/$tdir/d2/x1
11737         touch $DIR/$tdir/d2/x2
11738         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
11739         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
11740         # regular file
11741         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
11742         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
11743                 error "check path $tdir/d2/$tfile failed"
11744
11745         # softlink
11746         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
11747         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
11748         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
11749                 error "check path $tdir/d2/p/q/r/slink failed"
11750
11751         # softlink to wrong file
11752         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
11753         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
11754         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
11755                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
11756
11757         # hardlink
11758         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
11759         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
11760         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
11761         # fid2path dir/fsname should both work
11762         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
11763                 error "check path $tdir/d2/a/b/c/new_file failed"
11764         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
11765                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
11766
11767         # hardlink count: check that there are 2 links
11768         # Doesnt work with CMD yet: 17935
11769         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
11770                 err17935 "expected 2 links"
11771
11772         # hardlink indexing: remove the first link
11773         rm $DIR/$tdir/d2/p/q/r/hlink
11774         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
11775                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
11776
11777         return 0
11778 }
11779 run_test 162a "path lookup sanity"
11780
11781 test_162b() {
11782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11783         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11784
11785         mkdir $DIR/$tdir
11786         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
11787                                 error "create striped dir failed"
11788
11789         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
11790                                         tail -n 1 | awk '{print $2}')
11791         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
11792
11793         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
11794         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
11795
11796         # regular file
11797         for ((i=0;i<5;i++)); do
11798                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
11799                         error "get fid for f$i failed"
11800                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
11801                         error "check path $tdir/striped_dir/f$i failed"
11802
11803                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
11804                         error "get fid for d$i failed"
11805                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
11806                         error "check path $tdir/striped_dir/d$i failed"
11807         done
11808
11809         return 0
11810 }
11811 run_test 162b "striped directory path lookup sanity"
11812
11813 # LU-4239: Verify fid2path works with paths 100 or more directories deep
11814 test_162c() {
11815         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
11816                 skip "Need MDS version at least 2.7.51" && return
11817         test_mkdir $DIR/$tdir.local
11818         test_mkdir $DIR/$tdir.remote
11819         local lpath=$tdir.local
11820         local rpath=$tdir.remote
11821
11822         for ((i = 0; i <= 101; i++)); do
11823                 lpath="$lpath/$i"
11824                 mkdir $DIR/$lpath
11825                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
11826                         error "get fid for local directory $DIR/$lpath failed"
11827                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
11828                         error "check path for local directory $DIR/$lpath failed"
11829
11830                 rpath="$rpath/$i"
11831                 test_mkdir $DIR/$rpath
11832                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
11833                         error "get fid for remote directory $DIR/$rpath failed"
11834                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
11835                         error "check path for remote directory $DIR/$rpath failed"
11836         done
11837
11838         return 0
11839 }
11840 run_test 162c "fid2path works with paths 100 or more directories deep"
11841
11842 test_169() {
11843         # do directio so as not to populate the page cache
11844         log "creating a 10 Mb file"
11845         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11846         log "starting reads"
11847         dd if=$DIR/$tfile of=/dev/null bs=4096 &
11848         log "truncating the file"
11849         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11850         log "killing dd"
11851         kill %+ || true # reads might have finished
11852         echo "wait until dd is finished"
11853         wait
11854         log "removing the temporary file"
11855         rm -rf $DIR/$tfile || error "tmp file removal failed"
11856 }
11857 run_test 169 "parallel read and truncate should not deadlock"
11858
11859 test_170() {
11860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11861         $LCTL clear     # bug 18514
11862         $LCTL debug_daemon start $TMP/${tfile}_log_good
11863         touch $DIR/$tfile
11864         $LCTL debug_daemon stop
11865         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
11866                error "sed failed to read log_good"
11867
11868         $LCTL debug_daemon start $TMP/${tfile}_log_good
11869         rm -rf $DIR/$tfile
11870         $LCTL debug_daemon stop
11871
11872         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
11873                error "lctl df log_bad failed"
11874
11875         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11876         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11877
11878         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
11879         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
11880
11881         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
11882                 error "bad_line good_line1 good_line2 are empty"
11883
11884         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11885         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
11886         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11887
11888         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
11889         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11890         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11891
11892         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
11893                 error "bad_line_new good_line_new are empty"
11894
11895         local expected_good=$((good_line1 + good_line2*2))
11896
11897         rm -f $TMP/${tfile}*
11898         # LU-231, short malformed line may not be counted into bad lines
11899         if [ $bad_line -ne $bad_line_new ] &&
11900                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
11901                 error "expected $bad_line bad lines, but got $bad_line_new"
11902                 return 1
11903         fi
11904
11905         if [ $expected_good -ne $good_line_new ]; then
11906                 error "expected $expected_good good lines, but got $good_line_new"
11907                 return 2
11908         fi
11909         true
11910 }
11911 run_test 170 "test lctl df to handle corrupted log ====================="
11912
11913 test_171() { # bug20592
11914         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11915 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11916         $LCTL set_param fail_loc=0x50e
11917         $LCTL set_param fail_val=3000
11918         multiop_bg_pause $DIR/$tfile O_s || true
11919         local MULTIPID=$!
11920         kill -USR1 $MULTIPID
11921         # cause log dump
11922         sleep 3
11923         wait $MULTIPID
11924         if dmesg | grep "recursive fault"; then
11925                 error "caught a recursive fault"
11926         fi
11927         $LCTL set_param fail_loc=0
11928         true
11929 }
11930 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11931
11932 # it would be good to share it with obdfilter-survey/iokit-libecho code
11933 setup_obdecho_osc () {
11934         local rc=0
11935         local ost_nid=$1
11936         local obdfilter_name=$2
11937         echo "Creating new osc for $obdfilter_name on $ost_nid"
11938         # make sure we can find loopback nid
11939         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11940
11941         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11942                            ${obdfilter_name}_osc_UUID || rc=2; }
11943         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11944                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11945         return $rc
11946 }
11947
11948 cleanup_obdecho_osc () {
11949         local obdfilter_name=$1
11950         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11951         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11952         return 0
11953 }
11954
11955 obdecho_test() {
11956         local OBD=$1
11957         local node=$2
11958         local pages=${3:-64}
11959         local rc=0
11960         local id
11961
11962         local count=10
11963         local obd_size=$(get_obd_size $node $OBD)
11964         local page_size=$(get_page_size $node)
11965         if [[ -n "$obd_size" ]]; then
11966                 local new_count=$((obd_size / (pages * page_size / 1024)))
11967                 [[ $new_count -ge $count ]] || count=$new_count
11968         fi
11969
11970         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11971         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11972                            rc=2; }
11973         if [ $rc -eq 0 ]; then
11974             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11975             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11976         fi
11977         echo "New object id is $id"
11978         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11979                            rc=4; }
11980         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11981                            "test_brw $count w v $pages $id" || rc=4; }
11982         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11983                            rc=4; }
11984         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11985                                         "cleanup" || rc=5; }
11986         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11987                                         "detach" || rc=6; }
11988         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11989         return $rc
11990 }
11991
11992 test_180a() {
11993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11994         remote_ost_nodsh && skip "remote OST with nodsh" && return
11995         local rc=0
11996         local rmmod_local=0
11997
11998         if ! module_loaded obdecho; then
11999             load_module obdecho/obdecho
12000             rmmod_local=1
12001         fi
12002
12003         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
12004         local host=$(lctl get_param -n osc.$osc.import |
12005                              awk '/current_connection:/ {print $2}' )
12006         local target=$(lctl get_param -n osc.$osc.import |
12007                              awk '/target:/ {print $2}' )
12008         target=${target%_UUID}
12009
12010         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
12011         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
12012         [[ -n $target ]] && cleanup_obdecho_osc $target
12013         [ $rmmod_local -eq 1 ] && rmmod obdecho
12014         return $rc
12015 }
12016 run_test 180a "test obdecho on osc"
12017
12018 test_180b() {
12019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12020         remote_ost_nodsh && skip "remote OST with nodsh" && return
12021         local rc=0
12022         local rmmod_remote=0
12023
12024         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12025                 rmmod_remote=true || error "failed to load module obdecho"
12026         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
12027         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
12028         $rmmod_remote && do_facet ost1 "rmmod obdecho"
12029         return $rc
12030 }
12031 run_test 180b "test obdecho directly on obdfilter"
12032
12033 test_180c() { # LU-2598
12034         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12035         remote_ost_nodsh && skip "remote OST with nodsh" && return
12036         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
12037                 skip "Need MDS version at least 2.4.0" && return
12038
12039         local rc=0
12040         local rmmod_remote=false
12041         local pages=16384 # 64MB bulk I/O RPC size
12042         local target
12043
12044         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12045                 rmmod_remote=true || error "failed to load module obdecho"
12046
12047         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
12048                 head -n1)
12049         if [ -n "$target" ]; then
12050                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
12051         else
12052                 echo "there is no obdfilter target on ost1"
12053                 rc=2
12054         fi
12055         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
12056         return $rc
12057 }
12058 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
12059
12060 test_181() { # bug 22177
12061         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12062         # create enough files to index the directory
12063         createmany -o $DIR/$tdir/foobar 4000
12064         # print attributes for debug purpose
12065         lsattr -d .
12066         # open dir
12067         multiop_bg_pause $DIR/$tdir D_Sc || return 1
12068         MULTIPID=$!
12069         # remove the files & current working dir
12070         unlinkmany $DIR/$tdir/foobar 4000
12071         rmdir $DIR/$tdir
12072         kill -USR1 $MULTIPID
12073         wait $MULTIPID
12074         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
12075         return 0
12076 }
12077 run_test 181 "Test open-unlinked dir ========================"
12078
12079 test_182() {
12080         local fcount=1000
12081         local tcount=10
12082
12083         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12084
12085         $LCTL set_param mdc.*.rpc_stats=clear
12086
12087         for (( i = 0; i < $tcount; i++ )) ; do
12088                 mkdir $DIR/$tdir/$i
12089         done
12090
12091         for (( i = 0; i < $tcount; i++ )) ; do
12092                 createmany -o $DIR/$tdir/$i/f- $fcount &
12093         done
12094         wait
12095
12096         for (( i = 0; i < $tcount; i++ )) ; do
12097                 unlinkmany $DIR/$tdir/$i/f- $fcount &
12098         done
12099         wait
12100
12101         $LCTL get_param mdc.*.rpc_stats
12102
12103         rm -rf $DIR/$tdir
12104 }
12105 run_test 182 "Test parallel modify metadata operations ================"
12106
12107 test_183() { # LU-2275
12108         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12109         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
12110                 skip "Need MDS version at least 2.3.56" && return
12111
12112         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12113         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12114         echo aaa > $DIR/$tdir/$tfile
12115
12116 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
12117         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
12118
12119         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
12120         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
12121
12122         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
12123
12124         # Flush negative dentry cache
12125         touch $DIR/$tdir/$tfile
12126
12127         # We are not checking for any leaked references here, they'll
12128         # become evident next time we do cleanup with module unload.
12129         rm -rf $DIR/$tdir
12130 }
12131 run_test 183 "No crash or request leak in case of strange dispositions ========"
12132
12133 # test suite 184 is for LU-2016, LU-2017
12134 test_184a() {
12135         check_swap_layouts_support && return 0
12136
12137         dir0=$DIR/$tdir/$testnum
12138         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
12139         ref1=/etc/passwd
12140         ref2=/etc/group
12141         file1=$dir0/f1
12142         file2=$dir0/f2
12143         $SETSTRIPE -c1 $file1
12144         cp $ref1 $file1
12145         $SETSTRIPE -c2 $file2
12146         cp $ref2 $file2
12147         gen1=$($GETSTRIPE -g $file1)
12148         gen2=$($GETSTRIPE -g $file2)
12149
12150         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
12151         gen=$($GETSTRIPE -g $file1)
12152         [[ $gen1 != $gen ]] ||
12153                 "Layout generation on $file1 does not change"
12154         gen=$($GETSTRIPE -g $file2)
12155         [[ $gen2 != $gen ]] ||
12156                 "Layout generation on $file2 does not change"
12157
12158         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
12159         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12160 }
12161 run_test 184a "Basic layout swap"
12162
12163 test_184b() {
12164         check_swap_layouts_support && return 0
12165
12166         dir0=$DIR/$tdir/$testnum
12167         mkdir -p $dir0 || error "creating dir $dir0"
12168         file1=$dir0/f1
12169         file2=$dir0/f2
12170         file3=$dir0/f3
12171         dir1=$dir0/d1
12172         dir2=$dir0/d2
12173         mkdir $dir1 $dir2
12174         $SETSTRIPE -c1 $file1
12175         $SETSTRIPE -c2 $file2
12176         $SETSTRIPE -c1 $file3
12177         chown $RUNAS_ID $file3
12178         gen1=$($GETSTRIPE -g $file1)
12179         gen2=$($GETSTRIPE -g $file2)
12180
12181         $LFS swap_layouts $dir1 $dir2 &&
12182                 error "swap of directories layouts should fail"
12183         $LFS swap_layouts $dir1 $file1 &&
12184                 error "swap of directory and file layouts should fail"
12185         $RUNAS $LFS swap_layouts $file1 $file2 &&
12186                 error "swap of file we cannot write should fail"
12187         $LFS swap_layouts $file1 $file3 &&
12188                 error "swap of file with different owner should fail"
12189         /bin/true # to clear error code
12190 }
12191 run_test 184b "Forbidden layout swap (will generate errors)"
12192
12193 test_184c() {
12194         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
12195         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
12196         check_swap_layouts_support && return 0
12197
12198         local dir0=$DIR/$tdir/$testnum
12199         mkdir -p $dir0 || error "creating dir $dir0"
12200
12201         local ref1=$dir0/ref1
12202         local ref2=$dir0/ref2
12203         local file1=$dir0/file1
12204         local file2=$dir0/file2
12205         # create a file large enough for the concurrent test
12206         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
12207         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
12208         echo "ref file size: ref1($(stat -c %s $ref1))," \
12209              "ref2($(stat -c %s $ref2))"
12210
12211         cp $ref2 $file2
12212         dd if=$ref1 of=$file1 bs=16k &
12213         local DD_PID=$!
12214
12215         # Make sure dd starts to copy file
12216         while [ ! -f $file1 ]; do sleep 0.1; done
12217
12218         $LFS swap_layouts $file1 $file2
12219         local rc=$?
12220         wait $DD_PID
12221         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
12222         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
12223
12224         # how many bytes copied before swapping layout
12225         local copied=$(stat -c %s $file2)
12226         local remaining=$(stat -c %s $ref1)
12227         remaining=$((remaining - copied))
12228         echo "Copied $copied bytes before swapping layout..."
12229
12230         cmp -n $copied $file1 $ref2 | grep differ &&
12231                 error "Content mismatch [0, $copied) of ref2 and file1"
12232         cmp -n $copied $file2 $ref1 ||
12233                 error "Content mismatch [0, $copied) of ref1 and file2"
12234         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
12235                 error "Content mismatch [$copied, EOF) of ref1 and file1"
12236
12237         # clean up
12238         rm -f $ref1 $ref2 $file1 $file2
12239 }
12240 run_test 184c "Concurrent write and layout swap"
12241
12242 test_184d() {
12243         check_swap_layouts_support && return 0
12244         [ -z "$(which getfattr 2>/dev/null)" ] &&
12245                 skip "no getfattr command" && return 0
12246
12247         local file1=$DIR/$tdir/$tfile-1
12248         local file2=$DIR/$tdir/$tfile-2
12249         local file3=$DIR/$tdir/$tfile-3
12250         local lovea1
12251         local lovea2
12252
12253         mkdir -p $DIR/$tdir
12254         touch $file1 || error "create $file1 failed"
12255         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12256                 error "create $file2 failed"
12257         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12258                 error "create $file3 failed"
12259         lovea1=$(get_layout_param $file1)
12260
12261         $LFS swap_layouts $file2 $file3 ||
12262                 error "swap $file2 $file3 layouts failed"
12263         $LFS swap_layouts $file1 $file2 ||
12264                 error "swap $file1 $file2 layouts failed"
12265
12266         lovea2=$(get_layout_param $file2)
12267         echo "$lovea1"
12268         echo "$lovea2"
12269         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12270
12271         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12272         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12273 }
12274 run_test 184d "allow stripeless layouts swap"
12275
12276 test_184e() {
12277         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12278                 { skip "Need MDS version at least 2.6.94"; return 0; }
12279         check_swap_layouts_support && return 0
12280         [ -z "$(which getfattr 2>/dev/null)" ] &&
12281                 skip "no getfattr command" && return 0
12282
12283         local file1=$DIR/$tdir/$tfile-1
12284         local file2=$DIR/$tdir/$tfile-2
12285         local file3=$DIR/$tdir/$tfile-3
12286         local lovea
12287
12288         mkdir -p $DIR/$tdir
12289         touch $file1 || error "create $file1 failed"
12290         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12291                 error "create $file2 failed"
12292         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12293                 error "create $file3 failed"
12294
12295         $LFS swap_layouts $file1 $file2 ||
12296                 error "swap $file1 $file2 layouts failed"
12297
12298         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12299         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12300
12301         echo 123 > $file1 || error "Should be able to write into $file1"
12302
12303         $LFS swap_layouts $file1 $file3 ||
12304                 error "swap $file1 $file3 layouts failed"
12305
12306         echo 123 > $file1 || error "Should be able to write into $file1"
12307
12308         rm -rf $file1 $file2 $file3
12309 }
12310 run_test 184e "Recreate layout after stripeless layout swaps"
12311
12312 test_185() { # LU-2441
12313         # LU-3553 - no volatile file support in old servers
12314         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12315                 { skip "Need MDS version at least 2.3.60"; return 0; }
12316
12317         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12318         touch $DIR/$tdir/spoo
12319         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12320         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12321                 error "cannot create/write a volatile file"
12322         [ "$FILESET" == "" ] &&
12323         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12324                 error "FID is still valid after close"
12325
12326         multiop_bg_pause $DIR/$tdir vVw4096_c
12327         local multi_pid=$!
12328
12329         local OLD_IFS=$IFS
12330         IFS=":"
12331         local fidv=($fid)
12332         IFS=$OLD_IFS
12333         # assume that the next FID for this client is sequential, since stdout
12334         # is unfortunately eaten by multiop_bg_pause
12335         local n=$((${fidv[1]} + 1))
12336         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12337         if [ "$FILESET" == "" ]; then
12338                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12339                         error "FID is missing before close"
12340         fi
12341         kill -USR1 $multi_pid
12342         # 1 second delay, so if mtime change we will see it
12343         sleep 1
12344         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12345         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12346 }
12347 run_test 185 "Volatile file support"
12348
12349 test_187a() {
12350         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12351         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12352                 skip "Need MDS version at least 2.3.0" && return
12353
12354         local dir0=$DIR/$tdir/$testnum
12355         mkdir -p $dir0 || error "creating dir $dir0"
12356
12357         local file=$dir0/file1
12358         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12359         local dv1=$($LFS data_version $file)
12360         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12361         local dv2=$($LFS data_version $file)
12362         [[ $dv1 != $dv2 ]] ||
12363                 error "data version did not change on write $dv1 == $dv2"
12364
12365         # clean up
12366         rm -f $file1
12367 }
12368 run_test 187a "Test data version change"
12369
12370 test_187b() {
12371         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12372         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12373                 skip "Need MDS version at least 2.3.0" && return
12374
12375         local dir0=$DIR/$tdir/$testnum
12376         mkdir -p $dir0 || error "creating dir $dir0"
12377
12378         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12379         [[ ${DV[0]} != ${DV[1]} ]] ||
12380                 error "data version did not change on write"\
12381                       " ${DV[0]} == ${DV[1]}"
12382
12383         # clean up
12384         rm -f $file1
12385 }
12386 run_test 187b "Test data version change on volatile file"
12387
12388 test_200() {
12389         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12390         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12391         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12392
12393         local POOL=${POOL:-cea1}
12394         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12395         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12396         # Pool OST targets
12397         local first_ost=0
12398         local last_ost=$(($OSTCOUNT - 1))
12399         local ost_step=2
12400         local ost_list=$(seq $first_ost $ost_step $last_ost)
12401         local ost_range="$first_ost $last_ost $ost_step"
12402         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12403         local file_dir=$POOL_ROOT/file_tst
12404         local subdir=$test_path/subdir
12405         local rc=0
12406
12407         if ! combined_mgs_mds ; then
12408                 mount_mgs_client
12409         fi
12410
12411         while : ; do
12412                 # former test_200a test_200b
12413                 pool_add $POOL                          || { rc=$? ; break; }
12414                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12415                 # former test_200c test_200d
12416                 mkdir -p $test_path
12417                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12418                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12419                 mkdir -p $subdir
12420                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12421                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12422                                                         || { rc=$? ; break; }
12423                 # former test_200e test_200f
12424                 local files=$((OSTCOUNT*3))
12425                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12426                                                         || { rc=$? ; break; }
12427                 pool_create_files $POOL $file_dir $files "$ost_list" \
12428                                                         || { rc=$? ; break; }
12429                 # former test_200g test_200h
12430                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12431                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12432
12433                 # former test_201a test_201b test_201c
12434                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12435
12436                 local f=$test_path/$tfile
12437                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12438                 pool_remove $POOL $f                    || { rc=$? ; break; }
12439                 break
12440         done
12441
12442         destroy_test_pools
12443
12444         if ! combined_mgs_mds ; then
12445                 umount_mgs_client
12446         fi
12447         return $rc
12448 }
12449 run_test 200 "OST pools"
12450
12451 # usage: default_attr <count | size | offset>
12452 default_attr() {
12453         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12454 }
12455
12456 # usage: check_default_stripe_attr
12457 check_default_stripe_attr() {
12458         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12459         case $1 in
12460         --stripe-count|-c)
12461                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12462         --stripe-size|-S)
12463                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12464         --stripe-index|-i)
12465                 EXPECTED=-1;;
12466         *)
12467                 error "unknown getstripe attr '$1'"
12468         esac
12469
12470         [ $ACTUAL != $EXPECTED ] &&
12471                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12472 }
12473
12474 test_204a() {
12475         test_mkdir -p $DIR/$tdir
12476         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12477
12478         check_default_stripe_attr --stripe-count
12479         check_default_stripe_attr --stripe-size
12480         check_default_stripe_attr --stripe-index
12481
12482         return 0
12483 }
12484 run_test 204a "Print default stripe attributes ================="
12485
12486 test_204b() {
12487         test_mkdir -p $DIR/$tdir
12488         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12489
12490         check_default_stripe_attr --stripe-size
12491         check_default_stripe_attr --stripe-index
12492
12493         return 0
12494 }
12495 run_test 204b "Print default stripe size and offset  ==========="
12496
12497 test_204c() {
12498         test_mkdir -p $DIR/$tdir
12499         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12500
12501         check_default_stripe_attr --stripe-count
12502         check_default_stripe_attr --stripe-index
12503
12504         return 0
12505 }
12506 run_test 204c "Print default stripe count and offset ==========="
12507
12508 test_204d() {
12509         test_mkdir -p $DIR/$tdir
12510         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12511
12512         check_default_stripe_attr --stripe-count
12513         check_default_stripe_attr --stripe-size
12514
12515         return 0
12516 }
12517 run_test 204d "Print default stripe count and size ============="
12518
12519 test_204e() {
12520         test_mkdir -p $DIR/$tdir
12521         $SETSTRIPE -d $DIR/$tdir
12522
12523         check_default_stripe_attr --stripe-count --raw
12524         check_default_stripe_attr --stripe-size --raw
12525         check_default_stripe_attr --stripe-index --raw
12526
12527         return 0
12528 }
12529 run_test 204e "Print raw stripe attributes ================="
12530
12531 test_204f() {
12532         test_mkdir -p $DIR/$tdir
12533         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12534
12535         check_default_stripe_attr --stripe-size --raw
12536         check_default_stripe_attr --stripe-index --raw
12537
12538         return 0
12539 }
12540 run_test 204f "Print raw stripe size and offset  ==========="
12541
12542 test_204g() {
12543         test_mkdir -p $DIR/$tdir
12544         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12545
12546         check_default_stripe_attr --stripe-count --raw
12547         check_default_stripe_attr --stripe-index --raw
12548
12549         return 0
12550 }
12551 run_test 204g "Print raw stripe count and offset ==========="
12552
12553 test_204h() {
12554         test_mkdir -p $DIR/$tdir
12555         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12556
12557         check_default_stripe_attr --stripe-count --raw
12558         check_default_stripe_attr --stripe-size --raw
12559
12560         return 0
12561 }
12562 run_test 204h "Print raw stripe count and size ============="
12563
12564 # Figure out which job scheduler is being used, if any,
12565 # or use a fake one
12566 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12567         JOBENV=SLURM_JOB_ID
12568 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12569         JOBENV=LSB_JOBID
12570 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12571         JOBENV=PBS_JOBID
12572 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12573         JOBENV=LOADL_STEP_ID
12574 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12575         JOBENV=JOB_ID
12576 else
12577         $LCTL list_param jobid_name > /dev/null 2>&1
12578         if [ $? -eq 0 ]; then
12579                 JOBENV=nodelocal
12580         else
12581                 JOBENV=FAKE_JOBID
12582         fi
12583 fi
12584
12585 verify_jobstats() {
12586         local cmd=($1)
12587         shift
12588         local facets="$@"
12589
12590 # we don't really need to clear the stats for this test to work, since each
12591 # command has a unique jobid, but it makes debugging easier if needed.
12592 #       for facet in $facets; do
12593 #               local dev=$(convert_facet2label $facet)
12594 #               # clear old jobstats
12595 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
12596 #       done
12597
12598         # use a new JobID for each test, or we might see an old one
12599         [ "$JOBENV" = "FAKE_JOBID" ] &&
12600                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12601
12602         JOBVAL=${!JOBENV}
12603
12604         [ "$JOBENV" = "nodelocal" ] && {
12605                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12606                 $LCTL set_param jobid_name=$FAKE_JOBID
12607                 JOBVAL=$FAKE_JOBID
12608         }
12609
12610         log "Test: ${cmd[*]}"
12611         log "Using JobID environment variable $JOBENV=$JOBVAL"
12612
12613         if [ $JOBENV = "FAKE_JOBID" ]; then
12614                 FAKE_JOBID=$JOBVAL ${cmd[*]}
12615         else
12616                 ${cmd[*]}
12617         fi
12618
12619         # all files are created on OST0000
12620         for facet in $facets; do
12621                 local stats="*.$(convert_facet2label $facet).job_stats"
12622                 if [ $(do_facet $facet lctl get_param $stats |
12623                        grep -c $JOBVAL) -ne 1 ]; then
12624                         do_facet $facet lctl get_param $stats
12625                         error "No jobstats for $JOBVAL found on $facet::$stats"
12626                 fi
12627         done
12628 }
12629
12630 jobstats_set() {
12631         trap 0
12632         NEW_JOBENV=${1:-$OLD_JOBENV}
12633         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
12634         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
12635 }
12636
12637 cleanup_205() {
12638         trap 0
12639         do_facet $SINGLEMDS \
12640                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
12641         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
12642         cleanup_changelog
12643 }
12644
12645 test_205() { # Job stats
12646         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
12647                 { skip "Need MDS version with at least 2.7.1"; return 0; }
12648
12649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12650         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12651         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12652         remote_ost_nodsh && skip "remote OST with nodsh" && return
12653
12654         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
12655                 skip "Server doesn't support jobstats" && return 0
12656         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
12657
12658         OLD_JOBENV=$($LCTL get_param -n jobid_var)
12659         if [ $OLD_JOBENV != $JOBENV ]; then
12660                 jobstats_set $JOBENV
12661                 trap cleanup_205 EXIT
12662         fi
12663
12664         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
12665         echo "Registered as changelog user $CL_USER"
12666
12667         OLD_INTERVAL=$(do_facet $SINGLEMDS \
12668                        lctl get_param -n mdt.*.job_cleanup_interval)
12669         local interval_new=5
12670         do_facet $SINGLEMDS \
12671                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
12672         local start=$SECONDS
12673
12674         local cmd
12675         # mkdir
12676         cmd="mkdir $DIR/$tdir"
12677         verify_jobstats "$cmd" "$SINGLEMDS"
12678         # rmdir
12679         cmd="rmdir $DIR/$tdir"
12680         verify_jobstats "$cmd" "$SINGLEMDS"
12681         # mkdir on secondary MDT
12682         if [ $MDSCOUNT -gt 1 ]; then
12683                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
12684                 verify_jobstats "$cmd" "mds2"
12685         fi
12686         # mknod
12687         cmd="mknod $DIR/$tfile c 1 3"
12688         verify_jobstats "$cmd" "$SINGLEMDS"
12689         # unlink
12690         cmd="rm -f $DIR/$tfile"
12691         verify_jobstats "$cmd" "$SINGLEMDS"
12692         # create all files on OST0000 so verify_jobstats can find OST stats
12693         # open & close
12694         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
12695         verify_jobstats "$cmd" "$SINGLEMDS"
12696         # setattr
12697         cmd="touch $DIR/$tfile"
12698         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12699         # write
12700         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
12701         verify_jobstats "$cmd" "ost1"
12702         # read
12703         cancel_lru_locks osc
12704         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
12705         verify_jobstats "$cmd" "ost1"
12706         # truncate
12707         cmd="$TRUNCATE $DIR/$tfile 0"
12708         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12709         # rename
12710         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
12711         verify_jobstats "$cmd" "$SINGLEMDS"
12712         # jobstats expiry - sleep until old stats should be expired
12713         local left=$((interval_new + 5 - (SECONDS - start)))
12714         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
12715                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
12716                         "0" $left
12717         cmd="mkdir $DIR/$tdir.expire"
12718         verify_jobstats "$cmd" "$SINGLEMDS"
12719         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
12720             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
12721
12722         # Ensure that jobid are present in changelog (if supported by MDS)
12723         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
12724         then
12725                 $LFS changelog $MDT0 | tail -9
12726                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
12727                 [ $jobids -eq 9 ] ||
12728                         error "Wrong changelog jobid count $jobids != 9"
12729
12730                 # LU-5862
12731                 JOBENV="disable"
12732                 jobstats_set $JOBENV
12733                 touch $DIR/$tfile
12734                 $LFS changelog $MDT0 | tail -1
12735                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
12736                 [ $jobids -eq 0 ] ||
12737                         error "Unexpected jobids when jobid_var=$JOBENV"
12738         fi
12739
12740         cleanup_205
12741 }
12742 run_test 205 "Verify job stats"
12743
12744 # LU-1480, LU-1773 and LU-1657
12745 test_206() {
12746         mkdir -p $DIR/$tdir
12747         $SETSTRIPE -c -1 $DIR/$tdir
12748 #define OBD_FAIL_LOV_INIT 0x1403
12749         $LCTL set_param fail_loc=0xa0001403
12750         $LCTL set_param fail_val=1
12751         touch $DIR/$tdir/$tfile || true
12752 }
12753 run_test 206 "fail lov_init_raid0() doesn't lbug"
12754
12755 test_207a() {
12756         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12757         local fsz=`stat -c %s $DIR/$tfile`
12758         cancel_lru_locks mdc
12759
12760         # do not return layout in getattr intent
12761 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
12762         $LCTL set_param fail_loc=0x170
12763         local sz=`stat -c %s $DIR/$tfile`
12764
12765         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
12766
12767         rm -rf $DIR/$tfile
12768 }
12769 run_test 207a "can refresh layout at glimpse"
12770
12771 test_207b() {
12772         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12773         local cksum=`md5sum $DIR/$tfile`
12774         local fsz=`stat -c %s $DIR/$tfile`
12775         cancel_lru_locks mdc
12776         cancel_lru_locks osc
12777
12778         # do not return layout in getattr intent
12779 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
12780         $LCTL set_param fail_loc=0x171
12781
12782         # it will refresh layout after the file is opened but before read issues
12783         echo checksum is "$cksum"
12784         echo "$cksum" |md5sum -c --quiet || error "file differs"
12785
12786         rm -rf $DIR/$tfile
12787 }
12788 run_test 207b "can refresh layout at open"
12789
12790 test_208() {
12791         # FIXME: in this test suite, only RD lease is used. This is okay
12792         # for now as only exclusive open is supported. After generic lease
12793         # is done, this test suite should be revised. - Jinshan
12794
12795         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12796         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
12797                 { skip "Need MDS version at least 2.4.52"; return 0; }
12798
12799         echo "==== test 1: verify get lease work"
12800         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
12801
12802         echo "==== test 2: verify lease can be broken by upcoming open"
12803         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12804         local PID=$!
12805         sleep 1
12806
12807         $MULTIOP $DIR/$tfile oO_RDONLY:c
12808         kill -USR1 $PID && wait $PID || error "break lease error"
12809
12810         echo "==== test 3: verify lease can't be granted if an open already exists"
12811         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
12812         local PID=$!
12813         sleep 1
12814
12815         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
12816         kill -USR1 $PID && wait $PID || error "open file error"
12817
12818         echo "==== test 4: lease can sustain over recovery"
12819         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
12820         PID=$!
12821         sleep 1
12822
12823         fail mds1
12824
12825         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
12826
12827         echo "==== test 5: lease broken can't be regained by replay"
12828         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12829         PID=$!
12830         sleep 1
12831
12832         # open file to break lease and then recovery
12833         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
12834         fail mds1
12835
12836         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
12837
12838         rm -f $DIR/$tfile
12839 }
12840 run_test 208 "Exclusive open"
12841
12842 test_209() {
12843         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
12844                 skip_env "must have disp_stripe" && return
12845
12846         touch $DIR/$tfile
12847         sync; sleep 5; sync;
12848
12849         echo 3 > /proc/sys/vm/drop_caches
12850         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12851
12852         # open/close 500 times
12853         for i in $(seq 500); do
12854                 cat $DIR/$tfile
12855         done
12856
12857         echo 3 > /proc/sys/vm/drop_caches
12858         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12859
12860         echo "before: $req_before, after: $req_after"
12861         [ $((req_after - req_before)) -ge 300 ] &&
12862                 error "open/close requests are not freed"
12863         return 0
12864 }
12865 run_test 209 "read-only open/close requests should be freed promptly"
12866
12867 test_212() {
12868         size=`date +%s`
12869         size=$((size % 8192 + 1))
12870         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
12871         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
12872         rm -f $DIR/f212 $DIR/f212.xyz
12873 }
12874 run_test 212 "Sendfile test ============================================"
12875
12876 test_213() {
12877         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
12878         cancel_lru_locks osc
12879         lctl set_param fail_loc=0x8000040f
12880         # generate a read lock
12881         cat $DIR/$tfile > /dev/null
12882         # write to the file, it will try to cancel the above read lock.
12883         cat /etc/hosts >> $DIR/$tfile
12884 }
12885 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
12886
12887 test_214() { # for bug 20133
12888         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
12889         for (( i=0; i < 340; i++ )) ; do
12890                 touch $DIR/$tdir/d214c/a$i
12891         done
12892
12893         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
12894         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
12895         ls $DIR/d214c || error "ls $DIR/d214c failed"
12896         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
12897         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
12898 }
12899 run_test 214 "hash-indexed directory test - bug 20133"
12900
12901 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
12902 create_lnet_proc_files() {
12903         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
12904         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
12905
12906         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
12907         rm -f "$TMP/lnet_$1.sys_tmp"
12908 }
12909
12910 # counterpart of create_lnet_proc_files
12911 remove_lnet_proc_files() {
12912         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
12913 }
12914
12915 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12916 # 3rd arg as regexp for body
12917 check_lnet_proc_stats() {
12918         local l=$(cat "$TMP/lnet_$1" |wc -l)
12919         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
12920
12921         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
12922 }
12923
12924 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12925 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
12926 # optional and can be regexp for 2nd line (lnet.routes case)
12927 check_lnet_proc_entry() {
12928         local blp=2          # blp stands for 'position of 1st line of body'
12929         [ -z "$5" ] || blp=3 # lnet.routes case
12930
12931         local l=$(cat "$TMP/lnet_$1" |wc -l)
12932         # subtracting one from $blp because the body can be empty
12933         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12934
12935         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12936                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12937
12938         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12939                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12940
12941         # bail out if any unexpected line happened
12942         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12943         [ "$?" != 0 ] || error "$2 misformatted"
12944 }
12945
12946 test_215() { # for bugs 18102, 21079, 21517
12947         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12948         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12949         local P='[1-9][0-9]*'           # positive numeric
12950         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12951         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12952         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12953         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12954
12955         local L1 # regexp for 1st line
12956         local L2 # regexp for 2nd line (optional)
12957         local BR # regexp for the rest (body)
12958
12959         # lnet.stats should look as 11 space-separated non-negative numerics
12960         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12961         create_lnet_proc_files "stats"
12962         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12963         remove_lnet_proc_files "stats"
12964
12965         # lnet.routes should look like this:
12966         # Routing disabled/enabled
12967         # net hops priority state router
12968         # where net is a string like tcp0, hops > 0, priority >= 0,
12969         # state is up/down,
12970         # router is a string like 192.168.1.1@tcp2
12971         L1="^Routing (disabled|enabled)$"
12972         L2="^net +hops +priority +state +router$"
12973         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12974         create_lnet_proc_files "routes"
12975         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12976         remove_lnet_proc_files "routes"
12977
12978         # lnet.routers should look like this:
12979         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12980         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12981         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12982         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12983         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12984         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12985         create_lnet_proc_files "routers"
12986         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12987         remove_lnet_proc_files "routers"
12988
12989         # lnet.peers should look like this:
12990         # nid refs state last max rtr min tx min queue
12991         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12992         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12993         # numeric (0 or >0 or <0), queue >= 0.
12994         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12995         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12996         create_lnet_proc_files "peers"
12997         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12998         remove_lnet_proc_files "peers"
12999
13000         # lnet.buffers  should look like this:
13001         # pages count credits min
13002         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
13003         L1="^pages +count +credits +min$"
13004         BR="^ +$N +$N +$I +$I$"
13005         create_lnet_proc_files "buffers"
13006         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
13007         remove_lnet_proc_files "buffers"
13008
13009         # lnet.nis should look like this:
13010         # nid status alive refs peer rtr max tx min
13011         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
13012         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
13013         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
13014         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
13015         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
13016         create_lnet_proc_files "nis"
13017         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
13018         remove_lnet_proc_files "nis"
13019
13020         # can we successfully write to lnet.stats?
13021         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
13022         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
13023 }
13024 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
13025
13026 test_216() { # bug 20317
13027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13028         remote_ost_nodsh && skip "remote OST with nodsh" && return
13029
13030         local node
13031         local facets=$(get_facets OST)
13032         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13033
13034         save_lustre_params client "osc.*.contention_seconds" > $p
13035         save_lustre_params $facets \
13036                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
13037         save_lustre_params $facets \
13038                 "ldlm.namespaces.filter-*.contended_locks" >> $p
13039         save_lustre_params $facets \
13040                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
13041         clear_stats osc.*.osc_stats
13042
13043         # agressive lockless i/o settings
13044         do_nodes $(comma_list $(osts_nodes)) \
13045                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
13046                         ldlm.namespaces.filter-*.contended_locks=0 \
13047                         ldlm.namespaces.filter-*.contention_seconds=60"
13048         lctl set_param -n osc.*.contention_seconds=60
13049
13050         $DIRECTIO write $DIR/$tfile 0 10 4096
13051         $CHECKSTAT -s 40960 $DIR/$tfile
13052
13053         # disable lockless i/o
13054         do_nodes $(comma_list $(osts_nodes)) \
13055                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
13056                         ldlm.namespaces.filter-*.contended_locks=32 \
13057                         ldlm.namespaces.filter-*.contention_seconds=0"
13058         lctl set_param -n osc.*.contention_seconds=0
13059         clear_stats osc.*.osc_stats
13060
13061         dd if=/dev/zero of=$DIR/$tfile count=0
13062         $CHECKSTAT -s 0 $DIR/$tfile
13063
13064         restore_lustre_params <$p
13065         rm -f $p
13066         rm $DIR/$tfile
13067 }
13068 run_test 216 "check lockless direct write updates file size and kms correctly"
13069
13070 test_217() { # bug 22430
13071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13072         local node
13073         local nid
13074
13075         for node in $(nodes_list); do
13076                 nid=$(host_nids_address $node $NETTYPE)
13077                 if [[ $nid = *-* ]] ; then
13078                         echo "lctl ping $(h2nettype $nid)"
13079                         lctl ping $(h2nettype $nid)
13080                 else
13081                         echo "skipping $node (no hyphen detected)"
13082                 fi
13083         done
13084 }
13085 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
13086
13087 test_218() {
13088        # do directio so as not to populate the page cache
13089        log "creating a 10 Mb file"
13090        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13091        log "starting reads"
13092        dd if=$DIR/$tfile of=/dev/null bs=4096 &
13093        log "truncating the file"
13094        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13095        log "killing dd"
13096        kill %+ || true # reads might have finished
13097        echo "wait until dd is finished"
13098        wait
13099        log "removing the temporary file"
13100        rm -rf $DIR/$tfile || error "tmp file removal failed"
13101 }
13102 run_test 218 "parallel read and truncate should not deadlock ======================="
13103
13104 test_219() {
13105         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13106         # write one partial page
13107         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
13108         # set no grant so vvp_io_commit_write will do sync write
13109         $LCTL set_param fail_loc=0x411
13110         # write a full page at the end of file
13111         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
13112
13113         $LCTL set_param fail_loc=0
13114         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
13115         $LCTL set_param fail_loc=0x411
13116         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
13117
13118         # LU-4201
13119         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
13120         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
13121 }
13122 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
13123
13124 test_220() { #LU-325
13125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13126         remote_ost_nodsh && skip "remote OST with nodsh" && return
13127         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13128         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13129         local OSTIDX=0
13130
13131         # create on MDT0000 so the last_id and next_id are correct
13132         mkdir $DIR/$tdir
13133         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
13134         OST=${OST%_UUID}
13135
13136         # on the mdt's osc
13137         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
13138         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
13139                         osc.$mdtosc_proc1.prealloc_last_id)
13140         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
13141                         osc.$mdtosc_proc1.prealloc_next_id)
13142
13143         $LFS df -i
13144
13145         if ! combined_mgs_mds ; then
13146                 mount_mgs_client
13147         fi
13148
13149         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
13150         #define OBD_FAIL_OST_ENOINO              0x229
13151         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
13152         create_pool $FSNAME.$TESTNAME || return 1
13153         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
13154
13155         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
13156
13157         MDSOBJS=$((last_id - next_id))
13158         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
13159
13160         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
13161         echo "OST still has $count kbytes free"
13162
13163         echo "create $MDSOBJS files @next_id..."
13164         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
13165
13166         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13167                         osc.$mdtosc_proc1.prealloc_last_id)
13168         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13169                         osc.$mdtosc_proc1.prealloc_next_id)
13170
13171         echo "after creation, last_id=$last_id2, next_id=$next_id2"
13172         $LFS df -i
13173
13174         echo "cleanup..."
13175
13176         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
13177         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
13178
13179         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
13180                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
13181         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
13182                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
13183         echo "unlink $MDSOBJS files @$next_id..."
13184         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
13185
13186         if ! combined_mgs_mds ; then
13187                 umount_mgs_client
13188         fi
13189 }
13190 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
13191
13192 test_221() {
13193         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13194         dd if=`which date` of=$MOUNT/date oflag=sync
13195         chmod +x $MOUNT/date
13196
13197         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
13198         $LCTL set_param fail_loc=0x80001401
13199
13200         $MOUNT/date > /dev/null
13201         rm -f $MOUNT/date
13202 }
13203 run_test 221 "make sure fault and truncate race to not cause OOM"
13204
13205 test_222a () {
13206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13207        rm -rf $DIR/$tdir
13208        test_mkdir -p $DIR/$tdir
13209        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
13210        createmany -o $DIR/$tdir/$tfile 10
13211        cancel_lru_locks mdc
13212        cancel_lru_locks osc
13213        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13214        $LCTL set_param fail_loc=0x31a
13215        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
13216        $LCTL set_param fail_loc=0
13217        rm -r $DIR/$tdir
13218 }
13219 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
13220
13221 test_222b () {
13222         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13223         rm -rf $DIR/$tdir
13224         test_mkdir -p $DIR/$tdir
13225         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
13226         createmany -o $DIR/$tdir/$tfile 10
13227         cancel_lru_locks mdc
13228         cancel_lru_locks osc
13229         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13230         $LCTL set_param fail_loc=0x31a
13231         rm -r $DIR/$tdir || error "AGL for rmdir failed"
13232         $LCTL set_param fail_loc=0
13233 }
13234 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
13235
13236 test_223 () {
13237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13238        rm -rf $DIR/$tdir
13239        test_mkdir -p $DIR/$tdir
13240        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
13241        createmany -o $DIR/$tdir/$tfile 10
13242        cancel_lru_locks mdc
13243        cancel_lru_locks osc
13244        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
13245        $LCTL set_param fail_loc=0x31b
13246        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
13247        $LCTL set_param fail_loc=0
13248        rm -r $DIR/$tdir
13249 }
13250 run_test 223 "osc reenqueue if without AGL lock granted ======================="
13251
13252 test_224a() { # LU-1039, MRP-303
13253         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13254         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
13255         $LCTL set_param fail_loc=0x508
13256         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
13257         $LCTL set_param fail_loc=0
13258         df $DIR
13259 }
13260 run_test 224a "Don't panic on bulk IO failure"
13261
13262 test_224b() { # LU-1039, MRP-303
13263         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13264         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
13265         cancel_lru_locks osc
13266         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
13267         $LCTL set_param fail_loc=0x515
13268         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
13269         $LCTL set_param fail_loc=0
13270         df $DIR
13271 }
13272 run_test 224b "Don't panic on bulk IO failure"
13273
13274 test_224c() { # LU-6441
13275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13276         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13277
13278         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13279         save_writethrough $p
13280         set_cache writethrough on
13281
13282         local pages_per_rpc=$($LCTL get_param \
13283                                 osc.*.max_pages_per_rpc)
13284         local at_max=$($LCTL get_param -n at_max)
13285         local timeout=$($LCTL get_param -n timeout)
13286         local test_at="$LCTL get_param -n at_max"
13287         local param_at="$FSNAME.sys.at_max"
13288         local test_timeout="$LCTL get_param -n timeout"
13289         local param_timeout="$FSNAME.sys.timeout"
13290
13291         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
13292
13293         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13294                 error "conf_param at_max=0 failed"
13295         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13296                 error "conf_param timeout=5 failed"
13297
13298         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13299         do_facet ost1 $LCTL set_param fail_loc=0x520
13300         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13301         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13302         sync
13303         do_facet ost1 $LCTL set_param fail_loc=0
13304
13305         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13306                 error "conf_param at_max=$at_max failed"
13307         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13308                 $timeout || error "conf_param timeout=$timeout failed"
13309
13310         $LCTL set_param -n $pages_per_rpc
13311         restore_lustre_params < $p
13312         rm -f $p
13313 }
13314 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13315
13316 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13317 test_225a () {
13318         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13319         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13320         if [ -z ${MDSSURVEY} ]; then
13321               skip_env "mds-survey not found" && return
13322         fi
13323
13324         [ $MDSCOUNT -ge 2 ] &&
13325                 skip "skipping now for more than one MDT" && return
13326
13327        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13328             { skip "Need MDS version at least 2.2.51"; return; }
13329
13330        local mds=$(facet_host $SINGLEMDS)
13331        local target=$(do_nodes $mds 'lctl dl' | \
13332                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13333
13334        local cmd1="file_count=1000 thrhi=4"
13335        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13336        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13337        local cmd="$cmd1 $cmd2 $cmd3"
13338
13339        rm -f ${TMP}/mds_survey*
13340        echo + $cmd
13341        eval $cmd || error "mds-survey with zero-stripe failed"
13342        cat ${TMP}/mds_survey*
13343        rm -f ${TMP}/mds_survey*
13344 }
13345 run_test 225a "Metadata survey sanity with zero-stripe"
13346
13347 test_225b () {
13348         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13349         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13350         if [ -z ${MDSSURVEY} ]; then
13351               skip_env "mds-survey not found" && return
13352         fi
13353         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13354             { skip "Need MDS version at least 2.2.51"; return; }
13355
13356         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13357               skip_env "Need to mount OST to test" && return
13358         fi
13359
13360        local mds=$(facet_host $SINGLEMDS)
13361        local target=$(do_nodes $mds 'lctl dl' | \
13362                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13363
13364        local cmd1="file_count=1000 thrhi=4"
13365        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13366        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13367        local cmd="$cmd1 $cmd2 $cmd3"
13368
13369        rm -f ${TMP}/mds_survey*
13370        echo + $cmd
13371        eval $cmd || error "mds-survey with stripe_count failed"
13372        cat ${TMP}/mds_survey*
13373        rm -f ${TMP}/mds_survey*
13374 }
13375 run_test 225b "Metadata survey sanity with stripe_count = 1"
13376
13377 mcreate_path2fid () {
13378         local mode=$1
13379         local major=$2
13380         local minor=$3
13381         local name=$4
13382         local desc=$5
13383         local path=$DIR/$tdir/$name
13384         local fid
13385         local rc
13386         local fid_path
13387
13388         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13389                 error "cannot create $desc"
13390
13391         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13392         rc=$?
13393         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13394
13395         fid_path=$($LFS fid2path $MOUNT $fid)
13396         rc=$?
13397         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13398
13399         [ "$path" == "$fid_path" ] ||
13400                 error "fid2path returned $fid_path, expected $path"
13401
13402         echo "pass with $path and $fid"
13403 }
13404
13405 test_226a () {
13406         rm -rf $DIR/$tdir
13407         mkdir -p $DIR/$tdir
13408
13409         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13410         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13411         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13412         mcreate_path2fid 0040666 0 0 dir "directory"
13413         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13414         mcreate_path2fid 0100666 0 0 file "regular file"
13415         mcreate_path2fid 0120666 0 0 link "symbolic link"
13416         mcreate_path2fid 0140666 0 0 sock "socket"
13417 }
13418 run_test 226a "call path2fid and fid2path on files of all type"
13419
13420 test_226b () {
13421         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13422         rm -rf $DIR/$tdir
13423         local MDTIDX=1
13424
13425         mkdir -p $DIR/$tdir
13426         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13427                 error "create remote directory failed"
13428         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13429         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13430                                 "character special file (null)"
13431         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13432                                 "character special file (no device)"
13433         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13434         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13435                                 "block special file (loop)"
13436         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13437         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13438         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13439 }
13440 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13441
13442 # LU-1299 Executing or running ldd on a truncated executable does not
13443 # cause an out-of-memory condition.
13444 test_227() {
13445         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13446         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13447         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13448         chmod +x $MOUNT/date
13449
13450         $MOUNT/date > /dev/null
13451         ldd $MOUNT/date > /dev/null
13452         rm -f $MOUNT/date
13453 }
13454 run_test 227 "running truncated executable does not cause OOM"
13455
13456 # LU-1512 try to reuse idle OI blocks
13457 test_228a() {
13458         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13459         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13460         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13461                 skip "ldiskfs only test" && return
13462
13463         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13464         local myDIR=$DIR/$tdir
13465
13466         mkdir -p $myDIR
13467         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13468         $LCTL set_param fail_loc=0x80001002
13469         createmany -o $myDIR/t- 10000
13470         $LCTL set_param fail_loc=0
13471         # The guard is current the largest FID holder
13472         touch $myDIR/guard
13473         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13474                     tr -d '[')
13475         local IDX=$(($SEQ % 64))
13476
13477         do_facet $SINGLEMDS sync
13478         # Make sure journal flushed.
13479         sleep 6
13480         local blk1=$(do_facet $SINGLEMDS \
13481                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13482                      grep Blockcount | awk '{print $4}')
13483
13484         # Remove old files, some OI blocks will become idle.
13485         unlinkmany $myDIR/t- 10000
13486         # Create new files, idle OI blocks should be reused.
13487         createmany -o $myDIR/t- 2000
13488         do_facet $SINGLEMDS sync
13489         # Make sure journal flushed.
13490         sleep 6
13491         local blk2=$(do_facet $SINGLEMDS \
13492                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13493                      grep Blockcount | awk '{print $4}')
13494
13495         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13496 }
13497 run_test 228a "try to reuse idle OI blocks"
13498
13499 test_228b() {
13500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13501         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13502         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13503                 skip "ldiskfs only test" && return
13504
13505         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13506         local myDIR=$DIR/$tdir
13507
13508         mkdir -p $myDIR
13509         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13510         $LCTL set_param fail_loc=0x80001002
13511         createmany -o $myDIR/t- 10000
13512         $LCTL set_param fail_loc=0
13513         # The guard is current the largest FID holder
13514         touch $myDIR/guard
13515         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13516                     tr -d '[')
13517         local IDX=$(($SEQ % 64))
13518
13519         do_facet $SINGLEMDS sync
13520         # Make sure journal flushed.
13521         sleep 6
13522         local blk1=$(do_facet $SINGLEMDS \
13523                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13524                      grep Blockcount | awk '{print $4}')
13525
13526         # Remove old files, some OI blocks will become idle.
13527         unlinkmany $myDIR/t- 10000
13528
13529         # stop the MDT
13530         stop $SINGLEMDS || error "Fail to stop MDT."
13531         # remount the MDT
13532         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13533
13534         df $MOUNT || error "Fail to df."
13535         # Create new files, idle OI blocks should be reused.
13536         createmany -o $myDIR/t- 2000
13537         do_facet $SINGLEMDS sync
13538         # Make sure journal flushed.
13539         sleep 6
13540         local blk2=$(do_facet $SINGLEMDS \
13541                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13542                      grep Blockcount | awk '{print $4}')
13543
13544         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13545 }
13546 run_test 228b "idle OI blocks can be reused after MDT restart"
13547
13548 #LU-1881
13549 test_228c() {
13550         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13551         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13552         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13553                 skip "ldiskfs only test" && return
13554
13555         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13556         local myDIR=$DIR/$tdir
13557
13558         mkdir -p $myDIR
13559         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13560         $LCTL set_param fail_loc=0x80001002
13561         # 20000 files can guarantee there are index nodes in the OI file
13562         createmany -o $myDIR/t- 20000
13563         $LCTL set_param fail_loc=0
13564         # The guard is current the largest FID holder
13565         touch $myDIR/guard
13566         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13567                     tr -d '[')
13568         local IDX=$(($SEQ % 64))
13569
13570         do_facet $SINGLEMDS sync
13571         # Make sure journal flushed.
13572         sleep 6
13573         local blk1=$(do_facet $SINGLEMDS \
13574                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13575                      grep Blockcount | awk '{print $4}')
13576
13577         # Remove old files, some OI blocks will become idle.
13578         unlinkmany $myDIR/t- 20000
13579         rm -f $myDIR/guard
13580         # The OI file should become empty now
13581
13582         # Create new files, idle OI blocks should be reused.
13583         createmany -o $myDIR/t- 2000
13584         do_facet $SINGLEMDS sync
13585         # Make sure journal flushed.
13586         sleep 6
13587         local blk2=$(do_facet $SINGLEMDS \
13588                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13589                      grep Blockcount | awk '{print $4}')
13590
13591         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13592 }
13593 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13594
13595 test_229() { # LU-2482, LU-3448
13596         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13597                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13598                 return
13599         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13600         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
13601
13602         rm -f $DIR/$tfile
13603
13604         # Create a file with a released layout and stripe count 2.
13605         $MULTIOP $DIR/$tfile H2c ||
13606                 error "failed to create file with released layout"
13607
13608         $GETSTRIPE -v $DIR/$tfile
13609
13610         local pattern=$($GETSTRIPE -L $DIR/$tfile)
13611         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
13612
13613         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
13614         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
13615         stat $DIR/$tfile || error "failed to stat released file"
13616
13617         chown $RUNAS_ID $DIR/$tfile ||
13618                 error "chown $RUNAS_ID $DIR/$tfile failed"
13619
13620         chgrp $RUNAS_ID $DIR/$tfile ||
13621                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
13622
13623         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
13624         rm $DIR/$tfile || error "failed to remove released file"
13625 }
13626 run_test 229 "getstripe/stat/rm/attr changes work on released files"
13627
13628 test_230a() {
13629         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13630         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13631         local MDTIDX=1
13632
13633         test_mkdir $DIR/$tdir
13634         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
13635         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
13636         [ $mdt_idx -ne 0 ] &&
13637                 error "create local directory on wrong MDT $mdt_idx"
13638
13639         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
13640                         error "create remote directory failed"
13641         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
13642         [ $mdt_idx -ne $MDTIDX ] &&
13643                 error "create remote directory on wrong MDT $mdt_idx"
13644
13645         createmany -o $DIR/$tdir/test_230/t- 10 ||
13646                 error "create files on remote directory failed"
13647         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
13648         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
13649         rm -r $DIR/$tdir || error "unlink remote directory failed"
13650 }
13651 run_test 230a "Create remote directory and files under the remote directory"
13652
13653 test_230b() {
13654         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13655         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13656         local MDTIDX=1
13657         local mdt_index
13658         local i
13659         local file
13660         local pid
13661         local stripe_count
13662         local migrate_dir=$DIR/$tdir/migrate_dir
13663         local other_dir=$DIR/$tdir/other_dir
13664
13665         test_mkdir $DIR/$tdir
13666         test_mkdir -i0 -c1 $migrate_dir
13667         test_mkdir -i0 -c1 $other_dir
13668         for ((i=0; i<10; i++)); do
13669                 mkdir -p $migrate_dir/dir_${i}
13670                 createmany -o $migrate_dir/dir_${i}/f 10 ||
13671                         error "create files under remote dir failed $i"
13672         done
13673
13674         cp /etc/passwd $migrate_dir/$tfile
13675         cp /etc/passwd $other_dir/$tfile
13676         chattr +SAD $migrate_dir
13677         chattr +SAD $migrate_dir/$tfile
13678
13679         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13680         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13681         local old_dir_mode=$(stat -c%f $migrate_dir)
13682         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
13683
13684         mkdir -p $migrate_dir/dir_default_stripe2
13685         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
13686         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
13687
13688         mkdir -p $other_dir
13689         ln $migrate_dir/$tfile $other_dir/luna
13690         ln $migrate_dir/$tfile $migrate_dir/sofia
13691         ln $other_dir/$tfile $migrate_dir/david
13692         ln -s $migrate_dir/$tfile $other_dir/zachary
13693         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
13694         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
13695
13696         $LFS migrate -m $MDTIDX $migrate_dir ||
13697                 error "fails on migrating remote dir to MDT1"
13698
13699         echo "migratate to MDT1, then checking.."
13700         for ((i = 0; i < 10; i++)); do
13701                 for file in $(find $migrate_dir/dir_${i}); do
13702                         mdt_index=$($LFS getstripe -M $file)
13703                         [ $mdt_index == $MDTIDX ] ||
13704                                 error "$file is not on MDT${MDTIDX}"
13705                 done
13706         done
13707
13708         # the multiple link file should still in MDT0
13709         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
13710         [ $mdt_index == 0 ] ||
13711                 error "$file is not on MDT${MDTIDX}"
13712
13713         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13714         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13715                 error " expect $old_dir_flag get $new_dir_flag"
13716
13717         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13718         [ "$old_file_flag" = "$new_file_flag" ] ||
13719                 error " expect $old_file_flag get $new_file_flag"
13720
13721         local new_dir_mode=$(stat -c%f $migrate_dir)
13722         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13723                 error "expect mode $old_dir_mode get $new_dir_mode"
13724
13725         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13726         [ "$old_file_mode" = "$new_file_mode" ] ||
13727                 error "expect mode $old_file_mode get $new_file_mode"
13728
13729         diff /etc/passwd $migrate_dir/$tfile ||
13730                 error "$tfile different after migration"
13731
13732         diff /etc/passwd $other_dir/luna ||
13733                 error "luna different after migration"
13734
13735         diff /etc/passwd $migrate_dir/sofia ||
13736                 error "sofia different after migration"
13737
13738         diff /etc/passwd $migrate_dir/david ||
13739                 error "david different after migration"
13740
13741         diff /etc/passwd $other_dir/zachary ||
13742                 error "zachary different after migration"
13743
13744         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13745                 error "${tfile}_ln different after migration"
13746
13747         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13748                 error "${tfile}_ln_other different after migration"
13749
13750         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
13751         [ $stripe_count = 2 ] ||
13752                 error "dir strpe_count $d != 2 after migration."
13753
13754         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
13755         [ $stripe_count = 2 ] ||
13756                 error "file strpe_count $d != 2 after migration."
13757
13758         #migrate back to MDT0
13759         MDTIDX=0
13760
13761         $LFS migrate -m $MDTIDX $migrate_dir ||
13762                 error "fails on migrating remote dir to MDT0"
13763
13764         echo "migrate back to MDT0, checking.."
13765         for file in $(find $migrate_dir); do
13766                 mdt_index=$($LFS getstripe -M $file)
13767                 [ $mdt_index == $MDTIDX ] ||
13768                         error "$file is not on MDT${MDTIDX}"
13769         done
13770
13771         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13772         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13773                 error " expect $old_dir_flag get $new_dir_flag"
13774
13775         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13776         [ "$old_file_flag" = "$new_file_flag" ] ||
13777                 error " expect $old_file_flag get $new_file_flag"
13778
13779         local new_dir_mode=$(stat -c%f $migrate_dir)
13780         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13781                 error "expect mode $old_dir_mode get $new_dir_mode"
13782
13783         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13784         [ "$old_file_mode" = "$new_file_mode" ] ||
13785                 error "expect mode $old_file_mode get $new_file_mode"
13786
13787         diff /etc/passwd ${migrate_dir}/$tfile ||
13788                 error "$tfile different after migration"
13789
13790         diff /etc/passwd ${other_dir}/luna ||
13791                 error "luna different after migration"
13792
13793         diff /etc/passwd ${migrate_dir}/sofia ||
13794                 error "sofia different after migration"
13795
13796         diff /etc/passwd ${other_dir}/zachary ||
13797                 error "zachary different after migration"
13798
13799         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13800                 error "${tfile}_ln different after migration"
13801
13802         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13803                 error "${tfile}_ln_other different after migration"
13804
13805         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
13806         [ $stripe_count = 2 ] ||
13807                 error "dir strpe_count $d != 2 after migration."
13808
13809         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
13810         [ $stripe_count = 2 ] ||
13811                 error "file strpe_count $d != 2 after migration."
13812
13813         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13814 }
13815 run_test 230b "migrate directory"
13816
13817 test_230c() {
13818         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13819         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13820         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13821         local MDTIDX=1
13822         local mdt_index
13823         local file
13824         local migrate_dir=$DIR/$tdir/migrate_dir
13825
13826         #If migrating directory fails in the middle, all entries of
13827         #the directory is still accessiable.
13828         test_mkdir $DIR/$tdir
13829         test_mkdir -i0 -c1 $migrate_dir
13830         stat $migrate_dir
13831         createmany -o $migrate_dir/f 10 ||
13832                 error "create files under ${migrate_dir} failed"
13833
13834         #failed after migrating 5 entries
13835         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
13836         do_facet mds1 lctl set_param fail_loc=0x20001801
13837         do_facet mds1 lctl  set_param fail_val=5
13838         local t=$(ls $migrate_dir | wc -l)
13839         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
13840                 error "migrate should fail after 5 entries"
13841
13842         mkdir $migrate_dir/dir &&
13843                 error "mkdir succeeds under migrating directory"
13844         touch $migrate_dir/file &&
13845                 error "touch file succeeds under migrating directory"
13846
13847         local u=$(ls $migrate_dir | wc -l)
13848         [ "$u" == "$t" ] || error "$u != $t during migration"
13849
13850         for file in $(find $migrate_dir); do
13851                 stat $file || error "stat $file failed"
13852         done
13853
13854         do_facet mds1 lctl set_param fail_loc=0
13855         do_facet mds1 lctl set_param fail_val=0
13856
13857         $LFS migrate -m $MDTIDX $migrate_dir ||
13858                 error "migrate open files should failed with open files"
13859
13860         echo "Finish migration, then checking.."
13861         for file in $(find $migrate_dir); do
13862                 mdt_index=$($LFS getstripe -M $file)
13863                 [ $mdt_index == $MDTIDX ] ||
13864                         error "$file is not on MDT${MDTIDX}"
13865         done
13866
13867         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13868 }
13869 run_test 230c "check directory accessiblity if migration is failed"
13870
13871 test_230d() {
13872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13873         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13874         local MDTIDX=1
13875         local mdt_index
13876         local migrate_dir=$DIR/$tdir/migrate_dir
13877         local i
13878         local j
13879
13880         test_mkdir $DIR/$tdir
13881         test_mkdir -i0 -c1 $migrate_dir
13882
13883         for ((i=0; i<100; i++)); do
13884                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
13885                 createmany -o $migrate_dir/dir_${i}/f 100 ||
13886                         error "create files under remote dir failed $i"
13887         done
13888
13889         $LFS migrate -m $MDTIDX $migrate_dir ||
13890                 error "migrate remote dir error"
13891
13892         echo "Finish migration, then checking.."
13893         for file in $(find $migrate_dir); do
13894                 mdt_index=$($LFS getstripe -M $file)
13895                 [ $mdt_index == $MDTIDX ] ||
13896                         error "$file is not on MDT${MDTIDX}"
13897         done
13898
13899         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13900 }
13901 run_test 230d "check migrate big directory"
13902
13903 test_230e() {
13904         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13905         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13906         local i
13907         local j
13908         local a_fid
13909         local b_fid
13910
13911         mkdir -p $DIR/$tdir
13912         mkdir $DIR/$tdir/migrate_dir
13913         mkdir $DIR/$tdir/other_dir
13914         touch $DIR/$tdir/migrate_dir/a
13915         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
13916         ls $DIR/$tdir/other_dir
13917
13918         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13919                 error "migrate dir fails"
13920
13921         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13922         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13923
13924         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13925         [ $mdt_index == 0 ] || error "a is not on MDT0"
13926
13927         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
13928                 error "migrate dir fails"
13929
13930         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
13931         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
13932
13933         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13934         [ $mdt_index == 1 ] || error "a is not on MDT1"
13935
13936         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
13937         [ $mdt_index == 1 ] || error "b is not on MDT1"
13938
13939         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13940         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
13941
13942         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
13943
13944         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13945 }
13946 run_test 230e "migrate mulitple local link files"
13947
13948 test_230f() {
13949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13950         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13951         local a_fid
13952         local ln_fid
13953
13954         mkdir -p $DIR/$tdir
13955         mkdir $DIR/$tdir/migrate_dir
13956         $LFS mkdir -i1 $DIR/$tdir/other_dir
13957         touch $DIR/$tdir/migrate_dir/a
13958         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13959         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13960         ls $DIR/$tdir/other_dir
13961
13962         # a should be migrated to MDT1, since no other links on MDT0
13963         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13964                 error "#1 migrate dir fails"
13965         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13966         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13967         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13968         [ $mdt_index == 1 ] || error "a is not on MDT1"
13969
13970         # a should stay on MDT1, because it is a mulitple link file
13971         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13972                 error "#2 migrate dir fails"
13973         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13974         [ $mdt_index == 1 ] || error "a is not on MDT1"
13975
13976         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13977                 error "#3 migrate dir fails"
13978
13979         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13980         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13981         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13982
13983         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13984         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13985
13986         # a should be migrated to MDT0, since no other links on MDT1
13987         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13988                 error "#4 migrate dir fails"
13989         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13990         [ $mdt_index == 0 ] || error "a is not on MDT0"
13991
13992         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13993 }
13994 run_test 230f "migrate mulitple remote link files"
13995
13996 test_230g() {
13997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13998         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13999
14000         mkdir -p $DIR/$tdir/migrate_dir
14001
14002         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
14003                 error "migrating dir to non-exist MDT succeeds"
14004         true
14005 }
14006 run_test 230g "migrate dir to non-exist MDT"
14007
14008 test_230h() {
14009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14010         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14011         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
14012                 skip "Need MDS version at least 2.7.64" && return
14013         local mdt_index
14014
14015         mkdir -p $DIR/$tdir/migrate_dir
14016
14017         $LFS migrate -m1 $DIR &&
14018                 error "migrating mountpoint1 should fail"
14019
14020         $LFS migrate -m1 $DIR/$tdir/.. &&
14021                 error "migrating mountpoint2 should fail"
14022
14023         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
14024                 error "migrating $tdir fail"
14025
14026         mdt_index=$($LFS getstripe -M $DIR/$tdir)
14027         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14028
14029         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14030         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14031
14032 }
14033 run_test 230h "migrate .. and root"
14034
14035 test_230i() {
14036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14037         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14038
14039         mkdir -p $DIR/$tdir/migrate_dir
14040
14041         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
14042                 error "migration fails with a tailing slash"
14043
14044         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
14045                 error "migration fails with two tailing slashes"
14046 }
14047 run_test 230i "lfs migrate -m tolerates trailing slashes"
14048
14049 test_231a()
14050 {
14051         # For simplicity this test assumes that max_pages_per_rpc
14052         # is the same across all OSCs
14053         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
14054         local bulk_size=$((max_pages * 4096))
14055         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
14056                                        head -n 1)
14057
14058         mkdir -p $DIR/$tdir
14059         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
14060                 error "failed to set stripe with -S ${brw_size}M option"
14061
14062         # clear the OSC stats
14063         $LCTL set_param osc.*.stats=0 &>/dev/null
14064         stop_writeback
14065
14066         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
14067         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
14068                 oflag=direct &>/dev/null || error "dd failed"
14069
14070         sync; sleep 1; sync # just to be safe
14071         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
14072         if [ x$nrpcs != "x1" ]; then
14073                 $LCTL get_param osc.*.stats
14074                 error "found $nrpcs ost_write RPCs, not 1 as expected"
14075         fi
14076
14077         start_writeback
14078         # Drop the OSC cache, otherwise we will read from it
14079         cancel_lru_locks osc
14080
14081         # clear the OSC stats
14082         $LCTL set_param osc.*.stats=0 &>/dev/null
14083
14084         # Client reads $bulk_size.
14085         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
14086                 iflag=direct &>/dev/null || error "dd failed"
14087
14088         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
14089         if [ x$nrpcs != "x1" ]; then
14090                 $LCTL get_param osc.*.stats
14091                 error "found $nrpcs ost_read RPCs, not 1 as expected"
14092         fi
14093 }
14094 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
14095
14096 test_231b() {
14097         mkdir -p $DIR/$tdir
14098         local i
14099         for i in {0..1023}; do
14100                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
14101                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
14102                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
14103         done
14104         sync
14105 }
14106 run_test 231b "must not assert on fully utilized OST request buffer"
14107
14108 test_232() {
14109         mkdir -p $DIR/$tdir
14110         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14111         $LCTL set_param fail_loc=0x31c
14112
14113         # ignore dd failure
14114         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
14115
14116         $LCTL set_param fail_loc=0
14117         umount_client $MOUNT || error "umount failed"
14118         mount_client $MOUNT || error "mount failed"
14119 }
14120 run_test 232 "failed lock should not block umount"
14121
14122 test_233a() {
14123         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
14124         { skip "Need MDS version at least 2.3.64"; return; }
14125         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14126
14127         local fid=$($LFS path2fid $MOUNT)
14128         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14129                 error "cannot access $MOUNT using its FID '$fid'"
14130 }
14131 run_test 233a "checking that OBF of the FS root succeeds"
14132
14133 test_233b() {
14134         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
14135         { skip "Need MDS version at least 2.5.90"; return; }
14136         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14137
14138         local fid=$($LFS path2fid $MOUNT/.lustre)
14139         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14140                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
14141
14142         fid=$($LFS path2fid $MOUNT/.lustre/fid)
14143         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14144                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
14145 }
14146 run_test 233b "checking that OBF of the FS .lustre succeeds"
14147
14148 test_234() {
14149         local p="$TMP/sanityN-$TESTNAME.parameters"
14150         save_lustre_params client "llite.*.xattr_cache" > $p
14151         lctl set_param llite.*.xattr_cache 1 ||
14152                 { skip "xattr cache is not supported"; return 0; }
14153
14154         mkdir -p $DIR/$tdir || error "mkdir failed"
14155         touch $DIR/$tdir/$tfile || error "touch failed"
14156         # OBD_FAIL_LLITE_XATTR_ENOMEM
14157         $LCTL set_param fail_loc=0x1405
14158         # output of the form: attr 2 4 44 3 fc13 x86_64
14159         V=($(IFS=".-" rpm -q attr))
14160         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
14161               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
14162                 # attr pre-2.4.44-7 had a bug with rc
14163                 # LU-3703 - SLES 11 and FC13 clients have older attr
14164                 getfattr -n user.attr $DIR/$tdir/$tfile &&
14165                         error "getfattr should have failed with ENOMEM"
14166         else
14167                 skip "LU-3703: attr version $(getfattr --version) too old"
14168         fi
14169         $LCTL set_param fail_loc=0x0
14170         rm -rf $DIR/$tdir
14171
14172         restore_lustre_params < $p
14173         rm -f $p
14174 }
14175 run_test 234 "xattr cache should not crash on ENOMEM"
14176
14177 test_235() {
14178         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
14179                 skip "Need MDS version at least 2.4.52" && return
14180         flock_deadlock $DIR/$tfile
14181         local RC=$?
14182         case $RC in
14183                 0)
14184                 ;;
14185                 124) error "process hangs on a deadlock"
14186                 ;;
14187                 *) error "error executing flock_deadlock $DIR/$tfile"
14188                 ;;
14189         esac
14190 }
14191 run_test 235 "LU-1715: flock deadlock detection does not work properly"
14192
14193 #LU-2935
14194 test_236() {
14195         check_swap_layouts_support && return 0
14196         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
14197
14198         local ref1=/etc/passwd
14199         local ref2=/etc/group
14200         local file1=$DIR/$tdir/f1
14201         local file2=$DIR/$tdir/f2
14202
14203         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
14204         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
14205         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
14206         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
14207         local fd=$(free_fd)
14208         local cmd="exec $fd<>$file2"
14209         eval $cmd
14210         rm $file2
14211         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
14212                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
14213         cmd="exec $fd>&-"
14214         eval $cmd
14215         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14216
14217         #cleanup
14218         rm -rf $DIR/$tdir
14219 }
14220 run_test 236 "Layout swap on open unlinked file"
14221
14222 # test to verify file handle related system calls
14223 # (name_to_handle_at/open_by_handle_at)
14224 # The new system calls are supported in glibc >= 2.14.
14225
14226 test_237() {
14227         echo "Test file_handle syscalls" > $DIR/$tfile ||
14228                 error "write failed"
14229         check_fhandle_syscalls $DIR/$tfile ||
14230                 error "check_fhandle_syscalls failed"
14231 }
14232 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
14233
14234 # LU-4659 linkea consistency
14235 test_238() {
14236         local server_version=$(lustre_version_code $SINGLEMDS)
14237
14238         [[ $server_version -gt $(version_code 2.5.57) ]] ||
14239                 [[ $server_version -gt $(version_code 2.5.1) &&
14240                    $server_version -lt $(version_code 2.5.50) ]] ||
14241                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
14242
14243         touch $DIR/$tfile
14244         ln $DIR/$tfile $DIR/$tfile.lnk
14245         touch $DIR/$tfile.new
14246         mv $DIR/$tfile.new $DIR/$tfile
14247         local fid1=$($LFS path2fid $DIR/$tfile)
14248         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
14249         local path1=$($LFS fid2path $FSNAME "$fid1")
14250         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
14251         local path2=$($LFS fid2path $FSNAME "$fid2")
14252         [ $tfile.lnk == $path2 ] ||
14253                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
14254         rm -f $DIR/$tfile*
14255 }
14256 run_test 238 "Verify linkea consistency"
14257
14258 test_239() {
14259         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
14260                 skip "Need MDS version at least 2.5.60" && return
14261         local list=$(comma_list $(mdts_nodes))
14262
14263         mkdir -p $DIR/$tdir
14264         createmany -o $DIR/$tdir/f- 5000
14265         unlinkmany $DIR/$tdir/f- 5000
14266         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.1) ] &&
14267                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
14268         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
14269                         osp.*MDT*.sync_in_flight" | calc_sum)
14270         [ "$changes" -eq 0 ] || error "$changes not synced"
14271 }
14272 run_test 239 "osp_sync test"
14273
14274 test_239a() { #LU-5297
14275         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14276         touch $DIR/$tfile
14277         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
14278         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
14279         chgrp $RUNAS_GID $DIR/$tfile
14280         wait_delete_completed
14281 }
14282 run_test 239a "process invalid osp sync record correctly"
14283
14284 test_239b() { #LU-5297
14285         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14286         touch $DIR/$tfile1
14287         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
14288         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
14289         chgrp $RUNAS_GID $DIR/$tfile1
14290         wait_delete_completed
14291         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14292         touch $DIR/$tfile2
14293         chgrp $RUNAS_GID $DIR/$tfile2
14294         wait_delete_completed
14295 }
14296 run_test 239b "process osp sync record with ENOMEM error correctly"
14297
14298 test_240() {
14299         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14300         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14301
14302         mkdir -p $DIR/$tdir
14303
14304         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14305                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14306         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14307                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14308
14309         umount_client $MOUNT || error "umount failed"
14310         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14311         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14312         mount_client $MOUNT || error "failed to mount client"
14313
14314         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14315         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14316 }
14317 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14318
14319 test_241_bio() {
14320         for LOOP in $(seq $1); do
14321                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14322                 cancel_lru_locks osc || true
14323         done
14324 }
14325
14326 test_241_dio() {
14327         for LOOP in $(seq $1); do
14328                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14329                                                 iflag=direct 2>/dev/null
14330         done
14331 }
14332
14333 test_241a() { # was test_241
14334         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14335         ls -la $DIR/$tfile
14336         cancel_lru_locks osc
14337         test_241_bio 1000 &
14338         PID=$!
14339         test_241_dio 1000
14340         wait $PID
14341 }
14342 run_test 241a "bio vs dio"
14343
14344 test_241b() {
14345         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14346         ls -la $DIR/$tfile
14347         test_241_dio 1000 &
14348         PID=$!
14349         test_241_dio 1000
14350         wait $PID
14351 }
14352 run_test 241b "dio vs dio"
14353
14354 test_242() {
14355         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14356         mkdir -p $DIR/$tdir
14357         touch $DIR/$tdir/$tfile
14358
14359         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14360         do_facet mds1 lctl set_param fail_loc=0x105
14361         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14362
14363         do_facet mds1 lctl set_param fail_loc=0
14364         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14365 }
14366 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14367
14368 test_243()
14369 {
14370         test_mkdir -p $DIR/$tdir
14371         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14372 }
14373 run_test 243 "various group lock tests"
14374
14375 test_244()
14376 {
14377         test_mkdir -p $DIR/$tdir
14378         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14379         sendfile_grouplock $DIR/$tdir/$tfile || \
14380                 error "sendfile+grouplock failed"
14381         rm -rf $DIR/$tdir
14382 }
14383 run_test 244 "sendfile with group lock tests"
14384
14385 test_245() {
14386         local flagname="multi_mod_rpcs"
14387         local connect_data_name="max_mod_rpcs"
14388         local out
14389
14390         # check if multiple modify RPCs flag is set
14391         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14392                 grep "connect_flags:")
14393         echo "$out"
14394
14395         echo "$out" | grep -qw $flagname
14396         if [ $? -ne 0 ]; then
14397                 echo "connect flag $flagname is not set"
14398                 return
14399         fi
14400
14401         # check if multiple modify RPCs data is set
14402         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14403         echo "$out"
14404
14405         echo "$out" | grep -qw $connect_data_name ||
14406                 error "import should have connect data $connect_data_name"
14407 }
14408 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14409
14410 test_246() { # LU-7371
14411         remote_ost_nodsh && skip "remote OST with nodsh" && return
14412         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14413                 skip "Need OST version >= 2.7.62" && return 0
14414         do_facet ost1 $LCTL set_param fail_val=4095
14415 #define OBD_FAIL_OST_READ_SIZE          0x234
14416         do_facet ost1 $LCTL set_param fail_loc=0x234
14417         $LFS setstripe $DIR/$tfile -i 0 -c 1
14418         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14419         cancel_lru_locks $FSNAME-OST0000
14420         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14421 }
14422 run_test 246 "Read file of size 4095 should return right length"
14423
14424 test_247a() {
14425         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14426                 grep -q subtree ||
14427                 { skip "Fileset feature is not supported"; return; }
14428
14429         local submount=${MOUNT}_$tdir
14430
14431         mkdir $MOUNT/$tdir
14432         mkdir -p $submount || error "mkdir $submount failed"
14433         FILESET="$FILESET/$tdir" mount_client $submount ||
14434                 error "mount $submount failed"
14435         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14436         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14437                 error "read $MOUNT/$tdir/$tfile failed"
14438         umount_client $submount || error "umount $submount failed"
14439         rmdir $submount
14440 }
14441 run_test 247a "mount subdir as fileset"
14442
14443 test_247b() {
14444         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14445                 { skip "Fileset feature is not supported"; return; }
14446
14447         local submount=${MOUNT}_$tdir
14448
14449         rm -rf $MOUNT/$tdir
14450         mkdir -p $submount || error "mkdir $submount failed"
14451         SKIP_FILESET=1
14452         FILESET="$FILESET/$tdir" mount_client $submount &&
14453                 error "mount $submount should fail"
14454         rmdir $submount
14455 }
14456 run_test 247b "mount subdir that dose not exist"
14457
14458 test_247c() {
14459         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14460                 { skip "Fileset feature is not supported"; return; }
14461
14462         local submount=${MOUNT}_$tdir
14463
14464         mkdir -p $MOUNT/$tdir/dir1
14465         mkdir -p $submount || error "mkdir $submount failed"
14466         FILESET="$FILESET/$tdir" mount_client $submount ||
14467                 error "mount $submount failed"
14468         local fid=$($LFS path2fid $MOUNT/)
14469         $LFS fid2path $submount $fid && error "fid2path should fail"
14470         umount_client $submount || error "umount $submount failed"
14471         rmdir $submount
14472 }
14473 run_test 247c "running fid2path outside root"
14474
14475 test_247d() {
14476         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14477                 { skip "Fileset feature is not supported"; return; }
14478
14479         local submount=${MOUNT}_$tdir
14480
14481         mkdir -p $MOUNT/$tdir/dir1
14482         mkdir -p $submount || error "mkdir $submount failed"
14483         FILESET="$FILESET/$tdir" mount_client $submount ||
14484                 error "mount $submount failed"
14485         local fid=$($LFS path2fid $submount/dir1)
14486         $LFS fid2path $submount $fid || error "fid2path should succeed"
14487         umount_client $submount || error "umount $submount failed"
14488         rmdir $submount
14489 }
14490 run_test 247d "running fid2path inside root"
14491
14492 # LU-8037
14493 test_247e() {
14494         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14495                 grep -q subtree ||
14496                 { skip "Fileset feature is not supported"; return; }
14497
14498         local submount=${MOUNT}_$tdir
14499
14500         mkdir $MOUNT/$tdir
14501         mkdir -p $submount || error "mkdir $submount failed"
14502         FILESET="$FILESET/.." mount_client $submount &&
14503                 error "mount $submount should fail"
14504         rmdir $submount
14505 }
14506 run_test 247e "mount .. as fileset"
14507
14508 test_248() {
14509         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14510         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14511
14512         # create a large file for fast read verification
14513         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14514
14515         # make sure the file is created correctly
14516         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14517                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14518
14519         echo "Test 1: verify that fast read is 4 times faster on cache read"
14520
14521         # small read with fast read enabled
14522         $LCTL set_param -n llite.*.fast_read=1
14523         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14524                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14525                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14526         # small read with fast read disabled
14527         $LCTL set_param -n llite.*.fast_read=0
14528         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14529                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14530                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14531
14532         # verify that fast read is 4 times faster for cache read
14533         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
14534                 error_not_in_vm "fast read was not 4 times faster: " \
14535                            "$t_fast vs $t_slow"
14536
14537         echo "Test 2: verify the performance between big and small read"
14538         $LCTL set_param -n llite.*.fast_read=1
14539
14540         # 1k non-cache read
14541         cancel_lru_locks osc
14542         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14543                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14544                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14545
14546         # 1M non-cache read
14547         cancel_lru_locks osc
14548         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14549                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14550                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14551
14552         # verify that big IO is not 4 times faster than small IO
14553         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
14554                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
14555
14556         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
14557         rm -f $DIR/$tfile
14558 }
14559 run_test 248 "fast read verification"
14560
14561 test_249() { # LU-7890
14562         rm -f $DIR/$tfile
14563         $SETSTRIPE -c 1 $DIR/$tfile
14564
14565         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
14566         skip "Need at least version 2.8.54"
14567
14568         # Offset 2T == 4k * 512M
14569         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
14570                 error "dd to 2T offset failed"
14571 }
14572 run_test 249 "Write above 2T file size"
14573
14574 test_250() {
14575         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
14576          && skip "no 16TB file size limit on ZFS" && return
14577
14578         $SETSTRIPE -c 1 $DIR/$tfile
14579         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
14580         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
14581         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
14582         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
14583                 conv=notrunc,fsync && error "append succeeded"
14584         return 0
14585 }
14586 run_test 250 "Write above 16T limit"
14587
14588 test_251() {
14589         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
14590
14591         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
14592         #Skip once - writing the first stripe will succeed
14593         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14594         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
14595                 error "short write happened"
14596
14597         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14598         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
14599                 error "short read happened"
14600
14601         rm -f $DIR/$tfile
14602 }
14603 run_test 251 "Handling short read and write correctly"
14604
14605 test_252() {
14606         local tgt
14607         local dev
14608         local out
14609         local uuid
14610         local num
14611         local gen
14612
14613         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14614         remote_ost_nodsh && skip "remote OST with nodsh" && return
14615         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
14616              "$(facet_fstype mds1)" != "ldiskfs" ]; then
14617                 skip "ldiskfs only test"
14618                 return
14619         fi
14620
14621         # check lr_reader on OST0000
14622         tgt=ost1
14623         dev=$(facet_device $tgt)
14624         out=$(do_facet $tgt $LR_READER $dev)
14625         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14626         echo "$out"
14627         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
14628         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
14629                 error "Invalid uuid returned by $LR_READER on target $tgt"
14630         echo -e "uuid returned by $LR_READER is '$uuid'\n"
14631
14632         # check lr_reader -c on MDT0000
14633         tgt=mds1
14634         dev=$(facet_device $tgt)
14635         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
14636                 echo "$LR_READER does not support additional options"
14637                 return 0
14638         fi
14639         out=$(do_facet $tgt $LR_READER -c $dev)
14640         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14641         echo "$out"
14642         num=$(echo "$out" | grep -c "mdtlov")
14643         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
14644                 error "Invalid number of mdtlov clients returned by $LR_READER"
14645         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
14646
14647         # check lr_reader -cr on MDT0000
14648         out=$(do_facet $tgt $LR_READER -cr $dev)
14649         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14650         echo "$out"
14651         echo "$out" | grep -q "^reply_data:$" ||
14652                 error "$LR_READER should have returned 'reply_data' section"
14653         num=$(echo "$out" | grep -c "client_generation")
14654         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
14655 }
14656 run_test 252 "check lr_reader tool"
14657
14658 test_253_fill_ost() {
14659         local size_mb #how many MB should we write to pass watermark
14660         local lwm=$3  #low watermark
14661         local free_10mb #10% of free space
14662
14663         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14664         size_mb=$((free_kb / 1024 - lwm))
14665         free_10mb=$((free_kb / 10240))
14666         #If 10% of free space cross low watermark use it
14667         if (( free_10mb > size_mb )); then
14668                 size_mb=$free_10mb
14669         else
14670                 #At least we need to store 1.1 of difference between
14671                 #free space and low watermark
14672                 size_mb=$((size_mb + size_mb / 10))
14673         fi
14674         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
14675                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
14676                          oflag=append conv=notrunc
14677         fi
14678
14679         sleep_maxage
14680
14681         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14682         echo "OST still has $((free_kb / 1024)) mbytes free"
14683 }
14684
14685 test_253() {
14686         local ostidx=0
14687         local rc=0
14688
14689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14690         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14691         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
14692
14693         local ost_name=$($LFS osts |
14694                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
14695         # on the mdt's osc
14696         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
14697         do_facet $SINGLEMDS $LCTL get_param -n \
14698                 osp.$mdtosc_proc1.reserved_mb_high ||
14699                 { skip  "remote MDS does not support reserved_mb_high" &&
14700                   return; }
14701
14702         rm -rf $DIR/$tdir
14703         wait_mds_ost_sync
14704         wait_delete_completed
14705         mkdir $DIR/$tdir
14706
14707         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
14708                         osp.$mdtosc_proc1.reserved_mb_high)
14709         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
14710                         osp.$mdtosc_proc1.reserved_mb_low)
14711         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
14712
14713         if ! combined_mgs_mds ; then
14714                 mount_mgs_client
14715         fi
14716         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
14717         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
14718                 error "Adding $ost_name to pool failed"
14719
14720         # Wait for client to see a OST at pool
14721         wait_update $HOSTNAME "$LCTL get_param -n
14722                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
14723                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
14724                 error "Client can not see the pool"
14725         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
14726                 error "Setstripe failed"
14727
14728         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
14729         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
14730         echo "OST still has $((blocks/1024)) mbytes free"
14731
14732         local new_lwm=$((blocks/1024-10))
14733         do_facet $SINGLEMDS $LCTL set_param \
14734                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
14735         do_facet $SINGLEMDS $LCTL set_param \
14736                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
14737
14738         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14739
14740         #First enospc could execute orphan deletion so repeat.
14741         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14742
14743         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14744                         osp.$mdtosc_proc1.prealloc_status)
14745         echo "prealloc_status $oa_status"
14746
14747         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
14748                 error "File creation should fail"
14749         #object allocation was stopped, but we still able to append files
14750         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
14751                 error "Append failed"
14752         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
14753
14754         wait_delete_completed
14755
14756         sleep_maxage
14757
14758         for i in $(seq 10 12); do
14759                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
14760                         error "File creation failed after rm";
14761         done
14762
14763         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14764                         osp.$mdtosc_proc1.prealloc_status)
14765         echo "prealloc_status $oa_status"
14766
14767         if (( oa_status != 0 )); then
14768                 error "Object allocation still disable after rm"
14769         fi
14770         do_facet $SINGLEMDS $LCTL set_param \
14771                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
14772         do_facet $SINGLEMDS $LCTL set_param \
14773                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
14774
14775
14776         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
14777                 error "Remove $ost_name from pool failed"
14778         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14779                 error "Pool destroy fialed"
14780
14781         if ! combined_mgs_mds ; then
14782                 umount_mgs_client
14783         fi
14784 }
14785 run_test 253 "Check object allocation limit"
14786
14787 test_254() {
14788         local cl_user
14789
14790         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14791         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14792         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
14793                 { skip "MDS does not support changelog_size" && return; }
14794
14795         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14796         echo "Registered as changelog user $cl_user"
14797
14798         $LFS changelog_clear $MDT0 $cl_user 0
14799
14800         local size1=$(do_facet mds1 \
14801                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14802         echo "Changelog size $size1"
14803
14804         rm -rf $DIR/$tdir
14805         $LFS mkdir -i 0 $DIR/$tdir
14806         # change something
14807         mkdir -p $DIR/$tdir/pics/2008/zachy
14808         touch $DIR/$tdir/pics/2008/zachy/timestamp
14809         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
14810         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14811         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14812         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14813         rm $DIR/$tdir/pics/desktop.jpg
14814
14815         local size2=$(do_facet mds1 \
14816                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14817         echo "Changelog size after work $size2"
14818
14819         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14820
14821         if (( size2 <= size1 )); then
14822                 error "Changelog size after work should be greater than original"
14823         fi
14824         return 0
14825 }
14826 run_test 254 "Check changelog size"
14827
14828 ladvise_no_type()
14829 {
14830         local type=$1
14831         local file=$2
14832
14833         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
14834                 awk -F: '{print $2}' | grep $type > /dev/null
14835         if [ $? -ne 0 ]; then
14836                 return 0
14837         fi
14838         return 1
14839 }
14840
14841 ladvise_no_ioctl()
14842 {
14843         local file=$1
14844
14845         lfs ladvise -a willread $file > /dev/null 2>&1
14846         if [ $? -eq 0 ]; then
14847                 return 1
14848         fi
14849
14850         lfs ladvise -a willread $file 2>&1 |
14851                 grep "Inappropriate ioctl for device" > /dev/null
14852         if [ $? -eq 0 ]; then
14853                 return 0
14854         fi
14855         return 1
14856 }
14857
14858 percent() {
14859         bc <<<"scale=2; ($1 - $2) * 100 / $2"
14860 }
14861
14862 # run a random read IO workload
14863 # usage: random_read_iops <filename> <filesize> <iosize>
14864 random_read_iops() {
14865         local file=$1
14866         local fsize=$2
14867         local iosize=${3:-4096}
14868
14869         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
14870                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
14871 }
14872
14873 drop_file_oss_cache() {
14874         local file="$1"
14875         local nodes="$2"
14876
14877         $LFS ladvise -a dontneed $file 2>/dev/null ||
14878                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
14879 }
14880
14881 ladvise_willread_performance()
14882 {
14883         local repeat=10
14884         local average_origin=0
14885         local average_cache=0
14886         local average_ladvise=0
14887
14888         for ((i = 1; i <= $repeat; i++)); do
14889                 echo "Iter $i/$repeat: reading without willread hint"
14890                 cancel_lru_locks osc
14891                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14892                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
14893                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
14894                 average_origin=$(bc <<<"$average_origin + $speed_origin")
14895
14896                 cancel_lru_locks osc
14897                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
14898                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
14899                 average_cache=$(bc <<<"$average_cache + $speed_cache")
14900
14901                 cancel_lru_locks osc
14902                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14903                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
14904                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
14905                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
14906                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
14907         done
14908         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
14909         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
14910         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
14911
14912         speedup_cache=$(percent $average_cache $average_origin)
14913         speedup_ladvise=$(percent $average_ladvise $average_origin)
14914
14915         echo "Average uncached read: $average_origin"
14916         echo "Average speedup with OSS cached read: " \
14917                 "$average_cache = +$speedup_cache%"
14918         echo "Average speedup with ladvise willread: " \
14919                 "$average_ladvise = +$speedup_ladvise%"
14920
14921         local lowest_speedup=20
14922         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
14923                 echo "Speedup with OSS cached read less than $lowest_speedup%, " \
14924                         "got $average_cache%. Skipping ladvise willread check."
14925                 return 0
14926         fi
14927
14928         # the test won't work on ZFS until it supports 'ladvise dontneed', but
14929         # it is still good to run until then to exercise 'ladvise willread'
14930         ! $LFS ladvise -a dontneed $DIR/$tfile &&
14931                 [ "$(facet_fstype ost1)" = "zfs" ] &&
14932                 echo "osd-zfs does not support dontneed or drop_caches" &&
14933                 return 0
14934
14935         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
14936         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
14937                 error_not_in_vm "Speedup with willread is less than " \
14938                         "$lowest_speedup%, got $average_ladvise%"
14939 }
14940
14941 test_255a() {
14942         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14943                 skip "lustre < 2.8.54 does not support ladvise " && return
14944         remote_ost_nodsh && skip "remote OST with nodsh" && return
14945
14946         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
14947
14948         ladvise_no_type willread $DIR/$tfile &&
14949                 skip "willread ladvise is not supported" && return
14950
14951         ladvise_no_ioctl $DIR/$tfile &&
14952                 skip "ladvise ioctl is not supported" && return
14953
14954         local size_mb=100
14955         local size=$((size_mb * 1048576))
14956         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14957                 error "dd to $DIR/$tfile failed"
14958
14959         lfs ladvise -a willread $DIR/$tfile ||
14960                 error "Ladvise failed with no range argument"
14961
14962         lfs ladvise -a willread -s 0 $DIR/$tfile ||
14963                 error "Ladvise failed with no -l or -e argument"
14964
14965         lfs ladvise -a willread -e 1 $DIR/$tfile ||
14966                 error "Ladvise failed with only -e argument"
14967
14968         lfs ladvise -a willread -l 1 $DIR/$tfile ||
14969                 error "Ladvise failed with only -l argument"
14970
14971         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
14972                 error "End offset should not be smaller than start offset"
14973
14974         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
14975                 error "End offset should not be equal to start offset"
14976
14977         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
14978                 error "Ladvise failed with overflowing -s argument"
14979
14980         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
14981                 error "Ladvise failed with overflowing -e argument"
14982
14983         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
14984                 error "Ladvise failed with overflowing -l argument"
14985
14986         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
14987                 error "Ladvise succeeded with conflicting -l and -e arguments"
14988
14989         echo "Synchronous ladvise should wait"
14990         local delay=4
14991 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
14992         do_nodes $(comma_list $(osts_nodes)) \
14993                 $LCTL set_param fail_val=$delay fail_loc=0x237
14994
14995         local start_ts=$SECONDS
14996         lfs ladvise -a willread $DIR/$tfile ||
14997                 error "Ladvise failed with no range argument"
14998         local end_ts=$SECONDS
14999         local inteval_ts=$((end_ts - start_ts))
15000
15001         if [ $inteval_ts -lt $(($delay - 1)) ]; then
15002                 error "Synchronous advice didn't wait reply"
15003         fi
15004
15005         echo "Asynchronous ladvise shouldn't wait"
15006         local start_ts=$SECONDS
15007         lfs ladvise -a willread -b $DIR/$tfile ||
15008                 error "Ladvise failed with no range argument"
15009         local end_ts=$SECONDS
15010         local inteval_ts=$((end_ts - start_ts))
15011
15012         if [ $inteval_ts -gt $(($delay / 2)) ]; then
15013                 error "Asynchronous advice blocked"
15014         fi
15015
15016         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
15017         ladvise_willread_performance
15018 }
15019 run_test 255a "check 'lfs ladvise -a willread'"
15020
15021 facet_meminfo() {
15022         local facet=$1
15023         local info=$2
15024
15025         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
15026 }
15027
15028 test_255b() {
15029         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15030                 skip "lustre < 2.8.54 does not support ladvise " && return
15031         remote_ost_nodsh && skip "remote OST with nodsh" && return
15032
15033         lfs setstripe -c 1 -i 0 $DIR/$tfile
15034
15035         ladvise_no_type dontneed $DIR/$tfile &&
15036                 skip "dontneed ladvise is not supported" && return
15037
15038         ladvise_no_ioctl $DIR/$tfile &&
15039                 skip "ladvise ioctl is not supported" && return
15040
15041         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15042                 skip "lustre < 2.8.54 does not support ladvise" && return
15043
15044         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15045                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15046                 skip "zfs-osd does not support 'ladvise dontneed'" && return
15047
15048         local size_mb=100
15049         local size=$((size_mb * 1048576))
15050         # In order to prevent disturbance of other processes, only check 3/4
15051         # of the memory usage
15052         local kibibytes=$((size_mb * 1024 * 3 / 4))
15053
15054         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15055                 error "dd to $DIR/$tfile failed"
15056
15057         local total=$(facet_meminfo ost1 MemTotal)
15058         echo "Total memory: $total KiB"
15059
15060         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
15061         local before_read=$(facet_meminfo ost1 Cached)
15062         echo "Cache used before read: $before_read KiB"
15063
15064         lfs ladvise -a willread $DIR/$tfile ||
15065                 error "Ladvise willread failed"
15066         local after_read=$(facet_meminfo ost1 Cached)
15067         echo "Cache used after read: $after_read KiB"
15068
15069         lfs ladvise -a dontneed $DIR/$tfile ||
15070                 error "Ladvise dontneed again failed"
15071         local no_read=$(facet_meminfo ost1 Cached)
15072         echo "Cache used after dontneed ladvise: $no_read KiB"
15073
15074         if [ $total -lt $((before_read + kibibytes)) ]; then
15075                 echo "Memory is too small, abort checking"
15076                 return 0
15077         fi
15078
15079         if [ $((before_read + kibibytes)) -gt $after_read ]; then
15080                 error "Ladvise willread should use more memory" \
15081                         "than $kibibytes KiB"
15082         fi
15083
15084         if [ $((no_read + kibibytes)) -gt $after_read ]; then
15085                 error "Ladvise dontneed should release more memory" \
15086                         "than $kibibytes KiB"
15087         fi
15088 }
15089 run_test 255b "check 'lfs ladvise -a dontneed'"
15090
15091 test_256() {
15092         local cl_user
15093         local cat_sl
15094         local mdt_dev
15095
15096         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15097         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15098         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
15099                 skip "ldiskfs only test" && return
15100
15101         mdt_dev=$(mdsdevname 1)
15102         echo $mdt_dev
15103         cl_user=$(do_facet mds1 \
15104         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
15105         if [[ -n $cl_user ]]; then
15106                 skip "active changelog user"
15107                 return
15108         fi
15109
15110         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15111         echo "Registered as changelog user $cl_user"
15112
15113         rm -rf $DIR/$tdir
15114         mkdir -p $DIR/$tdir
15115
15116         $LFS changelog_clear $MDT0 $cl_user 0
15117
15118         # change something
15119         touch $DIR/$tdir/{1..10}
15120
15121         # stop the MDT
15122         stop mds1 || error "Fail to stop MDT."
15123
15124         # remount the MDT
15125         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
15126
15127         #after mount new plainllog is used
15128         touch $DIR/$tdir/{11..19}
15129         local TEMP256FILE=$(mktemp TEMP256XXXXXX)
15130         cat_sl=$(do_facet mds1 \
15131         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15132          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15133         rm $TEMP256FILE
15134
15135         if (( cat_sl != 2 )); then
15136                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15137                 error "Changelog catalog has wrong number of slots $cat_sl"
15138         fi
15139
15140         $LFS changelog_clear $MDT0 $cl_user 0
15141
15142         TEMP256FILE=$(mktemp TEMP256XXXXXX)
15143         cat_sl=$(do_facet mds1 \
15144         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15145          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15146         rm $TEMP256FILE
15147
15148         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15149
15150         if (( cat_sl == 2 )); then
15151                 error "Empty plain llog was not deleted from changelog catalog"
15152         fi
15153         if (( cat_sl != 1 )); then
15154                 error "Active plain llog shouldn\`t be deleted from catalog"
15155         fi
15156 }
15157 run_test 256 "Check llog delete for empty and not full state"
15158
15159 test_257() {
15160         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15161         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15162                 skip "Need MDS version at least 2.8.55" && return
15163
15164         test_mkdir -p $DIR/$tdir
15165
15166         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
15167                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
15168         stat $DIR/$tdir
15169
15170 #define OBD_FAIL_MDS_XATTR_REP                  0x161
15171         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15172         local facet=mds$((mdtidx + 1))
15173         set_nodes_failloc $(facet_active_host $facet) 0x80000161
15174         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
15175
15176         stop $facet || error "stop MDS failed"
15177         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
15178                 error "start MDS fail"
15179 }
15180 run_test 257 "xattr locks are not lost"
15181
15182 # Verify we take the i_mutex when security requires it
15183 test_258a() {
15184 #define OBD_FAIL_IMUTEX_SEC 0x141c
15185         $LCTL set_param fail_loc=0x141c
15186         touch $DIR/$tfile
15187         chmod u+s $DIR/$tfile
15188         chmod a+rwx $DIR/$tfile
15189         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15190         RC=$?
15191         if [ $RC -ne 0 ]; then
15192                 error "error, failed to take i_mutex, rc=$?"
15193         fi
15194         rm -f $DIR/$tfile
15195 }
15196 run_test 258a
15197
15198 # Verify we do NOT take the i_mutex in the normal case
15199 test_258b() {
15200 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
15201         $LCTL set_param fail_loc=0x141d
15202         touch $DIR/$tfile
15203         chmod a+rwx $DIR
15204         chmod a+rw $DIR/$tfile
15205         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15206         RC=$?
15207         if [ $RC -ne 0 ]; then
15208                 error "error, took i_mutex unnecessarily, rc=$?"
15209         fi
15210         rm -f $DIR/$tfile
15211
15212 }
15213 run_test 258b "verify i_mutex security behavior"
15214
15215 test_260() {
15216 #define OBD_FAIL_MDC_CLOSE               0x806
15217         $LCTL set_param fail_loc=0x80000806
15218         touch $DIR/$tfile
15219
15220 }
15221 run_test 260 "Check mdc_close fail"
15222
15223 test_275() {
15224         remote_ost_nodsh && skip "remote OST with nodsh" && return
15225         [ $(lustre_version_code ost1) -lt $(version_code 2.10.1) ] &&
15226                 skip "Need OST version >= 2.10.1" && return 0
15227
15228         local file=$DIR/$tfile
15229         local oss
15230
15231         oss=$(comma_list $(osts_nodes))
15232
15233         dd if=/dev/urandom of=$file bs=1M count=2 ||
15234                 error "failed to create a file"
15235         cancel_lru_locks osc
15236
15237         #lock 1
15238         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
15239                 error "failed to read a file"
15240
15241 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
15242         $LCTL set_param fail_loc=0x8000031f
15243
15244         cancel_lru_locks osc &
15245         sleep 1
15246
15247 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
15248         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
15249         #IO takes another lock, but matches the PENDING one
15250         #and places it to the IO RPC
15251         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
15252                 error "failed to read a file with PENDING lock"
15253 }
15254 run_test 275 "Read on a canceled duplicate lock"
15255
15256 test_276() {
15257         remote_ost_nodsh && skip "remote OST with nodsh" && return
15258         local pid
15259
15260         do_facet ost1 "(while true; do \
15261                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
15262                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
15263         pid=$!
15264
15265         for LOOP in $(seq 20); do
15266                 stop ost1
15267                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
15268         done
15269         kill -9 $pid
15270         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
15271                 rm $TMP/sanity_276_pid"
15272 }
15273 run_test 276 "Race between mount and obd_statfs"
15274
15275 cleanup_test_300() {
15276         trap 0
15277         umask $SAVE_UMASK
15278 }
15279 test_striped_dir() {
15280         local mdt_index=$1
15281         local stripe_count
15282         local stripe_index
15283
15284         mkdir -p $DIR/$tdir
15285
15286         SAVE_UMASK=$(umask)
15287         trap cleanup_test_300 RETURN EXIT
15288
15289         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
15290                                                 $DIR/$tdir/striped_dir ||
15291                 error "set striped dir error"
15292
15293         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
15294         [ "$mode" = "755" ] || error "expect 755 got $mode"
15295
15296         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
15297                 error "getdirstripe failed"
15298         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
15299         if [ "$stripe_count" != "2" ]; then
15300                 error "1:stripe_count is $stripe_count, expect 2"
15301         fi
15302         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
15303         if [ "$stripe_count" != "2" ]; then
15304                 error "2:stripe_count is $stripe_count, expect 2"
15305         fi
15306
15307         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
15308         if [ "$stripe_index" != "$mdt_index" ]; then
15309                 error "stripe_index is $stripe_index, expect $mdt_index"
15310         fi
15311
15312         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15313                 error "nlink error after create striped dir"
15314
15315         mkdir $DIR/$tdir/striped_dir/a
15316         mkdir $DIR/$tdir/striped_dir/b
15317
15318         stat $DIR/$tdir/striped_dir/a ||
15319                 error "create dir under striped dir failed"
15320         stat $DIR/$tdir/striped_dir/b ||
15321                 error "create dir under striped dir failed"
15322
15323         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
15324                 error "nlink error after mkdir"
15325
15326         rmdir $DIR/$tdir/striped_dir/a
15327         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
15328                 error "nlink error after rmdir"
15329
15330         rmdir $DIR/$tdir/striped_dir/b
15331         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15332                 error "nlink error after rmdir"
15333
15334         chattr +i $DIR/$tdir/striped_dir
15335         createmany -o $DIR/$tdir/striped_dir/f 10 &&
15336                 error "immutable flags not working under striped dir!"
15337         chattr -i $DIR/$tdir/striped_dir
15338
15339         rmdir $DIR/$tdir/striped_dir ||
15340                 error "rmdir striped dir error"
15341
15342         cleanup_test_300
15343
15344         true
15345 }
15346
15347 test_300a() {
15348         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15349                 skip "skipped for lustre < 2.7.0" && return
15350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15351         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15352
15353         test_striped_dir 0 || error "failed on striped dir on MDT0"
15354         test_striped_dir 1 || error "failed on striped dir on MDT0"
15355 }
15356 run_test 300a "basic striped dir sanity test"
15357
15358 test_300b() {
15359         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15360                 skip "skipped for lustre < 2.7.0" && return
15361         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15362         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15363         local i
15364         local mtime1
15365         local mtime2
15366         local mtime3
15367
15368         test_mkdir $DIR/$tdir || error "mkdir fail"
15369         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15370                 error "set striped dir error"
15371         for ((i=0; i<10; i++)); do
15372                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
15373                 sleep 1
15374                 touch $DIR/$tdir/striped_dir/file_$i ||
15375                                         error "touch error $i"
15376                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
15377                 [ $mtime1 -eq $mtime2 ] &&
15378                         error "mtime not change after create"
15379                 sleep 1
15380                 rm -f $DIR/$tdir/striped_dir/file_$i ||
15381                                         error "unlink error $i"
15382                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
15383                 [ $mtime2 -eq $mtime3 ] &&
15384                         error "mtime did not change after unlink"
15385         done
15386         true
15387 }
15388 run_test 300b "check ctime/mtime for striped dir"
15389
15390 test_300c() {
15391         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15392                 skip "skipped for lustre < 2.7.0" && return
15393         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15394         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15395         local file_count
15396
15397         mkdir -p $DIR/$tdir
15398         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
15399                 error "set striped dir error"
15400
15401         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
15402                 error "chown striped dir failed"
15403
15404         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
15405                 error "create 5k files failed"
15406
15407         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
15408
15409         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
15410
15411         rm -rf $DIR/$tdir
15412 }
15413 run_test 300c "chown && check ls under striped directory"
15414
15415 test_300d() {
15416         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15417                 skip "skipped for lustre < 2.7.0" && return
15418         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15419         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15420         local stripe_count
15421         local file
15422
15423         mkdir -p $DIR/$tdir
15424         $SETSTRIPE -c 2 $DIR/$tdir
15425
15426         #local striped directory
15427         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15428                 error "set striped dir error"
15429         createmany -o $DIR/$tdir/striped_dir/f 10 ||
15430                 error "create 10 files failed"
15431
15432         #remote striped directory
15433         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
15434                 error "set striped dir error"
15435         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
15436                 error "create 10 files failed"
15437
15438         for file in $(find $DIR/$tdir); do
15439                 stripe_count=$($GETSTRIPE -c $file)
15440                 [ $stripe_count -eq 2 ] ||
15441                         error "wrong stripe $stripe_count for $file"
15442         done
15443
15444         rm -rf $DIR/$tdir
15445 }
15446 run_test 300d "check default stripe under striped directory"
15447
15448 test_300e() {
15449         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15450                 skip "Need MDS version at least 2.7.55" && return
15451         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15452         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15453         local stripe_count
15454         local file
15455
15456         mkdir -p $DIR/$tdir
15457
15458         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15459                 error "set striped dir error"
15460
15461         touch $DIR/$tdir/striped_dir/a
15462         touch $DIR/$tdir/striped_dir/b
15463         touch $DIR/$tdir/striped_dir/c
15464
15465         mkdir $DIR/$tdir/striped_dir/dir_a
15466         mkdir $DIR/$tdir/striped_dir/dir_b
15467         mkdir $DIR/$tdir/striped_dir/dir_c
15468
15469         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
15470                 error "set striped adir under striped dir error"
15471
15472         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
15473                 error "set striped bdir under striped dir error"
15474
15475         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
15476                 error "set striped cdir under striped dir error"
15477
15478         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
15479                 error "rename dir under striped dir fails"
15480
15481         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
15482                 error "rename dir under different stripes fails"
15483
15484         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
15485                 error "rename file under striped dir should succeed"
15486
15487         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
15488                 error "rename dir under striped dir should succeed"
15489
15490         rm -rf $DIR/$tdir
15491 }
15492 run_test 300e "check rename under striped directory"
15493
15494 test_300f() {
15495         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15496                 skip "Need MDS version at least 2.7.55" && return
15497         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15498         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15499         local stripe_count
15500         local file
15501
15502         rm -rf $DIR/$tdir
15503         mkdir -p $DIR/$tdir
15504
15505         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15506                 error "set striped dir error"
15507
15508         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
15509                 error "set striped dir error"
15510
15511         touch $DIR/$tdir/striped_dir/a
15512         mkdir $DIR/$tdir/striped_dir/dir_a
15513         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
15514                 error "create striped dir under striped dir fails"
15515
15516         touch $DIR/$tdir/striped_dir1/b
15517         mkdir $DIR/$tdir/striped_dir1/dir_b
15518         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
15519                 error "create striped dir under striped dir fails"
15520
15521         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
15522                 error "rename dir under different striped dir should fail"
15523
15524         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
15525                 error "rename striped dir under diff striped dir should fail"
15526
15527         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
15528                 error "rename file under diff striped dirs fails"
15529
15530         rm -rf $DIR/$tdir
15531 }
15532 run_test 300f "check rename cross striped directory"
15533
15534 test_300_check_default_striped_dir()
15535 {
15536         local dirname=$1
15537         local default_count=$2
15538         local default_index=$3
15539         local stripe_count
15540         local stripe_index
15541         local dir_stripe_index
15542         local dir
15543
15544         echo "checking $dirname $default_count $default_index"
15545         $LFS setdirstripe -D -c $default_count -i $default_index \
15546                                 -t all_char $DIR/$tdir/$dirname ||
15547                 error "set default stripe on striped dir error"
15548         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
15549         [ $stripe_count -eq $default_count ] ||
15550                 error "expect $default_count get $stripe_count for $dirname"
15551
15552         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
15553         [ $stripe_index -eq $default_index ] ||
15554                 error "expect $default_index get $stripe_index for $dirname"
15555
15556         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
15557                                                 error "create dirs failed"
15558
15559         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
15560         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
15561         for dir in $(find $DIR/$tdir/$dirname/*); do
15562                 stripe_count=$($LFS getdirstripe -c $dir)
15563                 [ $stripe_count -eq $default_count ] ||
15564                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
15565                 error "stripe count $default_count != $stripe_count for $dir"
15566
15567                 stripe_index=$($LFS getdirstripe -i $dir)
15568                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
15569                         error "$stripe_index != $default_index for $dir"
15570
15571                 #check default stripe
15572                 stripe_count=$($LFS getdirstripe -D -c $dir)
15573                 [ $stripe_count -eq $default_count ] ||
15574                 error "default count $default_count != $stripe_count for $dir"
15575
15576                 stripe_index=$($LFS getdirstripe -D -i $dir)
15577                 [ $stripe_index -eq $default_index ] ||
15578                 error "default index $default_index != $stripe_index for $dir"
15579         done
15580         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
15581 }
15582
15583 test_300g() {
15584         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15585                 skip "Need MDS version at least 2.7.55" && return
15586         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15587         local dir
15588         local stripe_count
15589         local stripe_index
15590
15591         mkdir $DIR/$tdir
15592         mkdir $DIR/$tdir/normal_dir
15593
15594         #Checking when client cache stripe index
15595         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
15596         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
15597                 error "create striped_dir failed"
15598
15599         mkdir $DIR/$tdir/striped_dir/dir1 ||
15600                 error "create dir1 fails"
15601         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
15602         [ $stripe_index -eq 1 ] ||
15603                 error "dir1 expect 1 got $stripe_index"
15604
15605         $LFS setdirstripe -i2 $DIR/$tdir/striped_dir/dir2 ||
15606                 error "create dir2 fails"
15607         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir2)
15608         [ $stripe_index -eq 2 ] ||
15609                 error "dir2 expect 2 got $stripe_index"
15610
15611         #check default stripe count/stripe index
15612         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
15613         test_300_check_default_striped_dir normal_dir 1 0
15614         test_300_check_default_striped_dir normal_dir 2 1
15615         test_300_check_default_striped_dir normal_dir 2 -1
15616
15617         #delete default stripe information
15618         echo "delete default stripeEA"
15619         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
15620                 error "set default stripe on striped dir error"
15621
15622         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
15623         for dir in $(find $DIR/$tdir/normal_dir/*); do
15624                 stripe_count=$($LFS getdirstripe -c $dir)
15625                 [ $stripe_count -eq 0 ] ||
15626                         error "expect 1 get $stripe_count for $dir"
15627                 stripe_index=$($LFS getdirstripe -i $dir)
15628                 [ $stripe_index -eq 0 ] ||
15629                         error "expect 0 get $stripe_index for $dir"
15630         done
15631 }
15632 run_test 300g "check default striped directory for normal directory"
15633
15634 test_300h() {
15635         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15636                 skip "Need MDS version at least 2.7.55" && return
15637         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15638         local dir
15639         local stripe_count
15640
15641         mkdir $DIR/$tdir
15642         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
15643                                         $DIR/$tdir/striped_dir ||
15644                 error "set striped dir error"
15645
15646         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
15647         test_300_check_default_striped_dir striped_dir 1 0
15648         test_300_check_default_striped_dir striped_dir 2 1
15649         test_300_check_default_striped_dir striped_dir 2 -1
15650
15651         #delete default stripe information
15652         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
15653                 error "set default stripe on striped dir error"
15654
15655         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
15656         for dir in $(find $DIR/$tdir/striped_dir/*); do
15657                 stripe_count=$($LFS getdirstripe -c $dir)
15658                 [ $stripe_count -eq 0 ] ||
15659                         error "expect 1 get $stripe_count for $dir"
15660         done
15661 }
15662 run_test 300h "check default striped directory for striped directory"
15663
15664 test_300i() {
15665         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15666                 skip "Need MDS version at least 2.7.55" && return
15667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15668         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15669         local stripe_count
15670         local file
15671
15672         mkdir $DIR/$tdir
15673
15674         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15675                 error "set striped dir error"
15676
15677         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15678                 error "create files under striped dir failed"
15679
15680         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
15681                 error "set striped hashdir error"
15682
15683         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
15684                 error "create dir0 under hash dir failed"
15685         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
15686                 error "create dir1 under hash dir failed"
15687
15688         # unfortunately, we need to umount to clear dir layout cache for now
15689         # once we fully implement dir layout, we can drop this
15690         umount_client $MOUNT || error "umount failed"
15691         mount_client $MOUNT || error "mount failed"
15692
15693         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
15694         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
15695         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
15696
15697         #set the stripe to be unknown hash type
15698         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
15699         $LCTL set_param fail_loc=0x1901
15700         for ((i = 0; i < 10; i++)); do
15701                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
15702                         error "stat f-$i failed"
15703                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
15704         done
15705
15706         touch $DIR/$tdir/striped_dir/f0 &&
15707                 error "create under striped dir with unknown hash should fail"
15708
15709         $LCTL set_param fail_loc=0
15710
15711         umount_client $MOUNT || error "umount failed"
15712         mount_client $MOUNT || error "mount failed"
15713
15714         return 0
15715 }
15716 run_test 300i "client handle unknown hash type striped directory"
15717
15718 test_300j() {
15719         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15720         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15721                 skip "Need MDS version at least 2.7.55" && return
15722         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15723         local stripe_count
15724         local file
15725
15726         mkdir $DIR/$tdir
15727
15728         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
15729         $LCTL set_param fail_loc=0x1702
15730         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15731                 error "set striped dir error"
15732
15733         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15734                 error "create files under striped dir failed"
15735
15736         $LCTL set_param fail_loc=0
15737
15738         rm -rf $DIR/$tdir || error "unlink striped dir fails"
15739
15740         return 0
15741 }
15742 run_test 300j "test large update record"
15743
15744 test_300k() {
15745         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15746                 skip "Need MDS version at least 2.7.55" && return
15747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15748         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15749         local stripe_count
15750         local file
15751
15752         mkdir $DIR/$tdir
15753
15754         #define OBD_FAIL_LARGE_STRIPE   0x1703
15755         $LCTL set_param fail_loc=0x1703
15756         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
15757                 error "set striped dir error"
15758         $LCTL set_param fail_loc=0
15759
15760         $LFS getdirstripe $DIR/$tdir/striped_dir ||
15761                 error "getstripeddir fails"
15762         rm -rf $DIR/$tdir/striped_dir ||
15763                 error "unlink striped dir fails"
15764
15765         return 0
15766 }
15767 run_test 300k "test large striped directory"
15768
15769 test_300l() {
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_index
15775
15776         test_mkdir -p $DIR/$tdir/striped_dir
15777         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
15778                         error "chown $RUNAS_ID failed"
15779         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
15780                 error "set default striped dir failed"
15781
15782         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
15783         $LCTL set_param fail_loc=0x80000158
15784         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
15785
15786         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
15787         [ $stripe_index -eq 1 ] ||
15788                 error "expect 1 get $stripe_index for $dir"
15789 }
15790 run_test 300l "non-root user to create dir under striped dir with stale layout"
15791
15792 test_300m() {
15793         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15794                 skip "Need MDS version at least 2.7.55" && return
15795         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15796         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
15797
15798         mkdir -p $DIR/$tdir/striped_dir
15799         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
15800                 error "set default stripes dir error"
15801
15802         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
15803
15804         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
15805         [ $stripe_count -eq 0 ] ||
15806                         error "expect 0 get $stripe_count for a"
15807
15808         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
15809                 error "set default stripes dir error"
15810
15811         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
15812
15813         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
15814         [ $stripe_count -eq 0 ] ||
15815                         error "expect 0 get $stripe_count for b"
15816
15817         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
15818                 error "set default stripes dir error"
15819
15820         mkdir $DIR/$tdir/striped_dir/c &&
15821                 error "default stripe_index is invalid, mkdir c should fails"
15822
15823         rm -rf $DIR/$tdir || error "rmdir fails"
15824 }
15825 run_test 300m "setstriped directory on single MDT FS"
15826
15827 cleanup_300n() {
15828         local list=$(comma_list $(mdts_nodes))
15829
15830         trap 0
15831         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15832 }
15833
15834 test_300n() {
15835         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15836                 skip "Need MDS version at least 2.7.55" && return
15837         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15838         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15839         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15840
15841         local stripe_index
15842         local list=$(comma_list $(mdts_nodes))
15843
15844         trap cleanup_300n RETURN EXIT
15845         mkdir -p $DIR/$tdir
15846         chmod 777 $DIR/$tdir
15847         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
15848                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15849                 error "create striped dir succeeds with gid=0"
15850
15851         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15852         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
15853                 error "create striped dir fails with gid=-1"
15854
15855         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15856         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
15857                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15858                 error "set default striped dir succeeds with gid=0"
15859
15860
15861         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15862         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
15863                 error "set default striped dir fails with gid=-1"
15864
15865
15866         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15867         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
15868                                         error "create test_dir fails"
15869         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
15870                                         error "create test_dir1 fails"
15871         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
15872                                         error "create test_dir2 fails"
15873         cleanup_300n
15874 }
15875 run_test 300n "non-root user to create dir under striped dir with default EA"
15876
15877 test_300o() {
15878         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15879                 skip "Need MDS version at least 2.7.55" && return
15880         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15881         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15882         local numfree1
15883         local numfree2
15884
15885         mkdir -p $DIR/$tdir
15886
15887         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
15888         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
15889         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
15890                 skip "not enough free inodes $numfree1 $numfree2"
15891                 return
15892         fi
15893
15894         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
15895         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
15896         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
15897                 skip "not enough free space $numfree1 $numfree2"
15898                 return
15899         fi
15900
15901         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
15902                 error "setdirstripe fails"
15903
15904         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
15905                 error "create dirs fails"
15906
15907         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
15908         ls $DIR/$tdir/striped_dir > /dev/null ||
15909                 error "ls striped dir fails"
15910         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
15911                 error "unlink big striped dir fails"
15912 }
15913 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
15914
15915 test_300p() {
15916         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15917         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15918         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15919
15920         mkdir -p $DIR/$tdir
15921
15922         #define OBD_FAIL_OUT_ENOSPC     0x1704
15923         do_facet mds2 lctl set_param fail_loc=0x80001704
15924         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
15925                         error "create striped directory should fail"
15926
15927         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
15928
15929         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
15930         true
15931 }
15932 run_test 300p "create striped directory without space"
15933
15934 test_300q() {
15935         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15936         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15937
15938         local fd=$(free_fd)
15939         local cmd="exec $fd<$tdir"
15940         cd $DIR
15941         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
15942         eval $cmd
15943         cmd="exec $fd<&-"
15944         trap "eval $cmd" EXIT
15945         cd $tdir || error "cd $tdir fails"
15946         rmdir  ../$tdir || error "rmdir $tdir fails"
15947         mkdir local_dir && error "create dir succeeds"
15948         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
15949         eval $cmd
15950         return 0
15951 }
15952 run_test 300q "create remote directory under orphan directory"
15953
15954 prepare_remote_file() {
15955         mkdir $DIR/$tdir/src_dir ||
15956                 error "create remote source failed"
15957
15958         cp /etc/hosts $DIR/$tdir/src_dir/a || error
15959         touch $DIR/$tdir/src_dir/a
15960
15961         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
15962                 error "create remote target dir failed"
15963
15964         touch $DIR/$tdir/tgt_dir/b
15965
15966         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
15967                 error "rename dir cross MDT failed!"
15968
15969         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
15970                 error "src_child still exists after rename"
15971
15972         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
15973                 error "missing file(a) after rename"
15974
15975         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
15976                 error "diff after rename"
15977 }
15978
15979 test_310a() {
15980         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15981         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15982         local remote_file=$DIR/$tdir/tgt_dir/b
15983
15984         mkdir -p $DIR/$tdir
15985
15986         prepare_remote_file || error "prepare remote file failed"
15987
15988         #open-unlink file
15989         $OPENUNLINK $remote_file $remote_file || error
15990         $CHECKSTAT -a $remote_file || error
15991 }
15992 run_test 310a "open unlink remote file"
15993
15994 test_310b() {
15995         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15996         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15997         local remote_file=$DIR/$tdir/tgt_dir/b
15998
15999         mkdir -p $DIR/$tdir
16000
16001         prepare_remote_file || error "prepare remote file failed"
16002
16003         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16004         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
16005         $CHECKSTAT -t file $remote_file || error "check file failed"
16006 }
16007 run_test 310b "unlink remote file with multiple links while open"
16008
16009 test_310c() {
16010         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
16011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16012         local remote_file=$DIR/$tdir/tgt_dir/b
16013
16014         mkdir -p $DIR/$tdir
16015
16016         prepare_remote_file || error "prepare remote file failed"
16017
16018         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16019         multiop_bg_pause $remote_file O_uc ||
16020                         error "mulitop failed for remote file"
16021         MULTIPID=$!
16022         $MULTIOP $DIR/$tfile Ouc
16023         kill -USR1 $MULTIPID
16024         wait $MULTIPID
16025 }
16026 run_test 310c "open-unlink remote file with multiple links"
16027
16028 #LU-4825
16029 test_311() {
16030         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16031         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16032                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16033         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16034         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16035
16036         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16037
16038         mkdir -p $DIR/$tdir
16039         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16040         createmany -o $DIR/$tdir/$tfile. 1000
16041
16042         # statfs data is not real time, let's just calculate it
16043         old_iused=$((old_iused + 1000))
16044
16045         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16046                         osp.*OST0000*MDT0000.create_count")
16047         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16048                                 osp.*OST0000*MDT0000.max_create_count")
16049         for idx in $(seq $MDSCOUNT); do
16050                 do_facet mds$idx "lctl set_param -n \
16051                         osp.*OST0000*MDT000?.max_create_count=0"
16052         done
16053
16054         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16055         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16056         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16057
16058         unlinkmany $DIR/$tdir/$tfile. 1000
16059
16060         for idx in $(seq $MDSCOUNT); do
16061                 do_facet mds$idx "lctl set_param -n \
16062                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16063                 do_facet mds$idx "lctl set_param -n \
16064                         osp.*OST0000*MDT000?.create_count=$count"
16065         done
16066
16067         local new_iused
16068         for i in $(seq 120); do
16069                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16070                 # system may be too busy to destroy all objs in time, use
16071                 # a somewhat small value to not fail autotest
16072                 [ $((old_iused - new_iused)) -gt 400 ] && break
16073                 sleep 1
16074         done
16075
16076         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16077         [ $((old_iused - new_iused)) -gt 400 ] ||
16078                 error "objs not destroyed after unlink"
16079 }
16080 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16081
16082 zfs_oid_to_objid()
16083 {
16084         local ost=$1
16085         local objid=$2
16086
16087         local vdevdir=$(dirname $(facet_vdevice $ost))
16088         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
16089         local zfs_zapid=$(do_facet $ost $cmd |
16090                           grep -w "/O/0/d$((objid%32))" -C 5 |
16091                           awk '/Object/{getline; print $1}')
16092         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
16093                           awk "/$objid = /"'{printf $3}')
16094
16095         echo $zfs_objid
16096 }
16097
16098 zfs_object_blksz() {
16099         local ost=$1
16100         local objid=$2
16101
16102         local vdevdir=$(dirname $(facet_vdevice $ost))
16103         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
16104         local blksz=$(do_facet $ost $cmd $objid |
16105                       awk '/dblk/{getline; printf $4}')
16106
16107         case "${blksz: -1}" in
16108                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
16109                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
16110                 *) ;;
16111         esac
16112
16113         echo $blksz
16114 }
16115
16116 test_312() { # LU-4856
16117         remote_ost_nodsh && skip "remote OST with nodsh" && return
16118
16119         [ $(facet_fstype ost1) = "zfs" ] ||
16120                 { skip "the test only applies to zfs" && return; }
16121
16122         local max_blksz=$(do_facet ost1 \
16123                           $ZFS get -p recordsize $(facet_device ost1) |
16124                           awk '!/VALUE/{print $3}')
16125         local min_blksz=$(getconf PAGE_SIZE)
16126
16127         # to make life a little bit easier
16128         $LFS mkdir -c 1 -i 0 $DIR/$tdir
16129         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16130
16131         local tf=$DIR/$tdir/$tfile
16132         touch $tf
16133         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16134
16135         # Get ZFS object id
16136         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16137
16138         # block size change by sequential over write
16139         local blksz
16140         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
16141                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
16142
16143                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16144                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
16145         done
16146         rm -f $tf
16147
16148         # block size change by sequential append write
16149         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
16150         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16151         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16152
16153         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
16154                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
16155                         oflag=sync conv=notrunc
16156
16157                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16158                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
16159                         error "blksz error, actual $blksz, "    \
16160                                 "expected: 2 * $count * $min_blksz"
16161         done
16162         rm -f $tf
16163
16164         # random write
16165         touch $tf
16166         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16167         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16168
16169         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
16170         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16171         [ $blksz -eq $min_blksz ] ||
16172                 error "blksz error: $blksz, expected: $min_blksz"
16173
16174         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
16175         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16176         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
16177
16178         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
16179         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16180         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
16181 }
16182 run_test 312 "make sure ZFS adjusts its block size by write pattern"
16183
16184 test_313() {
16185         remote_ost_nodsh && skip "remote OST with nodsh" && return
16186
16187         local file=$DIR/$tfile
16188         rm -f $file
16189         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
16190
16191         # define OBD_FAIL_TGT_RCVD_EIO           0x720
16192         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16193         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
16194                 error "write should failed"
16195         do_facet ost1 "$LCTL set_param fail_loc=0"
16196         rm -f $file
16197 }
16198 run_test 313 "io should fail after last_rcvd update fail"
16199
16200 test_314() {
16201         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16202
16203         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
16204         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16205         rm -f $DIR/$tfile
16206         wait_delete_completed
16207         do_facet ost1 "$LCTL set_param fail_loc=0"
16208 }
16209 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
16210
16211 test_315() { # LU-618
16212         local file=$DIR/$tfile
16213         rm -f $file
16214
16215         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
16216         $MULTIOP $file oO_RDONLY:r4096000_c &
16217         PID=$!
16218
16219         sleep 2
16220
16221         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
16222         kill -USR1 $PID
16223
16224         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
16225         rm -f $file
16226 }
16227 run_test 315 "read should be accounted"
16228
16229 test_fake_rw() {
16230         local read_write=$1
16231         if [ "$read_write" = "write" ]; then
16232                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
16233         elif [ "$read_write" = "read" ]; then
16234                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
16235         else
16236                 error "argument error"
16237         fi
16238
16239         # turn off debug for performance testing
16240         local saved_debug=$($LCTL get_param -n debug)
16241         $LCTL set_param debug=0
16242
16243         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
16244
16245         # get ost1 size - lustre-OST0000
16246         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
16247         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
16248         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
16249
16250         if [ "$read_write" = "read" ]; then
16251                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
16252         fi
16253
16254         local start_time=$(date +%s.%N)
16255         $dd_cmd bs=1M count=$blocks oflag=sync ||
16256                 error "real dd $read_write error"
16257         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
16258
16259         if [ "$read_write" = "write" ]; then
16260                 rm -f $DIR/$tfile
16261         fi
16262
16263         # define OBD_FAIL_OST_FAKE_RW           0x238
16264         do_facet ost1 $LCTL set_param fail_loc=0x238
16265
16266         local start_time=$(date +%s.%N)
16267         $dd_cmd bs=1M count=$blocks oflag=sync ||
16268                 error "fake dd $read_write error"
16269         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
16270
16271         if [ "$read_write" = "write" ]; then
16272                 # verify file size
16273                 cancel_lru_locks osc
16274                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
16275                         error "$tfile size not $blocks MB"
16276         fi
16277         do_facet ost1 $LCTL set_param fail_loc=0
16278
16279         echo "fake $read_write $duration_fake vs. normal $read_write" \
16280                 "$duration in seconds"
16281         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
16282                 error_not_in_vm "fake write is slower"
16283
16284         $LCTL set_param -n debug="$saved_debug"
16285         rm -f $DIR/$tfile
16286 }
16287 test_399a() { # LU-7655 for OST fake write
16288         remote_ost_nodsh && skip "remote OST with nodsh" && return
16289
16290         test_fake_rw write
16291 }
16292 run_test 399a "fake write should not be slower than normal write"
16293
16294 test_399b() { # LU-8726 for OST fake read
16295         remote_ost_nodsh && skip "remote OST with nodsh" && return
16296
16297         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
16298                 skip "ldiskfs only test" && return 0
16299         fi
16300         test_fake_rw read
16301 }
16302 run_test 399b "fake read should not be slower than normal read"
16303
16304 test_400a() { # LU-1606, was conf-sanity test_74
16305         local extra_flags=''
16306         local out=$TMP/$tfile
16307         local prefix=/usr/include/lustre
16308         local prog
16309
16310         if ! which $CC > /dev/null 2>&1; then
16311                 skip_env "$CC is not installed"
16312                 return 0
16313         fi
16314
16315         if ! [[ -d $prefix ]]; then
16316                 # Assume we're running in tree and fixup the include path.
16317                 extra_flags+=" -I$LUSTRE/../libcfs/include"
16318                 extra_flags+=" -I$LUSTRE/include"
16319                 extra_flags+=" -L$LUSTRE/utils"
16320         fi
16321
16322         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
16323                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
16324                         error "client api broken"
16325         done
16326         rm -f $out
16327 }
16328 run_test 400a "Lustre client api program can compile and link"
16329
16330 test_400b() { # LU-1606, LU-5011
16331         local header
16332         local out=$TMP/$tfile
16333         local prefix=/usr/include/lustre
16334
16335         # We use a hard coded prefix so that this test will not fail
16336         # when run in tree. There are headers in lustre/include/lustre/
16337         # that are not packaged (like lustre_idl.h) and have more
16338         # complicated include dependencies (like config.h and lnet/types.h).
16339         # Since this test about correct packaging we just skip them when
16340         # they don't exist (see below) rather than try to fixup cppflags.
16341
16342         if ! which $CC > /dev/null 2>&1; then
16343                 skip_env "$CC is not installed"
16344                 return 0
16345         fi
16346
16347         for header in $prefix/*.h; do
16348                 if ! [[ -f "$header" ]]; then
16349                         continue
16350                 fi
16351
16352                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
16353                         continue # liblustreapi.h is deprecated.
16354                 fi
16355
16356                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
16357                         error "cannot compile '$header'"
16358         done
16359         rm -f $out
16360 }
16361 run_test 400b "packaged headers can be compiled"
16362
16363 test_401a() { #LU-7437
16364         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
16365         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
16366                 return
16367         #count the number of parameters by "list_param -R"
16368         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
16369         #count the number of parameters by listing proc files
16370         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
16371         echo "proc_dirs='$proc_dirs'"
16372         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
16373         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
16374                       sort -u | wc -l)
16375
16376         [ $params -eq $procs ] ||
16377                 error "found $params parameters vs. $procs proc files"
16378
16379         # test the list_param -D option only returns directories
16380         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
16381         #count the number of parameters by listing proc directories
16382         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
16383                 sort -u | wc -l)
16384
16385         [ $params -eq $procs ] ||
16386                 error "found $params parameters vs. $procs proc files"
16387 }
16388 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
16389
16390 test_401b() {
16391         local save=$($LCTL get_param -n jobid_var)
16392         local tmp=testing
16393
16394         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
16395                 error "no error returned when setting bad parameters"
16396
16397         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
16398         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
16399
16400         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
16401         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
16402         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
16403 }
16404 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
16405
16406 test_401c() {
16407         local jobid_var_old=$($LCTL get_param -n jobid_var)
16408         local jobid_var_new
16409
16410         $LCTL set_param jobid_var= &&
16411                 error "no error returned for 'set_param a='"
16412
16413         jobid_var_new=$($LCTL get_param -n jobid_var)
16414         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16415                 error "jobid_var was changed by setting without value"
16416
16417         $LCTL set_param jobid_var &&
16418                 error "no error returned for 'set_param a'"
16419
16420         jobid_var_new=$($LCTL get_param -n jobid_var)
16421         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16422                 error "jobid_var was changed by setting without value"
16423 }
16424 run_test 401c "Verify 'lctl set_param' without value fails in either format."
16425
16426 test_401d() {
16427         local jobid_var_old=$($LCTL get_param -n jobid_var)
16428         local jobid_var_new
16429         local new_value="foo=bar"
16430
16431         $LCTL set_param jobid_var=$new_value ||
16432                 error "'set_param a=b' did not accept a value containing '='"
16433
16434         jobid_var_new=$($LCTL get_param -n jobid_var)
16435         [[ "$jobid_var_new" == "$new_value" ]] ||
16436                 error "'set_param a=b' failed on a value containing '='"
16437
16438         # Reset the jobid_var to test the other format
16439         $LCTL set_param jobid_var=$jobid_var_old
16440         jobid_var_new=$($LCTL get_param -n jobid_var)
16441         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16442                 error "failed to reset jobid_var"
16443
16444         $LCTL set_param jobid_var $new_value ||
16445                 error "'set_param a b' did not accept a value containing '='"
16446
16447         jobid_var_new=$($LCTL get_param -n jobid_var)
16448         [[ "$jobid_var_new" == "$new_value" ]] ||
16449                 error "'set_param a b' failed on a value containing '='"
16450
16451         $LCTL set_param jobid_var $jobid_var_old
16452         jobid_var_new=$($LCTL get_param -n jobid_var)
16453         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16454                 error "failed to reset jobid_var"
16455 }
16456 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
16457
16458 test_402() {
16459         local server_version=$(lustre_version_code $SINGLEMDS)
16460         [[ $server_version -ge $(version_code 2.7.66) ]] ||
16461         [[ $server_version -ge $(version_code 2.7.18.4) &&
16462                 $server_version -lt $(version_code 2.7.50) ]] ||
16463         [[ $server_version -ge $(version_code 2.7.2) &&
16464                 $server_version -lt $(version_code 2.7.11) ]] ||
16465                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
16466                         return; }
16467         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16468         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
16469 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
16470         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
16471         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
16472                 echo "Touch failed - OK"
16473 }
16474 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
16475
16476 test_403() {
16477         local file1=$DIR/$tfile.1
16478         local file2=$DIR/$tfile.2
16479         local tfile=$TMP/$tfile
16480
16481         rm -f $file1 $file2 $tfile
16482
16483         touch $file1
16484         ln $file1 $file2
16485
16486         # 30 sec OBD_TIMEOUT in ll_getattr()
16487         # right before populating st_nlink
16488         $LCTL set_param fail_loc=0x80001409
16489         stat -c %h $file1 > $tfile &
16490
16491         # create an alias, drop all locks and reclaim the dentry
16492         < $file2
16493         cancel_lru_locks mdc
16494         cancel_lru_locks osc
16495         sysctl -w vm.drop_caches=2
16496
16497         wait
16498
16499         [ `cat $tfile` -gt 0 ] || error "wrong nlink count: `cat $tfile`"
16500
16501         rm -f $tfile $file1 $file2
16502 }
16503 run_test 403 "i_nlink should not drop to zero due to aliasing"
16504
16505 test_404() { # LU-6601
16506         local server_version=$(lustre_version_code $SINGLEMDS)
16507         [[ $server_version -ge $(version_code 2.8.53) ]] ||
16508                 { skip "Need server version newer than 2.8.52"; return 0; }
16509
16510         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16511         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
16512                 awk '/osp .*-osc-MDT/ { print $4}')
16513
16514         local osp
16515         for osp in $mosps; do
16516                 echo "Deactivate: " $osp
16517                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
16518                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16519                         awk -vp=$osp '$4 == p { print $2 }')
16520                 [ $stat = IN ] || {
16521                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16522                         error "deactivate error"
16523                 }
16524                 echo "Activate: " $osp
16525                 do_facet $SINGLEMDS $LCTL --device %$osp activate
16526                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16527                         awk -vp=$osp '$4 == p { print $2 }')
16528                 [ $stat = UP ] || {
16529                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16530                         error "activate error"
16531                 }
16532         done
16533 }
16534 run_test 404 "validate manual {de}activated works properly for OSPs"
16535
16536 test_405() {
16537         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
16538         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
16539                 skip "Layout swap lock is not supported" && return
16540
16541         check_swap_layouts_support && return 0
16542
16543         test_mkdir -p $DIR/$tdir
16544         swap_lock_test -d $DIR/$tdir ||
16545                 error "One layout swap locked test failed"
16546 }
16547 run_test 405 "Various layout swap lock tests"
16548
16549 test_406() {
16550         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16551         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16552         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
16553         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16554         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
16555                 skip "Need MDS version at least 2.8.50" && return
16556
16557         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
16558         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
16559         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
16560         local def_pool=$($GETSTRIPE -p $MOUNT)
16561         local test_pool=$TESTNAME
16562
16563         if ! combined_mgs_mds ; then
16564                 mount_mgs_client
16565         fi
16566         pool_add $test_pool || error "pool_add failed"
16567         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
16568                 error "pool_add_targets failed"
16569
16570         # parent set default stripe count only, child will stripe from both
16571         # parent and fs default
16572         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
16573                 error "setstripe $MOUNT failed"
16574         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
16575         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
16576         for i in $(seq 10); do
16577                 local f=$DIR/$tdir/$tfile.$i
16578                 touch $f || error "touch failed"
16579                 local count=$($GETSTRIPE -c $f)
16580                 [ $count -eq $OSTCOUNT ] ||
16581                         error "$f stripe count $count != $OSTCOUNT"
16582                 local offset=$($GETSTRIPE -i $f)
16583                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
16584                 local size=$($GETSTRIPE -S $f)
16585                 [ $size -eq $((def_stripe_size * 2)) ] ||
16586                         error "$f stripe size $size != $((def_stripe_size * 2))"
16587                 local pool=$($GETSTRIPE -p $f)
16588                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
16589         done
16590
16591         # change fs default striping, delete parent default striping, now child
16592         # will stripe from new fs default striping only
16593         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
16594                 error "change $MOUNT default stripe failed"
16595         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
16596         for i in $(seq 11 20); do
16597                 local f=$DIR/$tdir/$tfile.$i
16598                 touch $f || error "touch $f failed"
16599                 local count=$($GETSTRIPE -c $f)
16600                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
16601                 local offset=$($GETSTRIPE -i $f)
16602                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
16603                 local size=$($GETSTRIPE -S $f)
16604                 [ $size -eq $def_stripe_size ] ||
16605                         error "$f stripe size $size != $def_stripe_size"
16606                 local pool=$($GETSTRIPE -p $f)
16607                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
16608
16609         done
16610
16611         unlinkmany $DIR/$tdir/$tfile. 1 20
16612
16613         # restore FS default striping
16614         if [ -z $def_pool ]; then
16615                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16616                         -i $def_stripe_offset $MOUNT ||
16617                         error "restore default striping failed"
16618         else
16619                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16620                         -i $def_stripe_offset -p $def_pool $MOUNT ||
16621                         error "restore default striping with $def_pool failed"
16622         fi
16623
16624         local f=$DIR/$tdir/$tfile
16625         pool_remove_all_targets $test_pool $f
16626         pool_remove $test_pool $f
16627
16628         if ! combined_mgs_mds ; then
16629                 umount_mgs_client
16630         fi
16631 }
16632 run_test 406 "DNE support fs default striping"
16633
16634 test_407() {
16635         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16636         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
16637                 skip "Need MDS version at least 2.8.55" && return
16638         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16639
16640         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
16641                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
16642         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
16643                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
16644         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
16645
16646         #define OBD_FAIL_DT_TXN_STOP    0x2019
16647         for idx in $(seq $MDSCOUNT); do
16648                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
16649         done
16650         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
16651         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
16652                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
16653         true
16654 }
16655 run_test 407 "transaction fail should cause operation fail"
16656
16657 test_408() {
16658         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
16659
16660         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
16661         lctl set_param fail_loc=0x8000040a
16662         # let ll_prepare_partial_page() fail
16663         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
16664
16665         rm -f $DIR/$tfile
16666
16667         # create at least 100 unused inodes so that
16668         # shrink_icache_memory(0) should not return 0
16669         touch $DIR/$tfile-{0..100}
16670         rm -f $DIR/$tfile-{0..100}
16671         sync
16672
16673         echo 2 > /proc/sys/vm/drop_caches
16674 }
16675 run_test 408 "drop_caches should not hang due to page leaks"
16676
16677 test_409()
16678 {
16679         [ $MDSCOUNT -lt 2 ] &&
16680                 skip "We need at least 2 MDTs for this test" && return
16681
16682         check_mount_and_prep
16683
16684         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
16685         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
16686         touch $DIR/$tdir/guard || error "(2) Fail to create"
16687
16688         local PREFIX=$(str_repeat 'A' 128)
16689         echo "Create 1K hard links start at $(date)"
16690         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16691                 error "(3) Fail to hard link"
16692
16693         echo "Links count should be right although linkEA overflow"
16694         stat $DIR/$tdir/guard || error "(4) Fail to stat"
16695         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
16696         [ $linkcount -eq 1001 ] ||
16697                 error "(5) Unexpected hard links count: $linkcount"
16698
16699         echo "List all links start at $(date)"
16700         ls -l $DIR/$tdir/foo > /dev/null ||
16701                 error "(6) Fail to list $DIR/$tdir/foo"
16702
16703         echo "Unlink hard links start at $(date)"
16704         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16705                 error "(7) Fail to unlink"
16706 }
16707 run_test 409 "Large amount of cross-MDTs hard links on the same file"
16708
16709 test_410()
16710 {
16711         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
16712                 skip "Need client version at least 2.9.59" && return
16713
16714         # Create a file, and stat it from the kernel
16715         local testfile=$DIR/$tfile
16716         touch $testfile
16717
16718         local run_id=$RANDOM
16719         local my_ino=$(stat --format "%i" $testfile)
16720
16721         # Try to insert the module. This will always fail as the
16722         # module is designed to not be inserted.
16723         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
16724             &> /dev/null
16725
16726         # Anything but success is a test failure
16727         dmesg | grep -q \
16728             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
16729             error "no inode match"
16730 }
16731 run_test 410 "Test inode number returned from kernel thread"
16732
16733 prep_801() {
16734         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
16735         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
16736                 skip "Need server version at least 2.9.55" & exit 0
16737         start_full_debug_logging
16738 }
16739
16740 post_801() {
16741         stop_full_debug_logging
16742 }
16743
16744 barrier_stat() {
16745         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
16746                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16747                            awk '/The barrier for/ { print $7 }')
16748                 echo $st
16749         else
16750                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
16751                 echo \'$st\'
16752         fi
16753 }
16754
16755 barrier_expired() {
16756         local expired
16757
16758         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
16759                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16760                           awk '/will be expired/ { print $7 }')
16761         else
16762                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
16763         fi
16764
16765         echo $expired
16766 }
16767
16768 test_801a() {
16769         prep_801
16770
16771         echo "Start barrier_freeze at: $(date)"
16772         #define OBD_FAIL_BARRIER_DELAY          0x2202
16773         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
16774         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
16775
16776         sleep 2
16777         local b_status=$(barrier_stat)
16778         echo "Got barrier status at: $(date)"
16779         [ "$b_status" = "'freezing_p1'" ] ||
16780                 error "(1) unexpected barrier status $b_status"
16781
16782         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16783         wait
16784         b_status=$(barrier_stat)
16785         [ "$b_status" = "'frozen'" ] ||
16786                 error "(2) unexpected barrier status $b_status"
16787
16788         local expired=$(barrier_expired)
16789         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
16790         sleep $((expired + 3))
16791
16792         b_status=$(barrier_stat)
16793         [ "$b_status" = "'expired'" ] ||
16794                 error "(3) unexpected barrier status $b_status"
16795
16796         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
16797                 error "(4) fail to freeze barrier"
16798
16799         b_status=$(barrier_stat)
16800         [ "$b_status" = "'frozen'" ] ||
16801                 error "(5) unexpected barrier status $b_status"
16802
16803         echo "Start barrier_thaw at: $(date)"
16804         #define OBD_FAIL_BARRIER_DELAY          0x2202
16805         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
16806         do_facet mgs $LCTL barrier_thaw $FSNAME &
16807
16808         sleep 2
16809         b_status=$(barrier_stat)
16810         echo "Got barrier status at: $(date)"
16811         [ "$b_status" = "'thawing'" ] ||
16812                 error "(6) unexpected barrier status $b_status"
16813
16814         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16815         wait
16816         b_status=$(barrier_stat)
16817         [ "$b_status" = "'thawed'" ] ||
16818                 error "(7) unexpected barrier status $b_status"
16819
16820         #define OBD_FAIL_BARRIER_FAILURE        0x2203
16821         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
16822         do_facet mgs $LCTL barrier_freeze $FSNAME
16823
16824         b_status=$(barrier_stat)
16825         [ "$b_status" = "'failed'" ] ||
16826                 error "(8) unexpected barrier status $b_status"
16827
16828         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
16829         do_facet mgs $LCTL barrier_thaw $FSNAME
16830
16831         post_801
16832 }
16833 run_test 801a "write barrier user interfaces and stat machine"
16834
16835 test_801b() {
16836         prep_801
16837
16838         mkdir $DIR/$tdir || error "(1) fail to mkdir"
16839         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
16840         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
16841         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
16842         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
16843
16844         cancel_lru_locks mdc
16845
16846         # 180 seconds should be long enough
16847         do_facet mgs $LCTL barrier_freeze $FSNAME 180
16848
16849         local b_status=$(barrier_stat)
16850         [ "$b_status" = "'frozen'" ] ||
16851                 error "(6) unexpected barrier status $b_status"
16852
16853         mkdir $DIR/$tdir/d0/d10 &
16854         mkdir_pid=$!
16855
16856         touch $DIR/$tdir/d1/f13 &
16857         touch_pid=$!
16858
16859         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
16860         ln_pid=$!
16861
16862         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
16863         mv_pid=$!
16864
16865         rm -f $DIR/$tdir/d4/f12 &
16866         rm_pid=$!
16867
16868         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
16869
16870         # To guarantee taht the 'stat' is not blocked
16871         b_status=$(barrier_stat)
16872         [ "$b_status" = "'frozen'" ] ||
16873                 error "(8) unexpected barrier status $b_status"
16874
16875         # let above commands to run at background
16876         sleep 5
16877
16878         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
16879         ps -p $touch_pid || error "(10) touch should be blocked"
16880         ps -p $ln_pid || error "(11) link should be blocked"
16881         ps -p $mv_pid || error "(12) rename should be blocked"
16882         ps -p $rm_pid || error "(13) unlink should be blocked"
16883
16884         b_status=$(barrier_stat)
16885         [ "$b_status" = "'frozen'" ] ||
16886                 error "(14) unexpected barrier status $b_status"
16887
16888         do_facet mgs $LCTL barrier_thaw $FSNAME
16889         b_status=$(barrier_stat)
16890         [ "$b_status" = "'thawed'" ] ||
16891                 error "(15) unexpected barrier status $b_status"
16892
16893         wait $mkdir_pid || error "(16) mkdir should succeed"
16894         wait $touch_pid || error "(17) touch should succeed"
16895         wait $ln_pid || error "(18) link should succeed"
16896         wait $mv_pid || error "(19) rename should succeed"
16897         wait $rm_pid || error "(20) unlink should succeed"
16898
16899         post_801
16900 }
16901 run_test 801b "modification will be blocked by write barrier"
16902
16903 test_801c() {
16904         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
16905
16906         prep_801
16907
16908         stop mds2 || error "(1) Fail to stop mds2"
16909
16910         do_facet mgs $LCTL barrier_freeze $FSNAME 30
16911
16912         local b_status=$(barrier_stat)
16913         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
16914                 do_facet mgs $LCTL barrier_thaw $FSNAME
16915                 error "(2) unexpected barrier status $b_status"
16916         }
16917
16918         do_facet mgs $LCTL barrier_rescan $FSNAME ||
16919                 error "(3) Fail to rescan barrier bitmap"
16920
16921         do_facet mgs $LCTL barrier_freeze $FSNAME 10
16922
16923         b_status=$(barrier_stat)
16924         [ "$b_status" = "'frozen'" ] ||
16925                 error "(4) unexpected barrier status $b_status"
16926
16927         do_facet mgs $LCTL barrier_thaw $FSNAME
16928         b_status=$(barrier_stat)
16929         [ "$b_status" = "'thawed'" ] ||
16930                 error "(5) unexpected barrier status $b_status"
16931
16932         local devname=$(mdsdevname 2)
16933
16934         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
16935
16936         do_facet mgs $LCTL barrier_rescan $FSNAME ||
16937                 error "(7) Fail to rescan barrier bitmap"
16938
16939         post_801
16940 }
16941 run_test 801c "rescan barrier bitmap"
16942
16943 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
16944 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
16945 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
16946
16947 cleanup_802() {
16948         trap 0
16949
16950         stopall
16951         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
16952         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
16953         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
16954         setupall
16955 }
16956
16957 test_802() {
16958
16959         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
16960         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
16961                 skip "Need server version at least 2.9.55" & exit 0
16962
16963         mkdir $DIR/$tdir || error "(1) fail to mkdir"
16964
16965         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
16966                 error "(2) Fail to copy"
16967
16968         trap cleanup_802 EXIT
16969
16970         # sync by force before remount as readonly
16971         sync; sync_all_data; sleep 3; sync_all_data
16972
16973         stopall
16974
16975         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
16976         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
16977         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
16978
16979         echo "Mount the server as read only"
16980         setupall server_only || error "(3) Fail to start servers"
16981
16982         echo "Mount client without ro should fail"
16983         mount_client $MOUNT &&
16984                 error "(4) Mount client without 'ro' should fail"
16985
16986         echo "Mount client with ro should succeed"
16987         mount_client $MOUNT ro ||
16988                 error "(5) Mount client with 'ro' should succeed"
16989
16990         echo "Modify should be refused"
16991         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
16992
16993         echo "Read should be allowed"
16994         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
16995                 error "(7) Read should succeed under ro mode"
16996
16997         cleanup_802
16998 }
16999 run_test 802 "simulate readonly device"
17000
17001 cleanup_805() {
17002         do_facet $SINGLEMDS zfs set quota=$old $fsset
17003         unlinkmany $DIR/$tdir/f- 1000000
17004         trap 0
17005 }
17006
17007 test_805() {
17008         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
17009         [ "$(facet_fstype mds1)" != "zfs" ] &&
17010                 skip "ZFS specific test" && return
17011         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
17012                 skip "netfree not implemented before 0.7" && return
17013         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.57) ]] ||
17014                 { skip "Need MDS version at least 2.10.57" && return 0; }
17015
17016         local fsset
17017         local freekb
17018         local usedkb
17019         local old
17020         local quota
17021         local pref="osd-zfs.lustre-MDT0000."
17022
17023         # limit available space on MDS dataset to meet nospace issue
17024         # quickly. then ZFS 0.7.2 can use reserved space if asked
17025         # properly (using netfree flag in osd_declare_destroy()
17026         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
17027         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
17028                 gawk '{print $3}')
17029         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
17030         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
17031         let "usedkb=usedkb-freekb"
17032         let "freekb=freekb/2"
17033         if let "freekb > 5000"; then
17034                 let "freekb=5000"
17035         fi
17036         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
17037         trap cleanup_805 EXIT
17038         mkdir $DIR/$tdir
17039         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
17040         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
17041         rm -rf $DIR/$tdir || error "not able to remove"
17042         do_facet $SINGLEMDS zfs set quota=$old $fsset
17043         trap 0
17044 }
17045 run_test 805 "ZFS can remove from full fs"
17046
17047 test_810() {
17048         local ORIG
17049         local CSUM
17050
17051         lctl set_param fail_loc=0x411
17052         dd if=/dev/urandom of=$DIR/$tfile bs=10240 count=2
17053         ORIG=$(md5sum $DIR/$tfile)
17054         lctl set_param ldlm.namespaces.*osc*.lru_size=clear
17055         CSUM=$(md5sum $DIR/$tfile)
17056         if [ "$ORIG" != "$CSUM" ]; then
17057                 error "$ORIG != $CSUM"
17058         fi
17059 }
17060 run_test 810 "partial page writes on ZFS (LU-11663)"
17061
17062 #
17063 # tests that do cleanup/setup should be run at the end
17064 #
17065
17066 test_900() {
17067         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17068         local ls
17069         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
17070         $LCTL set_param fail_loc=0x903
17071
17072         cancel_lru_locks MGC
17073
17074         FAIL_ON_ERROR=true cleanup
17075         FAIL_ON_ERROR=true setup
17076 }
17077 run_test 900 "umount should not race with any mgc requeue thread"
17078
17079 complete $SECONDS
17080 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
17081 check_and_cleanup_lustre
17082 if [ "$I_MOUNTED" != "yes" ]; then
17083         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
17084 fi
17085 exit_status