Whamcloud - gitweb
LU-11069 llite: correct file position after appending writes
[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 # createtest also checks that device nodes are created and
2194 # then visible correctly (#2091)
2195 test_28() { # bug 2091
2196         test_mkdir $DIR/d28
2197         $CREATETEST $DIR/d28/ct || error
2198 }
2199 run_test 28 "create/mknod/mkdir with bad file types ============"
2200
2201 test_29() {
2202         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2203         sync; sleep 1; sync # flush out any dirty pages from previous tests
2204         cancel_lru_locks
2205         test_mkdir $DIR/d29
2206         touch $DIR/d29/foo
2207         log 'first d29'
2208         ls -l $DIR/d29
2209
2210         declare -i LOCKCOUNTORIG=0
2211         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2212                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2213         done
2214         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2215
2216         declare -i LOCKUNUSEDCOUNTORIG=0
2217         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2218                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2219         done
2220
2221         log 'second d29'
2222         ls -l $DIR/d29
2223         log 'done'
2224
2225         declare -i LOCKCOUNTCURRENT=0
2226         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2227                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2228         done
2229
2230         declare -i LOCKUNUSEDCOUNTCURRENT=0
2231         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2232                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2233         done
2234
2235         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2236                 $LCTL set_param -n ldlm.dump_namespaces ""
2237                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2238                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2239                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2240                 return 2
2241         fi
2242         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2243                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2244                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2245                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2246                 return 3
2247         fi
2248 }
2249 run_test 29 "IT_GETATTR regression  ============================"
2250
2251 test_30a() { # was test_30
2252         cp $(which ls) $DIR || cp /bin/ls $DIR
2253         $DIR/ls / || error
2254         rm $DIR/ls
2255 }
2256 run_test 30a "execute binary from Lustre (execve) =============="
2257
2258 test_30b() {
2259         cp `which ls` $DIR || cp /bin/ls $DIR
2260         chmod go+rx $DIR/ls
2261         $RUNAS $DIR/ls / || error
2262         rm $DIR/ls
2263 }
2264 run_test 30b "execute binary from Lustre as non-root ==========="
2265
2266 test_30c() { # b=22376
2267         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2268         cp `which ls` $DIR || cp /bin/ls $DIR
2269         chmod a-rw $DIR/ls
2270         cancel_lru_locks mdc
2271         cancel_lru_locks osc
2272         $RUNAS $DIR/ls / || error
2273         rm -f $DIR/ls
2274 }
2275 run_test 30c "execute binary from Lustre without read perms ===="
2276
2277 test_31a() {
2278         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2279         $CHECKSTAT -a $DIR/f31 || error
2280 }
2281 run_test 31a "open-unlink file =================================="
2282
2283 test_31b() {
2284         touch $DIR/f31 || error
2285         ln $DIR/f31 $DIR/f31b || error
2286         $MULTIOP $DIR/f31b Ouc || error
2287         $CHECKSTAT -t file $DIR/f31 || error
2288 }
2289 run_test 31b "unlink file with multiple links while open ======="
2290
2291 test_31c() {
2292         touch $DIR/f31 || error
2293         ln $DIR/f31 $DIR/f31c || error
2294         multiop_bg_pause $DIR/f31 O_uc || return 1
2295         MULTIPID=$!
2296         $MULTIOP $DIR/f31c Ouc
2297         kill -USR1 $MULTIPID
2298         wait $MULTIPID
2299 }
2300 run_test 31c "open-unlink file with multiple links ============="
2301
2302 test_31d() {
2303         opendirunlink $DIR/d31d $DIR/d31d || error
2304         $CHECKSTAT -a $DIR/d31d || error
2305 }
2306 run_test 31d "remove of open directory ========================="
2307
2308 test_31e() { # bug 2904
2309         openfilleddirunlink $DIR/d31e || error
2310 }
2311 run_test 31e "remove of open non-empty directory ==============="
2312
2313 test_31f() { # bug 4554
2314         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2315         set -vx
2316         test_mkdir $DIR/d31f
2317         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2318         cp /etc/hosts $DIR/d31f
2319         ls -l $DIR/d31f
2320         $GETSTRIPE $DIR/d31f/hosts
2321         multiop_bg_pause $DIR/d31f D_c || return 1
2322         MULTIPID=$!
2323
2324         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2325         test_mkdir $DIR/d31f
2326         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2327         cp /etc/hosts $DIR/d31f
2328         ls -l $DIR/d31f
2329         $GETSTRIPE $DIR/d31f/hosts
2330         multiop_bg_pause $DIR/d31f D_c || return 1
2331         MULTIPID2=$!
2332
2333         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2334         wait $MULTIPID || error "first opendir $MULTIPID failed"
2335
2336         sleep 6
2337
2338         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2339         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2340         set +vx
2341 }
2342 run_test 31f "remove of open directory with open-unlink file ==="
2343
2344 test_31g() {
2345         echo "-- cross directory link --"
2346         test_mkdir -c1 $DIR/${tdir}ga
2347         test_mkdir -c1 $DIR/${tdir}gb
2348         touch $DIR/${tdir}ga/f
2349         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2350         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2351         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2352         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2353         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2354 }
2355 run_test 31g "cross directory link==============="
2356
2357 test_31h() {
2358         echo "-- cross directory link --"
2359         test_mkdir -c1 $DIR/${tdir}
2360         test_mkdir -c1 $DIR/${tdir}/dir
2361         touch $DIR/${tdir}/f
2362         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2363         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2364         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2365         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2366         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2367 }
2368 run_test 31h "cross directory link under child==============="
2369
2370 test_31i() {
2371         echo "-- cross directory link --"
2372         test_mkdir -c1 $DIR/$tdir
2373         test_mkdir -c1 $DIR/$tdir/dir
2374         touch $DIR/$tdir/dir/f
2375         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2376         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2377         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2378         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2379         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2380 }
2381 run_test 31i "cross directory link under parent==============="
2382
2383 test_31j() {
2384         test_mkdir -c1 -p $DIR/$tdir
2385         test_mkdir -c1 -p $DIR/$tdir/dir1
2386         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2387         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2388         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2389         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2390         return 0
2391 }
2392 run_test 31j "link for directory==============="
2393
2394 test_31k() {
2395         test_mkdir -c1 -p $DIR/$tdir
2396         touch $DIR/$tdir/s
2397         touch $DIR/$tdir/exist
2398         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2399         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2400         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2401         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2402         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2403         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2404         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2405         return 0
2406 }
2407 run_test 31k "link to file: the same, non-existing, dir==============="
2408
2409 test_31m() {
2410         mkdir $DIR/d31m
2411         touch $DIR/d31m/s
2412         mkdir $DIR/d31m2
2413         touch $DIR/d31m2/exist
2414         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2415         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2416         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2417         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2418         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2419         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2420         return 0
2421 }
2422 run_test 31m "link to file: the same, non-existing, dir==============="
2423
2424 test_31n() {
2425         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2426         nlink=$(stat --format=%h $DIR/$tfile)
2427         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2428         local fd=$(free_fd)
2429         local cmd="exec $fd<$DIR/$tfile"
2430         eval $cmd
2431         cmd="exec $fd<&-"
2432         trap "eval $cmd" EXIT
2433         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2434         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2435         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2436         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2437         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2438         eval $cmd
2439 }
2440 run_test 31n "check link count of unlinked file"
2441
2442 link_one() {
2443         local TEMPNAME=$(mktemp $1_XXXXXX)
2444         mlink $TEMPNAME $1 2> /dev/null &&
2445                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2446         munlink $TEMPNAME
2447 }
2448
2449 test_31o() { # LU-2901
2450         test_mkdir -p $DIR/$tdir
2451         for LOOP in $(seq 100); do
2452                 rm -f $DIR/$tdir/$tfile*
2453                 for THREAD in $(seq 8); do
2454                         link_one $DIR/$tdir/$tfile.$LOOP &
2455                 done
2456                 wait
2457                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2458                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2459                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2460                         break || true
2461         done
2462 }
2463 run_test 31o "duplicate hard links with same filename"
2464
2465 test_31p() {
2466         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2467
2468         test_mkdir $DIR/$tdir
2469         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2470         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2471
2472         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2473                 error "open unlink test1 failed"
2474         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2475                 error "open unlink test2 failed"
2476
2477         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2478                 error "test1 still exists"
2479         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2480                 error "test2 still exists"
2481 }
2482 run_test 31p "remove of open striped directory"
2483
2484 cleanup_test32_mount() {
2485         local rc=0
2486         trap 0
2487         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2488         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2489         losetup -d $loopdev || true
2490         rm -rf $DIR/$tdir
2491         return $rc
2492 }
2493
2494 test_32a() {
2495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2496         echo "== more mountpoints and symlinks ================="
2497         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2498         trap cleanup_test32_mount EXIT
2499         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2500         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2501         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2502         cleanup_test32_mount
2503 }
2504 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2505
2506 test_32b() {
2507         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2508         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2509         trap cleanup_test32_mount EXIT
2510         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2511         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2512         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2513         cleanup_test32_mount
2514 }
2515 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2516
2517 test_32c() {
2518         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2519         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2520         trap cleanup_test32_mount EXIT
2521         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2522         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2523         test_mkdir -p $DIR/$tdir/d2/test_dir
2524         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2525         cleanup_test32_mount
2526 }
2527 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2528
2529 test_32d() {
2530         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2531         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2532         trap cleanup_test32_mount EXIT
2533         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2534         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2535         test_mkdir -p $DIR/$tdir/d2/test_dir
2536         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2537         cleanup_test32_mount
2538 }
2539 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir ========="
2540
2541 test_32e() {
2542         [ -e $DIR/d32e ] && rm -fr $DIR/d32e
2543         test_mkdir -p $DIR/d32e/tmp
2544         TMP_DIR=$DIR/d32e/tmp
2545         ln -s $DIR/d32e $TMP_DIR/symlink11
2546         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2547         $CHECKSTAT -t link $DIR/d32e/tmp/symlink11 || error
2548         $CHECKSTAT -t link $DIR/d32e/symlink01 || error
2549 }
2550 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir ===="
2551
2552 test_32f() {
2553         [ -e $DIR/d32f ] && rm -fr $DIR/d32f
2554         test_mkdir -p $DIR/d32f/tmp
2555         TMP_DIR=$DIR/d32f/tmp
2556         ln -s $DIR/d32f $TMP_DIR/symlink11
2557         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2558         ls $DIR/d32f/tmp/symlink11  || error
2559         ls $DIR/d32f/symlink01 || error
2560 }
2561 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir ===="
2562
2563 test_32g() {
2564         TMP_DIR=$DIR/$tdir/tmp
2565         test_mkdir -p $DIR/$tdir/tmp
2566         test_mkdir $DIR/${tdir}2
2567         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2568         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2569         $CHECKSTAT -t link $TMP_DIR/symlink12 || error
2570         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error
2571         $CHECKSTAT -t dir -f $TMP_DIR/symlink12 || error
2572         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error
2573 }
2574 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2575
2576 test_32h() {
2577         rm -fr $DIR/$tdir $DIR/${tdir}2
2578         TMP_DIR=$DIR/$tdir/tmp
2579         test_mkdir -p $DIR/$tdir/tmp
2580         test_mkdir $DIR/${tdir}2
2581         ln -s $DIR/${tdir}2 $TMP_DIR/symlink12
2582         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2583         ls $TMP_DIR/symlink12 || error
2584         ls $DIR/$tdir/symlink02  || error
2585 }
2586 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2587
2588 test_32i() {
2589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2590         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2591         trap cleanup_test32_mount EXIT
2592         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2593         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2594         touch $DIR/$tdir/test_file
2595         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2596         cleanup_test32_mount
2597 }
2598 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2599
2600 test_32j() {
2601         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2602         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2603         trap cleanup_test32_mount EXIT
2604         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2605         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2606         touch $DIR/$tdir/test_file
2607         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2608         cleanup_test32_mount
2609 }
2610 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2611
2612 test_32k() {
2613         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2614         rm -fr $DIR/$tdir
2615         trap cleanup_test32_mount EXIT
2616         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2617         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2618         test_mkdir -p $DIR/$tdir/d2
2619         touch $DIR/$tdir/d2/test_file || error
2620         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2621         cleanup_test32_mount
2622 }
2623 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2624
2625 test_32l() {
2626         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2627         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         test_mkdir -p $DIR/$tdir/d2
2632         touch $DIR/$tdir/d2/test_file
2633         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2634         cleanup_test32_mount
2635 }
2636 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2637
2638 test_32m() {
2639         rm -fr $DIR/d32m
2640         test_mkdir -p $DIR/d32m/tmp
2641         TMP_DIR=$DIR/d32m/tmp
2642         ln -s $DIR $TMP_DIR/symlink11
2643         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2644         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2645         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2646 }
2647 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2648
2649 test_32n() {
2650         rm -fr $DIR/d32n
2651         test_mkdir -p $DIR/d32n/tmp
2652         TMP_DIR=$DIR/d32n/tmp
2653         ln -s $DIR $TMP_DIR/symlink11
2654         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2655         ls -l $DIR/d32n/tmp/symlink11  || error
2656         ls -l $DIR/d32n/symlink01 || error
2657 }
2658 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2659
2660 test_32o() {
2661         touch $DIR/$tfile
2662         test_mkdir -p $DIR/d32o/tmp
2663         TMP_DIR=$DIR/d32o/tmp
2664         ln -s $DIR/$tfile $TMP_DIR/symlink12
2665         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2666         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2667         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2668         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2669         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2670 }
2671 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2672
2673 test_32p() {
2674         log 32p_1
2675         rm -fr $DIR/d32p
2676         log 32p_2
2677         rm -f $DIR/$tfile
2678         log 32p_3
2679         touch $DIR/$tfile
2680         log 32p_4
2681         test_mkdir -p $DIR/d32p/tmp
2682         log 32p_5
2683         TMP_DIR=$DIR/d32p/tmp
2684         log 32p_6
2685         ln -s $DIR/$tfile $TMP_DIR/symlink12
2686         log 32p_7
2687         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2688         log 32p_8
2689         cat $DIR/d32p/tmp/symlink12 || error
2690         log 32p_9
2691         cat $DIR/d32p/symlink02 || error
2692         log 32p_10
2693 }
2694 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2695
2696 test_32q() {
2697         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2698         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2699         trap cleanup_test32_mount EXIT
2700         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2701         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2702         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2703         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2704         cleanup_test32_mount
2705 }
2706 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2707
2708 test_32r() {
2709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2710         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2711         trap cleanup_test32_mount EXIT
2712         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2713         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2714         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2715         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2716         cleanup_test32_mount
2717 }
2718 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2719
2720 test_33aa() {
2721         rm -f $DIR/$tfile
2722         touch $DIR/$tfile
2723         chmod 444 $DIR/$tfile
2724         chown $RUNAS_ID $DIR/$tfile
2725         log 33_1
2726         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2727         log 33_2
2728 }
2729 run_test 33aa "write file with mode 444 (should return error) ===="
2730
2731 test_33a() {
2732         rm -fr $DIR/d33
2733         test_mkdir -p $DIR/d33
2734         chown $RUNAS_ID $DIR/d33
2735         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33|| error "create"
2736         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/d33/f33 && \
2737                 error "open RDWR" || true
2738 }
2739 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2740
2741 test_33b() {
2742         rm -fr $DIR/d33
2743         test_mkdir -p $DIR/d33
2744         chown $RUNAS_ID $DIR/d33
2745         $RUNAS $OPENFILE -f 1286739555 $DIR/d33/f33 || true
2746 }
2747 run_test 33b "test open file with malformed flags (No panic)"
2748
2749 test_33c() {
2750         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2751         local ostnum
2752         local ostname
2753         local write_bytes
2754         local all_zeros
2755
2756         remote_ost_nodsh && skip "remote OST with nodsh" && return
2757         all_zeros=:
2758         rm -fr $DIR/d33
2759         test_mkdir -p $DIR/d33
2760         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2761
2762         sync
2763         for ostnum in $(seq $OSTCOUNT); do
2764                 # test-framework's OST numbering is one-based, while Lustre's
2765                 # is zero-based
2766                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2767                 # Parsing llobdstat's output sucks; we could grep the /proc
2768                 # path, but that's likely to not be as portable as using the
2769                 # llobdstat utility.  So we parse lctl output instead.
2770                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2771                         obdfilter/$ostname/stats |
2772                         awk '/^write_bytes/ {print $7}' )
2773                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2774                 if (( ${write_bytes:-0} > 0 ))
2775                 then
2776                         all_zeros=false
2777                         break;
2778                 fi
2779         done
2780
2781         $all_zeros || return 0
2782
2783         # Write four bytes
2784         echo foo > $DIR/d33/bar
2785         # Really write them
2786         sync
2787
2788         # Total up write_bytes after writing.  We'd better find non-zeros.
2789         for ostnum in $(seq $OSTCOUNT); do
2790                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2791                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2792                         obdfilter/$ostname/stats |
2793                         awk '/^write_bytes/ {print $7}' )
2794                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2795                 if (( ${write_bytes:-0} > 0 ))
2796                 then
2797                         all_zeros=false
2798                         break;
2799                 fi
2800         done
2801
2802         if $all_zeros
2803         then
2804                 for ostnum in $(seq $OSTCOUNT); do
2805                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2806                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2807                         do_facet ost$ostnum lctl get_param -n \
2808                                 obdfilter/$ostname/stats
2809                 done
2810                 error "OST not keeping write_bytes stats (b22312)"
2811         fi
2812 }
2813 run_test 33c "test llobdstat and write_bytes"
2814
2815 test_33d() {
2816         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2818         local MDTIDX=1
2819         local remote_dir=$DIR/$tdir/remote_dir
2820
2821         test_mkdir -p $DIR/$tdir
2822         $LFS mkdir -i $MDTIDX $remote_dir ||
2823                 error "create remote directory failed"
2824
2825         touch $remote_dir/$tfile
2826         chmod 444 $remote_dir/$tfile
2827         chown $RUNAS_ID $remote_dir/$tfile
2828
2829         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2830
2831         chown $RUNAS_ID $remote_dir
2832         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2833                                         error "create" || true
2834         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2835                                     error "open RDWR" || true
2836         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2837 }
2838 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2839
2840 test_33e() {
2841         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2842
2843         mkdir $DIR/$tdir
2844
2845         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2846         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2847         mkdir $DIR/$tdir/local_dir
2848
2849         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2850         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2851         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2852
2853         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2854                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2855
2856         rmdir $DIR/$tdir/* || error "rmdir failed"
2857
2858         umask 777
2859         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2860         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2861         mkdir $DIR/$tdir/local_dir
2862
2863         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2864         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2865         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2866
2867         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2868                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2869
2870         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2871
2872         umask 000
2873         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2874         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2875         mkdir $DIR/$tdir/local_dir
2876
2877         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2878         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2879         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2880
2881         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2882                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2883 }
2884 run_test 33e "mkdir and striped directory should have same mode"
2885
2886 cleanup_33f() {
2887         trap 0
2888         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2889 }
2890
2891 test_33f() {
2892         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2893         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2894
2895         mkdir $DIR/$tdir
2896         chmod go+rwx $DIR/$tdir
2897         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2898         trap cleanup_33f EXIT
2899
2900         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2901                 error "cannot create striped directory"
2902
2903         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2904                 error "cannot create files in striped directory"
2905
2906         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2907                 error "cannot remove files in striped directory"
2908
2909         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2910                 error "cannot remove striped directory"
2911
2912         cleanup_33f
2913 }
2914 run_test 33f "nonroot user can create, access, and remove a striped directory"
2915
2916 test_33g() {
2917         mkdir -p $DIR/$tdir/dir2
2918
2919         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2920         echo $err
2921         [[ $err =~ "exists" ]] || error "Not exists error"
2922 }
2923 run_test 33g "nonroot user create already existing root created file"
2924
2925 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2926 test_34a() {
2927         rm -f $DIR/f34
2928         $MCREATE $DIR/f34 || error
2929         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2930         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2931         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2932         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2933 }
2934 run_test 34a "truncate file that has not been opened ==========="
2935
2936 test_34b() {
2937         [ ! -f $DIR/f34 ] && test_34a
2938         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2939         $OPENFILE -f O_RDONLY $DIR/f34
2940         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2941         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2942 }
2943 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2944
2945 test_34c() {
2946         [ ! -f $DIR/f34 ] && test_34a
2947         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2948         $OPENFILE -f O_RDWR $DIR/f34
2949         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2950         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2951 }
2952 run_test 34c "O_RDWR opening file-with-size works =============="
2953
2954 test_34d() {
2955         [ ! -f $DIR/f34 ] && test_34a
2956         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2957         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2958         rm $DIR/f34
2959 }
2960 run_test 34d "write to sparse file ============================="
2961
2962 test_34e() {
2963         rm -f $DIR/f34e
2964         $MCREATE $DIR/f34e || error
2965         $TRUNCATE $DIR/f34e 1000 || error
2966         $CHECKSTAT -s 1000 $DIR/f34e || error
2967         $OPENFILE -f O_RDWR $DIR/f34e
2968         $CHECKSTAT -s 1000 $DIR/f34e || error
2969 }
2970 run_test 34e "create objects, some with size and some without =="
2971
2972 test_34f() { # bug 6242, 6243
2973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2974         SIZE34F=48000
2975         rm -f $DIR/f34f
2976         $MCREATE $DIR/f34f || error
2977         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2978         dd if=$DIR/f34f of=$TMP/f34f
2979         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2980         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2981         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2982         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2983         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2984 }
2985 run_test 34f "read from a file with no objects until EOF ======="
2986
2987 test_34g() {
2988         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2989         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2990         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2991         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2992         cancel_lru_locks osc
2993         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2994                 error "wrong size after lock cancel"
2995
2996         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2997         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2998                 error "expanding truncate failed"
2999         cancel_lru_locks osc
3000         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
3001                 error "wrong expanded size after lock cancel"
3002 }
3003 run_test 34g "truncate long file ==============================="
3004
3005 test_34h() {
3006         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3007         local gid=10
3008         local sz=1000
3009
3010         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
3011         sync # Flush the cache so that multiop below does not block on cache
3012              # flush when getting the group lock
3013         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3014         MULTIPID=$!
3015
3016         # Since just timed wait is not good enough, let's do a sync write
3017         # that way we are sure enough time for a roundtrip + processing
3018         # passed + 2 seconds of extra margin.
3019         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
3020         rm $DIR/${tfile}-1
3021         sleep 2
3022
3023         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3024                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3025                 kill -9 $MULTIPID
3026         fi
3027         wait $MULTIPID
3028         local nsz=`stat -c %s $DIR/$tfile`
3029         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3030 }
3031 run_test 34h "ftruncate file under grouplock should not block"
3032
3033 test_35a() {
3034         cp /bin/sh $DIR/f35a
3035         chmod 444 $DIR/f35a
3036         chown $RUNAS_ID $DIR/f35a
3037         $RUNAS $DIR/f35a && error || true
3038         rm $DIR/f35a
3039 }
3040 run_test 35a "exec file with mode 444 (should return and not leak) ====="
3041
3042 test_36a() {
3043         rm -f $DIR/f36
3044         utime $DIR/f36 || error
3045 }
3046 run_test 36a "MDS utime check (mknod, utime) ==================="
3047
3048 test_36b() {
3049         echo "" > $DIR/f36
3050         utime $DIR/f36 || error
3051 }
3052 run_test 36b "OST utime check (open, utime) ===================="
3053
3054 test_36c() {
3055         rm -f $DIR/d36/f36
3056         test_mkdir $DIR/d36
3057         chown $RUNAS_ID $DIR/d36
3058         $RUNAS utime $DIR/d36/f36 || error
3059 }
3060 run_test 36c "non-root MDS utime check (mknod, utime) =========="
3061
3062 test_36d() {
3063         [ ! -d $DIR/d36 ] && test_36c
3064         echo "" > $DIR/d36/f36
3065         $RUNAS utime $DIR/d36/f36 || error
3066 }
3067 run_test 36d "non-root OST utime check (open, utime) ==========="
3068
3069 test_36e() {
3070         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3071         test_mkdir -p $DIR/$tdir
3072         touch $DIR/$tdir/$tfile
3073         $RUNAS utime $DIR/$tdir/$tfile && \
3074                 error "utime worked, expected failure" || true
3075 }
3076 run_test 36e "utime on non-owned file (should return error) ===="
3077
3078 subr_36fh() {
3079         local fl="$1"
3080         local LANG_SAVE=$LANG
3081         local LC_LANG_SAVE=$LC_LANG
3082         export LANG=C LC_LANG=C # for date language
3083
3084         DATESTR="Dec 20  2000"
3085         test_mkdir -p $DIR/$tdir
3086         lctl set_param fail_loc=$fl
3087         date; date +%s
3088         cp /etc/hosts $DIR/$tdir/$tfile
3089         sync & # write RPC generated with "current" inode timestamp, but delayed
3090         sleep 1
3091         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3092         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3093         cancel_lru_locks osc
3094         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3095         date; date +%s
3096         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3097                 echo "BEFORE: $LS_BEFORE" && \
3098                 echo "AFTER : $LS_AFTER" && \
3099                 echo "WANT  : $DATESTR" && \
3100                 error "$DIR/$tdir/$tfile timestamps changed" || true
3101
3102         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3103 }
3104
3105 test_36f() {
3106         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3107         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3108         subr_36fh "0x80000214"
3109 }
3110 run_test 36f "utime on file racing with OST BRW write =========="
3111
3112 test_36g() {
3113         remote_ost_nodsh && skip "remote OST with nodsh" && return
3114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3115         local fmd_max_age
3116         local fmd_before
3117         local fmd_after
3118
3119         test_mkdir -p $DIR/$tdir
3120         fmd_max_age=$(do_facet ost1 \
3121                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3122                 head -n 1")
3123
3124         fmd_before=$(do_facet ost1 \
3125                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3126         touch $DIR/$tdir/$tfile
3127         sleep $((fmd_max_age + 12))
3128         fmd_after=$(do_facet ost1 \
3129                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3130
3131         echo "fmd_before: $fmd_before"
3132         echo "fmd_after: $fmd_after"
3133         [[ $fmd_after -gt $fmd_before ]] &&
3134                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3135                 error "fmd didn't expire after ping" || true
3136 }
3137 run_test 36g "filter mod data cache expiry ====================="
3138
3139 test_36h() {
3140         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3141         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3142         subr_36fh "0x80000227"
3143 }
3144 run_test 36h "utime on file racing with OST BRW write =========="
3145
3146 test_36i() {
3147         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3148
3149         test_mkdir $DIR/$tdir
3150         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3151
3152         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3153         local new_mtime=$((mtime + 200))
3154
3155         #change Modify time of striped dir
3156         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3157                         error "change mtime failed"
3158
3159         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3160
3161         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3162 }
3163 run_test 36i "change mtime on striped directory"
3164
3165 # test_37 - duplicate with tests 32q 32r
3166
3167 test_38() {
3168         local file=$DIR/$tfile
3169         touch $file
3170         openfile -f O_DIRECTORY $file
3171         local RC=$?
3172         local ENOTDIR=20
3173         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3174         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3175 }
3176 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3177
3178 test_39a() { # was test_39
3179         touch $DIR/$tfile
3180         touch $DIR/${tfile}2
3181 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3182 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3183 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3184         sleep 2
3185         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3186         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3187                 echo "mtime"
3188                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3189                 echo "atime"
3190                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3191                 echo "ctime"
3192                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3193                 error "O_TRUNC didn't change timestamps"
3194         fi
3195 }
3196 run_test 39a "mtime changed on create ==========================="
3197
3198 test_39b() {
3199         test_mkdir -p -c1 $DIR/$tdir
3200         cp -p /etc/passwd $DIR/$tdir/fopen
3201         cp -p /etc/passwd $DIR/$tdir/flink
3202         cp -p /etc/passwd $DIR/$tdir/funlink
3203         cp -p /etc/passwd $DIR/$tdir/frename
3204         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3205
3206         sleep 1
3207         echo "aaaaaa" >> $DIR/$tdir/fopen
3208         echo "aaaaaa" >> $DIR/$tdir/flink
3209         echo "aaaaaa" >> $DIR/$tdir/funlink
3210         echo "aaaaaa" >> $DIR/$tdir/frename
3211
3212         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3213         local link_new=`stat -c %Y $DIR/$tdir/flink`
3214         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3215         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3216
3217         cat $DIR/$tdir/fopen > /dev/null
3218         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3219         rm -f $DIR/$tdir/funlink2
3220         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3221
3222         for (( i=0; i < 2; i++ )) ; do
3223                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3224                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3225                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3226                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3227
3228                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3229                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3230                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3231                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3232
3233                 cancel_lru_locks osc
3234                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3235         done
3236 }
3237 run_test 39b "mtime change on open, link, unlink, rename  ======"
3238
3239 # this should be set to past
3240 TEST_39_MTIME=`date -d "1 year ago" +%s`
3241
3242 # bug 11063
3243 test_39c() {
3244         touch $DIR1/$tfile
3245         sleep 2
3246         local mtime0=`stat -c %Y $DIR1/$tfile`
3247
3248         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3249         local mtime1=`stat -c %Y $DIR1/$tfile`
3250         [ "$mtime1" = $TEST_39_MTIME ] || \
3251                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3252
3253         local d1=`date +%s`
3254         echo hello >> $DIR1/$tfile
3255         local d2=`date +%s`
3256         local mtime2=`stat -c %Y $DIR1/$tfile`
3257         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3258                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3259
3260         mv $DIR1/$tfile $DIR1/$tfile-1
3261
3262         for (( i=0; i < 2; i++ )) ; do
3263                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3264                 [ "$mtime2" = "$mtime3" ] || \
3265                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3266
3267                 cancel_lru_locks osc
3268                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3269         done
3270 }
3271 run_test 39c "mtime change on rename ==========================="
3272
3273 # bug 21114
3274 test_39d() {
3275         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3276         touch $DIR1/$tfile
3277
3278         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3279
3280         for (( i=0; i < 2; i++ )) ; do
3281                 local mtime=`stat -c %Y $DIR1/$tfile`
3282                 [ $mtime = $TEST_39_MTIME ] || \
3283                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3284
3285                 cancel_lru_locks osc
3286                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3287         done
3288 }
3289 run_test 39d "create, utime, stat =============================="
3290
3291 # bug 21114
3292 test_39e() {
3293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3294         touch $DIR1/$tfile
3295         local mtime1=`stat -c %Y $DIR1/$tfile`
3296
3297         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3298
3299         for (( i=0; i < 2; i++ )) ; do
3300                 local mtime2=`stat -c %Y $DIR1/$tfile`
3301                 [ $mtime2 = $TEST_39_MTIME ] || \
3302                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3303
3304                 cancel_lru_locks osc
3305                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3306         done
3307 }
3308 run_test 39e "create, stat, utime, stat ========================"
3309
3310 # bug 21114
3311 test_39f() {
3312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3313         touch $DIR1/$tfile
3314         mtime1=`stat -c %Y $DIR1/$tfile`
3315
3316         sleep 2
3317         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3318
3319         for (( i=0; i < 2; i++ )) ; do
3320                 local mtime2=`stat -c %Y $DIR1/$tfile`
3321                 [ $mtime2 = $TEST_39_MTIME ] || \
3322                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3323
3324                 cancel_lru_locks osc
3325                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3326         done
3327 }
3328 run_test 39f "create, stat, sleep, utime, stat ================="
3329
3330 # bug 11063
3331 test_39g() {
3332         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3333         echo hello >> $DIR1/$tfile
3334         local mtime1=`stat -c %Y $DIR1/$tfile`
3335
3336         sleep 2
3337         chmod o+r $DIR1/$tfile
3338
3339         for (( i=0; i < 2; i++ )) ; do
3340                 local mtime2=`stat -c %Y $DIR1/$tfile`
3341                 [ "$mtime1" = "$mtime2" ] || \
3342                         error "lost mtime: $mtime2, should be $mtime1"
3343
3344                 cancel_lru_locks osc
3345                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3346         done
3347 }
3348 run_test 39g "write, chmod, stat ==============================="
3349
3350 # bug 11063
3351 test_39h() {
3352         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3353         touch $DIR1/$tfile
3354         sleep 1
3355
3356         local d1=`date`
3357         echo hello >> $DIR1/$tfile
3358         local mtime1=`stat -c %Y $DIR1/$tfile`
3359
3360         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3361         local d2=`date`
3362         if [ "$d1" != "$d2" ]; then
3363                 echo "write and touch not within one second"
3364         else
3365                 for (( i=0; i < 2; i++ )) ; do
3366                         local mtime2=`stat -c %Y $DIR1/$tfile`
3367                         [ "$mtime2" = $TEST_39_MTIME ] || \
3368                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3369
3370                         cancel_lru_locks osc
3371                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3372                 done
3373         fi
3374 }
3375 run_test 39h "write, utime within one second, stat ============="
3376
3377 test_39i() {
3378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3379         touch $DIR1/$tfile
3380         sleep 1
3381
3382         echo hello >> $DIR1/$tfile
3383         local mtime1=`stat -c %Y $DIR1/$tfile`
3384
3385         mv $DIR1/$tfile $DIR1/$tfile-1
3386
3387         for (( i=0; i < 2; i++ )) ; do
3388                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3389
3390                 [ "$mtime1" = "$mtime2" ] || \
3391                         error "lost mtime: $mtime2, should be $mtime1"
3392
3393                 cancel_lru_locks osc
3394                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3395         done
3396 }
3397 run_test 39i "write, rename, stat =============================="
3398
3399 test_39j() {
3400         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3401         start_full_debug_logging
3402         touch $DIR1/$tfile
3403         sleep 1
3404
3405         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3406         lctl set_param fail_loc=0x80000412
3407         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3408                 error "multiop failed"
3409         local multipid=$!
3410         local mtime1=`stat -c %Y $DIR1/$tfile`
3411
3412         mv $DIR1/$tfile $DIR1/$tfile-1
3413
3414         kill -USR1 $multipid
3415         wait $multipid || error "multiop close failed"
3416
3417         for (( i=0; i < 2; i++ )) ; do
3418                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3419                 [ "$mtime1" = "$mtime2" ] ||
3420                         error "mtime is lost on close: $mtime2, " \
3421                               "should be $mtime1"
3422
3423                 cancel_lru_locks osc
3424                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3425         done
3426         lctl set_param fail_loc=0
3427         stop_full_debug_logging
3428 }
3429 run_test 39j "write, rename, close, stat ======================="
3430
3431 test_39k() {
3432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3433         touch $DIR1/$tfile
3434         sleep 1
3435
3436         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3437         local multipid=$!
3438         local mtime1=`stat -c %Y $DIR1/$tfile`
3439
3440         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3441
3442         kill -USR1 $multipid
3443         wait $multipid || error "multiop close failed"
3444
3445         for (( i=0; i < 2; i++ )) ; do
3446                 local mtime2=`stat -c %Y $DIR1/$tfile`
3447
3448                 [ "$mtime2" = $TEST_39_MTIME ] || \
3449                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3450
3451                 cancel_lru_locks osc
3452                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3453         done
3454 }
3455 run_test 39k "write, utime, close, stat ========================"
3456
3457 # this should be set to future
3458 TEST_39_ATIME=`date -d "1 year" +%s`
3459
3460 test_39l() {
3461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3462         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3463         local atime_diff=$(do_facet $SINGLEMDS \
3464                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3465         rm -rf $DIR/$tdir
3466         mkdir -p $DIR/$tdir
3467
3468         # test setting directory atime to future
3469         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3470         local atime=$(stat -c %X $DIR/$tdir)
3471         [ "$atime" = $TEST_39_ATIME ] ||
3472                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3473
3474         # test setting directory atime from future to now
3475         local now=$(date +%s)
3476         touch -a -d @$now $DIR/$tdir
3477
3478         atime=$(stat -c %X $DIR/$tdir)
3479         [ "$atime" -eq "$now"  ] ||
3480                 error "atime is not updated from future: $atime, $now"
3481
3482         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3483         sleep 3
3484
3485         # test setting directory atime when now > dir atime + atime_diff
3486         local d1=$(date +%s)
3487         ls $DIR/$tdir
3488         local d2=$(date +%s)
3489         cancel_lru_locks mdc
3490         atime=$(stat -c %X $DIR/$tdir)
3491         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3492                 error "atime is not updated  : $atime, should be $d2"
3493
3494         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3495         sleep 3
3496
3497         # test not setting directory atime when now < dir atime + atime_diff
3498         ls $DIR/$tdir
3499         cancel_lru_locks mdc
3500         atime=$(stat -c %X $DIR/$tdir)
3501         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3502                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3503
3504         do_facet $SINGLEMDS \
3505                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3506 }
3507 run_test 39l "directory atime update ==========================="
3508
3509 test_39m() {
3510         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3511         touch $DIR1/$tfile
3512         sleep 2
3513         local far_past_mtime=$(date -d "May 29 1953" +%s)
3514         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3515
3516         touch -m -d @$far_past_mtime $DIR1/$tfile
3517         touch -a -d @$far_past_atime $DIR1/$tfile
3518
3519         for (( i=0; i < 2; i++ )) ; do
3520                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3521                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3522                         error "atime or mtime set incorrectly"
3523
3524                 cancel_lru_locks osc
3525                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3526         done
3527 }
3528 run_test 39m "test atime and mtime before 1970"
3529
3530 test_39n() { # LU-3832
3531         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3532         local atime_diff=$(do_facet $SINGLEMDS \
3533                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3534         local atime0
3535         local atime1
3536         local atime2
3537
3538         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3539
3540         rm -rf $DIR/$tfile
3541         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3542         atime0=$(stat -c %X $DIR/$tfile)
3543
3544         sleep 5
3545         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3546         atime1=$(stat -c %X $DIR/$tfile)
3547
3548         sleep 5
3549         cancel_lru_locks mdc
3550         cancel_lru_locks osc
3551         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3552         atime2=$(stat -c %X $DIR/$tfile)
3553
3554         do_facet $SINGLEMDS \
3555                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3556
3557         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3558         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3559 }
3560 run_test 39n "check that O_NOATIME is honored"
3561
3562 test_39o() {
3563         TESTDIR=$DIR/$tdir/$tfile
3564         [ -e $TESTDIR ] && rm -rf $TESTDIR
3565         mkdir -p $TESTDIR
3566         cd $TESTDIR
3567         links1=2
3568         ls
3569         mkdir a b
3570         ls
3571         links2=$(stat -c %h .)
3572         [ $(($links1 + 2)) != $links2 ] &&
3573                 error "wrong links count $(($links1 + 2)) != $links2"
3574         rmdir b
3575         links3=$(stat -c %h .)
3576         [ $(($links1 + 1)) != $links3 ] &&
3577                 error "wrong links count $links1 != $links3"
3578         return 0
3579 }
3580 run_test 39o "directory cached attributes updated after create ========"
3581
3582 test_39p() {
3583         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3584         local MDTIDX=1
3585         TESTDIR=$DIR/$tdir/$tfile
3586         [ -e $TESTDIR ] && rm -rf $TESTDIR
3587         test_mkdir -p $TESTDIR
3588         cd $TESTDIR
3589         links1=2
3590         ls
3591         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir1
3592         $LFS mkdir -i $MDTIDX $TESTDIR/remote_dir2
3593         ls
3594         links2=$(stat -c %h .)
3595         [ $(($links1 + 2)) != $links2 ] &&
3596                 error "wrong links count $(($links1 + 2)) != $links2"
3597         rmdir remote_dir2
3598         links3=$(stat -c %h .)
3599         [ $(($links1 + 1)) != $links3 ] &&
3600                 error "wrong links count $links1 != $links3"
3601         return 0
3602 }
3603 run_test 39p "remote directory cached attributes updated after create ========"
3604
3605
3606 test_39q() { # LU-8041
3607         local testdir=$DIR/$tdir
3608         mkdir -p $testdir
3609         multiop_bg_pause $testdir D_c || error "multiop failed"
3610         local multipid=$!
3611         cancel_lru_locks mdc
3612         kill -USR1 $multipid
3613         local atime=$(stat -c %X $testdir)
3614         [ "$atime" -ne 0 ] || error "atime is zero"
3615 }
3616 run_test 39q "close won't zero out atime"
3617
3618 test_40() {
3619         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3620         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3621                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3622         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3623                 error "$tfile is not 4096 bytes in size"
3624 }
3625 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3626
3627 test_41() {
3628         # bug 1553
3629         small_write $DIR/f41 18
3630 }
3631 run_test 41 "test small file write + fstat ====================="
3632
3633 count_ost_writes() {
3634         lctl get_param -n osc.*.stats |
3635                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3636                         END { printf("%0.0f", writes) }'
3637 }
3638
3639 # decent default
3640 WRITEBACK_SAVE=500
3641 DIRTY_RATIO_SAVE=40
3642 MAX_DIRTY_RATIO=50
3643 BG_DIRTY_RATIO_SAVE=10
3644 MAX_BG_DIRTY_RATIO=25
3645
3646 start_writeback() {
3647         trap 0
3648         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3649         # dirty_ratio, dirty_background_ratio
3650         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3651                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3652                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3653                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3654         else
3655                 # if file not here, we are a 2.4 kernel
3656                 kill -CONT `pidof kupdated`
3657         fi
3658 }
3659
3660 stop_writeback() {
3661         # setup the trap first, so someone cannot exit the test at the
3662         # exact wrong time and mess up a machine
3663         trap start_writeback EXIT
3664         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3665         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3666                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3667                 sysctl -w vm.dirty_writeback_centisecs=0
3668                 sysctl -w vm.dirty_writeback_centisecs=0
3669                 # save and increase /proc/sys/vm/dirty_ratio
3670                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3671                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3672                 # save and increase /proc/sys/vm/dirty_background_ratio
3673                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3674                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3675         else
3676                 # if file not here, we are a 2.4 kernel
3677                 kill -STOP `pidof kupdated`
3678         fi
3679 }
3680
3681 # ensure that all stripes have some grant before we test client-side cache
3682 setup_test42() {
3683         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3684                 dd if=/dev/zero of=$i bs=4k count=1
3685                 rm $i
3686         done
3687 }
3688
3689 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3690 # file truncation, and file removal.
3691 test_42a() {
3692         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3693         setup_test42
3694         cancel_lru_locks osc
3695         stop_writeback
3696         sync; sleep 1; sync # just to be safe
3697         BEFOREWRITES=`count_ost_writes`
3698         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3699         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3700         AFTERWRITES=`count_ost_writes`
3701         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3702                 error "$BEFOREWRITES < $AFTERWRITES"
3703         start_writeback
3704 }
3705 run_test 42a "ensure that we don't flush on close"
3706
3707 test_42b() {
3708         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3709         setup_test42
3710         cancel_lru_locks osc
3711         stop_writeback
3712         sync
3713         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3714         BEFOREWRITES=$(count_ost_writes)
3715         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3716         AFTERWRITES=$(count_ost_writes)
3717         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3718                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3719         fi
3720         BEFOREWRITES=$(count_ost_writes)
3721         sync || error "sync: $?"
3722         AFTERWRITES=$(count_ost_writes)
3723         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3724                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3725         fi
3726         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3727         start_writeback
3728         return 0
3729 }
3730 run_test 42b "test destroy of file with cached dirty data ======"
3731
3732 # if these tests just want to test the effect of truncation,
3733 # they have to be very careful.  consider:
3734 # - the first open gets a {0,EOF}PR lock
3735 # - the first write conflicts and gets a {0, count-1}PW
3736 # - the rest of the writes are under {count,EOF}PW
3737 # - the open for truncate tries to match a {0,EOF}PR
3738 #   for the filesize and cancels the PWs.
3739 # any number of fixes (don't get {0,EOF} on open, match
3740 # composite locks, do smarter file size management) fix
3741 # this, but for now we want these tests to verify that
3742 # the cancellation with truncate intent works, so we
3743 # start the file with a full-file pw lock to match against
3744 # until the truncate.
3745 trunc_test() {
3746         test=$1
3747         file=$DIR/$test
3748         offset=$2
3749         cancel_lru_locks osc
3750         stop_writeback
3751         # prime the file with 0,EOF PW to match
3752         touch $file
3753         $TRUNCATE $file 0
3754         sync; sync
3755         # now the real test..
3756         dd if=/dev/zero of=$file bs=1024 count=100
3757         BEFOREWRITES=`count_ost_writes`
3758         $TRUNCATE $file $offset
3759         cancel_lru_locks osc
3760         AFTERWRITES=`count_ost_writes`
3761         start_writeback
3762 }
3763
3764 test_42c() {
3765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3766         trunc_test 42c 1024
3767         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3768             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3769         rm $file
3770 }
3771 run_test 42c "test partial truncate of file with cached dirty data"
3772
3773 test_42d() {
3774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3775         trunc_test 42d 0
3776         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3777             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3778         rm $file
3779 }
3780 run_test 42d "test complete truncate of file with cached dirty data"
3781
3782 test_42e() { # bug22074
3783         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3784         local TDIR=$DIR/${tdir}e
3785         local pagesz=$(page_size)
3786         local pages=16 # hardcoded 16 pages, don't change it.
3787         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3788         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3789         local max_dirty_mb
3790         local warmup_files
3791
3792         test_mkdir -p $DIR/${tdir}e
3793         $SETSTRIPE -c 1 $TDIR
3794         createmany -o $TDIR/f $files
3795
3796         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3797
3798         # we assume that with $OSTCOUNT files, at least one of them will
3799         # be allocated on OST0.
3800         warmup_files=$((OSTCOUNT * max_dirty_mb))
3801         createmany -o $TDIR/w $warmup_files
3802
3803         # write a large amount of data into one file and sync, to get good
3804         # avail_grant number from OST.
3805         for ((i=0; i<$warmup_files; i++)); do
3806                 idx=$($GETSTRIPE -i $TDIR/w$i)
3807                 [ $idx -ne 0 ] && continue
3808                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3809                 break
3810         done
3811         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3812         sync
3813         $LCTL get_param $proc_osc0/cur_dirty_bytes
3814         $LCTL get_param $proc_osc0/cur_grant_bytes
3815
3816         # create as much dirty pages as we can while not to trigger the actual
3817         # RPCs directly. but depends on the env, VFS may trigger flush during this
3818         # period, hopefully we are good.
3819         for ((i=0; i<$warmup_files; i++)); do
3820                 idx=$($GETSTRIPE -i $TDIR/w$i)
3821                 [ $idx -ne 0 ] && continue
3822                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3823         done
3824         $LCTL get_param $proc_osc0/cur_dirty_bytes
3825         $LCTL get_param $proc_osc0/cur_grant_bytes
3826
3827         # perform the real test
3828         $LCTL set_param $proc_osc0/rpc_stats 0
3829         for ((;i<$files; i++)); do
3830                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3831                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3832         done
3833         sync
3834         $LCTL get_param $proc_osc0/rpc_stats
3835
3836         local percent=0
3837         local have_ppr=false
3838         $LCTL get_param $proc_osc0/rpc_stats |
3839                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3840                         # skip lines until we are at the RPC histogram data
3841                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3842                         $have_ppr || continue
3843
3844                         # we only want the percent stat for < 16 pages
3845                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3846
3847                         percent=$((percent + WPCT))
3848                         if [[ $percent -gt 15 ]]; then
3849                                 error "less than 16-pages write RPCs" \
3850                                       "$percent% > 15%"
3851                                 break
3852                         fi
3853                 done
3854         rm -rf $TDIR
3855 }
3856 run_test 42e "verify sub-RPC writes are not done synchronously"
3857
3858 test_43A() { # was test_43
3859         test_mkdir -p $DIR/$tdir
3860         cp -p /bin/ls $DIR/$tdir/$tfile
3861         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3862         pid=$!
3863         # give multiop a chance to open
3864         sleep 1
3865
3866         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
3867         kill -USR1 $pid
3868 }
3869 run_test 43A "execution of file opened for write should return -ETXTBSY"
3870
3871 test_43a() {
3872         test_mkdir $DIR/$tdir
3873         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3874                 cp -p multiop $DIR/$tdir/multiop
3875         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3876                 error "multiop open $TMP/$tfile.junk failed"
3877         MULTIOP_PID=$!
3878         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3879         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3880         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3881 }
3882 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3883
3884 test_43b() {
3885         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3886         test_mkdir $DIR/$tdir
3887         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3888                 cp -p multiop $DIR/$tdir/multiop
3889         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3890                 error "multiop open $TMP/$tfile.junk failed"
3891         MULTIOP_PID=$!
3892         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3893         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3894         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3895 }
3896 run_test 43b "truncate of file being executed should return -ETXTBSY"
3897
3898 test_43c() {
3899         local testdir="$DIR/$tdir"
3900         test_mkdir $testdir
3901         cp $SHELL $testdir/
3902         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
3903                 ( cd $testdir && md5sum -c )
3904 }
3905 run_test 43c "md5sum of copy into lustre"
3906
3907 test_44A() { # was test_44
3908         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
3909         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3910         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3911 }
3912 run_test 44A "zero length read from a sparse stripe"
3913
3914 test_44a() {
3915         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3916                 awk '{ print $2 }')
3917         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3918         [[ $nstripe -gt $OSTCOUNT ]] &&
3919             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3920             return
3921         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3922                 awk '{ print $2 }')
3923         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3924                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3925                         awk '{ print $2 }')
3926         fi
3927
3928         OFFSETS="0 $((stride/2)) $((stride-1))"
3929         for offset in $OFFSETS; do
3930                 for i in $(seq 0 $((nstripe-1))); do
3931                         local GLOBALOFFSETS=""
3932                         # size in Bytes
3933                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3934                         local myfn=$DIR/d44a-$size
3935                         echo "--------writing $myfn at $size"
3936                         ll_sparseness_write $myfn $size ||
3937                                 error "ll_sparseness_write"
3938                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3939                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3940                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3941
3942                         for j in $(seq 0 $((nstripe-1))); do
3943                                 # size in Bytes
3944                                 size=$((((j + $nstripe )*$stride + $offset)))
3945                                 ll_sparseness_write $myfn $size ||
3946                                         error "ll_sparseness_write"
3947                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3948                         done
3949                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3950                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3951                         rm -f $myfn
3952                 done
3953         done
3954 }
3955 run_test 44a "test sparse pwrite ==============================="
3956
3957 dirty_osc_total() {
3958         tot=0
3959         for d in `lctl get_param -n osc.*.cur_dirty_bytes`; do
3960                 tot=$(($tot + $d))
3961         done
3962         echo $tot
3963 }
3964 do_dirty_record() {
3965         before=`dirty_osc_total`
3966         echo executing "\"$*\""
3967         eval $*
3968         after=`dirty_osc_total`
3969         echo before $before, after $after
3970 }
3971 test_45() {
3972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3973         f="$DIR/f45"
3974         # Obtain grants from OST if it supports it
3975         echo blah > ${f}_grant
3976         stop_writeback
3977         sync
3978         do_dirty_record "echo blah > $f"
3979         [[ $before -eq $after ]] && error "write wasn't cached"
3980         do_dirty_record "> $f"
3981         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3982         do_dirty_record "echo blah > $f"
3983         [[ $before -eq $after ]] && error "write wasn't cached"
3984         do_dirty_record "sync"
3985         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3986         do_dirty_record "echo blah > $f"
3987         [[ $before -eq $after ]] && error "write wasn't cached"
3988         do_dirty_record "cancel_lru_locks osc"
3989         [[ $before -gt $after ]] ||
3990                 error "lock cancellation didn't lower dirty count"
3991         start_writeback
3992 }
3993 run_test 45 "osc io page accounting ============================"
3994
3995 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3996 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3997 # objects offset and an assert hit when an rpc was built with 1023's mapped
3998 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3999 test_46() {
4000         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4001         f="$DIR/f46"
4002         stop_writeback
4003         sync
4004         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4005         sync
4006         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
4007         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
4008         sync
4009         start_writeback
4010 }
4011 run_test 46 "dirtying a previously written page ================"
4012
4013 # test_47 is removed "Device nodes check" is moved to test_28
4014
4015 test_48a() { # bug 2399
4016         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
4017         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
4018                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
4019                 return
4020         test_mkdir $DIR/$tdir
4021         cd $DIR/$tdir
4022         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
4023         test_mkdir $DIR/$tdir || error "recreate directory failed"
4024         touch foo || error "'touch foo' failed after recreating cwd"
4025         test_mkdir bar || error "'mkdir foo' failed after recreating cwd"
4026         touch .foo || error "'touch .foo' failed after recreating cwd"
4027         test_mkdir .bar || error "'mkdir .foo' failed after recreating cwd"
4028         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4029         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4030         cd . || error "'cd .' failed after recreating cwd"
4031         test_mkdir . && error "'mkdir .' worked after recreating cwd"
4032         rmdir . && error "'rmdir .' worked after recreating cwd"
4033         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4034         cd .. || error "'cd ..' failed after recreating cwd"
4035 }
4036 run_test 48a "Access renamed working dir (should return errors)="
4037
4038 test_48b() { # bug 2399
4039         rm -rf $DIR/$tdir
4040         test_mkdir $DIR/$tdir
4041         cd $DIR/$tdir
4042         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4043         touch foo && error "'touch foo' worked after removing cwd"
4044         test_mkdir foo && error "'mkdir foo' worked after removing cwd"
4045         touch .foo && error "'touch .foo' worked after removing cwd"
4046         test_mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4047         ls . > /dev/null && error "'ls .' worked after removing cwd"
4048         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4049         test_mkdir . && error "'mkdir .' worked after removing cwd"
4050         rmdir . && error "'rmdir .' worked after removing cwd"
4051         ln -s . foo && error "'ln -s .' worked after removing cwd"
4052         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4053 }
4054 run_test 48b "Access removed working dir (should return errors)="
4055
4056 test_48c() { # bug 2350
4057         #lctl set_param debug=-1
4058         #set -vx
4059         rm -rf $DIR/$tdir
4060         test_mkdir -p $DIR/$tdir/dir
4061         cd $DIR/$tdir/dir
4062         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4063         $TRACE touch foo && error "touch foo worked after removing cwd"
4064         $TRACE test_mkdir foo && error "'mkdir foo' worked after removing cwd"
4065         touch .foo && error "touch .foo worked after removing cwd"
4066         test_mkdir .foo && error "mkdir .foo worked after removing cwd"
4067         $TRACE ls . && error "'ls .' worked after removing cwd"
4068         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4069         $TRACE test_mkdir . && error "'mkdir .' worked after removing cwd"
4070         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4071         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4072         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4073 }
4074 run_test 48c "Access removed working subdir (should return errors)"
4075
4076 test_48d() { # bug 2350
4077         #lctl set_param debug=-1
4078         #set -vx
4079         rm -rf $DIR/$tdir
4080         test_mkdir -p $DIR/$tdir/dir
4081         cd $DIR/$tdir/dir
4082         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4083         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4084         $TRACE touch foo && error "'touch foo' worked after removing parent"
4085         $TRACE test_mkdir foo && error "mkdir foo worked after removing parent"
4086         touch .foo && error "'touch .foo' worked after removing parent"
4087         test_mkdir .foo && error "mkdir .foo worked after removing parent"
4088         $TRACE ls . && error "'ls .' worked after removing parent"
4089         $TRACE ls .. && error "'ls ..' worked after removing parent"
4090         $TRACE test_mkdir . && error "'mkdir .' worked after removing parent"
4091         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4092         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4093         true
4094 }
4095 run_test 48d "Access removed parent subdir (should return errors)"
4096
4097 test_48e() { # bug 4134
4098         #lctl set_param debug=-1
4099         #set -vx
4100         rm -rf $DIR/$tdir
4101         test_mkdir -p $DIR/$tdir/dir
4102         cd $DIR/$tdir/dir
4103         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4104         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4105         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4106         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4107         # On a buggy kernel addition of "touch foo" after cd .. will
4108         # produce kernel oops in lookup_hash_it
4109         touch ../foo && error "'cd ..' worked after recreate parent"
4110         cd $DIR
4111         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4112 }
4113 run_test 48e "Access to recreated parent subdir (should return errors)"
4114
4115 test_49() { # LU-1030
4116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4117         remote_ost_nodsh && skip "remote OST with nodsh" && return
4118         # get ost1 size - lustre-OST0000
4119         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4120                 awk '{ print $4 }')
4121         # write 800M at maximum
4122         [[ $ost1_size -lt 2 ]] && ost1_size=2
4123         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4124
4125         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4126         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4127         local dd_pid=$!
4128
4129         # change max_pages_per_rpc while writing the file
4130         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4131         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4132         # loop until dd process exits
4133         while ps ax -opid | grep -wq $dd_pid; do
4134                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4135                 sleep $((RANDOM % 5 + 1))
4136         done
4137         # restore original max_pages_per_rpc
4138         $LCTL set_param $osc1_mppc=$orig_mppc
4139         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4140 }
4141 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4142
4143 test_50() {
4144         # bug 1485
4145         test_mkdir $DIR/$tdir
4146         cd $DIR/$tdir
4147         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4148 }
4149 run_test 50 "special situations: /proc symlinks  ==============="
4150
4151 test_51a() {    # was test_51
4152         # bug 1516 - create an empty entry right after ".." then split dir
4153         test_mkdir -c1 $DIR/$tdir
4154         touch $DIR/$tdir/foo
4155         $MCREATE $DIR/$tdir/bar
4156         rm $DIR/$tdir/foo
4157         createmany -m $DIR/$tdir/longfile 201
4158         FNUM=202
4159         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4160                 $MCREATE $DIR/$tdir/longfile$FNUM
4161                 FNUM=$(($FNUM + 1))
4162                 echo -n "+"
4163         done
4164         echo
4165         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4166 }
4167 run_test 51a "special situations: split htree with empty entry =="
4168
4169 cleanup_print_lfs_df () {
4170         trap 0
4171         $LFS df
4172         $LFS df -i
4173 }
4174
4175 test_51b() {
4176         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4177         local dir=$DIR/$tdir
4178
4179         local nrdirs=$((65536 + 100))
4180
4181         # cleanup the directory
4182         rm -fr $dir
4183
4184         test_mkdir -p -c1 $dir
4185
4186         $LFS df
4187         $LFS df -i
4188         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4189         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4190         [[ $numfree -lt $nrdirs ]] &&
4191                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4192                 return
4193
4194         # need to check free space for the directories as well
4195         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4196         numfree=$(( blkfree / $(fs_inode_ksize) ))
4197         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4198                 return
4199
4200         trap cleanup_print_lfs_df EXIT
4201
4202         # create files
4203         createmany -d $dir/d $nrdirs || {
4204                 unlinkmany $dir/d $nrdirs
4205                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4206         }
4207
4208         # really created :
4209         nrdirs=$(ls -U $dir | wc -l)
4210
4211         # unlink all but 100 subdirectories, then check it still works
4212         local left=100
4213         local delete=$((nrdirs - left))
4214
4215         $LFS df
4216         $LFS df -i
4217
4218         # for ldiskfs the nlink count should be 1, but this is OSD specific
4219         # and so this is listed for informational purposes only
4220         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4221         unlinkmany -d $dir/d $delete ||
4222                 error "unlink of first $delete subdirs failed"
4223
4224         echo "nlink between: $(stat -c %h $dir)"
4225         local found=$(ls -U $dir | wc -l)
4226         [ $found -ne $left ] &&
4227                 error "can't find subdirs: found only $found, expected $left"
4228
4229         unlinkmany -d $dir/d $delete $left ||
4230                 error "unlink of second $left subdirs failed"
4231         # regardless of whether the backing filesystem tracks nlink accurately
4232         # or not, the nlink count shouldn't be more than "." and ".." here
4233         local after=$(stat -c %h $dir)
4234         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4235                 echo "nlink after: $after"
4236
4237         cleanup_print_lfs_df
4238 }
4239 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4240
4241 test_51d() {
4242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4243         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
4244         test_mkdir -p $DIR/$tdir
4245         createmany -o $DIR/$tdir/t- 1000
4246         $GETSTRIPE $DIR/$tdir > $TMP/$tfile
4247         for N in $(seq 0 $((OSTCOUNT - 1))); do
4248                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4249                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4250                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4251                         '($1 == '$N') { objs += 1 } \
4252                         END { printf("%0.0f", objs) }')
4253                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4254         done
4255         unlinkmany $DIR/$tdir/t- 1000
4256
4257         NLAST=0
4258         for N in $(seq 1 $((OSTCOUNT - 1))); do
4259                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4260                         error "OST $N has less objects vs OST $NLAST" \
4261                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4262                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4263                         error "OST $N has less objects vs OST $NLAST" \
4264                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4265
4266                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4267                         error "OST $N has less #0 objects vs OST $NLAST" \
4268                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4269                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4270                         error "OST $N has less #0 objects vs OST $NLAST" \
4271                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4272                 NLAST=$N
4273         done
4274         rm -f $TMP/$tfile
4275 }
4276 run_test 51d "check object distribution"
4277
4278 test_51e() {
4279         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4280                 skip "ldiskfs only test"
4281                 return
4282         fi
4283
4284         test_mkdir -c1 $DIR/$tdir       || error "create $tdir failed"
4285         test_mkdir -c1 $DIR/$tdir/d0    || error "create d0 failed"
4286
4287         touch $DIR/$tdir/d0/foo
4288         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4289                 error "file exceed 65000 nlink limit!"
4290         unlinkmany $DIR/$tdir/d0/f- 65001
4291         return 0
4292 }
4293 run_test 51e "check file nlink limit"
4294
4295 test_51f() {
4296         test_mkdir $DIR/$tdir
4297
4298         local max=100000
4299         local ulimit_old=$(ulimit -n)
4300         local spare=20 # number of spare fd's for scripts/libraries, etc.
4301         local mdt=$(lfs getstripe -M $DIR/$tdir)
4302         local numfree=$(lfs df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4303
4304         echo "MDT$mdt numfree=$numfree, max=$max"
4305         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4306         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4307                 while ! ulimit -n $((numfree + spare)); do
4308                         numfree=$((numfree * 3 / 4))
4309                 done
4310                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4311         else
4312                 echo "left ulimit at $ulimit_old"
4313         fi
4314
4315         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4316                 unlinkmany $DIR/$tdir/f $numfree
4317                 error "create+open $numfree files in $DIR/$tdir failed"
4318         }
4319         ulimit -n $ulimit_old
4320
4321         # if createmany exits at 120s there will be fewer than $numfree files
4322         unlinkmany $DIR/$tdir/f $numfree || true
4323 }
4324 run_test 51f "check many open files limit"
4325
4326 test_52a() {
4327         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4328         test_mkdir -p $DIR/$tdir
4329         touch $DIR/$tdir/foo
4330         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4331         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4332         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4333         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4334         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4335                                         error "link worked"
4336         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4337         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4338         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4339                                                      error "lsattr"
4340         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4341         cp -r $DIR/$tdir $TMP/
4342         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4343 }
4344 run_test 52a "append-only flag test (should return errors)"
4345
4346 test_52b() {
4347         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4348         test_mkdir -p $DIR/$tdir
4349         touch $DIR/$tdir/foo
4350         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4351         cat test > $DIR/$tdir/foo && error "cat test worked"
4352         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4353         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4354         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4355                                         error "link worked"
4356         echo foo >> $DIR/$tdir/foo && error "echo worked"
4357         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4358         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4359         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4360         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4361                                                         error "lsattr"
4362         chattr -i $DIR/$tdir/foo || error "chattr failed"
4363
4364         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4365 }
4366 run_test 52b "immutable flag test (should return errors) ======="
4367
4368 test_53() {
4369         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4370         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4371         remote_ost_nodsh && skip "remote OST with nodsh" && return
4372
4373         local param
4374         local param_seq
4375         local ostname
4376         local mds_last
4377         local mds_last_seq
4378         local ost_last
4379         local ost_last_seq
4380         local ost_last_id
4381         local ostnum
4382         local node
4383         local found=false
4384         local support_last_seq=true
4385
4386         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4387                 support_last_seq=false
4388
4389         # only test MDT0000
4390         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4391         local value
4392         for value in $(do_facet $SINGLEMDS \
4393                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4394                 param=$(echo ${value[0]} | cut -d "=" -f1)
4395                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4396
4397                 if $support_last_seq; then
4398                         param_seq=$(echo $param |
4399                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4400                         mds_last_seq=$(do_facet $SINGLEMDS \
4401                                        $LCTL get_param -n $param_seq)
4402                 fi
4403                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4404
4405                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4406                 node=$(facet_active_host ost$((ostnum+1)))
4407                 param="obdfilter.$ostname.last_id"
4408                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4409                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4410                         ost_last_id=$ost_last
4411
4412                         if $support_last_seq; then
4413                                 ost_last_id=$(echo $ost_last |
4414                                               awk -F':' '{print $2}' |
4415                                               sed -e "s/^0x//g")
4416                                 ost_last_seq=$(echo $ost_last |
4417                                                awk -F':' '{print $1}')
4418                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4419                         fi
4420
4421                         if [[ $ost_last_id != $mds_last ]]; then
4422                                 error "$ost_last_id != $mds_last"
4423                         else
4424                                 found=true
4425                                 break
4426                         fi
4427                 done
4428         done
4429         $found || error "can not match last_seq/last_id for $mdtosc"
4430         return 0
4431 }
4432 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4433
4434 test_54a() {
4435         perl -MSocket -e ';' || { skip "no Socket perl module installed" && return; }
4436
4437         $SOCKETSERVER $DIR/socket ||
4438                 error "$SOCKETSERVER $DIR/socket failed: $?"
4439         $SOCKETCLIENT $DIR/socket ||
4440                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4441         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4442 }
4443 run_test 54a "unix domain socket test =========================="
4444
4445 test_54b() {
4446         f="$DIR/f54b"
4447         mknod $f c 1 3
4448         chmod 0666 $f
4449         dd if=/dev/zero of=$f bs=$(page_size) count=1
4450 }
4451 run_test 54b "char device works in lustre ======================"
4452
4453 find_loop_dev() {
4454         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4455         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4456         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4457
4458         for i in $(seq 3 7); do
4459                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4460                 LOOPDEV=$LOOPBASE$i
4461                 LOOPNUM=$i
4462                 break
4463         done
4464 }
4465
4466 cleanup_54c() {
4467         local rc=0
4468         loopdev="$DIR/loop54c"
4469
4470         trap 0
4471         $UMOUNT $DIR/$tdir || rc=$?
4472         losetup -d $loopdev || true
4473         losetup -d $LOOPDEV || true
4474         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4475         return $rc
4476 }
4477
4478 test_54c() {
4479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4480         loopdev="$DIR/loop54c"
4481
4482         find_loop_dev
4483         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4484         trap cleanup_54c EXIT
4485         mknod $loopdev b 7 $LOOPNUM
4486         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4487         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4488         losetup $loopdev $DIR/$tfile ||
4489                 error "can't set up $loopdev for $DIR/$tfile"
4490         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4491         test_mkdir -p $DIR/$tdir
4492         mount -t ext2 $loopdev $DIR/$tdir ||
4493                 error "error mounting $loopdev on $DIR/$tdir"
4494         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4495                 error "dd write"
4496         df $DIR/$tdir
4497         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4498                 error "dd read"
4499         cleanup_54c
4500 }
4501 run_test 54c "block device works in lustre ====================="
4502
4503 test_54d() {
4504         f="$DIR/f54d"
4505         string="aaaaaa"
4506         mknod $f p
4507         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4508 }
4509 run_test 54d "fifo device works in lustre ======================"
4510
4511 test_54e() {
4512         f="$DIR/f54e"
4513         string="aaaaaa"
4514         cp -aL /dev/console $f
4515         echo $string > $f || error "echo $string to $f failed"
4516 }
4517 run_test 54e "console/tty device works in lustre ======================"
4518
4519 #The test_55 used to be iopen test and it was removed by bz#24037.
4520 #run_test 55 "check iopen_connect_dentry() ======================"
4521
4522 test_56a() {    # was test_56
4523         rm -rf $DIR/$tdir
4524         $SETSTRIPE -d $DIR
4525         test_mkdir -p $DIR/$tdir/dir
4526         NUMFILES=3
4527         NUMFILESx2=$(($NUMFILES * 2))
4528         for i in $(seq 1 $NUMFILES); do
4529                 touch $DIR/$tdir/file$i
4530                 touch $DIR/$tdir/dir/file$i
4531         done
4532
4533         # test lfs getstripe with --recursive
4534         FILENUM=$($GETSTRIPE --recursive $DIR/$tdir | grep -c obdidx)
4535         [[ $FILENUM -eq $NUMFILESx2 ]] ||
4536                 error "$GETSTRIPE --recursive: found $FILENUM, not $NUMFILESx2"
4537         FILENUM=$($GETSTRIPE $DIR/$tdir | grep -c obdidx)
4538         [[ $FILENUM -eq $NUMFILES ]] ||
4539                 error "$GETSTRIPE $DIR/$tdir: found $FILENUM, not $NUMFILES"
4540         echo "$GETSTRIPE --recursive passed."
4541
4542         # test lfs getstripe with file instead of dir
4543         FILENUM=$($GETSTRIPE $DIR/$tdir/file1 | grep -c obdidx)
4544         [[ $FILENUM -eq 1 ]] ||
4545                 error "$GETSTRIPE $DIR/$tdir/file1: found $FILENUM, not 1"
4546         echo "$GETSTRIPE file1 passed."
4547
4548         #test lfs getstripe with --verbose
4549         [[ $($GETSTRIPE --verbose $DIR/$tdir |
4550                 grep -c lmm_magic) -eq $NUMFILES ]] ||
4551                 error "$GETSTRIPE --verbose $DIR/$tdir: want $NUMFILES"
4552         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_magic) -eq 0 ]] ||
4553                 error "$GETSTRIPE $DIR/$tdir: showed lmm_magic"
4554
4555         #test lfs getstripe with -v prints lmm_fid
4556         [[ $($GETSTRIPE -v $DIR/$tdir | grep -c lmm_fid) -eq $NUMFILES ]] ||
4557                 error "$GETSTRIPE -v $DIR/$tdir: want $NUMFILES lmm_fid: lines"
4558         [[ $($GETSTRIPE $DIR/$tdir | grep -c lmm_fid) -eq 0 ]] ||
4559                 error "$GETSTRIPE $DIR/$tdir: showed lmm_fid"
4560         echo "$GETSTRIPE --verbose passed."
4561
4562         #check for FID information
4563         local fid1=$($GETSTRIPE --fid $DIR/$tdir/file1)
4564         local fid2=$($GETSTRIPE --verbose $DIR/$tdir/file1 |
4565                        awk '/lmm_fid: / { print $2 }')
4566         local fid3=$($LFS path2fid $DIR/$tdir/file1)
4567         [ "$fid1" != "$fid2" ] &&
4568                 error "getstripe --fid $fid1 != getstripe --verbose $fid2"
4569         [ "$fid1" != "$fid3" ] &&
4570                 error "getstripe --fid $fid1 != lfs path2fid $fid3"
4571         echo "$GETSTRIPE --fid passed."
4572
4573         #test lfs getstripe with --obd
4574         $GETSTRIPE --obd wrong_uuid $DIR/$tdir 2>&1 |
4575                 grep -q "unknown obduuid" ||
4576                 error "$GETSTRIPE --obd wrong_uuid should return error message"
4577
4578         [[ $OSTCOUNT -lt 2 ]] &&
4579                 skip_env "skipping other $GETSTRIPE --obd test" && return
4580
4581         OSTIDX=1
4582         OBDUUID=$(ostuuid_from_index $OSTIDX)
4583         FILENUM=$($GETSTRIPE -ir $DIR/$tdir | grep "^$OSTIDX\$" | wc -l)
4584         FOUND=$($GETSTRIPE -r --obd $OBDUUID $DIR/$tdir | grep obdidx | wc -l)
4585         [[ $FOUND -eq $FILENUM ]] ||
4586                 error "$GETSTRIPE --obd wrong: found $FOUND, expected $FILENUM"
4587         [[ $($GETSTRIPE -r -v --obd $OBDUUID $DIR/$tdir |
4588                 sed '/^[         ]*'${OSTIDX}'[  ]/d' |
4589                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4590                 error "$GETSTRIPE --obd: should not show file on other obd"
4591         echo "$GETSTRIPE --obd passed"
4592 }
4593 run_test 56a "check $GETSTRIPE"
4594
4595 test_56b() {
4596         test_mkdir $DIR/$tdir
4597         NUMDIRS=3
4598         for i in $(seq 1 $NUMDIRS); do
4599                 test_mkdir $DIR/$tdir/dir$i
4600         done
4601
4602         # test lfs getdirstripe default mode is non-recursion, which is
4603         # different from lfs getstripe
4604         dircnt=$($LFS getdirstripe $DIR/$tdir | grep -c lmv_stripe_count)
4605         [[ $dircnt -eq 1 ]] ||
4606                 error "$LFS getdirstripe: found $dircnt, not 1"
4607         dircnt=$($LFS getdirstripe --recursive $DIR/$tdir |
4608                 grep -c lmv_stripe_count)
4609         [[ $dircnt -eq $((NUMDIRS + 1)) ]] ||
4610                 error "$LFS getdirstripe --recursive: found $dircnt, \
4611                         not $((NUMDIRS + 1))"
4612 }
4613 run_test 56b "check $LFS getdirstripe"
4614
4615 test_56c() {
4616         local ost_idx=0
4617         local ost_name=$(ostname_from_index $ost_idx)
4618
4619         local old_status=$(ost_dev_status $ost_idx)
4620         [[ -z "$old_status" ]] ||
4621                 { skip_env "OST $ost_name is in $old_status status"; return 0; }
4622
4623         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4624         sleep_maxage
4625
4626         local new_status=$(ost_dev_status $ost_idx)
4627         [[ "$new_status" = "D" ]] ||
4628                 error "OST $ost_name is in status of '$new_status', not 'D'"
4629
4630         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4631         sleep_maxage
4632
4633         new_status=$(ost_dev_status $ost_idx)
4634         [[ -z "$new_status" ]] ||
4635                 error "OST $ost_name is in status of '$new_status', not ''"
4636 }
4637 run_test 56c "check 'lfs df' showing device status"
4638
4639 NUMFILES=3
4640 NUMDIRS=3
4641 setup_56() {
4642         local LOCAL_NUMFILES="$1"
4643         local LOCAL_NUMDIRS="$2"
4644         local MKDIR_PARAMS="$3"
4645         local DIR_STRIPE_PARAMS="$4"
4646
4647         if [ ! -d "$TDIR" ] ; then
4648                 test_mkdir -p $DIR_STRIPE_PARAMS $TDIR
4649                 [ "$MKDIR_PARAMS" ] && $SETSTRIPE $MKDIR_PARAMS $TDIR
4650                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4651                         touch $TDIR/file$i
4652                 done
4653                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4654                         test_mkdir $DIR_STRIPE_PARAMS $TDIR/dir$i
4655                         for j in `seq 1 $LOCAL_NUMFILES` ; do
4656                                 touch $TDIR/dir$i/file$j
4657                         done
4658                 done
4659         fi
4660 }
4661
4662 setup_56_special() {
4663         LOCAL_NUMFILES=$1
4664         LOCAL_NUMDIRS=$2
4665         setup_56 $1 $2
4666         if [ ! -e "$TDIR/loop1b" ] ; then
4667                 for i in `seq 1 $LOCAL_NUMFILES` ; do
4668                         mknod $TDIR/loop${i}b b 7 $i
4669                         mknod $TDIR/null${i}c c 1 3
4670                         ln -s $TDIR/file1 $TDIR/link${i}l
4671                 done
4672                 for i in `seq 1 $LOCAL_NUMDIRS` ; do
4673                         mknod $TDIR/dir$i/loop${i}b b 7 $i
4674                         mknod $TDIR/dir$i/null${i}c c 1 3
4675                         ln -s $TDIR/dir$i/file1 $TDIR/dir$i/link${i}l
4676                 done
4677         fi
4678 }
4679
4680 test_56g() {
4681         $SETSTRIPE -d $DIR
4682
4683         TDIR=$DIR/${tdir}g
4684         setup_56 $NUMFILES $NUMDIRS
4685
4686         EXPECTED=$(($NUMDIRS + 2))
4687         # test lfs find with -name
4688         for i in $(seq 1 $NUMFILES) ; do
4689                 NUMS=$($LFIND -name "*$i" $TDIR | wc -l)
4690                 [ $NUMS -eq $EXPECTED ] ||
4691                         error "lfs find -name \"*$i\" $TDIR wrong: "\
4692                               "found $NUMS, expected $EXPECTED"
4693         done
4694 }
4695 run_test 56g "check lfs find -name ============================="
4696
4697 test_56h() {
4698         $SETSTRIPE -d $DIR
4699
4700         TDIR=$DIR/${tdir}g
4701         setup_56 $NUMFILES $NUMDIRS
4702
4703         EXPECTED=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4704         # test lfs find with ! -name
4705         for i in $(seq 1 $NUMFILES) ; do
4706                 NUMS=$($LFIND ! -name "*$i" $TDIR | wc -l)
4707                 [ $NUMS -eq $EXPECTED ] ||
4708                         error "lfs find ! -name \"*$i\" $TDIR wrong: "\
4709                               "found $NUMS, expected $EXPECTED"
4710         done
4711 }
4712 run_test 56h "check lfs find ! -name ============================="
4713
4714 test_56i() {
4715        tdir=${tdir}i
4716        test_mkdir -p $DIR/$tdir
4717        UUID=$(ostuuid_from_index 0 $DIR/$tdir)
4718        CMD="$LFIND -ost $UUID $DIR/$tdir"
4719        OUT=$($CMD)
4720        [ -z "$OUT" ] || error "\"$CMD\" returned directory '$OUT'"
4721 }
4722 run_test 56i "check 'lfs find -ost UUID' skips directories ======="
4723
4724 test_56j() {
4725         TDIR=$DIR/${tdir}g
4726         setup_56_special $NUMFILES $NUMDIRS
4727
4728         EXPECTED=$((NUMDIRS + 1))
4729         CMD="$LFIND -type d $TDIR"
4730         NUMS=$($CMD | wc -l)
4731         [ $NUMS -eq $EXPECTED ] ||
4732                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4733 }
4734 run_test 56j "check lfs find -type d ============================="
4735
4736 test_56k() {
4737         TDIR=$DIR/${tdir}g
4738         setup_56_special $NUMFILES $NUMDIRS
4739
4740         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4741         CMD="$LFIND -type f $TDIR"
4742         NUMS=$($CMD | wc -l)
4743         [ $NUMS -eq $EXPECTED ] ||
4744                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4745 }
4746 run_test 56k "check lfs find -type f ============================="
4747
4748 test_56l() {
4749         TDIR=$DIR/${tdir}g
4750         setup_56_special $NUMFILES $NUMDIRS
4751
4752         EXPECTED=$((NUMDIRS + NUMFILES))
4753         CMD="$LFIND -type b $TDIR"
4754         NUMS=$($CMD | wc -l)
4755         [ $NUMS -eq $EXPECTED ] ||
4756                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4757 }
4758 run_test 56l "check lfs find -type b ============================="
4759
4760 test_56m() {
4761         TDIR=$DIR/${tdir}g
4762         setup_56_special $NUMFILES $NUMDIRS
4763
4764         EXPECTED=$((NUMDIRS + NUMFILES))
4765         CMD="$LFIND -type c $TDIR"
4766         NUMS=$($CMD | wc -l)
4767         [ $NUMS -eq $EXPECTED ] ||
4768                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4769 }
4770 run_test 56m "check lfs find -type c ============================="
4771
4772 test_56n() {
4773         TDIR=$DIR/${tdir}g
4774         setup_56_special $NUMFILES $NUMDIRS
4775
4776         EXPECTED=$((NUMDIRS + NUMFILES))
4777         CMD="$LFIND -type l $TDIR"
4778         NUMS=$($CMD | wc -l)
4779         [ $NUMS -eq $EXPECTED ] ||
4780                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4781 }
4782 run_test 56n "check lfs find -type l ============================="
4783
4784 test_56o() {
4785         TDIR=$DIR/${tdir}o
4786         setup_56 $NUMFILES $NUMDIRS
4787         utime $TDIR/file1 > /dev/null || error "utime (1)"
4788         utime $TDIR/file2 > /dev/null || error "utime (2)"
4789         utime $TDIR/dir1 > /dev/null || error "utime (3)"
4790         utime $TDIR/dir2 > /dev/null || error "utime (4)"
4791         utime $TDIR/dir1/file1 > /dev/null || error "utime (5)"
4792         dd if=/dev/zero count=1 >> $TDIR/dir1/file1 && sync
4793
4794         EXPECTED=4
4795         NUMS=`$LFIND -mtime +0 $TDIR | wc -l`
4796         [ $NUMS -eq $EXPECTED ] || \
4797                 error "lfs find -mtime +0 $TDIR wrong: found $NUMS, expected $EXPECTED"
4798
4799         EXPECTED=12
4800         CMD="$LFIND -mtime 0 $TDIR"
4801         NUMS=$($CMD | wc -l)
4802         [ $NUMS -eq $EXPECTED ] ||
4803                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4804 }
4805 run_test 56o "check lfs find -mtime for old files =========================="
4806
4807 test_56p() {
4808         [ $RUNAS_ID -eq $UID ] &&
4809                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4810
4811         TDIR=$DIR/${tdir}p
4812         setup_56 $NUMFILES $NUMDIRS
4813
4814         chown $RUNAS_ID $TDIR/file* || error "chown $DIR/${tdir}g/file$i failed"
4815         EXPECTED=$NUMFILES
4816         CMD="$LFIND -uid $RUNAS_ID $TDIR"
4817         NUMS=$($CMD | wc -l)
4818         [ $NUMS -eq $EXPECTED ] || \
4819                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4820
4821         EXPECTED=$(((NUMFILES + 1) * NUMDIRS + 1))
4822         CMD="$LFIND ! -uid $RUNAS_ID $TDIR"
4823         NUMS=$($CMD | wc -l)
4824         [ $NUMS -eq $EXPECTED ] || \
4825                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4826 }
4827 run_test 56p "check lfs find -uid and ! -uid ==============================="
4828
4829 test_56q() {
4830         [ $RUNAS_ID -eq $UID ] &&
4831                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4832
4833         TDIR=$DIR/${tdir}q
4834         setup_56 $NUMFILES $NUMDIRS
4835
4836         chgrp $RUNAS_GID $TDIR/file* || error "chown $TDIR/file$i failed"
4837
4838         EXPECTED=$NUMFILES
4839         CMD="$LFIND -gid $RUNAS_GID $TDIR"
4840         NUMS=$($CMD | wc -l)
4841         [ $NUMS -eq $EXPECTED ] ||
4842                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4843
4844         EXPECTED=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4845         CMD="$LFIND ! -gid $RUNAS_GID $TDIR"
4846         NUMS=$($CMD | wc -l)
4847         [ $NUMS -eq $EXPECTED ] ||
4848                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4849 }
4850 run_test 56q "check lfs find -gid and ! -gid ==============================="
4851
4852 test_56r() {
4853         TDIR=$DIR/${tdir}r
4854         setup_56 $NUMFILES $NUMDIRS
4855
4856         EXPECTED=12
4857         CMD="$LFIND -size 0 -type f $TDIR"
4858         NUMS=$($CMD | wc -l)
4859         [ $NUMS -eq $EXPECTED ] ||
4860                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4861         EXPECTED=0
4862         CMD="$LFIND ! -size 0 -type f $TDIR"
4863         NUMS=$($CMD | wc -l)
4864         [ $NUMS -eq $EXPECTED ] ||
4865                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4866         echo "test" > $TDIR/$tfile
4867         echo "test2" > $TDIR/$tfile.2 && sync
4868         EXPECTED=1
4869         CMD="$LFIND -size 5 -type f $TDIR"
4870         NUMS=$($CMD | wc -l)
4871         [ $NUMS -eq $EXPECTED ] ||
4872                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4873         EXPECTED=1
4874         CMD="$LFIND -size +5 -type f $TDIR"
4875         NUMS=$($CMD | wc -l)
4876         [ $NUMS -eq $EXPECTED ] ||
4877                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4878         EXPECTED=2
4879         CMD="$LFIND -size +0 -type f $TDIR"
4880         NUMS=$($CMD | wc -l)
4881         [ $NUMS -eq $EXPECTED ] ||
4882                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4883         EXPECTED=2
4884         CMD="$LFIND ! -size -5 -type f $TDIR"
4885         NUMS=$($CMD | wc -l)
4886         [ $NUMS -eq $EXPECTED ] ||
4887                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4888         EXPECTED=12
4889         CMD="$LFIND -size -5 -type f $TDIR"
4890         NUMS=$($CMD | wc -l)
4891         [ $NUMS -eq $EXPECTED ] ||
4892                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4893 }
4894 run_test 56r "check lfs find -size works =========================="
4895
4896 test_56s() { # LU-611
4897         TDIR=$DIR/${tdir}s
4898
4899         #LU-9369
4900         setup_56 0 $NUMDIRS
4901         for i in $(seq 1 $NUMDIRS); do
4902                 $SETSTRIPE -c $((OSTCOUNT + 1)) $TDIR/dir$i/$tfile
4903         done
4904         EXPECTED=$NUMDIRS
4905         CMD="$LFIND -c $OSTCOUNT $TDIR"
4906         NUMS=$($CMD | wc -l)
4907         [ $NUMS -eq $EXPECTED ] || {
4908                 $GETSTRIPE -R $TDIR
4909                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4910         }
4911         rm -rf $TDIR
4912
4913         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT"
4914         if [[ $OSTCOUNT -gt 1 ]]; then
4915                 $SETSTRIPE -c 1 $TDIR/$tfile.{0,1,2,3}
4916                 ONESTRIPE=4
4917                 EXTRA=4
4918         else
4919                 ONESTRIPE=$(((NUMDIRS + 1) * NUMFILES))
4920                 EXTRA=0
4921         fi
4922
4923         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4924         CMD="$LFIND -stripe-count $OSTCOUNT -type f $TDIR"
4925         NUMS=$($CMD | wc -l)
4926         [ $NUMS -eq $EXPECTED ] || {
4927                 $GETSTRIPE -R $TDIR
4928                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4929         }
4930
4931         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + EXTRA))
4932         CMD="$LFIND -stripe-count +0 -type f $TDIR"
4933         NUMS=$($CMD | wc -l)
4934         [ $NUMS -eq $EXPECTED ] || {
4935                 $GETSTRIPE -R $TDIR
4936                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4937         }
4938
4939         EXPECTED=$ONESTRIPE
4940         CMD="$LFIND -stripe-count 1 -type f $TDIR"
4941         NUMS=$($CMD | wc -l)
4942         [ $NUMS -eq $EXPECTED ] || {
4943                 $GETSTRIPE -R $TDIR
4944                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4945         }
4946
4947         CMD="$LFIND -stripe-count -2 -type f $TDIR"
4948         NUMS=$($CMD | wc -l)
4949         [ $NUMS -eq $EXPECTED ] || {
4950                 $GETSTRIPE -R $TDIR
4951                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4952         }
4953
4954         EXPECTED=0
4955         CMD="$LFIND -stripe-count $((OSTCOUNT + 1)) -type f $TDIR"
4956         NUMS=$($CMD | wc -l)
4957         [ $NUMS -eq $EXPECTED ] || {
4958                 $GETSTRIPE -R $TDIR
4959                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4960         }
4961 }
4962 run_test 56s "check lfs find -stripe-count works"
4963
4964 test_56t() { # LU-611
4965         TDIR=$DIR/${tdir}t
4966
4967         #LU-9369
4968         setup_56 0 $NUMDIRS
4969         for i in $(seq 1 $NUMDIRS); do
4970                 $SETSTRIPE -S 4M $TDIR/dir$i/$tfile
4971         done
4972         EXPECTED=$NUMDIRS
4973         CMD="$LFIND -S 4M $TDIR"
4974         NUMS=$($CMD | wc -l)
4975         [ $NUMS -eq $EXPECTED ] || {
4976                 $GETSTRIPE -R $TDIR
4977                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4978         }
4979         rm -rf $TDIR
4980
4981         setup_56 $NUMFILES $NUMDIRS "--stripe-size 512k"
4982
4983         $SETSTRIPE -S 256k $TDIR/$tfile.{0,1,2,3}
4984
4985         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
4986         CMD="$LFIND -stripe-size 512k -type f $TDIR"
4987         NUMS=$($CMD | wc -l)
4988         [ $NUMS -eq $EXPECTED ] ||
4989                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4990
4991         CMD="$LFIND -stripe-size +320k -type f $TDIR"
4992         NUMS=$($CMD | wc -l)
4993         [ $NUMS -eq $EXPECTED ] ||
4994                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
4995
4996         EXPECTED=$(((NUMDIRS + 1) * NUMFILES + 4))
4997         CMD="$LFIND -stripe-size +200k -type f $TDIR"
4998         NUMS=$($CMD | wc -l)
4999         [ $NUMS -eq $EXPECTED ] ||
5000                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5001
5002         CMD="$LFIND -stripe-size -640k -type f $TDIR"
5003         NUMS=$($CMD | wc -l)
5004         [ $NUMS -eq $EXPECTED ] ||
5005                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5006
5007         EXPECTED=4
5008         CMD="$LFIND -stripe-size 256k -type f $TDIR"
5009         NUMS=$($CMD | wc -l)
5010         [ $NUMS -eq $EXPECTED ] ||
5011                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5012
5013         CMD="$LFIND -stripe-size -320k -type f $TDIR"
5014         NUMS=$($CMD | wc -l)
5015         [ $NUMS -eq $EXPECTED ] ||
5016                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5017
5018         EXPECTED=0
5019         CMD="$LFIND -stripe-size 1024k -type f $TDIR"
5020         NUMS=$($CMD | wc -l)
5021         [ $NUMS -eq $EXPECTED ] ||
5022                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5023 }
5024 run_test 56t "check lfs find -stripe-size works"
5025
5026 test_56u() { # LU-611
5027         TDIR=$DIR/${tdir}u
5028         setup_56 $NUMFILES $NUMDIRS "-i 0"
5029
5030         if [[ $OSTCOUNT -gt 1 ]]; then
5031                 $SETSTRIPE -i 1 $TDIR/$tfile.{0,1,2,3}
5032                 ONESTRIPE=4
5033         else
5034                 ONESTRIPE=0
5035         fi
5036
5037         EXPECTED=$(((NUMDIRS + 1) * NUMFILES))
5038         CMD="$LFIND -stripe-index 0 -type f $TDIR"
5039         NUMS=$($CMD | wc -l)
5040         [ $NUMS -eq $EXPECTED ] ||
5041                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5042
5043         EXPECTED=$ONESTRIPE
5044         CMD="$LFIND -stripe-index 1 -type f $TDIR"
5045         NUMS=$($CMD | wc -l)
5046         [ $NUMS -eq $EXPECTED ] ||
5047                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5048
5049         CMD="$LFIND ! -stripe-index 0 -type f $TDIR"
5050         NUMS=$($CMD | wc -l)
5051         [ $NUMS -eq $EXPECTED ] ||
5052                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5053
5054         EXPECTED=0
5055         # This should produce an error and not return any files
5056         CMD="$LFIND -stripe-index $OSTCOUNT -type f $TDIR"
5057         NUMS=$($CMD 2>/dev/null | wc -l)
5058         [ $NUMS -eq $EXPECTED ] ||
5059                 error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5060
5061         if [[ $OSTCOUNT -gt 1 ]]; then
5062                 EXPECTED=$(((NUMDIRS + 1) * NUMFILES + ONESTRIPE))
5063                 CMD="$LFIND -stripe-index 0,1 -type f $TDIR"
5064                 NUMS=$($CMD | wc -l)
5065                 [ $NUMS -eq $EXPECTED ] ||
5066                         error "\"$CMD\" wrong: found $NUMS, expected $EXPECTED"
5067         fi
5068 }
5069 run_test 56u "check lfs find -stripe-index works"
5070
5071 test_56v() {
5072     local MDT_IDX=0
5073
5074     TDIR=$DIR/${tdir}v
5075     rm -rf $TDIR
5076     setup_56 $NUMFILES $NUMDIRS
5077
5078     UUID=$(mdtuuid_from_index $MDT_IDX $TDIR)
5079     [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT index $MDT_IDX"
5080
5081     for file in $($LFIND -mdt $UUID $TDIR); do
5082         file_mdt_idx=$($GETSTRIPE -M $file)
5083         [ $file_mdt_idx -eq $MDT_IDX ] ||
5084             error "'lfind -mdt $UUID' != 'getstripe -M' ($file_mdt_idx)"
5085     done
5086 }
5087 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
5088
5089 test_56w() {
5090         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5091         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5092         TDIR=$DIR/${tdir}w
5093
5094         rm -rf $TDIR || error "remove $TDIR failed"
5095         setup_56 $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5096
5097         local stripe_size
5098         stripe_size=$($GETSTRIPE -S -d $TDIR) ||
5099                 error "$GETSTRIPE -S -d $TDIR failed"
5100         stripe_size=${stripe_size%% *}
5101
5102         local file_size=$((stripe_size * OSTCOUNT))
5103         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5104         local required_space=$((file_num * file_size))
5105
5106         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5107                            head -n1)
5108         [[ $free_space -le $((required_space / 1024)) ]] &&
5109                 skip_env "need $required_space bytes, have $free_space KB" &&
5110                 return
5111
5112         local dd_bs=65536
5113         local dd_count=$((file_size / dd_bs))
5114
5115         # write data into the files
5116         local i
5117         local j
5118         local file
5119         for i in $(seq 1 $NUMFILES); do
5120                 file=$TDIR/file$i
5121                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5122                         error "write data into $file failed"
5123         done
5124         for i in $(seq 1 $NUMDIRS); do
5125                 for j in $(seq 1 $NUMFILES); do
5126                         file=$TDIR/dir$i/file$j
5127                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5128                                 error "write data into $file failed"
5129                 done
5130         done
5131
5132         # $LFS_MIGRATE will fail if hard link migration is unsupported
5133         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5134                 createmany -l$TDIR/dir1/file1 $TDIR/dir1/link 200 ||
5135                         error "creating links to $TDIR/dir1/file1 failed"
5136         fi
5137
5138         local expected=-1
5139         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5140
5141         # lfs_migrate file
5142         local cmd="$LFS_MIGRATE -y -c $expected $TDIR/file1"
5143         echo "$cmd"
5144         eval $cmd || error "$cmd failed"
5145
5146         check_stripe_count $TDIR/file1 $expected
5147
5148         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5149         then
5150                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5151                 # OST 1 if it is on OST 0. This file is small enough to
5152                 # be on only one stripe.
5153                 file=$TDIR/migr_1_ost
5154                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5155                         error "write data into $file failed"
5156                 local obdidx=$($LFS getstripe -i $file)
5157                 local oldmd5=$(md5sum $file)
5158                 local newobdidx=0
5159                 [[ $obdidx -eq 0 ]] && newobdidx=1
5160                 cmd="$LFS migrate -i $newobdidx $file"
5161                 echo $cmd
5162                 eval $cmd || error "$cmd failed"
5163                 local realobdix=$($LFS getstripe -i $file)
5164                 local newmd5=$(md5sum $file)
5165                 [[ $newobdidx -ne $realobdix ]] &&
5166                         error "new OST is different (was=$obdidx, wanted=$newobdidx, got=$realobdix)"
5167                 [[ "$oldmd5" != "$newmd5" ]] &&
5168                         error "md5sum differ: $oldmd5, $newmd5"
5169         fi
5170
5171     # lfs_migrate dir
5172     cmd="$LFS_MIGRATE -y -c $expected $TDIR/dir1"
5173     echo "$cmd"
5174     eval $cmd || error "$cmd failed"
5175
5176     for j in $(seq 1 $NUMFILES); do
5177         check_stripe_count $TDIR/dir1/file$j $expected
5178     done
5179
5180     # lfs_migrate works with lfs find
5181     cmd="$LFIND -stripe_count $OSTCOUNT -type f $TDIR |
5182          $LFS_MIGRATE -y -c $expected"
5183     echo "$cmd"
5184     eval $cmd || error "$cmd failed"
5185
5186     for i in $(seq 2 $NUMFILES); do
5187         check_stripe_count $TDIR/file$i $expected
5188     done
5189     for i in $(seq 2 $NUMDIRS); do
5190         for j in $(seq 1 $NUMFILES); do
5191             check_stripe_count $TDIR/dir$i/file$j $expected
5192         done
5193     done
5194 }
5195 run_test 56w "check lfs_migrate -c stripe_count works"
5196
5197 test_56x() {
5198         check_swap_layouts_support && return 0
5199         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5200
5201         local dir0=$DIR/$tdir
5202         local ref1=/etc/passwd
5203         local file1=$dir0/file1
5204
5205         test_mkdir $dir0 || error "creating dir $dir0"
5206         $LFS setstripe -c 2 $file1
5207         cp $ref1 $file1
5208         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5209         stripe=$($LFS getstripe -c $file1)
5210         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5211         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5212
5213         # clean up
5214         rm -f $file1
5215 }
5216 run_test 56x "lfs migration support"
5217
5218 test_56xa() {
5219         check_swap_layouts_support && return 0
5220         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5221
5222         local dir0=$DIR/$tdir/$testnum
5223         test_mkdir -p $dir0 || error "creating dir $dir0"
5224
5225         local ref1=/etc/passwd
5226         local file1=$dir0/file1
5227
5228         $LFS setstripe -c 2 $file1
5229         cp $ref1 $file1
5230         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5231         local stripe=$($LFS getstripe -c $file1)
5232         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5233         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5234
5235         # clean up
5236         rm -f $file1
5237 }
5238 run_test 56xa "lfs migration --block support"
5239
5240 check_migrate_links() {
5241         local dir="$1"
5242         local file1="$dir/file1"
5243         local begin="$2"
5244         local count="$3"
5245         local total_count=$(($begin + $count - 1))
5246         local symlink_count=10
5247         local uniq_count=10
5248
5249         if [ ! -f "$file1" ]; then
5250                 echo -n "creating initial file..."
5251                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5252                         error "cannot setstripe initial file"
5253                 echo "done"
5254
5255                 echo -n "creating symlinks..."
5256                 for s in $(seq 1 $symlink_count); do
5257                         ln -s "$file1" "$dir/slink$s" ||
5258                                 error "cannot create symlinks"
5259                 done
5260                 echo "done"
5261
5262                 echo -n "creating nonlinked files..."
5263                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5264                         error "cannot create nonlinked files"
5265                 echo "done"
5266         fi
5267
5268         # create hard links
5269         if [ ! -f "$dir/file$total_count" ]; then
5270                 echo -n "creating hard links $begin:$total_count..."
5271                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5272                         /dev/null || error "cannot create hard links"
5273                 echo "done"
5274         fi
5275
5276         echo -n "checking number of hard links listed in xattrs..."
5277         local fid=$($LFS getstripe -F "$file1")
5278         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5279
5280         echo "${#paths[*]}"
5281         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5282                         echo "hard link list has unexpected size, skipping test"
5283                         return 0
5284         fi
5285         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5286                         error "link names should exceed xattrs size"
5287         fi
5288
5289         echo -n "migrating files..."
5290         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5291         local rc=$?
5292         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5293         echo "done"
5294
5295         # make sure all links have been properly migrated
5296         echo -n "verifying files..."
5297         fid=$($LFS getstripe -F "$file1") ||
5298                 error "cannot get fid for file $file1"
5299         for i in $(seq 2 $total_count); do
5300                 local fid2=$($LFS getstripe -F $dir/file$i)
5301                 [ "$fid2" == "$fid" ] ||
5302                         error "migrated hard link has mismatched FID"
5303         done
5304
5305         # make sure hard links were properly detected, and migration was
5306         # performed only once for the entire link set; nonlinked files should
5307         # also be migrated
5308         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5309         local expected=$(($uniq_count + 1))
5310         [ "$actual" -eq  "$expected" ] ||
5311                 error "hard links individually migrated ($actual != $expected)"
5312
5313         # make sure the correct number of hard links are present
5314         local hardlinks=$(stat -c '%h' "$file1")
5315         [ $hardlinks -eq $total_count ] ||
5316                 error "num hard links $hardlinks != $total_count"
5317         echo "done"
5318
5319         return 0
5320 }
5321
5322 test_56xb() {
5323         local dir0="$DIR/$tdir"
5324
5325         test_mkdir "$dir0" || error "cannot create dir $dir0"
5326
5327         echo "testing lfs migrate mode when all links fit within xattrs"
5328         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 2 99
5329
5330         echo "testing rsync mode when all links fit within xattrs"
5331         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 2 99
5332
5333         echo "testing lfs migrate mode when all links do not fit within xattrs"
5334         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir0" 101 100
5335
5336         echo "testing rsync mode when all links do not fit within xattrs"
5337         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir0" 101 100
5338
5339         # clean up
5340         rm -rf $dir0
5341 }
5342 run_test 56xb "lfs migration hard link support"
5343
5344 test_56y() {
5345         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5346                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5347                 return
5348
5349         local res=""
5350         local dir0=$DIR/$tdir/$testnum
5351         test_mkdir -p $dir0 || error "creating dir $dir0"
5352         local f1=$dir0/file1
5353         local f2=$dir0/file2
5354
5355         touch $f1 || error "creating std file $f1"
5356         $MULTIOP $f2 H2c || error "creating released file $f2"
5357
5358         # a directory can be raid0, so ask only for files
5359         res=$($LFIND $dir0 -L raid0 -type f | wc -l)
5360         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5361
5362         res=$($LFIND $dir0 \! -L raid0 -type f | wc -l)
5363         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5364
5365         # only files can be released, so no need to force file search
5366         res=$($LFIND $dir0 -L released)
5367         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5368
5369         res=$($LFIND $dir0 \! -L released)
5370         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5371
5372 }
5373 run_test 56y "lfs find -L raid0|released"
5374
5375 test_56z() { # LU-4824
5376         # This checks to make sure 'lfs find' continues after errors
5377         # There are two classes of errors that should be caught:
5378         # - If multiple paths are provided, all should be searched even if one
5379         #   errors out
5380         # - If errors are encountered during the search, it should not terminate
5381         #   early
5382         local i
5383         test_mkdir $DIR/$tdir
5384         for i in d{0..9}; do
5385                 test_mkdir $DIR/$tdir/$i
5386         done
5387         touch $DIR/$tdir/d{0..9}/$tfile
5388         $LFS find $DIR/non_existent_dir $DIR/$tdir &&
5389                 error "$LFS find did not return an error"
5390         # Make a directory unsearchable. This should NOT be the last entry in
5391         # directory order.  Arbitrarily pick the 6th entry
5392         chmod 700 $($LFS find $DIR/$tdir -type d | sed '6!d')
5393         local count=$($RUNAS $LFS find $DIR/non_existent $DIR/$tdir | wc -l)
5394         # The user should be able to see 10 directories and 9 files
5395         [ $count == 19 ] || error "$LFS find did not continue after error"
5396 }
5397 run_test 56z "lfs find should continue after an error"
5398
5399 test_56aa() { # LU-5937
5400         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5401
5402         mkdir $DIR/$tdir
5403         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
5404
5405         createmany -o $DIR/$tdir/striped_dir/${tfile}- 1024
5406         local dirs=$(lfs find --size +8k $DIR/$tdir/)
5407
5408         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5409 }
5410 run_test 56aa "lfs find --size under striped dir"
5411
5412 test_57a() {
5413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5414         # note test will not do anything if MDS is not local
5415         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5416                 skip "ldiskfs only test"
5417                 return
5418         fi
5419
5420         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5421         local MNTDEV="osd*.*MDT*.mntdev"
5422         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5423         [ -z "$DEV" ] && error "can't access $MNTDEV"
5424         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5425                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5426                         error "can't access $DEV"
5427                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5428                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5429                 rm $TMP/t57a.dump
5430         done
5431 }
5432 run_test 57a "verify MDS filesystem created with large inodes =="
5433
5434 test_57b() {
5435         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5436         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5437                 skip "ldiskfs only test"
5438                 return
5439         fi
5440
5441         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5442         local dir=$DIR/$tdir
5443
5444         local FILECOUNT=100
5445         local FILE1=$dir/f1
5446         local FILEN=$dir/f$FILECOUNT
5447
5448         rm -rf $dir || error "removing $dir"
5449         test_mkdir -p -c1 $dir || error "creating $dir"
5450         local mdtidx=$($LFS getstripe -M $dir)
5451         local mdtname=MDT$(printf %04x $mdtidx)
5452         local facet=mds$((mdtidx + 1))
5453
5454         echo "mcreating $FILECOUNT files"
5455         createmany -m $dir/f 1 $FILECOUNT || \
5456                 error "creating files in $dir"
5457
5458         # verify that files do not have EAs yet
5459         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5460         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5461
5462         sync
5463         sleep 1
5464         df $dir  #make sure we get new statfs data
5465         local MDSFREE=$(do_facet $facet \
5466                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5467         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5468         echo "opening files to create objects/EAs"
5469         local FILE
5470         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5471                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5472         done
5473
5474         # verify that files have EAs now
5475         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5476         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5477
5478         sleep 1  #make sure we get new statfs data
5479         df $dir
5480         local MDSFREE2=$(do_facet $facet \
5481                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5482         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5483         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5484                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5485                         error "MDC before $MDCFREE != after $MDCFREE2"
5486                 else
5487                         echo "MDC before $MDCFREE != after $MDCFREE2"
5488                         echo "unable to confirm if MDS has large inodes"
5489                 fi
5490         fi
5491         rm -rf $dir
5492 }
5493 run_test 57b "default LOV EAs are stored inside large inodes ==="
5494
5495 test_58() {
5496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5497         [ -z "$(which wiretest 2>/dev/null)" ] &&
5498                         skip_env "could not find wiretest" && return
5499         wiretest
5500 }
5501 run_test 58 "verify cross-platform wire constants =============="
5502
5503 test_59() {
5504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5505         echo "touch 130 files"
5506         createmany -o $DIR/f59- 130
5507         echo "rm 130 files"
5508         unlinkmany $DIR/f59- 130
5509         sync
5510         # wait for commitment of removal
5511         wait_delete_completed
5512 }
5513 run_test 59 "verify cancellation of llog records async ========="
5514
5515 TEST60_HEAD="test_60 run $RANDOM"
5516 test_60a() {
5517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5518         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5519         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5520                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5521                         skip_env "missing subtest run-llog.sh" && return
5522
5523         log "$TEST60_HEAD - from kernel mode"
5524         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5525         do_facet mgs sh run-llog.sh
5526         do_facet mgs $LCTL dk > $TMP/$tfile
5527
5528         # LU-6388: test llog_reader
5529         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5530         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5531         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5532                         skip_env "missing llog_reader" && return
5533         local fstype=$(facet_fstype mgs)
5534         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5535                 skip_env "Only for ldiskfs or zfs type mgs" && return
5536
5537         local mntpt=$(facet_mntpt mgs)
5538         local mgsdev=$(mgsdevname 1)
5539         local fid_list
5540         local fid
5541         local rec_list
5542         local rec
5543         local rec_type
5544         local obj_file
5545         local path
5546         local seq
5547         local oid
5548         local pass=true
5549
5550         #get fid and record list
5551         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5552                 tail -n 4))
5553         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5554                 tail -n 4))
5555         #remount mgs as ldiskfs or zfs type
5556         stop mgs || error "stop mgs failed"
5557         mount_fstype mgs || error "remount mgs failed"
5558         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5559                 fid=${fid_list[i]}
5560                 rec=${rec_list[i]}
5561                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5562                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5563                 oid=$((16#$oid))
5564
5565                 case $fstype in
5566                         ldiskfs )
5567                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5568                         zfs )
5569                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5570                 esac
5571                 echo "obj_file is $obj_file"
5572                 do_facet mgs $llog_reader $obj_file
5573
5574                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5575                         awk '{ print $3 }' | sed -e "s/^type=//g")
5576                 if [ $rec_type != $rec ]; then
5577                         echo "FAILED test_60a wrong record type $rec_type," \
5578                               "should be $rec"
5579                         pass=false
5580                         break
5581                 fi
5582
5583                 #check obj path if record type is LLOG_LOGID_MAGIC
5584                 if [ "$rec" == "1064553b" ]; then
5585                         path=$(do_facet mgs $llog_reader $obj_file |
5586                                 grep "path=" | awk '{ print $NF }' |
5587                                 sed -e "s/^path=//g")
5588                         if [ $obj_file != $mntpt/$path ]; then
5589                                 echo "FAILED test_60a wrong obj path" \
5590                                       "$montpt/$path, should be $obj_file"
5591                                 pass=false
5592                                 break
5593                         fi
5594                 fi
5595         done
5596         rm -f $TMP/$tfile
5597         #restart mgs before "error", otherwise it will block the next test
5598         stop mgs || error "stop mgs failed"
5599         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5600         $pass || error "test failed, see FAILED test_60a messages for specifics"
5601 }
5602 run_test 60a "llog_test run from kernel module and test llog_reader"
5603
5604 test_60aa() {
5605         # test old logid format
5606         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5607                 do_facet mgs $LCTL dl | grep MGS
5608                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5609                         error "old llog_print failed"
5610         fi
5611
5612         # test new logid format
5613         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5614                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5615                         error "new llog_print failed"
5616         fi
5617 }
5618 run_test 60aa "llog_print works with FIDs and simple names"
5619
5620 test_60b() { # bug 6411
5621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5622         dmesg > $DIR/$tfile
5623         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5624                                 /llog.test/ {
5625                                         if (marker)
5626                                                 from_marker++
5627                                         from_begin++
5628                                 }
5629                                 END {
5630                                         if (marker)
5631                                                 print from_marker
5632                                         else
5633                                                 print from_begin
5634                                 }")
5635         [[ $LLOG_COUNT -gt 100 ]] &&
5636                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5637 }
5638 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5639
5640 test_60c() {
5641         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5642         echo "create 5000 files"
5643         createmany -o $DIR/f60c- 5000
5644 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5645         lctl set_param fail_loc=0x80000137
5646         unlinkmany $DIR/f60c- 5000
5647         lctl set_param fail_loc=0
5648 }
5649 run_test 60c "unlink file when mds full"
5650
5651 test_60d() {
5652         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5653         SAVEPRINTK=$(lctl get_param -n printk)
5654
5655         # verify "lctl mark" is even working"
5656         MESSAGE="test message ID $RANDOM $$"
5657         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5658         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5659
5660         lctl set_param printk=0 || error "set lnet.printk failed"
5661         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5662         MESSAGE="new test message ID $RANDOM $$"
5663         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5664         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5665         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5666
5667         lctl set_param -n printk="$SAVEPRINTK"
5668 }
5669 run_test 60d "test printk console message masking"
5670
5671 test_60e() {
5672         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5673         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5674         touch $DIR/$tfile
5675 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5676         do_facet mds1 lctl set_param fail_loc=0x15b
5677         rm $DIR/$tfile
5678 }
5679 run_test 60e "no space while new llog is being created"
5680
5681 test_61() {
5682         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5683         f="$DIR/f61"
5684         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5685         cancel_lru_locks osc
5686         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5687         sync
5688 }
5689 run_test 61 "mmap() writes don't make sync hang ================"
5690
5691 # bug 2330 - insufficient obd_match error checking causes LBUG
5692 test_62() {
5693         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5694         f="$DIR/f62"
5695         echo foo > $f
5696         cancel_lru_locks osc
5697         lctl set_param fail_loc=0x405
5698         cat $f && error "cat succeeded, expect -EIO"
5699         lctl set_param fail_loc=0
5700 }
5701 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5702 # match every page all of the time.
5703 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5704
5705 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5706 # Though this test is irrelevant anymore, it helped to reveal some
5707 # other grant bugs (LU-4482), let's keep it.
5708 test_63a() {   # was test_63
5709         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5710         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5711         for i in `seq 10` ; do
5712                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5713                 sleep 5
5714                 kill $!
5715                 sleep 1
5716         done
5717
5718         rm -f $DIR/f63 || true
5719 }
5720 run_test 63a "Verify oig_wait interruption does not crash ======="
5721
5722 # bug 2248 - async write errors didn't return to application on sync
5723 # bug 3677 - async write errors left page locked
5724 test_63b() {
5725         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5726         debugsave
5727         lctl set_param debug=-1
5728
5729         # ensure we have a grant to do async writes
5730         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
5731         rm $DIR/$tfile
5732
5733         sync    # sync lest earlier test intercept the fail_loc
5734
5735         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
5736         lctl set_param fail_loc=0x80000406
5737         $MULTIOP $DIR/$tfile Owy && \
5738                 error "sync didn't return ENOMEM"
5739         sync; sleep 2; sync     # do a real sync this time to flush page
5740         lctl get_param -n llite.*.dump_page_cache | grep locked && \
5741                 error "locked page left in cache after async error" || true
5742         debugrestore
5743 }
5744 run_test 63b "async write errors should be returned to fsync ==="
5745
5746 test_64a () {
5747         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5748         df $DIR
5749         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
5750 }
5751 run_test 64a "verify filter grant calculations (in kernel) ====="
5752
5753 test_64b () {
5754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5755         sh oos.sh $MOUNT || error "oos.sh failed: $?"
5756 }
5757 run_test 64b "check out-of-space detection on client ==========="
5758
5759 test_64c() {
5760         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
5761 }
5762 run_test 64c "verify grant shrink ========================------"
5763
5764 # bug 1414 - set/get directories' stripe info
5765 test_65a() {
5766         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5767         test_mkdir -p $DIR/$tdir
5768         touch $DIR/$tdir/f1
5769         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
5770 }
5771 run_test 65a "directory with no stripe info ===================="
5772
5773 test_65b() {
5774         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5775         test_mkdir -p $DIR/$tdir
5776         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5777
5778         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5779                                                 error "setstripe"
5780         touch $DIR/$tdir/f2
5781         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
5782 }
5783 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
5784
5785 test_65c() {
5786         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5787         if [[ $OSTCOUNT -gt 1 ]]; then
5788                 test_mkdir -p $DIR/$tdir
5789                 local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5790
5791                 $SETSTRIPE -S $(($STRIPESIZE * 4)) -i 1 \
5792                         -c $(($OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
5793                 touch $DIR/$tdir/f3
5794                 $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
5795         fi
5796 }
5797 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
5798
5799 test_65d() {
5800         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5801         test_mkdir -p $DIR/$tdir
5802         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
5803         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5804
5805         if [[ $STRIPECOUNT -le 0 ]]; then
5806                 sc=1
5807         elif [[ $STRIPECOUNT -gt 2000 ]]; then
5808 #LOV_MAX_STRIPE_COUNT is 2000
5809                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
5810         else
5811                 sc=$(($STRIPECOUNT - 1))
5812         fi
5813         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
5814         touch $DIR/$tdir/f4 $DIR/$tdir/f5
5815         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
5816                 error "lverify failed"
5817 }
5818 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
5819
5820 test_65e() {
5821         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5822         test_mkdir -p $DIR/$tdir
5823
5824         $SETSTRIPE $DIR/$tdir || error "setstripe"
5825         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5826                                         error "no stripe info failed"
5827         touch $DIR/$tdir/f6
5828         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
5829 }
5830 run_test 65e "directory setstripe defaults ======================="
5831
5832 test_65f() {
5833         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5834         test_mkdir -p $DIR/${tdir}f
5835         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
5836 }
5837 run_test 65f "dir setstripe permission (should return error) ==="
5838
5839 test_65g() {
5840         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5841         test_mkdir -p $DIR/$tdir
5842         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5843
5844         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5845                                                         error "setstripe"
5846         $SETSTRIPE -d $DIR/$tdir || error "setstripe"
5847         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
5848                 error "delete default stripe failed"
5849 }
5850 run_test 65g "directory setstripe -d ==========================="
5851
5852 test_65h() {
5853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5854         test_mkdir -p $DIR/$tdir
5855         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
5856
5857         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
5858                                                         error "setstripe"
5859         test_mkdir -p $DIR/$tdir/dd1
5860         [ $($GETSTRIPE -c $DIR/$tdir) == $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
5861                 error "stripe info inherit failed"
5862 }
5863 run_test 65h "directory stripe info inherit ===================="
5864
5865 test_65i() { # bug6367
5866         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5867         $SETSTRIPE -S 65536 -c -1 $MOUNT
5868 }
5869 run_test 65i "set non-default striping on root directory (bug 6367)="
5870
5871 test_65ia() { # bug12836
5872         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5873         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
5874 }
5875 run_test 65ia "getstripe on -1 default directory striping"
5876
5877 test_65ib() { # bug12836
5878         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5879         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
5880 }
5881 run_test 65ib "getstripe -v on -1 default directory striping"
5882
5883 test_65ic() { # bug12836
5884         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5885         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
5886 }
5887 run_test 65ic "new find on -1 default directory striping"
5888
5889 test_65j() { # bug6367
5890         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5891         sync; sleep 1
5892         # if we aren't already remounting for each test, do so for this test
5893         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
5894                 cleanup || error "failed to unmount"
5895                 setup
5896         fi
5897         $SETSTRIPE -d $MOUNT || error "setstripe failed"
5898 }
5899 run_test 65j "set default striping on root directory (bug 6367)="
5900
5901 test_65k() { # bug11679
5902         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5903         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5904         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5905
5906         local disable_precreate=true
5907         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
5908                 disable_precreate=false
5909
5910         echo "Check OST status: "
5911         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
5912                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
5913
5914         for OSC in $MDS_OSCS; do
5915                 echo $OSC "is active"
5916                 do_facet $SINGLEMDS lctl --device %$OSC activate
5917         done
5918
5919         for INACTIVE_OSC in $MDS_OSCS; do
5920                 local ost=$(osc_to_ost $INACTIVE_OSC)
5921                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
5922                                lov.*md*.target_obd |
5923                                awk -F: /$ost/'{ print $1 }' | head -n 1)
5924
5925                 mkdir -p $DIR/$tdir
5926                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
5927                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
5928
5929                 echo "Deactivate: " $INACTIVE_OSC
5930                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
5931
5932                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
5933                               osp.$ost*MDT0000.create_count")
5934                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
5935                                   osp.$ost*MDT0000.max_create_count")
5936                 $disable_precreate &&
5937                         do_facet $SINGLEMDS "lctl set_param -n \
5938                                 osp.$ost*MDT0000.max_create_count=0"
5939
5940                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
5941                         [ -f $DIR/$tdir/$idx ] && continue
5942                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
5943                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
5944                                 error "setstripe $idx should succeed"
5945                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
5946                 done
5947                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
5948                 rmdir $DIR/$tdir
5949
5950                 do_facet $SINGLEMDS "lctl set_param -n \
5951                         osp.$ost*MDT0000.max_create_count=$max_count"
5952                 do_facet $SINGLEMDS "lctl set_param -n \
5953                         osp.$ost*MDT0000.create_count=$count"
5954                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
5955                 echo $INACTIVE_OSC "is Activate"
5956
5957                 wait_osc_import_state mds ost$ostnum FULL
5958         done
5959 }
5960 run_test 65k "validate manual striping works properly with deactivated OSCs"
5961
5962 test_65l() { # bug 12836
5963         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5964         test_mkdir -p $DIR/$tdir/test_dir
5965         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
5966         $LFS find -mtime -1 $DIR/$tdir >/dev/null
5967 }
5968 run_test 65l "lfs find on -1 stripe dir ========================"
5969
5970 test_65m() {
5971         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
5972         true
5973 }
5974 run_test 65m "normal user can't set filesystem default stripe"
5975
5976 # bug 2543 - update blocks count on client
5977 test_66() {
5978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5979         COUNT=${COUNT:-8}
5980         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
5981         sync; sync_all_data; sync; sync_all_data
5982         cancel_lru_locks osc
5983         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
5984         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
5985 }
5986 run_test 66 "update inode blocks count on client ==============="
5987
5988 meminfo() {
5989         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
5990 }
5991
5992 swap_used() {
5993         swapon -s | awk '($1 == "'$1'") { print $4 }'
5994 }
5995
5996 # bug5265, obdfilter oa2dentry return -ENOENT
5997 # #define OBD_FAIL_SRV_ENOENT 0x217
5998 test_69() {
5999         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6000         remote_ost_nodsh && skip "remote OST with nodsh" && return
6001
6002         f="$DIR/$tfile"
6003         $SETSTRIPE -c 1 -i 0 $f
6004
6005         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6006
6007         do_facet ost1 lctl set_param fail_loc=0x217
6008         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6009         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6010
6011         do_facet ost1 lctl set_param fail_loc=0
6012         $DIRECTIO write $f 0 2 || error "write error"
6013
6014         cancel_lru_locks osc
6015         $DIRECTIO read $f 0 1 || error "read error"
6016
6017         do_facet ost1 lctl set_param fail_loc=0x217
6018         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6019
6020         do_facet ost1 lctl set_param fail_loc=0
6021         rm -f $f
6022 }
6023 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6024
6025 test_71() {
6026         test_mkdir -p $DIR/$tdir
6027         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6028         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6029 }
6030 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6031
6032 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6033         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6034         [ "$RUNAS_ID" = "$UID" ] &&
6035                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6036
6037         # Check that testing environment is properly set up. Skip if not
6038         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
6039                 skip_env "User $RUNAS_ID does not exist - skipping"
6040                 return 0
6041         }
6042         touch $DIR/$tfile
6043         chmod 777 $DIR/$tfile
6044         chmod ug+s $DIR/$tfile
6045         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6046                 error "$RUNAS dd $DIR/$tfile failed"
6047         # See if we are still setuid/sgid
6048         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6049                 error "S/gid is not dropped on write"
6050         # Now test that MDS is updated too
6051         cancel_lru_locks mdc
6052         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6053                 error "S/gid is not dropped on MDS"
6054         rm -f $DIR/$tfile
6055 }
6056 run_test 72a "Test that remove suid works properly (bug5695) ===="
6057
6058 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6059         local perm
6060
6061         [ "$RUNAS_ID" = "$UID" ] && \
6062                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6063         [ "$RUNAS_ID" -eq 0 ] && \
6064                 skip_env "RUNAS_ID = 0 -- skipping" && return
6065
6066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6067         # Check that testing environment is properly set up. Skip if not
6068         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
6069                 skip_env "User $RUNAS_ID does not exist - skipping"
6070                 return 0
6071         }
6072         touch $DIR/${tfile}-f{g,u}
6073         test_mkdir $DIR/${tfile}-dg
6074         test_mkdir $DIR/${tfile}-du
6075         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6076         chmod g+s $DIR/${tfile}-{f,d}g
6077         chmod u+s $DIR/${tfile}-{f,d}u
6078         for perm in 777 2777 4777; do
6079                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6080                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6081                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6082                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6083         done
6084         true
6085 }
6086 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6087
6088 # bug 3462 - multiple simultaneous MDC requests
6089 test_73() {
6090         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6091         test_mkdir $DIR/d73-1
6092         test_mkdir $DIR/d73-2
6093         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6094         pid1=$!
6095
6096         lctl set_param fail_loc=0x80000129
6097         $MULTIOP $DIR/d73-1/f73-2 Oc &
6098         sleep 1
6099         lctl set_param fail_loc=0
6100
6101         $MULTIOP $DIR/d73-2/f73-3 Oc &
6102         pid3=$!
6103
6104         kill -USR1 $pid1
6105         wait $pid1 || return 1
6106
6107         sleep 25
6108
6109         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6110         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6111         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6112
6113         rm -rf $DIR/d73-*
6114 }
6115 run_test 73 "multiple MDC requests (should not deadlock)"
6116
6117 test_74a() { # bug 6149, 6184
6118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6119         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6120         #
6121         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6122         # will spin in a tight reconnection loop
6123         touch $DIR/f74a
6124         $LCTL set_param fail_loc=0x8000030e
6125         # get any lock that won't be difficult - lookup works.
6126         ls $DIR/f74a
6127         $LCTL set_param fail_loc=0
6128         rm -f $DIR/f74a
6129         true
6130 }
6131 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6132
6133 test_74b() { # bug 13310
6134         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6135         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6136         #
6137         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6138         # will spin in a tight reconnection loop
6139         $LCTL set_param fail_loc=0x8000030e
6140         # get a "difficult" lock
6141         touch $DIR/f74b
6142         $LCTL set_param fail_loc=0
6143         rm -f $DIR/f74b
6144         true
6145 }
6146 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6147
6148 test_74c() {
6149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6150         #define OBD_FAIL_LDLM_NEW_LOCK
6151         $LCTL set_param fail_loc=0x319
6152         touch $DIR/$tfile && error "touch successful"
6153         $LCTL set_param fail_loc=0
6154         true
6155 }
6156 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6157
6158 num_inodes() {
6159         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6160 }
6161
6162 test_76() { # Now for bug 20433, added originally in bug 1443
6163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6164         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6165         cancel_lru_locks osc
6166         BEFORE_INODES=$(num_inodes)
6167         echo "before inodes: $BEFORE_INODES"
6168         local COUNT=1000
6169         [ "$SLOW" = "no" ] && COUNT=100
6170         for i in $(seq $COUNT); do
6171                 touch $DIR/$tfile
6172                 rm -f $DIR/$tfile
6173         done
6174         cancel_lru_locks osc
6175         AFTER_INODES=$(num_inodes)
6176         echo "after inodes: $AFTER_INODES"
6177         local wait=0
6178         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6179                 sleep 2
6180                 AFTER_INODES=$(num_inodes)
6181                 wait=$((wait+2))
6182                 echo "wait $wait seconds inodes: $AFTER_INODES"
6183                 if [ $wait -gt 30 ]; then
6184                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6185                 fi
6186         done
6187 }
6188 run_test 76 "confirm clients recycle inodes properly ===="
6189
6190
6191 export ORIG_CSUM=""
6192 set_checksums()
6193 {
6194         # Note: in sptlrpc modes which enable its own bulk checksum, the
6195         # original crc32_le bulk checksum will be automatically disabled,
6196         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6197         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6198         # In this case set_checksums() will not be no-op, because sptlrpc
6199         # bulk checksum will be enabled all through the test.
6200
6201         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6202         lctl set_param -n osc.*.checksums $1
6203         return 0
6204 }
6205
6206 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6207                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6208 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6209 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6210 set_checksum_type()
6211 {
6212         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6213         log "set checksum type to $1"
6214         return 0
6215 }
6216 F77_TMP=$TMP/f77-temp
6217 F77SZ=8
6218 setup_f77() {
6219         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6220                 error "error writing to $F77_TMP"
6221 }
6222
6223 test_77a() { # bug 10889
6224         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6225         $GSS && skip "could not run with gss" && return
6226         [ ! -f $F77_TMP ] && setup_f77
6227         set_checksums 1
6228         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6229         set_checksums 0
6230         rm -f $DIR/$tfile
6231 }
6232 run_test 77a "normal checksum read/write operation"
6233
6234 test_77b() { # bug 10889
6235         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6236         $GSS && skip "could not run with gss" && return
6237         [ ! -f $F77_TMP ] && setup_f77
6238         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6239         $LCTL set_param fail_loc=0x80000409
6240         set_checksums 1
6241
6242         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6243                 error "dd error: $?"
6244         $LCTL set_param fail_loc=0
6245
6246         for algo in $CKSUM_TYPES; do
6247                 cancel_lru_locks osc
6248                 set_checksum_type $algo
6249                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6250                 $LCTL set_param fail_loc=0x80000408
6251                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6252                 $LCTL set_param fail_loc=0
6253         done
6254         set_checksums 0
6255         set_checksum_type $ORIG_CSUM_TYPE
6256         rm -f $DIR/$tfile
6257 }
6258 run_test 77b "checksum error on client write, read"
6259
6260 cleanup_77c() {
6261         trap 0
6262         set_checksums 0
6263         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
6264         $check_ost &&
6265                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
6266         [ -n $osc_file_prefix ] && rm -f ${osc_file_prefix}*
6267         $check_ost && [ -n $ost_file_prefix ] &&
6268                 do_facet ost1 rm -f ${ost_file_prefix}\*
6269 }
6270
6271 test_77c() {
6272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6273         $GSS && skip "could not run with gss" && return
6274
6275         local bad1
6276         local osc_file_prefix
6277         local osc_file
6278         local check_ost=false
6279         local ost_file_prefix
6280         local ost_file
6281         local orig_cksum
6282         local dump_cksum
6283         local fid
6284
6285         # ensure corruption will occur on first OSS/OST
6286         $LFS setstripe -i 0 $DIR/$tfile
6287
6288         [ ! -f $F77_TMP ] && setup_f77
6289         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6290                 error "dd write error: $?"
6291         fid=$($LFS path2fid $DIR/$tfile)
6292
6293         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
6294         then
6295                 check_ost=true
6296                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
6297                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
6298         else
6299                 echo "OSS do not support bulk pages dump upon error"
6300         fi
6301
6302         osc_file_prefix=$($LCTL get_param -n debug_path)
6303         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
6304
6305         trap cleanup_77c EXIT
6306
6307         set_checksums 1
6308         # enable bulk pages dump upon error on Client
6309         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
6310         # enable bulk pages dump upon error on OSS
6311         $check_ost &&
6312                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
6313
6314         # flush Client cache to allow next read to reach OSS
6315         cancel_lru_locks osc
6316
6317         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
6318         $LCTL set_param fail_loc=0x80000408
6319         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
6320         $LCTL set_param fail_loc=0
6321
6322         rm -f $DIR/$tfile
6323
6324         # check cksum dump on Client
6325         osc_file=$(ls ${osc_file_prefix}*)
6326         [ -n "$osc_file" ] || error "no checksum dump file on Client"
6327         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
6328         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
6329         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
6330         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
6331                      cksum)
6332         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
6333         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6334                 error "dump content does not match on Client"
6335
6336         $check_ost || { skip "No need to check cksum dump on OSS"; return 0; }
6337
6338         # check cksum dump on OSS
6339         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
6340         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
6341         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
6342         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
6343         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6344                 error "dump content does not match on OSS"
6345
6346         cleanup_77c
6347 }
6348 run_test 77c "checksum error on client read with debug"
6349
6350 test_77d() { # bug 10889
6351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6352         $GSS && skip "could not run with gss" && return
6353         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6354         $LCTL set_param fail_loc=0x80000409
6355         set_checksums 1
6356         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6357                 error "direct write: rc=$?"
6358         $LCTL set_param fail_loc=0
6359         set_checksums 0
6360
6361         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6362         $LCTL set_param fail_loc=0x80000408
6363         set_checksums 1
6364         cancel_lru_locks osc
6365         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6366                 error "direct read: rc=$?"
6367         $LCTL set_param fail_loc=0
6368         set_checksums 0
6369 }
6370 run_test 77d "checksum error on OST direct write, read"
6371
6372 test_77f() { # bug 10889
6373         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6374         $GSS && skip "could not run with gss" && return
6375         set_checksums 1
6376         for algo in $CKSUM_TYPES; do
6377                 cancel_lru_locks osc
6378                 set_checksum_type $algo
6379                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6380                 $LCTL set_param fail_loc=0x409
6381                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6382                         error "direct write succeeded"
6383                 $LCTL set_param fail_loc=0
6384         done
6385         set_checksum_type $ORIG_CSUM_TYPE
6386         set_checksums 0
6387 }
6388 run_test 77f "repeat checksum error on write (expect error)"
6389
6390 test_77g() { # bug 10889
6391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6392         $GSS && skip "could not run with gss" && return
6393         remote_ost_nodsh && skip "remote OST with nodsh" && return
6394
6395         [ ! -f $F77_TMP ] && setup_f77
6396
6397         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6398         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6399         do_facet ost1 lctl set_param fail_loc=0x8000021a
6400         set_checksums 1
6401         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6402                 error "write error: rc=$?"
6403         do_facet ost1 lctl set_param fail_loc=0
6404         set_checksums 0
6405
6406         cancel_lru_locks osc
6407         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6408         do_facet ost1 lctl set_param fail_loc=0x8000021b
6409         set_checksums 1
6410         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6411         do_facet ost1 lctl set_param fail_loc=0
6412         set_checksums 0
6413 }
6414 run_test 77g "checksum error on OST write, read"
6415
6416 test_77j() { # bug 13805
6417         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6418         $GSS && skip "could not run with gss" && return
6419         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6420         lctl set_param fail_loc=0x40c
6421         remount_client $MOUNT
6422         lctl set_param fail_loc=0
6423         # wait async osc connect to finish and reflect updated state value
6424         local i
6425         for (( i=0; i < OSTCOUNT; i++ )) ; do
6426                 wait_osc_import_state client ost$((i+1)) FULL
6427         done
6428
6429         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6430                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6431                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6432                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6433         done
6434         remount_client $MOUNT
6435 }
6436 run_test 77j "client only supporting ADLER32"
6437
6438 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6439 rm -f $F77_TMP
6440 unset F77_TMP
6441
6442 cleanup_test_78() {
6443         trap 0
6444         rm -f $DIR/$tfile
6445 }
6446
6447 test_78() { # bug 10901
6448         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6449         remote_ost || { skip_env "local OST" && return; }
6450
6451         NSEQ=5
6452         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6453         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6454         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6455         echo "MemTotal: $MEMTOTAL"
6456
6457         # reserve 256MB of memory for the kernel and other running processes,
6458         # and then take 1/2 of the remaining memory for the read/write buffers.
6459         if [ $MEMTOTAL -gt 512 ] ;then
6460                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6461         else
6462                 # for those poor memory-starved high-end clusters...
6463                 MEMTOTAL=$((MEMTOTAL / 2))
6464         fi
6465         echo "Mem to use for directio: $MEMTOTAL"
6466
6467         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6468         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6469         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6470         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6471                 head -n1)
6472         echo "Smallest OST: $SMALLESTOST"
6473         [[ $SMALLESTOST -lt 10240 ]] &&
6474                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6475
6476         trap cleanup_test_78 EXIT
6477
6478         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6479                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6480
6481         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6482         echo "File size: $F78SIZE"
6483         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6484         for i in $(seq 1 $NSEQ); do
6485                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6486                 echo directIO rdwr round $i of $NSEQ
6487                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6488         done
6489
6490         cleanup_test_78
6491 }
6492 run_test 78 "handle large O_DIRECT writes correctly ============"
6493
6494 test_79() { # bug 12743
6495         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6496         wait_delete_completed
6497
6498         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6499         BKFREE=$(calc_osc_kbytes kbytesfree)
6500         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6501
6502         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6503         DFTOTAL=`echo $STRING | cut -d, -f1`
6504         DFUSED=`echo $STRING  | cut -d, -f2`
6505         DFAVAIL=`echo $STRING | cut -d, -f3`
6506         DFFREE=$(($DFTOTAL - $DFUSED))
6507
6508         ALLOWANCE=$((64 * $OSTCOUNT))
6509
6510         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6511            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6512                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6513         fi
6514         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6515            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6516                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6517         fi
6518         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6519            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6520                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6521         fi
6522 }
6523 run_test 79 "df report consistency check ======================="
6524
6525 test_80() { # bug 10718
6526         remote_ost_nodsh && skip "remote OST with nodsh" && return
6527         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6528         # relax strong synchronous semantics for slow backends like ZFS
6529         local soc="obdfilter.*.sync_on_lock_cancel"
6530         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6531         local hosts=
6532         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6533                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6534                           facet_active_host $host; done | sort -u)
6535                 do_nodes $hosts lctl set_param $soc=never
6536         fi
6537
6538         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6539         sync; sleep 1; sync
6540         local BEFORE=`date +%s`
6541         cancel_lru_locks osc
6542         local AFTER=`date +%s`
6543         local DIFF=$((AFTER-BEFORE))
6544         if [ $DIFF -gt 1 ] ; then
6545                 error "elapsed for 1M@1T = $DIFF"
6546         fi
6547
6548         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6549
6550         rm -f $DIR/$tfile
6551 }
6552 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6553
6554 test_81a() { # LU-456
6555         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6556         remote_ost_nodsh && skip "remote OST with nodsh" && return
6557         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6558         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6559         do_facet ost1 lctl set_param fail_loc=0x80000228
6560
6561         # write should trigger a retry and success
6562         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6563         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6564         RC=$?
6565         if [ $RC -ne 0 ] ; then
6566                 error "write should success, but failed for $RC"
6567         fi
6568 }
6569 run_test 81a "OST should retry write when get -ENOSPC ==============="
6570
6571 test_81b() { # LU-456
6572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6573         remote_ost_nodsh && skip "remote OST with nodsh" && return
6574         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6575         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6576         do_facet ost1 lctl set_param fail_loc=0x228
6577
6578         # write should retry several times and return -ENOSPC finally
6579         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6580         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6581         RC=$?
6582         ENOSPC=28
6583         if [ $RC -ne $ENOSPC ] ; then
6584                 error "dd should fail for -ENOSPC, but succeed."
6585         fi
6586 }
6587 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6588
6589 test_82() { # LU-1031
6590         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6591         local gid1=14091995
6592         local gid2=16022000
6593
6594         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6595         local MULTIPID1=$!
6596         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6597         local MULTIPID2=$!
6598         kill -USR1 $MULTIPID2
6599         sleep 2
6600         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6601                 error "First grouplock does not block second one"
6602         else
6603                 echo "Second grouplock blocks first one"
6604         fi
6605         kill -USR1 $MULTIPID1
6606         wait $MULTIPID1
6607         wait $MULTIPID2
6608 }
6609 run_test 82 "Basic grouplock test ==============================="
6610
6611 test_83() {
6612         local sfile="/boot/System.map-$(uname -r)"
6613         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
6614         $LCTL set_param fail_loc=0x140d
6615         cp $sfile $DIR/$tfile || error "write failed"
6616         diff -c $sfile $DIR/$tfile || error "files are different"
6617         $LCTL set_param fail_loc=0
6618         rm -f $DIR/$tfile
6619 }
6620 run_test 83 "Short write in ptask ==============================="
6621
6622 test_99a() {
6623         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6624                 return
6625         test_mkdir -p $DIR/d99cvsroot
6626         chown $RUNAS_ID $DIR/d99cvsroot
6627         local oldPWD=$PWD       # bug 13584, use $TMP as working dir
6628         cd $TMP
6629
6630         $RUNAS cvs -d $DIR/d99cvsroot init || error "cvs init failed"
6631         cd $oldPWD
6632 }
6633 run_test 99a "cvs init ========================================="
6634
6635 test_99b() {
6636         [ -z "$(which cvs 2>/dev/null)" ] &&
6637                 skip_env "could not find cvs" && return
6638         [ ! -d $DIR/d99cvsroot ] && test_99a
6639         cd /etc/init.d
6640         # some versions of cvs import exit(1) when asked to import links or
6641         # files they can't read.  ignore those files.
6642         TOIGNORE=$(find . -type l -printf '-I %f\n' -o \
6643                         ! -perm /4 -printf '-I %f\n')
6644         $RUNAS cvs -d $DIR/d99cvsroot import -m "nomesg" $TOIGNORE \
6645                 d99reposname vtag rtag
6646 }
6647 run_test 99b "cvs import ======================================="
6648
6649 test_99c() {
6650         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6651         [ ! -d $DIR/d99cvsroot ] && test_99b
6652         cd $DIR
6653         test_mkdir -p $DIR/d99reposname
6654         chown $RUNAS_ID $DIR/d99reposname
6655         $RUNAS cvs -d $DIR/d99cvsroot co d99reposname
6656 }
6657 run_test 99c "cvs checkout ====================================="
6658
6659 test_99d() {
6660         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6661         [ ! -d $DIR/d99cvsroot ] && test_99c
6662         cd $DIR/d99reposname
6663         $RUNAS touch foo99
6664         $RUNAS cvs add -m 'addmsg' foo99
6665 }
6666 run_test 99d "cvs add =========================================="
6667
6668 test_99e() {
6669         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6670         [ ! -d $DIR/d99cvsroot ] && test_99c
6671         cd $DIR/d99reposname
6672         $RUNAS cvs update
6673 }
6674 run_test 99e "cvs update ======================================="
6675
6676 test_99f() {
6677         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" && return
6678         [ ! -d $DIR/d99cvsroot ] && test_99d
6679         cd $DIR/d99reposname
6680         $RUNAS cvs commit -m 'nomsg' foo99
6681     rm -fr $DIR/d99cvsroot
6682 }
6683 run_test 99f "cvs commit ======================================="
6684
6685 test_100() {
6686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6687         [[ "$NETTYPE" =~ tcp ]] ||
6688                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" &&
6689                         return ; }
6690
6691         remote_ost_nodsh && skip "remote OST with nodsh" && return
6692         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6693         remote_servers ||
6694                 { skip "useless for local single node setup" && return; }
6695
6696         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
6697                 [ "$PROT" != "tcp" ] && continue
6698                 RPORT=$(echo $REMOTE | cut -d: -f2)
6699                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
6700
6701                 rc=0
6702                 LPORT=`echo $LOCAL | cut -d: -f2`
6703                 if [ $LPORT -ge 1024 ]; then
6704                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
6705                         netstat -tna
6706                         error_exit "local: $LPORT > 1024, remote: $RPORT"
6707                 fi
6708         done
6709         [ "$rc" = 0 ] || error_exit "privileged port not found" )
6710 }
6711 run_test 100 "check local port using privileged port ==========="
6712
6713 function get_named_value()
6714 {
6715     local tag
6716
6717     tag=$1
6718     while read ;do
6719         line=$REPLY
6720         case $line in
6721         $tag*)
6722             echo $line | sed "s/^$tag[ ]*//"
6723             break
6724             ;;
6725         esac
6726     done
6727 }
6728
6729 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
6730                    awk '/^max_cached_mb/ { print $2 }')
6731
6732 cleanup_101a() {
6733         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
6734         trap 0
6735 }
6736
6737 test_101a() {
6738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6739         [ $MDSCOUNT -ge 2 ] && skip "needs < 2 MDTs" && return #LU-4322
6740         local s
6741         local discard
6742         local nreads=10000
6743         local cache_limit=32
6744
6745         $LCTL set_param -n osc.*-osc*.rpc_stats 0
6746         trap cleanup_101a EXIT
6747         $LCTL set_param -n llite.*.read_ahead_stats 0
6748         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
6749
6750         #
6751         # randomly read 10000 of 64K chunks from file 3x 32MB in size
6752         #
6753         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
6754         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
6755
6756         discard=0
6757         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
6758                 get_named_value 'read but discarded' | cut -d" " -f1); do
6759                         discard=$(($discard + $s))
6760         done
6761         cleanup_101a
6762
6763         if [[ $(($discard * 10)) -gt $nreads ]]; then
6764                 $LCTL get_param osc.*-osc*.rpc_stats
6765                 $LCTL get_param llite.*.read_ahead_stats
6766                 error "too many ($discard) discarded pages"
6767         fi
6768         rm -f $DIR/$tfile || true
6769 }
6770 run_test 101a "check read-ahead for random reads ================"
6771
6772 setup_test101bc() {
6773         test_mkdir -p $DIR/$tdir
6774         local STRIPE_SIZE=$1
6775         local FILE_LENGTH=$2
6776         STRIPE_OFFSET=0
6777
6778         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
6779
6780         local list=$(comma_list $(osts_nodes))
6781         set_osd_param $list '' read_cache_enable 0
6782         set_osd_param $list '' writethrough_cache_enable 0
6783
6784         trap cleanup_test101bc EXIT
6785         # prepare the read-ahead file
6786         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
6787
6788         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
6789                                 count=$FILE_SIZE_MB 2> /dev/null
6790
6791 }
6792
6793 cleanup_test101bc() {
6794         trap 0
6795         rm -rf $DIR/$tdir
6796         rm -f $DIR/$tfile
6797
6798         local list=$(comma_list $(osts_nodes))
6799         set_osd_param $list '' read_cache_enable 1
6800         set_osd_param $list '' writethrough_cache_enable 1
6801 }
6802
6803 calc_total() {
6804         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
6805 }
6806
6807 ra_check_101() {
6808         local READ_SIZE=$1
6809         local STRIPE_SIZE=$2
6810         local FILE_LENGTH=$3
6811         local RA_INC=1048576
6812         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
6813         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
6814                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
6815         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
6816                         get_named_value 'read but discarded' |
6817                         cut -d" " -f1 | calc_total)
6818         if [[ $DISCARD -gt $discard_limit ]]; then
6819                 $LCTL get_param llite.*.read_ahead_stats
6820                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
6821         else
6822                 echo "Read-ahead success for size ${READ_SIZE}"
6823         fi
6824 }
6825
6826 test_101b() {
6827         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6828         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6829         local STRIPE_SIZE=1048576
6830         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
6831         if [ $SLOW == "yes" ]; then
6832                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
6833         else
6834                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
6835         fi
6836
6837         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
6838
6839         # prepare the read-ahead file
6840         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6841         cancel_lru_locks osc
6842         for BIDX in 2 4 8 16 32 64 128 256
6843         do
6844                 local BSIZE=$((BIDX*4096))
6845                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
6846                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
6847                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
6848                 $LCTL set_param -n llite.*.read_ahead_stats 0
6849                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
6850                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
6851                 cancel_lru_locks osc
6852                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
6853         done
6854         cleanup_test101bc
6855         true
6856 }
6857 run_test 101b "check stride-io mode read-ahead ================="
6858
6859 test_101c() {
6860         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6861         local STRIPE_SIZE=1048576
6862         local FILE_LENGTH=$((STRIPE_SIZE*100))
6863         local nreads=10000
6864         local osc_rpc_stats
6865
6866         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
6867
6868         cancel_lru_locks osc
6869         $LCTL set_param osc.*.rpc_stats 0
6870         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
6871         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
6872                 local stats=$($LCTL get_param -n $osc_rpc_stats)
6873                 local lines=$(echo "$stats" | awk 'END {print NR;}')
6874                 local size
6875
6876                 if [ $lines -le 20 ]; then
6877                         continue
6878                 fi
6879                 for size in 1 2 4 8; do
6880                         local rpc=$(echo "$stats" |
6881                                     awk '($1 == "'$size':") {print $2; exit; }')
6882                         [ $rpc != 0 ] &&
6883                                 error "Small $((size*4))k read IO $rpc !"
6884                 done
6885                 echo "$osc_rpc_stats check passed!"
6886         done
6887         cleanup_test101bc
6888         true
6889 }
6890 run_test 101c "check stripe_size aligned read-ahead ================="
6891
6892 set_read_ahead() {
6893         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
6894         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
6895 }
6896
6897 test_101d() {
6898         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6899         local file=$DIR/$tfile
6900         local sz_MB=${FILESIZE_101d:-500}
6901         local ra_MB=${READAHEAD_MB:-40}
6902
6903         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
6904         [ $free_MB -lt $sz_MB ] &&
6905                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
6906
6907         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
6908         $SETSTRIPE -c -1 $file || error "setstripe failed"
6909
6910         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
6911         echo Cancel LRU locks on lustre client to flush the client cache
6912         cancel_lru_locks osc
6913
6914         echo Disable read-ahead
6915         local old_READAHEAD=$(set_read_ahead 0)
6916
6917         echo Reading the test file $file with read-ahead disabled
6918         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6919
6920         echo Cancel LRU locks on lustre client to flush the client cache
6921         cancel_lru_locks osc
6922         echo Enable read-ahead with ${ra_MB}MB
6923         set_read_ahead $ra_MB
6924
6925         echo Reading the test file $file with read-ahead enabled
6926         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
6927
6928         echo "read-ahead disabled time read $raOFF"
6929         echo "read-ahead enabled  time read $raON"
6930
6931         set_read_ahead $old_READAHEAD
6932         rm -f $file
6933         wait_delete_completed
6934
6935         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
6936                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
6937 }
6938 run_test 101d "file read with and without read-ahead enabled"
6939
6940 test_101e() {
6941         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6942         local file=$DIR/$tfile
6943         local size_KB=500  #KB
6944         local count=100
6945         local bsize=1024
6946
6947         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
6948         local need_KB=$((count * size_KB))
6949         [[ $free_KB -le $need_KB ]] &&
6950                 skip_env "Need free space $need_KB, have $free_KB" && return
6951
6952         echo "Creating $count ${size_KB}K test files"
6953         for ((i = 0; i < $count; i++)); do
6954                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
6955         done
6956
6957         echo "Cancel LRU locks on lustre client to flush the client cache"
6958         cancel_lru_locks osc
6959
6960         echo "Reset readahead stats"
6961         $LCTL set_param -n llite.*.read_ahead_stats 0
6962
6963         for ((i = 0; i < $count; i++)); do
6964                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
6965         done
6966
6967         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
6968                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
6969
6970         for ((i = 0; i < $count; i++)); do
6971                 rm -rf $file.$i 2>/dev/null
6972         done
6973
6974         #10000 means 20% reads are missing in readahead
6975         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
6976 }
6977 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
6978
6979 test_101f() {
6980         which iozone || { skip "no iozone installed" && return; }
6981
6982         local old_debug=$($LCTL get_param debug)
6983         old_debug=${old_debug#*=}
6984         $LCTL set_param debug="reada mmap"
6985
6986         # create a test file
6987         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
6988
6989         echo Cancel LRU locks on lustre client to flush the client cache
6990         cancel_lru_locks osc
6991
6992         echo Reset readahead stats
6993         $LCTL set_param -n llite.*.read_ahead_stats 0
6994
6995         echo mmap read the file with small block size
6996         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
6997                 > /dev/null 2>&1
6998
6999         echo checking missing pages
7000         $LCTL get_param llite.*.read_ahead_stats
7001         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7002                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7003
7004         $LCTL set_param debug="$old_debug"
7005         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7006         rm -f $DIR/$tfile
7007 }
7008 run_test 101f "check mmap read performance"
7009
7010 test_101g_brw_size_test() {
7011         local mb=$1
7012         local pages=$((mb * 1048576 / $(page_size)))
7013
7014         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7015                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7016         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7017                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7018                         return 2
7019         done
7020
7021         $LCTL set_param -n osc.*.rpc_stats=0
7022
7023         # 10 RPCs should be enough for the test
7024         local count=10
7025         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
7026                 { error "dd write ${mb} MB blocks failed"; return 3; }
7027         cancel_lru_locks osc
7028         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
7029                 { error "dd write ${mb} MB blocks failed"; return 4; }
7030
7031         # calculate number of full-sized read and write RPCs
7032         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7033                 sed -n '/pages per rpc/,/^$/p' |
7034                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7035                 END { print reads,writes }'))
7036         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7037                 return 5
7038         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7039                 return 6
7040
7041         return 0
7042 }
7043
7044 test_101g() {
7045         local rpcs
7046         local osts=$(get_facets OST)
7047         local list=$(comma_list $(osts_nodes))
7048         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7049         local brw_size="obdfilter.*.brw_size"
7050         local ostver=$(lustre_version_code ost1)
7051
7052         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7053
7054         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7055         if [ $ostver -ge $(version_code 2.8.52) ] ||
7056            [ $ostver -ge $(version_code 2.7.17) -a \
7057              $ostver -lt $(version_code 2.7.50) ]; then
7058                 [ $ostver -ge $(version_code 2.9.52) ] && suffix="M"
7059                 if [[ $orig_mb -lt 16 ]]; then
7060                         save_lustre_params $osts "$brw_size" > $p
7061                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7062                                 error "set 16MB RPC size failed"
7063
7064                         echo "remount client to enable new RPC size"
7065                         remount_client $MOUNT || error "remount_client failed"
7066                 fi
7067
7068                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7069                 # should be able to set brw_size=12, but no rpc_stats for that
7070                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7071         fi
7072
7073         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7074
7075         if [[ $orig_mb -lt 16 ]]; then
7076                 restore_lustre_params < $p
7077                 remount_client $MOUNT || error "remount_client restore failed"
7078         fi
7079
7080         rm -f $p $DIR/$tfile
7081 }
7082 run_test 101g "Big bulk(4/16 MiB) readahead"
7083
7084 setup_test102() {
7085         test_mkdir $DIR/$tdir
7086         chown $RUNAS_ID $DIR/$tdir
7087         STRIPE_SIZE=65536
7088         STRIPE_OFFSET=1
7089         STRIPE_COUNT=$OSTCOUNT
7090         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7091
7092         trap cleanup_test102 EXIT
7093         cd $DIR
7094         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7095         cd $DIR/$tdir
7096         for num in 1 2 3 4; do
7097                 for count in $(seq 1 $STRIPE_COUNT); do
7098                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7099                                 local size=`expr $STRIPE_SIZE \* $num`
7100                                 local file=file"$num-$idx-$count"
7101                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7102                         done
7103                 done
7104         done
7105
7106         cd $DIR
7107         $1 tar cf $TMP/f102.tar $tdir --xattrs
7108 }
7109
7110 cleanup_test102() {
7111         trap 0
7112         rm -f $TMP/f102.tar
7113         rm -rf $DIR/d0.sanity/d102
7114 }
7115
7116 test_102a() {
7117         local testfile=$DIR/$tfile
7118
7119         touch $testfile
7120
7121         [ "$UID" != 0 ] && skip_env "must run as root" && return
7122         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7123                 skip_env "must have user_xattr" && return
7124
7125         [ -z "$(which setfattr 2>/dev/null)" ] &&
7126                 skip_env "could not find setfattr" && return
7127
7128         echo "set/get xattr..."
7129         setfattr -n trusted.name1 -v value1 $testfile ||
7130                 error "setfattr -n trusted.name1=value1 $testfile failed"
7131         getfattr -n trusted.name1 $testfile 2> /dev/null |
7132           grep "trusted.name1=.value1" ||
7133                 error "$testfile missing trusted.name1=value1"
7134
7135         setfattr -n user.author1 -v author1 $testfile ||
7136                 error "setfattr -n user.author1=author1 $testfile failed"
7137         getfattr -n user.author1 $testfile 2> /dev/null |
7138           grep "user.author1=.author1" ||
7139                 error "$testfile missing trusted.author1=author1"
7140
7141         echo "listxattr..."
7142         setfattr -n trusted.name2 -v value2 $testfile ||
7143                 error "$testfile unable to set trusted.name2"
7144         setfattr -n trusted.name3 -v value3 $testfile ||
7145                 error "$testfile unable to set trusted.name3"
7146         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7147             grep "trusted.name" | wc -l) -eq 3 ] ||
7148                 error "$testfile missing 3 trusted.name xattrs"
7149
7150         setfattr -n user.author2 -v author2 $testfile ||
7151                 error "$testfile unable to set user.author2"
7152         setfattr -n user.author3 -v author3 $testfile ||
7153                 error "$testfile unable to set user.author3"
7154         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7155             grep "user.author" | wc -l) -eq 3 ] ||
7156                 error "$testfile missing 3 user.author xattrs"
7157
7158         echo "remove xattr..."
7159         setfattr -x trusted.name1 $testfile ||
7160                 error "$testfile error deleting trusted.name1"
7161         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7162                 error "$testfile did not delete trusted.name1 xattr"
7163
7164         setfattr -x user.author1 $testfile ||
7165                 error "$testfile error deleting user.author1"
7166         echo "set lustre special xattr ..."
7167         $LFS setstripe -c1 $testfile
7168         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7169                 awk -F "=" '/trusted.lov/ { print $2 }' )
7170         setfattr -n "trusted.lov" -v $lovea $testfile ||
7171                 error "$testfile doesn't ignore setting trusted.lov again"
7172         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7173                 error "$testfile allow setting invalid trusted.lov"
7174         rm -f $testfile
7175 }
7176 run_test 102a "user xattr test =================================="
7177
7178 test_102b() {
7179         [ -z "$(which setfattr 2>/dev/null)" ] &&
7180                 skip_env "could not find setfattr" && return
7181
7182         # b10930: get/set/list trusted.lov xattr
7183         echo "get/set/list trusted.lov xattr ..."
7184         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7185         local testfile=$DIR/$tfile
7186         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7187                 error "setstripe failed"
7188         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7189                 error "getstripe failed"
7190         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7191                 error "can't get trusted.lov from $testfile"
7192
7193         local testfile2=${testfile}2
7194         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7195                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7196
7197         $MCREATE $testfile2
7198         setfattr -n trusted.lov -v $value $testfile2
7199         local stripe_size=$($GETSTRIPE -S $testfile2)
7200         local stripe_count=$($GETSTRIPE -c $testfile2)
7201         [[ $stripe_size -eq 65536 ]] ||
7202                 error "stripe size $stripe_size != 65536"
7203         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7204                 error "stripe count $stripe_count != $STRIPECOUNT"
7205         rm -f $DIR/$tfile
7206 }
7207 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7208
7209 test_102c() {
7210         [ -z "$(which setfattr 2>/dev/null)" ] &&
7211                 skip_env "could not find setfattr" && return
7212
7213         # b10930: get/set/list lustre.lov xattr
7214         echo "get/set/list lustre.lov xattr ..."
7215         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7216         test_mkdir $DIR/$tdir
7217         chown $RUNAS_ID $DIR/$tdir
7218         local testfile=$DIR/$tdir/$tfile
7219         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7220                 error "setstripe failed"
7221         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7222                 error "getstripe failed"
7223         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7224         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7225
7226         local testfile2=${testfile}2
7227         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7228                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7229
7230         $RUNAS $MCREATE $testfile2
7231         $RUNAS setfattr -n lustre.lov -v $value $testfile2
7232         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
7233         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
7234         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
7235         [ $stripe_count -eq $STRIPECOUNT ] ||
7236                 error "stripe count $stripe_count != $STRIPECOUNT"
7237 }
7238 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
7239
7240 compare_stripe_info1() {
7241         local stripe_index_all_zero=true
7242
7243         for num in 1 2 3 4; do
7244                 for count in $(seq 1 $STRIPE_COUNT); do
7245                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
7246                                 local size=$((STRIPE_SIZE * num))
7247                                 local file=file"$num-$offset-$count"
7248                                 stripe_size=$($LFS getstripe -S $PWD/$file)
7249                                 [[ $stripe_size -ne $size ]] &&
7250                                     error "$file: size $stripe_size != $size"
7251                                 stripe_count=$($LFS getstripe -c $PWD/$file)
7252                                 # allow fewer stripes to be created, ORI-601
7253                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
7254                                     error "$file: count $stripe_count != $count"
7255                                 stripe_index=$($LFS getstripe -i $PWD/$file)
7256                                 [[ $stripe_index -ne 0 ]] &&
7257                                         stripe_index_all_zero=false
7258                         done
7259                 done
7260         done
7261         $stripe_index_all_zero &&
7262                 error "all files are being extracted starting from OST index 0"
7263         return 0
7264 }
7265
7266 have_xattrs_include() {
7267         tar --help | grep -q xattrs-include &&
7268                 echo --xattrs-include="lustre.*"
7269 }
7270
7271 test_102d() {
7272         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7273         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7274         local xinc=$(have_xattrs_include)
7275         setup_test102
7276         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $xinc
7277         cd $DIR/$tdir/$tdir
7278         compare_stripe_info1
7279 }
7280 run_test 102d "tar restore stripe info from tarfile,not keep osts ==========="
7281
7282 test_102f() {
7283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7284         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7285         local xinc=$(have_xattrs_include)
7286         setup_test102
7287         test_mkdir $DIR/$tdir.restore
7288         cd $DIR
7289         tar cf - --xattrs $tdir | tar xf - \
7290                 -C $DIR/$tdir.restore --xattrs $xinc
7291         cd $DIR/$tdir.restore/$tdir
7292         compare_stripe_info1
7293 }
7294 run_test 102f "tar copy files, not keep osts ==========="
7295
7296 grow_xattr() {
7297         local xsize=${1:-1024}  # in bytes
7298         local file=$DIR/$tfile
7299
7300         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7301                 skip "must have user_xattr" && return 0
7302         [ -z "$(which setfattr 2>/dev/null)" ] &&
7303                 skip_env "could not find setfattr" && return 0
7304         [ -z "$(which getfattr 2>/dev/null)" ] &&
7305                 skip_env "could not find getfattr" && return 0
7306
7307         touch $file
7308
7309         local value="$(generate_string $xsize)"
7310
7311         local xbig=trusted.big
7312         log "save $xbig on $file"
7313         setfattr -n $xbig -v $value $file ||
7314                 error "saving $xbig on $file failed"
7315
7316         local orig=$(get_xattr_value $xbig $file)
7317         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7318
7319         local xsml=trusted.sml
7320         log "save $xsml on $file"
7321         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7322
7323         local new=$(get_xattr_value $xbig $file)
7324         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7325
7326         log "grow $xsml on $file"
7327         setfattr -n $xsml -v "$value" $file ||
7328                 error "growing $xsml on $file failed"
7329
7330         new=$(get_xattr_value $xbig $file)
7331         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7332         log "$xbig still valid after growing $xsml"
7333
7334         rm -f $file
7335 }
7336
7337 test_102h() { # bug 15777
7338         grow_xattr 1024
7339 }
7340 run_test 102h "grow xattr from inside inode to external block"
7341
7342 test_102ha() {
7343         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7344         grow_xattr $(max_xattr_size)
7345 }
7346 run_test 102ha "grow xattr from inside inode to external inode"
7347
7348 test_102i() { # bug 17038
7349         [ -z "$(which getfattr 2>/dev/null)" ] &&
7350                 skip "could not find getfattr" && return
7351         touch $DIR/$tfile
7352         ln -s $DIR/$tfile $DIR/${tfile}link
7353         getfattr -n trusted.lov $DIR/$tfile ||
7354                 error "lgetxattr on $DIR/$tfile failed"
7355         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7356                 grep -i "no such attr" ||
7357                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7358         rm -f $DIR/$tfile $DIR/${tfile}link
7359 }
7360 run_test 102i "lgetxattr test on symbolic link ============"
7361
7362 test_102j() {
7363         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7364         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7365         local xinc=$(have_xattrs_include)
7366         setup_test102 "$RUNAS"
7367         chown $RUNAS_ID $DIR/$tdir
7368         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $xinc
7369         cd $DIR/$tdir/$tdir
7370         compare_stripe_info1 "$RUNAS"
7371 }
7372 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7373
7374 test_102k() {
7375         [ -z "$(which setfattr 2>/dev/null)" ] &&
7376                 skip "could not find setfattr" && return
7377         touch $DIR/$tfile
7378         # b22187 just check that does not crash for regular file.
7379         setfattr -n trusted.lov $DIR/$tfile
7380         # b22187 'setfattr -n trusted.lov' should work as remove LOV EA for directories
7381         local test_kdir=$DIR/$tdir
7382         test_mkdir $test_kdir
7383         local default_size=`$GETSTRIPE -S $test_kdir`
7384         local default_count=`$GETSTRIPE -c $test_kdir`
7385         local default_offset=`$GETSTRIPE -i $test_kdir`
7386         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7387                 error 'dir setstripe failed'
7388         setfattr -n trusted.lov $test_kdir
7389         local stripe_size=`$GETSTRIPE -S $test_kdir`
7390         local stripe_count=`$GETSTRIPE -c $test_kdir`
7391         local stripe_offset=`$GETSTRIPE -i $test_kdir`
7392         [ $stripe_size -eq $default_size ] ||
7393                 error "stripe size $stripe_size != $default_size"
7394         [ $stripe_count -eq $default_count ] ||
7395                 error "stripe count $stripe_count != $default_count"
7396         [ $stripe_offset -eq $default_offset ] ||
7397                 error "stripe offset $stripe_offset != $default_offset"
7398         rm -rf $DIR/$tfile $test_kdir
7399 }
7400 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7401
7402 test_102l() {
7403         [ -z "$(which getfattr 2>/dev/null)" ] &&
7404                 skip "could not find getfattr" && return
7405
7406         # LU-532 trusted. xattr is invisible to non-root
7407         local testfile=$DIR/$tfile
7408
7409         touch $testfile
7410
7411         echo "listxattr as user..."
7412         chown $RUNAS_ID $testfile
7413         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7414             grep -q "trusted" &&
7415                 error "$testfile trusted xattrs are user visible"
7416
7417         return 0;
7418 }
7419 run_test 102l "listxattr size test =================================="
7420
7421 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7422         local path=$DIR/$tfile
7423         touch $path
7424
7425         listxattr_size_check $path || error "listattr_size_check $path failed"
7426 }
7427 run_test 102m "Ensure listxattr fails on small bufffer ========"
7428
7429 cleanup_test102
7430
7431 getxattr() { # getxattr path name
7432         # Return the base64 encoding of the value of xattr name on path.
7433         local path=$1
7434         local name=$2
7435
7436         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7437         # file: $path
7438         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7439         #
7440         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7441
7442         getfattr --absolute-names --encoding=base64 --name=$name $path |
7443                 awk -F= -v name=$name '$1 == name {
7444                         print substr($0, index($0, "=") + 1);
7445         }'
7446 }
7447
7448 test_102n() { # LU-4101 mdt: protect internal xattrs
7449         [ -z "$(which setfattr 2>/dev/null)" ] &&
7450                 skip "could not find setfattr" && return
7451         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7452         then
7453                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7454                 return
7455         fi
7456
7457         local file0=$DIR/$tfile.0
7458         local file1=$DIR/$tfile.1
7459         local xattr0=$TMP/$tfile.0
7460         local xattr1=$TMP/$tfile.1
7461         local namelist="lov lma lmv link fid version som hsm"
7462         local name
7463         local value
7464
7465         rm -rf $file0 $file1 $xattr0 $xattr1
7466         touch $file0 $file1
7467
7468         # Get 'before' xattrs of $file1.
7469         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7470
7471         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7472                 namelist+=" lfsck_namespace"
7473         for name in $namelist; do
7474                 # Try to copy xattr from $file0 to $file1.
7475                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7476
7477                 setfattr --name=trusted.$name --value="$value" $file1 ||
7478                         error "setxattr 'trusted.$name' failed"
7479
7480                 # Try to set a garbage xattr.
7481                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7482
7483                 if [[ x$name == "xlov" ]]; then
7484                         setfattr --name=trusted.lov --value="$value" $file1 &&
7485                         error "setxattr invalid 'trusted.lov' success"
7486                 else
7487                         setfattr --name=trusted.$name --value="$value" $file1 ||
7488                                 error "setxattr invalid 'trusted.$name' failed"
7489                 fi
7490
7491                 # Try to remove the xattr from $file1. We don't care if this
7492                 # appears to succeed or fail, we just don't want there to be
7493                 # any changes or crashes.
7494                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7495         done
7496
7497         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7498         then
7499                 name="lfsck_ns"
7500                 # Try to copy xattr from $file0 to $file1.
7501                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7502
7503                 setfattr --name=trusted.$name --value="$value" $file1 ||
7504                         error "setxattr 'trusted.$name' failed"
7505
7506                 # Try to set a garbage xattr.
7507                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7508
7509                 setfattr --name=trusted.$name --value="$value" $file1 ||
7510                         error "setxattr 'trusted.$name' failed"
7511
7512                 # Try to remove the xattr from $file1. We don't care if this
7513                 # appears to succeed or fail, we just don't want there to be
7514                 # any changes or crashes.
7515                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7516         fi
7517
7518         # Get 'after' xattrs of file1.
7519         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7520
7521         if ! diff $xattr0 $xattr1; then
7522                 error "before and after xattrs of '$file1' differ"
7523         fi
7524
7525         rm -rf $file0 $file1 $xattr0 $xattr1
7526
7527         return 0
7528 }
7529 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7530
7531 test_102p() { # LU-4703 setxattr did not check ownership
7532         local testfile=$DIR/$tfile
7533
7534         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7535                 skip "MDS needs to be at least 2.5.56" && return
7536
7537         touch $testfile
7538
7539         echo "setfacl as user..."
7540         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7541         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7542
7543         echo "setfattr as user..."
7544         setfacl -m "u:$RUNAS_ID:---" $testfile
7545         $RUNAS setfattr -x system.posix_acl_access $testfile
7546         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7547 }
7548 run_test 102p "check setxattr(2) correctly fails without permission"
7549
7550 test_102q() {
7551         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7552                 skip "MDS needs to be at least 2.6.92" && return
7553         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7554 }
7555 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7556
7557 test_102r() {
7558         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7559                 skip "MDS needs to be at least 2.6.93" && return
7560         touch $DIR/$tfile || error "touch"
7561         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7562         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7563         rm $DIR/$tfile || error "rm"
7564
7565         #normal directory
7566         mkdir -p $DIR/$tdir || error "mkdir"
7567         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7568         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7569         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7570                 error "$testfile error deleting user.author1"
7571         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7572                 grep "user.$(basename $tdir)" &&
7573                 error "$tdir did not delete user.$(basename $tdir)"
7574         rmdir $DIR/$tdir || error "rmdir"
7575
7576         #striped directory
7577         test_mkdir $DIR/$tdir || error "make striped dir"
7578         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7579         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7580         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7581                 error "$testfile error deleting user.author1"
7582         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7583                 grep "user.$(basename $tdir)" &&
7584                 error "$tdir did not delete user.$(basename $tdir)"
7585         rmdir $DIR/$tdir || error "rm striped dir"
7586 }
7587 run_test 102r "set EAs with empty values"
7588
7589 test_102s() {
7590         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.52) ] &&
7591                 skip "MDS needs to be at least 2.11.52"
7592
7593         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
7594
7595         save_lustre_params client "llite.*.xattr_cache" > $save
7596
7597         for cache in 0 1; do
7598                 lctl set_param llite.*.xattr_cache=$cache
7599
7600                 rm -f $DIR/$tfile
7601                 touch $DIR/$tfile || error "touch"
7602                 for prefix in lustre security system trusted user; do
7603                         # Note getxattr() may fail with 'Operation not
7604                         # supported' or 'No such attribute' depending
7605                         # on prefix and cache.
7606                         getfattr -n $prefix.n102s $DIR/$tfile &&
7607                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
7608                 done
7609         done
7610
7611         restore_lustre_params < $save
7612 }
7613 run_test 102s "getting nonexistent xattrs should fail"
7614
7615 run_acl_subtest()
7616 {
7617     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7618     return $?
7619 }
7620
7621 test_103a() {
7622         [ "$UID" != 0 ] && skip_env "must run as root" && return
7623         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7624                 skip "must have acl enabled" && return
7625         [ -z "$(which setfacl 2>/dev/null)" ] &&
7626                 skip_env "could not find setfacl" && return
7627         $GSS && skip "could not run under gss" && return
7628         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7629
7630         gpasswd -a daemon bin                           # LU-5641
7631         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7632
7633         declare -a identity_old
7634
7635         for num in $(seq $MDSCOUNT); do
7636                 switch_identity $num true || identity_old[$num]=$?
7637         done
7638
7639         SAVE_UMASK=$(umask)
7640         umask 0022
7641         mkdir -p $DIR/$tdir
7642         cd $DIR/$tdir
7643
7644         echo "performing cp ..."
7645         run_acl_subtest cp || error "run_acl_subtest cp failed"
7646         echo "performing getfacl-noacl..."
7647         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7648         echo "performing misc..."
7649         run_acl_subtest misc || error  "misc test failed"
7650         echo "performing permissions..."
7651         run_acl_subtest permissions || error "permissions failed"
7652         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7653         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7654              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7655              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7656         then
7657                 echo "performing permissions xattr..."
7658                 run_acl_subtest permissions_xattr ||
7659                         error "permissions_xattr failed"
7660         fi
7661         echo "performing setfacl..."
7662         run_acl_subtest setfacl || error  "setfacl test failed"
7663
7664         # inheritance test got from HP
7665         echo "performing inheritance..."
7666         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7667         chmod +x make-tree || error "chmod +x failed"
7668         run_acl_subtest inheritance || error "inheritance test failed"
7669         rm -f make-tree
7670
7671         echo "LU-974 ignore umask when acl is enabled..."
7672         run_acl_subtest 974 || error "LU-974 umask test failed"
7673         if [ $MDSCOUNT -ge 2 ]; then
7674                 run_acl_subtest 974_remote ||
7675                         error "LU-974 umask test failed under remote dir"
7676         fi
7677
7678         echo "LU-2561 newly created file is same size as directory..."
7679         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7680                 run_acl_subtest 2561 || error "LU-2561 test failed"
7681         else
7682                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
7683         fi
7684
7685         run_acl_subtest 4924 || error "LU-4924 test failed"
7686
7687         cd $SAVE_PWD
7688         umask $SAVE_UMASK
7689
7690         for num in $(seq $MDSCOUNT); do
7691                 if [ "${identity_old[$num]}" = 1 ]; then
7692                         switch_identity $num false || identity_old[$num]=$?
7693                 fi
7694         done
7695 }
7696 run_test 103a "acl test ========================================="
7697
7698 test_103b() {
7699         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7700         local noacl=false
7701         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
7702         local mountopts=$MDS_MOUNT_OPTS
7703
7704         if [[ "$MDS_MOUNT_OPTS" =~ "noacl" ]]; then
7705                 noacl=true
7706         else
7707                 # stop the MDT
7708                 stop $SINGLEMDS || error "failed to stop MDT."
7709                 # remount the MDT
7710                 if [ -z "$MDS_MOUNT_OPTS" ]; then
7711                         MDS_MOUNT_OPTS="-o noacl"
7712                 else
7713                         MDS_MOUNT_OPTS="${MDS_MOUNT_OPTS},noacl"
7714                 fi
7715                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7716                         error "failed to start MDT."
7717                 MDS_MOUNT_OPTS=$mountopts
7718         fi
7719
7720         touch $DIR/$tfile
7721         setfacl -m u:bin:rw $DIR/$tfile && error "setfacl should fail"
7722
7723         if ! $noacl; then
7724                 # stop the MDT
7725                 stop $SINGLEMDS || error "failed to stop MDT."
7726                 # remount the MDT
7727                 start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS ||
7728                         error "failed to start MDT."
7729         fi
7730
7731         true
7732 }
7733 run_test 103b "MDS mount option 'noacl'"
7734
7735 test_103c() {
7736         mkdir -p $DIR/$tdir
7737         cp -rp $DIR/$tdir $DIR/$tdir.bak
7738
7739         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
7740                 error "$DIR/$tdir shouldn't contain default ACL"
7741         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
7742                 error "$DIR/$tdir.bak shouldn't contain default ACL"
7743         true
7744 }
7745 run_test 103c "'cp -rp' won't set empty acl"
7746
7747 test_104a() {
7748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7749         touch $DIR/$tfile
7750         lfs df || error "lfs df failed"
7751         lfs df -ih || error "lfs df -ih failed"
7752         lfs df -h $DIR || error "lfs df -h $DIR failed"
7753         lfs df -i $DIR || error "lfs df -i $DIR failed"
7754         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
7755         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
7756
7757         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
7758         lctl --device %$OSC deactivate
7759         lfs df || error "lfs df with deactivated OSC failed"
7760         lctl --device %$OSC activate
7761         # wait the osc back to normal
7762         wait_osc_import_state client ost FULL
7763
7764         lfs df || error "lfs df with reactivated OSC failed"
7765         rm -f $DIR/$tfile
7766 }
7767 run_test 104a "lfs df [-ih] [path] test ========================="
7768
7769 test_104b() {
7770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7771         [ $RUNAS_ID -eq $UID ] &&
7772                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
7773         chmod 666 /dev/obd
7774         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
7775                         grep "Permission denied" | wc -l)))
7776         if [ $denied_cnt -ne 0 ]; then
7777                 error "lfs check servers test failed"
7778         fi
7779 }
7780 run_test 104b "$RUNAS lfs check servers test ===================="
7781
7782 test_105a() {
7783         # doesn't work on 2.4 kernels
7784         touch $DIR/$tfile
7785         if $(flock_is_enabled); then
7786                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
7787         else
7788                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
7789         fi
7790         rm -f $DIR/$tfile
7791 }
7792 run_test 105a "flock when mounted without -o flock test ========"
7793
7794 test_105b() {
7795         touch $DIR/$tfile
7796         if $(flock_is_enabled); then
7797                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
7798         else
7799                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
7800         fi
7801         rm -f $DIR/$tfile
7802 }
7803 run_test 105b "fcntl when mounted without -o flock test ========"
7804
7805 test_105c() {
7806         touch $DIR/$tfile
7807         if $(flock_is_enabled); then
7808                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
7809         else
7810                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
7811         fi
7812         rm -f $DIR/$tfile
7813 }
7814 run_test 105c "lockf when mounted without -o flock test ========"
7815
7816 test_105d() { # bug 15924
7817         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7818         test_mkdir -p $DIR/$tdir
7819         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7820         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
7821         $LCTL set_param fail_loc=0x80000315
7822         flocks_test 2 $DIR/$tdir
7823 }
7824 run_test 105d "flock race (should not freeze) ========"
7825
7826 test_105e() { # bug 22660 && 22040
7827         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
7828         touch $DIR/$tfile
7829         flocks_test 3 $DIR/$tfile
7830 }
7831 run_test 105e "Two conflicting flocks from same process ======="
7832
7833 test_106() { #bug 10921
7834         test_mkdir -p $DIR/$tdir
7835         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
7836         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
7837 }
7838 run_test 106 "attempt exec of dir followed by chown of that dir"
7839
7840 test_107() {
7841         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7842         CDIR=`pwd`
7843         cd $DIR
7844
7845         local file=core
7846         rm -f $file
7847
7848         local save_pattern=$(sysctl -n kernel.core_pattern)
7849         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
7850         sysctl -w kernel.core_pattern=$file
7851         sysctl -w kernel.core_uses_pid=0
7852
7853         ulimit -c unlimited
7854         sleep 60 &
7855         SLEEPPID=$!
7856
7857         sleep 1
7858
7859         kill -s 11 $SLEEPPID
7860         wait $SLEEPPID
7861         if [ -e $file ]; then
7862                 size=`stat -c%s $file`
7863                 [ $size -eq 0 ] && error "Fail to create core file $file"
7864         else
7865                 error "Fail to create core file $file"
7866         fi
7867         rm -f $file
7868         sysctl -w kernel.core_pattern=$save_pattern
7869         sysctl -w kernel.core_uses_pid=$save_uses_pid
7870         cd $CDIR
7871 }
7872 run_test 107 "Coredump on SIG"
7873
7874 test_110() {
7875         test_mkdir -p $DIR/$tdir
7876         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255) ||
7877                 error "mkdir with 255 char failed"
7878         test_mkdir $DIR/$tdir/$(str_repeat 'b' 256) &&
7879                 error "mkdir with 256 char should fail, but did not"
7880         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
7881                 error "create with 255 char failed"
7882         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
7883                 error "create with 256 char should fail, but did not"
7884
7885         ls -l $DIR/$tdir
7886         rm -rf $DIR/$tdir
7887 }
7888 run_test 110 "filename length checking"
7889
7890 #
7891 # Purpose: To verify dynamic thread (OSS) creation.
7892 #
7893 test_115() {
7894         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7895         remote_ost_nodsh && skip "remote OST with nodsh" && return
7896
7897         # Lustre does not stop service threads once they are started.
7898         # Reset number of running threads to default.
7899         stopall
7900         setupall
7901
7902         local OSTIO_pre
7903         local save_params="$TMP/sanity-$TESTNAME.parameters"
7904
7905         # Get ll_ost_io count before I/O
7906         OSTIO_pre=$(do_facet ost1 \
7907                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
7908         # Exit if lustre is not running (ll_ost_io not running).
7909         [ -z "$OSTIO_pre" ] && error "no OSS threads"
7910
7911         echo "Starting with $OSTIO_pre threads"
7912         local thread_max=$((OSTIO_pre * 2))
7913         local rpc_in_flight=$((thread_max * 2))
7914         # Number of I/O Process proposed to be started.
7915         local nfiles
7916         local facets=$(get_facets OST)
7917
7918         save_lustre_params client \
7919                 "osc.*OST*.max_rpcs_in_flight" > $save_params
7920         save_lustre_params $facets \
7921                 "ost.OSS.ost_io.threads_max" >> $save_params
7922
7923         # Set in_flight to $rpc_in_flight
7924         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
7925                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
7926         nfiles=${rpc_in_flight}
7927         # Set ost thread_max to $thread_max
7928         do_facet ost1 \
7929                 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
7930
7931         # 5 Minutes should be sufficient for max number of OSS
7932         # threads(thread_max) to be created.
7933         local timeout=300
7934
7935         # Start I/O.
7936         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
7937         mkdir -p $DIR/$tdir
7938         for i in $(seq $nfiles); do
7939                 local file=$DIR/$tdir/${tfile}-$i
7940                 $LFS setstripe -c -1 -i 0 $file
7941                 ($WTL $file $timeout)&
7942         done
7943
7944         # I/O Started - Wait for thread_started to reach thread_max or report
7945         # error if thread_started is more than thread_max.
7946         echo "Waiting for thread_started to reach thread_max"
7947         local thread_started=0
7948         local end_time=$((SECONDS + timeout))
7949
7950         while [ $SECONDS -le $end_time ] ; do
7951                 echo -n "."
7952                 # Get ost i/o thread_started count.
7953                 thread_started=$(do_facet ost1 \
7954                         "$LCTL get_param \
7955                         ost.OSS.ost_io.threads_started | cut -d= -f2")
7956                 # Break out if thread_started is equal/greater than thread_max
7957                 if [[ $thread_started -ge $thread_max ]]; then
7958                         echo ll_ost_io thread_started $thread_started, \
7959                                 equal/greater than thread_max $thread_max
7960                         break
7961                 fi
7962                 sleep 1
7963         done
7964
7965         # Cleanup - We have the numbers, Kill i/o jobs if running.
7966         jobcount=($(jobs -p))
7967         for i in $(seq 0 $((${#jobcount[@]}-1)))
7968         do
7969                 kill -9 ${jobcount[$i]}
7970                 if [ $? -ne 0 ] ; then
7971                         echo Warning: \
7972                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
7973                 fi
7974         done
7975
7976         # Cleanup files left by WTL binary.
7977         for i in $(seq $nfiles); do
7978                 local file=$DIR/$tdir/${tfile}-$i
7979                 rm -rf $file
7980                 if [ $? -ne 0 ] ; then
7981                         echo "Warning: Failed to delete file $file"
7982                 fi
7983         done
7984
7985         restore_lustre_params <$save_params
7986         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
7987
7988         # Error out if no new thread has started or Thread started is greater
7989         # than thread max.
7990         if [[ $thread_started -le $OSTIO_pre ||
7991                         $thread_started -gt $thread_max ]]; then
7992                 error "ll_ost_io: thread_started $thread_started" \
7993                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
7994                       "No new thread started or thread started greater " \
7995                       "than thread_max."
7996         fi
7997 }
7998 run_test 115 "verify dynamic thread creation===================="
7999
8000 free_min_max () {
8001         wait_delete_completed
8002         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8003         echo "OST kbytes available: ${AVAIL[@]}"
8004         MAXV=${AVAIL[0]}
8005         MAXI=0
8006         MINV=${AVAIL[0]}
8007         MINI=0
8008         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8009                 #echo OST $i: ${AVAIL[i]}kb
8010                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8011                         MAXV=${AVAIL[i]}
8012                         MAXI=$i
8013                 fi
8014                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8015                         MINV=${AVAIL[i]}
8016                         MINI=$i
8017                 fi
8018         done
8019         echo "Min free space: OST $MINI: $MINV"
8020         echo "Max free space: OST $MAXI: $MAXV"
8021 }
8022
8023 test_116a() { # was previously test_116()
8024         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8025         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8026
8027         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8028
8029         echo -n "Free space priority "
8030         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
8031                 head -n1
8032         declare -a AVAIL
8033         free_min_max
8034
8035         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
8036         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
8037                 && return
8038         trap simple_cleanup_common EXIT
8039
8040
8041         # Check if we need to generate uneven OSTs
8042         test_mkdir -p $DIR/$tdir/OST${MINI}
8043         local FILL=$((MINV / 4))
8044         local DIFF=$((MAXV - MINV))
8045         local DIFF2=$((DIFF * 100 / MINV))
8046
8047         local threshold=$(do_facet $SINGLEMDS \
8048                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
8049         threshold=${threshold%%%}
8050         echo -n "Check for uneven OSTs: "
8051         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
8052
8053         if [[ $DIFF2 -gt $threshold ]]; then
8054                 echo "ok"
8055                 echo "Don't need to fill OST$MINI"
8056         else
8057                 # generate uneven OSTs. Write 2% over the QOS threshold value
8058                 echo "no"
8059                 DIFF=$((threshold - DIFF2 + 2))
8060                 DIFF2=$((MINV * DIFF / 100))
8061                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
8062                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
8063                         error "setstripe failed"
8064                 DIFF=$((DIFF2 / 2048))
8065                 i=0
8066                 while [ $i -lt $DIFF ]; do
8067                         i=$((i + 1))
8068                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
8069                                 bs=2M count=1 2>/dev/null
8070                         echo -n .
8071                 done
8072                 echo .
8073                 sync
8074                 sleep_maxage
8075                 free_min_max
8076         fi
8077
8078         DIFF=$((MAXV - MINV))
8079         DIFF2=$((DIFF * 100 / MINV))
8080         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8081         if [ $DIFF2 -gt $threshold ]; then
8082                 echo "ok"
8083         else
8084                 echo "failed - QOS mode won't be used"
8085                 skip "QOS imbalance criteria not met"
8086                 simple_cleanup_common
8087                 return
8088         fi
8089
8090         MINI1=$MINI
8091         MINV1=$MINV
8092         MAXI1=$MAXI
8093         MAXV1=$MAXV
8094
8095         # now fill using QOS
8096         $SETSTRIPE -c 1 $DIR/$tdir
8097         FILL=$((FILL / 200))
8098         if [ $FILL -gt 600 ]; then
8099                 FILL=600
8100         fi
8101         echo "writing $FILL files to QOS-assigned OSTs"
8102         i=0
8103         while [ $i -lt $FILL ]; do
8104                 i=$((i + 1))
8105                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8106                         count=1 2>/dev/null
8107                 echo -n .
8108         done
8109         echo "wrote $i 200k files"
8110         sync
8111         sleep_maxage
8112
8113         echo "Note: free space may not be updated, so measurements might be off"
8114         free_min_max
8115         DIFF2=$((MAXV - MINV))
8116         echo "free space delta: orig $DIFF final $DIFF2"
8117         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8118         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8119         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8120         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8121         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8122         if [[ $DIFF -gt 0 ]]; then
8123                 FILL=$((DIFF2 * 100 / DIFF - 100))
8124                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8125         fi
8126
8127         # Figure out which files were written where
8128         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8129                awk '/'$MINI1': / {print $2; exit}')
8130         echo $UUID
8131         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8132         echo "$MINC files created on smaller OST $MINI1"
8133         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8134                awk '/'$MAXI1': / {print $2; exit}')
8135         echo $UUID
8136         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8137         echo "$MAXC files created on larger OST $MAXI1"
8138         if [[ $MINC -gt 0 ]]; then
8139                 FILL=$((MAXC * 100 / MINC - 100))
8140                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8141         fi
8142         [[ $MAXC -gt $MINC ]] ||
8143                 error_ignore LU-9 "stripe QOS didn't balance free space"
8144         simple_cleanup_common
8145 }
8146 run_test 116a "stripe QOS: free space balance ==================="
8147
8148 test_116b() { # LU-2093
8149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8150         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8151
8152 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8153         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8154                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8155         [ -z "$old_rr" ] && skip "no QOS" && return 0
8156         do_facet $SINGLEMDS lctl set_param \
8157                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8158         mkdir -p $DIR/$tdir
8159         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8160         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8161         do_facet $SINGLEMDS lctl set_param fail_loc=0
8162         rm -rf $DIR/$tdir
8163         do_facet $SINGLEMDS lctl set_param \
8164                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8165 }
8166 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8167
8168 test_117() # bug 10891
8169 {
8170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8171         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8172         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8173         lctl set_param fail_loc=0x21e
8174         > $DIR/$tfile || error "truncate failed"
8175         lctl set_param fail_loc=0
8176         echo "Truncate succeeded."
8177         rm -f $DIR/$tfile
8178 }
8179 run_test 117 "verify osd extend =========="
8180
8181 NO_SLOW_RESENDCOUNT=4
8182 export OLD_RESENDCOUNT=""
8183 set_resend_count () {
8184         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8185         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8186         lctl set_param -n $PROC_RESENDCOUNT $1
8187         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8188 }
8189
8190 # for reduce test_118* time (b=14842)
8191 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8192
8193 # Reset async IO behavior after error case
8194 reset_async() {
8195         FILE=$DIR/reset_async
8196
8197         # Ensure all OSCs are cleared
8198         $SETSTRIPE -c -1 $FILE
8199         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8200         sync
8201         rm $FILE
8202 }
8203
8204 test_118a() #bug 11710
8205 {
8206         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8207         reset_async
8208
8209         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8210         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8211         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8212
8213         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8214                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8215                 return 1;
8216         fi
8217         rm -f $DIR/$tfile
8218 }
8219 run_test 118a "verify O_SYNC works =========="
8220
8221 test_118b()
8222 {
8223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8224         remote_ost_nodsh && skip "remote OST with nodsh" && return
8225
8226         reset_async
8227
8228         #define OBD_FAIL_SRV_ENOENT 0x217
8229         set_nodes_failloc "$(osts_nodes)" 0x217
8230         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8231         RC=$?
8232         set_nodes_failloc "$(osts_nodes)" 0
8233         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8234         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8235                     grep -c writeback)
8236
8237         if [[ $RC -eq 0 ]]; then
8238                 error "Must return error due to dropped pages, rc=$RC"
8239                 return 1;
8240         fi
8241
8242         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8243                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8244                 return 1;
8245         fi
8246
8247         echo "Dirty pages not leaked on ENOENT"
8248
8249         # Due to the above error the OSC will issue all RPCs syncronously
8250         # until a subsequent RPC completes successfully without error.
8251         $MULTIOP $DIR/$tfile Ow4096yc
8252         rm -f $DIR/$tfile
8253
8254         return 0
8255 }
8256 run_test 118b "Reclaim dirty pages on fatal error =========="
8257
8258 test_118c()
8259 {
8260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8261
8262         # for 118c, restore the original resend count, LU-1940
8263         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
8264                                 set_resend_count $OLD_RESENDCOUNT
8265         remote_ost_nodsh && skip "remote OST with nodsh" && return
8266
8267         reset_async
8268
8269         #define OBD_FAIL_OST_EROFS               0x216
8270         set_nodes_failloc "$(osts_nodes)" 0x216
8271
8272         # multiop should block due to fsync until pages are written
8273         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8274         MULTIPID=$!
8275         sleep 1
8276
8277         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8278                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8279         fi
8280
8281         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8282                     grep -c writeback)
8283         if [[ $WRITEBACK -eq 0 ]]; then
8284                 error "No page in writeback, writeback=$WRITEBACK"
8285         fi
8286
8287         set_nodes_failloc "$(osts_nodes)" 0
8288         wait $MULTIPID
8289         RC=$?
8290         if [[ $RC -ne 0 ]]; then
8291                 error "Multiop fsync failed, rc=$RC"
8292         fi
8293
8294         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8295         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8296                     grep -c writeback)
8297         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8298                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8299         fi
8300
8301         rm -f $DIR/$tfile
8302         echo "Dirty pages flushed via fsync on EROFS"
8303         return 0
8304 }
8305 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
8306
8307 # continue to use small resend count to reduce test_118* time (b=14842)
8308 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8309
8310 test_118d()
8311 {
8312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8313         remote_ost_nodsh && skip "remote OST with nodsh" && return
8314
8315         reset_async
8316
8317         #define OBD_FAIL_OST_BRW_PAUSE_BULK
8318         set_nodes_failloc "$(osts_nodes)" 0x214
8319         # multiop should block due to fsync until pages are written
8320         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8321         MULTIPID=$!
8322         sleep 1
8323
8324         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8325                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8326         fi
8327
8328         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8329                     grep -c writeback)
8330         if [[ $WRITEBACK -eq 0 ]]; then
8331                 error "No page in writeback, writeback=$WRITEBACK"
8332         fi
8333
8334         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
8335         set_nodes_failloc "$(osts_nodes)" 0
8336
8337         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8338         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8339                     grep -c writeback)
8340         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8341                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8342         fi
8343
8344         rm -f $DIR/$tfile
8345         echo "Dirty pages gaurenteed flushed via fsync"
8346         return 0
8347 }
8348 run_test 118d "Fsync validation inject a delay of the bulk =========="
8349
8350 test_118f() {
8351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8352         reset_async
8353
8354         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
8355         lctl set_param fail_loc=0x8000040a
8356
8357         # Should simulate EINVAL error which is fatal
8358         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8359         RC=$?
8360         if [[ $RC -eq 0 ]]; then
8361                 error "Must return error due to dropped pages, rc=$RC"
8362         fi
8363
8364         lctl set_param fail_loc=0x0
8365
8366         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8367         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8368         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8369                     grep -c writeback)
8370         if [[ $LOCKED -ne 0 ]]; then
8371                 error "Locked pages remain in cache, locked=$LOCKED"
8372         fi
8373
8374         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8375                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8376         fi
8377
8378         rm -f $DIR/$tfile
8379         echo "No pages locked after fsync"
8380
8381         reset_async
8382         return 0
8383 }
8384 run_test 118f "Simulate unrecoverable OSC side error =========="
8385
8386 test_118g() {
8387         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8388         reset_async
8389
8390         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8391         lctl set_param fail_loc=0x406
8392
8393         # simulate local -ENOMEM
8394         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8395         RC=$?
8396
8397         lctl set_param fail_loc=0
8398         if [[ $RC -eq 0 ]]; then
8399                 error "Must return error due to dropped pages, rc=$RC"
8400         fi
8401
8402         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8403         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8404         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8405                         grep -c writeback)
8406         if [[ $LOCKED -ne 0 ]]; then
8407                 error "Locked pages remain in cache, locked=$LOCKED"
8408         fi
8409
8410         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8411                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8412         fi
8413
8414         rm -f $DIR/$tfile
8415         echo "No pages locked after fsync"
8416
8417         reset_async
8418         return 0
8419 }
8420 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8421
8422 test_118h() {
8423         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8424         remote_ost_nodsh && skip "remote OST with nodsh" && return
8425
8426         reset_async
8427
8428         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8429         set_nodes_failloc "$(osts_nodes)" 0x20e
8430         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8431         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8432         RC=$?
8433
8434         set_nodes_failloc "$(osts_nodes)" 0
8435         if [[ $RC -eq 0 ]]; then
8436                 error "Must return error due to dropped pages, rc=$RC"
8437         fi
8438
8439         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8440         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8441         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8442                     grep -c writeback)
8443         if [[ $LOCKED -ne 0 ]]; then
8444                 error "Locked pages remain in cache, locked=$LOCKED"
8445         fi
8446
8447         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8448                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8449         fi
8450
8451         rm -f $DIR/$tfile
8452         echo "No pages locked after fsync"
8453
8454         return 0
8455 }
8456 run_test 118h "Verify timeout in handling recoverables errors  =========="
8457
8458 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8459
8460 test_118i() {
8461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8462         remote_ost_nodsh && skip "remote OST with nodsh" && return
8463
8464         reset_async
8465
8466         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8467         set_nodes_failloc "$(osts_nodes)" 0x20e
8468
8469         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8470         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8471         PID=$!
8472         sleep 5
8473         set_nodes_failloc "$(osts_nodes)" 0
8474
8475         wait $PID
8476         RC=$?
8477         if [[ $RC -ne 0 ]]; then
8478                 error "got error, but should be not, rc=$RC"
8479         fi
8480
8481         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8482         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8483         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8484         if [[ $LOCKED -ne 0 ]]; then
8485                 error "Locked pages remain in cache, locked=$LOCKED"
8486         fi
8487
8488         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8489                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8490         fi
8491
8492         rm -f $DIR/$tfile
8493         echo "No pages locked after fsync"
8494
8495         return 0
8496 }
8497 run_test 118i "Fix error before timeout in recoverable error  =========="
8498
8499 [ "$SLOW" = "no" ] && set_resend_count 4
8500
8501 test_118j() {
8502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8503         remote_ost_nodsh && skip "remote OST with nodsh" && return
8504
8505         reset_async
8506
8507         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8508         set_nodes_failloc "$(osts_nodes)" 0x220
8509
8510         # return -EIO from OST
8511         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8512         RC=$?
8513         set_nodes_failloc "$(osts_nodes)" 0x0
8514         if [[ $RC -eq 0 ]]; then
8515                 error "Must return error due to dropped pages, rc=$RC"
8516         fi
8517
8518         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8519         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8520         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8521         if [[ $LOCKED -ne 0 ]]; then
8522                 error "Locked pages remain in cache, locked=$LOCKED"
8523         fi
8524
8525         # in recoverable error on OST we want resend and stay until it finished
8526         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8527                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8528         fi
8529
8530         rm -f $DIR/$tfile
8531         echo "No pages locked after fsync"
8532
8533         return 0
8534 }
8535 run_test 118j "Simulate unrecoverable OST side error =========="
8536
8537 test_118k()
8538 {
8539         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8540         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8541
8542         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8543         set_nodes_failloc "$(osts_nodes)" 0x20e
8544         test_mkdir -p $DIR/$tdir
8545
8546         for ((i=0;i<10;i++)); do
8547                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8548                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8549                 SLEEPPID=$!
8550                 sleep 0.500s
8551                 kill $SLEEPPID
8552                 wait $SLEEPPID
8553         done
8554
8555         set_nodes_failloc "$(osts_nodes)" 0
8556         rm -rf $DIR/$tdir
8557 }
8558 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8559
8560 test_118l()
8561 {
8562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8563         # LU-646
8564         test_mkdir -p $DIR/$tdir
8565         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8566         rm -rf $DIR/$tdir
8567 }
8568 run_test 118l "fsync dir ========="
8569
8570 test_118m() # LU-3066
8571 {
8572         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8573         test_mkdir -p $DIR/$tdir
8574         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8575         rm -rf $DIR/$tdir
8576 }
8577 run_test 118m "fdatasync dir ========="
8578
8579 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8580
8581 test_119a() # bug 11737
8582 {
8583         BSIZE=$((512 * 1024))
8584         directio write $DIR/$tfile 0 1 $BSIZE
8585         # We ask to read two blocks, which is more than a file size.
8586         # directio will indicate an error when requested and actual
8587         # sizes aren't equeal (a normal situation in this case) and
8588         # print actual read amount.
8589         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8590         if [ "$NOB" != "$BSIZE" ]; then
8591                 error "read $NOB bytes instead of $BSIZE"
8592         fi
8593         rm -f $DIR/$tfile
8594 }
8595 run_test 119a "Short directIO read must return actual read amount"
8596
8597 test_119b() # bug 11737
8598 {
8599         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8600
8601         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8602         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8603         sync
8604         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8605                 error "direct read failed"
8606         rm -f $DIR/$tfile
8607 }
8608 run_test 119b "Sparse directIO read must return actual read amount"
8609
8610 test_119c() # bug 13099
8611 {
8612         BSIZE=1048576
8613         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8614         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8615         rm -f $DIR/$tfile
8616 }
8617 run_test 119c "Testing for direct read hitting hole"
8618
8619 test_119d() # bug 15950
8620 {
8621         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8622         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8623         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8624         BSIZE=1048576
8625         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8626         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8627         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8628         lctl set_param fail_loc=0x40d
8629         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8630         pid_dio=$!
8631         sleep 1
8632         cat $DIR/$tfile > /dev/null &
8633         lctl set_param fail_loc=0
8634         pid_reads=$!
8635         wait $pid_dio
8636         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8637         sleep 2
8638         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8639         error "the read rpcs have not completed in 2s"
8640         rm -f $DIR/$tfile
8641         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8642 }
8643 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8644
8645 test_120a() {
8646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8647         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8648         test_mkdir -p $DIR/$tdir
8649         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8650                skip "no early lock cancel on server" && return 0
8651
8652         lru_resize_disable mdc
8653         lru_resize_disable osc
8654         cancel_lru_locks mdc
8655         # asynchronous object destroy at MDT could cause bl ast to client
8656         cancel_lru_locks osc
8657
8658         stat $DIR/$tdir > /dev/null
8659         can1=$(do_facet $SINGLEMDS \
8660                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8661                awk '/ldlm_cancel/ {print $2}')
8662         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8663                awk '/ldlm_bl_callback/ {print $2}')
8664         test_mkdir -c1 $DIR/$tdir/d1
8665         can2=$(do_facet $SINGLEMDS \
8666                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8667                awk '/ldlm_cancel/ {print $2}')
8668         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8669                awk '/ldlm_bl_callback/ {print $2}')
8670         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8671         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8672         lru_resize_enable mdc
8673         lru_resize_enable osc
8674 }
8675 run_test 120a "Early Lock Cancel: mkdir test"
8676
8677 test_120b() {
8678         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8679         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8680         test_mkdir $DIR/$tdir
8681         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8682                skip "no early lock cancel on server" && return 0
8683         lru_resize_disable mdc
8684         lru_resize_disable osc
8685         cancel_lru_locks mdc
8686         stat $DIR/$tdir > /dev/null
8687         can1=$(do_facet $SINGLEMDS \
8688                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8689                awk '/ldlm_cancel/ {print $2}')
8690         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8691                awk '/ldlm_bl_callback/ {print $2}')
8692         touch $DIR/$tdir/f1
8693         can2=$(do_facet $SINGLEMDS \
8694                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8695                awk '/ldlm_cancel/ {print $2}')
8696         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8697                awk '/ldlm_bl_callback/ {print $2}')
8698         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8699         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8700         lru_resize_enable mdc
8701         lru_resize_enable osc
8702 }
8703 run_test 120b "Early Lock Cancel: create test"
8704
8705 test_120c() {
8706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8707         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8708         test_mkdir -c1 $DIR/$tdir
8709         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8710                skip "no early lock cancel on server" && return 0
8711         lru_resize_disable mdc
8712         lru_resize_disable osc
8713         test_mkdir -p -c1 $DIR/$tdir/d1
8714         test_mkdir -p -c1 $DIR/$tdir/d2
8715         touch $DIR/$tdir/d1/f1
8716         cancel_lru_locks mdc
8717         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
8718         can1=$(do_facet $SINGLEMDS \
8719                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8720                awk '/ldlm_cancel/ {print $2}')
8721         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8722                awk '/ldlm_bl_callback/ {print $2}')
8723         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8724         can2=$(do_facet $SINGLEMDS \
8725                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8726                awk '/ldlm_cancel/ {print $2}')
8727         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8728                awk '/ldlm_bl_callback/ {print $2}')
8729         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8730         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8731         lru_resize_enable mdc
8732         lru_resize_enable osc
8733 }
8734 run_test 120c "Early Lock Cancel: link test"
8735
8736 test_120d() {
8737         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8738         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8739         test_mkdir -p -c1 $DIR/$tdir
8740         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel)" ] && \
8741                skip "no early lock cancel on server" && return 0
8742         lru_resize_disable mdc
8743         lru_resize_disable osc
8744         touch $DIR/$tdir
8745         cancel_lru_locks mdc
8746         stat $DIR/$tdir > /dev/null
8747         can1=$(do_facet $SINGLEMDS \
8748                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8749                awk '/ldlm_cancel/ {print $2}')
8750         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8751                awk '/ldlm_bl_callback/ {print $2}')
8752         chmod a+x $DIR/$tdir
8753         can2=$(do_facet $SINGLEMDS \
8754                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8755                awk '/ldlm_cancel/ {print $2}')
8756         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8757                awk '/ldlm_bl_callback/ {print $2}')
8758         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8759         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8760         lru_resize_enable mdc
8761         lru_resize_enable osc
8762 }
8763 run_test 120d "Early Lock Cancel: setattr test"
8764
8765 test_120e() {
8766         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8767         ! $($LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_can) &&
8768                 skip "no early lock cancel on server" && return 0
8769         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8770         local dlmtrace_set=false
8771
8772         test_mkdir -p -c1 $DIR/$tdir
8773         lru_resize_disable mdc
8774         lru_resize_disable osc
8775         ! $LCTL get_param debug | grep -q dlmtrace &&
8776                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
8777         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
8778         cancel_lru_locks mdc
8779         cancel_lru_locks osc
8780         dd if=$DIR/$tdir/f1 of=/dev/null
8781         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
8782         # XXX client can not do early lock cancel of OST lock
8783         # during unlink (LU-4206), so cancel osc lock now.
8784         sleep 2
8785         cancel_lru_locks osc
8786         can1=$(do_facet $SINGLEMDS \
8787                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8788                awk '/ldlm_cancel/ {print $2}')
8789         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8790                awk '/ldlm_bl_callback/ {print $2}')
8791         unlink $DIR/$tdir/f1
8792         sleep 5
8793         can2=$(do_facet $SINGLEMDS \
8794                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8795                awk '/ldlm_cancel/ {print $2}')
8796         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8797                awk '/ldlm_bl_callback/ {print $2}')
8798         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
8799                 $LCTL dk $TMP/cancel.debug.txt
8800         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
8801                 $LCTL dk $TMP/blocking.debug.txt
8802         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
8803         lru_resize_enable mdc
8804         lru_resize_enable osc
8805 }
8806 run_test 120e "Early Lock Cancel: unlink test"
8807
8808 test_120f() {
8809         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8810         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8811                skip "no early lock cancel on server" && return 0
8812         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8813         test_mkdir -p -c1 $DIR/$tdir
8814         lru_resize_disable mdc
8815         lru_resize_disable osc
8816         test_mkdir -p -c1 $DIR/$tdir/d1
8817         test_mkdir -p -c1 $DIR/$tdir/d2
8818         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
8819         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
8820         cancel_lru_locks mdc
8821         cancel_lru_locks osc
8822         dd if=$DIR/$tdir/d1/f1 of=/dev/null
8823         dd if=$DIR/$tdir/d2/f2 of=/dev/null
8824         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
8825         # XXX client can not do early lock cancel of OST lock
8826         # during rename (LU-4206), so cancel osc lock now.
8827         sleep 2
8828         cancel_lru_locks osc
8829         can1=$(do_facet $SINGLEMDS \
8830                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8831                awk '/ldlm_cancel/ {print $2}')
8832         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8833                awk '/ldlm_bl_callback/ {print $2}')
8834         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
8835         sleep 5
8836         can2=$(do_facet $SINGLEMDS \
8837                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8838                awk '/ldlm_cancel/ {print $2}')
8839         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8840                awk '/ldlm_bl_callback/ {print $2}')
8841         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8842         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8843         lru_resize_enable mdc
8844         lru_resize_enable osc
8845 }
8846 run_test 120f "Early Lock Cancel: rename test"
8847
8848 test_120g() {
8849         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8850         [ -z "`lctl get_param -n mdc.*.connect_flags | grep early_lock_cancel`" ] && \
8851                skip "no early lock cancel on server" && return 0
8852         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8853         lru_resize_disable mdc
8854         lru_resize_disable osc
8855         count=10000
8856         echo create $count files
8857         test_mkdir -p $DIR/$tdir
8858         cancel_lru_locks mdc
8859         cancel_lru_locks osc
8860         t0=`date +%s`
8861
8862         can0=$(do_facet $SINGLEMDS \
8863                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8864                awk '/ldlm_cancel/ {print $2}')
8865         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8866                awk '/ldlm_bl_callback/ {print $2}')
8867         createmany -o $DIR/$tdir/f $count
8868         sync
8869         can1=$(do_facet $SINGLEMDS \
8870                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8871                awk '/ldlm_cancel/ {print $2}')
8872         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8873                awk '/ldlm_bl_callback/ {print $2}')
8874         t1=$(date +%s)
8875         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
8876         echo rm $count files
8877         rm -r $DIR/$tdir
8878         sync
8879         can2=$(do_facet $SINGLEMDS \
8880                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8881                awk '/ldlm_cancel/ {print $2}')
8882         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8883                awk '/ldlm_bl_callback/ {print $2}')
8884         t2=$(date +%s)
8885         echo total: $count removes in $((t2-t1))
8886         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
8887         sleep 2
8888         # wait for commitment of removal
8889         lru_resize_enable mdc
8890         lru_resize_enable osc
8891 }
8892 run_test 120g "Early Lock Cancel: performance test"
8893
8894 test_121() { #bug #10589
8895         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8896         rm -rf $DIR/$tfile
8897         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
8898 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
8899         lctl set_param fail_loc=0x310
8900         cancel_lru_locks osc > /dev/null
8901         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
8902         lctl set_param fail_loc=0
8903         [[ $reads -eq $writes ]] ||
8904                 error "read $reads blocks, must be $writes blocks"
8905 }
8906 run_test 121 "read cancel race ========="
8907
8908 test_123a() { # was test 123, statahead(bug 11401)
8909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8910         SLOWOK=0
8911         if [ -z "$(grep "processor.*: 1" /proc/cpuinfo)" ]; then
8912             log "testing on UP system. Performance may be not as good as expected."
8913                         SLOWOK=1
8914         fi
8915
8916         rm -rf $DIR/$tdir
8917         test_mkdir -p $DIR/$tdir
8918         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
8919         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
8920         MULT=10
8921         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
8922                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
8923
8924                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8925                 lctl set_param -n llite.*.statahead_max 0
8926                 lctl get_param llite.*.statahead_max
8927                 cancel_lru_locks mdc
8928                 cancel_lru_locks osc
8929                 stime=`date +%s`
8930                 time ls -l $DIR/$tdir | wc -l
8931                 etime=`date +%s`
8932                 delta=$((etime - stime))
8933                 log "ls $i files without statahead: $delta sec"
8934                 lctl set_param llite.*.statahead_max=$max
8935
8936                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8937                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
8938                 cancel_lru_locks mdc
8939                 cancel_lru_locks osc
8940                 stime=`date +%s`
8941                 time ls -l $DIR/$tdir | wc -l
8942                 etime=`date +%s`
8943                 delta_sa=$((etime - stime))
8944                 log "ls $i files with statahead: $delta_sa sec"
8945                 lctl get_param -n llite.*.statahead_stats
8946                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
8947
8948                 [[ $swrong -lt $ewrong ]] &&
8949                         log "statahead was stopped, maybe too many locks held!"
8950                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
8951
8952                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8953                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
8954                     lctl set_param -n llite.*.statahead_max 0
8955                     lctl get_param llite.*.statahead_max
8956                     cancel_lru_locks mdc
8957                     cancel_lru_locks osc
8958                     stime=`date +%s`
8959                     time ls -l $DIR/$tdir | wc -l
8960                     etime=`date +%s`
8961                     delta=$((etime - stime))
8962                     log "ls $i files again without statahead: $delta sec"
8963                     lctl set_param llite.*.statahead_max=$max
8964                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
8965                         if [  $SLOWOK -eq 0 ]; then
8966                                 error "ls $i files is slower with statahead!"
8967                         else
8968                                 log "ls $i files is slower with statahead!"
8969                         fi
8970                         break
8971                     fi
8972                 fi
8973
8974                 [ $delta -gt 20 ] && break
8975                 [ $delta -gt 8 ] && MULT=$((50 / delta))
8976                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
8977         done
8978         log "ls done"
8979
8980         stime=`date +%s`
8981         rm -r $DIR/$tdir
8982         sync
8983         etime=`date +%s`
8984         delta=$((etime - stime))
8985         log "rm -r $DIR/$tdir/: $delta seconds"
8986         log "rm done"
8987         lctl get_param -n llite.*.statahead_stats
8988 }
8989 run_test 123a "verify statahead work"
8990
8991 test_123b () { # statahead(bug 15027)
8992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8993         test_mkdir -p $DIR/$tdir
8994         createmany -o $DIR/$tdir/$tfile-%d 1000
8995
8996         cancel_lru_locks mdc
8997         cancel_lru_locks osc
8998
8999 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
9000         lctl set_param fail_loc=0x80000803
9001         ls -lR $DIR/$tdir > /dev/null
9002         log "ls done"
9003         lctl set_param fail_loc=0x0
9004         lctl get_param -n llite.*.statahead_stats
9005         rm -r $DIR/$tdir
9006         sync
9007
9008 }
9009 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
9010
9011 test_124a() {
9012         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9013         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
9014                 skip "no lru resize on server" && return 0
9015         local NR=2000
9016         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9017
9018         log "create $NR files at $DIR/$tdir"
9019         createmany -o $DIR/$tdir/f $NR ||
9020                 error "failed to create $NR files in $DIR/$tdir"
9021
9022         cancel_lru_locks mdc
9023         ls -l $DIR/$tdir > /dev/null
9024
9025         local NSDIR=""
9026         local LRU_SIZE=0
9027         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
9028                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
9029                 LRU_SIZE=$($LCTL get_param -n $PARAM)
9030                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
9031                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
9032                         log "NSDIR=$NSDIR"
9033                         log "NS=$(basename $NSDIR)"
9034                         break
9035                 fi
9036         done
9037
9038         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
9039                 skip "Not enough cached locks created!"
9040                 return 0
9041         fi
9042         log "LRU=$LRU_SIZE"
9043
9044         local SLEEP=30
9045
9046         # We know that lru resize allows one client to hold $LIMIT locks
9047         # for 10h. After that locks begin to be killed by client.
9048         local MAX_HRS=10
9049         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
9050         log "LIMIT=$LIMIT"
9051         if [ $LIMIT -lt $LRU_SIZE ]; then
9052             skip "Limit is too small $LIMIT"
9053             return 0
9054         fi
9055
9056         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
9057         # killing locks. Some time was spent for creating locks. This means
9058         # that up to the moment of sleep finish we must have killed some of
9059         # them (10-100 locks). This depends on how fast ther were created.
9060         # Many of them were touched in almost the same moment and thus will
9061         # be killed in groups.
9062         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
9063
9064         # Use $LRU_SIZE_B here to take into account real number of locks
9065         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
9066         local LRU_SIZE_B=$LRU_SIZE
9067         log "LVF=$LVF"
9068         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
9069         log "OLD_LVF=$OLD_LVF"
9070         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
9071
9072         # Let's make sure that we really have some margin. Client checks
9073         # cached locks every 10 sec.
9074         SLEEP=$((SLEEP+20))
9075         log "Sleep ${SLEEP} sec"
9076         local SEC=0
9077         while ((SEC<$SLEEP)); do
9078                 echo -n "..."
9079                 sleep 5
9080                 SEC=$((SEC+5))
9081                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9082                 echo -n "$LRU_SIZE"
9083         done
9084         echo ""
9085         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9086         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9087
9088         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9089                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9090                 unlinkmany $DIR/$tdir/f $NR
9091                 return
9092         }
9093
9094         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9095         log "unlink $NR files at $DIR/$tdir"
9096         unlinkmany $DIR/$tdir/f $NR
9097 }
9098 run_test 124a "lru resize ======================================="
9099
9100 get_max_pool_limit()
9101 {
9102         local limit=$($LCTL get_param \
9103                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9104         local max=0
9105         for l in $limit; do
9106                 if [[ $l -gt $max ]]; then
9107                         max=$l
9108                 fi
9109         done
9110         echo $max
9111 }
9112
9113 test_124b() {
9114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9115         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
9116                 skip "no lru resize on server" && return 0
9117
9118         LIMIT=$(get_max_pool_limit)
9119
9120         NR=$(($(default_lru_size)*20))
9121         if [[ $NR -gt $LIMIT ]]; then
9122                 log "Limit lock number by $LIMIT locks"
9123                 NR=$LIMIT
9124         fi
9125
9126         IFree=$(mdsrate_inodes_available)
9127         if [ $IFree -lt $NR ]; then
9128                 log "Limit lock number by $IFree inodes"
9129                 NR=$IFree
9130         fi
9131
9132         lru_resize_disable mdc
9133         test_mkdir -p $DIR/$tdir/disable_lru_resize ||
9134                 error "failed to create $DIR/$tdir/disable_lru_resize"
9135
9136         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9137         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9138         cancel_lru_locks mdc
9139         stime=`date +%s`
9140         PID=""
9141         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9142         PID="$PID $!"
9143         sleep 2
9144         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9145         PID="$PID $!"
9146         sleep 2
9147         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9148         PID="$PID $!"
9149         wait $PID
9150         etime=`date +%s`
9151         nolruresize_delta=$((etime-stime))
9152         log "ls -la time: $nolruresize_delta seconds"
9153         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9154         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9155
9156         lru_resize_enable mdc
9157         test_mkdir -p $DIR/$tdir/enable_lru_resize ||
9158                 error "failed to create $DIR/$tdir/enable_lru_resize"
9159
9160         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9161         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9162         cancel_lru_locks mdc
9163         stime=`date +%s`
9164         PID=""
9165         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9166         PID="$PID $!"
9167         sleep 2
9168         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9169         PID="$PID $!"
9170         sleep 2
9171         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9172         PID="$PID $!"
9173         wait $PID
9174         etime=`date +%s`
9175         lruresize_delta=$((etime-stime))
9176         log "ls -la time: $lruresize_delta seconds"
9177         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9178
9179         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9180                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9181         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9182                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9183         else
9184                 log "lru resize performs the same with no lru resize"
9185         fi
9186         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9187 }
9188 run_test 124b "lru resize (performance test) ======================="
9189
9190 test_124c() {
9191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9192         [ -z "$($LCTL get_param -n mdc.*.connect_flags | grep lru_resize)" ] &&
9193                 skip "no lru resize on server" && return 0
9194
9195         # cache ununsed locks on client
9196         local nr=100
9197         cancel_lru_locks mdc
9198         test_mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
9199         createmany -o $DIR/$tdir/f $nr ||
9200                 error "failed to create $nr files in $DIR/$tdir"
9201         ls -l $DIR/$tdir > /dev/null
9202
9203         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9204         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9205         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9206         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9207         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9208
9209         # set lru_max_age to 1 sec
9210         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9211         echo "sleep $((recalc_p * 2)) seconds..."
9212         sleep $((recalc_p * 2))
9213
9214         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
9215         # restore lru_max_age
9216         $LCTL set_param -n $nsdir.lru_max_age $max_age
9217         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
9218         unlinkmany $DIR/$tdir/f $nr
9219 }
9220 run_test 124c "LRUR cancel very aged locks"
9221
9222 test_125() { # 13358
9223         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
9224         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] && skip "must have acl enabled" && return
9225         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9226         test_mkdir -p $DIR/d125 || error "mkdir failed"
9227         $SETSTRIPE -S 65536 -c -1 $DIR/d125 || error "setstripe failed"
9228         setfacl -R -m u:bin:rwx $DIR/d125 || error "setfacl $DIR/d125 failed"
9229         ls -ld $DIR/d125 || error "cannot access $DIR/d125"
9230 }
9231 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
9232
9233 test_126() { # bug 12829/13455
9234         [ -z "$(lctl get_param -n llite.*.client_type | grep local)" ] && skip "must run as local client" && return
9235         [ "$UID" != 0 ] && skip_env "skipping $TESTNAME (must run as root)" && return
9236         $GSS && skip "must run as gss disabled" && return
9237
9238         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
9239         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
9240         rm -f $DIR/$tfile
9241         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
9242 }
9243 run_test 126 "check that the fsgid provided by the client is taken into account"
9244
9245 test_127a() { # bug 15521
9246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9247         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
9248         $LCTL set_param osc.*.stats=0
9249         FSIZE=$((2048 * 1024))
9250         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9251         cancel_lru_locks osc
9252         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
9253
9254         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
9255         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9256                 echo "got $COUNT $NAME"
9257                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
9258                 eval $NAME=$COUNT || error "Wrong proc format"
9259
9260                 case $NAME in
9261                         read_bytes|write_bytes)
9262                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
9263                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
9264                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
9265                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
9266                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
9267                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
9268                                 error "sumsquare is too small: $SUMSQ"
9269                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
9270                                 error "sumsquare is too big: $SUMSQ"
9271                         ;;
9272                         *) ;;
9273                 esac
9274         done < $DIR/${tfile}.tmp
9275
9276         #check that we actually got some stats
9277         [ "$read_bytes" ] || error "Missing read_bytes stats"
9278         [ "$write_bytes" ] || error "Missing write_bytes stats"
9279         [ "$read_bytes" != 0 ] || error "no read done"
9280         [ "$write_bytes" != 0 ] || error "no write done"
9281 }
9282 run_test 127a "verify the client stats are sane"
9283
9284 test_127b() { # bug LU-333
9285         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9286         $LCTL set_param llite.*.stats=0
9287         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
9288         # perform 2 reads and writes so MAX is different from SUM.
9289         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9290         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9291         cancel_lru_locks osc
9292         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9293         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9294
9295         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
9296         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9297                 echo "got $COUNT $NAME"
9298                 eval $NAME=$COUNT || error "Wrong proc format"
9299
9300         case $NAME in
9301                 read_bytes)
9302                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9303                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9304                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9305                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9306                         ;;
9307                 write_bytes)
9308                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9309                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9310                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9311                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9312                         ;;
9313                         *) ;;
9314                 esac
9315         done < $TMP/${tfile}.tmp
9316
9317         #check that we actually got some stats
9318         [ "$read_bytes" ] || error "Missing read_bytes stats"
9319         [ "$write_bytes" ] || error "Missing write_bytes stats"
9320         [ "$read_bytes" != 0 ] || error "no read done"
9321         [ "$write_bytes" != 0 ] || error "no write done"
9322
9323         rm -f $TMP/${tfile}.tmp
9324 }
9325 run_test 127b "verify the llite client stats are sane"
9326
9327 test_128() { # bug 15212
9328         touch $DIR/$tfile
9329         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
9330                 find $DIR/$tfile
9331                 find $DIR/$tfile
9332         EOF
9333
9334         result=$(grep error $TMP/$tfile.log)
9335         rm -f $DIR/$tfile $TMP/$tfile.log
9336         [ -z "$result" ] ||
9337                 error "consecutive find's under interactive lfs failed"
9338 }
9339 run_test 128 "interactive lfs for 2 consecutive find's"
9340
9341 set_dir_limits () {
9342         local mntdev
9343         local canondev
9344         local node
9345
9346         local ldproc=/proc/fs/ldiskfs
9347         local facets=$(get_facets MDS)
9348
9349         for facet in ${facets//,/ }; do
9350                 canondev=$(ldiskfs_canon \
9351                            *.$(convert_facet2label $facet).mntdev $facet)
9352                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
9353                         ldproc=/sys/fs/ldiskfs
9354                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
9355                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
9356         done
9357 }
9358
9359 check_mds_dmesg() {
9360         local facets=$(get_facets MDS)
9361         for facet in ${facets//,/ }; do
9362                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
9363         done
9364         return 1
9365 }
9366
9367 test_129() {
9368         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9369                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9370
9371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9372         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9373                 skip "ldiskfs only test"
9374                 return
9375         fi
9376         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9377         local ENOSPC=28
9378         local EFBIG=27
9379         local has_warning=false
9380
9381         rm -rf $DIR/$tdir
9382         mkdir -p $DIR/$tdir
9383
9384         # block size of mds1
9385         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
9386         set_dir_limits $maxsize $maxsize
9387         local dirsize=$(stat -c%s "$DIR/$tdir")
9388         local nfiles=0
9389         while [[ $dirsize -le $maxsize ]]; do
9390                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
9391                 rc=$?
9392                 if ! $has_warning; then
9393                         check_mds_dmesg '"is approaching"' && has_warning=true
9394                 fi
9395                 # check two errors:
9396                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
9397                 # EFBIG for previous versions included in ldiskfs series
9398                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9399                         set_dir_limits 0 0
9400                         echo "return code $rc received as expected"
9401
9402                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
9403                                 error_exit "create failed w/o dir size limit"
9404
9405                         check_mds_dmesg '"has reached"' ||
9406                                 error_exit "reached message should be output"
9407
9408                         [ $has_warning = "false" ] &&
9409                                 error_exit "warning message should be output"
9410
9411                         dirsize=$(stat -c%s "$DIR/$tdir")
9412
9413                         [[ $dirsize -ge $maxsize ]] && return 0
9414                         error_exit "current dir size $dirsize, " \
9415                                    "previous limit $maxsize"
9416                 elif [ $rc -ne 0 ]; then
9417                         set_dir_limits 0 0
9418                         error_exit "return $rc received instead of expected " \
9419                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
9420                 fi
9421                 nfiles=$((nfiles + 1))
9422                 dirsize=$(stat -c%s "$DIR/$tdir")
9423         done
9424
9425         set_dir_limits 0 0
9426         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
9427 }
9428 run_test 129 "test directory size limit ========================"
9429
9430 OLDIFS="$IFS"
9431 cleanup_130() {
9432         trap 0
9433         IFS="$OLDIFS"
9434 }
9435
9436 test_130a() {
9437         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9438         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9439                 return
9440
9441         trap cleanup_130 EXIT RETURN
9442
9443         local fm_file=$DIR/$tfile
9444         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9445         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9446                 error "dd failed for $fm_file"
9447
9448         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9449         filefrag -ves $fm_file
9450         RC=$?
9451         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9452                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9453         [ $RC != 0 ] && error "filefrag $fm_file failed"
9454
9455         filefrag_op=$(filefrag -ve $fm_file |
9456                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9457         lun=$($GETSTRIPE -i $fm_file)
9458
9459         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9460         IFS=$'\n'
9461         tot_len=0
9462         for line in $filefrag_op
9463         do
9464                 frag_lun=`echo $line | cut -d: -f5`
9465                 ext_len=`echo $line | cut -d: -f4`
9466                 if (( $frag_lun != $lun )); then
9467                         cleanup_130
9468                         error "FIEMAP on 1-stripe file($fm_file) failed"
9469                         return
9470                 fi
9471                 (( tot_len += ext_len ))
9472         done
9473
9474         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9475                 cleanup_130
9476                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9477                 return
9478         fi
9479
9480         cleanup_130
9481
9482         echo "FIEMAP on single striped file succeeded"
9483 }
9484 run_test 130a "FIEMAP (1-stripe file)"
9485
9486 test_130b() {
9487         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
9488
9489         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9490         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9491                 return
9492
9493         trap cleanup_130 EXIT RETURN
9494
9495         local fm_file=$DIR/$tfile
9496         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9497                         error "setstripe on $fm_file"
9498         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9499                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9500
9501         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9502                 error "dd failed on $fm_file"
9503
9504         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9505         filefrag_op=$(filefrag -ve $fm_file |
9506                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9507
9508         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9509                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9510
9511         IFS=$'\n'
9512         tot_len=0
9513         num_luns=1
9514         for line in $filefrag_op
9515         do
9516                 frag_lun=$(echo $line | cut -d: -f5 |
9517                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9518                 ext_len=$(echo $line | cut -d: -f4)
9519                 if (( $frag_lun != $last_lun )); then
9520                         if (( tot_len != 1024 )); then
9521                                 cleanup_130
9522                                 error "FIEMAP on $fm_file failed; returned " \
9523                                 "len $tot_len for OST $last_lun instead of 1024"
9524                                 return
9525                         else
9526                                 (( num_luns += 1 ))
9527                                 tot_len=0
9528                         fi
9529                 fi
9530                 (( tot_len += ext_len ))
9531                 last_lun=$frag_lun
9532         done
9533         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9534                 cleanup_130
9535                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9536                         "luns or wrong len for OST $last_lun"
9537                 return
9538         fi
9539
9540         cleanup_130
9541
9542         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9543 }
9544 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9545
9546 test_130c() {
9547         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9548
9549         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9550         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9551                 return
9552
9553         trap cleanup_130 EXIT RETURN
9554
9555         local fm_file=$DIR/$tfile
9556         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9557         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9558                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9559
9560         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9561                         error "dd failed on $fm_file"
9562
9563         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9564         filefrag_op=$(filefrag -ve $fm_file |
9565                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9566
9567         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9568                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9569
9570         IFS=$'\n'
9571         tot_len=0
9572         num_luns=1
9573         for line in $filefrag_op
9574         do
9575                 frag_lun=$(echo $line | cut -d: -f5 |
9576                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9577                 ext_len=$(echo $line | cut -d: -f4)
9578                 if (( $frag_lun != $last_lun )); then
9579                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9580                         if (( logical != 512 )); then
9581                                 cleanup_130
9582                                 error "FIEMAP on $fm_file failed; returned " \
9583                                 "logical start for lun $logical instead of 512"
9584                                 return
9585                         fi
9586                         if (( tot_len != 512 )); then
9587                                 cleanup_130
9588                                 error "FIEMAP on $fm_file failed; returned " \
9589                                 "len $tot_len for OST $last_lun instead of 1024"
9590                                 return
9591                         else
9592                                 (( num_luns += 1 ))
9593                                 tot_len=0
9594                         fi
9595                 fi
9596                 (( tot_len += ext_len ))
9597                 last_lun=$frag_lun
9598         done
9599         if (( num_luns != 2 || tot_len != 512 )); then
9600                 cleanup_130
9601                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9602                         "luns or wrong len for OST $last_lun"
9603                 return
9604         fi
9605
9606         cleanup_130
9607
9608         echo "FIEMAP on 2-stripe file with hole succeeded"
9609 }
9610 run_test 130c "FIEMAP (2-stripe file with hole)"
9611
9612 test_130d() {
9613         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
9614
9615         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9616         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9617                 return
9618
9619         trap cleanup_130 EXIT RETURN
9620
9621         local fm_file=$DIR/$tfile
9622         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9623                         error "setstripe on $fm_file"
9624         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9625                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9626
9627         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
9628         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
9629                 error "dd failed on $fm_file"
9630
9631         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9632         filefrag_op=$(filefrag -ve $fm_file |
9633                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9634
9635         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9636                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9637
9638         IFS=$'\n'
9639         tot_len=0
9640         num_luns=1
9641         for line in $filefrag_op
9642         do
9643                 frag_lun=$(echo $line | cut -d: -f5 |
9644                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9645                 ext_len=$(echo $line | cut -d: -f4)
9646                 if (( $frag_lun != $last_lun )); then
9647                         if (( tot_len != 1024 )); then
9648                                 cleanup_130
9649                                 error "FIEMAP on $fm_file failed; returned " \
9650                                 "len $tot_len for OST $last_lun instead of 1024"
9651                                 return
9652                         else
9653                                 (( num_luns += 1 ))
9654                                 tot_len=0
9655                         fi
9656                 fi
9657                 (( tot_len += ext_len ))
9658                 last_lun=$frag_lun
9659         done
9660         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
9661                 cleanup_130
9662                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9663                         "luns or wrong len for OST $last_lun"
9664                 return
9665         fi
9666
9667         cleanup_130
9668
9669         echo "FIEMAP on N-stripe file succeeded"
9670 }
9671 run_test 130d "FIEMAP (N-stripe file)"
9672
9673 test_130e() {
9674         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9675
9676         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9677         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9678
9679         trap cleanup_130 EXIT RETURN
9680
9681         local fm_file=$DIR/$tfile
9682         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9683         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9684                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9685
9686         NUM_BLKS=512
9687         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9688         for ((i = 0; i < $NUM_BLKS; i++))
9689         do
9690                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9691         done
9692
9693         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9694         filefrag_op=$(filefrag -ve $fm_file |
9695                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9696
9697         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9698                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9699
9700         IFS=$'\n'
9701         tot_len=0
9702         num_luns=1
9703         for line in $filefrag_op
9704         do
9705                 frag_lun=$(echo $line | cut -d: -f5 |
9706                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9707                 ext_len=$(echo $line | cut -d: -f4)
9708                 if (( $frag_lun != $last_lun )); then
9709                         if (( tot_len != $EXPECTED_LEN )); then
9710                                 cleanup_130
9711                                 error "FIEMAP on $fm_file failed; returned " \
9712                                 "len $tot_len for OST $last_lun instead " \
9713                                 "of $EXPECTED_LEN"
9714                                 return
9715                         else
9716                                 (( num_luns += 1 ))
9717                                 tot_len=0
9718                         fi
9719                 fi
9720                 (( tot_len += ext_len ))
9721                 last_lun=$frag_lun
9722         done
9723         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
9724                 cleanup_130
9725                 error "FIEMAP on $fm_file failed; returned wrong number " \
9726                         "of luns or wrong len for OST $last_lun"
9727                 return
9728         fi
9729
9730         cleanup_130
9731
9732         echo "FIEMAP with continuation calls succeeded"
9733 }
9734 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
9735
9736 # Test for writev/readv
9737 test_131a() {
9738         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
9739                 error "writev test failed"
9740         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
9741                 error "readv failed"
9742         rm -f $DIR/$tfile
9743 }
9744 run_test 131a "test iov's crossing stripe boundary for writev/readv"
9745
9746 test_131b() {
9747         local fsize=$((524288 + 1048576 + 1572864))
9748         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
9749                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9750                         error "append writev test failed"
9751
9752         ((fsize += 1572864 + 1048576))
9753         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
9754                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
9755                         error "append writev test failed"
9756         rm -f $DIR/$tfile
9757 }
9758 run_test 131b "test append writev"
9759
9760 test_131c() {
9761         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
9762         error "NOT PASS"
9763 }
9764 run_test 131c "test read/write on file w/o objects"
9765
9766 test_131d() {
9767         rwv -f $DIR/$tfile -w -n 1 1572864
9768         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
9769         if [ "$NOB" != 1572864 ]; then
9770                 error "Short read filed: read $NOB bytes instead of 1572864"
9771         fi
9772         rm -f $DIR/$tfile
9773 }
9774 run_test 131d "test short read"
9775
9776 test_131e() {
9777         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
9778         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
9779         error "read hitting hole failed"
9780         rm -f $DIR/$tfile
9781 }
9782 run_test 131e "test read hitting hole"
9783
9784 check_stats() {
9785         local facet=$1
9786         local op=$2
9787         local want=${3:-0}
9788         local res
9789
9790         case $facet in
9791         mds*) res=$(do_facet $facet \
9792                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
9793                  ;;
9794         ost*) res=$(do_facet $facet \
9795                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
9796                  ;;
9797         *) error "Wrong facet '$facet'" ;;
9798         esac
9799         echo $res
9800         [ "$res" ] || error "The counter for $op on $facet was not incremented"
9801         # if the argument $3 is zero, it means any stat increment is ok.
9802         if [[ $want -gt 0 ]]; then
9803                 local count=$(echo $res | awk '{ print $2 }')
9804                 [[ $count -ne $want ]] &&
9805                         error "The $op counter on $facet is $count, not $want"
9806         fi
9807 }
9808
9809 test_133a() {
9810         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9811         remote_ost_nodsh && skip "remote OST with nodsh" && return
9812         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9813
9814         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9815                 { skip "MDS doesn't support rename stats"; return; }
9816         local testdir=$DIR/${tdir}/stats_testdir
9817         mkdir -p $DIR/${tdir}
9818
9819         # clear stats.
9820         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9821         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9822
9823         # verify mdt stats first.
9824         mkdir ${testdir} || error "mkdir failed"
9825         check_stats $SINGLEMDS "mkdir" 1
9826         touch ${testdir}/${tfile} || error "touch failed"
9827         check_stats $SINGLEMDS "open" 1
9828         check_stats $SINGLEMDS "close" 1
9829         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
9830                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
9831                 check_stats $SINGLEMDS "mknod" 2
9832         }
9833         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
9834         check_stats $SINGLEMDS "unlink" 1
9835         rm -f ${testdir}/${tfile} || error "file remove failed"
9836         check_stats $SINGLEMDS "unlink" 2
9837
9838         # remove working dir and check mdt stats again.
9839         rmdir ${testdir} || error "rmdir failed"
9840         check_stats $SINGLEMDS "rmdir" 1
9841
9842         local testdir1=$DIR/${tdir}/stats_testdir1
9843         mkdir -p ${testdir}
9844         mkdir -p ${testdir1}
9845         touch ${testdir1}/test1
9846         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
9847         check_stats $SINGLEMDS "crossdir_rename" 1
9848
9849         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
9850         check_stats $SINGLEMDS "samedir_rename" 1
9851
9852         rm -rf $DIR/${tdir}
9853 }
9854 run_test 133a "Verifying MDT stats ========================================"
9855
9856 test_133b() {
9857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9858         remote_ost_nodsh && skip "remote OST with nodsh" && return
9859         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9860         local testdir=$DIR/${tdir}/stats_testdir
9861         mkdir -p ${testdir} || error "mkdir failed"
9862         touch ${testdir}/${tfile} || error "touch failed"
9863         cancel_lru_locks mdc
9864
9865         # clear stats.
9866         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9867         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9868
9869         # extra mdt stats verification.
9870         chmod 444 ${testdir}/${tfile} || error "chmod failed"
9871         check_stats $SINGLEMDS "setattr" 1
9872         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9873         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
9874         then            # LU-1740
9875                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
9876                 check_stats $SINGLEMDS "getattr" 1
9877         fi
9878         $LFS df || error "lfs failed"
9879         check_stats $SINGLEMDS "statfs" 1
9880
9881         rm -rf $DIR/${tdir}
9882 }
9883 run_test 133b "Verifying extra MDT stats =================================="
9884
9885 test_133c() {
9886         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9887         remote_ost_nodsh && skip "remote OST with nodsh" && return
9888         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9889         local testdir=$DIR/${tdir}/stats_testdir
9890         test_mkdir -p ${testdir} || error "mkdir failed"
9891
9892         # verify obdfilter stats.
9893         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
9894         sync
9895         cancel_lru_locks osc
9896         wait_delete_completed
9897
9898         # clear stats.
9899         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
9900         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
9901
9902         dd if=/dev/zero of=${testdir}/${tfile} conv=notrunc bs=512k count=1 || error "dd failed"
9903         sync
9904         cancel_lru_locks osc
9905         check_stats ost1 "write" 1
9906
9907         dd if=${testdir}/${tfile} of=/dev/null bs=1k count=1 || error "dd failed"
9908         check_stats ost1 "read" 1
9909
9910         > ${testdir}/${tfile} || error "truncate failed"
9911         check_stats ost1 "punch" 1
9912
9913         rm -f ${testdir}/${tfile} || error "file remove failed"
9914         wait_delete_completed
9915         check_stats ost1 "destroy" 1
9916
9917         rm -rf $DIR/${tdir}
9918 }
9919 run_test 133c "Verifying OST stats ========================================"
9920
9921 order_2() {
9922         local value=$1
9923         local orig=$value
9924         local order=1
9925
9926         while [ $value -ge 2 ]; do
9927                 order=$((order*2))
9928                 value=$((value/2))
9929         done
9930
9931         if [ $orig -gt $order ]; then
9932                 order=$((order*2))
9933         fi
9934         echo $order
9935 }
9936
9937 size_in_KMGT() {
9938     local value=$1
9939     local size=('K' 'M' 'G' 'T');
9940     local i=0
9941     local size_string=$value
9942
9943     while [ $value -ge 1024 ]; do
9944         if [ $i -gt 3 ]; then
9945             #T is the biggest unit we get here, if that is bigger,
9946             #just return XXXT
9947             size_string=${value}T
9948             break
9949         fi
9950         value=$((value >> 10))
9951         if [ $value -lt 1024 ]; then
9952             size_string=${value}${size[$i]}
9953             break
9954         fi
9955         i=$((i + 1))
9956     done
9957
9958     echo $size_string
9959 }
9960
9961 get_rename_size() {
9962         local size=$1
9963         local context=${2:-.}
9964         local sample=$(do_facet $SINGLEMDS $LCTL \
9965                 get_param mdt.$FSNAME-MDT0000.rename_stats |
9966                 grep -A1 $context |
9967                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
9968         echo $sample
9969 }
9970
9971 test_133d() {
9972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9973         remote_ost_nodsh && skip "remote OST with nodsh" && return
9974         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9975         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
9976         { skip "MDS doesn't support rename stats"; return; }
9977
9978         local testdir1=$DIR/${tdir}/stats_testdir1
9979         local testdir2=$DIR/${tdir}/stats_testdir2
9980         mkdir -p $DIR/${tdir}
9981
9982         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
9983
9984         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
9985         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
9986
9987         createmany -o $testdir1/test 512 || error "createmany failed"
9988
9989         # check samedir rename size
9990         mv ${testdir1}/test0 ${testdir1}/test_0
9991
9992         local testdir1_size=$(ls -l $DIR/${tdir} |
9993                 awk '/stats_testdir1/ {print $5}')
9994         local testdir2_size=$(ls -l $DIR/${tdir} |
9995                 awk '/stats_testdir2/ {print $5}')
9996
9997         testdir1_size=$(order_2 $testdir1_size)
9998         testdir2_size=$(order_2 $testdir2_size)
9999
10000         testdir1_size=$(size_in_KMGT $testdir1_size)
10001         testdir2_size=$(size_in_KMGT $testdir2_size)
10002
10003         echo "source rename dir size: ${testdir1_size}"
10004         echo "target rename dir size: ${testdir2_size}"
10005
10006         local cmd="do_facet $SINGLEMDS $LCTL "
10007         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
10008
10009         eval $cmd || error "$cmd failed"
10010         local samedir=$($cmd | grep 'same_dir')
10011         local same_sample=$(get_rename_size $testdir1_size)
10012         [ -z "$samedir" ] && error "samedir_rename_size count error"
10013         [[ $same_sample -eq 1 ]] ||
10014                 error "samedir_rename_size error $same_sample"
10015         echo "Check same dir rename stats success"
10016
10017         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10018
10019         # check crossdir rename size
10020         mv ${testdir1}/test_0 ${testdir2}/test_0
10021
10022         testdir1_size=$(ls -l $DIR/${tdir} |
10023                 awk '/stats_testdir1/ {print $5}')
10024         testdir2_size=$(ls -l $DIR/${tdir} |
10025                 awk '/stats_testdir2/ {print $5}')
10026
10027         testdir1_size=$(order_2 $testdir1_size)
10028         testdir2_size=$(order_2 $testdir2_size)
10029
10030         testdir1_size=$(size_in_KMGT $testdir1_size)
10031         testdir2_size=$(size_in_KMGT $testdir2_size)
10032
10033         echo "source rename dir size: ${testdir1_size}"
10034         echo "target rename dir size: ${testdir2_size}"
10035
10036         eval $cmd || error "$cmd failed"
10037         local crossdir=$($cmd | grep 'crossdir')
10038         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10039         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10040         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10041         [[ $src_sample -eq 1 ]] ||
10042                 error "crossdir_rename_size error $src_sample"
10043         [[ $tgt_sample -eq 1 ]] ||
10044                 error "crossdir_rename_size error $tgt_sample"
10045         echo "Check cross dir rename stats success"
10046         rm -rf $DIR/${tdir}
10047 }
10048 run_test 133d "Verifying rename_stats ========================================"
10049
10050 test_133e() {
10051         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10052         remote_ost_nodsh && skip "remote OST with nodsh" && return
10053         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10054         local testdir=$DIR/${tdir}/stats_testdir
10055         local ctr f0 f1 bs=32768 count=42 sum
10056
10057         mkdir -p ${testdir} || error "mkdir failed"
10058
10059         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10060
10061         for ctr in {write,read}_bytes; do
10062                 sync
10063                 cancel_lru_locks osc
10064
10065                 do_facet ost1 $LCTL set_param -n \
10066                         "obdfilter.*.exports.clear=clear"
10067
10068                 if [ $ctr = write_bytes ]; then
10069                         f0=/dev/zero
10070                         f1=${testdir}/${tfile}
10071                 else
10072                         f0=${testdir}/${tfile}
10073                         f1=/dev/null
10074                 fi
10075
10076                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10077                         error "dd failed"
10078                 sync
10079                 cancel_lru_locks osc
10080
10081                 sum=$(do_facet ost1 $LCTL get_param \
10082                         "obdfilter.*.exports.*.stats" |
10083                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10084                                 $1 == ctr { sum += $7 }
10085                                 END { printf("%0.0f", sum) }')
10086
10087                 if ((sum != bs * count)); then
10088                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10089                 fi
10090         done
10091
10092         rm -rf $DIR/${tdir}
10093 }
10094 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10095
10096 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10097
10098 test_133f() {
10099         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10100         remote_ost_nodsh && skip "remote OST with nodsh" && return
10101         # First without trusting modes.
10102         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10103         echo "proc_dirs='$proc_dirs'"
10104         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10105         find $proc_dirs -exec cat '{}' \; &> /dev/null
10106
10107         # Second verifying readability.
10108         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10109
10110         # eventually, this can also be replaced with "lctl get_param -R",
10111         # but not until that option is always available on the server
10112         local facet
10113         for facet in mds1 ost1; do
10114                 local facet_proc_dirs=$(do_facet $facet \
10115                                         \\\ls -d $proc_regexp 2>/dev/null)
10116                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10117                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10118                 do_facet $facet find $facet_proc_dirs \
10119                         ! -name req_history \
10120                         -exec cat '{}' \\\; &> /dev/null
10121
10122                 do_facet $facet find $facet_proc_dirs \
10123                         ! -name req_history \
10124                         -type f \
10125                         -exec cat '{}' \\\; &> /dev/null ||
10126                                 error "proc file read failed"
10127         done
10128 }
10129 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
10130
10131 test_133g() {
10132         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10133         remote_ost_nodsh && skip "remote OST with nodsh" && return
10134         # Second verifying writability.
10135         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10136         echo "proc_dirs='$proc_dirs'"
10137         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10138         find $proc_dirs \
10139                 -type f \
10140                 -not -name force_lbug \
10141                 -not -name changelog_mask \
10142                 -exec badarea_io '{}' \; &> /dev/null ||
10143                 error "find $proc_dirs failed"
10144
10145         local facet
10146         for facet in mds1 ost1; do
10147                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10148                         skip "Too old lustre on $facet" && continue
10149                 local facet_proc_dirs=$(do_facet $facet \
10150                                         \\\ls -d $proc_regexp 2> /dev/null)
10151                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10152                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10153                 do_facet $facet find $facet_proc_dirs \
10154                         -type f \
10155                         -not -name force_lbug \
10156                         -not -name changelog_mask \
10157                         -exec badarea_io '{}' \\\; &> /dev/null ||
10158                                 error "$facet find $facet_proc_dirs failed"
10159         done
10160
10161         # remount the FS in case writes/reads /proc break the FS
10162         cleanup || error "failed to unmount"
10163         setup || error "failed to setup"
10164         true
10165 }
10166 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
10167
10168 test_133h() {
10169         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10170         remote_ost_nodsh && skip "remote OST with nodsh" && return
10171         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10172                 skip "Need MDS version at least 2.9.54" && return
10173
10174         local facet
10175         for facet in client mds1 ost1; do
10176                 local facet_proc_dirs=$(do_facet $facet \
10177                                         \\\ls -d $proc_regexp 2> /dev/null)
10178                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10179                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10180                 # Get the list of files that are missing the terminating newline
10181                 local missing=($(do_facet $facet \
10182                         find ${facet_proc_dirs} -type f \|              \
10183                                 while read F\; do                       \
10184                                         awk -v FS='\v' -v RS='\v\v'     \
10185                                         "'END { if(NR>0 &&              \
10186                                         \\\$NF !~ /.*\\\n\$/)           \
10187                                                 print FILENAME}'"       \
10188                                         '\$F'\;                         \
10189                                 done 2>/dev/null))
10190                 [ ${#missing[*]} -eq 0 ] ||
10191                         error "files do not end with newline: ${missing[*]}"
10192         done
10193 }
10194 run_test 133h "Proc files should end with newlines"
10195
10196 test_134a() {
10197         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10198         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10199                 skip "Need MDS version at least 2.7.54" && return
10200
10201         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10202         cancel_lru_locks mdc
10203
10204         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10205         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10206         [ $unused -eq 0 ] || error "$unused locks are not cleared"
10207
10208         local nr=1000
10209         createmany -o $DIR/$tdir/f $nr ||
10210                 error "failed to create $nr files in $DIR/$tdir"
10211         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10212
10213         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
10214         do_facet mds1 $LCTL set_param fail_loc=0x327
10215         do_facet mds1 $LCTL set_param fail_val=500
10216         touch $DIR/$tdir/m
10217
10218         echo "sleep 10 seconds ..."
10219         sleep 10
10220         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
10221
10222         do_facet mds1 $LCTL set_param fail_loc=0
10223         do_facet mds1 $LCTL set_param fail_val=0
10224         [ $lck_cnt -lt $unused ] ||
10225                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
10226
10227         rm $DIR/$tdir/m
10228         unlinkmany $DIR/$tdir/f $nr
10229 }
10230 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
10231
10232 test_134b() {
10233         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10234         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10235                 skip "Need MDS version at least 2.7.54" && return
10236
10237         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10238         cancel_lru_locks mdc
10239
10240         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10241                         ldlm.lock_reclaim_threshold_mb)
10242         # disable reclaim temporarily
10243         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10244
10245         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10246         do_facet mds1 $LCTL set_param fail_loc=0x328
10247         do_facet mds1 $LCTL set_param fail_val=500
10248
10249         $LCTL set_param debug=+trace
10250
10251         local nr=600
10252         createmany -o $DIR/$tdir/f $nr &
10253         local create_pid=$!
10254
10255         echo "Sleep $TIMEOUT seconds ..."
10256         sleep $TIMEOUT
10257         if ! ps -p $create_pid  > /dev/null 2>&1; then
10258                 do_facet mds1 $LCTL set_param fail_loc=0
10259                 do_facet mds1 $LCTL set_param fail_val=0
10260                 do_facet mds1 $LCTL set_param \
10261                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10262                 error "createmany finished incorrectly!"
10263         fi
10264         do_facet mds1 $LCTL set_param fail_loc=0
10265         do_facet mds1 $LCTL set_param fail_val=0
10266         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10267         wait $create_pid || return 1
10268
10269         unlinkmany $DIR/$tdir/f $nr
10270 }
10271 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10272
10273 test_140() { #bug-17379
10274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10275         test_mkdir -p $DIR/$tdir || error "Creating dir $DIR/$tdir"
10276         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10277         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10278
10279         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10280         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10281         local i=0
10282         while i=`expr $i + 1`; do
10283                 test_mkdir -p $i || error "Creating dir $i"
10284                 cd $i || error "Changing to $i"
10285                 ln -s ../stat stat || error "Creating stat symlink"
10286                 # Read the symlink until ELOOP present,
10287                 # not LBUGing the system is considered success,
10288                 # we didn't overrun the stack.
10289                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10290                 [ $ret -ne 0 ] && {
10291                         if [ $ret -eq 40 ]; then
10292                                 break  # -ELOOP
10293                         else
10294                                 error "Open stat symlink"
10295                                 return
10296                         fi
10297                 }
10298         done
10299         i=`expr $i - 1`
10300         echo "The symlink depth = $i"
10301         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10302                                         error "Invalid symlink depth"
10303
10304         # Test recursive symlink
10305         ln -s symlink_self symlink_self
10306         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10307         echo "open symlink_self returns $ret"
10308         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10309 }
10310 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10311
10312 test_150() {
10313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10314         local TF="$TMP/$tfile"
10315
10316         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10317         cp $TF $DIR/$tfile
10318         cancel_lru_locks osc
10319         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10320         remount_client $MOUNT
10321         df -P $MOUNT
10322         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10323
10324         $TRUNCATE $TF 6000
10325         $TRUNCATE $DIR/$tfile 6000
10326         cancel_lru_locks osc
10327         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10328
10329         echo "12345" >>$TF
10330         echo "12345" >>$DIR/$tfile
10331         cancel_lru_locks osc
10332         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10333
10334         echo "12345" >>$TF
10335         echo "12345" >>$DIR/$tfile
10336         cancel_lru_locks osc
10337         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10338
10339         rm -f $TF
10340         true
10341 }
10342 run_test 150 "truncate/append tests"
10343
10344 #LU-2902 roc_hit was not able to read all values from lproc
10345 function roc_hit_init() {
10346         local list=$(comma_list $(osts_nodes))
10347         local dir=$DIR/$tdir-check
10348         local file=$dir/file
10349         local BEFORE
10350         local AFTER
10351         local idx
10352
10353         test_mkdir -p $dir
10354         #use setstripe to do a write to every ost
10355         for i in $(seq 0 $((OSTCOUNT-1))); do
10356                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10357                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10358                 idx=$(printf %04x $i)
10359                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10360                         awk '$1 == "cache_access" {sum += $7}
10361                                 END { printf("%0.0f", sum) }')
10362
10363                 cancel_lru_locks osc
10364                 cat $file >/dev/null
10365
10366                 AFTER=$(get_osd_param $list *OST*$idx stats |
10367                         awk '$1 == "cache_access" {sum += $7}
10368                                 END { printf("%0.0f", sum) }')
10369
10370                 echo BEFORE:$BEFORE AFTER:$AFTER
10371                 if ! let "AFTER - BEFORE == 4"; then
10372                         rm -rf $dir
10373                         error "roc_hit is not safe to use"
10374                 fi
10375                 rm $file
10376         done
10377
10378         rm -rf $dir
10379 }
10380
10381 function roc_hit() {
10382         local list=$(comma_list $(osts_nodes))
10383         echo $(get_osd_param $list '' stats |
10384                 awk '$1 == "cache_hit" {sum += $7}
10385                         END { printf("%0.0f", sum) }')
10386 }
10387
10388 function set_cache() {
10389         local on=1
10390
10391         if [ "$2" == "off" ]; then
10392                 on=0;
10393         fi
10394         local list=$(comma_list $(osts_nodes))
10395         set_osd_param $list '' $1_cache_enable $on
10396
10397         cancel_lru_locks osc
10398 }
10399
10400 test_151() {
10401         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10402         remote_ost_nodsh && skip "remote OST with nodsh" && return
10403
10404         local CPAGES=3
10405         local list=$(comma_list $(osts_nodes))
10406
10407         # check whether obdfilter is cache capable at all
10408         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10409                 echo "not cache-capable obdfilter"
10410                 return 0
10411         fi
10412
10413         # check cache is enabled on all obdfilters
10414         if get_osd_param $list '' read_cache_enable | grep 0; then
10415                 echo "oss cache is disabled"
10416                 return 0
10417         fi
10418
10419         set_osd_param $list '' writethrough_cache_enable 1
10420
10421         # check write cache is enabled on all obdfilters
10422         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10423                 echo "oss write cache is NOT enabled"
10424                 return 0
10425         fi
10426
10427         roc_hit_init
10428
10429         #define OBD_FAIL_OBD_NO_LRU  0x609
10430         do_nodes $list $LCTL set_param fail_loc=0x609
10431
10432         # pages should be in the case right after write
10433         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10434                 error "dd failed"
10435
10436         local BEFORE=$(roc_hit)
10437         cancel_lru_locks osc
10438         cat $DIR/$tfile >/dev/null
10439         local AFTER=$(roc_hit)
10440
10441         do_nodes $list $LCTL set_param fail_loc=0
10442
10443         if ! let "AFTER - BEFORE == CPAGES"; then
10444                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10445         fi
10446
10447         # the following read invalidates the cache
10448         cancel_lru_locks osc
10449         set_osd_param $list '' read_cache_enable 0
10450         cat $DIR/$tfile >/dev/null
10451
10452         # now data shouldn't be found in the cache
10453         BEFORE=$(roc_hit)
10454         cancel_lru_locks osc
10455         cat $DIR/$tfile >/dev/null
10456         AFTER=$(roc_hit)
10457         if let "AFTER - BEFORE != 0"; then
10458                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10459         fi
10460
10461         set_osd_param $list '' read_cache_enable 1
10462         rm -f $DIR/$tfile
10463 }
10464 run_test 151 "test cache on oss and controls ==============================="
10465
10466 test_152() {
10467         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10468         local TF="$TMP/$tfile"
10469
10470         # simulate ENOMEM during write
10471 #define OBD_FAIL_OST_NOMEM      0x226
10472         lctl set_param fail_loc=0x80000226
10473         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10474         cp $TF $DIR/$tfile
10475         sync || error "sync failed"
10476         lctl set_param fail_loc=0
10477
10478         # discard client's cache
10479         cancel_lru_locks osc
10480
10481         # simulate ENOMEM during read
10482         lctl set_param fail_loc=0x80000226
10483         cmp $TF $DIR/$tfile || error "cmp failed"
10484         lctl set_param fail_loc=0
10485
10486         rm -f $TF
10487 }
10488 run_test 152 "test read/write with enomem ============================"
10489
10490 test_153() {
10491         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10492 }
10493 run_test 153 "test if fdatasync does not crash ======================="
10494
10495 dot_lustre_fid_permission_check() {
10496         local fid=$1
10497         local ffid=$MOUNT/.lustre/fid/$fid
10498         local test_dir=$2
10499
10500         echo "stat fid $fid"
10501         stat $ffid > /dev/null || error "stat $ffid failed."
10502         echo "touch fid $fid"
10503         touch $ffid || error "touch $ffid failed."
10504         echo "write to fid $fid"
10505         cat /etc/hosts > $ffid || error "write $ffid failed."
10506         echo "read fid $fid"
10507         diff /etc/hosts $ffid || error "read $ffid failed."
10508         echo "append write to fid $fid"
10509         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10510         echo "rename fid $fid"
10511         mv $ffid $test_dir/$tfile.1 &&
10512                 error "rename $ffid to $tfile.1 should fail."
10513         touch $test_dir/$tfile.1
10514         mv $test_dir/$tfile.1 $ffid &&
10515                 error "rename $tfile.1 to $ffid should fail."
10516         rm -f $test_dir/$tfile.1
10517         echo "truncate fid $fid"
10518         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10519         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10520                 echo "link fid $fid"
10521                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10522         fi
10523         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10524                 echo "setfacl fid $fid"
10525                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10526                 echo "getfacl fid $fid"
10527                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10528         fi
10529         echo "unlink fid $fid"
10530         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10531         echo "mknod fid $fid"
10532         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10533
10534         fid=[0xf00000400:0x1:0x0]
10535         ffid=$MOUNT/.lustre/fid/$fid
10536
10537         echo "stat non-exist fid $fid"
10538         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10539         echo "write to non-exist fid $fid"
10540         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10541         echo "link new fid $fid"
10542         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10543
10544         mkdir -p $test_dir/$tdir
10545         touch $test_dir/$tdir/$tfile
10546         fid=$($LFS path2fid $test_dir/$tdir)
10547         rc=$?
10548         [ $rc -ne 0 ] &&
10549                 error "error: could not get fid for $test_dir/$dir/$tfile."
10550
10551         ffid=$MOUNT/.lustre/fid/$fid
10552
10553         echo "ls $fid"
10554         ls $ffid > /dev/null || error "ls $ffid failed."
10555         echo "touch $fid/$tfile.1"
10556         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10557
10558         echo "touch $MOUNT/.lustre/fid/$tfile"
10559         touch $MOUNT/.lustre/fid/$tfile && \
10560                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10561
10562         echo "setxattr to $MOUNT/.lustre/fid"
10563         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10564
10565         echo "listxattr for $MOUNT/.lustre/fid"
10566         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10567
10568         echo "delxattr from $MOUNT/.lustre/fid"
10569         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10570
10571         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10572         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10573                 error "touch invalid fid should fail."
10574
10575         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10576         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10577                 error "touch non-normal fid should fail."
10578
10579         echo "rename $tdir to $MOUNT/.lustre/fid"
10580         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10581                 error "rename to $MOUNT/.lustre/fid should fail."
10582
10583         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10584         then            # LU-3547
10585                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10586                 local new_obf_mode=777
10587
10588                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10589                 chmod $new_obf_mode $DIR/.lustre/fid ||
10590                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10591
10592                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10593                 [ $obf_mode -eq $new_obf_mode ] ||
10594                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10595
10596                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10597                 chmod $old_obf_mode $DIR/.lustre/fid ||
10598                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10599         fi
10600
10601         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10602         fid=$($LFS path2fid $test_dir/$tfile-2)
10603
10604         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10605         then # LU-5424
10606                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10607                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10608                         error "create lov data thru .lustre failed"
10609         fi
10610         echo "cp /etc/passwd $test_dir/$tfile-2"
10611         cp /etc/passwd $test_dir/$tfile-2 ||
10612                 error "copy to $test_dir/$tfile-2 failed."
10613         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10614         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10615                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10616
10617         rm -rf $test_dir/tfile.lnk
10618         rm -rf $test_dir/$tfile-2
10619 }
10620
10621 test_154A() {
10622         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10623                 skip "Need MDS version at least 2.4.1" && return
10624
10625         local tf=$DIR/$tfile
10626         touch $tf
10627
10628         local fid=$($LFS path2fid $tf)
10629         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10630
10631         # check that we get the same pathname back
10632         local found=$($LFS fid2path $MOUNT "$fid")
10633         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10634         [ "$found" == "$tf" ] ||
10635                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10636 }
10637 run_test 154A "lfs path2fid and fid2path basic checks"
10638
10639 test_154B() {
10640         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10641                 skip "Need MDS version at least 2.4.1" && return
10642
10643         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10644         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10645         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10646         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10647
10648         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10649         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10650
10651         # check that we get the same pathname
10652         echo "PFID: $PFID, name: $name"
10653         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10654         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10655         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10656                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10657
10658         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10659 }
10660 run_test 154B "verify the ll_decode_linkea tool"
10661
10662 test_154a() {
10663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10664         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10665                 { skip "Need MDS version at least 2.2.51"; return 0; }
10666         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10667         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10668
10669         cp /etc/hosts $DIR/$tfile
10670
10671         fid=$($LFS path2fid $DIR/$tfile)
10672         rc=$?
10673         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10674
10675         dot_lustre_fid_permission_check "$fid" $DIR ||
10676                 error "dot lustre permission check $fid failed"
10677
10678         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10679
10680         touch $MOUNT/.lustre/file &&
10681                 error "creation is not allowed under .lustre"
10682
10683         mkdir $MOUNT/.lustre/dir &&
10684                 error "mkdir is not allowed under .lustre"
10685
10686         rm -rf $DIR/$tfile
10687 }
10688 run_test 154a "Open-by-FID"
10689
10690 test_154b() {
10691         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10692         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10693                 { skip "Need MDS version at least 2.2.51"; return 0; }
10694         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10695
10696         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
10697
10698         local remote_dir=$DIR/$tdir/remote_dir
10699         local MDTIDX=1
10700         local rc=0
10701
10702         mkdir -p $DIR/$tdir
10703         $LFS mkdir -i $MDTIDX $remote_dir ||
10704                 error "create remote directory failed"
10705
10706         cp /etc/hosts $remote_dir/$tfile
10707
10708         fid=$($LFS path2fid $remote_dir/$tfile)
10709         rc=$?
10710         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
10711
10712         dot_lustre_fid_permission_check "$fid" $remote_dir ||
10713                 error "dot lustre permission check $fid failed"
10714         rm -rf $DIR/$tdir
10715 }
10716 run_test 154b "Open-by-FID for remote directory"
10717
10718 test_154c() {
10719         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10720                 skip "Need MDS version at least 2.4.1" && return
10721
10722         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
10723         local FID1=$($LFS path2fid $DIR/$tfile.1)
10724         local FID2=$($LFS path2fid $DIR/$tfile.2)
10725         local FID3=$($LFS path2fid $DIR/$tfile.3)
10726
10727         local N=1
10728         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
10729                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
10730                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
10731                 local want=FID$N
10732                 [ "$FID" = "${!want}" ] ||
10733                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
10734                 N=$((N + 1))
10735         done
10736
10737         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
10738         do
10739                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
10740                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
10741                 N=$((N + 1))
10742         done
10743 }
10744 run_test 154c "lfs path2fid and fid2path multiple arguments"
10745
10746 test_154d() {
10747         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10748         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
10749                 skip "Need MDS version at least 2.5.53" && return
10750
10751         if remote_mds; then
10752                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
10753         else
10754                 nid="0@lo"
10755         fi
10756         local proc_ofile="mdt.*.exports.'$nid'.open_files"
10757         local fd
10758         local cmd
10759
10760         rm -f $DIR/$tfile
10761         touch $DIR/$tfile
10762
10763         local fid=$($LFS path2fid $DIR/$tfile)
10764         # Open the file
10765         fd=$(free_fd)
10766         cmd="exec $fd<$DIR/$tfile"
10767         eval $cmd
10768         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
10769         echo "$fid_list" | grep "$fid"
10770         rc=$?
10771
10772         cmd="exec $fd>/dev/null"
10773         eval $cmd
10774         if [ $rc -ne 0 ]; then
10775                 error "FID $fid not found in open files list $fid_list"
10776         fi
10777 }
10778 run_test 154d "Verify open file fid"
10779
10780 test_154e()
10781 {
10782         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
10783                 skip "Need MDS version at least 2.6.50" && return
10784
10785         if ls -a $MOUNT | grep -q '^\.lustre$'; then
10786                 error ".lustre returned by readdir"
10787         fi
10788 }
10789 run_test 154e ".lustre is not returned by readdir"
10790
10791 test_154f() {
10792         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10793         # create parent directory on a single MDT to avoid cross-MDT hardlinks
10794         test_mkdir -p -c1 $DIR/$tdir/d
10795         # test dirs inherit from its stripe
10796         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
10797         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
10798         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
10799         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
10800         touch $DIR/f
10801
10802         # get fid of parents
10803         local FID0=$($LFS path2fid $DIR/$tdir/d)
10804         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
10805         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
10806         local FID3=$($LFS path2fid $DIR)
10807
10808         # check that path2fid --parents returns expected <parent_fid>/name
10809         # 1) test for a directory (single parent)
10810         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
10811         [ "$parent" == "$FID0/foo1" ] ||
10812                 error "expected parent: $FID0/foo1, got: $parent"
10813
10814         # 2) test for a file with nlink > 1 (multiple parents)
10815         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
10816         echo "$parent" | grep -F "$FID1/$tfile" ||
10817                 error "$FID1/$tfile not returned in parent list"
10818         echo "$parent" | grep -F "$FID2/link" ||
10819                 error "$FID2/link not returned in parent list"
10820
10821         # 3) get parent by fid
10822         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
10823         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10824         echo "$parent" | grep -F "$FID1/$tfile" ||
10825                 error "$FID1/$tfile not returned in parent list (by fid)"
10826         echo "$parent" | grep -F "$FID2/link" ||
10827                 error "$FID2/link not returned in parent list (by fid)"
10828
10829         # 4) test for entry in root directory
10830         parent=$($LFS path2fid --parents $DIR/f)
10831         echo "$parent" | grep -F "$FID3/f" ||
10832                 error "$FID3/f not returned in parent list"
10833
10834         # 5) test it on root directory
10835         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
10836                 error "$MOUNT should not have parents"
10837
10838         # enable xattr caching and check that linkea is correctly updated
10839         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10840         save_lustre_params client "llite.*.xattr_cache" > $save
10841         lctl set_param llite.*.xattr_cache 1
10842
10843         # 6.1) linkea update on rename
10844         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
10845
10846         # get parents by fid
10847         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10848         # foo1 should no longer be returned in parent list
10849         echo "$parent" | grep -F "$FID1" &&
10850                 error "$FID1 should no longer be in parent list"
10851         # the new path should appear
10852         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
10853                 error "$FID2/$tfile.moved is not in parent list"
10854
10855         # 6.2) linkea update on unlink
10856         rm -f $DIR/$tdir/d/foo2/link
10857         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
10858         # foo2/link should no longer be returned in parent list
10859         echo "$parent" | grep -F "$FID2/link" &&
10860                 error "$FID2/link should no longer be in parent list"
10861         true
10862
10863         rm -f $DIR/f
10864         restore_lustre_params < $save
10865         rm -f $save
10866 }
10867 run_test 154f "get parent fids by reading link ea"
10868
10869 test_154g()
10870 {
10871         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
10872            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
10873                 { skip "Need MDS version at least 2.6.92"; return 0; }
10874         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10875
10876         mkdir -p $DIR/$tdir
10877         llapi_fid_test -d $DIR/$tdir
10878 }
10879 run_test 154g "various llapi FID tests"
10880
10881 test_155_small_load() {
10882     local temp=$TMP/$tfile
10883     local file=$DIR/$tfile
10884
10885     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
10886         error "dd of=$temp bs=6096 count=1 failed"
10887     cp $temp $file
10888     cancel_lru_locks osc
10889     cmp $temp $file || error "$temp $file differ"
10890
10891     $TRUNCATE $temp 6000
10892     $TRUNCATE $file 6000
10893     cmp $temp $file || error "$temp $file differ (truncate1)"
10894
10895     echo "12345" >>$temp
10896     echo "12345" >>$file
10897     cmp $temp $file || error "$temp $file differ (append1)"
10898
10899     echo "12345" >>$temp
10900     echo "12345" >>$file
10901     cmp $temp $file || error "$temp $file differ (append2)"
10902
10903     rm -f $temp $file
10904     true
10905 }
10906
10907 test_155_big_load() {
10908     remote_ost_nodsh && skip "remote OST with nodsh" && return
10909     local temp=$TMP/$tfile
10910     local file=$DIR/$tfile
10911
10912     free_min_max
10913     local cache_size=$(do_facet ost$((MAXI+1)) \
10914         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
10915     local large_file_size=$((cache_size * 2))
10916
10917     echo "OSS cache size: $cache_size KB"
10918     echo "Large file size: $large_file_size KB"
10919
10920     [ $MAXV -le $large_file_size ] && \
10921         skip_env "max available OST size needs > $large_file_size KB" && \
10922         return 0
10923
10924     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
10925
10926     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
10927         error "dd of=$temp bs=$large_file_size count=1k failed"
10928     cp $temp $file
10929     ls -lh $temp $file
10930     cancel_lru_locks osc
10931     cmp $temp $file || error "$temp $file differ"
10932
10933     rm -f $temp $file
10934     true
10935 }
10936
10937 save_writethrough() {
10938         local facets=$(get_facets OST)
10939
10940         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
10941         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
10942 }
10943
10944 test_155a() {
10945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10946         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10947         save_writethrough $p
10948
10949         set_cache read on
10950         set_cache writethrough on
10951         test_155_small_load
10952         restore_lustre_params < $p
10953         rm -f $p
10954 }
10955 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
10956
10957 test_155b() {
10958         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10959         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10960         save_writethrough $p
10961
10962         set_cache read on
10963         set_cache writethrough off
10964         test_155_small_load
10965         restore_lustre_params < $p
10966         rm -f $p
10967 }
10968 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
10969
10970 test_155c() {
10971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10972         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10973         save_writethrough $p
10974
10975         set_cache read off
10976         set_cache writethrough on
10977         test_155_small_load
10978         restore_lustre_params < $p
10979         rm -f $p
10980 }
10981 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
10982
10983 test_155d() {
10984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10985         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10986         save_writethrough $p
10987
10988         set_cache read off
10989         set_cache writethrough off
10990         test_155_small_load
10991         restore_lustre_params < $p
10992         rm -f $p
10993 }
10994 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
10995
10996 test_155e() {
10997         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10998         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10999         save_writethrough $p
11000
11001         set_cache read on
11002         set_cache writethrough on
11003         test_155_big_load
11004         restore_lustre_params < $p
11005         rm -f $p
11006 }
11007 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11008
11009 test_155f() {
11010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11011         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11012         save_writethrough $p
11013
11014         set_cache read on
11015         set_cache writethrough off
11016         test_155_big_load
11017         restore_lustre_params < $p
11018         rm -f $p
11019 }
11020 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11021
11022 test_155g() {
11023         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11024         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11025         save_writethrough $p
11026
11027         set_cache read off
11028         set_cache writethrough on
11029         test_155_big_load
11030         restore_lustre_params < $p
11031         rm -f $p
11032 }
11033 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11034
11035 test_155h() {
11036         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11037         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11038         save_writethrough $p
11039
11040         set_cache read off
11041         set_cache writethrough off
11042         test_155_big_load
11043         restore_lustre_params < $p
11044         rm -f $p
11045 }
11046 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11047
11048 test_156() {
11049         remote_ost_nodsh && skip "remote OST with nodsh" && return
11050         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11051         local CPAGES=3
11052         local BEFORE
11053         local AFTER
11054         local file="$DIR/$tfile"
11055         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11056
11057         [ "$(facet_fstype ost1)" = "zfs" -a \
11058            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11059                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
11060                 return
11061
11062         save_writethrough $p
11063         roc_hit_init
11064
11065         log "Turn on read and write cache"
11066         set_cache read on
11067         set_cache writethrough on
11068
11069         log "Write data and read it back."
11070         log "Read should be satisfied from the cache."
11071         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11072         BEFORE=$(roc_hit)
11073         cancel_lru_locks osc
11074         cat $file >/dev/null
11075         AFTER=$(roc_hit)
11076         if ! let "AFTER - BEFORE == CPAGES"; then
11077                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11078         else
11079                 log "cache hits:: before: $BEFORE, after: $AFTER"
11080         fi
11081
11082         log "Read again; it should be satisfied from the cache."
11083         BEFORE=$AFTER
11084         cancel_lru_locks osc
11085         cat $file >/dev/null
11086         AFTER=$(roc_hit)
11087         if ! let "AFTER - BEFORE == CPAGES"; then
11088                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11089         else
11090                 log "cache hits:: before: $BEFORE, after: $AFTER"
11091         fi
11092
11093         log "Turn off the read cache and turn on the write cache"
11094         set_cache read off
11095         set_cache writethrough on
11096
11097         log "Read again; it should be satisfied from the cache."
11098         BEFORE=$(roc_hit)
11099         cancel_lru_locks osc
11100         cat $file >/dev/null
11101         AFTER=$(roc_hit)
11102         if ! let "AFTER - BEFORE == CPAGES"; then
11103                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11104         else
11105                 log "cache hits:: before: $BEFORE, after: $AFTER"
11106         fi
11107
11108         log "Read again; it should not be satisfied from the cache."
11109         BEFORE=$AFTER
11110         cancel_lru_locks osc
11111         cat $file >/dev/null
11112         AFTER=$(roc_hit)
11113         if ! let "AFTER - BEFORE == 0"; then
11114                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11115         else
11116                 log "cache hits:: before: $BEFORE, after: $AFTER"
11117         fi
11118
11119         log "Write data and read it back."
11120         log "Read should be satisfied from the cache."
11121         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11122         BEFORE=$(roc_hit)
11123         cancel_lru_locks osc
11124         cat $file >/dev/null
11125         AFTER=$(roc_hit)
11126         if ! let "AFTER - BEFORE == CPAGES"; then
11127                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11128         else
11129                 log "cache hits:: before: $BEFORE, after: $AFTER"
11130         fi
11131
11132         log "Read again; it should not be satisfied from the cache."
11133         BEFORE=$AFTER
11134         cancel_lru_locks osc
11135         cat $file >/dev/null
11136         AFTER=$(roc_hit)
11137         if ! let "AFTER - BEFORE == 0"; then
11138                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11139         else
11140                 log "cache hits:: before: $BEFORE, after: $AFTER"
11141         fi
11142
11143         log "Turn off read and write cache"
11144         set_cache read off
11145         set_cache writethrough off
11146
11147         log "Write data and read it back"
11148         log "It should not be satisfied from the cache."
11149         rm -f $file
11150         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11151         cancel_lru_locks osc
11152         BEFORE=$(roc_hit)
11153         cat $file >/dev/null
11154         AFTER=$(roc_hit)
11155         if ! let "AFTER - BEFORE == 0"; then
11156                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11157         else
11158                 log "cache hits:: before: $BEFORE, after: $AFTER"
11159         fi
11160
11161         log "Turn on the read cache and turn off the write cache"
11162         set_cache read on
11163         set_cache writethrough off
11164
11165         log "Write data and read it back"
11166         log "It should not be satisfied from the cache."
11167         rm -f $file
11168         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11169         BEFORE=$(roc_hit)
11170         cancel_lru_locks osc
11171         cat $file >/dev/null
11172         AFTER=$(roc_hit)
11173         if ! let "AFTER - BEFORE == 0"; then
11174                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11175         else
11176                 log "cache hits:: before: $BEFORE, after: $AFTER"
11177         fi
11178
11179         log "Read again; it should be satisfied from the cache."
11180         BEFORE=$(roc_hit)
11181         cancel_lru_locks osc
11182         cat $file >/dev/null
11183         AFTER=$(roc_hit)
11184         if ! let "AFTER - BEFORE == CPAGES"; then
11185                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11186         else
11187                 log "cache hits:: before: $BEFORE, after: $AFTER"
11188         fi
11189
11190         rm -f $file
11191         restore_lustre_params < $p
11192         rm -f $p
11193 }
11194 run_test 156 "Verification of tunables"
11195
11196 #Changelogs
11197 cleanup_changelog () {
11198         trap 0
11199         echo "Deregistering changelog client $CL_USER"
11200         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11201 }
11202
11203 err17935 () {
11204         if [[ $MDSCOUNT -gt 1 ]]; then
11205                 error_ignore bz17935 $*
11206         else
11207                 error $*
11208         fi
11209 }
11210
11211 changelog_chmask()
11212 {
11213         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11214
11215         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
11216
11217         if [ $MASK -eq 1 ]; then
11218                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
11219         else
11220                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
11221         fi
11222 }
11223
11224 changelog_extract_field() {
11225         local mdt=$1
11226         local cltype=$2
11227         local file=$3
11228         local identifier=$4
11229
11230         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
11231                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
11232                 tail -1
11233 }
11234
11235 test_160a() {
11236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11237         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11238         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11239                 { skip "Need MDS version at least 2.2.0"; return; }
11240
11241         local CL_USERS="mdd.$MDT0.changelog_users"
11242         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11243         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11244                 changelog_register -n)
11245         echo "Registered as changelog user $CL_USER"
11246         trap cleanup_changelog EXIT
11247         $GET_CL_USERS | grep -q $CL_USER ||
11248                 error "User $CL_USER not found in changelog_users"
11249
11250         # change something
11251         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11252         touch $DIR/$tdir/pics/2008/zachy/timestamp
11253         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11254         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11255         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11256         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11257         rm $DIR/$tdir/pics/desktop.jpg
11258
11259         $LFS changelog $MDT0 | tail -5
11260
11261         echo "verifying changelog mask"
11262         changelog_chmask "MKDIR"
11263         changelog_chmask "CLOSE"
11264
11265         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11266         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11267
11268         changelog_chmask "MKDIR"
11269         changelog_chmask "CLOSE"
11270
11271         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11272         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11273
11274         $LFS changelog $MDT0
11275         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11276         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11277         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11278         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11279
11280         # verify contents
11281         echo "verifying target fid"
11282         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11283         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11284         [ "$fidc" == "$fidf" ] ||
11285                 err17935 "fid in changelog $fidc != file fid $fidf"
11286         echo "verifying parent fid"
11287         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11288         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11289         [ "$fidc" == "$fidf" ] ||
11290                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11291
11292         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11293         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11294         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11295         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11296         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11297                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11298
11299         MIN_REC=$($GET_CL_USERS |
11300                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11301         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11302         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11303         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11304                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11305
11306         # LU-3446 changelog index reset on MDT restart
11307         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11308         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11309         $LFS changelog_clear $MDT0 $CL_USER 0
11310         stop $SINGLEMDS || error "Fail to stop MDT."
11311         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11312         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11313         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11314         [ $CUR_REC1 == $CUR_REC2 ] ||
11315                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11316
11317         echo "verifying user deregister"
11318         cleanup_changelog
11319         $GET_CL_USERS | grep -q $CL_USER &&
11320                 error "User $CL_USER still in changelog_users"
11321
11322         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11323         if [ $CL_USER -eq 0 ]; then
11324                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11325                 touch $DIR/$tdir/chloe
11326                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11327                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11328                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11329         else
11330                 echo "$CL_USER other changelog users; can't verify off"
11331         fi
11332 }
11333 run_test 160a "changelog sanity"
11334
11335 test_160b() { # LU-3587
11336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11337         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11338         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11339                 { skip "Need MDS version at least 2.2.0"; return; }
11340
11341         local CL_USERS="mdd.$MDT0.changelog_users"
11342         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11343         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11344                 changelog_register -n)
11345         echo "Registered as changelog user $CL_USER"
11346         trap cleanup_changelog EXIT
11347         $GET_CL_USERS | grep -q $CL_USER ||
11348                 error "User $CL_USER not found in changelog_users"
11349
11350         local LONGNAME1=$(str_repeat a 255)
11351         local LONGNAME2=$(str_repeat b 255)
11352
11353         cd $DIR
11354         echo "creating very long named file"
11355         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11356         echo "moving very long named file"
11357         mv $LONGNAME1 $LONGNAME2
11358
11359         $LFS changelog $MDT0 | grep RENME
11360         rm -f $LONGNAME2
11361         cleanup_changelog
11362 }
11363 run_test 160b "Verify that very long rename doesn't crash in changelog"
11364
11365 test_160c() {
11366         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11367
11368         local rc=0
11369         local server_version=$(lustre_version_code $SINGLEMDS)
11370
11371         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11372                 [[ $server_version -gt $(version_code 2.5.1) &&
11373                    $server_version -lt $(version_code 2.5.50) ]] ||
11374                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11375         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11376
11377         # Registration step
11378         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11379                 changelog_register -n)
11380         trap cleanup_changelog EXIT
11381
11382         rm -rf $DIR/$tdir
11383         mkdir -p $DIR/$tdir
11384         $MCREATE $DIR/$tdir/foo_160c
11385         changelog_chmask "TRUNC"
11386         $TRUNCATE $DIR/$tdir/foo_160c 200
11387         changelog_chmask "TRUNC"
11388         $TRUNCATE $DIR/$tdir/foo_160c 199
11389         $LFS changelog $MDT0
11390         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11391         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11392         $LFS changelog_clear $MDT0 $CL_USER 0
11393
11394         # Deregistration step
11395         cleanup_changelog
11396 }
11397 run_test 160c "verify that changelog log catch the truncate event"
11398
11399 test_160d() {
11400         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11401         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11402
11403         local server_version=$(lustre_version_code mds1)
11404         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11405
11406         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11407                 { skip "Need MDS version at least 2.7.60+"; return; }
11408         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11409
11410         # Registration step
11411         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11412                 changelog_register -n)
11413
11414         trap cleanup_changelog EXIT
11415         mkdir -p $DIR/$tdir/migrate_dir
11416         $LFS changelog_clear $MDT0 $CL_USER 0
11417
11418         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11419         $LFS changelog $MDT0
11420         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11421         $LFS changelog_clear $MDT0 $CL_USER 0
11422         [ $MIGRATES -eq 1 ] ||
11423                 error "MIGRATE changelog mask count $MIGRATES != 1"
11424
11425         # Deregistration step
11426         cleanup_changelog
11427 }
11428 run_test 160d "verify that changelog log catch the migrate event"
11429
11430 test_160e() {
11431         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11432
11433         # Create a user
11434         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11435                 changelog_register -n)
11436         echo "Registered as changelog user $CL_USER"
11437         trap cleanup_changelog EXIT
11438
11439         # Delete a future user (expect fail)
11440         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11441         local rc=$?
11442
11443         if [ $rc -eq 0 ]; then
11444                 error "Deleted non-existant user cl77"
11445         elif [ $rc -ne 2 ]; then
11446                 error "changelog_deregister failed with $rc, " \
11447                         "expected 2 (ENOENT)"
11448         fi
11449
11450         # Clear to a bad index (1 billion should be safe)
11451         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11452         rc=$?
11453
11454         if [ $rc -eq 0 ]; then
11455                 error "Successfully cleared to invalid CL index"
11456         elif [ $rc -ne 22 ]; then
11457                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11458         fi
11459 }
11460 run_test 160e "changelog negative testing"
11461
11462 test_161a() {
11463         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11464         test_mkdir -p -c1 $DIR/$tdir
11465         cp /etc/hosts $DIR/$tdir/$tfile
11466         test_mkdir -c1 $DIR/$tdir/foo1
11467         test_mkdir -c1 $DIR/$tdir/foo2
11468         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11469         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11470         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11471         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11472         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11473         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11474                 $LFS fid2path $DIR $FID
11475                 err17935 "bad link ea"
11476         fi
11477     # middle
11478     rm $DIR/$tdir/foo2/zachary
11479     # last
11480     rm $DIR/$tdir/foo2/thor
11481     # first
11482     rm $DIR/$tdir/$tfile
11483     # rename
11484     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11485     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11486         then
11487         $LFS fid2path $DIR $FID
11488         err17935 "bad link rename"
11489     fi
11490     rm $DIR/$tdir/foo2/maggie
11491
11492         # overflow the EA
11493         local longname=filename_avg_len_is_thirty_two_
11494         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11495                 error "failed to hardlink many files"
11496         links=$($LFS fid2path $DIR $FID | wc -l)
11497         echo -n "${links}/1000 links in link EA"
11498         [[ $links -gt 60 ]] ||
11499                 err17935 "expected at least 60 links in link EA"
11500         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11501                 error "failed to unlink many hardlinks"
11502 }
11503 run_test 161a "link ea sanity"
11504
11505 test_161b() {
11506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11507         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11508         local MDTIDX=1
11509         local remote_dir=$DIR/$tdir/remote_dir
11510
11511         mkdir -p $DIR/$tdir
11512         $LFS mkdir -i $MDTIDX $remote_dir ||
11513                 error "create remote directory failed"
11514
11515         cp /etc/hosts $remote_dir/$tfile
11516         mkdir -p $remote_dir/foo1
11517         mkdir -p $remote_dir/foo2
11518         ln $remote_dir/$tfile $remote_dir/foo1/sofia
11519         ln $remote_dir/$tfile $remote_dir/foo2/zachary
11520         ln $remote_dir/$tfile $remote_dir/foo1/luna
11521         ln $remote_dir/$tfile $remote_dir/foo2/thor
11522
11523         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
11524                      tr -d ']')
11525         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11526                 $LFS fid2path $DIR $FID
11527                 err17935 "bad link ea"
11528         fi
11529         # middle
11530         rm $remote_dir/foo2/zachary
11531         # last
11532         rm $remote_dir/foo2/thor
11533         # first
11534         rm $remote_dir/$tfile
11535         # rename
11536         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
11537         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
11538         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
11539                 $LFS fid2path $DIR $FID
11540                 err17935 "bad link rename"
11541         fi
11542         rm $remote_dir/foo2/maggie
11543
11544         # overflow the EA
11545         local longname=filename_avg_len_is_thirty_two_
11546         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
11547                 error "failed to hardlink many files"
11548         links=$($LFS fid2path $DIR $FID | wc -l)
11549         echo -n "${links}/1000 links in link EA"
11550         [[ ${links} -gt 60 ]] ||
11551                 err17935 "expected at least 60 links in link EA"
11552         unlinkmany $remote_dir/foo2/$longname 1000 ||
11553         error "failed to unlink many hardlinks"
11554 }
11555 run_test 161b "link ea sanity under remote directory"
11556
11557 test_161c() {
11558         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11560         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
11561                 skip "Need MDS version at least 2.1.5" && return
11562
11563         # define CLF_RENAME_LAST 0x0001
11564         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
11565         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11566                 changelog_register -n)
11567
11568         trap cleanup_changelog EXIT
11569         rm -rf $DIR/$tdir
11570         mkdir -p $DIR/$tdir
11571         touch $DIR/$tdir/foo_161c
11572         touch $DIR/$tdir/bar_161c
11573         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11574         $LFS changelog $MDT0 | grep RENME
11575         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
11576                 cut -f5 -d' ')
11577         $LFS changelog_clear $MDT0 $CL_USER 0
11578         if [ x$flags != "x0x1" ]; then
11579                 error "flag $flags is not 0x1"
11580         fi
11581         echo "rename overwrite a target having nlink = 1," \
11582                 "changelog record has flags of $flags"
11583
11584         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
11585         touch $DIR/$tdir/foo_161c
11586         touch $DIR/$tdir/bar_161c
11587         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11588         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
11589         $LFS changelog $MDT0 | grep RENME
11590         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11591         $LFS changelog_clear $MDT0 $CL_USER 0
11592         if [ x$flags != "x0x0" ]; then
11593                 error "flag $flags is not 0x0"
11594         fi
11595         echo "rename overwrite a target having nlink > 1," \
11596                 "changelog record has flags of $flags"
11597
11598         # rename doesn't overwrite a target (changelog flag 0x0)
11599         touch $DIR/$tdir/foo_161c
11600         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
11601         $LFS changelog $MDT0 | grep RENME
11602         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
11603         $LFS changelog_clear $MDT0 $CL_USER 0
11604         if [ x$flags != "x0x0" ]; then
11605                 error "flag $flags is not 0x0"
11606         fi
11607         echo "rename doesn't overwrite a target," \
11608                 "changelog record has flags of $flags"
11609
11610         # define CLF_UNLINK_LAST 0x0001
11611         # unlink a file having nlink = 1 (changelog flag 0x1)
11612         rm -f $DIR/$tdir/foo2_161c
11613         $LFS changelog $MDT0 | grep UNLNK
11614         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11615         $LFS changelog_clear $MDT0 $CL_USER 0
11616         if [ x$flags != "x0x1" ]; then
11617                 error "flag $flags is not 0x1"
11618         fi
11619         echo "unlink a file having nlink = 1," \
11620                 "changelog record has flags of $flags"
11621
11622         # unlink a file having nlink > 1 (changelog flag 0x0)
11623         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
11624         rm -f $DIR/$tdir/foobar_161c
11625         $LFS changelog $MDT0 | grep UNLNK
11626         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
11627         $LFS changelog_clear $MDT0 $CL_USER 0
11628         if [ x$flags != "x0x0" ]; then
11629                 error "flag $flags is not 0x0"
11630         fi
11631         echo "unlink a file having nlink > 1," \
11632                 "changelog record has flags of $flags"
11633         cleanup_changelog
11634 }
11635 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
11636
11637 test_161d() {
11638         local user
11639         local pid
11640         local fid
11641
11642         # cleanup previous run
11643         rm -rf $DIR/$tdir/$tfile
11644
11645         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11646                 changelog_register -n)
11647         [[ $? -eq 0 ]] || error "changelog_register failed"
11648
11649         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
11650         # interfer with $MOUNT/.lustre/fid/ access
11651         mkdir $DIR/$tdir
11652         [[ $? -eq 0 ]] || error "mkdir failed"
11653
11654         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
11655         $LCTL set_param fail_loc=0x8000140c
11656         # 5s pause
11657         $LCTL set_param fail_val=5
11658
11659         # create file
11660         echo foofoo > $DIR/$tdir/$tfile &
11661         pid=$!
11662
11663         # wait for create to be delayed
11664         sleep 2
11665
11666         ps -p $pid
11667         [[ $? -eq 0 ]] || error "create should be blocked"
11668
11669         local tempfile=$(mktemp)
11670         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
11671         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
11672         # some delay may occur during ChangeLog publishing and file read just
11673         # above, that could allow file write to happen finally
11674         [[ -s $tempfile ]] && echo "file should be empty"
11675
11676         $LCTL set_param fail_loc=0
11677
11678         wait $pid
11679         [[ $? -eq 0 ]] || error "create failed"
11680
11681         $LFS changelog_clear $MDT0 $user 0
11682         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
11683 }
11684 run_test 161d "create with concurrent .lustre/fid access"
11685
11686 check_path() {
11687     local expected=$1
11688     shift
11689     local fid=$2
11690
11691     local path=$(${LFS} fid2path $*)
11692     # Remove the '//' indicating a remote directory
11693     path=$(echo $path | sed 's#//#/#g')
11694     RC=$?
11695
11696     if [ $RC -ne 0 ]; then
11697         err17935 "path looked up of $expected failed. Error $RC"
11698         return $RC
11699     elif [ "${path}" != "${expected}" ]; then
11700         err17935 "path looked up \"${path}\" instead of \"${expected}\""
11701         return 2
11702     fi
11703     echo "fid $fid resolves to path $path (expected $expected)"
11704 }
11705
11706 test_162a() { # was test_162
11707         # Make changes to filesystem
11708         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11709         test_mkdir -p -c1 $DIR/$tdir/d2
11710         touch $DIR/$tdir/d2/$tfile
11711         touch $DIR/$tdir/d2/x1
11712         touch $DIR/$tdir/d2/x2
11713         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
11714         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
11715         # regular file
11716         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
11717         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
11718                 error "check path $tdir/d2/$tfile failed"
11719
11720         # softlink
11721         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
11722         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
11723         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
11724                 error "check path $tdir/d2/p/q/r/slink failed"
11725
11726         # softlink to wrong file
11727         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
11728         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
11729         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
11730                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
11731
11732         # hardlink
11733         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
11734         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
11735         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
11736         # fid2path dir/fsname should both work
11737         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
11738                 error "check path $tdir/d2/a/b/c/new_file failed"
11739         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
11740                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
11741
11742         # hardlink count: check that there are 2 links
11743         # Doesnt work with CMD yet: 17935
11744         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
11745                 err17935 "expected 2 links"
11746
11747         # hardlink indexing: remove the first link
11748         rm $DIR/$tdir/d2/p/q/r/hlink
11749         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
11750                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
11751
11752         return 0
11753 }
11754 run_test 162a "path lookup sanity"
11755
11756 test_162b() {
11757         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11758         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11759
11760         mkdir $DIR/$tdir
11761         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
11762                                 error "create striped dir failed"
11763
11764         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
11765                                         tail -n 1 | awk '{print $2}')
11766         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
11767
11768         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
11769         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
11770
11771         # regular file
11772         for ((i=0;i<5;i++)); do
11773                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
11774                         error "get fid for f$i failed"
11775                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
11776                         error "check path $tdir/striped_dir/f$i failed"
11777
11778                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
11779                         error "get fid for d$i failed"
11780                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
11781                         error "check path $tdir/striped_dir/d$i failed"
11782         done
11783
11784         return 0
11785 }
11786 run_test 162b "striped directory path lookup sanity"
11787
11788 # LU-4239: Verify fid2path works with paths 100 or more directories deep
11789 test_162c() {
11790         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
11791                 skip "Need MDS version at least 2.7.51" && return
11792         test_mkdir $DIR/$tdir.local
11793         test_mkdir $DIR/$tdir.remote
11794         local lpath=$tdir.local
11795         local rpath=$tdir.remote
11796
11797         for ((i = 0; i <= 101; i++)); do
11798                 lpath="$lpath/$i"
11799                 mkdir $DIR/$lpath
11800                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
11801                         error "get fid for local directory $DIR/$lpath failed"
11802                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
11803                         error "check path for local directory $DIR/$lpath failed"
11804
11805                 rpath="$rpath/$i"
11806                 test_mkdir $DIR/$rpath
11807                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
11808                         error "get fid for remote directory $DIR/$rpath failed"
11809                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
11810                         error "check path for remote directory $DIR/$rpath failed"
11811         done
11812
11813         return 0
11814 }
11815 run_test 162c "fid2path works with paths 100 or more directories deep"
11816
11817 test_169() {
11818         # do directio so as not to populate the page cache
11819         log "creating a 10 Mb file"
11820         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
11821         log "starting reads"
11822         dd if=$DIR/$tfile of=/dev/null bs=4096 &
11823         log "truncating the file"
11824         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
11825         log "killing dd"
11826         kill %+ || true # reads might have finished
11827         echo "wait until dd is finished"
11828         wait
11829         log "removing the temporary file"
11830         rm -rf $DIR/$tfile || error "tmp file removal failed"
11831 }
11832 run_test 169 "parallel read and truncate should not deadlock"
11833
11834 test_170() {
11835         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11836         $LCTL clear     # bug 18514
11837         $LCTL debug_daemon start $TMP/${tfile}_log_good
11838         touch $DIR/$tfile
11839         $LCTL debug_daemon stop
11840         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
11841                error "sed failed to read log_good"
11842
11843         $LCTL debug_daemon start $TMP/${tfile}_log_good
11844         rm -rf $DIR/$tfile
11845         $LCTL debug_daemon stop
11846
11847         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
11848                error "lctl df log_bad failed"
11849
11850         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11851         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11852
11853         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
11854         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
11855
11856         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
11857                 error "bad_line good_line1 good_line2 are empty"
11858
11859         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11860         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
11861         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
11862
11863         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
11864         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
11865         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
11866
11867         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
11868                 error "bad_line_new good_line_new are empty"
11869
11870         local expected_good=$((good_line1 + good_line2*2))
11871
11872         rm -f $TMP/${tfile}*
11873         # LU-231, short malformed line may not be counted into bad lines
11874         if [ $bad_line -ne $bad_line_new ] &&
11875                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
11876                 error "expected $bad_line bad lines, but got $bad_line_new"
11877                 return 1
11878         fi
11879
11880         if [ $expected_good -ne $good_line_new ]; then
11881                 error "expected $expected_good good lines, but got $good_line_new"
11882                 return 2
11883         fi
11884         true
11885 }
11886 run_test 170 "test lctl df to handle corrupted log ====================="
11887
11888 test_171() { # bug20592
11889         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11890 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
11891         $LCTL set_param fail_loc=0x50e
11892         $LCTL set_param fail_val=3000
11893         multiop_bg_pause $DIR/$tfile O_s || true
11894         local MULTIPID=$!
11895         kill -USR1 $MULTIPID
11896         # cause log dump
11897         sleep 3
11898         wait $MULTIPID
11899         if dmesg | grep "recursive fault"; then
11900                 error "caught a recursive fault"
11901         fi
11902         $LCTL set_param fail_loc=0
11903         true
11904 }
11905 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
11906
11907 # it would be good to share it with obdfilter-survey/iokit-libecho code
11908 setup_obdecho_osc () {
11909         local rc=0
11910         local ost_nid=$1
11911         local obdfilter_name=$2
11912         echo "Creating new osc for $obdfilter_name on $ost_nid"
11913         # make sure we can find loopback nid
11914         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
11915
11916         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
11917                            ${obdfilter_name}_osc_UUID || rc=2; }
11918         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
11919                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
11920         return $rc
11921 }
11922
11923 cleanup_obdecho_osc () {
11924         local obdfilter_name=$1
11925         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
11926         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
11927         return 0
11928 }
11929
11930 obdecho_test() {
11931         local OBD=$1
11932         local node=$2
11933         local pages=${3:-64}
11934         local rc=0
11935         local id
11936
11937         local count=10
11938         local obd_size=$(get_obd_size $node $OBD)
11939         local page_size=$(get_page_size $node)
11940         if [[ -n "$obd_size" ]]; then
11941                 local new_count=$((obd_size / (pages * page_size / 1024)))
11942                 [[ $new_count -ge $count ]] || count=$new_count
11943         fi
11944
11945         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
11946         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
11947                            rc=2; }
11948         if [ $rc -eq 0 ]; then
11949             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
11950             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
11951         fi
11952         echo "New object id is $id"
11953         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
11954                            rc=4; }
11955         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
11956                            "test_brw $count w v $pages $id" || rc=4; }
11957         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
11958                            rc=4; }
11959         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
11960                                         "cleanup" || rc=5; }
11961         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
11962                                         "detach" || rc=6; }
11963         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
11964         return $rc
11965 }
11966
11967 test_180a() {
11968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11969         remote_ost_nodsh && skip "remote OST with nodsh" && return
11970         local rc=0
11971         local rmmod_local=0
11972
11973         if ! module_loaded obdecho; then
11974             load_module obdecho/obdecho
11975             rmmod_local=1
11976         fi
11977
11978         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
11979         local host=$(lctl get_param -n osc.$osc.import |
11980                              awk '/current_connection:/ {print $2}' )
11981         local target=$(lctl get_param -n osc.$osc.import |
11982                              awk '/target:/ {print $2}' )
11983         target=${target%_UUID}
11984
11985         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
11986         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
11987         [[ -n $target ]] && cleanup_obdecho_osc $target
11988         [ $rmmod_local -eq 1 ] && rmmod obdecho
11989         return $rc
11990 }
11991 run_test 180a "test obdecho on osc"
11992
11993 test_180b() {
11994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11995         remote_ost_nodsh && skip "remote OST with nodsh" && return
11996         local rc=0
11997         local rmmod_remote=0
11998
11999         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12000                 rmmod_remote=true || error "failed to load module obdecho"
12001         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
12002         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
12003         $rmmod_remote && do_facet ost1 "rmmod obdecho"
12004         return $rc
12005 }
12006 run_test 180b "test obdecho directly on obdfilter"
12007
12008 test_180c() { # LU-2598
12009         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12010         remote_ost_nodsh && skip "remote OST with nodsh" && return
12011         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
12012                 skip "Need MDS version at least 2.4.0" && return
12013
12014         local rc=0
12015         local rmmod_remote=false
12016         local pages=16384 # 64MB bulk I/O RPC size
12017         local target
12018
12019         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12020                 rmmod_remote=true || error "failed to load module obdecho"
12021
12022         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
12023                 head -n1)
12024         if [ -n "$target" ]; then
12025                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
12026         else
12027                 echo "there is no obdfilter target on ost1"
12028                 rc=2
12029         fi
12030         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
12031         return $rc
12032 }
12033 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
12034
12035 test_181() { # bug 22177
12036         test_mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12037         # create enough files to index the directory
12038         createmany -o $DIR/$tdir/foobar 4000
12039         # print attributes for debug purpose
12040         lsattr -d .
12041         # open dir
12042         multiop_bg_pause $DIR/$tdir D_Sc || return 1
12043         MULTIPID=$!
12044         # remove the files & current working dir
12045         unlinkmany $DIR/$tdir/foobar 4000
12046         rmdir $DIR/$tdir
12047         kill -USR1 $MULTIPID
12048         wait $MULTIPID
12049         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
12050         return 0
12051 }
12052 run_test 181 "Test open-unlinked dir ========================"
12053
12054 test_182() {
12055         local fcount=1000
12056         local tcount=10
12057
12058         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12059
12060         $LCTL set_param mdc.*.rpc_stats=clear
12061
12062         for (( i = 0; i < $tcount; i++ )) ; do
12063                 mkdir $DIR/$tdir/$i
12064         done
12065
12066         for (( i = 0; i < $tcount; i++ )) ; do
12067                 createmany -o $DIR/$tdir/$i/f- $fcount &
12068         done
12069         wait
12070
12071         for (( i = 0; i < $tcount; i++ )) ; do
12072                 unlinkmany $DIR/$tdir/$i/f- $fcount &
12073         done
12074         wait
12075
12076         $LCTL get_param mdc.*.rpc_stats
12077
12078         rm -rf $DIR/$tdir
12079 }
12080 run_test 182 "Test parallel modify metadata operations ================"
12081
12082 test_183() { # LU-2275
12083         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12084         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
12085                 skip "Need MDS version at least 2.3.56" && return
12086
12087         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12088         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12089         echo aaa > $DIR/$tdir/$tfile
12090
12091 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
12092         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
12093
12094         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
12095         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
12096
12097         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
12098
12099         # Flush negative dentry cache
12100         touch $DIR/$tdir/$tfile
12101
12102         # We are not checking for any leaked references here, they'll
12103         # become evident next time we do cleanup with module unload.
12104         rm -rf $DIR/$tdir
12105 }
12106 run_test 183 "No crash or request leak in case of strange dispositions ========"
12107
12108 # test suite 184 is for LU-2016, LU-2017
12109 test_184a() {
12110         check_swap_layouts_support && return 0
12111
12112         dir0=$DIR/$tdir/$testnum
12113         test_mkdir -p -c1 $dir0 || error "creating dir $dir0"
12114         ref1=/etc/passwd
12115         ref2=/etc/group
12116         file1=$dir0/f1
12117         file2=$dir0/f2
12118         $SETSTRIPE -c1 $file1
12119         cp $ref1 $file1
12120         $SETSTRIPE -c2 $file2
12121         cp $ref2 $file2
12122         gen1=$($GETSTRIPE -g $file1)
12123         gen2=$($GETSTRIPE -g $file2)
12124
12125         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
12126         gen=$($GETSTRIPE -g $file1)
12127         [[ $gen1 != $gen ]] ||
12128                 "Layout generation on $file1 does not change"
12129         gen=$($GETSTRIPE -g $file2)
12130         [[ $gen2 != $gen ]] ||
12131                 "Layout generation on $file2 does not change"
12132
12133         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
12134         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12135 }
12136 run_test 184a "Basic layout swap"
12137
12138 test_184b() {
12139         check_swap_layouts_support && return 0
12140
12141         dir0=$DIR/$tdir/$testnum
12142         mkdir -p $dir0 || error "creating dir $dir0"
12143         file1=$dir0/f1
12144         file2=$dir0/f2
12145         file3=$dir0/f3
12146         dir1=$dir0/d1
12147         dir2=$dir0/d2
12148         mkdir $dir1 $dir2
12149         $SETSTRIPE -c1 $file1
12150         $SETSTRIPE -c2 $file2
12151         $SETSTRIPE -c1 $file3
12152         chown $RUNAS_ID $file3
12153         gen1=$($GETSTRIPE -g $file1)
12154         gen2=$($GETSTRIPE -g $file2)
12155
12156         $LFS swap_layouts $dir1 $dir2 &&
12157                 error "swap of directories layouts should fail"
12158         $LFS swap_layouts $dir1 $file1 &&
12159                 error "swap of directory and file layouts should fail"
12160         $RUNAS $LFS swap_layouts $file1 $file2 &&
12161                 error "swap of file we cannot write should fail"
12162         $LFS swap_layouts $file1 $file3 &&
12163                 error "swap of file with different owner should fail"
12164         /bin/true # to clear error code
12165 }
12166 run_test 184b "Forbidden layout swap (will generate errors)"
12167
12168 test_184c() {
12169         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
12170         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
12171         check_swap_layouts_support && return 0
12172
12173         local dir0=$DIR/$tdir/$testnum
12174         mkdir -p $dir0 || error "creating dir $dir0"
12175
12176         local ref1=$dir0/ref1
12177         local ref2=$dir0/ref2
12178         local file1=$dir0/file1
12179         local file2=$dir0/file2
12180         # create a file large enough for the concurrent test
12181         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
12182         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
12183         echo "ref file size: ref1($(stat -c %s $ref1))," \
12184              "ref2($(stat -c %s $ref2))"
12185
12186         cp $ref2 $file2
12187         dd if=$ref1 of=$file1 bs=16k &
12188         local DD_PID=$!
12189
12190         # Make sure dd starts to copy file
12191         while [ ! -f $file1 ]; do sleep 0.1; done
12192
12193         $LFS swap_layouts $file1 $file2
12194         local rc=$?
12195         wait $DD_PID
12196         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
12197         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
12198
12199         # how many bytes copied before swapping layout
12200         local copied=$(stat -c %s $file2)
12201         local remaining=$(stat -c %s $ref1)
12202         remaining=$((remaining - copied))
12203         echo "Copied $copied bytes before swapping layout..."
12204
12205         cmp -n $copied $file1 $ref2 | grep differ &&
12206                 error "Content mismatch [0, $copied) of ref2 and file1"
12207         cmp -n $copied $file2 $ref1 ||
12208                 error "Content mismatch [0, $copied) of ref1 and file2"
12209         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
12210                 error "Content mismatch [$copied, EOF) of ref1 and file1"
12211
12212         # clean up
12213         rm -f $ref1 $ref2 $file1 $file2
12214 }
12215 run_test 184c "Concurrent write and layout swap"
12216
12217 test_184d() {
12218         check_swap_layouts_support && return 0
12219         [ -z "$(which getfattr 2>/dev/null)" ] &&
12220                 skip "no getfattr command" && return 0
12221
12222         local file1=$DIR/$tdir/$tfile-1
12223         local file2=$DIR/$tdir/$tfile-2
12224         local file3=$DIR/$tdir/$tfile-3
12225         local lovea1
12226         local lovea2
12227
12228         mkdir -p $DIR/$tdir
12229         touch $file1 || error "create $file1 failed"
12230         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12231                 error "create $file2 failed"
12232         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12233                 error "create $file3 failed"
12234         lovea1=$(get_layout_param $file1)
12235
12236         $LFS swap_layouts $file2 $file3 ||
12237                 error "swap $file2 $file3 layouts failed"
12238         $LFS swap_layouts $file1 $file2 ||
12239                 error "swap $file1 $file2 layouts failed"
12240
12241         lovea2=$(get_layout_param $file2)
12242         echo "$lovea1"
12243         echo "$lovea2"
12244         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12245
12246         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12247         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12248 }
12249 run_test 184d "allow stripeless layouts swap"
12250
12251 test_184e() {
12252         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12253                 { skip "Need MDS version at least 2.6.94"; return 0; }
12254         check_swap_layouts_support && return 0
12255         [ -z "$(which getfattr 2>/dev/null)" ] &&
12256                 skip "no getfattr command" && return 0
12257
12258         local file1=$DIR/$tdir/$tfile-1
12259         local file2=$DIR/$tdir/$tfile-2
12260         local file3=$DIR/$tdir/$tfile-3
12261         local lovea
12262
12263         mkdir -p $DIR/$tdir
12264         touch $file1 || error "create $file1 failed"
12265         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12266                 error "create $file2 failed"
12267         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12268                 error "create $file3 failed"
12269
12270         $LFS swap_layouts $file1 $file2 ||
12271                 error "swap $file1 $file2 layouts failed"
12272
12273         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12274         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12275
12276         echo 123 > $file1 || error "Should be able to write into $file1"
12277
12278         $LFS swap_layouts $file1 $file3 ||
12279                 error "swap $file1 $file3 layouts failed"
12280
12281         echo 123 > $file1 || error "Should be able to write into $file1"
12282
12283         rm -rf $file1 $file2 $file3
12284 }
12285 run_test 184e "Recreate layout after stripeless layout swaps"
12286
12287 test_185() { # LU-2441
12288         # LU-3553 - no volatile file support in old servers
12289         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12290                 { skip "Need MDS version at least 2.3.60"; return 0; }
12291
12292         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12293         touch $DIR/$tdir/spoo
12294         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12295         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12296                 error "cannot create/write a volatile file"
12297         [ "$FILESET" == "" ] &&
12298         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12299                 error "FID is still valid after close"
12300
12301         multiop_bg_pause $DIR/$tdir vVw4096_c
12302         local multi_pid=$!
12303
12304         local OLD_IFS=$IFS
12305         IFS=":"
12306         local fidv=($fid)
12307         IFS=$OLD_IFS
12308         # assume that the next FID for this client is sequential, since stdout
12309         # is unfortunately eaten by multiop_bg_pause
12310         local n=$((${fidv[1]} + 1))
12311         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12312         if [ "$FILESET" == "" ]; then
12313                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12314                         error "FID is missing before close"
12315         fi
12316         kill -USR1 $multi_pid
12317         # 1 second delay, so if mtime change we will see it
12318         sleep 1
12319         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12320         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12321 }
12322 run_test 185 "Volatile file support"
12323
12324 test_187a() {
12325         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12326         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12327                 skip "Need MDS version at least 2.3.0" && return
12328
12329         local dir0=$DIR/$tdir/$testnum
12330         mkdir -p $dir0 || error "creating dir $dir0"
12331
12332         local file=$dir0/file1
12333         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12334         local dv1=$($LFS data_version $file)
12335         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12336         local dv2=$($LFS data_version $file)
12337         [[ $dv1 != $dv2 ]] ||
12338                 error "data version did not change on write $dv1 == $dv2"
12339
12340         # clean up
12341         rm -f $file1
12342 }
12343 run_test 187a "Test data version change"
12344
12345 test_187b() {
12346         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12347         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12348                 skip "Need MDS version at least 2.3.0" && return
12349
12350         local dir0=$DIR/$tdir/$testnum
12351         mkdir -p $dir0 || error "creating dir $dir0"
12352
12353         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12354         [[ ${DV[0]} != ${DV[1]} ]] ||
12355                 error "data version did not change on write"\
12356                       " ${DV[0]} == ${DV[1]}"
12357
12358         # clean up
12359         rm -f $file1
12360 }
12361 run_test 187b "Test data version change on volatile file"
12362
12363 test_200() {
12364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12365         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12366         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12367
12368         local POOL=${POOL:-cea1}
12369         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12370         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12371         # Pool OST targets
12372         local first_ost=0
12373         local last_ost=$(($OSTCOUNT - 1))
12374         local ost_step=2
12375         local ost_list=$(seq $first_ost $ost_step $last_ost)
12376         local ost_range="$first_ost $last_ost $ost_step"
12377         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12378         local file_dir=$POOL_ROOT/file_tst
12379         local subdir=$test_path/subdir
12380         local rc=0
12381
12382         if ! combined_mgs_mds ; then
12383                 mount_mgs_client
12384         fi
12385
12386         while : ; do
12387                 # former test_200a test_200b
12388                 pool_add $POOL                          || { rc=$? ; break; }
12389                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12390                 # former test_200c test_200d
12391                 mkdir -p $test_path
12392                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12393                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12394                 mkdir -p $subdir
12395                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12396                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12397                                                         || { rc=$? ; break; }
12398                 # former test_200e test_200f
12399                 local files=$((OSTCOUNT*3))
12400                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12401                                                         || { rc=$? ; break; }
12402                 pool_create_files $POOL $file_dir $files "$ost_list" \
12403                                                         || { rc=$? ; break; }
12404                 # former test_200g test_200h
12405                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12406                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12407
12408                 # former test_201a test_201b test_201c
12409                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12410
12411                 local f=$test_path/$tfile
12412                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12413                 pool_remove $POOL $f                    || { rc=$? ; break; }
12414                 break
12415         done
12416
12417         destroy_test_pools
12418
12419         if ! combined_mgs_mds ; then
12420                 umount_mgs_client
12421         fi
12422         return $rc
12423 }
12424 run_test 200 "OST pools"
12425
12426 # usage: default_attr <count | size | offset>
12427 default_attr() {
12428         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12429 }
12430
12431 # usage: check_default_stripe_attr
12432 check_default_stripe_attr() {
12433         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12434         case $1 in
12435         --stripe-count|-c)
12436                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12437         --stripe-size|-S)
12438                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12439         --stripe-index|-i)
12440                 EXPECTED=-1;;
12441         *)
12442                 error "unknown getstripe attr '$1'"
12443         esac
12444
12445         [ $ACTUAL != $EXPECTED ] &&
12446                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12447 }
12448
12449 test_204a() {
12450         test_mkdir -p $DIR/$tdir
12451         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12452
12453         check_default_stripe_attr --stripe-count
12454         check_default_stripe_attr --stripe-size
12455         check_default_stripe_attr --stripe-index
12456
12457         return 0
12458 }
12459 run_test 204a "Print default stripe attributes ================="
12460
12461 test_204b() {
12462         test_mkdir -p $DIR/$tdir
12463         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12464
12465         check_default_stripe_attr --stripe-size
12466         check_default_stripe_attr --stripe-index
12467
12468         return 0
12469 }
12470 run_test 204b "Print default stripe size and offset  ==========="
12471
12472 test_204c() {
12473         test_mkdir -p $DIR/$tdir
12474         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12475
12476         check_default_stripe_attr --stripe-count
12477         check_default_stripe_attr --stripe-index
12478
12479         return 0
12480 }
12481 run_test 204c "Print default stripe count and offset ==========="
12482
12483 test_204d() {
12484         test_mkdir -p $DIR/$tdir
12485         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12486
12487         check_default_stripe_attr --stripe-count
12488         check_default_stripe_attr --stripe-size
12489
12490         return 0
12491 }
12492 run_test 204d "Print default stripe count and size ============="
12493
12494 test_204e() {
12495         test_mkdir -p $DIR/$tdir
12496         $SETSTRIPE -d $DIR/$tdir
12497
12498         check_default_stripe_attr --stripe-count --raw
12499         check_default_stripe_attr --stripe-size --raw
12500         check_default_stripe_attr --stripe-index --raw
12501
12502         return 0
12503 }
12504 run_test 204e "Print raw stripe attributes ================="
12505
12506 test_204f() {
12507         test_mkdir -p $DIR/$tdir
12508         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12509
12510         check_default_stripe_attr --stripe-size --raw
12511         check_default_stripe_attr --stripe-index --raw
12512
12513         return 0
12514 }
12515 run_test 204f "Print raw stripe size and offset  ==========="
12516
12517 test_204g() {
12518         test_mkdir -p $DIR/$tdir
12519         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12520
12521         check_default_stripe_attr --stripe-count --raw
12522         check_default_stripe_attr --stripe-index --raw
12523
12524         return 0
12525 }
12526 run_test 204g "Print raw stripe count and offset ==========="
12527
12528 test_204h() {
12529         test_mkdir -p $DIR/$tdir
12530         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12531
12532         check_default_stripe_attr --stripe-count --raw
12533         check_default_stripe_attr --stripe-size --raw
12534
12535         return 0
12536 }
12537 run_test 204h "Print raw stripe count and size ============="
12538
12539 # Figure out which job scheduler is being used, if any,
12540 # or use a fake one
12541 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
12542         JOBENV=SLURM_JOB_ID
12543 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
12544         JOBENV=LSB_JOBID
12545 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
12546         JOBENV=PBS_JOBID
12547 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
12548         JOBENV=LOADL_STEP_ID
12549 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
12550         JOBENV=JOB_ID
12551 else
12552         $LCTL list_param jobid_name > /dev/null 2>&1
12553         if [ $? -eq 0 ]; then
12554                 JOBENV=nodelocal
12555         else
12556                 JOBENV=FAKE_JOBID
12557         fi
12558 fi
12559
12560 verify_jobstats() {
12561         local cmd=($1)
12562         shift
12563         local facets="$@"
12564
12565 # we don't really need to clear the stats for this test to work, since each
12566 # command has a unique jobid, but it makes debugging easier if needed.
12567 #       for facet in $facets; do
12568 #               local dev=$(convert_facet2label $facet)
12569 #               # clear old jobstats
12570 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
12571 #       done
12572
12573         # use a new JobID for each test, or we might see an old one
12574         [ "$JOBENV" = "FAKE_JOBID" ] &&
12575                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12576
12577         JOBVAL=${!JOBENV}
12578
12579         [ "$JOBENV" = "nodelocal" ] && {
12580                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
12581                 $LCTL set_param jobid_name=$FAKE_JOBID
12582                 JOBVAL=$FAKE_JOBID
12583         }
12584
12585         log "Test: ${cmd[*]}"
12586         log "Using JobID environment variable $JOBENV=$JOBVAL"
12587
12588         if [ $JOBENV = "FAKE_JOBID" ]; then
12589                 FAKE_JOBID=$JOBVAL ${cmd[*]}
12590         else
12591                 ${cmd[*]}
12592         fi
12593
12594         # all files are created on OST0000
12595         for facet in $facets; do
12596                 local stats="*.$(convert_facet2label $facet).job_stats"
12597                 if [ $(do_facet $facet lctl get_param $stats |
12598                        grep -c $JOBVAL) -ne 1 ]; then
12599                         do_facet $facet lctl get_param $stats
12600                         error "No jobstats for $JOBVAL found on $facet::$stats"
12601                 fi
12602         done
12603 }
12604
12605 jobstats_set() {
12606         trap 0
12607         NEW_JOBENV=${1:-$OLD_JOBENV}
12608         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
12609         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
12610 }
12611
12612 cleanup_205() {
12613         trap 0
12614         do_facet $SINGLEMDS \
12615                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
12616         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
12617         cleanup_changelog
12618 }
12619
12620 test_205() { # Job stats
12621         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
12622                 { skip "Need MDS version with at least 2.7.1"; return 0; }
12623
12624         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12625         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12626         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12627         remote_ost_nodsh && skip "remote OST with nodsh" && return
12628
12629         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
12630                 skip "Server doesn't support jobstats" && return 0
12631         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
12632
12633         OLD_JOBENV=$($LCTL get_param -n jobid_var)
12634         if [ $OLD_JOBENV != $JOBENV ]; then
12635                 jobstats_set $JOBENV
12636                 trap cleanup_205 EXIT
12637         fi
12638
12639         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
12640         echo "Registered as changelog user $CL_USER"
12641
12642         OLD_INTERVAL=$(do_facet $SINGLEMDS \
12643                        lctl get_param -n mdt.*.job_cleanup_interval)
12644         local interval_new=5
12645         do_facet $SINGLEMDS \
12646                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
12647         local start=$SECONDS
12648
12649         local cmd
12650         # mkdir
12651         cmd="mkdir $DIR/$tdir"
12652         verify_jobstats "$cmd" "$SINGLEMDS"
12653         # rmdir
12654         cmd="rmdir $DIR/$tdir"
12655         verify_jobstats "$cmd" "$SINGLEMDS"
12656         # mkdir on secondary MDT
12657         if [ $MDSCOUNT -gt 1 ]; then
12658                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
12659                 verify_jobstats "$cmd" "mds2"
12660         fi
12661         # mknod
12662         cmd="mknod $DIR/$tfile c 1 3"
12663         verify_jobstats "$cmd" "$SINGLEMDS"
12664         # unlink
12665         cmd="rm -f $DIR/$tfile"
12666         verify_jobstats "$cmd" "$SINGLEMDS"
12667         # create all files on OST0000 so verify_jobstats can find OST stats
12668         # open & close
12669         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
12670         verify_jobstats "$cmd" "$SINGLEMDS"
12671         # setattr
12672         cmd="touch $DIR/$tfile"
12673         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12674         # write
12675         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
12676         verify_jobstats "$cmd" "ost1"
12677         # read
12678         cancel_lru_locks osc
12679         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
12680         verify_jobstats "$cmd" "ost1"
12681         # truncate
12682         cmd="$TRUNCATE $DIR/$tfile 0"
12683         verify_jobstats "$cmd" "$SINGLEMDS ost1"
12684         # rename
12685         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
12686         verify_jobstats "$cmd" "$SINGLEMDS"
12687         # jobstats expiry - sleep until old stats should be expired
12688         local left=$((interval_new + 5 - (SECONDS - start)))
12689         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
12690                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
12691                         "0" $left
12692         cmd="mkdir $DIR/$tdir.expire"
12693         verify_jobstats "$cmd" "$SINGLEMDS"
12694         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
12695             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
12696
12697         # Ensure that jobid are present in changelog (if supported by MDS)
12698         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
12699         then
12700                 $LFS changelog $MDT0 | tail -9
12701                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
12702                 [ $jobids -eq 9 ] ||
12703                         error "Wrong changelog jobid count $jobids != 9"
12704
12705                 # LU-5862
12706                 JOBENV="disable"
12707                 jobstats_set $JOBENV
12708                 touch $DIR/$tfile
12709                 $LFS changelog $MDT0 | tail -1
12710                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
12711                 [ $jobids -eq 0 ] ||
12712                         error "Unexpected jobids when jobid_var=$JOBENV"
12713         fi
12714
12715         cleanup_205
12716 }
12717 run_test 205 "Verify job stats"
12718
12719 # LU-1480, LU-1773 and LU-1657
12720 test_206() {
12721         mkdir -p $DIR/$tdir
12722         $SETSTRIPE -c -1 $DIR/$tdir
12723 #define OBD_FAIL_LOV_INIT 0x1403
12724         $LCTL set_param fail_loc=0xa0001403
12725         $LCTL set_param fail_val=1
12726         touch $DIR/$tdir/$tfile || true
12727 }
12728 run_test 206 "fail lov_init_raid0() doesn't lbug"
12729
12730 test_207a() {
12731         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12732         local fsz=`stat -c %s $DIR/$tfile`
12733         cancel_lru_locks mdc
12734
12735         # do not return layout in getattr intent
12736 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
12737         $LCTL set_param fail_loc=0x170
12738         local sz=`stat -c %s $DIR/$tfile`
12739
12740         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
12741
12742         rm -rf $DIR/$tfile
12743 }
12744 run_test 207a "can refresh layout at glimpse"
12745
12746 test_207b() {
12747         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
12748         local cksum=`md5sum $DIR/$tfile`
12749         local fsz=`stat -c %s $DIR/$tfile`
12750         cancel_lru_locks mdc
12751         cancel_lru_locks osc
12752
12753         # do not return layout in getattr intent
12754 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
12755         $LCTL set_param fail_loc=0x171
12756
12757         # it will refresh layout after the file is opened but before read issues
12758         echo checksum is "$cksum"
12759         echo "$cksum" |md5sum -c --quiet || error "file differs"
12760
12761         rm -rf $DIR/$tfile
12762 }
12763 run_test 207b "can refresh layout at open"
12764
12765 test_208() {
12766         # FIXME: in this test suite, only RD lease is used. This is okay
12767         # for now as only exclusive open is supported. After generic lease
12768         # is done, this test suite should be revised. - Jinshan
12769
12770         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12771         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
12772                 { skip "Need MDS version at least 2.4.52"; return 0; }
12773
12774         echo "==== test 1: verify get lease work"
12775         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
12776
12777         echo "==== test 2: verify lease can be broken by upcoming open"
12778         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12779         local PID=$!
12780         sleep 1
12781
12782         $MULTIOP $DIR/$tfile oO_RDONLY:c
12783         kill -USR1 $PID && wait $PID || error "break lease error"
12784
12785         echo "==== test 3: verify lease can't be granted if an open already exists"
12786         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
12787         local PID=$!
12788         sleep 1
12789
12790         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
12791         kill -USR1 $PID && wait $PID || error "open file error"
12792
12793         echo "==== test 4: lease can sustain over recovery"
12794         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
12795         PID=$!
12796         sleep 1
12797
12798         fail mds1
12799
12800         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
12801
12802         echo "==== test 5: lease broken can't be regained by replay"
12803         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
12804         PID=$!
12805         sleep 1
12806
12807         # open file to break lease and then recovery
12808         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
12809         fail mds1
12810
12811         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
12812
12813         rm -f $DIR/$tfile
12814 }
12815 run_test 208 "Exclusive open"
12816
12817 test_209() {
12818         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
12819                 skip_env "must have disp_stripe" && return
12820
12821         touch $DIR/$tfile
12822         sync; sleep 5; sync;
12823
12824         echo 3 > /proc/sys/vm/drop_caches
12825         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12826
12827         # open/close 500 times
12828         for i in $(seq 500); do
12829                 cat $DIR/$tfile
12830         done
12831
12832         echo 3 > /proc/sys/vm/drop_caches
12833         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
12834
12835         echo "before: $req_before, after: $req_after"
12836         [ $((req_after - req_before)) -ge 300 ] &&
12837                 error "open/close requests are not freed"
12838         return 0
12839 }
12840 run_test 209 "read-only open/close requests should be freed promptly"
12841
12842 test_212() {
12843         size=`date +%s`
12844         size=$((size % 8192 + 1))
12845         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
12846         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
12847         rm -f $DIR/f212 $DIR/f212.xyz
12848 }
12849 run_test 212 "Sendfile test ============================================"
12850
12851 test_213() {
12852         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
12853         cancel_lru_locks osc
12854         lctl set_param fail_loc=0x8000040f
12855         # generate a read lock
12856         cat $DIR/$tfile > /dev/null
12857         # write to the file, it will try to cancel the above read lock.
12858         cat /etc/hosts >> $DIR/$tfile
12859 }
12860 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
12861
12862 test_214() { # for bug 20133
12863         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
12864         for (( i=0; i < 340; i++ )) ; do
12865                 touch $DIR/$tdir/d214c/a$i
12866         done
12867
12868         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
12869         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
12870         ls $DIR/d214c || error "ls $DIR/d214c failed"
12871         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
12872         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
12873 }
12874 run_test 214 "hash-indexed directory test - bug 20133"
12875
12876 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
12877 create_lnet_proc_files() {
12878         lctl get_param -n $1 >$TMP/lnet_$1.out || error "cannot read lnet.$1"
12879         sysctl lnet.$1 >$TMP/lnet_$1.sys_tmp || error "cannot read lnet.$1"
12880
12881         sed "s/^lnet.$1\ =\ //g" "$TMP/lnet_$1.sys_tmp" >$TMP/lnet_$1.sys
12882         rm -f "$TMP/lnet_$1.sys_tmp"
12883 }
12884
12885 # counterpart of create_lnet_proc_files
12886 remove_lnet_proc_files() {
12887         rm -f $TMP/lnet_$1.out $TMP/lnet_$1.sys
12888 }
12889
12890 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12891 # 3rd arg as regexp for body
12892 check_lnet_proc_stats() {
12893         local l=$(cat "$TMP/lnet_$1" |wc -l)
12894         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
12895
12896         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
12897 }
12898
12899 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
12900 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
12901 # optional and can be regexp for 2nd line (lnet.routes case)
12902 check_lnet_proc_entry() {
12903         local blp=2          # blp stands for 'position of 1st line of body'
12904         [ -z "$5" ] || blp=3 # lnet.routes case
12905
12906         local l=$(cat "$TMP/lnet_$1" |wc -l)
12907         # subtracting one from $blp because the body can be empty
12908         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
12909
12910         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
12911                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
12912
12913         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
12914                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
12915
12916         # bail out if any unexpected line happened
12917         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
12918         [ "$?" != 0 ] || error "$2 misformatted"
12919 }
12920
12921 test_215() { # for bugs 18102, 21079, 21517
12922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12923         local N='(0|[1-9][0-9]*)'       # non-negative numeric
12924         local P='[1-9][0-9]*'           # positive numeric
12925         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
12926         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
12927         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
12928         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
12929
12930         local L1 # regexp for 1st line
12931         local L2 # regexp for 2nd line (optional)
12932         local BR # regexp for the rest (body)
12933
12934         # lnet.stats should look as 11 space-separated non-negative numerics
12935         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
12936         create_lnet_proc_files "stats"
12937         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
12938         remove_lnet_proc_files "stats"
12939
12940         # lnet.routes should look like this:
12941         # Routing disabled/enabled
12942         # net hops priority state router
12943         # where net is a string like tcp0, hops > 0, priority >= 0,
12944         # state is up/down,
12945         # router is a string like 192.168.1.1@tcp2
12946         L1="^Routing (disabled|enabled)$"
12947         L2="^net +hops +priority +state +router$"
12948         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
12949         create_lnet_proc_files "routes"
12950         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
12951         remove_lnet_proc_files "routes"
12952
12953         # lnet.routers should look like this:
12954         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
12955         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
12956         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
12957         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
12958         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
12959         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
12960         create_lnet_proc_files "routers"
12961         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
12962         remove_lnet_proc_files "routers"
12963
12964         # lnet.peers should look like this:
12965         # nid refs state last max rtr min tx min queue
12966         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
12967         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
12968         # numeric (0 or >0 or <0), queue >= 0.
12969         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
12970         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
12971         create_lnet_proc_files "peers"
12972         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
12973         remove_lnet_proc_files "peers"
12974
12975         # lnet.buffers  should look like this:
12976         # pages count credits min
12977         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
12978         L1="^pages +count +credits +min$"
12979         BR="^ +$N +$N +$I +$I$"
12980         create_lnet_proc_files "buffers"
12981         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
12982         remove_lnet_proc_files "buffers"
12983
12984         # lnet.nis should look like this:
12985         # nid status alive refs peer rtr max tx min
12986         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
12987         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
12988         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
12989         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
12990         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
12991         create_lnet_proc_files "nis"
12992         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
12993         remove_lnet_proc_files "nis"
12994
12995         # can we successfully write to lnet.stats?
12996         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
12997         sysctl -w lnet.stats=0 || error "cannot write to lnet.stats"
12998 }
12999 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
13000
13001 test_216() { # bug 20317
13002         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13003         remote_ost_nodsh && skip "remote OST with nodsh" && return
13004
13005         local node
13006         local facets=$(get_facets OST)
13007         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13008
13009         save_lustre_params client "osc.*.contention_seconds" > $p
13010         save_lustre_params $facets \
13011                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
13012         save_lustre_params $facets \
13013                 "ldlm.namespaces.filter-*.contended_locks" >> $p
13014         save_lustre_params $facets \
13015                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
13016         clear_stats osc.*.osc_stats
13017
13018         # agressive lockless i/o settings
13019         do_nodes $(comma_list $(osts_nodes)) \
13020                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
13021                         ldlm.namespaces.filter-*.contended_locks=0 \
13022                         ldlm.namespaces.filter-*.contention_seconds=60"
13023         lctl set_param -n osc.*.contention_seconds=60
13024
13025         $DIRECTIO write $DIR/$tfile 0 10 4096
13026         $CHECKSTAT -s 40960 $DIR/$tfile
13027
13028         # disable lockless i/o
13029         do_nodes $(comma_list $(osts_nodes)) \
13030                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
13031                         ldlm.namespaces.filter-*.contended_locks=32 \
13032                         ldlm.namespaces.filter-*.contention_seconds=0"
13033         lctl set_param -n osc.*.contention_seconds=0
13034         clear_stats osc.*.osc_stats
13035
13036         dd if=/dev/zero of=$DIR/$tfile count=0
13037         $CHECKSTAT -s 0 $DIR/$tfile
13038
13039         restore_lustre_params <$p
13040         rm -f $p
13041         rm $DIR/$tfile
13042 }
13043 run_test 216 "check lockless direct write updates file size and kms correctly"
13044
13045 test_217() { # bug 22430
13046         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13047         local node
13048         local nid
13049
13050         for node in $(nodes_list); do
13051                 nid=$(host_nids_address $node $NETTYPE)
13052                 if [[ $nid = *-* ]] ; then
13053                         echo "lctl ping $(h2nettype $nid)"
13054                         lctl ping $(h2nettype $nid)
13055                 else
13056                         echo "skipping $node (no hyphen detected)"
13057                 fi
13058         done
13059 }
13060 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
13061
13062 test_218() {
13063        # do directio so as not to populate the page cache
13064        log "creating a 10 Mb file"
13065        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13066        log "starting reads"
13067        dd if=$DIR/$tfile of=/dev/null bs=4096 &
13068        log "truncating the file"
13069        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13070        log "killing dd"
13071        kill %+ || true # reads might have finished
13072        echo "wait until dd is finished"
13073        wait
13074        log "removing the temporary file"
13075        rm -rf $DIR/$tfile || error "tmp file removal failed"
13076 }
13077 run_test 218 "parallel read and truncate should not deadlock ======================="
13078
13079 test_219() {
13080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13081         # write one partial page
13082         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
13083         # set no grant so vvp_io_commit_write will do sync write
13084         $LCTL set_param fail_loc=0x411
13085         # write a full page at the end of file
13086         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
13087
13088         $LCTL set_param fail_loc=0
13089         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
13090         $LCTL set_param fail_loc=0x411
13091         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
13092
13093         # LU-4201
13094         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
13095         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
13096 }
13097 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
13098
13099 test_220() { #LU-325
13100         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13101         remote_ost_nodsh && skip "remote OST with nodsh" && return
13102         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13103         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13104         local OSTIDX=0
13105
13106         # create on MDT0000 so the last_id and next_id are correct
13107         mkdir $DIR/$tdir
13108         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
13109         OST=${OST%_UUID}
13110
13111         # on the mdt's osc
13112         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
13113         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
13114                         osc.$mdtosc_proc1.prealloc_last_id)
13115         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
13116                         osc.$mdtosc_proc1.prealloc_next_id)
13117
13118         $LFS df -i
13119
13120         if ! combined_mgs_mds ; then
13121                 mount_mgs_client
13122         fi
13123
13124         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
13125         #define OBD_FAIL_OST_ENOINO              0x229
13126         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
13127         create_pool $FSNAME.$TESTNAME || return 1
13128         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
13129
13130         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
13131
13132         MDSOBJS=$((last_id - next_id))
13133         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
13134
13135         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
13136         echo "OST still has $count kbytes free"
13137
13138         echo "create $MDSOBJS files @next_id..."
13139         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
13140
13141         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13142                         osc.$mdtosc_proc1.prealloc_last_id)
13143         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13144                         osc.$mdtosc_proc1.prealloc_next_id)
13145
13146         echo "after creation, last_id=$last_id2, next_id=$next_id2"
13147         $LFS df -i
13148
13149         echo "cleanup..."
13150
13151         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
13152         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
13153
13154         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
13155                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
13156         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
13157                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
13158         echo "unlink $MDSOBJS files @$next_id..."
13159         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
13160
13161         if ! combined_mgs_mds ; then
13162                 umount_mgs_client
13163         fi
13164 }
13165 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
13166
13167 test_221() {
13168         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13169         dd if=`which date` of=$MOUNT/date oflag=sync
13170         chmod +x $MOUNT/date
13171
13172         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
13173         $LCTL set_param fail_loc=0x80001401
13174
13175         $MOUNT/date > /dev/null
13176         rm -f $MOUNT/date
13177 }
13178 run_test 221 "make sure fault and truncate race to not cause OOM"
13179
13180 test_222a () {
13181         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13182        rm -rf $DIR/$tdir
13183        test_mkdir -p $DIR/$tdir
13184        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
13185        createmany -o $DIR/$tdir/$tfile 10
13186        cancel_lru_locks mdc
13187        cancel_lru_locks osc
13188        #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13189        $LCTL set_param fail_loc=0x31a
13190        ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
13191        $LCTL set_param fail_loc=0
13192        rm -r $DIR/$tdir
13193 }
13194 run_test 222a "AGL for ls should not trigger CLIO lock failure ================"
13195
13196 test_222b () {
13197         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13198         rm -rf $DIR/$tdir
13199         test_mkdir -p $DIR/$tdir
13200         $SETSTRIPE -c 1 -i 0 $DIR/$tdir
13201         createmany -o $DIR/$tdir/$tfile 10
13202         cancel_lru_locks mdc
13203         cancel_lru_locks osc
13204         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13205         $LCTL set_param fail_loc=0x31a
13206         rm -r $DIR/$tdir || error "AGL for rmdir failed"
13207         $LCTL set_param fail_loc=0
13208 }
13209 run_test 222b "AGL for rmdir should not trigger CLIO lock failure ============="
13210
13211 test_223 () {
13212         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13213        rm -rf $DIR/$tdir
13214        test_mkdir -p $DIR/$tdir
13215        $SETSTRIPE -c 1 -i 0 $DIR/$tdir
13216        createmany -o $DIR/$tdir/$tfile 10
13217        cancel_lru_locks mdc
13218        cancel_lru_locks osc
13219        #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
13220        $LCTL set_param fail_loc=0x31b
13221        ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
13222        $LCTL set_param fail_loc=0
13223        rm -r $DIR/$tdir
13224 }
13225 run_test 223 "osc reenqueue if without AGL lock granted ======================="
13226
13227 test_224a() { # LU-1039, MRP-303
13228         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13229         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
13230         $LCTL set_param fail_loc=0x508
13231         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
13232         $LCTL set_param fail_loc=0
13233         df $DIR
13234 }
13235 run_test 224a "Don't panic on bulk IO failure"
13236
13237 test_224b() { # LU-1039, MRP-303
13238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13239         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
13240         cancel_lru_locks osc
13241         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
13242         $LCTL set_param fail_loc=0x515
13243         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
13244         $LCTL set_param fail_loc=0
13245         df $DIR
13246 }
13247 run_test 224b "Don't panic on bulk IO failure"
13248
13249 test_224c() { # LU-6441
13250         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13251         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13252
13253         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13254         save_writethrough $p
13255         set_cache writethrough on
13256
13257         local pages_per_rpc=$($LCTL get_param \
13258                                 osc.*.max_pages_per_rpc)
13259         local at_max=$($LCTL get_param -n at_max)
13260         local timeout=$($LCTL get_param -n timeout)
13261         local test_at="$LCTL get_param -n at_max"
13262         local param_at="$FSNAME.sys.at_max"
13263         local test_timeout="$LCTL get_param -n timeout"
13264         local param_timeout="$FSNAME.sys.timeout"
13265
13266         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
13267
13268         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13269                 error "conf_param at_max=0 failed"
13270         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13271                 error "conf_param timeout=5 failed"
13272
13273         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13274         do_facet ost1 $LCTL set_param fail_loc=0x520
13275         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13276         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13277         sync
13278         do_facet ost1 $LCTL set_param fail_loc=0
13279
13280         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13281                 error "conf_param at_max=$at_max failed"
13282         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13283                 $timeout || error "conf_param timeout=$timeout failed"
13284
13285         $LCTL set_param -n $pages_per_rpc
13286         restore_lustre_params < $p
13287         rm -f $p
13288 }
13289 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13290
13291 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13292 test_225a () {
13293         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13294         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13295         if [ -z ${MDSSURVEY} ]; then
13296               skip_env "mds-survey not found" && return
13297         fi
13298
13299         [ $MDSCOUNT -ge 2 ] &&
13300                 skip "skipping now for more than one MDT" && return
13301
13302        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13303             { skip "Need MDS version at least 2.2.51"; return; }
13304
13305        local mds=$(facet_host $SINGLEMDS)
13306        local target=$(do_nodes $mds 'lctl dl' | \
13307                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13308
13309        local cmd1="file_count=1000 thrhi=4"
13310        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13311        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13312        local cmd="$cmd1 $cmd2 $cmd3"
13313
13314        rm -f ${TMP}/mds_survey*
13315        echo + $cmd
13316        eval $cmd || error "mds-survey with zero-stripe failed"
13317        cat ${TMP}/mds_survey*
13318        rm -f ${TMP}/mds_survey*
13319 }
13320 run_test 225a "Metadata survey sanity with zero-stripe"
13321
13322 test_225b () {
13323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13324         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13325         if [ -z ${MDSSURVEY} ]; then
13326               skip_env "mds-survey not found" && return
13327         fi
13328         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13329             { skip "Need MDS version at least 2.2.51"; return; }
13330
13331         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13332               skip_env "Need to mount OST to test" && return
13333         fi
13334
13335        local mds=$(facet_host $SINGLEMDS)
13336        local target=$(do_nodes $mds 'lctl dl' | \
13337                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13338
13339        local cmd1="file_count=1000 thrhi=4"
13340        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13341        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13342        local cmd="$cmd1 $cmd2 $cmd3"
13343
13344        rm -f ${TMP}/mds_survey*
13345        echo + $cmd
13346        eval $cmd || error "mds-survey with stripe_count failed"
13347        cat ${TMP}/mds_survey*
13348        rm -f ${TMP}/mds_survey*
13349 }
13350 run_test 225b "Metadata survey sanity with stripe_count = 1"
13351
13352 mcreate_path2fid () {
13353         local mode=$1
13354         local major=$2
13355         local minor=$3
13356         local name=$4
13357         local desc=$5
13358         local path=$DIR/$tdir/$name
13359         local fid
13360         local rc
13361         local fid_path
13362
13363         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13364                 error "cannot create $desc"
13365
13366         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13367         rc=$?
13368         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13369
13370         fid_path=$($LFS fid2path $MOUNT $fid)
13371         rc=$?
13372         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13373
13374         [ "$path" == "$fid_path" ] ||
13375                 error "fid2path returned $fid_path, expected $path"
13376
13377         echo "pass with $path and $fid"
13378 }
13379
13380 test_226a () {
13381         rm -rf $DIR/$tdir
13382         mkdir -p $DIR/$tdir
13383
13384         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13385         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13386         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13387         mcreate_path2fid 0040666 0 0 dir "directory"
13388         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13389         mcreate_path2fid 0100666 0 0 file "regular file"
13390         mcreate_path2fid 0120666 0 0 link "symbolic link"
13391         mcreate_path2fid 0140666 0 0 sock "socket"
13392 }
13393 run_test 226a "call path2fid and fid2path on files of all type"
13394
13395 test_226b () {
13396         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13397         rm -rf $DIR/$tdir
13398         local MDTIDX=1
13399
13400         mkdir -p $DIR/$tdir
13401         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13402                 error "create remote directory failed"
13403         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13404         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13405                                 "character special file (null)"
13406         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13407                                 "character special file (no device)"
13408         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13409         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13410                                 "block special file (loop)"
13411         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13412         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13413         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13414 }
13415 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13416
13417 # LU-1299 Executing or running ldd on a truncated executable does not
13418 # cause an out-of-memory condition.
13419 test_227() {
13420         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13421         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13422         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13423         chmod +x $MOUNT/date
13424
13425         $MOUNT/date > /dev/null
13426         ldd $MOUNT/date > /dev/null
13427         rm -f $MOUNT/date
13428 }
13429 run_test 227 "running truncated executable does not cause OOM"
13430
13431 # LU-1512 try to reuse idle OI blocks
13432 test_228a() {
13433         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13434         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13435         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13436                 skip "ldiskfs only test" && return
13437
13438         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13439         local myDIR=$DIR/$tdir
13440
13441         mkdir -p $myDIR
13442         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13443         $LCTL set_param fail_loc=0x80001002
13444         createmany -o $myDIR/t- 10000
13445         $LCTL set_param fail_loc=0
13446         # The guard is current the largest FID holder
13447         touch $myDIR/guard
13448         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13449                     tr -d '[')
13450         local IDX=$(($SEQ % 64))
13451
13452         do_facet $SINGLEMDS sync
13453         # Make sure journal flushed.
13454         sleep 6
13455         local blk1=$(do_facet $SINGLEMDS \
13456                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13457                      grep Blockcount | awk '{print $4}')
13458
13459         # Remove old files, some OI blocks will become idle.
13460         unlinkmany $myDIR/t- 10000
13461         # Create new files, idle OI blocks should be reused.
13462         createmany -o $myDIR/t- 2000
13463         do_facet $SINGLEMDS sync
13464         # Make sure journal flushed.
13465         sleep 6
13466         local blk2=$(do_facet $SINGLEMDS \
13467                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13468                      grep Blockcount | awk '{print $4}')
13469
13470         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13471 }
13472 run_test 228a "try to reuse idle OI blocks"
13473
13474 test_228b() {
13475         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13476         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13477         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13478                 skip "ldiskfs only test" && return
13479
13480         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13481         local myDIR=$DIR/$tdir
13482
13483         mkdir -p $myDIR
13484         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13485         $LCTL set_param fail_loc=0x80001002
13486         createmany -o $myDIR/t- 10000
13487         $LCTL set_param fail_loc=0
13488         # The guard is current the largest FID holder
13489         touch $myDIR/guard
13490         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13491                     tr -d '[')
13492         local IDX=$(($SEQ % 64))
13493
13494         do_facet $SINGLEMDS sync
13495         # Make sure journal flushed.
13496         sleep 6
13497         local blk1=$(do_facet $SINGLEMDS \
13498                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13499                      grep Blockcount | awk '{print $4}')
13500
13501         # Remove old files, some OI blocks will become idle.
13502         unlinkmany $myDIR/t- 10000
13503
13504         # stop the MDT
13505         stop $SINGLEMDS || error "Fail to stop MDT."
13506         # remount the MDT
13507         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13508
13509         df $MOUNT || error "Fail to df."
13510         # Create new files, idle OI blocks should be reused.
13511         createmany -o $myDIR/t- 2000
13512         do_facet $SINGLEMDS sync
13513         # Make sure journal flushed.
13514         sleep 6
13515         local blk2=$(do_facet $SINGLEMDS \
13516                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13517                      grep Blockcount | awk '{print $4}')
13518
13519         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13520 }
13521 run_test 228b "idle OI blocks can be reused after MDT restart"
13522
13523 #LU-1881
13524 test_228c() {
13525         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13526         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13527         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13528                 skip "ldiskfs only test" && return
13529
13530         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13531         local myDIR=$DIR/$tdir
13532
13533         mkdir -p $myDIR
13534         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13535         $LCTL set_param fail_loc=0x80001002
13536         # 20000 files can guarantee there are index nodes in the OI file
13537         createmany -o $myDIR/t- 20000
13538         $LCTL set_param fail_loc=0
13539         # The guard is current the largest FID holder
13540         touch $myDIR/guard
13541         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13542                     tr -d '[')
13543         local IDX=$(($SEQ % 64))
13544
13545         do_facet $SINGLEMDS sync
13546         # Make sure journal flushed.
13547         sleep 6
13548         local blk1=$(do_facet $SINGLEMDS \
13549                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13550                      grep Blockcount | awk '{print $4}')
13551
13552         # Remove old files, some OI blocks will become idle.
13553         unlinkmany $myDIR/t- 20000
13554         rm -f $myDIR/guard
13555         # The OI file should become empty now
13556
13557         # Create new files, idle OI blocks should be reused.
13558         createmany -o $myDIR/t- 2000
13559         do_facet $SINGLEMDS sync
13560         # Make sure journal flushed.
13561         sleep 6
13562         local blk2=$(do_facet $SINGLEMDS \
13563                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13564                      grep Blockcount | awk '{print $4}')
13565
13566         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13567 }
13568 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
13569
13570 test_229() { # LU-2482, LU-3448
13571         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
13572                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
13573                 return
13574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13575         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
13576
13577         rm -f $DIR/$tfile
13578
13579         # Create a file with a released layout and stripe count 2.
13580         $MULTIOP $DIR/$tfile H2c ||
13581                 error "failed to create file with released layout"
13582
13583         $GETSTRIPE -v $DIR/$tfile
13584
13585         local pattern=$($GETSTRIPE -L $DIR/$tfile)
13586         [ X"$pattern" = X"80000001" ] || error "pattern error ($pattern)"
13587
13588         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
13589         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
13590         stat $DIR/$tfile || error "failed to stat released file"
13591
13592         chown $RUNAS_ID $DIR/$tfile ||
13593                 error "chown $RUNAS_ID $DIR/$tfile failed"
13594
13595         chgrp $RUNAS_ID $DIR/$tfile ||
13596                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
13597
13598         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
13599         rm $DIR/$tfile || error "failed to remove released file"
13600 }
13601 run_test 229 "getstripe/stat/rm/attr changes work on released files"
13602
13603 test_230a() {
13604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13605         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13606         local MDTIDX=1
13607
13608         test_mkdir $DIR/$tdir
13609         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
13610         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
13611         [ $mdt_idx -ne 0 ] &&
13612                 error "create local directory on wrong MDT $mdt_idx"
13613
13614         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
13615                         error "create remote directory failed"
13616         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
13617         [ $mdt_idx -ne $MDTIDX ] &&
13618                 error "create remote directory on wrong MDT $mdt_idx"
13619
13620         createmany -o $DIR/$tdir/test_230/t- 10 ||
13621                 error "create files on remote directory failed"
13622         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
13623         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
13624         rm -r $DIR/$tdir || error "unlink remote directory failed"
13625 }
13626 run_test 230a "Create remote directory and files under the remote directory"
13627
13628 test_230b() {
13629         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13630         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13631         local MDTIDX=1
13632         local mdt_index
13633         local i
13634         local file
13635         local pid
13636         local stripe_count
13637         local migrate_dir=$DIR/$tdir/migrate_dir
13638         local other_dir=$DIR/$tdir/other_dir
13639
13640         test_mkdir $DIR/$tdir
13641         test_mkdir -i0 -c1 $migrate_dir
13642         test_mkdir -i0 -c1 $other_dir
13643         for ((i=0; i<10; i++)); do
13644                 mkdir -p $migrate_dir/dir_${i}
13645                 createmany -o $migrate_dir/dir_${i}/f 10 ||
13646                         error "create files under remote dir failed $i"
13647         done
13648
13649         cp /etc/passwd $migrate_dir/$tfile
13650         cp /etc/passwd $other_dir/$tfile
13651         chattr +SAD $migrate_dir
13652         chattr +SAD $migrate_dir/$tfile
13653
13654         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13655         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13656         local old_dir_mode=$(stat -c%f $migrate_dir)
13657         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
13658
13659         mkdir -p $migrate_dir/dir_default_stripe2
13660         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
13661         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
13662
13663         mkdir -p $other_dir
13664         ln $migrate_dir/$tfile $other_dir/luna
13665         ln $migrate_dir/$tfile $migrate_dir/sofia
13666         ln $other_dir/$tfile $migrate_dir/david
13667         ln -s $migrate_dir/$tfile $other_dir/zachary
13668         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
13669         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
13670
13671         $LFS migrate -m $MDTIDX $migrate_dir ||
13672                 error "fails on migrating remote dir to MDT1"
13673
13674         echo "migratate to MDT1, then checking.."
13675         for ((i = 0; i < 10; i++)); do
13676                 for file in $(find $migrate_dir/dir_${i}); do
13677                         mdt_index=$($LFS getstripe -M $file)
13678                         [ $mdt_index == $MDTIDX ] ||
13679                                 error "$file is not on MDT${MDTIDX}"
13680                 done
13681         done
13682
13683         # the multiple link file should still in MDT0
13684         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
13685         [ $mdt_index == 0 ] ||
13686                 error "$file is not on MDT${MDTIDX}"
13687
13688         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13689         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13690                 error " expect $old_dir_flag get $new_dir_flag"
13691
13692         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13693         [ "$old_file_flag" = "$new_file_flag" ] ||
13694                 error " expect $old_file_flag get $new_file_flag"
13695
13696         local new_dir_mode=$(stat -c%f $migrate_dir)
13697         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13698                 error "expect mode $old_dir_mode get $new_dir_mode"
13699
13700         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13701         [ "$old_file_mode" = "$new_file_mode" ] ||
13702                 error "expect mode $old_file_mode get $new_file_mode"
13703
13704         diff /etc/passwd $migrate_dir/$tfile ||
13705                 error "$tfile different after migration"
13706
13707         diff /etc/passwd $other_dir/luna ||
13708                 error "luna different after migration"
13709
13710         diff /etc/passwd $migrate_dir/sofia ||
13711                 error "sofia different after migration"
13712
13713         diff /etc/passwd $migrate_dir/david ||
13714                 error "david different after migration"
13715
13716         diff /etc/passwd $other_dir/zachary ||
13717                 error "zachary different after migration"
13718
13719         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13720                 error "${tfile}_ln different after migration"
13721
13722         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13723                 error "${tfile}_ln_other different after migration"
13724
13725         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
13726         [ $stripe_count = 2 ] ||
13727                 error "dir strpe_count $d != 2 after migration."
13728
13729         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
13730         [ $stripe_count = 2 ] ||
13731                 error "file strpe_count $d != 2 after migration."
13732
13733         #migrate back to MDT0
13734         MDTIDX=0
13735
13736         $LFS migrate -m $MDTIDX $migrate_dir ||
13737                 error "fails on migrating remote dir to MDT0"
13738
13739         echo "migrate back to MDT0, checking.."
13740         for file in $(find $migrate_dir); do
13741                 mdt_index=$($LFS getstripe -M $file)
13742                 [ $mdt_index == $MDTIDX ] ||
13743                         error "$file is not on MDT${MDTIDX}"
13744         done
13745
13746         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
13747         [ "$old_dir_flag" = "$new_dir_flag" ] ||
13748                 error " expect $old_dir_flag get $new_dir_flag"
13749
13750         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
13751         [ "$old_file_flag" = "$new_file_flag" ] ||
13752                 error " expect $old_file_flag get $new_file_flag"
13753
13754         local new_dir_mode=$(stat -c%f $migrate_dir)
13755         [ "$old_dir_mode" = "$new_dir_mode" ] ||
13756                 error "expect mode $old_dir_mode get $new_dir_mode"
13757
13758         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
13759         [ "$old_file_mode" = "$new_file_mode" ] ||
13760                 error "expect mode $old_file_mode get $new_file_mode"
13761
13762         diff /etc/passwd ${migrate_dir}/$tfile ||
13763                 error "$tfile different after migration"
13764
13765         diff /etc/passwd ${other_dir}/luna ||
13766                 error "luna different after migration"
13767
13768         diff /etc/passwd ${migrate_dir}/sofia ||
13769                 error "sofia different after migration"
13770
13771         diff /etc/passwd ${other_dir}/zachary ||
13772                 error "zachary different after migration"
13773
13774         diff /etc/passwd $migrate_dir/${tfile}_ln ||
13775                 error "${tfile}_ln different after migration"
13776
13777         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
13778                 error "${tfile}_ln_other different after migration"
13779
13780         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
13781         [ $stripe_count = 2 ] ||
13782                 error "dir strpe_count $d != 2 after migration."
13783
13784         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
13785         [ $stripe_count = 2 ] ||
13786                 error "file strpe_count $d != 2 after migration."
13787
13788         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13789 }
13790 run_test 230b "migrate directory"
13791
13792 test_230c() {
13793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13794         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13795         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13796         local MDTIDX=1
13797         local mdt_index
13798         local file
13799         local migrate_dir=$DIR/$tdir/migrate_dir
13800
13801         #If migrating directory fails in the middle, all entries of
13802         #the directory is still accessiable.
13803         test_mkdir $DIR/$tdir
13804         test_mkdir -i0 -c1 $migrate_dir
13805         stat $migrate_dir
13806         createmany -o $migrate_dir/f 10 ||
13807                 error "create files under ${migrate_dir} failed"
13808
13809         #failed after migrating 5 entries
13810         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
13811         do_facet mds1 lctl set_param fail_loc=0x20001801
13812         do_facet mds1 lctl  set_param fail_val=5
13813         local t=$(ls $migrate_dir | wc -l)
13814         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
13815                 error "migrate should fail after 5 entries"
13816
13817         mkdir $migrate_dir/dir &&
13818                 error "mkdir succeeds under migrating directory"
13819         touch $migrate_dir/file &&
13820                 error "touch file succeeds under migrating directory"
13821
13822         local u=$(ls $migrate_dir | wc -l)
13823         [ "$u" == "$t" ] || error "$u != $t during migration"
13824
13825         for file in $(find $migrate_dir); do
13826                 stat $file || error "stat $file failed"
13827         done
13828
13829         do_facet mds1 lctl set_param fail_loc=0
13830         do_facet mds1 lctl set_param fail_val=0
13831
13832         $LFS migrate -m $MDTIDX $migrate_dir ||
13833                 error "migrate open files should failed with open files"
13834
13835         echo "Finish migration, then checking.."
13836         for file in $(find $migrate_dir); do
13837                 mdt_index=$($LFS getstripe -M $file)
13838                 [ $mdt_index == $MDTIDX ] ||
13839                         error "$file is not on MDT${MDTIDX}"
13840         done
13841
13842         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13843 }
13844 run_test 230c "check directory accessiblity if migration is failed"
13845
13846 test_230d() {
13847         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13848         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13849         local MDTIDX=1
13850         local mdt_index
13851         local migrate_dir=$DIR/$tdir/migrate_dir
13852         local i
13853         local j
13854
13855         test_mkdir $DIR/$tdir
13856         test_mkdir -i0 -c1 $migrate_dir
13857
13858         for ((i=0; i<100; i++)); do
13859                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
13860                 createmany -o $migrate_dir/dir_${i}/f 100 ||
13861                         error "create files under remote dir failed $i"
13862         done
13863
13864         $LFS migrate -m $MDTIDX $migrate_dir ||
13865                 error "migrate remote dir error"
13866
13867         echo "Finish migration, then checking.."
13868         for file in $(find $migrate_dir); do
13869                 mdt_index=$($LFS getstripe -M $file)
13870                 [ $mdt_index == $MDTIDX ] ||
13871                         error "$file is not on MDT${MDTIDX}"
13872         done
13873
13874         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13875 }
13876 run_test 230d "check migrate big directory"
13877
13878 test_230e() {
13879         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13880         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13881         local i
13882         local j
13883         local a_fid
13884         local b_fid
13885
13886         mkdir -p $DIR/$tdir
13887         mkdir $DIR/$tdir/migrate_dir
13888         mkdir $DIR/$tdir/other_dir
13889         touch $DIR/$tdir/migrate_dir/a
13890         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
13891         ls $DIR/$tdir/other_dir
13892
13893         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13894                 error "migrate dir fails"
13895
13896         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13897         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13898
13899         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13900         [ $mdt_index == 0 ] || error "a is not on MDT0"
13901
13902         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
13903                 error "migrate dir fails"
13904
13905         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
13906         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
13907
13908         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13909         [ $mdt_index == 1 ] || error "a is not on MDT1"
13910
13911         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
13912         [ $mdt_index == 1 ] || error "b is not on MDT1"
13913
13914         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13915         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
13916
13917         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
13918
13919         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13920 }
13921 run_test 230e "migrate mulitple local link files"
13922
13923 test_230f() {
13924         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13925         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13926         local a_fid
13927         local ln_fid
13928
13929         mkdir -p $DIR/$tdir
13930         mkdir $DIR/$tdir/migrate_dir
13931         $LFS mkdir -i1 $DIR/$tdir/other_dir
13932         touch $DIR/$tdir/migrate_dir/a
13933         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
13934         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
13935         ls $DIR/$tdir/other_dir
13936
13937         # a should be migrated to MDT1, since no other links on MDT0
13938         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13939                 error "#1 migrate dir fails"
13940         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
13941         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
13942         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13943         [ $mdt_index == 1 ] || error "a is not on MDT1"
13944
13945         # a should stay on MDT1, because it is a mulitple link file
13946         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13947                 error "#2 migrate dir fails"
13948         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13949         [ $mdt_index == 1 ] || error "a is not on MDT1"
13950
13951         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
13952                 error "#3 migrate dir fails"
13953
13954         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
13955         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
13956         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
13957
13958         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
13959         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
13960
13961         # a should be migrated to MDT0, since no other links on MDT1
13962         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
13963                 error "#4 migrate dir fails"
13964         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
13965         [ $mdt_index == 0 ] || error "a is not on MDT0"
13966
13967         rm -rf $DIR/$tdir || error "rm dir failed after migration"
13968 }
13969 run_test 230f "migrate mulitple remote link files"
13970
13971 test_230g() {
13972         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13973         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13974
13975         mkdir -p $DIR/$tdir/migrate_dir
13976
13977         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
13978                 error "migrating dir to non-exist MDT succeeds"
13979         true
13980 }
13981 run_test 230g "migrate dir to non-exist MDT"
13982
13983 test_230h() {
13984         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13985         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13986         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
13987                 skip "Need MDS version at least 2.7.64" && return
13988         local mdt_index
13989
13990         mkdir -p $DIR/$tdir/migrate_dir
13991
13992         $LFS migrate -m1 $DIR &&
13993                 error "migrating mountpoint1 should fail"
13994
13995         $LFS migrate -m1 $DIR/$tdir/.. &&
13996                 error "migrating mountpoint2 should fail"
13997
13998         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
13999                 error "migrating $tdir fail"
14000
14001         mdt_index=$($LFS getstripe -M $DIR/$tdir)
14002         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14003
14004         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14005         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14006
14007 }
14008 run_test 230h "migrate .. and root"
14009
14010 test_230i() {
14011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14012         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14013
14014         mkdir -p $DIR/$tdir/migrate_dir
14015
14016         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
14017                 error "migration fails with a tailing slash"
14018
14019         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
14020                 error "migration fails with two tailing slashes"
14021 }
14022 run_test 230i "lfs migrate -m tolerates trailing slashes"
14023
14024 test_231a()
14025 {
14026         # For simplicity this test assumes that max_pages_per_rpc
14027         # is the same across all OSCs
14028         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
14029         local bulk_size=$((max_pages * 4096))
14030         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
14031                                        head -n 1)
14032
14033         mkdir -p $DIR/$tdir
14034         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
14035                 error "failed to set stripe with -S ${brw_size}M option"
14036
14037         # clear the OSC stats
14038         $LCTL set_param osc.*.stats=0 &>/dev/null
14039         stop_writeback
14040
14041         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
14042         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
14043                 oflag=direct &>/dev/null || error "dd failed"
14044
14045         sync; sleep 1; sync # just to be safe
14046         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
14047         if [ x$nrpcs != "x1" ]; then
14048                 $LCTL get_param osc.*.stats
14049                 error "found $nrpcs ost_write RPCs, not 1 as expected"
14050         fi
14051
14052         start_writeback
14053         # Drop the OSC cache, otherwise we will read from it
14054         cancel_lru_locks osc
14055
14056         # clear the OSC stats
14057         $LCTL set_param osc.*.stats=0 &>/dev/null
14058
14059         # Client reads $bulk_size.
14060         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
14061                 iflag=direct &>/dev/null || error "dd failed"
14062
14063         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
14064         if [ x$nrpcs != "x1" ]; then
14065                 $LCTL get_param osc.*.stats
14066                 error "found $nrpcs ost_read RPCs, not 1 as expected"
14067         fi
14068 }
14069 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
14070
14071 test_231b() {
14072         mkdir -p $DIR/$tdir
14073         local i
14074         for i in {0..1023}; do
14075                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
14076                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
14077                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
14078         done
14079         sync
14080 }
14081 run_test 231b "must not assert on fully utilized OST request buffer"
14082
14083 test_232() {
14084         mkdir -p $DIR/$tdir
14085         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14086         $LCTL set_param fail_loc=0x31c
14087
14088         # ignore dd failure
14089         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
14090
14091         $LCTL set_param fail_loc=0
14092         umount_client $MOUNT || error "umount failed"
14093         mount_client $MOUNT || error "mount failed"
14094 }
14095 run_test 232 "failed lock should not block umount"
14096
14097 test_233a() {
14098         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
14099         { skip "Need MDS version at least 2.3.64"; return; }
14100         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14101
14102         local fid=$($LFS path2fid $MOUNT)
14103         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14104                 error "cannot access $MOUNT using its FID '$fid'"
14105 }
14106 run_test 233a "checking that OBF of the FS root succeeds"
14107
14108 test_233b() {
14109         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
14110         { skip "Need MDS version at least 2.5.90"; return; }
14111         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14112
14113         local fid=$($LFS path2fid $MOUNT/.lustre)
14114         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14115                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
14116
14117         fid=$($LFS path2fid $MOUNT/.lustre/fid)
14118         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14119                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
14120 }
14121 run_test 233b "checking that OBF of the FS .lustre succeeds"
14122
14123 test_234() {
14124         local p="$TMP/sanityN-$TESTNAME.parameters"
14125         save_lustre_params client "llite.*.xattr_cache" > $p
14126         lctl set_param llite.*.xattr_cache 1 ||
14127                 { skip "xattr cache is not supported"; return 0; }
14128
14129         mkdir -p $DIR/$tdir || error "mkdir failed"
14130         touch $DIR/$tdir/$tfile || error "touch failed"
14131         # OBD_FAIL_LLITE_XATTR_ENOMEM
14132         $LCTL set_param fail_loc=0x1405
14133         # output of the form: attr 2 4 44 3 fc13 x86_64
14134         V=($(IFS=".-" rpm -q attr))
14135         if [[ ${V[1]} > 2 || ${V[2]} > 4 || ${V[3]} > 44 ||
14136               ${V[1]} = 2 && ${V[2]} = 4 && ${V[3]} = 44 && ${V[4]} > 6 ]]; then
14137                 # attr pre-2.4.44-7 had a bug with rc
14138                 # LU-3703 - SLES 11 and FC13 clients have older attr
14139                 getfattr -n user.attr $DIR/$tdir/$tfile &&
14140                         error "getfattr should have failed with ENOMEM"
14141         else
14142                 skip "LU-3703: attr version $(getfattr --version) too old"
14143         fi
14144         $LCTL set_param fail_loc=0x0
14145         rm -rf $DIR/$tdir
14146
14147         restore_lustre_params < $p
14148         rm -f $p
14149 }
14150 run_test 234 "xattr cache should not crash on ENOMEM"
14151
14152 test_235() {
14153         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
14154                 skip "Need MDS version at least 2.4.52" && return
14155         flock_deadlock $DIR/$tfile
14156         local RC=$?
14157         case $RC in
14158                 0)
14159                 ;;
14160                 124) error "process hangs on a deadlock"
14161                 ;;
14162                 *) error "error executing flock_deadlock $DIR/$tfile"
14163                 ;;
14164         esac
14165 }
14166 run_test 235 "LU-1715: flock deadlock detection does not work properly"
14167
14168 #LU-2935
14169 test_236() {
14170         check_swap_layouts_support && return 0
14171         test_mkdir -p -c1 $DIR/$tdir || error "mkdir $tdir failed"
14172
14173         local ref1=/etc/passwd
14174         local ref2=/etc/group
14175         local file1=$DIR/$tdir/f1
14176         local file2=$DIR/$tdir/f2
14177
14178         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
14179         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
14180         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
14181         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
14182         local fd=$(free_fd)
14183         local cmd="exec $fd<>$file2"
14184         eval $cmd
14185         rm $file2
14186         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
14187                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
14188         cmd="exec $fd>&-"
14189         eval $cmd
14190         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14191
14192         #cleanup
14193         rm -rf $DIR/$tdir
14194 }
14195 run_test 236 "Layout swap on open unlinked file"
14196
14197 # test to verify file handle related system calls
14198 # (name_to_handle_at/open_by_handle_at)
14199 # The new system calls are supported in glibc >= 2.14.
14200
14201 test_237() {
14202         echo "Test file_handle syscalls" > $DIR/$tfile ||
14203                 error "write failed"
14204         check_fhandle_syscalls $DIR/$tfile ||
14205                 error "check_fhandle_syscalls failed"
14206 }
14207 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
14208
14209 # LU-4659 linkea consistency
14210 test_238() {
14211         local server_version=$(lustre_version_code $SINGLEMDS)
14212
14213         [[ $server_version -gt $(version_code 2.5.57) ]] ||
14214                 [[ $server_version -gt $(version_code 2.5.1) &&
14215                    $server_version -lt $(version_code 2.5.50) ]] ||
14216                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
14217
14218         touch $DIR/$tfile
14219         ln $DIR/$tfile $DIR/$tfile.lnk
14220         touch $DIR/$tfile.new
14221         mv $DIR/$tfile.new $DIR/$tfile
14222         local fid1=$($LFS path2fid $DIR/$tfile)
14223         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
14224         local path1=$($LFS fid2path $FSNAME "$fid1")
14225         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
14226         local path2=$($LFS fid2path $FSNAME "$fid2")
14227         [ $tfile.lnk == $path2 ] ||
14228                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
14229         rm -f $DIR/$tfile*
14230 }
14231 run_test 238 "Verify linkea consistency"
14232
14233 test_239() {
14234         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
14235                 skip "Need MDS version at least 2.5.60" && return
14236         local list=$(comma_list $(mdts_nodes))
14237
14238         mkdir -p $DIR/$tdir
14239         createmany -o $DIR/$tdir/f- 5000
14240         unlinkmany $DIR/$tdir/f- 5000
14241         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.1) ] &&
14242                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
14243         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
14244                         osp.*MDT*.sync_in_flight" | calc_sum)
14245         [ "$changes" -eq 0 ] || error "$changes not synced"
14246 }
14247 run_test 239 "osp_sync test"
14248
14249 test_239a() { #LU-5297
14250         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14251         touch $DIR/$tfile
14252         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
14253         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
14254         chgrp $RUNAS_GID $DIR/$tfile
14255         wait_delete_completed
14256 }
14257 run_test 239a "process invalid osp sync record correctly"
14258
14259 test_239b() { #LU-5297
14260         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14261         touch $DIR/$tfile1
14262         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
14263         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
14264         chgrp $RUNAS_GID $DIR/$tfile1
14265         wait_delete_completed
14266         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14267         touch $DIR/$tfile2
14268         chgrp $RUNAS_GID $DIR/$tfile2
14269         wait_delete_completed
14270 }
14271 run_test 239b "process osp sync record with ENOMEM error correctly"
14272
14273 test_240() {
14274         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14275         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14276
14277         mkdir -p $DIR/$tdir
14278
14279         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14280                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14281         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14282                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14283
14284         umount_client $MOUNT || error "umount failed"
14285         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14286         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14287         mount_client $MOUNT || error "failed to mount client"
14288
14289         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14290         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14291 }
14292 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14293
14294 test_241_bio() {
14295         for LOOP in $(seq $1); do
14296                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14297                 cancel_lru_locks osc || true
14298         done
14299 }
14300
14301 test_241_dio() {
14302         for LOOP in $(seq $1); do
14303                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14304                                                 iflag=direct 2>/dev/null
14305         done
14306 }
14307
14308 test_241a() { # was test_241
14309         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14310         ls -la $DIR/$tfile
14311         cancel_lru_locks osc
14312         test_241_bio 1000 &
14313         PID=$!
14314         test_241_dio 1000
14315         wait $PID
14316 }
14317 run_test 241a "bio vs dio"
14318
14319 test_241b() {
14320         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14321         ls -la $DIR/$tfile
14322         test_241_dio 1000 &
14323         PID=$!
14324         test_241_dio 1000
14325         wait $PID
14326 }
14327 run_test 241b "dio vs dio"
14328
14329 test_242() {
14330         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14331         mkdir -p $DIR/$tdir
14332         touch $DIR/$tdir/$tfile
14333
14334         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14335         do_facet mds1 lctl set_param fail_loc=0x105
14336         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14337
14338         do_facet mds1 lctl set_param fail_loc=0
14339         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14340 }
14341 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14342
14343 test_243()
14344 {
14345         test_mkdir -p $DIR/$tdir
14346         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14347 }
14348 run_test 243 "various group lock tests"
14349
14350 test_244()
14351 {
14352         test_mkdir -p $DIR/$tdir
14353         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14354         sendfile_grouplock $DIR/$tdir/$tfile || \
14355                 error "sendfile+grouplock failed"
14356         rm -rf $DIR/$tdir
14357 }
14358 run_test 244 "sendfile with group lock tests"
14359
14360 test_245() {
14361         local flagname="multi_mod_rpcs"
14362         local connect_data_name="max_mod_rpcs"
14363         local out
14364
14365         # check if multiple modify RPCs flag is set
14366         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14367                 grep "connect_flags:")
14368         echo "$out"
14369
14370         echo "$out" | grep -qw $flagname
14371         if [ $? -ne 0 ]; then
14372                 echo "connect flag $flagname is not set"
14373                 return
14374         fi
14375
14376         # check if multiple modify RPCs data is set
14377         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14378         echo "$out"
14379
14380         echo "$out" | grep -qw $connect_data_name ||
14381                 error "import should have connect data $connect_data_name"
14382 }
14383 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14384
14385 test_246() { # LU-7371
14386         remote_ost_nodsh && skip "remote OST with nodsh" && return
14387         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14388                 skip "Need OST version >= 2.7.62" && return 0
14389         do_facet ost1 $LCTL set_param fail_val=4095
14390 #define OBD_FAIL_OST_READ_SIZE          0x234
14391         do_facet ost1 $LCTL set_param fail_loc=0x234
14392         $LFS setstripe $DIR/$tfile -i 0 -c 1
14393         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14394         cancel_lru_locks $FSNAME-OST0000
14395         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14396 }
14397 run_test 246 "Read file of size 4095 should return right length"
14398
14399 test_247a() {
14400         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14401                 grep -q subtree ||
14402                 { skip "Fileset feature is not supported"; return; }
14403
14404         local submount=${MOUNT}_$tdir
14405
14406         mkdir $MOUNT/$tdir
14407         mkdir -p $submount || error "mkdir $submount failed"
14408         FILESET="$FILESET/$tdir" mount_client $submount ||
14409                 error "mount $submount failed"
14410         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14411         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14412                 error "read $MOUNT/$tdir/$tfile failed"
14413         umount_client $submount || error "umount $submount failed"
14414         rmdir $submount
14415 }
14416 run_test 247a "mount subdir as fileset"
14417
14418 test_247b() {
14419         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14420                 { skip "Fileset feature is not supported"; return; }
14421
14422         local submount=${MOUNT}_$tdir
14423
14424         rm -rf $MOUNT/$tdir
14425         mkdir -p $submount || error "mkdir $submount failed"
14426         SKIP_FILESET=1
14427         FILESET="$FILESET/$tdir" mount_client $submount &&
14428                 error "mount $submount should fail"
14429         rmdir $submount
14430 }
14431 run_test 247b "mount subdir that dose not exist"
14432
14433 test_247c() {
14434         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14435                 { skip "Fileset feature is not supported"; return; }
14436
14437         local submount=${MOUNT}_$tdir
14438
14439         mkdir -p $MOUNT/$tdir/dir1
14440         mkdir -p $submount || error "mkdir $submount failed"
14441         FILESET="$FILESET/$tdir" mount_client $submount ||
14442                 error "mount $submount failed"
14443         local fid=$($LFS path2fid $MOUNT/)
14444         $LFS fid2path $submount $fid && error "fid2path should fail"
14445         umount_client $submount || error "umount $submount failed"
14446         rmdir $submount
14447 }
14448 run_test 247c "running fid2path outside root"
14449
14450 test_247d() {
14451         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14452                 { skip "Fileset feature is not supported"; return; }
14453
14454         local submount=${MOUNT}_$tdir
14455
14456         mkdir -p $MOUNT/$tdir/dir1
14457         mkdir -p $submount || error "mkdir $submount failed"
14458         FILESET="$FILESET/$tdir" mount_client $submount ||
14459                 error "mount $submount failed"
14460         local fid=$($LFS path2fid $submount/dir1)
14461         $LFS fid2path $submount $fid || error "fid2path should succeed"
14462         umount_client $submount || error "umount $submount failed"
14463         rmdir $submount
14464 }
14465 run_test 247d "running fid2path inside root"
14466
14467 # LU-8037
14468 test_247e() {
14469         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14470                 grep -q subtree ||
14471                 { skip "Fileset feature is not supported"; return; }
14472
14473         local submount=${MOUNT}_$tdir
14474
14475         mkdir $MOUNT/$tdir
14476         mkdir -p $submount || error "mkdir $submount failed"
14477         FILESET="$FILESET/.." mount_client $submount &&
14478                 error "mount $submount should fail"
14479         rmdir $submount
14480 }
14481 run_test 247e "mount .. as fileset"
14482
14483 test_248() {
14484         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14485         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14486
14487         # create a large file for fast read verification
14488         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14489
14490         # make sure the file is created correctly
14491         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14492                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14493
14494         echo "Test 1: verify that fast read is 4 times faster on cache read"
14495
14496         # small read with fast read enabled
14497         $LCTL set_param -n llite.*.fast_read=1
14498         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14499                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14500                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14501         # small read with fast read disabled
14502         $LCTL set_param -n llite.*.fast_read=0
14503         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14504                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14505                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14506
14507         # verify that fast read is 4 times faster for cache read
14508         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
14509                 error_not_in_vm "fast read was not 4 times faster: " \
14510                            "$t_fast vs $t_slow"
14511
14512         echo "Test 2: verify the performance between big and small read"
14513         $LCTL set_param -n llite.*.fast_read=1
14514
14515         # 1k non-cache read
14516         cancel_lru_locks osc
14517         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14518                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14519                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14520
14521         # 1M non-cache read
14522         cancel_lru_locks osc
14523         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
14524                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14525                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14526
14527         # verify that big IO is not 4 times faster than small IO
14528         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
14529                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
14530
14531         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
14532         rm -f $DIR/$tfile
14533 }
14534 run_test 248 "fast read verification"
14535
14536 test_249() { # LU-7890
14537         rm -f $DIR/$tfile
14538         $SETSTRIPE -c 1 $DIR/$tfile
14539
14540         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
14541         skip "Need at least version 2.8.54"
14542
14543         # Offset 2T == 4k * 512M
14544         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
14545                 error "dd to 2T offset failed"
14546 }
14547 run_test 249 "Write above 2T file size"
14548
14549 test_250() {
14550         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
14551          && skip "no 16TB file size limit on ZFS" && return
14552
14553         $SETSTRIPE -c 1 $DIR/$tfile
14554         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
14555         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
14556         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
14557         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
14558                 conv=notrunc,fsync && error "append succeeded"
14559         return 0
14560 }
14561 run_test 250 "Write above 16T limit"
14562
14563 test_251() {
14564         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
14565
14566         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
14567         #Skip once - writing the first stripe will succeed
14568         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14569         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
14570                 error "short write happened"
14571
14572         $LCTL set_param fail_loc=0xa0001407 fail_val=1
14573         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
14574                 error "short read happened"
14575
14576         rm -f $DIR/$tfile
14577 }
14578 run_test 251 "Handling short read and write correctly"
14579
14580 test_252() {
14581         local tgt
14582         local dev
14583         local out
14584         local uuid
14585         local num
14586         local gen
14587
14588         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14589         remote_ost_nodsh && skip "remote OST with nodsh" && return
14590         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
14591              "$(facet_fstype mds1)" != "ldiskfs" ]; then
14592                 skip "ldiskfs only test"
14593                 return
14594         fi
14595
14596         # check lr_reader on OST0000
14597         tgt=ost1
14598         dev=$(facet_device $tgt)
14599         out=$(do_facet $tgt $LR_READER $dev)
14600         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14601         echo "$out"
14602         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
14603         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
14604                 error "Invalid uuid returned by $LR_READER on target $tgt"
14605         echo -e "uuid returned by $LR_READER is '$uuid'\n"
14606
14607         # check lr_reader -c on MDT0000
14608         tgt=mds1
14609         dev=$(facet_device $tgt)
14610         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
14611                 echo "$LR_READER does not support additional options"
14612                 return 0
14613         fi
14614         out=$(do_facet $tgt $LR_READER -c $dev)
14615         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14616         echo "$out"
14617         num=$(echo "$out" | grep -c "mdtlov")
14618         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
14619                 error "Invalid number of mdtlov clients returned by $LR_READER"
14620         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
14621
14622         # check lr_reader -cr on MDT0000
14623         out=$(do_facet $tgt $LR_READER -cr $dev)
14624         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
14625         echo "$out"
14626         echo "$out" | grep -q "^reply_data:$" ||
14627                 error "$LR_READER should have returned 'reply_data' section"
14628         num=$(echo "$out" | grep -c "client_generation")
14629         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
14630 }
14631 run_test 252 "check lr_reader tool"
14632
14633 test_253_fill_ost() {
14634         local size_mb #how many MB should we write to pass watermark
14635         local lwm=$3  #low watermark
14636         local free_10mb #10% of free space
14637
14638         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14639         size_mb=$((free_kb / 1024 - lwm))
14640         free_10mb=$((free_kb / 10240))
14641         #If 10% of free space cross low watermark use it
14642         if (( free_10mb > size_mb )); then
14643                 size_mb=$free_10mb
14644         else
14645                 #At least we need to store 1.1 of difference between
14646                 #free space and low watermark
14647                 size_mb=$((size_mb + size_mb / 10))
14648         fi
14649         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
14650                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
14651                          oflag=append conv=notrunc
14652         fi
14653
14654         sleep_maxage
14655
14656         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
14657         echo "OST still has $((free_kb / 1024)) mbytes free"
14658 }
14659
14660 test_253() {
14661         local ostidx=0
14662         local rc=0
14663
14664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14665         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14666         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
14667
14668         local ost_name=$($LFS osts |
14669                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
14670         # on the mdt's osc
14671         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
14672         do_facet $SINGLEMDS $LCTL get_param -n \
14673                 osp.$mdtosc_proc1.reserved_mb_high ||
14674                 { skip  "remote MDS does not support reserved_mb_high" &&
14675                   return; }
14676
14677         rm -rf $DIR/$tdir
14678         wait_mds_ost_sync
14679         wait_delete_completed
14680         mkdir $DIR/$tdir
14681
14682         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
14683                         osp.$mdtosc_proc1.reserved_mb_high)
14684         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
14685                         osp.$mdtosc_proc1.reserved_mb_low)
14686         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
14687
14688         if ! combined_mgs_mds ; then
14689                 mount_mgs_client
14690         fi
14691         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
14692         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
14693                 error "Adding $ost_name to pool failed"
14694
14695         # Wait for client to see a OST at pool
14696         wait_update $HOSTNAME "$LCTL get_param -n
14697                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
14698                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
14699                 error "Client can not see the pool"
14700         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
14701                 error "Setstripe failed"
14702
14703         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
14704         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
14705         echo "OST still has $((blocks/1024)) mbytes free"
14706
14707         local new_lwm=$((blocks/1024-10))
14708         do_facet $SINGLEMDS $LCTL set_param \
14709                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
14710         do_facet $SINGLEMDS $LCTL set_param \
14711                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
14712
14713         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14714
14715         #First enospc could execute orphan deletion so repeat.
14716         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
14717
14718         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14719                         osp.$mdtosc_proc1.prealloc_status)
14720         echo "prealloc_status $oa_status"
14721
14722         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
14723                 error "File creation should fail"
14724         #object allocation was stopped, but we still able to append files
14725         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
14726                 error "Append failed"
14727         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
14728
14729         wait_delete_completed
14730
14731         sleep_maxage
14732
14733         for i in $(seq 10 12); do
14734                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
14735                         error "File creation failed after rm";
14736         done
14737
14738         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
14739                         osp.$mdtosc_proc1.prealloc_status)
14740         echo "prealloc_status $oa_status"
14741
14742         if (( oa_status != 0 )); then
14743                 error "Object allocation still disable after rm"
14744         fi
14745         do_facet $SINGLEMDS $LCTL set_param \
14746                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
14747         do_facet $SINGLEMDS $LCTL set_param \
14748                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
14749
14750
14751         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
14752                 error "Remove $ost_name from pool failed"
14753         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
14754                 error "Pool destroy fialed"
14755
14756         if ! combined_mgs_mds ; then
14757                 umount_mgs_client
14758         fi
14759 }
14760 run_test 253 "Check object allocation limit"
14761
14762 test_254() {
14763         local cl_user
14764
14765         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14766         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14767         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
14768                 { skip "MDS does not support changelog_size" && return; }
14769
14770         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
14771         echo "Registered as changelog user $cl_user"
14772
14773         $LFS changelog_clear $MDT0 $cl_user 0
14774
14775         local size1=$(do_facet mds1 \
14776                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14777         echo "Changelog size $size1"
14778
14779         rm -rf $DIR/$tdir
14780         $LFS mkdir -i 0 $DIR/$tdir
14781         # change something
14782         mkdir -p $DIR/$tdir/pics/2008/zachy
14783         touch $DIR/$tdir/pics/2008/zachy/timestamp
14784         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
14785         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14786         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14787         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14788         rm $DIR/$tdir/pics/desktop.jpg
14789
14790         local size2=$(do_facet mds1 \
14791                       $LCTL get_param -n mdd.$MDT0.changelog_size)
14792         echo "Changelog size after work $size2"
14793
14794         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
14795
14796         if (( size2 <= size1 )); then
14797                 error "Changelog size after work should be greater than original"
14798         fi
14799         return 0
14800 }
14801 run_test 254 "Check changelog size"
14802
14803 ladvise_no_type()
14804 {
14805         local type=$1
14806         local file=$2
14807
14808         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
14809                 awk -F: '{print $2}' | grep $type > /dev/null
14810         if [ $? -ne 0 ]; then
14811                 return 0
14812         fi
14813         return 1
14814 }
14815
14816 ladvise_no_ioctl()
14817 {
14818         local file=$1
14819
14820         lfs ladvise -a willread $file > /dev/null 2>&1
14821         if [ $? -eq 0 ]; then
14822                 return 1
14823         fi
14824
14825         lfs ladvise -a willread $file 2>&1 |
14826                 grep "Inappropriate ioctl for device" > /dev/null
14827         if [ $? -eq 0 ]; then
14828                 return 0
14829         fi
14830         return 1
14831 }
14832
14833 percent() {
14834         bc <<<"scale=2; ($1 - $2) * 100 / $2"
14835 }
14836
14837 # run a random read IO workload
14838 # usage: random_read_iops <filename> <filesize> <iosize>
14839 random_read_iops() {
14840         local file=$1
14841         local fsize=$2
14842         local iosize=${3:-4096}
14843
14844         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
14845                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
14846 }
14847
14848 drop_file_oss_cache() {
14849         local file="$1"
14850         local nodes="$2"
14851
14852         $LFS ladvise -a dontneed $file 2>/dev/null ||
14853                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
14854 }
14855
14856 ladvise_willread_performance()
14857 {
14858         local repeat=10
14859         local average_origin=0
14860         local average_cache=0
14861         local average_ladvise=0
14862
14863         for ((i = 1; i <= $repeat; i++)); do
14864                 echo "Iter $i/$repeat: reading without willread hint"
14865                 cancel_lru_locks osc
14866                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14867                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
14868                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
14869                 average_origin=$(bc <<<"$average_origin + $speed_origin")
14870
14871                 cancel_lru_locks osc
14872                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
14873                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
14874                 average_cache=$(bc <<<"$average_cache + $speed_cache")
14875
14876                 cancel_lru_locks osc
14877                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
14878                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
14879                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
14880                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
14881                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
14882         done
14883         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
14884         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
14885         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
14886
14887         speedup_cache=$(percent $average_cache $average_origin)
14888         speedup_ladvise=$(percent $average_ladvise $average_origin)
14889
14890         echo "Average uncached read: $average_origin"
14891         echo "Average speedup with OSS cached read: " \
14892                 "$average_cache = +$speedup_cache%"
14893         echo "Average speedup with ladvise willread: " \
14894                 "$average_ladvise = +$speedup_ladvise%"
14895
14896         local lowest_speedup=20
14897         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
14898                 echo "Speedup with OSS cached read less than $lowest_speedup%, " \
14899                         "got $average_cache%. Skipping ladvise willread check."
14900                 return 0
14901         fi
14902
14903         # the test won't work on ZFS until it supports 'ladvise dontneed', but
14904         # it is still good to run until then to exercise 'ladvise willread'
14905         ! $LFS ladvise -a dontneed $DIR/$tfile &&
14906                 [ "$(facet_fstype ost1)" = "zfs" ] &&
14907                 echo "osd-zfs does not support dontneed or drop_caches" &&
14908                 return 0
14909
14910         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
14911         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
14912                 error_not_in_vm "Speedup with willread is less than " \
14913                         "$lowest_speedup%, got $average_ladvise%"
14914 }
14915
14916 test_255a() {
14917         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
14918                 skip "lustre < 2.8.54 does not support ladvise " && return
14919         remote_ost_nodsh && skip "remote OST with nodsh" && return
14920
14921         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
14922
14923         ladvise_no_type willread $DIR/$tfile &&
14924                 skip "willread ladvise is not supported" && return
14925
14926         ladvise_no_ioctl $DIR/$tfile &&
14927                 skip "ladvise ioctl is not supported" && return
14928
14929         local size_mb=100
14930         local size=$((size_mb * 1048576))
14931         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
14932                 error "dd to $DIR/$tfile failed"
14933
14934         lfs ladvise -a willread $DIR/$tfile ||
14935                 error "Ladvise failed with no range argument"
14936
14937         lfs ladvise -a willread -s 0 $DIR/$tfile ||
14938                 error "Ladvise failed with no -l or -e argument"
14939
14940         lfs ladvise -a willread -e 1 $DIR/$tfile ||
14941                 error "Ladvise failed with only -e argument"
14942
14943         lfs ladvise -a willread -l 1 $DIR/$tfile ||
14944                 error "Ladvise failed with only -l argument"
14945
14946         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
14947                 error "End offset should not be smaller than start offset"
14948
14949         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
14950                 error "End offset should not be equal to start offset"
14951
14952         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
14953                 error "Ladvise failed with overflowing -s argument"
14954
14955         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
14956                 error "Ladvise failed with overflowing -e argument"
14957
14958         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
14959                 error "Ladvise failed with overflowing -l argument"
14960
14961         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
14962                 error "Ladvise succeeded with conflicting -l and -e arguments"
14963
14964         echo "Synchronous ladvise should wait"
14965         local delay=4
14966 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
14967         do_nodes $(comma_list $(osts_nodes)) \
14968                 $LCTL set_param fail_val=$delay fail_loc=0x237
14969
14970         local start_ts=$SECONDS
14971         lfs ladvise -a willread $DIR/$tfile ||
14972                 error "Ladvise failed with no range argument"
14973         local end_ts=$SECONDS
14974         local inteval_ts=$((end_ts - start_ts))
14975
14976         if [ $inteval_ts -lt $(($delay - 1)) ]; then
14977                 error "Synchronous advice didn't wait reply"
14978         fi
14979
14980         echo "Asynchronous ladvise shouldn't wait"
14981         local start_ts=$SECONDS
14982         lfs ladvise -a willread -b $DIR/$tfile ||
14983                 error "Ladvise failed with no range argument"
14984         local end_ts=$SECONDS
14985         local inteval_ts=$((end_ts - start_ts))
14986
14987         if [ $inteval_ts -gt $(($delay / 2)) ]; then
14988                 error "Asynchronous advice blocked"
14989         fi
14990
14991         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
14992         ladvise_willread_performance
14993 }
14994 run_test 255a "check 'lfs ladvise -a willread'"
14995
14996 facet_meminfo() {
14997         local facet=$1
14998         local info=$2
14999
15000         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
15001 }
15002
15003 test_255b() {
15004         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15005                 skip "lustre < 2.8.54 does not support ladvise " && return
15006         remote_ost_nodsh && skip "remote OST with nodsh" && return
15007
15008         lfs setstripe -c 1 -i 0 $DIR/$tfile
15009
15010         ladvise_no_type dontneed $DIR/$tfile &&
15011                 skip "dontneed ladvise is not supported" && return
15012
15013         ladvise_no_ioctl $DIR/$tfile &&
15014                 skip "ladvise ioctl is not supported" && return
15015
15016         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15017                 skip "lustre < 2.8.54 does not support ladvise" && return
15018
15019         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15020                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15021                 skip "zfs-osd does not support 'ladvise dontneed'" && return
15022
15023         local size_mb=100
15024         local size=$((size_mb * 1048576))
15025         # In order to prevent disturbance of other processes, only check 3/4
15026         # of the memory usage
15027         local kibibytes=$((size_mb * 1024 * 3 / 4))
15028
15029         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15030                 error "dd to $DIR/$tfile failed"
15031
15032         local total=$(facet_meminfo ost1 MemTotal)
15033         echo "Total memory: $total KiB"
15034
15035         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
15036         local before_read=$(facet_meminfo ost1 Cached)
15037         echo "Cache used before read: $before_read KiB"
15038
15039         lfs ladvise -a willread $DIR/$tfile ||
15040                 error "Ladvise willread failed"
15041         local after_read=$(facet_meminfo ost1 Cached)
15042         echo "Cache used after read: $after_read KiB"
15043
15044         lfs ladvise -a dontneed $DIR/$tfile ||
15045                 error "Ladvise dontneed again failed"
15046         local no_read=$(facet_meminfo ost1 Cached)
15047         echo "Cache used after dontneed ladvise: $no_read KiB"
15048
15049         if [ $total -lt $((before_read + kibibytes)) ]; then
15050                 echo "Memory is too small, abort checking"
15051                 return 0
15052         fi
15053
15054         if [ $((before_read + kibibytes)) -gt $after_read ]; then
15055                 error "Ladvise willread should use more memory" \
15056                         "than $kibibytes KiB"
15057         fi
15058
15059         if [ $((no_read + kibibytes)) -gt $after_read ]; then
15060                 error "Ladvise dontneed should release more memory" \
15061                         "than $kibibytes KiB"
15062         fi
15063 }
15064 run_test 255b "check 'lfs ladvise -a dontneed'"
15065
15066 test_256() {
15067         local cl_user
15068         local cat_sl
15069         local mdt_dev
15070
15071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15072         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15073         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
15074                 skip "ldiskfs only test" && return
15075
15076         mdt_dev=$(mdsdevname 1)
15077         echo $mdt_dev
15078         cl_user=$(do_facet mds1 \
15079         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
15080         if [[ -n $cl_user ]]; then
15081                 skip "active changelog user"
15082                 return
15083         fi
15084
15085         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15086         echo "Registered as changelog user $cl_user"
15087
15088         rm -rf $DIR/$tdir
15089         mkdir -p $DIR/$tdir
15090
15091         $LFS changelog_clear $MDT0 $cl_user 0
15092
15093         # change something
15094         touch $DIR/$tdir/{1..10}
15095
15096         # stop the MDT
15097         stop mds1 || error "Fail to stop MDT."
15098
15099         # remount the MDT
15100         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
15101
15102         #after mount new plainllog is used
15103         touch $DIR/$tdir/{11..19}
15104         local TEMP256FILE=$(mktemp TEMP256XXXXXX)
15105         cat_sl=$(do_facet mds1 \
15106         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15107          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15108         rm $TEMP256FILE
15109
15110         if (( cat_sl != 2 )); then
15111                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15112                 error "Changelog catalog has wrong number of slots $cat_sl"
15113         fi
15114
15115         $LFS changelog_clear $MDT0 $cl_user 0
15116
15117         TEMP256FILE=$(mktemp TEMP256XXXXXX)
15118         cat_sl=$(do_facet mds1 \
15119         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15120          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15121         rm $TEMP256FILE
15122
15123         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15124
15125         if (( cat_sl == 2 )); then
15126                 error "Empty plain llog was not deleted from changelog catalog"
15127         fi
15128         if (( cat_sl != 1 )); then
15129                 error "Active plain llog shouldn\`t be deleted from catalog"
15130         fi
15131 }
15132 run_test 256 "Check llog delete for empty and not full state"
15133
15134 test_257() {
15135         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15136         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15137                 skip "Need MDS version at least 2.8.55" && return
15138
15139         test_mkdir -p $DIR/$tdir
15140
15141         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
15142                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
15143         stat $DIR/$tdir
15144
15145 #define OBD_FAIL_MDS_XATTR_REP                  0x161
15146         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15147         local facet=mds$((mdtidx + 1))
15148         set_nodes_failloc $(facet_active_host $facet) 0x80000161
15149         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
15150
15151         stop $facet || error "stop MDS failed"
15152         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
15153                 error "start MDS fail"
15154 }
15155 run_test 257 "xattr locks are not lost"
15156
15157 # Verify we take the i_mutex when security requires it
15158 test_258a() {
15159 #define OBD_FAIL_IMUTEX_SEC 0x141c
15160         $LCTL set_param fail_loc=0x141c
15161         touch $DIR/$tfile
15162         chmod u+s $DIR/$tfile
15163         chmod a+rwx $DIR/$tfile
15164         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15165         RC=$?
15166         if [ $RC -ne 0 ]; then
15167                 error "error, failed to take i_mutex, rc=$?"
15168         fi
15169         rm -f $DIR/$tfile
15170 }
15171 run_test 258a
15172
15173 # Verify we do NOT take the i_mutex in the normal case
15174 test_258b() {
15175 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
15176         $LCTL set_param fail_loc=0x141d
15177         touch $DIR/$tfile
15178         chmod a+rwx $DIR
15179         chmod a+rw $DIR/$tfile
15180         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15181         RC=$?
15182         if [ $RC -ne 0 ]; then
15183                 error "error, took i_mutex unnecessarily, rc=$?"
15184         fi
15185         rm -f $DIR/$tfile
15186
15187 }
15188 run_test 258b "verify i_mutex security behavior"
15189
15190 test_260() {
15191 #define OBD_FAIL_MDC_CLOSE               0x806
15192         $LCTL set_param fail_loc=0x80000806
15193         touch $DIR/$tfile
15194
15195 }
15196 run_test 260 "Check mdc_close fail"
15197
15198 test_275() {
15199         remote_ost_nodsh && skip "remote OST with nodsh" && return
15200         [ $(lustre_version_code ost1) -lt $(version_code 2.10.1) ] &&
15201                 skip "Need OST version >= 2.10.1" && return 0
15202
15203         local file=$DIR/$tfile
15204         local oss
15205
15206         oss=$(comma_list $(osts_nodes))
15207
15208         dd if=/dev/urandom of=$file bs=1M count=2 ||
15209                 error "failed to create a file"
15210         cancel_lru_locks osc
15211
15212         #lock 1
15213         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
15214                 error "failed to read a file"
15215
15216 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
15217         $LCTL set_param fail_loc=0x8000031f
15218
15219         cancel_lru_locks osc &
15220         sleep 1
15221
15222 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
15223         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
15224         #IO takes another lock, but matches the PENDING one
15225         #and places it to the IO RPC
15226         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
15227                 error "failed to read a file with PENDING lock"
15228 }
15229 run_test 275 "Read on a canceled duplicate lock"
15230
15231 test_276() {
15232         remote_ost_nodsh && skip "remote OST with nodsh" && return
15233         local pid
15234
15235         do_facet ost1 "(while true; do \
15236                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
15237                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
15238         pid=$!
15239
15240         for LOOP in $(seq 20); do
15241                 stop ost1
15242                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
15243         done
15244         kill -9 $pid
15245         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
15246                 rm $TMP/sanity_276_pid"
15247 }
15248 run_test 276 "Race between mount and obd_statfs"
15249
15250 cleanup_test_300() {
15251         trap 0
15252         umask $SAVE_UMASK
15253 }
15254 test_striped_dir() {
15255         local mdt_index=$1
15256         local stripe_count
15257         local stripe_index
15258
15259         mkdir -p $DIR/$tdir
15260
15261         SAVE_UMASK=$(umask)
15262         trap cleanup_test_300 RETURN EXIT
15263
15264         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
15265                                                 $DIR/$tdir/striped_dir ||
15266                 error "set striped dir error"
15267
15268         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
15269         [ "$mode" = "755" ] || error "expect 755 got $mode"
15270
15271         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
15272                 error "getdirstripe failed"
15273         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
15274         if [ "$stripe_count" != "2" ]; then
15275                 error "1:stripe_count is $stripe_count, expect 2"
15276         fi
15277         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
15278         if [ "$stripe_count" != "2" ]; then
15279                 error "2:stripe_count is $stripe_count, expect 2"
15280         fi
15281
15282         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
15283         if [ "$stripe_index" != "$mdt_index" ]; then
15284                 error "stripe_index is $stripe_index, expect $mdt_index"
15285         fi
15286
15287         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15288                 error "nlink error after create striped dir"
15289
15290         mkdir $DIR/$tdir/striped_dir/a
15291         mkdir $DIR/$tdir/striped_dir/b
15292
15293         stat $DIR/$tdir/striped_dir/a ||
15294                 error "create dir under striped dir failed"
15295         stat $DIR/$tdir/striped_dir/b ||
15296                 error "create dir under striped dir failed"
15297
15298         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
15299                 error "nlink error after mkdir"
15300
15301         rmdir $DIR/$tdir/striped_dir/a
15302         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
15303                 error "nlink error after rmdir"
15304
15305         rmdir $DIR/$tdir/striped_dir/b
15306         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
15307                 error "nlink error after rmdir"
15308
15309         chattr +i $DIR/$tdir/striped_dir
15310         createmany -o $DIR/$tdir/striped_dir/f 10 &&
15311                 error "immutable flags not working under striped dir!"
15312         chattr -i $DIR/$tdir/striped_dir
15313
15314         rmdir $DIR/$tdir/striped_dir ||
15315                 error "rmdir striped dir error"
15316
15317         cleanup_test_300
15318
15319         true
15320 }
15321
15322 test_300a() {
15323         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15324                 skip "skipped for lustre < 2.7.0" && return
15325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15326         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15327
15328         test_striped_dir 0 || error "failed on striped dir on MDT0"
15329         test_striped_dir 1 || error "failed on striped dir on MDT0"
15330 }
15331 run_test 300a "basic striped dir sanity test"
15332
15333 test_300b() {
15334         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15335                 skip "skipped for lustre < 2.7.0" && return
15336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15337         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15338         local i
15339         local mtime1
15340         local mtime2
15341         local mtime3
15342
15343         test_mkdir $DIR/$tdir || error "mkdir fail"
15344         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15345                 error "set striped dir error"
15346         for ((i=0; i<10; i++)); do
15347                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
15348                 sleep 1
15349                 touch $DIR/$tdir/striped_dir/file_$i ||
15350                                         error "touch error $i"
15351                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
15352                 [ $mtime1 -eq $mtime2 ] &&
15353                         error "mtime not change after create"
15354                 sleep 1
15355                 rm -f $DIR/$tdir/striped_dir/file_$i ||
15356                                         error "unlink error $i"
15357                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
15358                 [ $mtime2 -eq $mtime3 ] &&
15359                         error "mtime did not change after unlink"
15360         done
15361         true
15362 }
15363 run_test 300b "check ctime/mtime for striped dir"
15364
15365 test_300c() {
15366         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
15367                 skip "skipped for lustre < 2.7.0" && return
15368         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15369         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15370         local file_count
15371
15372         mkdir -p $DIR/$tdir
15373         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
15374                 error "set striped dir error"
15375
15376         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
15377                 error "chown striped dir failed"
15378
15379         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
15380                 error "create 5k files failed"
15381
15382         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
15383
15384         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
15385
15386         rm -rf $DIR/$tdir
15387 }
15388 run_test 300c "chown && check ls under striped directory"
15389
15390 test_300d() {
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 stripe_count
15396         local file
15397
15398         mkdir -p $DIR/$tdir
15399         $SETSTRIPE -c 2 $DIR/$tdir
15400
15401         #local striped directory
15402         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15403                 error "set striped dir error"
15404         createmany -o $DIR/$tdir/striped_dir/f 10 ||
15405                 error "create 10 files failed"
15406
15407         #remote striped directory
15408         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
15409                 error "set striped dir error"
15410         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
15411                 error "create 10 files failed"
15412
15413         for file in $(find $DIR/$tdir); do
15414                 stripe_count=$($GETSTRIPE -c $file)
15415                 [ $stripe_count -eq 2 ] ||
15416                         error "wrong stripe $stripe_count for $file"
15417         done
15418
15419         rm -rf $DIR/$tdir
15420 }
15421 run_test 300d "check default stripe under striped directory"
15422
15423 test_300e() {
15424         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15425                 skip "Need MDS version at least 2.7.55" && return
15426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15427         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15428         local stripe_count
15429         local file
15430
15431         mkdir -p $DIR/$tdir
15432
15433         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15434                 error "set striped dir error"
15435
15436         touch $DIR/$tdir/striped_dir/a
15437         touch $DIR/$tdir/striped_dir/b
15438         touch $DIR/$tdir/striped_dir/c
15439
15440         mkdir $DIR/$tdir/striped_dir/dir_a
15441         mkdir $DIR/$tdir/striped_dir/dir_b
15442         mkdir $DIR/$tdir/striped_dir/dir_c
15443
15444         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
15445                 error "set striped adir under striped dir error"
15446
15447         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
15448                 error "set striped bdir under striped dir error"
15449
15450         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
15451                 error "set striped cdir under striped dir error"
15452
15453         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
15454                 error "rename dir under striped dir fails"
15455
15456         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
15457                 error "rename dir under different stripes fails"
15458
15459         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
15460                 error "rename file under striped dir should succeed"
15461
15462         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
15463                 error "rename dir under striped dir should succeed"
15464
15465         rm -rf $DIR/$tdir
15466 }
15467 run_test 300e "check rename under striped directory"
15468
15469 test_300f() {
15470         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15471                 skip "Need MDS version at least 2.7.55" && return
15472         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15473         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15474         local stripe_count
15475         local file
15476
15477         rm -rf $DIR/$tdir
15478         mkdir -p $DIR/$tdir
15479
15480         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
15481                 error "set striped dir error"
15482
15483         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
15484                 error "set striped dir error"
15485
15486         touch $DIR/$tdir/striped_dir/a
15487         mkdir $DIR/$tdir/striped_dir/dir_a
15488         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
15489                 error "create striped dir under striped dir fails"
15490
15491         touch $DIR/$tdir/striped_dir1/b
15492         mkdir $DIR/$tdir/striped_dir1/dir_b
15493         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
15494                 error "create striped dir under striped dir fails"
15495
15496         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
15497                 error "rename dir under different striped dir should fail"
15498
15499         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
15500                 error "rename striped dir under diff striped dir should fail"
15501
15502         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
15503                 error "rename file under diff striped dirs fails"
15504
15505         rm -rf $DIR/$tdir
15506 }
15507 run_test 300f "check rename cross striped directory"
15508
15509 test_300_check_default_striped_dir()
15510 {
15511         local dirname=$1
15512         local default_count=$2
15513         local default_index=$3
15514         local stripe_count
15515         local stripe_index
15516         local dir_stripe_index
15517         local dir
15518
15519         echo "checking $dirname $default_count $default_index"
15520         $LFS setdirstripe -D -c $default_count -i $default_index \
15521                                 -t all_char $DIR/$tdir/$dirname ||
15522                 error "set default stripe on striped dir error"
15523         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
15524         [ $stripe_count -eq $default_count ] ||
15525                 error "expect $default_count get $stripe_count for $dirname"
15526
15527         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
15528         [ $stripe_index -eq $default_index ] ||
15529                 error "expect $default_index get $stripe_index for $dirname"
15530
15531         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
15532                                                 error "create dirs failed"
15533
15534         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
15535         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
15536         for dir in $(find $DIR/$tdir/$dirname/*); do
15537                 stripe_count=$($LFS getdirstripe -c $dir)
15538                 [ $stripe_count -eq $default_count ] ||
15539                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
15540                 error "stripe count $default_count != $stripe_count for $dir"
15541
15542                 stripe_index=$($LFS getdirstripe -i $dir)
15543                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
15544                         error "$stripe_index != $default_index for $dir"
15545
15546                 #check default stripe
15547                 stripe_count=$($LFS getdirstripe -D -c $dir)
15548                 [ $stripe_count -eq $default_count ] ||
15549                 error "default count $default_count != $stripe_count for $dir"
15550
15551                 stripe_index=$($LFS getdirstripe -D -i $dir)
15552                 [ $stripe_index -eq $default_index ] ||
15553                 error "default index $default_index != $stripe_index for $dir"
15554         done
15555         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
15556 }
15557
15558 test_300g() {
15559         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15560                 skip "Need MDS version at least 2.7.55" && return
15561         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15562         local dir
15563         local stripe_count
15564         local stripe_index
15565
15566         mkdir $DIR/$tdir
15567         mkdir $DIR/$tdir/normal_dir
15568
15569         #Checking when client cache stripe index
15570         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
15571         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
15572                 error "create striped_dir failed"
15573
15574         mkdir $DIR/$tdir/striped_dir/dir1 ||
15575                 error "create dir1 fails"
15576         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
15577         [ $stripe_index -eq 1 ] ||
15578                 error "dir1 expect 1 got $stripe_index"
15579
15580         $LFS setdirstripe -i2 $DIR/$tdir/striped_dir/dir2 ||
15581                 error "create dir2 fails"
15582         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir2)
15583         [ $stripe_index -eq 2 ] ||
15584                 error "dir2 expect 2 got $stripe_index"
15585
15586         #check default stripe count/stripe index
15587         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
15588         test_300_check_default_striped_dir normal_dir 1 0
15589         test_300_check_default_striped_dir normal_dir 2 1
15590         test_300_check_default_striped_dir normal_dir 2 -1
15591
15592         #delete default stripe information
15593         echo "delete default stripeEA"
15594         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
15595                 error "set default stripe on striped dir error"
15596
15597         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
15598         for dir in $(find $DIR/$tdir/normal_dir/*); do
15599                 stripe_count=$($LFS getdirstripe -c $dir)
15600                 [ $stripe_count -eq 0 ] ||
15601                         error "expect 1 get $stripe_count for $dir"
15602                 stripe_index=$($LFS getdirstripe -i $dir)
15603                 [ $stripe_index -eq 0 ] ||
15604                         error "expect 0 get $stripe_index for $dir"
15605         done
15606 }
15607 run_test 300g "check default striped directory for normal directory"
15608
15609 test_300h() {
15610         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15611                 skip "Need MDS version at least 2.7.55" && return
15612         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15613         local dir
15614         local stripe_count
15615
15616         mkdir $DIR/$tdir
15617         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
15618                                         $DIR/$tdir/striped_dir ||
15619                 error "set striped dir error"
15620
15621         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
15622         test_300_check_default_striped_dir striped_dir 1 0
15623         test_300_check_default_striped_dir striped_dir 2 1
15624         test_300_check_default_striped_dir striped_dir 2 -1
15625
15626         #delete default stripe information
15627         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
15628                 error "set default stripe on striped dir error"
15629
15630         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
15631         for dir in $(find $DIR/$tdir/striped_dir/*); do
15632                 stripe_count=$($LFS getdirstripe -c $dir)
15633                 [ $stripe_count -eq 0 ] ||
15634                         error "expect 1 get $stripe_count for $dir"
15635         done
15636 }
15637 run_test 300h "check default striped directory for striped directory"
15638
15639 test_300i() {
15640         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15641                 skip "Need MDS version at least 2.7.55" && return
15642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15643         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15644         local stripe_count
15645         local file
15646
15647         mkdir $DIR/$tdir
15648
15649         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15650                 error "set striped dir error"
15651
15652         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15653                 error "create files under striped dir failed"
15654
15655         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
15656                 error "set striped hashdir error"
15657
15658         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
15659                 error "create dir0 under hash dir failed"
15660         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
15661                 error "create dir1 under hash dir failed"
15662
15663         # unfortunately, we need to umount to clear dir layout cache for now
15664         # once we fully implement dir layout, we can drop this
15665         umount_client $MOUNT || error "umount failed"
15666         mount_client $MOUNT || error "mount failed"
15667
15668         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
15669         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
15670         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
15671
15672         #set the stripe to be unknown hash type
15673         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
15674         $LCTL set_param fail_loc=0x1901
15675         for ((i = 0; i < 10; i++)); do
15676                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
15677                         error "stat f-$i failed"
15678                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
15679         done
15680
15681         touch $DIR/$tdir/striped_dir/f0 &&
15682                 error "create under striped dir with unknown hash should fail"
15683
15684         $LCTL set_param fail_loc=0
15685
15686         umount_client $MOUNT || error "umount failed"
15687         mount_client $MOUNT || error "mount failed"
15688
15689         return 0
15690 }
15691 run_test 300i "client handle unknown hash type striped directory"
15692
15693 test_300j() {
15694         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15695         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15696                 skip "Need MDS version at least 2.7.55" && return
15697         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15698         local stripe_count
15699         local file
15700
15701         mkdir $DIR/$tdir
15702
15703         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
15704         $LCTL set_param fail_loc=0x1702
15705         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
15706                 error "set striped dir error"
15707
15708         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
15709                 error "create files under striped dir failed"
15710
15711         $LCTL set_param fail_loc=0
15712
15713         rm -rf $DIR/$tdir || error "unlink striped dir fails"
15714
15715         return 0
15716 }
15717 run_test 300j "test large update record"
15718
15719 test_300k() {
15720         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15721                 skip "Need MDS version at least 2.7.55" && return
15722         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15723         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15724         local stripe_count
15725         local file
15726
15727         mkdir $DIR/$tdir
15728
15729         #define OBD_FAIL_LARGE_STRIPE   0x1703
15730         $LCTL set_param fail_loc=0x1703
15731         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
15732                 error "set striped dir error"
15733         $LCTL set_param fail_loc=0
15734
15735         $LFS getdirstripe $DIR/$tdir/striped_dir ||
15736                 error "getstripeddir fails"
15737         rm -rf $DIR/$tdir/striped_dir ||
15738                 error "unlink striped dir fails"
15739
15740         return 0
15741 }
15742 run_test 300k "test large striped directory"
15743
15744 test_300l() {
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_index
15750
15751         test_mkdir -p $DIR/$tdir/striped_dir
15752         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
15753                         error "chown $RUNAS_ID failed"
15754         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
15755                 error "set default striped dir failed"
15756
15757         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
15758         $LCTL set_param fail_loc=0x80000158
15759         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
15760
15761         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
15762         [ $stripe_index -eq 1 ] ||
15763                 error "expect 1 get $stripe_index for $dir"
15764 }
15765 run_test 300l "non-root user to create dir under striped dir with stale layout"
15766
15767 test_300m() {
15768         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15769                 skip "Need MDS version at least 2.7.55" && return
15770         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15771         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
15772
15773         mkdir -p $DIR/$tdir/striped_dir
15774         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
15775                 error "set default stripes dir error"
15776
15777         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
15778
15779         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
15780         [ $stripe_count -eq 0 ] ||
15781                         error "expect 0 get $stripe_count for a"
15782
15783         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
15784                 error "set default stripes dir error"
15785
15786         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
15787
15788         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
15789         [ $stripe_count -eq 0 ] ||
15790                         error "expect 0 get $stripe_count for b"
15791
15792         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
15793                 error "set default stripes dir error"
15794
15795         mkdir $DIR/$tdir/striped_dir/c &&
15796                 error "default stripe_index is invalid, mkdir c should fails"
15797
15798         rm -rf $DIR/$tdir || error "rmdir fails"
15799 }
15800 run_test 300m "setstriped directory on single MDT FS"
15801
15802 cleanup_300n() {
15803         local list=$(comma_list $(mdts_nodes))
15804
15805         trap 0
15806         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15807 }
15808
15809 test_300n() {
15810         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15811                 skip "Need MDS version at least 2.7.55" && return
15812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15813         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15814         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15815
15816         local stripe_index
15817         local list=$(comma_list $(mdts_nodes))
15818
15819         trap cleanup_300n RETURN EXIT
15820         mkdir -p $DIR/$tdir
15821         chmod 777 $DIR/$tdir
15822         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
15823                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15824                 error "create striped dir succeeds with gid=0"
15825
15826         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15827         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
15828                 error "create striped dir fails with gid=-1"
15829
15830         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15831         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
15832                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
15833                 error "set default striped dir succeeds with gid=0"
15834
15835
15836         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
15837         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
15838                 error "set default striped dir fails with gid=-1"
15839
15840
15841         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
15842         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
15843                                         error "create test_dir fails"
15844         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
15845                                         error "create test_dir1 fails"
15846         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
15847                                         error "create test_dir2 fails"
15848         cleanup_300n
15849 }
15850 run_test 300n "non-root user to create dir under striped dir with default EA"
15851
15852 test_300o() {
15853         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
15854                 skip "Need MDS version at least 2.7.55" && return
15855         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15856         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15857         local numfree1
15858         local numfree2
15859
15860         mkdir -p $DIR/$tdir
15861
15862         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
15863         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
15864         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
15865                 skip "not enough free inodes $numfree1 $numfree2"
15866                 return
15867         fi
15868
15869         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
15870         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
15871         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
15872                 skip "not enough free space $numfree1 $numfree2"
15873                 return
15874         fi
15875
15876         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
15877                 error "setdirstripe fails"
15878
15879         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
15880                 error "create dirs fails"
15881
15882         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
15883         ls $DIR/$tdir/striped_dir > /dev/null ||
15884                 error "ls striped dir fails"
15885         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
15886                 error "unlink big striped dir fails"
15887 }
15888 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
15889
15890 test_300p() {
15891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15892         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15893         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15894
15895         mkdir -p $DIR/$tdir
15896
15897         #define OBD_FAIL_OUT_ENOSPC     0x1704
15898         do_facet mds2 lctl set_param fail_loc=0x80001704
15899         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
15900                         error "create striped directory should fail"
15901
15902         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
15903
15904         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
15905         true
15906 }
15907 run_test 300p "create striped directory without space"
15908
15909 test_300q() {
15910         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15911         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
15912
15913         local fd=$(free_fd)
15914         local cmd="exec $fd<$tdir"
15915         cd $DIR
15916         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
15917         eval $cmd
15918         cmd="exec $fd<&-"
15919         trap "eval $cmd" EXIT
15920         cd $tdir || error "cd $tdir fails"
15921         rmdir  ../$tdir || error "rmdir $tdir fails"
15922         mkdir local_dir && error "create dir succeeds"
15923         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
15924         eval $cmd
15925         return 0
15926 }
15927 run_test 300q "create remote directory under orphan directory"
15928
15929 prepare_remote_file() {
15930         mkdir $DIR/$tdir/src_dir ||
15931                 error "create remote source failed"
15932
15933         cp /etc/hosts $DIR/$tdir/src_dir/a || error
15934         touch $DIR/$tdir/src_dir/a
15935
15936         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
15937                 error "create remote target dir failed"
15938
15939         touch $DIR/$tdir/tgt_dir/b
15940
15941         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
15942                 error "rename dir cross MDT failed!"
15943
15944         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
15945                 error "src_child still exists after rename"
15946
15947         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
15948                 error "missing file(a) after rename"
15949
15950         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
15951                 error "diff after rename"
15952 }
15953
15954 test_310a() {
15955         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15956         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15957         local remote_file=$DIR/$tdir/tgt_dir/b
15958
15959         mkdir -p $DIR/$tdir
15960
15961         prepare_remote_file || error "prepare remote file failed"
15962
15963         #open-unlink file
15964         $OPENUNLINK $remote_file $remote_file || error
15965         $CHECKSTAT -a $remote_file || error
15966 }
15967 run_test 310a "open unlink remote file"
15968
15969 test_310b() {
15970         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
15971         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15972         local remote_file=$DIR/$tdir/tgt_dir/b
15973
15974         mkdir -p $DIR/$tdir
15975
15976         prepare_remote_file || error "prepare remote file failed"
15977
15978         ln $remote_file $DIR/$tfile || error "link failed for remote file"
15979         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
15980         $CHECKSTAT -t file $remote_file || error "check file failed"
15981 }
15982 run_test 310b "unlink remote file with multiple links while open"
15983
15984 test_310c() {
15985         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
15986         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15987         local remote_file=$DIR/$tdir/tgt_dir/b
15988
15989         mkdir -p $DIR/$tdir
15990
15991         prepare_remote_file || error "prepare remote file failed"
15992
15993         ln $remote_file $DIR/$tfile || error "link failed for remote file"
15994         multiop_bg_pause $remote_file O_uc ||
15995                         error "mulitop failed for remote file"
15996         MULTIPID=$!
15997         $MULTIOP $DIR/$tfile Ouc
15998         kill -USR1 $MULTIPID
15999         wait $MULTIPID
16000 }
16001 run_test 310c "open-unlink remote file with multiple links"
16002
16003 #LU-4825
16004 test_311() {
16005         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16006         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16007                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16008         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16009         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16010
16011         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16012
16013         mkdir -p $DIR/$tdir
16014         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16015         createmany -o $DIR/$tdir/$tfile. 1000
16016
16017         # statfs data is not real time, let's just calculate it
16018         old_iused=$((old_iused + 1000))
16019
16020         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16021                         osp.*OST0000*MDT0000.create_count")
16022         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16023                                 osp.*OST0000*MDT0000.max_create_count")
16024         for idx in $(seq $MDSCOUNT); do
16025                 do_facet mds$idx "lctl set_param -n \
16026                         osp.*OST0000*MDT000?.max_create_count=0"
16027         done
16028
16029         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16030         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16031         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16032
16033         unlinkmany $DIR/$tdir/$tfile. 1000
16034
16035         for idx in $(seq $MDSCOUNT); do
16036                 do_facet mds$idx "lctl set_param -n \
16037                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16038                 do_facet mds$idx "lctl set_param -n \
16039                         osp.*OST0000*MDT000?.create_count=$count"
16040         done
16041
16042         local new_iused
16043         for i in $(seq 120); do
16044                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16045                 # system may be too busy to destroy all objs in time, use
16046                 # a somewhat small value to not fail autotest
16047                 [ $((old_iused - new_iused)) -gt 400 ] && break
16048                 sleep 1
16049         done
16050
16051         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16052         [ $((old_iused - new_iused)) -gt 400 ] ||
16053                 error "objs not destroyed after unlink"
16054 }
16055 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16056
16057 zfs_oid_to_objid()
16058 {
16059         local ost=$1
16060         local objid=$2
16061
16062         local vdevdir=$(dirname $(facet_vdevice $ost))
16063         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
16064         local zfs_zapid=$(do_facet $ost $cmd |
16065                           grep -w "/O/0/d$((objid%32))" -C 5 |
16066                           awk '/Object/{getline; print $1}')
16067         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
16068                           awk "/$objid = /"'{printf $3}')
16069
16070         echo $zfs_objid
16071 }
16072
16073 zfs_object_blksz() {
16074         local ost=$1
16075         local objid=$2
16076
16077         local vdevdir=$(dirname $(facet_vdevice $ost))
16078         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
16079         local blksz=$(do_facet $ost $cmd $objid |
16080                       awk '/dblk/{getline; printf $4}')
16081
16082         case "${blksz: -1}" in
16083                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
16084                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
16085                 *) ;;
16086         esac
16087
16088         echo $blksz
16089 }
16090
16091 test_312() { # LU-4856
16092         remote_ost_nodsh && skip "remote OST with nodsh" && return
16093
16094         [ $(facet_fstype ost1) = "zfs" ] ||
16095                 { skip "the test only applies to zfs" && return; }
16096
16097         local max_blksz=$(do_facet ost1 \
16098                           $ZFS get -p recordsize $(facet_device ost1) |
16099                           awk '!/VALUE/{print $3}')
16100         local min_blksz=$(getconf PAGE_SIZE)
16101
16102         # to make life a little bit easier
16103         $LFS mkdir -c 1 -i 0 $DIR/$tdir
16104         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16105
16106         local tf=$DIR/$tdir/$tfile
16107         touch $tf
16108         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16109
16110         # Get ZFS object id
16111         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16112
16113         # block size change by sequential over write
16114         local blksz
16115         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
16116                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
16117
16118                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16119                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
16120         done
16121         rm -f $tf
16122
16123         # block size change by sequential append write
16124         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
16125         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16126         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16127
16128         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
16129                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
16130                         oflag=sync conv=notrunc
16131
16132                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
16133                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
16134                         error "blksz error, actual $blksz, "    \
16135                                 "expected: 2 * $count * $min_blksz"
16136         done
16137         rm -f $tf
16138
16139         # random write
16140         touch $tf
16141         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
16142         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
16143
16144         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
16145         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16146         [ $blksz -eq $min_blksz ] ||
16147                 error "blksz error: $blksz, expected: $min_blksz"
16148
16149         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
16150         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16151         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
16152
16153         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
16154         blksz=$(zfs_object_blksz ost1 $zfs_objid)
16155         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
16156 }
16157 run_test 312 "make sure ZFS adjusts its block size by write pattern"
16158
16159 test_313() {
16160         remote_ost_nodsh && skip "remote OST with nodsh" && return
16161
16162         local file=$DIR/$tfile
16163         rm -f $file
16164         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
16165
16166         # define OBD_FAIL_TGT_RCVD_EIO           0x720
16167         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16168         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
16169                 error "write should failed"
16170         do_facet ost1 "$LCTL set_param fail_loc=0"
16171         rm -f $file
16172 }
16173 run_test 313 "io should fail after last_rcvd update fail"
16174
16175 test_314() {
16176         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16177
16178         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
16179         do_facet ost1 "$LCTL set_param fail_loc=0x720"
16180         rm -f $DIR/$tfile
16181         wait_delete_completed
16182         do_facet ost1 "$LCTL set_param fail_loc=0"
16183 }
16184 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
16185
16186 test_315() { # LU-618
16187         local file=$DIR/$tfile
16188         rm -f $file
16189
16190         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
16191         $MULTIOP $file oO_RDONLY:r4096000_c &
16192         PID=$!
16193
16194         sleep 2
16195
16196         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
16197         kill -USR1 $PID
16198
16199         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
16200         rm -f $file
16201 }
16202 run_test 315 "read should be accounted"
16203
16204 test_fake_rw() {
16205         local read_write=$1
16206         if [ "$read_write" = "write" ]; then
16207                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
16208         elif [ "$read_write" = "read" ]; then
16209                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
16210         else
16211                 error "argument error"
16212         fi
16213
16214         # turn off debug for performance testing
16215         local saved_debug=$($LCTL get_param -n debug)
16216         $LCTL set_param debug=0
16217
16218         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
16219
16220         # get ost1 size - lustre-OST0000
16221         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
16222         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
16223         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
16224
16225         if [ "$read_write" = "read" ]; then
16226                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
16227         fi
16228
16229         local start_time=$(date +%s.%N)
16230         $dd_cmd bs=1M count=$blocks oflag=sync ||
16231                 error "real dd $read_write error"
16232         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
16233
16234         if [ "$read_write" = "write" ]; then
16235                 rm -f $DIR/$tfile
16236         fi
16237
16238         # define OBD_FAIL_OST_FAKE_RW           0x238
16239         do_facet ost1 $LCTL set_param fail_loc=0x238
16240
16241         local start_time=$(date +%s.%N)
16242         $dd_cmd bs=1M count=$blocks oflag=sync ||
16243                 error "fake dd $read_write error"
16244         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
16245
16246         if [ "$read_write" = "write" ]; then
16247                 # verify file size
16248                 cancel_lru_locks osc
16249                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
16250                         error "$tfile size not $blocks MB"
16251         fi
16252         do_facet ost1 $LCTL set_param fail_loc=0
16253
16254         echo "fake $read_write $duration_fake vs. normal $read_write" \
16255                 "$duration in seconds"
16256         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
16257                 error_not_in_vm "fake write is slower"
16258
16259         $LCTL set_param -n debug="$saved_debug"
16260         rm -f $DIR/$tfile
16261 }
16262 test_399a() { # LU-7655 for OST fake write
16263         remote_ost_nodsh && skip "remote OST with nodsh" && return
16264
16265         test_fake_rw write
16266 }
16267 run_test 399a "fake write should not be slower than normal write"
16268
16269 test_399b() { # LU-8726 for OST fake read
16270         remote_ost_nodsh && skip "remote OST with nodsh" && return
16271
16272         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
16273                 skip "ldiskfs only test" && return 0
16274         fi
16275         test_fake_rw read
16276 }
16277 run_test 399b "fake read should not be slower than normal read"
16278
16279 test_400a() { # LU-1606, was conf-sanity test_74
16280         local extra_flags=''
16281         local out=$TMP/$tfile
16282         local prefix=/usr/include/lustre
16283         local prog
16284
16285         if ! which $CC > /dev/null 2>&1; then
16286                 skip_env "$CC is not installed"
16287                 return 0
16288         fi
16289
16290         if ! [[ -d $prefix ]]; then
16291                 # Assume we're running in tree and fixup the include path.
16292                 extra_flags+=" -I$LUSTRE/../libcfs/include"
16293                 extra_flags+=" -I$LUSTRE/include"
16294                 extra_flags+=" -L$LUSTRE/utils"
16295         fi
16296
16297         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
16298                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
16299                         error "client api broken"
16300         done
16301         rm -f $out
16302 }
16303 run_test 400a "Lustre client api program can compile and link"
16304
16305 test_400b() { # LU-1606, LU-5011
16306         local header
16307         local out=$TMP/$tfile
16308         local prefix=/usr/include/lustre
16309
16310         # We use a hard coded prefix so that this test will not fail
16311         # when run in tree. There are headers in lustre/include/lustre/
16312         # that are not packaged (like lustre_idl.h) and have more
16313         # complicated include dependencies (like config.h and lnet/types.h).
16314         # Since this test about correct packaging we just skip them when
16315         # they don't exist (see below) rather than try to fixup cppflags.
16316
16317         if ! which $CC > /dev/null 2>&1; then
16318                 skip_env "$CC is not installed"
16319                 return 0
16320         fi
16321
16322         for header in $prefix/*.h; do
16323                 if ! [[ -f "$header" ]]; then
16324                         continue
16325                 fi
16326
16327                 if [[ "$(basename $header)" == liblustreapi.h ]]; then
16328                         continue # liblustreapi.h is deprecated.
16329                 fi
16330
16331                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
16332                         error "cannot compile '$header'"
16333         done
16334         rm -f $out
16335 }
16336 run_test 400b "packaged headers can be compiled"
16337
16338 test_401a() { #LU-7437
16339         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
16340         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
16341                 return
16342         #count the number of parameters by "list_param -R"
16343         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
16344         #count the number of parameters by listing proc files
16345         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
16346         echo "proc_dirs='$proc_dirs'"
16347         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
16348         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
16349                       sort -u | wc -l)
16350
16351         [ $params -eq $procs ] ||
16352                 error "found $params parameters vs. $procs proc files"
16353
16354         # test the list_param -D option only returns directories
16355         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
16356         #count the number of parameters by listing proc directories
16357         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
16358                 sort -u | wc -l)
16359
16360         [ $params -eq $procs ] ||
16361                 error "found $params parameters vs. $procs proc files"
16362 }
16363 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
16364
16365 test_401b() {
16366         local save=$($LCTL get_param -n jobid_var)
16367         local tmp=testing
16368
16369         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
16370                 error "no error returned when setting bad parameters"
16371
16372         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
16373         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
16374
16375         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
16376         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
16377         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
16378 }
16379 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
16380
16381 test_401c() {
16382         local jobid_var_old=$($LCTL get_param -n jobid_var)
16383         local jobid_var_new
16384
16385         $LCTL set_param jobid_var= &&
16386                 error "no error returned for 'set_param a='"
16387
16388         jobid_var_new=$($LCTL get_param -n jobid_var)
16389         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16390                 error "jobid_var was changed by setting without value"
16391
16392         $LCTL set_param jobid_var &&
16393                 error "no error returned for 'set_param a'"
16394
16395         jobid_var_new=$($LCTL get_param -n jobid_var)
16396         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
16397                 error "jobid_var was changed by setting without value"
16398 }
16399 run_test 401c "Verify 'lctl set_param' without value fails in either format."
16400
16401 test_401d() {
16402         local jobid_var_old=$($LCTL get_param -n jobid_var)
16403         local jobid_var_new
16404         local new_value="foo=bar"
16405
16406         $LCTL set_param jobid_var=$new_value ||
16407                 error "'set_param a=b' did not accept a value containing '='"
16408
16409         jobid_var_new=$($LCTL get_param -n jobid_var)
16410         [[ "$jobid_var_new" == "$new_value" ]] ||
16411                 error "'set_param a=b' failed on a value containing '='"
16412
16413         # Reset the jobid_var to test the other format
16414         $LCTL set_param jobid_var=$jobid_var_old
16415         jobid_var_new=$($LCTL get_param -n jobid_var)
16416         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16417                 error "failed to reset jobid_var"
16418
16419         $LCTL set_param jobid_var $new_value ||
16420                 error "'set_param a b' did not accept a value containing '='"
16421
16422         jobid_var_new=$($LCTL get_param -n jobid_var)
16423         [[ "$jobid_var_new" == "$new_value" ]] ||
16424                 error "'set_param a b' failed on a value containing '='"
16425
16426         $LCTL set_param jobid_var $jobid_var_old
16427         jobid_var_new=$($LCTL get_param -n jobid_var)
16428         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
16429                 error "failed to reset jobid_var"
16430 }
16431 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
16432
16433 test_402() {
16434         local server_version=$(lustre_version_code $SINGLEMDS)
16435         [[ $server_version -ge $(version_code 2.7.66) ]] ||
16436         [[ $server_version -ge $(version_code 2.7.18.4) &&
16437                 $server_version -lt $(version_code 2.7.50) ]] ||
16438         [[ $server_version -ge $(version_code 2.7.2) &&
16439                 $server_version -lt $(version_code 2.7.11) ]] ||
16440                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
16441                         return; }
16442         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16443         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
16444 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
16445         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
16446         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
16447                 echo "Touch failed - OK"
16448 }
16449 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
16450
16451 test_403() {
16452         local file1=$DIR/$tfile.1
16453         local file2=$DIR/$tfile.2
16454         local tfile=$TMP/$tfile
16455
16456         rm -f $file1 $file2 $tfile
16457
16458         touch $file1
16459         ln $file1 $file2
16460
16461         # 30 sec OBD_TIMEOUT in ll_getattr()
16462         # right before populating st_nlink
16463         $LCTL set_param fail_loc=0x80001409
16464         stat -c %h $file1 > $tfile &
16465
16466         # create an alias, drop all locks and reclaim the dentry
16467         < $file2
16468         cancel_lru_locks mdc
16469         cancel_lru_locks osc
16470         sysctl -w vm.drop_caches=2
16471
16472         wait
16473
16474         [ `cat $tfile` -gt 0 ] || error "wrong nlink count: `cat $tfile`"
16475
16476         rm -f $tfile $file1 $file2
16477 }
16478 run_test 403 "i_nlink should not drop to zero due to aliasing"
16479
16480 test_404() { # LU-6601
16481         local server_version=$(lustre_version_code $SINGLEMDS)
16482         [[ $server_version -ge $(version_code 2.8.53) ]] ||
16483                 { skip "Need server version newer than 2.8.52"; return 0; }
16484
16485         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16486         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
16487                 awk '/osp .*-osc-MDT/ { print $4}')
16488
16489         local osp
16490         for osp in $mosps; do
16491                 echo "Deactivate: " $osp
16492                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
16493                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16494                         awk -vp=$osp '$4 == p { print $2 }')
16495                 [ $stat = IN ] || {
16496                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16497                         error "deactivate error"
16498                 }
16499                 echo "Activate: " $osp
16500                 do_facet $SINGLEMDS $LCTL --device %$osp activate
16501                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
16502                         awk -vp=$osp '$4 == p { print $2 }')
16503                 [ $stat = UP ] || {
16504                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
16505                         error "activate error"
16506                 }
16507         done
16508 }
16509 run_test 404 "validate manual {de}activated works properly for OSPs"
16510
16511 test_405() {
16512         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
16513         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
16514                 skip "Layout swap lock is not supported" && return
16515
16516         check_swap_layouts_support && return 0
16517
16518         test_mkdir -p $DIR/$tdir
16519         swap_lock_test -d $DIR/$tdir ||
16520                 error "One layout swap locked test failed"
16521 }
16522 run_test 405 "Various layout swap lock tests"
16523
16524 test_406() {
16525         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16526         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
16527         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
16528         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16529         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
16530                 skip "Need MDS version at least 2.8.50" && return
16531
16532         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
16533         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
16534         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
16535         local def_pool=$($GETSTRIPE -p $MOUNT)
16536         local test_pool=$TESTNAME
16537
16538         if ! combined_mgs_mds ; then
16539                 mount_mgs_client
16540         fi
16541         pool_add $test_pool || error "pool_add failed"
16542         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
16543                 error "pool_add_targets failed"
16544
16545         # parent set default stripe count only, child will stripe from both
16546         # parent and fs default
16547         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
16548                 error "setstripe $MOUNT failed"
16549         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
16550         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
16551         for i in $(seq 10); do
16552                 local f=$DIR/$tdir/$tfile.$i
16553                 touch $f || error "touch failed"
16554                 local count=$($GETSTRIPE -c $f)
16555                 [ $count -eq $OSTCOUNT ] ||
16556                         error "$f stripe count $count != $OSTCOUNT"
16557                 local offset=$($GETSTRIPE -i $f)
16558                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
16559                 local size=$($GETSTRIPE -S $f)
16560                 [ $size -eq $((def_stripe_size * 2)) ] ||
16561                         error "$f stripe size $size != $((def_stripe_size * 2))"
16562                 local pool=$($GETSTRIPE -p $f)
16563                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
16564         done
16565
16566         # change fs default striping, delete parent default striping, now child
16567         # will stripe from new fs default striping only
16568         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
16569                 error "change $MOUNT default stripe failed"
16570         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
16571         for i in $(seq 11 20); do
16572                 local f=$DIR/$tdir/$tfile.$i
16573                 touch $f || error "touch $f failed"
16574                 local count=$($GETSTRIPE -c $f)
16575                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
16576                 local offset=$($GETSTRIPE -i $f)
16577                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
16578                 local size=$($GETSTRIPE -S $f)
16579                 [ $size -eq $def_stripe_size ] ||
16580                         error "$f stripe size $size != $def_stripe_size"
16581                 local pool=$($GETSTRIPE -p $f)
16582                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
16583
16584         done
16585
16586         unlinkmany $DIR/$tdir/$tfile. 1 20
16587
16588         # restore FS default striping
16589         if [ -z $def_pool ]; then
16590                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16591                         -i $def_stripe_offset $MOUNT ||
16592                         error "restore default striping failed"
16593         else
16594                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
16595                         -i $def_stripe_offset -p $def_pool $MOUNT ||
16596                         error "restore default striping with $def_pool failed"
16597         fi
16598
16599         local f=$DIR/$tdir/$tfile
16600         pool_remove_all_targets $test_pool $f
16601         pool_remove $test_pool $f
16602
16603         if ! combined_mgs_mds ; then
16604                 umount_mgs_client
16605         fi
16606 }
16607 run_test 406 "DNE support fs default striping"
16608
16609 test_407() {
16610         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16611         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
16612                 skip "Need MDS version at least 2.8.55" && return
16613         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16614
16615         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
16616                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
16617         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
16618                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
16619         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
16620
16621         #define OBD_FAIL_DT_TXN_STOP    0x2019
16622         for idx in $(seq $MDSCOUNT); do
16623                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
16624         done
16625         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
16626         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
16627                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
16628         true
16629 }
16630 run_test 407 "transaction fail should cause operation fail"
16631
16632 test_408() {
16633         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
16634
16635         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
16636         lctl set_param fail_loc=0x8000040a
16637         # let ll_prepare_partial_page() fail
16638         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
16639
16640         rm -f $DIR/$tfile
16641
16642         # create at least 100 unused inodes so that
16643         # shrink_icache_memory(0) should not return 0
16644         touch $DIR/$tfile-{0..100}
16645         rm -f $DIR/$tfile-{0..100}
16646         sync
16647
16648         echo 2 > /proc/sys/vm/drop_caches
16649 }
16650 run_test 408 "drop_caches should not hang due to page leaks"
16651
16652 test_409()
16653 {
16654         [ $MDSCOUNT -lt 2 ] &&
16655                 skip "We need at least 2 MDTs for this test" && return
16656
16657         check_mount_and_prep
16658
16659         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
16660         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
16661         touch $DIR/$tdir/guard || error "(2) Fail to create"
16662
16663         local PREFIX=$(str_repeat 'A' 128)
16664         echo "Create 1K hard links start at $(date)"
16665         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16666                 error "(3) Fail to hard link"
16667
16668         echo "Links count should be right although linkEA overflow"
16669         stat $DIR/$tdir/guard || error "(4) Fail to stat"
16670         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
16671         [ $linkcount -eq 1001 ] ||
16672                 error "(5) Unexpected hard links count: $linkcount"
16673
16674         echo "List all links start at $(date)"
16675         ls -l $DIR/$tdir/foo > /dev/null ||
16676                 error "(6) Fail to list $DIR/$tdir/foo"
16677
16678         echo "Unlink hard links start at $(date)"
16679         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
16680                 error "(7) Fail to unlink"
16681 }
16682 run_test 409 "Large amount of cross-MDTs hard links on the same file"
16683
16684 test_410()
16685 {
16686         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
16687                 skip "Need client version at least 2.9.59" && return
16688
16689         # Create a file, and stat it from the kernel
16690         local testfile=$DIR/$tfile
16691         touch $testfile
16692
16693         local run_id=$RANDOM
16694         local my_ino=$(stat --format "%i" $testfile)
16695
16696         # Try to insert the module. This will always fail as the
16697         # module is designed to not be inserted.
16698         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
16699             &> /dev/null
16700
16701         # Anything but success is a test failure
16702         dmesg | grep -q \
16703             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
16704             error "no inode match"
16705 }
16706 run_test 410 "Test inode number returned from kernel thread"
16707
16708 prep_801() {
16709         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
16710         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
16711                 skip "Need server version at least 2.9.55" & exit 0
16712         start_full_debug_logging
16713 }
16714
16715 post_801() {
16716         stop_full_debug_logging
16717 }
16718
16719 barrier_stat() {
16720         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
16721                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16722                            awk '/The barrier for/ { print $7 }')
16723                 echo $st
16724         else
16725                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
16726                 echo \'$st\'
16727         fi
16728 }
16729
16730 barrier_expired() {
16731         local expired
16732
16733         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
16734                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
16735                           awk '/will be expired/ { print $7 }')
16736         else
16737                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
16738         fi
16739
16740         echo $expired
16741 }
16742
16743 test_801a() {
16744         prep_801
16745
16746         echo "Start barrier_freeze at: $(date)"
16747         #define OBD_FAIL_BARRIER_DELAY          0x2202
16748         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
16749         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
16750
16751         sleep 2
16752         local b_status=$(barrier_stat)
16753         echo "Got barrier status at: $(date)"
16754         [ "$b_status" = "'freezing_p1'" ] ||
16755                 error "(1) unexpected barrier status $b_status"
16756
16757         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16758         wait
16759         b_status=$(barrier_stat)
16760         [ "$b_status" = "'frozen'" ] ||
16761                 error "(2) unexpected barrier status $b_status"
16762
16763         local expired=$(barrier_expired)
16764         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
16765         sleep $((expired + 3))
16766
16767         b_status=$(barrier_stat)
16768         [ "$b_status" = "'expired'" ] ||
16769                 error "(3) unexpected barrier status $b_status"
16770
16771         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
16772                 error "(4) fail to freeze barrier"
16773
16774         b_status=$(barrier_stat)
16775         [ "$b_status" = "'frozen'" ] ||
16776                 error "(5) unexpected barrier status $b_status"
16777
16778         echo "Start barrier_thaw at: $(date)"
16779         #define OBD_FAIL_BARRIER_DELAY          0x2202
16780         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
16781         do_facet mgs $LCTL barrier_thaw $FSNAME &
16782
16783         sleep 2
16784         b_status=$(barrier_stat)
16785         echo "Got barrier status at: $(date)"
16786         [ "$b_status" = "'thawing'" ] ||
16787                 error "(6) unexpected barrier status $b_status"
16788
16789         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
16790         wait
16791         b_status=$(barrier_stat)
16792         [ "$b_status" = "'thawed'" ] ||
16793                 error "(7) unexpected barrier status $b_status"
16794
16795         #define OBD_FAIL_BARRIER_FAILURE        0x2203
16796         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
16797         do_facet mgs $LCTL barrier_freeze $FSNAME
16798
16799         b_status=$(barrier_stat)
16800         [ "$b_status" = "'failed'" ] ||
16801                 error "(8) unexpected barrier status $b_status"
16802
16803         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
16804         do_facet mgs $LCTL barrier_thaw $FSNAME
16805
16806         post_801
16807 }
16808 run_test 801a "write barrier user interfaces and stat machine"
16809
16810 test_801b() {
16811         prep_801
16812
16813         mkdir $DIR/$tdir || error "(1) fail to mkdir"
16814         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
16815         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
16816         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
16817         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
16818
16819         cancel_lru_locks mdc
16820
16821         # 180 seconds should be long enough
16822         do_facet mgs $LCTL barrier_freeze $FSNAME 180
16823
16824         local b_status=$(barrier_stat)
16825         [ "$b_status" = "'frozen'" ] ||
16826                 error "(6) unexpected barrier status $b_status"
16827
16828         mkdir $DIR/$tdir/d0/d10 &
16829         mkdir_pid=$!
16830
16831         touch $DIR/$tdir/d1/f13 &
16832         touch_pid=$!
16833
16834         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
16835         ln_pid=$!
16836
16837         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
16838         mv_pid=$!
16839
16840         rm -f $DIR/$tdir/d4/f12 &
16841         rm_pid=$!
16842
16843         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
16844
16845         # To guarantee taht the 'stat' is not blocked
16846         b_status=$(barrier_stat)
16847         [ "$b_status" = "'frozen'" ] ||
16848                 error "(8) unexpected barrier status $b_status"
16849
16850         # let above commands to run at background
16851         sleep 5
16852
16853         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
16854         ps -p $touch_pid || error "(10) touch should be blocked"
16855         ps -p $ln_pid || error "(11) link should be blocked"
16856         ps -p $mv_pid || error "(12) rename should be blocked"
16857         ps -p $rm_pid || error "(13) unlink should be blocked"
16858
16859         b_status=$(barrier_stat)
16860         [ "$b_status" = "'frozen'" ] ||
16861                 error "(14) unexpected barrier status $b_status"
16862
16863         do_facet mgs $LCTL barrier_thaw $FSNAME
16864         b_status=$(barrier_stat)
16865         [ "$b_status" = "'thawed'" ] ||
16866                 error "(15) unexpected barrier status $b_status"
16867
16868         wait $mkdir_pid || error "(16) mkdir should succeed"
16869         wait $touch_pid || error "(17) touch should succeed"
16870         wait $ln_pid || error "(18) link should succeed"
16871         wait $mv_pid || error "(19) rename should succeed"
16872         wait $rm_pid || error "(20) unlink should succeed"
16873
16874         post_801
16875 }
16876 run_test 801b "modification will be blocked by write barrier"
16877
16878 test_801c() {
16879         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
16880
16881         prep_801
16882
16883         stop mds2 || error "(1) Fail to stop mds2"
16884
16885         do_facet mgs $LCTL barrier_freeze $FSNAME 30
16886
16887         local b_status=$(barrier_stat)
16888         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
16889                 do_facet mgs $LCTL barrier_thaw $FSNAME
16890                 error "(2) unexpected barrier status $b_status"
16891         }
16892
16893         do_facet mgs $LCTL barrier_rescan $FSNAME ||
16894                 error "(3) Fail to rescan barrier bitmap"
16895
16896         do_facet mgs $LCTL barrier_freeze $FSNAME 10
16897
16898         b_status=$(barrier_stat)
16899         [ "$b_status" = "'frozen'" ] ||
16900                 error "(4) unexpected barrier status $b_status"
16901
16902         do_facet mgs $LCTL barrier_thaw $FSNAME
16903         b_status=$(barrier_stat)
16904         [ "$b_status" = "'thawed'" ] ||
16905                 error "(5) unexpected barrier status $b_status"
16906
16907         local devname=$(mdsdevname 2)
16908
16909         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
16910
16911         do_facet mgs $LCTL barrier_rescan $FSNAME ||
16912                 error "(7) Fail to rescan barrier bitmap"
16913
16914         post_801
16915 }
16916 run_test 801c "rescan barrier bitmap"
16917
16918 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
16919 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
16920 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
16921
16922 cleanup_802() {
16923         trap 0
16924
16925         stopall
16926         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
16927         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
16928         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
16929         setupall
16930 }
16931
16932 test_802() {
16933
16934         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
16935         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
16936                 skip "Need server version at least 2.9.55" & exit 0
16937
16938         mkdir $DIR/$tdir || error "(1) fail to mkdir"
16939
16940         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
16941                 error "(2) Fail to copy"
16942
16943         trap cleanup_802 EXIT
16944
16945         # sync by force before remount as readonly
16946         sync; sync_all_data; sleep 3; sync_all_data
16947
16948         stopall
16949
16950         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
16951         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
16952         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
16953
16954         echo "Mount the server as read only"
16955         setupall server_only || error "(3) Fail to start servers"
16956
16957         echo "Mount client without ro should fail"
16958         mount_client $MOUNT &&
16959                 error "(4) Mount client without 'ro' should fail"
16960
16961         echo "Mount client with ro should succeed"
16962         mount_client $MOUNT ro ||
16963                 error "(5) Mount client with 'ro' should succeed"
16964
16965         echo "Modify should be refused"
16966         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
16967
16968         echo "Read should be allowed"
16969         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
16970                 error "(7) Read should succeed under ro mode"
16971
16972         cleanup_802
16973 }
16974 run_test 802 "simulate readonly device"
16975
16976 cleanup_805() {
16977         do_facet $SINGLEMDS zfs set quota=$old $fsset
16978         unlinkmany $DIR/$tdir/f- 1000000
16979         trap 0
16980 }
16981
16982 test_805() {
16983         local zfs_version=$(do_node $SINGLEMDS cat /sys/module/zfs/version)
16984         [ "$(facet_fstype mds1)" != "zfs" ] &&
16985                 skip "ZFS specific test" && return
16986         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
16987                 skip "netfree not implemented before 0.7" && return
16988         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.10.57) ]] ||
16989                 { skip "Need MDS version at least 2.10.57" && return 0; }
16990
16991         local fsset
16992         local freekb
16993         local usedkb
16994         local old
16995         local quota
16996         local pref="osd-zfs.lustre-MDT0000."
16997
16998         # limit available space on MDS dataset to meet nospace issue
16999         # quickly. then ZFS 0.7.2 can use reserved space if asked
17000         # properly (using netfree flag in osd_declare_destroy()
17001         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
17002         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
17003                 gawk '{print $3}')
17004         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
17005         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
17006         let "usedkb=usedkb-freekb"
17007         let "freekb=freekb/2"
17008         if let "freekb > 5000"; then
17009                 let "freekb=5000"
17010         fi
17011         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
17012         trap cleanup_805 EXIT
17013         mkdir $DIR/$tdir
17014         $LFS setstripe -E 1M -L mdt $DIR/$tdir || error "DoM not working"
17015         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
17016         rm -rf $DIR/$tdir || error "not able to remove"
17017         do_facet $SINGLEMDS zfs set quota=$old $fsset
17018         trap 0
17019 }
17020 run_test 805 "ZFS can remove from full fs"
17021
17022 #
17023 # tests that do cleanup/setup should be run at the end
17024 #
17025
17026 test_900() {
17027         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17028         local ls
17029         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
17030         $LCTL set_param fail_loc=0x903
17031
17032         cancel_lru_locks MGC
17033
17034         FAIL_ON_ERROR=true cleanup
17035         FAIL_ON_ERROR=true setup
17036 }
17037 run_test 900 "umount should not race with any mgc requeue thread"
17038
17039 complete $SECONDS
17040 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
17041 check_and_cleanup_lustre
17042 if [ "$I_MOUNTED" != "yes" ]; then
17043         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
17044 fi
17045 exit_status