Whamcloud - gitweb
4d1f5906a29fedc5babe59ec62cf08a7c57fa300
[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 OSC=${OSC:-"osc"}
26
27 CC=${CC:-cc}
28 CHECKSTAT=${CHECKSTAT:-"checkstat -v"}
29 CREATETEST=${CREATETEST:-createtest}
30 LFS=${LFS:-lfs}
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-1957
67         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  180"
68         #                                               13    (min)"
69         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
70 fi
71
72 # Get the SLES version so we can make decisions on if a test should be run
73 #
74 # Returns a version string that should only be used in comparing
75 # strings returned by version_code()
76
77 sles_version_code()
78 {
79         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
80
81         # All SuSE Linux versions have one decimal. version_code expects two
82         local sles_version=$version.0
83         version_code $sles_version
84 }
85
86 if [ -r /etc/SuSE-release ]; then
87         sles_version=$(sles_version_code)
88         [ $sles_version -lt $(version_code 11.4.0) ] &&
89                 # bug number for skipped test: LU-4341
90                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
91         [ $sles_version -lt $(version_code 12.0.0) ] &&
92                 # bug number for skipped test: LU-3703
93                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
94 fi
95
96 FAIL_ON_ERROR=false
97
98 cleanup() {
99         echo -n "cln.."
100         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
101         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
102 }
103 setup() {
104         echo -n "mnt.."
105         load_modules
106         setupall || exit 10
107         echo "done"
108 }
109
110 check_swap_layouts_support()
111 {
112         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
113                 { skip "Does not support layout lock."; return 0; }
114         return 1
115 }
116
117 check_and_setup_lustre
118 DIR=${DIR:-$MOUNT}
119 assert_DIR
120
121 MDT0=$($LCTL get_param -n mdc.*.mds_server_uuid |
122         awk '{ gsub(/_UUID/,""); print $1 }' | head -n1)
123 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
124
125 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
126 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
127 rm -rf $DIR/[Rdfs][0-9]*
128
129 # $RUNAS_ID may get set incorrectly somewhere else
130 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] && error "\$RUNAS_ID set to 0, but \$UID is also 0!"
131
132 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
133
134 build_test_filter
135
136 if [ "${ONLY}" = "MOUNT" ] ; then
137         echo "Lustre is up, please go on"
138         exit
139 fi
140
141 echo "preparing for tests involving mounts"
142 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
143 touch $EXT2_DEV
144 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
145 echo # add a newline after mke2fs.
146
147 umask 077
148
149 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
150 lctl set_param debug=-1 2> /dev/null || true
151 test_0a() {
152         touch $DIR/$tfile
153         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
154         rm $DIR/$tfile
155         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
156 }
157 run_test 0a "touch; rm ====================="
158
159 test_0b() {
160         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
161         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
162 }
163 run_test 0b "chmod 0755 $DIR ============================="
164
165 test_0c() {
166         $LCTL get_param mdc.*.import | grep "state: FULL" ||
167                 error "import not FULL"
168         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
169                 error "bad target"
170 }
171 run_test 0c "check import proc"
172
173 test_1() {
174         test_mkdir $DIR/$tdir
175         test_mkdir $DIR/$tdir/d2
176         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
177         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
178         rmdir $DIR/$tdir/d2
179         rmdir $DIR/$tdir
180         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
181 }
182 run_test 1 "mkdir; remkdir; rmdir"
183
184 test_2() {
185         test_mkdir $DIR/$tdir
186         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
187         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
188         rm -r $DIR/$tdir
189         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
190 }
191 run_test 2 "mkdir; touch; rmdir; check file"
192
193 test_3() {
194         test_mkdir $DIR/$tdir
195         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
196         touch $DIR/$tdir/$tfile
197         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
198         rm -r $DIR/$tdir
199         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
200 }
201 run_test 3 "mkdir; touch; rmdir; check dir"
202
203 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
204 test_4() {
205         test_mkdir -i 1 $DIR/$tdir
206
207         touch $DIR/$tdir/$tfile ||
208                 error "Create file under remote directory failed"
209
210         rmdir $DIR/$tdir &&
211                 error "Expect error removing in-use dir $DIR/$tdir"
212
213         test -d $DIR/$tdir || error "Remote directory disappeared"
214
215         rm -rf $DIR/$tdir || error "remove remote dir error"
216 }
217 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
218
219 test_5() {
220         test_mkdir $DIR/$tdir
221         test_mkdir $DIR/$tdir/d2
222         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
223         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
224         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
225 }
226 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
227
228 test_6a() {
229         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
230         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
231         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
232                 error "$tfile does not have perm 0666 or UID $UID"
233         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
234         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
235                 error "$tfile should be 0666 and owned by UID $UID"
236 }
237 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
238
239 test_6c() {
240         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
241         touch $DIR/$tfile
242         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
243         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
244                 error "$tfile should be owned by UID $RUNAS_ID"
245         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
246         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
247                 error "$tfile should be owned by UID $RUNAS_ID"
248 }
249 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
250
251 test_6e() {
252         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
253         touch $DIR/$tfile
254         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
255         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
256                 error "$tfile should be owned by GID $UID"
257         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
258         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
259                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
260 }
261 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
262
263 test_6g() {
264         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
265         test_mkdir $DIR/$tdir
266         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
267         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
268         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
269         test_mkdir $DIR/$tdir/d/subdir
270         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
271                 error "$tdir/d/subdir should be GID $RUNAS_GID"
272         if [[ $MDSCOUNT -gt 1 ]]; then
273                 # check remote dir sgid inherite
274                 $LFS mkdir -i 0 $DIR/$tdir.local ||
275                         error "mkdir $tdir.local failed"
276                 chmod g+s $DIR/$tdir.local ||
277                         error "chmod $tdir.local failed"
278                 chgrp $RUNAS_GID $DIR/$tdir.local ||
279                         error "chgrp $tdir.local failed"
280                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
281                         error "mkdir $tdir.remote failed"
282                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
283                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
284                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
285                         error "$tdir.remote should be mode 02755"
286         fi
287 }
288 run_test 6g "verify new dir in sgid dir inherits group"
289
290 test_6h() { # bug 7331
291         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID" && return
292         touch $DIR/$tfile || error "touch failed"
293         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
294         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
295                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
296         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
297                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
298 }
299 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
300
301 test_7a() {
302         test_mkdir $DIR/$tdir
303         $MCREATE $DIR/$tdir/$tfile
304         chmod 0666 $DIR/$tdir/$tfile
305         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
306                 error "$tdir/$tfile should be mode 0666"
307 }
308 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
309
310 test_7b() {
311         if [ ! -d $DIR/$tdir ]; then
312                 test_mkdir $DIR/$tdir
313         fi
314         $MCREATE $DIR/$tdir/$tfile
315         echo -n foo > $DIR/$tdir/$tfile
316         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
317         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
318 }
319 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
320
321 test_8() {
322         test_mkdir $DIR/$tdir
323         touch $DIR/$tdir/$tfile
324         chmod 0666 $DIR/$tdir/$tfile
325         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
326                 error "$tfile mode not 0666"
327 }
328 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
329
330 test_9() {
331         test_mkdir $DIR/$tdir
332         test_mkdir $DIR/$tdir/d2
333         test_mkdir $DIR/$tdir/d2/d3
334         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
335 }
336 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
337
338 test_10() {
339         test_mkdir $DIR/$tdir
340         test_mkdir $DIR/$tdir/d2
341         touch $DIR/$tdir/d2/$tfile
342         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
343                 error "$tdir/d2/$tfile not a file"
344 }
345 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
346
347 test_11() {
348         test_mkdir $DIR/$tdir
349         test_mkdir $DIR/$tdir/d2
350         chmod 0666 $DIR/$tdir/d2
351         chmod 0705 $DIR/$tdir/d2
352         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
353                 error "$tdir/d2 mode not 0705"
354 }
355 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
356
357 test_12() {
358         test_mkdir $DIR/$tdir
359         touch $DIR/$tdir/$tfile
360         chmod 0666 $DIR/$tdir/$tfile
361         chmod 0654 $DIR/$tdir/$tfile
362         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
363                 error "$tdir/d2 mode not 0654"
364 }
365 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
366
367 test_13() {
368         test_mkdir $DIR/$tdir
369         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
370         >  $DIR/$tdir/$tfile
371         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
372                 error "$tdir/$tfile size not 0 after truncate"
373 }
374 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
375
376 test_14() {
377         test_mkdir $DIR/$tdir
378         touch $DIR/$tdir/$tfile
379         rm $DIR/$tdir/$tfile
380         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
381 }
382 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
383
384 test_15() {
385         test_mkdir $DIR/$tdir
386         touch $DIR/$tdir/$tfile
387         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
388         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
389                 error "$tdir/${tfile_2} not a file after rename"
390         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
391 }
392 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
393
394 test_16() {
395         test_mkdir $DIR/$tdir
396         touch $DIR/$tdir/$tfile
397         rm -rf $DIR/$tdir/$tfile
398         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
399 }
400 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
401
402 test_17a() {
403         test_mkdir $DIR/$tdir
404         touch $DIR/$tdir/$tfile
405         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
406         ls -l $DIR/$tdir
407         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
408                 error "$tdir/l-exist not a symlink"
409         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
410                 error "$tdir/l-exist not referencing a file"
411         rm -f $DIR/$tdir/l-exist
412         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
413 }
414 run_test 17a "symlinks: create, remove (real)"
415
416 test_17b() {
417         test_mkdir $DIR/$tdir
418         ln -s no-such-file $DIR/$tdir/l-dangle
419         ls -l $DIR/$tdir
420         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
421                 error "$tdir/l-dangle not referencing no-such-file"
422         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
423                 error "$tdir/l-dangle not referencing non-existent file"
424         rm -f $DIR/$tdir/l-dangle
425         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
426 }
427 run_test 17b "symlinks: create, remove (dangling)"
428
429 test_17c() { # bug 3440 - don't save failed open RPC for replay
430         test_mkdir $DIR/$tdir
431         ln -s foo $DIR/$tdir/$tfile
432         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
433 }
434 run_test 17c "symlinks: open dangling (should return error)"
435
436 test_17d() {
437         test_mkdir $DIR/$tdir
438         ln -s foo $DIR/$tdir/$tfile
439         touch $DIR/$tdir/$tfile || error "creating to new symlink"
440 }
441 run_test 17d "symlinks: create dangling"
442
443 test_17e() {
444         test_mkdir $DIR/$tdir
445         local foo=$DIR/$tdir/$tfile
446         ln -s $foo $foo || error "create symlink failed"
447         ls -l $foo || error "ls -l failed"
448         ls $foo && error "ls not failed" || true
449 }
450 run_test 17e "symlinks: create recursive symlink (should return error)"
451
452 test_17f() {
453         test_mkdir $DIR/$tdir
454         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
455         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
456         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
457         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
458         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
459         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
460         ls -l  $DIR/$tdir
461 }
462 run_test 17f "symlinks: long and very long symlink name"
463
464 # str_repeat(S, N) generate a string that is string S repeated N times
465 str_repeat() {
466         local s=$1
467         local n=$2
468         local ret=''
469         while [ $((n -= 1)) -ge 0 ]; do
470                 ret=$ret$s
471         done
472         echo $ret
473 }
474
475 # Long symlinks and LU-2241
476 test_17g() {
477         test_mkdir $DIR/$tdir
478         local TESTS="59 60 61 4094 4095"
479
480         # Fix for inode size boundary in 2.1.4
481         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.4) ] &&
482                 TESTS="4094 4095"
483
484         # Patch not applied to 2.2 or 2.3 branches
485         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
486         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.3.55) ] &&
487                 TESTS="4094 4095"
488
489         # skip long symlink name for rhel6.5.
490         # rhel6.5 has a limit (PATH_MAX - sizeof(struct filename))
491         grep -q '6.5' /etc/redhat-release &>/dev/null &&
492                 TESTS="59 60 61 4062 4063"
493
494         for i in $TESTS; do
495                 local SYMNAME=$(str_repeat 'x' $i)
496                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
497                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
498         done
499 }
500 run_test 17g "symlinks: really long symlink name and inode boundaries"
501
502 test_17h() { #bug 17378
503         remote_mds_nodsh && skip "remote MDS with nodsh" && return
504         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
505         local mdt_idx
506         test_mkdir $DIR/$tdir
507         if [[ $MDSCOUNT -gt 1 ]]; then
508                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
509         else
510                 mdt_idx=0
511         fi
512         $LFS setstripe -c -1 $DIR/$tdir
513         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
514         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
515         touch $DIR/$tdir/$tfile || true
516 }
517 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
518
519 test_17i() { #bug 20018
520         remote_mds_nodsh && skip "remote MDS with nodsh" && return
521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
522         test_mkdir -c1 $DIR/$tdir
523         local foo=$DIR/$tdir/$tfile
524         local mdt_idx
525         if [[ $MDSCOUNT -gt 1 ]]; then
526                 mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
527         else
528                 mdt_idx=0
529         fi
530         ln -s $foo $foo || error "create symlink failed"
531 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
532         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
533         ls -l $foo && error "error not detected"
534         return 0
535 }
536 run_test 17i "don't panic on short symlink"
537
538 test_17k() { #bug 22301
539         [[ -z "$(which rsync 2>/dev/null)" ]] &&
540                 skip "no rsync command" && return 0
541         rsync --help | grep -q xattr ||
542                 skip_env "$(rsync --version | head -n1) does not support xattrs"
543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
544         test_mkdir $DIR/$tdir
545         test_mkdir $DIR/$tdir.new
546         touch $DIR/$tdir/$tfile
547         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
548         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
549                 error "rsync failed with xattrs enabled"
550 }
551 run_test 17k "symlinks: rsync with xattrs enabled"
552
553 test_17l() { # LU-279
554         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
555                 skip "no getfattr command" && return 0
556         test_mkdir $DIR/$tdir
557         touch $DIR/$tdir/$tfile
558         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
559         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
560                 # -h to not follow symlinks. -m '' to list all the xattrs.
561                 # grep to remove first line: '# file: $path'.
562                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
563                 do
564                         lgetxattr_size_check $path $xattr ||
565                                 error "lgetxattr_size_check $path $xattr failed"
566                 done
567         done
568 }
569 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
570
571 # LU-1540
572 test_17m() {
573         local short_sym="0123456789"
574         local wdir=$DIR/$tdir
575         local i
576
577         remote_mds_nodsh && skip "remote MDS with nodsh" && return
578         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
579         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
580                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
581
582         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
583                 skip "ldiskfs only test" && return 0
584
585         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
586
587         test_mkdir $wdir
588         long_sym=$short_sym
589         # create a long symlink file
590         for ((i = 0; i < 4; ++i)); do
591                 long_sym=${long_sym}${long_sym}
592         done
593
594         echo "create 512 short and long symlink files under $wdir"
595         for ((i = 0; i < 256; ++i)); do
596                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
597                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
598         done
599
600         echo "erase them"
601         rm -f $wdir/*
602         sync
603         wait_delete_completed
604
605         echo "recreate the 512 symlink files with a shorter string"
606         for ((i = 0; i < 512; ++i)); do
607                 # rewrite the symlink file with a shorter string
608                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
609                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
610         done
611
612         local mds_index=$(($($LFS getstripe -M $wdir) + 1))
613         local devname=$(mdsdevname $mds_index)
614
615         echo "stop and checking mds${mds_index}:"
616         # e2fsck should not return error
617         stop mds${mds_index}
618         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
619         rc=$?
620
621         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
622                 error "start mds${mds_index} failed"
623         df $MOUNT > /dev/null 2>&1
624         [ $rc -eq 0 ] ||
625                 error "e2fsck detected error for short/long symlink: rc=$rc"
626 }
627 run_test 17m "run e2fsck against MDT which contains short/long symlink"
628
629 check_fs_consistency_17n() {
630         local mdt_index
631         local rc=0
632
633         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
634         # so it only check MDT1/MDT2 instead of all of MDTs.
635         for mdt_index in 1 2; do
636                 local devname=$(mdsdevname $mdt_index)
637                 # e2fsck should not return error
638                 stop mds${mdt_index}
639                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
640                         rc=$((rc + $?))
641
642                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
643                         error "mount mds$mdt_index failed"
644                 df $MOUNT > /dev/null 2>&1
645         done
646         return $rc
647 }
648
649 test_17n() {
650         local i
651
652         remote_mds_nodsh && skip "remote MDS with nodsh" && return
653         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] &&
654         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.2.93) ] &&
655                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks" && return
656
657         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
658                 skip "ldiskfs only test" && return 0
659
660         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
661
662         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
663
664         test_mkdir $DIR/$tdir
665         for ((i=0; i<10; i++)); do
666                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
667                         error "create remote dir error $i"
668                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
669                         error "create files under remote dir failed $i"
670         done
671
672         check_fs_consistency_17n ||
673                 error "e2fsck report error after create files under remote dir"
674
675         for ((i = 0; i < 10; i++)); do
676                 rm -rf $DIR/$tdir/remote_dir_${i} ||
677                         error "destroy remote dir error $i"
678         done
679
680         check_fs_consistency_17n ||
681                 error "e2fsck report error after unlink files under remote dir"
682
683         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.50) ] &&
684                 skip "lustre < 2.4.50 does not support migrate mv " && return
685
686         for ((i = 0; i < 10; i++)); do
687                 mkdir -p $DIR/$tdir/remote_dir_${i}
688                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
689                         error "create files under remote dir failed $i"
690                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
691                         error "migrate remote dir error $i"
692         done
693         check_fs_consistency_17n || error "e2fsck report error after migration"
694
695         for ((i = 0; i < 10; i++)); do
696                 rm -rf $DIR/$tdir/remote_dir_${i} ||
697                         error "destroy remote dir error $i"
698         done
699
700         check_fs_consistency_17n || error "e2fsck report error after unlink"
701 }
702 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
703
704 test_17o() {
705         remote_mds_nodsh && skip "remote MDS with nodsh" && return
706         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.64) ] &&
707                 skip "Need MDS version at least 2.3.64" && return
708
709         local wdir=$DIR/${tdir}o
710         local mdt_index
711         local rc=0
712
713         test_mkdir $wdir
714         touch $wdir/$tfile
715         mdt_index=$($LFS getstripe -M $wdir/$tfile)
716         mdt_index=$((mdt_index + 1))
717
718         cancel_lru_locks mdc
719         #fail mds will wait the failover finish then set
720         #following fail_loc to avoid interfer the recovery process.
721         fail mds${mdt_index}
722
723         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
724         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
725         ls -l $wdir/$tfile && rc=1
726         do_facet mds${mdt_index} lctl set_param fail_loc=0
727         [[ $rc -eq 0 ]] || error "stat file should fail"
728 }
729 run_test 17o "stat file with incompat LMA feature"
730
731 test_18() {
732         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
733         ls $DIR || error "Failed to ls $DIR: $?"
734 }
735 run_test 18 "touch .../f ; ls ... =============================="
736
737 test_19a() {
738         touch $DIR/$tfile
739         ls -l $DIR
740         rm $DIR/$tfile
741         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
742 }
743 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
744
745 test_19b() {
746         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
747 }
748 run_test 19b "ls -l .../f19 (should return error) =============="
749
750 test_19c() {
751         [ $RUNAS_ID -eq $UID ] &&
752                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
753         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
754 }
755 run_test 19c "$RUNAS touch .../f19 (should return error) =="
756
757 test_19d() {
758         cat $DIR/f19 && error || true
759 }
760 run_test 19d "cat .../f19 (should return error) =============="
761
762 test_20() {
763         touch $DIR/$tfile
764         rm $DIR/$tfile
765         touch $DIR/$tfile
766         rm $DIR/$tfile
767         touch $DIR/$tfile
768         rm $DIR/$tfile
769         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
770 }
771 run_test 20 "touch .../f ; ls -l ..."
772
773 test_21() {
774         test_mkdir $DIR/$tdir
775         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
776         ln -s dangle $DIR/$tdir/link
777         echo foo >> $DIR/$tdir/link
778         cat $DIR/$tdir/dangle
779         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
780         $CHECKSTAT -f -t file $DIR/$tdir/link ||
781                 error "$tdir/link not linked to a file"
782 }
783 run_test 21 "write to dangling link"
784
785 test_22() {
786         local wdir=$DIR/$tdir
787         test_mkdir $wdir
788         chown $RUNAS_ID:$RUNAS_GID $wdir
789         (cd $wdir || error "cd $wdir failed";
790                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
791                 $RUNAS tar xf -)
792         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
793         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
794         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
795                 error "checkstat -u failed"
796 }
797 run_test 22 "unpack tar archive as non-root user"
798
799 # was test_23
800 test_23a() {
801         test_mkdir $DIR/$tdir
802         local file=$DIR/$tdir/$tfile
803
804         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
805         openfile -f O_CREAT:O_EXCL $file &&
806                 error "$file recreate succeeded" || true
807 }
808 run_test 23a "O_CREAT|O_EXCL in subdir"
809
810 test_23b() { # bug 18988
811         test_mkdir $DIR/$tdir
812         local file=$DIR/$tdir/$tfile
813
814         rm -f $file
815         echo foo > $file || error "write filed"
816         echo bar >> $file || error "append filed"
817         $CHECKSTAT -s 8 $file || error "wrong size"
818         rm $file
819 }
820 run_test 23b "O_APPEND check"
821
822 # rename sanity
823 test_24a() {
824         echo '-- same directory rename'
825         test_mkdir $DIR/$tdir
826         touch $DIR/$tdir/$tfile.1
827         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
828         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
829 }
830 run_test 24a "rename file to non-existent target"
831
832 test_24b() {
833         test_mkdir $DIR/$tdir
834         touch $DIR/$tdir/$tfile.{1,2}
835         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
836         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
837         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
838 }
839 run_test 24b "rename file to existing target"
840
841 test_24c() {
842         test_mkdir $DIR/$tdir
843         test_mkdir $DIR/$tdir/d$testnum.1
844         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
845         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
846         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
847 }
848 run_test 24c "rename directory to non-existent target"
849
850 test_24d() {
851         test_mkdir -c1 $DIR/$tdir
852         test_mkdir -c1 $DIR/$tdir/d$testnum.1
853         test_mkdir -c1 $DIR/$tdir/d$testnum.2
854         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
855         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
856         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
857 }
858 run_test 24d "rename directory to existing target"
859
860 test_24e() {
861         echo '-- cross directory renames --'
862         test_mkdir $DIR/R5a
863         test_mkdir $DIR/R5b
864         touch $DIR/R5a/f
865         mv $DIR/R5a/f $DIR/R5b/g
866         $CHECKSTAT -a $DIR/R5a/f || error
867         $CHECKSTAT -t file $DIR/R5b/g || error
868 }
869 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
870
871 test_24f() {
872         test_mkdir $DIR/R6a
873         test_mkdir $DIR/R6b
874         touch $DIR/R6a/f $DIR/R6b/g
875         mv $DIR/R6a/f $DIR/R6b/g
876         $CHECKSTAT -a $DIR/R6a/f || error
877         $CHECKSTAT -t file $DIR/R6b/g || error
878 }
879 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
880
881 test_24g() {
882         test_mkdir $DIR/R7a
883         test_mkdir $DIR/R7b
884         test_mkdir $DIR/R7a/d
885         mv $DIR/R7a/d $DIR/R7b/e
886         $CHECKSTAT -a $DIR/R7a/d || error
887         $CHECKSTAT -t dir $DIR/R7b/e || error
888 }
889 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
890
891 test_24h() {
892         test_mkdir -c1 $DIR/R8a
893         test_mkdir -c1 $DIR/R8b
894         test_mkdir -c1 $DIR/R8a/d
895         test_mkdir -c1 $DIR/R8b/e
896         mrename $DIR/R8a/d $DIR/R8b/e
897         $CHECKSTAT -a $DIR/R8a/d || error
898         $CHECKSTAT -t dir $DIR/R8b/e || error
899 }
900 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
901
902 test_24i() {
903         echo "-- rename error cases"
904         test_mkdir $DIR/R9
905         test_mkdir $DIR/R9/a
906         touch $DIR/R9/f
907         mrename $DIR/R9/f $DIR/R9/a
908         $CHECKSTAT -t file $DIR/R9/f || error
909         $CHECKSTAT -t dir  $DIR/R9/a || error
910         $CHECKSTAT -a $DIR/R9/a/f || error
911 }
912 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
913
914 test_24j() {
915         test_mkdir $DIR/R10
916         mrename $DIR/R10/f $DIR/R10/g
917         $CHECKSTAT -t dir $DIR/R10 || error
918         $CHECKSTAT -a $DIR/R10/f || error
919         $CHECKSTAT -a $DIR/R10/g || error
920 }
921 run_test 24j "source does not exist ============================"
922
923 test_24k() {
924         test_mkdir $DIR/R11a
925         test_mkdir $DIR/R11a/d
926         touch $DIR/R11a/f
927         mv $DIR/R11a/f $DIR/R11a/d
928         $CHECKSTAT -a $DIR/R11a/f || error
929         $CHECKSTAT -t file $DIR/R11a/d/f || error
930 }
931 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
932
933 # bug 2429 - rename foo foo foo creates invalid file
934 test_24l() {
935         f="$DIR/f24l"
936         $MULTIOP $f OcNs || error
937 }
938 run_test 24l "Renaming a file to itself ========================"
939
940 test_24m() {
941         f="$DIR/f24m"
942         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
943         # on ext3 this does not remove either the source or target files
944         # though the "expected" operation would be to remove the source
945         $CHECKSTAT -t file ${f} || error "${f} missing"
946         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
947 }
948 run_test 24m "Renaming a file to a hard link to itself ========="
949
950 test_24n() {
951     f="$DIR/f24n"
952     # this stats the old file after it was renamed, so it should fail
953     touch ${f}
954     $CHECKSTAT ${f}
955     mv ${f} ${f}.rename
956     $CHECKSTAT ${f}.rename
957     $CHECKSTAT -a ${f}
958 }
959 run_test 24n "Statting the old file after renaming (Posix rename 2)"
960
961 test_24o() {
962         test_mkdir $DIR/$tdir
963         rename_many -s random -v -n 10 $DIR/$tdir
964 }
965 run_test 24o "rename of files during htree split"
966
967 test_24p() {
968         test_mkdir $DIR/R12a
969         test_mkdir $DIR/R12b
970         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
971         mrename $DIR/R12a $DIR/R12b
972         $CHECKSTAT -a $DIR/R12a || error
973         $CHECKSTAT -t dir $DIR/R12b || error
974         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
975         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
976 }
977 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
978
979 cleanup_multiop_pause() {
980         trap 0
981         kill -USR1 $MULTIPID
982 }
983
984 test_24q() {
985         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
986         test_mkdir $DIR/R13a
987         test_mkdir $DIR/R13b
988         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
989         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
990         MULTIPID=$!
991
992         trap cleanup_multiop_pause EXIT
993         mrename $DIR/R13a $DIR/R13b
994         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
995         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
996         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
997         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
998         cleanup_multiop_pause
999         wait $MULTIPID || error "multiop close failed"
1000 }
1001 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1002
1003 test_24r() { #bug 3789
1004         test_mkdir $DIR/R14a
1005         test_mkdir $DIR/R14a/b
1006         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1007         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1008         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1009 }
1010 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1011
1012 test_24s() {
1013         test_mkdir $DIR/R15a
1014         test_mkdir $DIR/R15a/b
1015         test_mkdir $DIR/R15a/b/c
1016         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1017         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1018         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1019 }
1020 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1021 test_24t() {
1022         test_mkdir $DIR/R16a
1023         test_mkdir $DIR/R16a/b
1024         test_mkdir $DIR/R16a/b/c
1025         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1026         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1027         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1028 }
1029 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1030
1031 test_24u() { # bug12192
1032         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error
1033         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1034 }
1035 run_test 24u "create stripe file"
1036
1037 page_size() {
1038         local size
1039         size=$(getconf PAGE_SIZE 2>/dev/null)
1040         echo -n ${size:-4096}
1041 }
1042
1043 simple_cleanup_common() {
1044         local rc=0
1045         trap 0
1046         [ -z "$DIR" -o -z "$tdir" ] && return 0
1047
1048         local start=$SECONDS
1049         rm -rf $DIR/$tdir
1050         rc=$?
1051         wait_delete_completed
1052         echo "cleanup time $((SECONDS - start))"
1053         return $rc
1054 }
1055
1056 max_pages_per_rpc() {
1057         local mdtname="$(printf "MDT%04x" ${1:-0})"
1058         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1059 }
1060
1061 test_24v() {
1062         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1063         local nrfiles=${COUNT:-100000}
1064         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1065         [ $(facet_fstype $SINGLEMDS) = "zfs" ] && nrfiles=${COUNT:-10000}
1066
1067         local fname="$DIR/$tdir/$tfile"
1068         test_mkdir "$(dirname $fname)"
1069         # assume MDT0000 has the fewest inodes
1070         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1071         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1072         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1073
1074         trap simple_cleanup_common EXIT
1075
1076         createmany -m "$fname" $nrfiles
1077
1078         cancel_lru_locks mdc
1079         lctl set_param mdc.*.stats clear
1080
1081         # was previously test_24D: LU-6101
1082         # readdir() returns correct number of entries after cursor reload
1083         local num_ls=$(ls $DIR/$tdir | wc -l)
1084         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1085         local num_all=$(ls -a $DIR/$tdir | wc -l)
1086         if [ $num_ls -ne $nrfiles -o $num_uniq -ne $nrfiles -o \
1087              $num_all -ne $((nrfiles + 2)) ]; then
1088                 error "Expected $nrfiles files, got $num_ls " \
1089                         "($num_uniq unique $num_all .&..)"
1090         fi
1091         # LU-5 large readdir
1092         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1093         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1094         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1095         # take into account of overhead in lu_dirpage header and end mark in
1096         # each page, plus one in rpc_num calculation.
1097         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1098         local page_entries=$((($(page_size) - 24) / dirent_size))
1099         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1100         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1101         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1102         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1103         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1104         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1105                 error "large readdir doesn't take effect: " \
1106                       "$mds_readpage should be about $rpc_max"
1107
1108         simple_cleanup_common
1109 }
1110 run_test 24v "list large directory (test hash collision, b=17560)"
1111
1112 test_24w() { # bug21506
1113         SZ1=234852
1114         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1115         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1116         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1117         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1118         [[ "$SZ1" -eq "$SZ2" ]] ||
1119                 error "Error reading at the end of the file $tfile"
1120 }
1121 run_test 24w "Reading a file larger than 4Gb"
1122
1123 test_24x() {
1124         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1125
1126         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1127                 skip "Need MDS version at least 2.7.56" && return
1128
1129         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1130         local MDTIDX=1
1131         local remote_dir=$DIR/$tdir/remote_dir
1132
1133         test_mkdir $DIR/$tdir
1134         $LFS mkdir -i $MDTIDX $remote_dir ||
1135                 error "create remote directory failed"
1136
1137         test_mkdir $DIR/$tdir/src_dir
1138         touch $DIR/$tdir/src_file
1139         test_mkdir $remote_dir/tgt_dir
1140         touch $remote_dir/tgt_file
1141
1142         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1143                 error "rename dir cross MDT failed!"
1144
1145         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1146                 error "rename file cross MDT failed!"
1147
1148         touch $DIR/$tdir/ln_file
1149         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1150                 error "ln file cross MDT failed"
1151
1152         rm -rf $DIR/$tdir || error "Can not delete directories"
1153 }
1154 run_test 24x "cross MDT rename/link"
1155
1156 test_24y() {
1157         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
1158         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1159         local remote_dir=$DIR/$tdir/remote_dir
1160         local mdtidx=1
1161
1162         test_mkdir $DIR/$tdir
1163         $LFS mkdir -i $mdtidx $remote_dir ||
1164                    error "create remote directory failed"
1165
1166         test_mkdir $remote_dir/src_dir
1167         touch $remote_dir/src_file
1168         test_mkdir $remote_dir/tgt_dir
1169         touch $remote_dir/tgt_file
1170
1171         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1172                 error "rename subdir in the same remote dir failed!"
1173
1174         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1175                 error "rename files in the same remote dir failed!"
1176
1177         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1178                 error "link files in the same remote dir failed!"
1179
1180         rm -rf $DIR/$tdir || error "Can not delete directories"
1181 }
1182 run_test 24y "rename/link on the same dir should succeed"
1183
1184 test_24A() { # LU-3182
1185         local NFILES=5000
1186
1187         rm -rf $DIR/$tdir
1188         test_mkdir $DIR/$tdir
1189         trap simple_cleanup_common EXIT
1190         createmany -m $DIR/$tdir/$tfile $NFILES
1191         local t=$(ls $DIR/$tdir | wc -l)
1192         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1193         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1194         if [ $t -ne $NFILES -o $u -ne $NFILES -o $v -ne $((NFILES + 2)) ] ; then
1195                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1196         fi
1197
1198         simple_cleanup_common || error "Can not delete directories"
1199 }
1200 run_test 24A "readdir() returns correct number of entries."
1201
1202 test_24B() { # LU-4805
1203         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1204         local count
1205
1206         test_mkdir $DIR/$tdir
1207         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1208                 error "create striped dir failed"
1209
1210         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1211         [ $count -eq 2 ] || error "Expected 2, got $count"
1212
1213         touch $DIR/$tdir/striped_dir/a
1214
1215         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1216         [ $count -eq 3 ] || error "Expected 3, got $count"
1217
1218         touch $DIR/$tdir/striped_dir/.f
1219
1220         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1221         [ $count -eq 4 ] || error "Expected 4, got $count"
1222
1223         rm -rf $DIR/$tdir || error "Can not delete directories"
1224 }
1225 run_test 24B "readdir for striped dir return correct number of entries"
1226
1227 test_24C() {
1228         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
1229
1230         mkdir $DIR/$tdir
1231         mkdir $DIR/$tdir/d0
1232         mkdir $DIR/$tdir/d1
1233
1234         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1235                 error "create striped dir failed"
1236
1237         cd $DIR/$tdir/d0/striped_dir
1238
1239         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1240         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1241         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1242
1243         [ "$d0_ino" = "$parent_ino" ] ||
1244                 error ".. wrong, expect $d0_ino, get $parent_ino"
1245
1246         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1247                 error "mv striped dir failed"
1248
1249         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1250
1251         [ "$d1_ino" = "$parent_ino" ] ||
1252                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1253 }
1254 run_test 24C "check .. in striped dir"
1255
1256 test_24E() {
1257         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
1258         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1259
1260         mkdir -p $DIR/$tdir
1261         mkdir $DIR/$tdir/src_dir
1262         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1263                 error "create remote source failed"
1264
1265         touch $DIR/$tdir/src_dir/src_child/a
1266
1267         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1268                 error "create remote target dir failed"
1269
1270         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1271                 error "create remote target child failed"
1272
1273         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1274                 error "rename dir cross MDT failed!"
1275
1276         find $DIR/$tdir
1277
1278         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1279                 error "src_child still exists after rename"
1280
1281         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1282                 error "missing file(a) after rename"
1283
1284         rm -rf $DIR/$tdir || error "Can not delete directories"
1285 }
1286 run_test 24E "cross MDT rename/link"
1287
1288 test_25a() {
1289         echo '== symlink sanity ============================================='
1290
1291         test_mkdir $DIR/d25
1292         ln -s d25 $DIR/s25
1293         touch $DIR/s25/foo || error
1294 }
1295 run_test 25a "create file in symlinked directory ==============="
1296
1297 test_25b() {
1298         [ ! -d $DIR/d25 ] && test_25a
1299         $CHECKSTAT -t file $DIR/s25/foo || error
1300 }
1301 run_test 25b "lookup file in symlinked directory ==============="
1302
1303 test_26a() {
1304         test_mkdir $DIR/d26
1305         test_mkdir $DIR/d26/d26-2
1306         ln -s d26/d26-2 $DIR/s26
1307         touch $DIR/s26/foo || error
1308 }
1309 run_test 26a "multiple component symlink ======================="
1310
1311 test_26b() {
1312         test_mkdir -p $DIR/$tdir/d26-2
1313         ln -s $tdir/d26-2/foo $DIR/s26-2
1314         touch $DIR/s26-2 || error
1315 }
1316 run_test 26b "multiple component symlink at end of lookup ======"
1317
1318 test_26c() {
1319         test_mkdir $DIR/d26.2
1320         touch $DIR/d26.2/foo
1321         ln -s d26.2 $DIR/s26.2-1
1322         ln -s s26.2-1 $DIR/s26.2-2
1323         ln -s s26.2-2 $DIR/s26.2-3
1324         chmod 0666 $DIR/s26.2-3/foo
1325 }
1326 run_test 26c "chain of symlinks"
1327
1328 # recursive symlinks (bug 439)
1329 test_26d() {
1330         ln -s d26-3/foo $DIR/d26-3
1331 }
1332 run_test 26d "create multiple component recursive symlink"
1333
1334 test_26e() {
1335         [ ! -h $DIR/d26-3 ] && test_26d
1336         rm $DIR/d26-3
1337 }
1338 run_test 26e "unlink multiple component recursive symlink"
1339
1340 # recursive symlinks (bug 7022)
1341 test_26f() {
1342         test_mkdir $DIR/$tdir
1343         test_mkdir $DIR/$tdir/$tfile
1344         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1345         test_mkdir -p lndir/bar1
1346         test_mkdir $DIR/$tdir/$tfile/$tfile
1347         cd $tfile                || error "cd $tfile failed"
1348         ln -s .. dotdot          || error "ln dotdot failed"
1349         ln -s dotdot/lndir lndir || error "ln lndir failed"
1350         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1351         output=`ls $tfile/$tfile/lndir/bar1`
1352         [ "$output" = bar1 ] && error "unexpected output"
1353         rm -r $tfile             || error "rm $tfile failed"
1354         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1355 }
1356 run_test 26f "rm -r of a directory which has recursive symlink"
1357
1358 test_27a() {
1359         test_mkdir $DIR/$tdir
1360         $LFS getstripe $DIR/$tdir
1361         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1362         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1363         cp /etc/hosts $DIR/$tdir/$tfile || error
1364 }
1365 run_test 27a "one stripe file"
1366
1367 test_27b() {
1368         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1369         test_mkdir $DIR/$tdir
1370         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1371         $LFS getstripe -c $DIR/$tdir/$tfile
1372         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1373                 error "two-stripe file doesn't have two stripes"
1374
1375         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1376 }
1377 run_test 27b "create and write to two stripe file"
1378
1379 test_27d() {
1380         test_mkdir $DIR/$tdir
1381         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1382                 error "setstripe failed"
1383         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1384         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1385 }
1386 run_test 27d "create file with default settings"
1387
1388 test_27e() {
1389         # LU-5839 adds check for existed layout before setting it
1390         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.56) ]] &&
1391                 skip "Need MDS version at least 2.7.56" && return
1392         test_mkdir $DIR/$tdir
1393         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1394         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1395         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1396 }
1397 run_test 27e "setstripe existing file (should return error)"
1398
1399 test_27f() {
1400         test_mkdir $DIR/$tdir
1401         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1402                 error "$SETSTRIPE $DIR/$tdir/$tfile failed"
1403         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1404                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1405         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1406         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1407 }
1408 run_test 27f "setstripe with bad stripe size (should return error)"
1409
1410 test_27g() {
1411         test_mkdir $DIR/$tdir
1412         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1413         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1414                 error "$DIR/$tdir/$tfile has object"
1415 }
1416 run_test 27g "$LFS getstripe with no objects"
1417
1418 test_27i() {
1419         test_mkdir $DIR/$tdir
1420         touch $DIR/$tdir/$tfile || error "touch failed"
1421         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1422                 error "missing objects"
1423 }
1424 run_test 27i "$LFS getstripe with some objects"
1425
1426 test_27j() {
1427         test_mkdir $DIR/$tdir
1428         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1429                 error "setstripe failed" || true
1430 }
1431 run_test 27j "setstripe with bad stripe offset (should return error)"
1432
1433 test_27k() { # bug 2844
1434         test_mkdir $DIR/$tdir
1435         local file=$DIR/$tdir/$tfile
1436         local ll_max_blksize=$((4 * 1024 * 1024))
1437         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1438         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1439         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1440         dd if=/dev/zero of=$file bs=4k count=1
1441         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1442         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1443 }
1444 run_test 27k "limit i_blksize for broken user apps"
1445
1446 test_27l() {
1447         mcreate $DIR/$tfile || error "creating file"
1448         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1449                 error "setstripe should have failed" || true
1450 }
1451 run_test 27l "check setstripe permissions (should return error)"
1452
1453 test_27m() {
1454         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1455
1456         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1457                    head -n1)
1458         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1459                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1460                 return
1461         fi
1462         trap simple_cleanup_common EXIT
1463         test_mkdir $DIR/$tdir
1464         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1465         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1466                 error "dd should fill OST0"
1467         i=2
1468         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1469                 i=$((i + 1))
1470                 [ $i -gt 256 ] && break
1471         done
1472         i=$((i + 1))
1473         touch $DIR/$tdir/$tfile.$i
1474         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1475             awk '{print $1}'| grep -w "0") ] &&
1476                 error "OST0 was full but new created file still use it"
1477         i=$((i + 1))
1478         touch $DIR/$tdir/$tfile.$i
1479         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1480             awk '{print $1}'| grep -w "0") ] &&
1481                 error "OST0 was full but new created file still use it"
1482         simple_cleanup_common
1483 }
1484 run_test 27m "create file while OST0 was full"
1485
1486 sleep_maxage() {
1487         local delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1488                       head -n 1 | awk '{ print $1 * 2 }')
1489         sleep $delay
1490 }
1491
1492 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1493 # if the OST isn't full anymore.
1494 reset_enospc() {
1495         local OSTIDX=${1:-""}
1496
1497         local list=$(comma_list $(osts_nodes))
1498         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1499
1500         do_nodes $list lctl set_param fail_loc=0
1501         sync    # initiate all OST_DESTROYs from MDS to OST
1502         sleep_maxage
1503 }
1504
1505 exhaust_precreations() {
1506         local OSTIDX=$1
1507         local FAILLOC=$2
1508         local FAILIDX=${3:-$OSTIDX}
1509         local ofacet=ost$((OSTIDX + 1))
1510
1511         test_mkdir -p -c1 $DIR/$tdir
1512         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
1513         local mfacet=mds$((mdtidx + 1))
1514         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1515
1516         local OST=$(ostname_from_index $OSTIDX)
1517
1518         # on the mdt's osc
1519         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1520         local last_id=$(do_facet $mfacet lctl get_param -n \
1521                         osc.$mdtosc_proc1.prealloc_last_id)
1522         local next_id=$(do_facet $mfacet lctl get_param -n \
1523                         osc.$mdtosc_proc1.prealloc_next_id)
1524
1525         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1526         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1527
1528         test_mkdir -p $DIR/$tdir/${OST}
1529         $SETSTRIPE -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1530 #define OBD_FAIL_OST_ENOSPC              0x215
1531         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1532         echo "Creating to objid $last_id on ost $OST..."
1533         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1534         do_facet $mfacet lctl get_param osc.$mdtosc_proc2.prealloc*
1535         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1536         sleep_maxage
1537 }
1538
1539 exhaust_all_precreations() {
1540         local i
1541         for (( i=0; i < OSTCOUNT; i++ )) ; do
1542                 exhaust_precreations $i $1 -1
1543         done
1544 }
1545
1546 test_27n() {
1547         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1549         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1550         remote_ost_nodsh && skip "remote OST with nodsh" && return
1551
1552         reset_enospc
1553         rm -f $DIR/$tdir/$tfile
1554         exhaust_precreations 0 0x80000215
1555         $LFS setstripe -c -1 $DIR/$tdir
1556         touch $DIR/$tdir/$tfile || error
1557         $LFS getstripe $DIR/$tdir/$tfile
1558         reset_enospc
1559 }
1560 run_test 27n "create file with some full OSTs"
1561
1562 test_27o() {
1563         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1564         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1565         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1566         remote_ost_nodsh && skip "remote OST with nodsh" && return
1567
1568         reset_enospc
1569         rm -f $DIR/$tdir/$tfile
1570         exhaust_all_precreations 0x215
1571
1572         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1573
1574         reset_enospc
1575         rm -rf $DIR/$tdir/*
1576 }
1577 run_test 27o "create file with all full OSTs (should error)"
1578
1579 test_27p() {
1580         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1581         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1582         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1583         remote_ost_nodsh && skip "remote OST with nodsh" && return
1584
1585         reset_enospc
1586         rm -f $DIR/$tdir/$tfile
1587         test_mkdir $DIR/$tdir
1588
1589         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1590         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1591         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1592
1593         exhaust_precreations 0 0x80000215
1594         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1595         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1596         $LFS getstripe $DIR/$tdir/$tfile
1597
1598         reset_enospc
1599 }
1600 run_test 27p "append to a truncated file with some full OSTs"
1601
1602 test_27q() {
1603         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1604         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1605         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1606         remote_ost_nodsh && skip "remote OST with nodsh" && return
1607
1608         reset_enospc
1609         rm -f $DIR/$tdir/$tfile
1610
1611         test_mkdir $DIR/$tdir
1612         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1613         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1614                 error "truncate $DIR/$tdir/$tfile failed"
1615         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1616
1617         exhaust_all_precreations 0x215
1618
1619         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1620         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1621
1622         reset_enospc
1623 }
1624 run_test 27q "append to truncated file with all OSTs full (should error)"
1625
1626 test_27r() {
1627         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1629         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1630         remote_ost_nodsh && skip "remote OST with nodsh" && return
1631
1632         reset_enospc
1633         rm -f $DIR/$tdir/$tfile
1634         exhaust_precreations 0 0x80000215
1635
1636         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile # && error
1637
1638         reset_enospc
1639 }
1640 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1641
1642 test_27s() { # bug 10725
1643         test_mkdir $DIR/$tdir
1644         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1645         local stripe_count=0
1646         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1647         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1648                 error "stripe width >= 2^32 succeeded" || true
1649
1650 }
1651 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1652
1653 test_27t() { # bug 10864
1654         WDIR=$(pwd)
1655         WLFS=$(which lfs)
1656         cd $DIR
1657         touch $tfile
1658         $WLFS getstripe $tfile
1659         cd $WDIR
1660 }
1661 run_test 27t "check that utils parse path correctly"
1662
1663 test_27u() { # bug 4900
1664         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1665         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1666         local index
1667         local list=$(comma_list $(mdts_nodes))
1668
1669 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1670         do_nodes $list $LCTL set_param fail_loc=0x139
1671         test_mkdir -p $DIR/$tdir
1672         trap simple_cleanup_common EXIT
1673         createmany -o $DIR/$tdir/t- 1000
1674         do_nodes $list $LCTL set_param fail_loc=0
1675
1676         TLOG=$TMP/$tfile.getstripe
1677         $LFS getstripe $DIR/$tdir > $TLOG
1678         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1679         unlinkmany $DIR/$tdir/t- 1000
1680         trap 0
1681         [[ $OBJS -gt 0 ]] &&
1682                 error "$OBJS objects created on OST-0. See $TLOG" ||
1683                 rm -f $TLOG
1684 }
1685 run_test 27u "skip object creation on OSC w/o objects"
1686
1687 test_27v() { # bug 4900
1688         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1689         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1690         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1691         remote_ost_nodsh && skip "remote OST with nodsh" && return
1692
1693         exhaust_all_precreations 0x215
1694         reset_enospc
1695
1696         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1697
1698         touch $DIR/$tdir/$tfile
1699         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1700         # all except ost1
1701         for (( i=1; i < OSTCOUNT; i++ )); do
1702                 do_facet ost$i lctl set_param fail_loc=0x705
1703         done
1704         local START=`date +%s`
1705         createmany -o $DIR/$tdir/$tfile 32
1706
1707         local FINISH=`date +%s`
1708         local TIMEOUT=`lctl get_param -n timeout`
1709         local PROCESS=$((FINISH - START))
1710         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
1711                error "$FINISH - $START >= $TIMEOUT / 2"
1712         sleep $((TIMEOUT / 2 - PROCESS))
1713         reset_enospc
1714 }
1715 run_test 27v "skip object creation on slow OST"
1716
1717 test_27w() { # bug 10997
1718         test_mkdir $DIR/$tdir
1719         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
1720         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
1721                 error "stripe size $size != 65536" || true
1722         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
1723                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
1724 }
1725 run_test 27w "check $LFS setstripe -S and getstrip -d options"
1726
1727 test_27wa() {
1728         [[ $OSTCOUNT -lt 2 ]] &&
1729                 skip_env "skipping multiple stripe count/offset test" && return
1730
1731         test_mkdir $DIR/$tdir
1732         for i in $(seq 1 $OSTCOUNT); do
1733                 offset=$((i - 1))
1734                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
1735                         error "setstripe -c $i -i $offset failed"
1736                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
1737                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
1738                 [ $count -ne $i ] && error "stripe count $count != $i" || true
1739                 [ $index -ne $offset ] &&
1740                         error "stripe offset $index != $offset" || true
1741         done
1742 }
1743 run_test 27wa "check $LFS setstripe -c -i options"
1744
1745 test_27x() {
1746         remote_ost_nodsh && skip "remote OST with nodsh" && return
1747         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1748         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1749         OFFSET=$(($OSTCOUNT - 1))
1750         OSTIDX=0
1751         local OST=$(ostname_from_index $OSTIDX)
1752
1753         test_mkdir $DIR/$tdir
1754         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
1755         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
1756         sleep_maxage
1757         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
1758         for i in $(seq 0 $OFFSET); do
1759                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
1760                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
1761                 error "OST0 was degraded but new created file still use it"
1762         done
1763         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
1764 }
1765 run_test 27x "create files while OST0 is degraded"
1766
1767 test_27y() {
1768         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
1769         remote_mds_nodsh && skip "remote MDS with nodsh" && return
1770         remote_ost_nodsh && skip "remote OST with nodsh" && return
1771         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1772
1773         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
1774         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
1775                 osc.$mdtosc.prealloc_last_id)
1776         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
1777                 osc.$mdtosc.prealloc_next_id)
1778         local fcount=$((last_id - next_id))
1779         [[ $fcount -eq 0 ]] && skip "not enough space on OST0" && return
1780         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
1781
1782         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
1783                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
1784         local OST_DEACTIVE_IDX=-1
1785         local OSC
1786         local OSTIDX
1787         local OST
1788
1789         for OSC in $MDS_OSCS; do
1790                 OST=$(osc_to_ost $OSC)
1791                 OSTIDX=$(index_from_ostuuid $OST)
1792                 if [ $OST_DEACTIVE_IDX == -1 ]; then
1793                         OST_DEACTIVE_IDX=$OSTIDX
1794                 fi
1795                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
1796                         echo $OSC "is Deactivated:"
1797                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
1798                 fi
1799         done
1800
1801         OSTIDX=$(index_from_ostuuid $OST)
1802         test_mkdir $DIR/$tdir
1803         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
1804
1805         for OSC in $MDS_OSCS; do
1806                 OST=$(osc_to_ost $OSC)
1807                 OSTIDX=$(index_from_ostuuid $OST)
1808                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1809                         echo $OST "is degraded:"
1810                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1811                                                 obdfilter.$OST.degraded=1
1812                 fi
1813         done
1814
1815         sleep_maxage
1816         createmany -o $DIR/$tdir/$tfile $fcount
1817
1818         for OSC in $MDS_OSCS; do
1819                 OST=$(osc_to_ost $OSC)
1820                 OSTIDX=$(index_from_ostuuid $OST)
1821                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
1822                         echo $OST "is recovered from degraded:"
1823                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
1824                                                 obdfilter.$OST.degraded=0
1825                 else
1826                         do_facet $SINGLEMDS lctl --device %$OSC activate
1827                 fi
1828         done
1829
1830         # all osp devices get activated, hence -1 stripe count restored
1831         local stripe_count=0
1832
1833         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
1834         # devices get activated.
1835         sleep_maxage
1836         $LFS setstripe -c -1 $DIR/$tfile
1837         stripe_count=$($LFS getstripe -c $DIR/$tfile)
1838         rm -f $DIR/$tfile
1839         [ $stripe_count -ne $OSTCOUNT ] &&
1840                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
1841         return 0
1842 }
1843 run_test 27y "create files while OST0 is degraded and the rest inactive"
1844
1845 check_seq_oid()
1846 {
1847         log "check file $1"
1848
1849         lmm_count=$($GETSTRIPE -c $1)
1850         lmm_seq=$($GETSTRIPE -v $1 | awk '/lmm_seq/ { print $2 }')
1851         lmm_oid=$($GETSTRIPE -v $1 | awk '/lmm_object_id/ { print $2 }')
1852
1853         local old_ifs="$IFS"
1854         IFS=$'[:]'
1855         fid=($($LFS path2fid $1))
1856         IFS="$old_ifs"
1857
1858         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
1859         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
1860
1861         # compare lmm_seq and lu_fid->f_seq
1862         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
1863         # compare lmm_object_id and lu_fid->oid
1864         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
1865
1866         # check the trusted.fid attribute of the OST objects of the file
1867         local have_obdidx=false
1868         local stripe_nr=0
1869         $GETSTRIPE $1 | while read obdidx oid hex seq; do
1870                 # skip lines up to and including "obdidx"
1871                 [ -z "$obdidx" ] && break
1872                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
1873                 $have_obdidx || continue
1874
1875                 local ost=$((obdidx + 1))
1876                 local dev=$(ostdevname $ost)
1877                 local oid_hex
1878
1879                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
1880
1881                 seq=$(echo $seq | sed -e "s/^0x//g")
1882                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
1883                         oid_hex=$(echo $oid)
1884                 else
1885                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
1886                 fi
1887                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
1888
1889                 local ff=""
1890                 #
1891                 # Don't unmount/remount the OSTs if we don't need to do that.
1892                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
1893                 # update too, until that use mount/ll_decode_filter_fid/mount.
1894                 # Re-enable when debugfs will understand new filter_fid.
1895                 #
1896                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
1897                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
1898                                 $dev 2>/dev/null" | grep "parent=")
1899                 fi
1900                 if [ -z "$ff" ]; then
1901                         stop ost$ost
1902                         mount_fstype ost$ost
1903                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
1904                                 $(facet_mntpt ost$ost)/$obj_file)
1905                         unmount_fstype ost$ost
1906                         start ost$ost $dev $OST_MOUNT_OPTS
1907                         clients_up
1908                 fi
1909
1910                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
1911
1912                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
1913
1914                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
1915                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
1916                 #
1917                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
1918                 #       stripe_size=1048576 component_id=1 component_start=0 \
1919                 #       component_end=33554432
1920                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
1921                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
1922                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
1923                 local ff_pstripe
1924                 if grep -q 'stripe=' <<<$ff; then
1925                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
1926                 else
1927                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
1928                         # into f_ver in this case.  See comment on ff_parent.
1929                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
1930                 fi
1931
1932                 # compare lmm_seq and filter_fid->ff_parent.f_seq
1933                 [ $ff_pseq = $lmm_seq ] ||
1934                         error "FF parent SEQ $ff_pseq != $lmm_seq"
1935                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
1936                 [ $ff_poid = $lmm_oid ] ||
1937                         error "FF parent OID $ff_poid != $lmm_oid"
1938                 (($ff_pstripe == $stripe_nr)) ||
1939                         error "FF stripe $ff_pstripe != $stripe_nr"
1940
1941                 stripe_nr=$((stripe_nr + 1))
1942                 [ $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
1943                         continue
1944                 if grep -q 'stripe_count=' <<<$ff; then
1945                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
1946                                             -e 's/ .*//' <<<$ff)
1947                         [ $lmm_count = $ff_scnt ] ||
1948                                 error "FF stripe count $lmm_count != $ff_scnt"
1949                 fi
1950         done
1951 }
1952
1953 test_27z() {
1954         remote_ost_nodsh && skip "remote OST with nodsh" && return
1955         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1956         test_mkdir $DIR/$tdir
1957
1958         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
1959                 { error "setstripe -c -1 failed"; return 1; }
1960         # We need to send a write to every object to get parent FID info set.
1961         # This _should_ also work for setattr, but does not currently.
1962         # touch $DIR/$tdir/$tfile-1 ||
1963         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
1964                 { error "dd $tfile-1 failed"; return 2; }
1965         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
1966                 { error "setstripe -c -1 failed"; return 3; }
1967         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
1968                 { error "dd $tfile-2 failed"; return 4; }
1969
1970         # make sure write RPCs have been sent to OSTs
1971         sync; sleep 5; sync
1972
1973         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
1974         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
1975 }
1976 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
1977
1978 test_27A() { # b=19102
1979         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
1980         local restore_size=$($GETSTRIPE -S $MOUNT)
1981         local restore_count=$($GETSTRIPE -c $MOUNT)
1982         local restore_offset=$($GETSTRIPE -i $MOUNT)
1983         $SETSTRIPE -c 0 -i -1 -S 0 $MOUNT
1984         wait_update $HOSTNAME "$GETSTRIPE -c $MOUNT | sed 's/  *//g'" "1" 20 ||
1985                 error "stripe count $($GETSTRIPE -c $MOUNT) != 1"
1986         local default_size=$($GETSTRIPE -S $MOUNT)
1987         local default_offset=$($GETSTRIPE -i $MOUNT)
1988         local dsize=$((1024 * 1024))
1989         [ $default_size -eq $dsize ] ||
1990                 error "stripe size $default_size != $dsize"
1991         [ $default_offset -eq -1 ] ||error "stripe offset $default_offset != -1"
1992         $SETSTRIPE -c $restore_count -i $restore_offset -S $restore_size $MOUNT
1993 }
1994 run_test 27A "check filesystem-wide default LOV EA values"
1995
1996 test_27B() { # LU-2523
1997         test_mkdir $DIR/$tdir
1998         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
1999         touch $DIR/$tdir/f0
2000         # open f1 with O_LOV_DELAY_CREATE
2001         # rename f0 onto f1
2002         # call setstripe ioctl on open file descriptor for f1
2003         # close
2004         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2005                 $DIR/$tdir/f0
2006
2007         rm -f $DIR/$tdir/f1
2008         # open f1 with O_LOV_DELAY_CREATE
2009         # unlink f1
2010         # call setstripe ioctl on open file descriptor for f1
2011         # close
2012         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2013
2014         # Allow multiop to fail in imitation of NFS's busted semantics.
2015         true
2016 }
2017 run_test 27B "call setstripe on open unlinked file/rename victim"
2018
2019 test_27C() { #LU-2871
2020         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs" && return
2021
2022         declare -a ost_idx
2023         local index
2024         local found
2025         local i
2026         local j
2027
2028         test_mkdir $DIR/$tdir
2029         cd $DIR/$tdir
2030         for i in $(seq 0 $((OSTCOUNT - 1))); do
2031                 # set stripe across all OSTs starting from OST$i
2032                 $SETSTRIPE -i $i -c -1 $tfile$i
2033                 # get striping information
2034                 ost_idx=($($GETSTRIPE $tfile$i |
2035                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2036                 echo ${ost_idx[@]}
2037
2038                 # check the layout
2039                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2040                         error "${#ost_idx[@]} != $OSTCOUNT"
2041
2042                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2043                         found=0
2044                         for j in $(echo ${ost_idx[@]}); do
2045                                 if [ $index -eq $j ]; then
2046                                         found=1
2047                                         break
2048                                 fi
2049                         done
2050                         [ $found = 1 ] ||
2051                                 error "Can not find $index in ${ost_idx[@]}"
2052                 done
2053         done
2054 }
2055 run_test 27C "check full striping across all OSTs"
2056
2057 test_27D() {
2058         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2059         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
2060         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2061         local POOL=${POOL:-testpool}
2062         local first_ost=0
2063         local last_ost=$(($OSTCOUNT - 1))
2064         local ost_step=1
2065         local ost_list=$(seq $first_ost $ost_step $last_ost)
2066         local ost_range="$first_ost $last_ost $ost_step"
2067
2068         if ! combined_mgs_mds ; then
2069                 mount_mgs_client
2070         fi
2071
2072         test_mkdir $DIR/$tdir
2073         pool_add $POOL || error "pool_add failed"
2074         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2075
2076         local skip27D
2077         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ] &&
2078                 skip27D += "-s 29"
2079         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.55) -o \
2080           $(lustre_version_code client) -lt $(version_code 2.9.55) ] &&
2081                 skip27D += "-s 30,31"
2082         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2083                 error "llapi_layout_test failed"
2084
2085         destroy_test_pools || error "destroy test pools failed"
2086
2087         if ! combined_mgs_mds ; then
2088                 umount_mgs_client
2089         fi
2090 }
2091 run_test 27D "validate llapi_layout API"
2092
2093 # Verify that default_easize is increased from its initial value after
2094 # accessing a widely striped file.
2095 test_27E() {
2096         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
2097         [ $(lustre_version_code client) -lt $(version_code 2.5.57) ] &&
2098                 skip "client does not have LU-3338 fix" && return
2099
2100         # 72 bytes is the minimum space required to store striping
2101         # information for a file striped across one OST:
2102         # (sizeof(struct lov_user_md_v3) +
2103         #  sizeof(struct lov_user_ost_data_v1))
2104         local min_easize=72
2105         $LCTL set_param -n llite.*.default_easize $min_easize ||
2106                 error "lctl set_param failed"
2107         local easize=$($LCTL get_param -n llite.*.default_easize)
2108
2109         [ $easize -eq $min_easize ] ||
2110                 error "failed to set default_easize"
2111
2112         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2113                 error "setstripe failed"
2114         cat $DIR/$tfile
2115         rm $DIR/$tfile
2116
2117         easize=$($LCTL get_param -n llite.*.default_easize)
2118
2119         [ $easize -gt $min_easize ] ||
2120                 error "default_easize not updated"
2121 }
2122 run_test 27E "check that default extended attribute size properly increases"
2123
2124 test_27F() { # LU-5346/LU-7975
2125         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2126         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.51) ]] &&
2127                 skip "Need MDS version at least 2.8.51" && return
2128         remote_ost_nodsh && skip "remote OST with nodsh" && return
2129
2130         test_mkdir $DIR/$tdir
2131         rm -f $DIR/$tdir/f0
2132         $SETSTRIPE -c 2 $DIR/$tdir
2133
2134         # stop all OSTs to reproduce situation for LU-7975 ticket
2135         for num in $(seq $OSTCOUNT); do
2136                 stop ost$num
2137         done
2138
2139         # open/create f0 with O_LOV_DELAY_CREATE
2140         # truncate f0 to a non-0 size
2141         # close
2142         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2143
2144         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2145         # open/write it again to force delayed layout creation
2146         cat /etc/hosts > $DIR/$tdir/f0 &
2147         catpid=$!
2148
2149         # restart OSTs
2150         for num in $(seq $OSTCOUNT); do
2151                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2152                         error "ost$num failed to start"
2153         done
2154
2155         wait $catpid || error "cat failed"
2156
2157         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2158         [[ $($GETSTRIPE -c $DIR/$tdir/f0) == 2 ]] || error "wrong stripecount"
2159
2160 }
2161 run_test 27F "Client resend delayed layout creation with non-zero size"
2162
2163 # createtest also checks that device nodes are created and
2164 # then visible correctly (#2091)
2165 test_28() { # bug 2091
2166         test_mkdir $DIR/d28
2167         $CREATETEST $DIR/d28/ct || error
2168 }
2169 run_test 28 "create/mknod/mkdir with bad file types ============"
2170
2171 test_29() {
2172         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return 0
2173         sync; sleep 1; sync # flush out any dirty pages from previous tests
2174         cancel_lru_locks
2175         test_mkdir $DIR/d29
2176         touch $DIR/d29/foo
2177         log 'first d29'
2178         ls -l $DIR/d29
2179
2180         declare -i LOCKCOUNTORIG=0
2181         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2182                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
2183         done
2184         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
2185
2186         declare -i LOCKUNUSEDCOUNTORIG=0
2187         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2188                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
2189         done
2190
2191         log 'second d29'
2192         ls -l $DIR/d29
2193         log 'done'
2194
2195         declare -i LOCKCOUNTCURRENT=0
2196         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
2197                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
2198         done
2199
2200         declare -i LOCKUNUSEDCOUNTCURRENT=0
2201         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
2202                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
2203         done
2204
2205         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
2206                 $LCTL set_param -n ldlm.dump_namespaces ""
2207                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
2208                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2209                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2210                 return 2
2211         fi
2212         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
2213                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
2214                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
2215                 log "dumped log to $TMP/test_29.dk (bug 5793)"
2216                 return 3
2217         fi
2218 }
2219 run_test 29 "IT_GETATTR regression  ============================"
2220
2221 test_30a() { # was test_30
2222         cp $(which ls) $DIR || cp /bin/ls $DIR
2223         $DIR/ls / || error
2224         rm $DIR/ls
2225 }
2226 run_test 30a "execute binary from Lustre (execve) =============="
2227
2228 test_30b() {
2229         cp `which ls` $DIR || cp /bin/ls $DIR
2230         chmod go+rx $DIR/ls
2231         $RUNAS $DIR/ls / || error
2232         rm $DIR/ls
2233 }
2234 run_test 30b "execute binary from Lustre as non-root ==========="
2235
2236 test_30c() { # b=22376
2237         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2238         cp `which ls` $DIR || cp /bin/ls $DIR
2239         chmod a-rw $DIR/ls
2240         cancel_lru_locks mdc
2241         cancel_lru_locks osc
2242         $RUNAS $DIR/ls / || error
2243         rm -f $DIR/ls
2244 }
2245 run_test 30c "execute binary from Lustre without read perms ===="
2246
2247 test_31a() {
2248         $OPENUNLINK $DIR/f31 $DIR/f31 || error
2249         $CHECKSTAT -a $DIR/f31 || error
2250 }
2251 run_test 31a "open-unlink file =================================="
2252
2253 test_31b() {
2254         touch $DIR/f31 || error
2255         ln $DIR/f31 $DIR/f31b || error
2256         $MULTIOP $DIR/f31b Ouc || error
2257         $CHECKSTAT -t file $DIR/f31 || error
2258 }
2259 run_test 31b "unlink file with multiple links while open ======="
2260
2261 test_31c() {
2262         touch $DIR/f31 || error
2263         ln $DIR/f31 $DIR/f31c || error
2264         multiop_bg_pause $DIR/f31 O_uc || return 1
2265         MULTIPID=$!
2266         $MULTIOP $DIR/f31c Ouc
2267         kill -USR1 $MULTIPID
2268         wait $MULTIPID
2269 }
2270 run_test 31c "open-unlink file with multiple links ============="
2271
2272 test_31d() {
2273         opendirunlink $DIR/d31d $DIR/d31d || error
2274         $CHECKSTAT -a $DIR/d31d || error
2275 }
2276 run_test 31d "remove of open directory ========================="
2277
2278 test_31e() { # bug 2904
2279         openfilleddirunlink $DIR/d31e || error
2280 }
2281 run_test 31e "remove of open non-empty directory ==============="
2282
2283 test_31f() { # bug 4554
2284         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2285         set -vx
2286         test_mkdir $DIR/d31f
2287         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2288         cp /etc/hosts $DIR/d31f
2289         ls -l $DIR/d31f
2290         $GETSTRIPE $DIR/d31f/hosts
2291         multiop_bg_pause $DIR/d31f D_c || return 1
2292         MULTIPID=$!
2293
2294         rm -rv $DIR/d31f || error "first of $DIR/d31f"
2295         test_mkdir $DIR/d31f
2296         $SETSTRIPE -S 1048576 -c 1 $DIR/d31f
2297         cp /etc/hosts $DIR/d31f
2298         ls -l $DIR/d31f
2299         $GETSTRIPE $DIR/d31f/hosts
2300         multiop_bg_pause $DIR/d31f D_c || return 1
2301         MULTIPID2=$!
2302
2303         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
2304         wait $MULTIPID || error "first opendir $MULTIPID failed"
2305
2306         sleep 6
2307
2308         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
2309         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
2310         set +vx
2311 }
2312 run_test 31f "remove of open directory with open-unlink file ==="
2313
2314 test_31g() {
2315         echo "-- cross directory link --"
2316         test_mkdir -c1 $DIR/${tdir}ga
2317         test_mkdir -c1 $DIR/${tdir}gb
2318         touch $DIR/${tdir}ga/f
2319         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
2320         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
2321         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
2322         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
2323         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
2324 }
2325 run_test 31g "cross directory link==============="
2326
2327 test_31h() {
2328         echo "-- cross directory link --"
2329         test_mkdir -c1 $DIR/${tdir}
2330         test_mkdir -c1 $DIR/${tdir}/dir
2331         touch $DIR/${tdir}/f
2332         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
2333         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
2334         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
2335         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
2336         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
2337 }
2338 run_test 31h "cross directory link under child==============="
2339
2340 test_31i() {
2341         echo "-- cross directory link --"
2342         test_mkdir -c1 $DIR/$tdir
2343         test_mkdir -c1 $DIR/$tdir/dir
2344         touch $DIR/$tdir/dir/f
2345         ln $DIR/$tdir/dir/f $DIR/$tdir/g
2346         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
2347         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
2348         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
2349         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
2350 }
2351 run_test 31i "cross directory link under parent==============="
2352
2353 test_31j() {
2354         test_mkdir -c1 -p $DIR/$tdir
2355         test_mkdir -c1 -p $DIR/$tdir/dir1
2356         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
2357         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
2358         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
2359         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
2360         return 0
2361 }
2362 run_test 31j "link for directory==============="
2363
2364 test_31k() {
2365         test_mkdir -c1 -p $DIR/$tdir
2366         touch $DIR/$tdir/s
2367         touch $DIR/$tdir/exist
2368         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
2369         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
2370         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
2371         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
2372         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
2373         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
2374         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
2375         return 0
2376 }
2377 run_test 31k "link to file: the same, non-existing, dir==============="
2378
2379 test_31m() {
2380         mkdir $DIR/d31m
2381         touch $DIR/d31m/s
2382         mkdir $DIR/d31m2
2383         touch $DIR/d31m2/exist
2384         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
2385         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
2386         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
2387         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
2388         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
2389         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
2390         return 0
2391 }
2392 run_test 31m "link to file: the same, non-existing, dir==============="
2393
2394 test_31n() {
2395         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
2396         nlink=$(stat --format=%h $DIR/$tfile)
2397         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2398         local fd=$(free_fd)
2399         local cmd="exec $fd<$DIR/$tfile"
2400         eval $cmd
2401         cmd="exec $fd<&-"
2402         trap "eval $cmd" EXIT
2403         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2404         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
2405         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
2406         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
2407         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
2408         eval $cmd
2409 }
2410 run_test 31n "check link count of unlinked file"
2411
2412 link_one() {
2413         local TEMPNAME=$(mktemp $1_XXXXXX)
2414         mlink $TEMPNAME $1 2> /dev/null &&
2415                 echo "$BASHPID: link $TEMPNAME to $1 succeeded"
2416         munlink $TEMPNAME
2417 }
2418
2419 test_31o() { # LU-2901
2420         test_mkdir $DIR/$tdir
2421         for LOOP in $(seq 100); do
2422                 rm -f $DIR/$tdir/$tfile*
2423                 for THREAD in $(seq 8); do
2424                         link_one $DIR/$tdir/$tfile.$LOOP &
2425                 done
2426                 wait
2427                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
2428                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
2429                         error "$LINKS duplicate links to $tfile.$LOOP" &&
2430                         break || true
2431         done
2432 }
2433 run_test 31o "duplicate hard links with same filename"
2434
2435 test_31p() {
2436         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2437
2438         test_mkdir $DIR/$tdir
2439         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2440         $LFS setdirstripe -D -c2 -t all_char $DIR/$tdir/striped_dir
2441
2442         opendirunlink $DIR/$tdir/striped_dir/test1 ||
2443                 error "open unlink test1 failed"
2444         opendirunlink $DIR/$tdir/striped_dir/test2 ||
2445                 error "open unlink test2 failed"
2446
2447         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
2448                 error "test1 still exists"
2449         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
2450                 error "test2 still exists"
2451 }
2452 run_test 31p "remove of open striped directory"
2453
2454 cleanup_test32_mount() {
2455         local rc=0
2456         trap 0
2457         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
2458         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
2459         losetup -d $loopdev || true
2460         rm -rf $DIR/$tdir
2461         return $rc
2462 }
2463
2464 test_32a() {
2465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2466         echo "== more mountpoints and symlinks ================="
2467         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2468         trap cleanup_test32_mount EXIT
2469         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2470         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2471         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. || error
2472         cleanup_test32_mount
2473 }
2474 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
2475
2476 test_32b() {
2477         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2478         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2479         trap cleanup_test32_mount EXIT
2480         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2481         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2482         ls -al $DIR/$tdir/ext2-mountpoint/.. || error
2483         cleanup_test32_mount
2484 }
2485 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
2486
2487 test_32c() {
2488         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2489         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2490         trap cleanup_test32_mount EXIT
2491         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2492         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2493         test_mkdir -p $DIR/$tdir/d2/test_dir
2494         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2495         cleanup_test32_mount
2496 }
2497 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
2498
2499 test_32d() {
2500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2501         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2502         trap cleanup_test32_mount EXIT
2503         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2504         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2505         test_mkdir -p $DIR/$tdir/d2/test_dir
2506         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir || error
2507         cleanup_test32_mount
2508 }
2509 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
2510
2511 test_32e() {
2512         rm -fr $DIR/$tdir
2513         test_mkdir -p $DIR/$tdir/tmp
2514         local tmp_dir=$DIR/$tdir/tmp
2515         ln -s $DIR/$tdir $tmp_dir/symlink11
2516         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2517         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
2518         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
2519 }
2520 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
2521
2522 test_32f() {
2523         rm -fr $DIR/$tdir
2524         test_mkdir -p $DIR/$tdir/tmp
2525         local tmp_dir=$DIR/$tdir/tmp
2526         ln -s $DIR/$tdir $tmp_dir/symlink11
2527         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
2528         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
2529         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
2530 }
2531 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
2532
2533 test_32g() {
2534         local tmp_dir=$DIR/$tdir/tmp
2535         test_mkdir -p $tmp_dir
2536         test_mkdir $DIR/${tdir}2
2537         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2538         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2539         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
2540         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
2541         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
2542         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
2543 }
2544 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2545
2546 test_32h() {
2547         rm -fr $DIR/$tdir $DIR/${tdir}2
2548         tmp_dir=$DIR/$tdir/tmp
2549         test_mkdir -p $tmp_dir
2550         test_mkdir $DIR/${tdir}2
2551         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
2552         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
2553         ls $tmp_dir/symlink12 || error "listing symlink12"
2554         ls $DIR/$tdir/symlink02  || error "listing symlink02"
2555 }
2556 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
2557
2558 test_32i() {
2559         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2560         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2561         trap cleanup_test32_mount EXIT
2562         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2563         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2564         touch $DIR/$tdir/test_file
2565         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file || error
2566         cleanup_test32_mount
2567 }
2568 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
2569
2570 test_32j() {
2571         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2572         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2573         trap cleanup_test32_mount EXIT
2574         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2575         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2576         touch $DIR/$tdir/test_file
2577         cat $DIR/$tdir/ext2-mountpoint/../test_file || error
2578         cleanup_test32_mount
2579 }
2580 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
2581
2582 test_32k() {
2583         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2584         rm -fr $DIR/$tdir
2585         trap cleanup_test32_mount EXIT
2586         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2587         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2588         test_mkdir -p $DIR/$tdir/d2
2589         touch $DIR/$tdir/d2/test_file || error
2590         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2591         cleanup_test32_mount
2592 }
2593 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
2594
2595 test_32l() {
2596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2597         rm -fr $DIR/$tdir
2598         trap cleanup_test32_mount EXIT
2599         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2600         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint || error
2601         test_mkdir -p $DIR/$tdir/d2
2602         touch $DIR/$tdir/d2/test_file
2603         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file || error
2604         cleanup_test32_mount
2605 }
2606 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
2607
2608 test_32m() {
2609         rm -fr $DIR/d32m
2610         test_mkdir -p $DIR/d32m/tmp
2611         TMP_DIR=$DIR/d32m/tmp
2612         ln -s $DIR $TMP_DIR/symlink11
2613         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2614         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 || error
2615         $CHECKSTAT -t link $DIR/d32m/symlink01 || error
2616 }
2617 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
2618
2619 test_32n() {
2620         rm -fr $DIR/d32n
2621         test_mkdir -p $DIR/d32n/tmp
2622         TMP_DIR=$DIR/d32n/tmp
2623         ln -s $DIR $TMP_DIR/symlink11
2624         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
2625         ls -l $DIR/d32n/tmp/symlink11  || error
2626         ls -l $DIR/d32n/symlink01 || error
2627 }
2628 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
2629
2630 test_32o() {
2631         touch $DIR/$tfile
2632         test_mkdir -p $DIR/d32o/tmp
2633         TMP_DIR=$DIR/d32o/tmp
2634         ln -s $DIR/$tfile $TMP_DIR/symlink12
2635         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2636         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 || error
2637         $CHECKSTAT -t link $DIR/d32o/symlink02 || error
2638         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 || error
2639         $CHECKSTAT -t file -f $DIR/d32o/symlink02 || error
2640 }
2641 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
2642
2643 test_32p() {
2644         log 32p_1
2645         rm -fr $DIR/d32p
2646         log 32p_2
2647         rm -f $DIR/$tfile
2648         log 32p_3
2649         touch $DIR/$tfile
2650         log 32p_4
2651         test_mkdir -p $DIR/d32p/tmp
2652         log 32p_5
2653         TMP_DIR=$DIR/d32p/tmp
2654         log 32p_6
2655         ln -s $DIR/$tfile $TMP_DIR/symlink12
2656         log 32p_7
2657         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
2658         log 32p_8
2659         cat $DIR/d32p/tmp/symlink12 || error
2660         log 32p_9
2661         cat $DIR/d32p/symlink02 || error
2662         log 32p_10
2663 }
2664 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
2665
2666 test_32q() {
2667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2668         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2669         trap cleanup_test32_mount EXIT
2670         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2671         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2672         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2673         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
2674         cleanup_test32_mount
2675 }
2676 run_test 32q "stat follows mountpoints in Lustre (should return error)"
2677
2678 test_32r() {
2679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2680         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
2681         trap cleanup_test32_mount EXIT
2682         test_mkdir -p $DIR/$tdir/ext2-mountpoint
2683         touch $DIR/$tdir/ext2-mountpoint/under_the_mount
2684         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint
2685         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
2686         cleanup_test32_mount
2687 }
2688 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
2689
2690 test_33aa() {
2691         rm -f $DIR/$tfile
2692         touch $DIR/$tfile
2693         chmod 444 $DIR/$tfile
2694         chown $RUNAS_ID $DIR/$tfile
2695         log 33_1
2696         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2697         log 33_2
2698 }
2699 run_test 33aa "write file with mode 444 (should return error)"
2700
2701 test_33a() {
2702         rm -fr $DIR/$tdir
2703         test_mkdir $DIR/$tdir
2704         chown $RUNAS_ID $DIR/$tdir
2705         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
2706                 error "$RUNAS create $tdir/$tfile failed"
2707         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
2708                 error "open RDWR" || true
2709 }
2710 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
2711
2712 test_33b() {
2713         rm -fr $DIR/$tdir
2714         test_mkdir $DIR/$tdir
2715         chown $RUNAS_ID $DIR/$tdir
2716         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
2717 }
2718 run_test 33b "test open file with malformed flags (No panic)"
2719
2720 test_33c() {
2721         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2722         local ostnum
2723         local ostname
2724         local write_bytes
2725         local all_zeros
2726
2727         remote_ost_nodsh && skip "remote OST with nodsh" && return
2728         all_zeros=:
2729         rm -fr $DIR/$tdir
2730         test_mkdir $DIR/$tdir
2731         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
2732
2733         sync
2734         for ostnum in $(seq $OSTCOUNT); do
2735                 # test-framework's OST numbering is one-based, while Lustre's
2736                 # is zero-based
2737                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2738                 # Parsing llobdstat's output sucks; we could grep the /proc
2739                 # path, but that's likely to not be as portable as using the
2740                 # llobdstat utility.  So we parse lctl output instead.
2741                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2742                         obdfilter/$ostname/stats |
2743                         awk '/^write_bytes/ {print $7}' )
2744                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
2745                 if (( ${write_bytes:-0} > 0 ))
2746                 then
2747                         all_zeros=false
2748                         break;
2749                 fi
2750         done
2751
2752         $all_zeros || return 0
2753
2754         # Write four bytes
2755         echo foo > $DIR/$tdir/bar
2756         # Really write them
2757         sync
2758
2759         # Total up write_bytes after writing.  We'd better find non-zeros.
2760         for ostnum in $(seq $OSTCOUNT); do
2761                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2762                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
2763                         obdfilter/$ostname/stats |
2764                         awk '/^write_bytes/ {print $7}' )
2765                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
2766                 if (( ${write_bytes:-0} > 0 ))
2767                 then
2768                         all_zeros=false
2769                         break;
2770                 fi
2771         done
2772
2773         if $all_zeros
2774         then
2775                 for ostnum in $(seq $OSTCOUNT); do
2776                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
2777                         echo "Check that write_bytes is present in obdfilter/*/stats:"
2778                         do_facet ost$ostnum lctl get_param -n \
2779                                 obdfilter/$ostname/stats
2780                 done
2781                 error "OST not keeping write_bytes stats (b22312)"
2782         fi
2783 }
2784 run_test 33c "test llobdstat and write_bytes"
2785
2786 test_33d() {
2787         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
2788         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2789         local MDTIDX=1
2790         local remote_dir=$DIR/$tdir/remote_dir
2791
2792         test_mkdir $DIR/$tdir
2793         $LFS mkdir -i $MDTIDX $remote_dir ||
2794                 error "create remote directory failed"
2795
2796         touch $remote_dir/$tfile
2797         chmod 444 $remote_dir/$tfile
2798         chown $RUNAS_ID $remote_dir/$tfile
2799
2800         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
2801
2802         chown $RUNAS_ID $remote_dir
2803         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
2804                                         error "create" || true
2805         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
2806                                     error "open RDWR" || true
2807         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
2808 }
2809 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
2810
2811 test_33e() {
2812         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2813
2814         mkdir $DIR/$tdir
2815
2816         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2817         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2818         mkdir $DIR/$tdir/local_dir
2819
2820         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2821         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2822         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2823
2824         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2825                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
2826
2827         rmdir $DIR/$tdir/* || error "rmdir failed"
2828
2829         umask 777
2830         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2831         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2832         mkdir $DIR/$tdir/local_dir
2833
2834         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2835         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2836         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2837
2838         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2839                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
2840
2841         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
2842
2843         umask 000
2844         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
2845         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
2846         mkdir $DIR/$tdir/local_dir
2847
2848         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
2849         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
2850         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
2851
2852         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
2853                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
2854 }
2855 run_test 33e "mkdir and striped directory should have same mode"
2856
2857 cleanup_33f() {
2858         trap 0
2859         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
2860 }
2861
2862 test_33f() {
2863         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
2864         remote_mds_nodsh && skip "remote MDS with nodsh" && return
2865
2866         mkdir $DIR/$tdir
2867         chmod go+rwx $DIR/$tdir
2868         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
2869         trap cleanup_33f EXIT
2870
2871         $RUNAS lfs mkdir -c$MDSCOUNT $DIR/$tdir/striped_dir ||
2872                 error "cannot create striped directory"
2873
2874         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
2875                 error "cannot create files in striped directory"
2876
2877         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
2878                 error "cannot remove files in striped directory"
2879
2880         $RUNAS rmdir $DIR/$tdir/striped_dir ||
2881                 error "cannot remove striped directory"
2882
2883         cleanup_33f
2884 }
2885 run_test 33f "nonroot user can create, access, and remove a striped directory"
2886
2887 test_33g() {
2888         mkdir -p $DIR/$tdir/dir2
2889
2890         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
2891         echo $err
2892         [[ $err =~ "exists" ]] || error "Not exists error"
2893 }
2894 run_test 33g "nonroot user create already existing root created file"
2895
2896 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
2897 test_34a() {
2898         rm -f $DIR/f34
2899         $MCREATE $DIR/f34 || error
2900         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2901         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error
2902         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2903         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2904 }
2905 run_test 34a "truncate file that has not been opened ==========="
2906
2907 test_34b() {
2908         [ ! -f $DIR/f34 ] && test_34a
2909         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2910         $OPENFILE -f O_RDONLY $DIR/f34
2911         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" || error
2912         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2913 }
2914 run_test 34b "O_RDONLY opening file doesn't create objects ====="
2915
2916 test_34c() {
2917         [ ! -f $DIR/f34 ] && test_34a
2918         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2919         $OPENFILE -f O_RDWR $DIR/f34
2920         $GETSTRIPE $DIR/f34 2>&1 | grep -q "no stripe info" && error
2921         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2922 }
2923 run_test 34c "O_RDWR opening file-with-size works =============="
2924
2925 test_34d() {
2926         [ ! -f $DIR/f34 ] && test_34a
2927         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 || error
2928         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 || error
2929         rm $DIR/f34
2930 }
2931 run_test 34d "write to sparse file ============================="
2932
2933 test_34e() {
2934         rm -f $DIR/f34e
2935         $MCREATE $DIR/f34e || error
2936         $TRUNCATE $DIR/f34e 1000 || error
2937         $CHECKSTAT -s 1000 $DIR/f34e || error
2938         $OPENFILE -f O_RDWR $DIR/f34e
2939         $CHECKSTAT -s 1000 $DIR/f34e || error
2940 }
2941 run_test 34e "create objects, some with size and some without =="
2942
2943 test_34f() { # bug 6242, 6243
2944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2945         SIZE34F=48000
2946         rm -f $DIR/f34f
2947         $MCREATE $DIR/f34f || error
2948         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
2949         dd if=$DIR/f34f of=$TMP/f34f
2950         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
2951         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
2952         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
2953         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
2954         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
2955 }
2956 run_test 34f "read from a file with no objects until EOF ======="
2957
2958 test_34g() {
2959         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2960         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE || error
2961         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error
2962         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || error "truncate failed"
2963         cancel_lru_locks osc
2964         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile || \
2965                 error "wrong size after lock cancel"
2966
2967         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error
2968         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2969                 error "expanding truncate failed"
2970         cancel_lru_locks osc
2971         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile || \
2972                 error "wrong expanded size after lock cancel"
2973 }
2974 run_test 34g "truncate long file ==============================="
2975
2976 test_34h() {
2977         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
2978         local gid=10
2979         local sz=1000
2980
2981         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error
2982         sync # Flush the cache so that multiop below does not block on cache
2983              # flush when getting the group lock
2984         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
2985         MULTIPID=$!
2986
2987         # Since just timed wait is not good enough, let's do a sync write
2988         # that way we are sure enough time for a roundtrip + processing
2989         # passed + 2 seconds of extra margin.
2990         dd if=/dev/zero of=$DIR/${tfile}-1 bs=4096 oflag=direct count=1
2991         rm $DIR/${tfile}-1
2992         sleep 2
2993
2994         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
2995                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
2996                 kill -9 $MULTIPID
2997         fi
2998         wait $MULTIPID
2999         local nsz=`stat -c %s $DIR/$tfile`
3000         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3001 }
3002 run_test 34h "ftruncate file under grouplock should not block"
3003
3004 test_35a() {
3005         cp /bin/sh $DIR/f35a
3006         chmod 444 $DIR/f35a
3007         chown $RUNAS_ID $DIR/f35a
3008         $RUNAS $DIR/f35a && error || true
3009         rm $DIR/f35a
3010 }
3011 run_test 35a "exec file with mode 444 (should return and not leak)"
3012
3013 test_36a() {
3014         rm -f $DIR/f36
3015         utime $DIR/f36 || error
3016 }
3017 run_test 36a "MDS utime check (mknod, utime)"
3018
3019 test_36b() {
3020         echo "" > $DIR/f36
3021         utime $DIR/f36 || error
3022 }
3023 run_test 36b "OST utime check (open, utime)"
3024
3025 test_36c() {
3026         rm -f $DIR/d36/f36
3027         test_mkdir $DIR/d36
3028         chown $RUNAS_ID $DIR/d36
3029         $RUNAS utime $DIR/d36/f36 || error
3030 }
3031 run_test 36c "non-root MDS utime check (mknod, utime)"
3032
3033 test_36d() {
3034         [ ! -d $DIR/d36 ] && test_36c
3035         echo "" > $DIR/d36/f36
3036         $RUNAS utime $DIR/d36/f36 || error
3037 }
3038 run_test 36d "non-root OST utime check (open, utime)"
3039
3040 test_36e() {
3041         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping" && return
3042         test_mkdir $DIR/$tdir
3043         touch $DIR/$tdir/$tfile
3044         $RUNAS utime $DIR/$tdir/$tfile &&
3045                 error "utime worked, expected failure" || true
3046 }
3047 run_test 36e "utime on non-owned file (should return error)"
3048
3049 subr_36fh() {
3050         local fl="$1"
3051         local LANG_SAVE=$LANG
3052         local LC_LANG_SAVE=$LC_LANG
3053         export LANG=C LC_LANG=C # for date language
3054
3055         DATESTR="Dec 20  2000"
3056         test_mkdir $DIR/$tdir
3057         lctl set_param fail_loc=$fl
3058         date; date +%s
3059         cp /etc/hosts $DIR/$tdir/$tfile
3060         sync & # write RPC generated with "current" inode timestamp, but delayed
3061         sleep 1
3062         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
3063         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
3064         cancel_lru_locks osc
3065         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
3066         date; date +%s
3067         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
3068                 echo "BEFORE: $LS_BEFORE" && \
3069                 echo "AFTER : $LS_AFTER" && \
3070                 echo "WANT  : $DATESTR" && \
3071                 error "$DIR/$tdir/$tfile timestamps changed" || true
3072
3073         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
3074 }
3075
3076 test_36f() {
3077         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3078         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
3079         subr_36fh "0x80000214"
3080 }
3081 run_test 36f "utime on file racing with OST BRW write =========="
3082
3083 test_36g() {
3084         remote_ost_nodsh && skip "remote OST with nodsh" && return
3085         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3086         local fmd_max_age
3087         local fmd_before
3088         local fmd_after
3089
3090         test_mkdir $DIR/$tdir
3091         fmd_max_age=$(do_facet ost1 \
3092                 "lctl get_param -n obdfilter.*.client_cache_seconds 2> /dev/null | \
3093                 head -n 1")
3094
3095         fmd_before=$(do_facet ost1 \
3096                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3097         touch $DIR/$tdir/$tfile
3098         sleep $((fmd_max_age + 12))
3099         fmd_after=$(do_facet ost1 \
3100                 "awk '/ll_fmd_cache/ {print \\\$2}' /proc/slabinfo")
3101
3102         echo "fmd_before: $fmd_before"
3103         echo "fmd_after: $fmd_after"
3104         [[ $fmd_after -gt $fmd_before ]] &&
3105                 echo "AFTER: $fmd_after > BEFORE: $fmd_before" &&
3106                 error "fmd didn't expire after ping" || true
3107 }
3108 run_test 36g "filter mod data cache expiry ====================="
3109
3110 test_36h() {
3111         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3112         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
3113         subr_36fh "0x80000227"
3114 }
3115 run_test 36h "utime on file racing with OST BRW write =========="
3116
3117 test_36i() {
3118         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
3119
3120         test_mkdir $DIR/$tdir
3121         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
3122
3123         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
3124         local new_mtime=$((mtime + 200))
3125
3126         #change Modify time of striped dir
3127         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
3128                         error "change mtime failed"
3129
3130         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
3131
3132         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
3133 }
3134 run_test 36i "change mtime on striped directory"
3135
3136 # test_37 - duplicate with tests 32q 32r
3137
3138 test_38() {
3139         local file=$DIR/$tfile
3140         touch $file
3141         openfile -f O_DIRECTORY $file
3142         local RC=$?
3143         local ENOTDIR=20
3144         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
3145         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
3146 }
3147 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
3148
3149 test_39a() { # was test_39
3150         touch $DIR/$tfile
3151         touch $DIR/${tfile}2
3152 #       ls -l  $DIR/$tfile $DIR/${tfile}2
3153 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
3154 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
3155         sleep 2
3156         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
3157         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
3158                 echo "mtime"
3159                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
3160                 echo "atime"
3161                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
3162                 echo "ctime"
3163                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
3164                 error "O_TRUNC didn't change timestamps"
3165         fi
3166 }
3167 run_test 39a "mtime changed on create"
3168
3169 test_39b() {
3170         test_mkdir -c1 $DIR/$tdir
3171         cp -p /etc/passwd $DIR/$tdir/fopen
3172         cp -p /etc/passwd $DIR/$tdir/flink
3173         cp -p /etc/passwd $DIR/$tdir/funlink
3174         cp -p /etc/passwd $DIR/$tdir/frename
3175         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
3176
3177         sleep 1
3178         echo "aaaaaa" >> $DIR/$tdir/fopen
3179         echo "aaaaaa" >> $DIR/$tdir/flink
3180         echo "aaaaaa" >> $DIR/$tdir/funlink
3181         echo "aaaaaa" >> $DIR/$tdir/frename
3182
3183         local open_new=`stat -c %Y $DIR/$tdir/fopen`
3184         local link_new=`stat -c %Y $DIR/$tdir/flink`
3185         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
3186         local rename_new=`stat -c %Y $DIR/$tdir/frename`
3187
3188         cat $DIR/$tdir/fopen > /dev/null
3189         ln $DIR/$tdir/flink $DIR/$tdir/flink2
3190         rm -f $DIR/$tdir/funlink2
3191         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
3192
3193         for (( i=0; i < 2; i++ )) ; do
3194                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
3195                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
3196                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
3197                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
3198
3199                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
3200                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
3201                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
3202                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
3203
3204                 cancel_lru_locks osc
3205                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3206         done
3207 }
3208 run_test 39b "mtime change on open, link, unlink, rename  ======"
3209
3210 # this should be set to past
3211 TEST_39_MTIME=`date -d "1 year ago" +%s`
3212
3213 # bug 11063
3214 test_39c() {
3215         touch $DIR1/$tfile
3216         sleep 2
3217         local mtime0=`stat -c %Y $DIR1/$tfile`
3218
3219         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3220         local mtime1=`stat -c %Y $DIR1/$tfile`
3221         [ "$mtime1" = $TEST_39_MTIME ] || \
3222                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
3223
3224         local d1=`date +%s`
3225         echo hello >> $DIR1/$tfile
3226         local d2=`date +%s`
3227         local mtime2=`stat -c %Y $DIR1/$tfile`
3228         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
3229                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
3230
3231         mv $DIR1/$tfile $DIR1/$tfile-1
3232
3233         for (( i=0; i < 2; i++ )) ; do
3234                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
3235                 [ "$mtime2" = "$mtime3" ] || \
3236                         error "mtime ($mtime2) changed (to $mtime3) on rename"
3237
3238                 cancel_lru_locks osc
3239                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3240         done
3241 }
3242 run_test 39c "mtime change on rename ==========================="
3243
3244 # bug 21114
3245 test_39d() {
3246         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3247         touch $DIR1/$tfile
3248
3249         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3250
3251         for (( i=0; i < 2; i++ )) ; do
3252                 local mtime=`stat -c %Y $DIR1/$tfile`
3253                 [ $mtime = $TEST_39_MTIME ] || \
3254                         error "mtime($mtime) is not set to $TEST_39_MTIME"
3255
3256                 cancel_lru_locks osc
3257                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3258         done
3259 }
3260 run_test 39d "create, utime, stat =============================="
3261
3262 # bug 21114
3263 test_39e() {
3264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3265         touch $DIR1/$tfile
3266         local mtime1=`stat -c %Y $DIR1/$tfile`
3267
3268         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3269
3270         for (( i=0; i < 2; i++ )) ; do
3271                 local mtime2=`stat -c %Y $DIR1/$tfile`
3272                 [ $mtime2 = $TEST_39_MTIME ] || \
3273                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3274
3275                 cancel_lru_locks osc
3276                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3277         done
3278 }
3279 run_test 39e "create, stat, utime, stat ========================"
3280
3281 # bug 21114
3282 test_39f() {
3283         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3284         touch $DIR1/$tfile
3285         mtime1=`stat -c %Y $DIR1/$tfile`
3286
3287         sleep 2
3288         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3289
3290         for (( i=0; i < 2; i++ )) ; do
3291                 local mtime2=`stat -c %Y $DIR1/$tfile`
3292                 [ $mtime2 = $TEST_39_MTIME ] || \
3293                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
3294
3295                 cancel_lru_locks osc
3296                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3297         done
3298 }
3299 run_test 39f "create, stat, sleep, utime, stat ================="
3300
3301 # bug 11063
3302 test_39g() {
3303         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3304         echo hello >> $DIR1/$tfile
3305         local mtime1=`stat -c %Y $DIR1/$tfile`
3306
3307         sleep 2
3308         chmod o+r $DIR1/$tfile
3309
3310         for (( i=0; i < 2; i++ )) ; do
3311                 local mtime2=`stat -c %Y $DIR1/$tfile`
3312                 [ "$mtime1" = "$mtime2" ] || \
3313                         error "lost mtime: $mtime2, should be $mtime1"
3314
3315                 cancel_lru_locks osc
3316                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3317         done
3318 }
3319 run_test 39g "write, chmod, stat ==============================="
3320
3321 # bug 11063
3322 test_39h() {
3323         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3324         touch $DIR1/$tfile
3325         sleep 1
3326
3327         local d1=`date`
3328         echo hello >> $DIR1/$tfile
3329         local mtime1=`stat -c %Y $DIR1/$tfile`
3330
3331         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3332         local d2=`date`
3333         if [ "$d1" != "$d2" ]; then
3334                 echo "write and touch not within one second"
3335         else
3336                 for (( i=0; i < 2; i++ )) ; do
3337                         local mtime2=`stat -c %Y $DIR1/$tfile`
3338                         [ "$mtime2" = $TEST_39_MTIME ] || \
3339                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
3340
3341                         cancel_lru_locks osc
3342                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3343                 done
3344         fi
3345 }
3346 run_test 39h "write, utime within one second, stat ============="
3347
3348 test_39i() {
3349         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3350         touch $DIR1/$tfile
3351         sleep 1
3352
3353         echo hello >> $DIR1/$tfile
3354         local mtime1=`stat -c %Y $DIR1/$tfile`
3355
3356         mv $DIR1/$tfile $DIR1/$tfile-1
3357
3358         for (( i=0; i < 2; i++ )) ; do
3359                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3360
3361                 [ "$mtime1" = "$mtime2" ] || \
3362                         error "lost mtime: $mtime2, should be $mtime1"
3363
3364                 cancel_lru_locks osc
3365                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3366         done
3367 }
3368 run_test 39i "write, rename, stat =============================="
3369
3370 test_39j() {
3371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3372         start_full_debug_logging
3373         touch $DIR1/$tfile
3374         sleep 1
3375
3376         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
3377         lctl set_param fail_loc=0x80000412
3378         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
3379                 error "multiop failed"
3380         local multipid=$!
3381         local mtime1=`stat -c %Y $DIR1/$tfile`
3382
3383         mv $DIR1/$tfile $DIR1/$tfile-1
3384
3385         kill -USR1 $multipid
3386         wait $multipid || error "multiop close failed"
3387
3388         for (( i=0; i < 2; i++ )) ; do
3389                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
3390                 [ "$mtime1" = "$mtime2" ] ||
3391                         error "mtime is lost on close: $mtime2, " \
3392                               "should be $mtime1"
3393
3394                 cancel_lru_locks osc
3395                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3396         done
3397         lctl set_param fail_loc=0
3398         stop_full_debug_logging
3399 }
3400 run_test 39j "write, rename, close, stat ======================="
3401
3402 test_39k() {
3403         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3404         touch $DIR1/$tfile
3405         sleep 1
3406
3407         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
3408         local multipid=$!
3409         local mtime1=`stat -c %Y $DIR1/$tfile`
3410
3411         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
3412
3413         kill -USR1 $multipid
3414         wait $multipid || error "multiop close failed"
3415
3416         for (( i=0; i < 2; i++ )) ; do
3417                 local mtime2=`stat -c %Y $DIR1/$tfile`
3418
3419                 [ "$mtime2" = $TEST_39_MTIME ] || \
3420                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
3421
3422                 cancel_lru_locks osc
3423                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3424         done
3425 }
3426 run_test 39k "write, utime, close, stat ========================"
3427
3428 # this should be set to future
3429 TEST_39_ATIME=`date -d "1 year" +%s`
3430
3431 test_39l() {
3432         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3433         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3434         local atime_diff=$(do_facet $SINGLEMDS \
3435                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3436         rm -rf $DIR/$tdir
3437         mkdir -p $DIR/$tdir
3438
3439         # test setting directory atime to future
3440         touch -a -d @$TEST_39_ATIME $DIR/$tdir
3441         local atime=$(stat -c %X $DIR/$tdir)
3442         [ "$atime" = $TEST_39_ATIME ] ||
3443                 error "atime is not set to future: $atime, $TEST_39_ATIME"
3444
3445         # test setting directory atime from future to now
3446         local now=$(date +%s)
3447         touch -a -d @$now $DIR/$tdir
3448
3449         atime=$(stat -c %X $DIR/$tdir)
3450         [ "$atime" -eq "$now"  ] ||
3451                 error "atime is not updated from future: $atime, $now"
3452
3453         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
3454         sleep 3
3455
3456         # test setting directory atime when now > dir atime + atime_diff
3457         local d1=$(date +%s)
3458         ls $DIR/$tdir
3459         local d2=$(date +%s)
3460         cancel_lru_locks mdc
3461         atime=$(stat -c %X $DIR/$tdir)
3462         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3463                 error "atime is not updated  : $atime, should be $d2"
3464
3465         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
3466         sleep 3
3467
3468         # test not setting directory atime when now < dir atime + atime_diff
3469         ls $DIR/$tdir
3470         cancel_lru_locks mdc
3471         atime=$(stat -c %X $DIR/$tdir)
3472         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
3473                 error "atime is updated to $atime, should remain $d1<atime<$d2"
3474
3475         do_facet $SINGLEMDS \
3476                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3477 }
3478 run_test 39l "directory atime update ==========================="
3479
3480 test_39m() {
3481         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3482         touch $DIR1/$tfile
3483         sleep 2
3484         local far_past_mtime=$(date -d "May 29 1953" +%s)
3485         local far_past_atime=$(date -d "Dec 17 1903" +%s)
3486
3487         touch -m -d @$far_past_mtime $DIR1/$tfile
3488         touch -a -d @$far_past_atime $DIR1/$tfile
3489
3490         for (( i=0; i < 2; i++ )) ; do
3491                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
3492                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
3493                         error "atime or mtime set incorrectly"
3494
3495                 cancel_lru_locks osc
3496                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
3497         done
3498 }
3499 run_test 39m "test atime and mtime before 1970"
3500
3501 test_39n() { # LU-3832
3502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
3503         local atime_diff=$(do_facet $SINGLEMDS \
3504                 lctl get_param -n mdd.*MDT0000*.atime_diff)
3505         local atime0
3506         local atime1
3507         local atime2
3508
3509         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
3510
3511         rm -rf $DIR/$tfile
3512         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
3513         atime0=$(stat -c %X $DIR/$tfile)
3514
3515         sleep 5
3516         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3517         atime1=$(stat -c %X $DIR/$tfile)
3518
3519         sleep 5
3520         cancel_lru_locks mdc
3521         cancel_lru_locks osc
3522         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
3523         atime2=$(stat -c %X $DIR/$tfile)
3524
3525         do_facet $SINGLEMDS \
3526                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
3527
3528         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
3529         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
3530 }
3531 run_test 39n "check that O_NOATIME is honored"
3532
3533 test_39o() {
3534         TESTDIR=$DIR/$tdir/$tfile
3535         [ -e $TESTDIR ] && rm -rf $TESTDIR
3536         mkdir -p $TESTDIR
3537         cd $TESTDIR
3538         links1=2
3539         ls
3540         mkdir a b
3541         ls
3542         links2=$(stat -c %h .)
3543         [ $(($links1 + 2)) != $links2 ] &&
3544                 error "wrong links count $(($links1 + 2)) != $links2"
3545         rmdir b
3546         links3=$(stat -c %h .)
3547         [ $(($links1 + 1)) != $links3 ] &&
3548                 error "wrong links count $links1 != $links3"
3549         return 0
3550 }
3551 run_test 39o "directory cached attributes updated after create"
3552
3553 test_39p() {
3554         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
3555         local MDTIDX=1
3556         TESTDIR=$DIR/$tdir/$tdir
3557         [ -e $TESTDIR ] && rm -rf $TESTDIR
3558         test_mkdir -p $TESTDIR
3559         cd $TESTDIR
3560         links1=2
3561         ls
3562         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
3563         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
3564         ls
3565         links2=$(stat -c %h .)
3566         [ $(($links1 + 2)) != $links2 ] &&
3567                 error "wrong links count $(($links1 + 2)) != $links2"
3568         rmdir remote_dir2
3569         links3=$(stat -c %h .)
3570         [ $(($links1 + 1)) != $links3 ] &&
3571                 error "wrong links count $links1 != $links3"
3572         return 0
3573 }
3574 run_test 39p "remote directory cached attributes updated after create ========"
3575
3576
3577 test_39q() { # LU-8041
3578         local testdir=$DIR/$tdir
3579         mkdir -p $testdir
3580         multiop_bg_pause $testdir D_c || error "multiop failed"
3581         local multipid=$!
3582         cancel_lru_locks mdc
3583         kill -USR1 $multipid
3584         local atime=$(stat -c %X $testdir)
3585         [ "$atime" -ne 0 ] || error "atime is zero"
3586 }
3587 run_test 39q "close won't zero out atime"
3588
3589 test_40() {
3590         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
3591         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
3592                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
3593         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
3594                 error "$tfile is not 4096 bytes in size"
3595 }
3596 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
3597
3598 test_41() {
3599         # bug 1553
3600         small_write $DIR/f41 18
3601 }
3602 run_test 41 "test small file write + fstat ====================="
3603
3604 count_ost_writes() {
3605         lctl get_param -n ${OSC}.*.stats |
3606                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
3607                         END { printf("%0.0f", writes) }'
3608 }
3609
3610 # decent default
3611 WRITEBACK_SAVE=500
3612 DIRTY_RATIO_SAVE=40
3613 MAX_DIRTY_RATIO=50
3614 BG_DIRTY_RATIO_SAVE=10
3615 MAX_BG_DIRTY_RATIO=25
3616
3617 start_writeback() {
3618         trap 0
3619         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
3620         # dirty_ratio, dirty_background_ratio
3621         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3622                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
3623                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
3624                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
3625         else
3626                 # if file not here, we are a 2.4 kernel
3627                 kill -CONT `pidof kupdated`
3628         fi
3629 }
3630
3631 stop_writeback() {
3632         # setup the trap first, so someone cannot exit the test at the
3633         # exact wrong time and mess up a machine
3634         trap start_writeback EXIT
3635         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
3636         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
3637                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
3638                 sysctl -w vm.dirty_writeback_centisecs=0
3639                 sysctl -w vm.dirty_writeback_centisecs=0
3640                 # save and increase /proc/sys/vm/dirty_ratio
3641                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
3642                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
3643                 # save and increase /proc/sys/vm/dirty_background_ratio
3644                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
3645                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
3646         else
3647                 # if file not here, we are a 2.4 kernel
3648                 kill -STOP `pidof kupdated`
3649         fi
3650 }
3651
3652 # ensure that all stripes have some grant before we test client-side cache
3653 setup_test42() {
3654         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
3655                 dd if=/dev/zero of=$i bs=4k count=1
3656                 rm $i
3657         done
3658 }
3659
3660 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
3661 # file truncation, and file removal.
3662 test_42a() {
3663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3664         setup_test42
3665         cancel_lru_locks $OSC
3666         stop_writeback
3667         sync; sleep 1; sync # just to be safe
3668         BEFOREWRITES=`count_ost_writes`
3669         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
3670         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
3671         AFTERWRITES=`count_ost_writes`
3672         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3673                 error "$BEFOREWRITES < $AFTERWRITES"
3674         start_writeback
3675 }
3676 run_test 42a "ensure that we don't flush on close"
3677
3678 test_42b() {
3679         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3680         setup_test42
3681         cancel_lru_locks $OSC
3682         stop_writeback
3683         sync
3684         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
3685         BEFOREWRITES=$(count_ost_writes)
3686         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
3687         AFTERWRITES=$(count_ost_writes)
3688         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3689                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
3690         fi
3691         BEFOREWRITES=$(count_ost_writes)
3692         sync || error "sync: $?"
3693         AFTERWRITES=$(count_ost_writes)
3694         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
3695                 error "$BEFOREWRITES < $AFTERWRITES on sync"
3696         fi
3697         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
3698         start_writeback
3699         return 0
3700 }
3701 run_test 42b "test destroy of file with cached dirty data ======"
3702
3703 # if these tests just want to test the effect of truncation,
3704 # they have to be very careful.  consider:
3705 # - the first open gets a {0,EOF}PR lock
3706 # - the first write conflicts and gets a {0, count-1}PW
3707 # - the rest of the writes are under {count,EOF}PW
3708 # - the open for truncate tries to match a {0,EOF}PR
3709 #   for the filesize and cancels the PWs.
3710 # any number of fixes (don't get {0,EOF} on open, match
3711 # composite locks, do smarter file size management) fix
3712 # this, but for now we want these tests to verify that
3713 # the cancellation with truncate intent works, so we
3714 # start the file with a full-file pw lock to match against
3715 # until the truncate.
3716 trunc_test() {
3717         test=$1
3718         file=$DIR/$test
3719         offset=$2
3720         cancel_lru_locks $OSC
3721         stop_writeback
3722         # prime the file with 0,EOF PW to match
3723         touch $file
3724         $TRUNCATE $file 0
3725         sync; sync
3726         # now the real test..
3727         dd if=/dev/zero of=$file bs=1024 count=100
3728         BEFOREWRITES=`count_ost_writes`
3729         $TRUNCATE $file $offset
3730         cancel_lru_locks $OSC
3731         AFTERWRITES=`count_ost_writes`
3732         start_writeback
3733 }
3734
3735 test_42c() {
3736         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3737         trunc_test 42c 1024
3738         [ $BEFOREWRITES -eq $AFTERWRITES ] && \
3739             error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
3740         rm $file
3741 }
3742 run_test 42c "test partial truncate of file with cached dirty data"
3743
3744 test_42d() {
3745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3746         trunc_test 42d 0
3747         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
3748             error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
3749         rm $file
3750 }
3751 run_test 42d "test complete truncate of file with cached dirty data"
3752
3753 test_42e() { # bug22074
3754         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3755         local TDIR=$DIR/${tdir}e
3756         local pagesz=$(page_size)
3757         local pages=16 # hardcoded 16 pages, don't change it.
3758         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
3759         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
3760         local max_dirty_mb
3761         local warmup_files
3762
3763         test_mkdir $DIR/${tdir}e
3764         $SETSTRIPE -c 1 $TDIR
3765         createmany -o $TDIR/f $files
3766
3767         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
3768
3769         # we assume that with $OSTCOUNT files, at least one of them will
3770         # be allocated on OST0.
3771         warmup_files=$((OSTCOUNT * max_dirty_mb))
3772         createmany -o $TDIR/w $warmup_files
3773
3774         # write a large amount of data into one file and sync, to get good
3775         # avail_grant number from OST.
3776         for ((i=0; i<$warmup_files; i++)); do
3777                 idx=$($GETSTRIPE -i $TDIR/w$i)
3778                 [ $idx -ne 0 ] && continue
3779                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
3780                 break
3781         done
3782         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
3783         sync
3784         $LCTL get_param $proc_osc0/cur_dirty_bytes
3785         $LCTL get_param $proc_osc0/cur_grant_bytes
3786
3787         # create as much dirty pages as we can while not to trigger the actual
3788         # RPCs directly. but depends on the env, VFS may trigger flush during this
3789         # period, hopefully we are good.
3790         for ((i=0; i<$warmup_files; i++)); do
3791                 idx=$($GETSTRIPE -i $TDIR/w$i)
3792                 [ $idx -ne 0 ] && continue
3793                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
3794         done
3795         $LCTL get_param $proc_osc0/cur_dirty_bytes
3796         $LCTL get_param $proc_osc0/cur_grant_bytes
3797
3798         # perform the real test
3799         $LCTL set_param $proc_osc0/rpc_stats 0
3800         for ((;i<$files; i++)); do
3801                 [ $($GETSTRIPE -i $TDIR/f$i) -eq 0 ] || continue
3802                 dd if=/dev/zero of=$TDIR/f$i bs=$pagesz count=$pages 2>/dev/null
3803         done
3804         sync
3805         $LCTL get_param $proc_osc0/rpc_stats
3806
3807         local percent=0
3808         local have_ppr=false
3809         $LCTL get_param $proc_osc0/rpc_stats |
3810                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
3811                         # skip lines until we are at the RPC histogram data
3812                         [ "$PPR" == "pages" ] && have_ppr=true && continue
3813                         $have_ppr || continue
3814
3815                         # we only want the percent stat for < 16 pages
3816                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
3817
3818                         percent=$((percent + WPCT))
3819                         if [[ $percent -gt 15 ]]; then
3820                                 error "less than 16-pages write RPCs" \
3821                                       "$percent% > 15%"
3822                                 break
3823                         fi
3824                 done
3825         rm -rf $TDIR
3826 }
3827 run_test 42e "verify sub-RPC writes are not done synchronously"
3828
3829 test_43A() { # was test_43
3830         test_mkdir $DIR/$tdir
3831         cp -p /bin/ls $DIR/$tdir/$tfile
3832         $MULTIOP $DIR/$tdir/$tfile Ow_c &
3833         pid=$!
3834         # give multiop a chance to open
3835         sleep 1
3836
3837         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
3838         kill -USR1 $pid
3839 }
3840 run_test 43A "execution of file opened for write should return -ETXTBSY"
3841
3842 test_43a() {
3843         test_mkdir $DIR/$tdir
3844         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3845                 cp -p multiop $DIR/$tdir/multiop
3846         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3847                 error "multiop open $TMP/$tfile.junk failed"
3848         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
3849         MULTIOP_PID=$!
3850         $MULTIOP $DIR/$tdir/multiop Oc && error "expected error, got success"
3851         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3852         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3853 }
3854 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
3855
3856 test_43b() {
3857         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3858         test_mkdir $DIR/$tdir
3859         cp -p $(which $MULTIOP) $DIR/$tdir/multiop ||
3860                 cp -p multiop $DIR/$tdir/multiop
3861         MULTIOP_PROG=$DIR/$tdir/multiop multiop_bg_pause $TMP/$tfile.junk O_c ||
3862                 error "multiop open $TMP/$tfile.junk failed"
3863         rm $TMP/$tfile.junk     # delete junk file on close (not part of test)
3864         MULTIOP_PID=$!
3865         $TRUNCATE $DIR/$tdir/multiop 0 && error "expected error, got success"
3866         kill -USR1 $MULTIOP_PID || error "kill -USR1 PID $MULTIOP_PID failed"
3867         wait $MULTIOP_PID || error "wait PID $MULTIOP_PID failed"
3868 }
3869 run_test 43b "truncate of file being executed should return -ETXTBSY"
3870
3871 test_43c() {
3872         local testdir="$DIR/$tdir"
3873         test_mkdir $testdir
3874         cp $SHELL $testdir/
3875         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
3876                 ( cd $testdir && md5sum -c )
3877 }
3878 run_test 43c "md5sum of copy into lustre"
3879
3880 test_44A() { # was test_44
3881         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
3882         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
3883         dd if=$DIR/f1 bs=4k count=1 > /dev/null
3884 }
3885 run_test 44A "zero length read from a sparse stripe"
3886
3887 test_44a() {
3888         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
3889                 awk '{ print $2 }')
3890         [ -z "$nstripe" ] && skip "can't get stripe info" && return
3891         [[ $nstripe -gt $OSTCOUNT ]] &&
3892             skip "Wrong default_stripe_count: $nstripe (OSTCOUNT: $OSTCOUNT)" &&
3893             return
3894         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
3895                 awk '{ print $2 }')
3896         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
3897                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
3898                         awk '{ print $2 }')
3899         fi
3900
3901         OFFSETS="0 $((stride/2)) $((stride-1))"
3902         for offset in $OFFSETS; do
3903                 for i in $(seq 0 $((nstripe-1))); do
3904                         local GLOBALOFFSETS=""
3905                         # size in Bytes
3906                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
3907                         local myfn=$DIR/d44a-$size
3908                         echo "--------writing $myfn at $size"
3909                         ll_sparseness_write $myfn $size ||
3910                                 error "ll_sparseness_write"
3911                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
3912                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3913                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3914
3915                         for j in $(seq 0 $((nstripe-1))); do
3916                                 # size in Bytes
3917                                 size=$((((j + $nstripe )*$stride + $offset)))
3918                                 ll_sparseness_write $myfn $size ||
3919                                         error "ll_sparseness_write"
3920                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
3921                         done
3922                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
3923                                 error "ll_sparseness_verify $GLOBALOFFSETS"
3924                         rm -f $myfn
3925                 done
3926         done
3927 }
3928 run_test 44a "test sparse pwrite ==============================="
3929
3930 dirty_osc_total() {
3931         tot=0
3932         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
3933                 tot=$(($tot + $d))
3934         done
3935         echo $tot
3936 }
3937 do_dirty_record() {
3938         before=`dirty_osc_total`
3939         echo executing "\"$*\""
3940         eval $*
3941         after=`dirty_osc_total`
3942         echo before $before, after $after
3943 }
3944 test_45() {
3945         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3946         f="$DIR/f45"
3947         # Obtain grants from OST if it supports it
3948         echo blah > ${f}_grant
3949         stop_writeback
3950         sync
3951         do_dirty_record "echo blah > $f"
3952         [[ $before -eq $after ]] && error "write wasn't cached"
3953         do_dirty_record "> $f"
3954         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
3955         do_dirty_record "echo blah > $f"
3956         [[ $before -eq $after ]] && error "write wasn't cached"
3957         do_dirty_record "sync"
3958         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
3959         do_dirty_record "echo blah > $f"
3960         [[ $before -eq $after ]] && error "write wasn't cached"
3961         do_dirty_record "cancel_lru_locks osc"
3962         [[ $before -gt $after ]] ||
3963                 error "lock cancellation didn't lower dirty count"
3964         start_writeback
3965 }
3966 run_test 45 "osc io page accounting ============================"
3967
3968 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
3969 # test tickles a bug where re-dirtying a page was failing to be mapped to the
3970 # objects offset and an assert hit when an rpc was built with 1023's mapped
3971 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
3972 test_46() {
3973         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
3974         f="$DIR/f46"
3975         stop_writeback
3976         sync
3977         dd if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3978         sync
3979         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=1023 count=1
3980         dd conv=notrunc if=/dev/zero of=$f bs=`page_size` seek=511 count=1
3981         sync
3982         start_writeback
3983 }
3984 run_test 46 "dirtying a previously written page ================"
3985
3986 # test_47 is removed "Device nodes check" is moved to test_28
3987
3988 test_48a() { # bug 2399
3989         [ $(facet_fstype $SINGLEMDS) = "zfs" ] &&
3990         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.63) ] &&
3991                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly" &&
3992                 return
3993         test_mkdir $DIR/$tdir
3994         cd $DIR/$tdir
3995         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
3996         test_mkdir $DIR/$tdir
3997         touch foo || error "'touch foo' failed after recreating cwd"
3998         test_mkdir bar
3999         touch .foo || error "'touch .foo' failed after recreating cwd"
4000         test_mkdir .bar
4001         ls . > /dev/null || error "'ls .' failed after recreating cwd"
4002         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4003         cd . || error "'cd .' failed after recreating cwd"
4004         mkdir . && error "'mkdir .' worked after recreating cwd"
4005         rmdir . && error "'rmdir .' worked after recreating cwd"
4006         ln -s . baz || error "'ln -s .' failed after recreating cwd"
4007         cd .. || error "'cd ..' failed after recreating cwd"
4008 }
4009 run_test 48a "Access renamed working dir (should return errors)="
4010
4011 test_48b() { # bug 2399
4012         rm -rf $DIR/$tdir
4013         test_mkdir $DIR/$tdir
4014         cd $DIR/$tdir
4015         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
4016         touch foo && error "'touch foo' worked after removing cwd"
4017         mkdir foo && error "'mkdir foo' worked after removing cwd"
4018         touch .foo && error "'touch .foo' worked after removing cwd"
4019         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
4020         ls . > /dev/null && error "'ls .' worked after removing cwd"
4021         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
4022         mkdir . && error "'mkdir .' worked after removing cwd"
4023         rmdir . && error "'rmdir .' worked after removing cwd"
4024         ln -s . foo && error "'ln -s .' worked after removing cwd"
4025         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
4026 }
4027 run_test 48b "Access removed working dir (should return errors)="
4028
4029 test_48c() { # bug 2350
4030         #lctl set_param debug=-1
4031         #set -vx
4032         rm -rf $DIR/$tdir
4033         test_mkdir -p $DIR/$tdir/dir
4034         cd $DIR/$tdir/dir
4035         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4036         $TRACE touch foo && error "touch foo worked after removing cwd"
4037         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
4038         touch .foo && error "touch .foo worked after removing cwd"
4039         mkdir .foo && error "mkdir .foo worked after removing cwd"
4040         $TRACE ls . && error "'ls .' worked after removing cwd"
4041         $TRACE ls .. || error "'ls ..' failed after removing cwd"
4042         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
4043         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
4044         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
4045         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
4046 }
4047 run_test 48c "Access removed working subdir (should return errors)"
4048
4049 test_48d() { # bug 2350
4050         #lctl set_param debug=-1
4051         #set -vx
4052         rm -rf $DIR/$tdir
4053         test_mkdir -p $DIR/$tdir/dir
4054         cd $DIR/$tdir/dir
4055         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4056         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4057         $TRACE touch foo && error "'touch foo' worked after removing parent"
4058         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
4059         touch .foo && error "'touch .foo' worked after removing parent"
4060         mkdir .foo && error "mkdir .foo worked after removing parent"
4061         $TRACE ls . && error "'ls .' worked after removing parent"
4062         $TRACE ls .. && error "'ls ..' worked after removing parent"
4063         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
4064         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
4065         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
4066         true
4067 }
4068 run_test 48d "Access removed parent subdir (should return errors)"
4069
4070 test_48e() { # bug 4134
4071         #lctl set_param debug=-1
4072         #set -vx
4073         rm -rf $DIR/$tdir
4074         test_mkdir -p $DIR/$tdir/dir
4075         cd $DIR/$tdir/dir
4076         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
4077         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
4078         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
4079         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
4080         # On a buggy kernel addition of "touch foo" after cd .. will
4081         # produce kernel oops in lookup_hash_it
4082         touch ../foo && error "'cd ..' worked after recreate parent"
4083         cd $DIR
4084         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
4085 }
4086 run_test 48e "Access to recreated parent subdir (should return errors)"
4087
4088 test_49() { # LU-1030
4089         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4090         remote_ost_nodsh && skip "remote OST with nodsh" && return
4091         # get ost1 size - lustre-OST0000
4092         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
4093                 awk '{ print $4 }')
4094         # write 800M at maximum
4095         [[ $ost1_size -lt 2 ]] && ost1_size=2
4096         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
4097
4098         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
4099         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
4100         local dd_pid=$!
4101
4102         # change max_pages_per_rpc while writing the file
4103         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
4104         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
4105         # loop until dd process exits
4106         while ps ax -opid | grep -wq $dd_pid; do
4107                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
4108                 sleep $((RANDOM % 5 + 1))
4109         done
4110         # restore original max_pages_per_rpc
4111         $LCTL set_param $osc1_mppc=$orig_mppc
4112         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
4113 }
4114 run_test 49 "Change max_pages_per_rpc won't break osc extent"
4115
4116 test_50() {
4117         # bug 1485
4118         test_mkdir $DIR/$tdir
4119         cd $DIR/$tdir
4120         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
4121 }
4122 run_test 50 "special situations: /proc symlinks  ==============="
4123
4124 test_51a() {    # was test_51
4125         # bug 1516 - create an empty entry right after ".." then split dir
4126         test_mkdir -c1 $DIR/$tdir
4127         touch $DIR/$tdir/foo
4128         $MCREATE $DIR/$tdir/bar
4129         rm $DIR/$tdir/foo
4130         createmany -m $DIR/$tdir/longfile 201
4131         FNUM=202
4132         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
4133                 $MCREATE $DIR/$tdir/longfile$FNUM
4134                 FNUM=$(($FNUM + 1))
4135                 echo -n "+"
4136         done
4137         echo
4138         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
4139 }
4140 run_test 51a "special situations: split htree with empty entry =="
4141
4142 cleanup_print_lfs_df () {
4143         trap 0
4144         $LFS df
4145         $LFS df -i
4146 }
4147
4148 test_51b() {
4149         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4150         local dir=$DIR/$tdir
4151
4152         local nrdirs=$((65536 + 100))
4153
4154         # cleanup the directory
4155         rm -fr $dir
4156
4157         test_mkdir -c1 $dir
4158
4159         $LFS df
4160         $LFS df -i
4161         local mdtidx=$(printf "%04x" $($LFS getstripe -M $dir))
4162         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
4163         [[ $numfree -lt $nrdirs ]] &&
4164                 skip "not enough free inodes ($numfree) on MDT$mdtidx" &&
4165                 return
4166
4167         # need to check free space for the directories as well
4168         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
4169         numfree=$(( blkfree / $(fs_inode_ksize) ))
4170         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)" &&
4171                 return
4172
4173         trap cleanup_print_lfs_df EXIT
4174
4175         # create files
4176         createmany -d $dir/d $nrdirs || {
4177                 unlinkmany $dir/d $nrdirs
4178                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
4179         }
4180
4181         # really created :
4182         nrdirs=$(ls -U $dir | wc -l)
4183
4184         # unlink all but 100 subdirectories, then check it still works
4185         local left=100
4186         local delete=$((nrdirs - left))
4187
4188         $LFS df
4189         $LFS df -i
4190
4191         # for ldiskfs the nlink count should be 1, but this is OSD specific
4192         # and so this is listed for informational purposes only
4193         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
4194         unlinkmany -d $dir/d $delete ||
4195                 error "unlink of first $delete subdirs failed"
4196
4197         echo "nlink between: $(stat -c %h $dir)"
4198         local found=$(ls -U $dir | wc -l)
4199         [ $found -ne $left ] &&
4200                 error "can't find subdirs: found only $found, expected $left"
4201
4202         unlinkmany -d $dir/d $delete $left ||
4203                 error "unlink of second $left subdirs failed"
4204         # regardless of whether the backing filesystem tracks nlink accurately
4205         # or not, the nlink count shouldn't be more than "." and ".." here
4206         local after=$(stat -c %h $dir)
4207         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
4208                 echo "nlink after: $after"
4209
4210         cleanup_print_lfs_df
4211 }
4212 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
4213
4214 test_51d() {
4215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4216         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
4217         test_mkdir $DIR/$tdir
4218         createmany -o $DIR/$tdir/t- 1000
4219         $LFS getstripe $DIR/$tdir > $TMP/$tfile
4220         for N in $(seq 0 $((OSTCOUNT - 1))); do
4221                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
4222                         END { printf("%0.0f", objs) }' $TMP/$tfile)
4223                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
4224                         '($1 == '$N') { objs += 1 } \
4225                         END { printf("%0.0f", objs) }')
4226                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
4227         done
4228         unlinkmany $DIR/$tdir/t- 1000
4229
4230         NLAST=0
4231         for N in $(seq 1 $((OSTCOUNT - 1))); do
4232                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
4233                         error "OST $N has less objects vs OST $NLAST" \
4234                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4235                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
4236                         error "OST $N has less objects vs OST $NLAST" \
4237                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
4238
4239                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
4240                         error "OST $N has less #0 objects vs OST $NLAST" \
4241                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4242                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
4243                         error "OST $N has less #0 objects vs OST $NLAST" \
4244                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
4245                 NLAST=$N
4246         done
4247         rm -f $TMP/$tfile
4248 }
4249 run_test 51d "check object distribution"
4250
4251 test_51e() {
4252         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
4253                 skip "ldiskfs only test"
4254                 return
4255         fi
4256
4257         test_mkdir -c1 $DIR/$tdir
4258         test_mkdir -c1 $DIR/$tdir/d0
4259
4260         touch $DIR/$tdir/d0/foo
4261         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
4262                 error "file exceed 65000 nlink limit!"
4263         unlinkmany $DIR/$tdir/d0/f- 65001
4264         return 0
4265 }
4266 run_test 51e "check file nlink limit"
4267
4268 test_51f() {
4269         test_mkdir $DIR/$tdir
4270
4271         local max=100000
4272         local ulimit_old=$(ulimit -n)
4273         local spare=20 # number of spare fd's for scripts/libraries, etc.
4274         local mdt=$(lfs getstripe -M $DIR/$tdir)
4275         local numfree=$(lfs df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
4276
4277         echo "MDT$mdt numfree=$numfree, max=$max"
4278         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
4279         if [ $((numfree + spare)) -gt $ulimit_old ]; then
4280                 while ! ulimit -n $((numfree + spare)); do
4281                         numfree=$((numfree * 3 / 4))
4282                 done
4283                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
4284         else
4285                 echo "left ulimit at $ulimit_old"
4286         fi
4287
4288         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
4289                 unlinkmany $DIR/$tdir/f $numfree
4290                 error "create+open $numfree files in $DIR/$tdir failed"
4291         }
4292         ulimit -n $ulimit_old
4293
4294         # if createmany exits at 120s there will be fewer than $numfree files
4295         unlinkmany $DIR/$tdir/f $numfree || true
4296 }
4297 run_test 51f "check many open files limit"
4298
4299 test_52a() {
4300         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
4301         test_mkdir $DIR/$tdir
4302         touch $DIR/$tdir/foo
4303         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
4304         echo bar >> $DIR/$tdir/foo || error "append bar failed"
4305         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4306         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4307         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4308                                         error "link worked"
4309         echo foo >> $DIR/$tdir/foo || error "append foo failed"
4310         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4311         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
4312                                                      error "lsattr"
4313         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
4314         cp -r $DIR/$tdir $TMP/
4315         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
4316 }
4317 run_test 52a "append-only flag test (should return errors)"
4318
4319 test_52b() {
4320         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
4321         test_mkdir $DIR/$tdir
4322         touch $DIR/$tdir/foo
4323         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
4324         cat test > $DIR/$tdir/foo && error "cat test worked"
4325         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
4326         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
4327         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
4328                                         error "link worked"
4329         echo foo >> $DIR/$tdir/foo && error "echo worked"
4330         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
4331         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
4332         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
4333         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
4334                                                         error "lsattr"
4335         chattr -i $DIR/$tdir/foo || error "chattr failed"
4336
4337         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
4338 }
4339 run_test 52b "immutable flag test (should return errors) ======="
4340
4341 test_53() {
4342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4343         remote_mds_nodsh && skip "remote MDS with nodsh" && return
4344         remote_ost_nodsh && skip "remote OST with nodsh" && return
4345
4346         local param
4347         local param_seq
4348         local ostname
4349         local mds_last
4350         local mds_last_seq
4351         local ost_last
4352         local ost_last_seq
4353         local ost_last_id
4354         local ostnum
4355         local node
4356         local found=false
4357         local support_last_seq=true
4358
4359         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
4360                 support_last_seq=false
4361
4362         # only test MDT0000
4363         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
4364         local value
4365         for value in $(do_facet $SINGLEMDS \
4366                        $LCTL get_param osc.$mdtosc.prealloc_last_id) ; do
4367                 param=$(echo ${value[0]} | cut -d "=" -f1)
4368                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
4369
4370                 if $support_last_seq; then
4371                         param_seq=$(echo $param |
4372                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
4373                         mds_last_seq=$(do_facet $SINGLEMDS \
4374                                        $LCTL get_param -n $param_seq)
4375                 fi
4376                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
4377
4378                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
4379                 node=$(facet_active_host ost$((ostnum+1)))
4380                 param="obdfilter.$ostname.last_id"
4381                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
4382                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
4383                         ost_last_id=$ost_last
4384
4385                         if $support_last_seq; then
4386                                 ost_last_id=$(echo $ost_last |
4387                                               awk -F':' '{print $2}' |
4388                                               sed -e "s/^0x//g")
4389                                 ost_last_seq=$(echo $ost_last |
4390                                                awk -F':' '{print $1}')
4391                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
4392                         fi
4393
4394                         if [[ $ost_last_id != $mds_last ]]; then
4395                                 error "$ost_last_id != $mds_last"
4396                         else
4397                                 found=true
4398                                 break
4399                         fi
4400                 done
4401         done
4402         $found || error "can not match last_seq/last_id for $mdtosc"
4403         return 0
4404 }
4405 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
4406
4407 test_54a() {
4408         perl -MSocket -e ';' || { skip "no Socket perl module installed" && return; }
4409
4410         $SOCKETSERVER $DIR/socket ||
4411                 error "$SOCKETSERVER $DIR/socket failed: $?"
4412         $SOCKETCLIENT $DIR/socket ||
4413                 error "$SOCKETCLIENT $DIR/socket failed: $?"
4414         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
4415 }
4416 run_test 54a "unix domain socket test =========================="
4417
4418 test_54b() {
4419         f="$DIR/f54b"
4420         mknod $f c 1 3
4421         chmod 0666 $f
4422         dd if=/dev/zero of=$f bs=$(page_size) count=1
4423 }
4424 run_test 54b "char device works in lustre ======================"
4425
4426 find_loop_dev() {
4427         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
4428         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
4429         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
4430
4431         for i in $(seq 3 7); do
4432                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
4433                 LOOPDEV=$LOOPBASE$i
4434                 LOOPNUM=$i
4435                 break
4436         done
4437 }
4438
4439 cleanup_54c() {
4440         local rc=0
4441         loopdev="$DIR/loop54c"
4442
4443         trap 0
4444         $UMOUNT $DIR/$tdir || rc=$?
4445         losetup -d $loopdev || true
4446         losetup -d $LOOPDEV || true
4447         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
4448         return $rc
4449 }
4450
4451 test_54c() {
4452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
4453         loopdev="$DIR/loop54c"
4454
4455         find_loop_dev
4456         [ -z "$LOOPNUM" ] && echo "couldn't find empty loop device" && return
4457         trap cleanup_54c EXIT
4458         mknod $loopdev b 7 $LOOPNUM
4459         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
4460         dd if=/dev/zero of=$DIR/$tfile bs=$(get_page_size client) seek=1024 count=1 > /dev/null
4461         losetup $loopdev $DIR/$tfile ||
4462                 error "can't set up $loopdev for $DIR/$tfile"
4463         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
4464         test_mkdir $DIR/$tdir
4465         mount -t ext2 $loopdev $DIR/$tdir ||
4466                 error "error mounting $loopdev on $DIR/$tdir"
4467         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$(get_page_size client) count=30 ||
4468                 error "dd write"
4469         df $DIR/$tdir
4470         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$(get_page_size client) count=30 ||
4471                 error "dd read"
4472         cleanup_54c
4473 }
4474 run_test 54c "block device works in lustre ====================="
4475
4476 test_54d() {
4477         f="$DIR/f54d"
4478         string="aaaaaa"
4479         mknod $f p
4480         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
4481 }
4482 run_test 54d "fifo device works in lustre ======================"
4483
4484 test_54e() {
4485         f="$DIR/f54e"
4486         string="aaaaaa"
4487         cp -aL /dev/console $f
4488         echo $string > $f || error "echo $string to $f failed"
4489 }
4490 run_test 54e "console/tty device works in lustre ======================"
4491
4492 test_56a() {
4493         local numfiles=3
4494         local dir=$DIR/$tdir
4495
4496         rm -rf $dir
4497         test_mkdir -p $dir/dir
4498         for i in $(seq $numfiles); do
4499                 touch $dir/file$i
4500                 touch $dir/dir/file$i
4501         done
4502
4503         local numcomp=$($LFS getstripe --component-count $dir)
4504
4505         [[ $numcomp == 0 ]] && numcomp=1
4506
4507         # test lfs getstripe with --recursive
4508         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
4509
4510         [[ $filenum -eq $((numfiles * 2)) ]] ||
4511                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
4512         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
4513         [[ $filenum -eq $numfiles ]] ||
4514                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
4515         echo "$LFS getstripe showed obdidx or l_ost_idx"
4516
4517         # test lfs getstripe with file instead of dir
4518         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
4519         [[ $filenum -eq 1 ]] ||
4520                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
4521         echo "$LFS getstripe file1 passed"
4522
4523         #test lfs getstripe with --verbose
4524         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
4525         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4526                 error "$LFS getstripe --verbose $dir: "\
4527                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
4528         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
4529                 error "$LFS getstripe $dir: showed lmm_magic"
4530
4531         #test lfs getstripe with -v prints lmm_fid
4532         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
4533         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
4534                 error "$LFS getstripe -v $dir: "\
4535                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
4536         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
4537                 error "$LFS getstripe $dir: showed lmm_fid by default"
4538         echo "$LFS getstripe --verbose passed"
4539
4540         #check for FID information
4541         local fid1=$($LFS getstripe --fid $dir/file1)
4542         local fid2=$($LFS getstripe --verbose $dir/file1 |
4543                      awk '/lmm_fid: / { print $2; exit; }')
4544         local fid3=$($LFS path2fid $dir/file1)
4545
4546         [ "$fid1" != "$fid2" ] &&
4547                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
4548         [ "$fid1" != "$fid3" ] &&
4549                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
4550         echo "$LFS getstripe --fid passed"
4551
4552         #test lfs getstripe with --obd
4553         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
4554                 error "$LFS getstripe --obd wrong_uuid: should return error"
4555
4556         [[ $OSTCOUNT -lt 2 ]] &&
4557                 skip_env "skip '$LFS getstripe --obd' tests: $OSTCOUNT < 2" &&
4558                 return
4559
4560         local ostidx=1
4561         local obduuid=$(ostuuid_from_index $ostidx)
4562         local found=$($LFS getstripe -r --obd $obduuid $dir |
4563                       egrep -c "obdidx|l_ost_idx")
4564
4565         filenum=$($LFS getstripe -ir $dir | grep "^$ostidx\$" | wc -l)
4566         [[ $found -eq $filenum ]] ||
4567                 error "$LFS getstripe --obd: found $found expect $filenum"
4568         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
4569                 sed '/^[         ]*'${ostidx}'[  ]/d' |
4570                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
4571                 error "$LFS getstripe --obd: should not show file on other obd"
4572         echo "$LFS getstripe --obd passed"
4573 }
4574 run_test 56a "check $LFS getstripe"
4575
4576 test_56b() {
4577         local dir=$DIR/$tdir
4578         local numdirs=3
4579
4580         test_mkdir $dir
4581         for i in $(seq $numdirs); do
4582                 test_mkdir $dir/dir$i
4583         done
4584
4585         # test lfs getdirstripe default mode is non-recursion, which is
4586         # different from lfs getstripe
4587         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
4588
4589         [[ $dircnt -eq 1 ]] ||
4590                 error "$LFS getdirstripe: found $dircnt, not 1"
4591         dircnt=$($LFS getdirstripe --recursive $dir |
4592                 grep -c lmv_stripe_count)
4593         [[ $dircnt -eq $((numdirs + 1)) ]] ||
4594                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
4595 }
4596 run_test 56b "check $LFS getdirstripe"
4597
4598 test_56c() {
4599         local ost_idx=0
4600         local ost_name=$(ostname_from_index $ost_idx)
4601         local old_status=$(ost_dev_status $ost_idx)
4602
4603         [[ -z "$old_status" ]] ||
4604                 { skip_env "OST $ost_name is in $old_status status"; return 0; }
4605
4606         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
4607         sleep_maxage
4608
4609         local new_status=$(ost_dev_status $ost_idx)
4610
4611         [[ "$new_status" = "D" ]] ||
4612                 error "OST $ost_name is in status of '$new_status', not 'D'"
4613
4614         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
4615         sleep_maxage
4616
4617         new_status=$(ost_dev_status $ost_idx)
4618         [[ -z "$new_status" ]] ||
4619                 error "OST $ost_name is in status of '$new_status', not ''"
4620 }
4621 run_test 56c "check 'lfs df' showing device status"
4622
4623 NUMFILES=3
4624 NUMDIRS=3
4625 setup_56() {
4626         local local_tdir="$1"
4627         local local_numfiles="$2"
4628         local local_numdirs="$3"
4629         local dir_params="$4"
4630         local dir_stripe_params="$5"
4631
4632         if [ ! -d "$local_tdir" ] ; then
4633                 test_mkdir -p $dir_stripe_params $local_tdir
4634                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
4635                 for i in $(seq $local_numfiles) ; do
4636                         touch $local_tdir/file$i
4637                 done
4638                 for i in $(seq $local_numdirs) ; do
4639                         test_mkdir $dir_stripe_params $local_tdir/dir$i
4640                         for j in $(seq $local_numfiles) ; do
4641                                 touch $local_tdir/dir$i/file$j
4642                         done
4643                 done
4644         fi
4645 }
4646
4647 setup_56_special() {
4648         local local_tdir=$1
4649         local local_numfiles=$2
4650         local local_numdirs=$3
4651
4652         setup_56 $local_tdir $local_numfiles $local_numdirs
4653
4654         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
4655                 for i in $(seq $local_numfiles) ; do
4656                         mknod $local_tdir/loop${i}b b 7 $i
4657                         mknod $local_tdir/null${i}c c 1 3
4658                         ln -s $local_tdir/file1 $local_tdir/link${i}
4659                 done
4660                 for i in $(seq $local_numdirs) ; do
4661                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
4662                         mknod $local_tdir/dir$i/null${i}c c 1 3
4663                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
4664                 done
4665         fi
4666 }
4667
4668 test_56g() {
4669         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4670         local expected=$(($NUMDIRS + 2))
4671
4672         setup_56 $dir $NUMFILES $NUMDIRS
4673
4674         # test lfs find with -name
4675         for i in $(seq $NUMFILES) ; do
4676                 local nums=$($LFS find -name "*$i" $dir | wc -l)
4677
4678                 [ $nums -eq $expected ] ||
4679                         error "lfs find -name '*$i' $dir wrong: "\
4680                               "found $nums, expected $expected"
4681         done
4682 }
4683 run_test 56g "check lfs find -name"
4684
4685 test_56h() {
4686         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4687         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
4688
4689         setup_56 $dir $NUMFILES $NUMDIRS
4690
4691         # test lfs find with ! -name
4692         for i in $(seq $NUMFILES) ; do
4693                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
4694
4695                 [ $nums -eq $expected ] ||
4696                         error "lfs find ! -name '*$i' $dir wrong: "\
4697                               "found $nums, expected $expected"
4698         done
4699 }
4700 run_test 56h "check lfs find ! -name"
4701
4702 test_56i() {
4703         local dir=$DIR/$tdir
4704
4705         test_mkdir $dir
4706
4707         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
4708         local out=$($cmd)
4709
4710         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
4711 }
4712 run_test 56i "check 'lfs find -ost UUID' skips directories"
4713
4714 test_56j() {
4715         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4716
4717         setup_56_special $dir $NUMFILES $NUMDIRS
4718
4719         local expected=$((NUMDIRS + 1))
4720         local cmd="$LFS find -type d $dir"
4721         local nums=$($cmd | wc -l)
4722
4723         [ $nums -eq $expected ] ||
4724                 error "'$cmd' wrong: found $nums, expected $expected"
4725 }
4726 run_test 56j "check lfs find -type d"
4727
4728 test_56k() {
4729         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4730
4731         setup_56_special $dir $NUMFILES $NUMDIRS
4732
4733         local expected=$(((NUMDIRS + 1) * NUMFILES))
4734         local cmd="$LFS find -type f $dir"
4735         local nums=$($cmd | wc -l)
4736
4737         [ $nums -eq $expected ] ||
4738                 error "'$cmd' wrong: found $nums, expected $expected"
4739 }
4740 run_test 56k "check lfs find -type f"
4741
4742 test_56l() {
4743         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4744
4745         setup_56_special $dir $NUMFILES $NUMDIRS
4746
4747         local expected=$((NUMDIRS + NUMFILES))
4748         local cmd="$LFS find -type b $dir"
4749         local nums=$($cmd | wc -l)
4750
4751         [ $nums -eq $expected ] ||
4752                 error "'$cmd' wrong: found $nums, expected $expected"
4753 }
4754 run_test 56l "check lfs find -type b"
4755
4756 test_56m() {
4757         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4758
4759         setup_56_special $dir $NUMFILES $NUMDIRS
4760
4761         local expected=$((NUMDIRS + NUMFILES))
4762         local cmd="$LFS find -type c $dir"
4763         local nums=$($cmd | wc -l)
4764         [ $nums -eq $expected ] ||
4765                 error "'$cmd' wrong: found $nums, expected $expected"
4766 }
4767 run_test 56m "check lfs find -type c"
4768
4769 test_56n() {
4770         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
4771         setup_56_special $dir $NUMFILES $NUMDIRS
4772
4773         local expected=$((NUMDIRS + NUMFILES))
4774         local cmd="$LFS find -type l $dir"
4775         local nums=$($cmd | wc -l)
4776
4777         [ $nums -eq $expected ] ||
4778                 error "'$cmd' wrong: found $nums, expected $expected"
4779 }
4780 run_test 56n "check lfs find -type l"
4781
4782 test_56o() {
4783         local dir=$DIR/$tdir
4784
4785         setup_56 $dir $NUMFILES $NUMDIRS
4786         utime $dir/file1 > /dev/null || error "utime (1)"
4787         utime $dir/file2 > /dev/null || error "utime (2)"
4788         utime $dir/dir1 > /dev/null || error "utime (3)"
4789         utime $dir/dir2 > /dev/null || error "utime (4)"
4790         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
4791         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
4792
4793         local expected=4
4794         local nums=$($LFS find -mtime +0 $dir | wc -l)
4795
4796         [ $nums -eq $expected ] ||
4797                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
4798
4799         expected=12
4800         cmd="$LFS find -mtime 0 $dir"
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         local dir=$DIR/$tdir
4812
4813         setup_56 $dir $NUMFILES $NUMDIRS
4814         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
4815
4816         local expected=$NUMFILES
4817         local cmd="$LFS find -uid $RUNAS_ID $dir"
4818         local nums=$($cmd | wc -l)
4819
4820         [ $nums -eq $expected ] ||
4821                 error "'$cmd' wrong: found $nums, expected $expected"
4822
4823         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
4824         cmd="$LFS find ! -uid $RUNAS_ID $dir"
4825         nums=$($cmd | wc -l)
4826         [ $nums -eq $expected ] ||
4827                 error "'$cmd' wrong: found $nums, expected $expected"
4828 }
4829 run_test 56p "check lfs find -uid and ! -uid"
4830
4831 test_56q() {
4832         [ $RUNAS_ID -eq $UID ] &&
4833                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
4834
4835         local dir=$DIR/$tdir
4836
4837         setup_56 $dir $NUMFILES $NUMDIRS
4838         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
4839
4840         local expected=$NUMFILES
4841         local cmd="$LFS find -gid $RUNAS_GID $dir"
4842         local nums=$($cmd | wc -l)
4843
4844         [ $nums -eq $expected ] ||
4845                 error "'$cmd' wrong: found $nums, expected $expected"
4846
4847         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
4848         cmd="$LFS find ! -gid $RUNAS_GID $dir"
4849         nums=$($cmd | wc -l)
4850         [ $nums -eq $expected ] ||
4851                 error "'$cmd' wrong: found $nums, expected $expected"
4852 }
4853 run_test 56q "check lfs find -gid and ! -gid"
4854
4855 test_56r() {
4856         local dir=$DIR/$tdir
4857
4858         setup_56 $dir $NUMFILES $NUMDIRS
4859
4860         local expected=12
4861         local cmd="$LFS find -size 0 -type f $dir"
4862         local nums=$($cmd | wc -l)
4863
4864         [ $nums -eq $expected ] ||
4865                 error "'$cmd' wrong: found $nums, expected $expected"
4866         expected=0
4867         cmd="$LFS find ! -size 0 -type f $dir"
4868         nums=$($cmd | wc -l)
4869         [ $nums -eq $expected ] ||
4870                 error "'$cmd' wrong: found $nums, expected $expected"
4871         echo "test" > $dir/$tfile
4872         echo "test2" > $dir/$tfile.2 && sync
4873         expected=1
4874         cmd="$LFS find -size 5 -type f $dir"
4875         nums=$($cmd | wc -l)
4876         [ $nums -eq $expected ] ||
4877                 error "'$cmd' wrong: found $nums, expected $expected"
4878         expected=1
4879         cmd="$LFS find -size +5 -type f $dir"
4880         nums=$($cmd | wc -l)
4881         [ $nums -eq $expected ] ||
4882                 error "'$cmd' wrong: found $nums, expected $expected"
4883         expected=2
4884         cmd="$LFS find -size +0 -type f $dir"
4885         nums=$($cmd | wc -l)
4886         [ $nums -eq $expected ] ||
4887                 error "'$cmd' wrong: found $nums, expected $expected"
4888         expected=2
4889         cmd="$LFS find ! -size -5 -type f $dir"
4890         nums=$($cmd | wc -l)
4891         [ $nums -eq $expected ] ||
4892                 error "'$cmd' wrong: found $nums, expected $expected"
4893         expected=12
4894         cmd="$LFS find -size -5 -type f $dir"
4895         nums=$($cmd | wc -l)
4896         [ $nums -eq $expected ] ||
4897                 error "'$cmd' wrong: found $nums, expected $expected"
4898 }
4899 run_test 56r "check lfs find -size works"
4900
4901 test_56s() { # LU-611 #LU-9369
4902         [[ $OSTCOUNT -lt 2 ]] && skip "need at least 2 OSTs" && return 0
4903
4904         local dir=$DIR/$tdir
4905         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
4906
4907         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
4908         for i in $(seq $NUMDIRS); do
4909                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
4910         done
4911
4912         local expected=$NUMDIRS
4913         local cmd="$LFS find -c $OSTCOUNT $dir"
4914         local nums=$($cmd | wc -l)
4915
4916         [ $nums -eq $expected ] || {
4917                 $LFS getstripe -R $dir
4918                 error "'$cmd' wrong: found $nums, expected $expected"
4919         }
4920
4921         expected=$((NUMDIRS + onestripe))
4922         cmd="$LFS find -stripe-count +0 -type f $dir"
4923         nums=$($cmd | wc -l)
4924         [ $nums -eq $expected ] || {
4925                 $LFS getstripe -R $dir
4926                 error "'$cmd' wrong: found $nums, expected $expected"
4927         }
4928
4929         expected=$onestripe
4930         cmd="$LFS find -stripe-count 1 -type f $dir"
4931         nums=$($cmd | wc -l)
4932         [ $nums -eq $expected ] || {
4933                 $LFS getstripe -R $dir
4934                 error "'$cmd' wrong: found $nums, expected $expected"
4935         }
4936
4937         cmd="$LFS find -stripe-count -2 -type f $dir"
4938         nums=$($cmd | wc -l)
4939         [ $nums -eq $expected ] || {
4940                 $LFS getstripe -R $dir
4941                 error "'$cmd' wrong: found $nums, expected $expected"
4942         }
4943
4944         expected=0
4945         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
4946         nums=$($cmd | wc -l)
4947         [ $nums -eq $expected ] || {
4948                 $LFS getstripe -R $dir
4949                 error "'$cmd' wrong: found $nums, expected $expected"
4950         }
4951 }
4952 run_test 56s "check lfs find -stripe-count works"
4953
4954 test_56t() { # LU-611 #LU-9369
4955         local dir=$DIR/$tdir
4956
4957         setup_56 $dir 0 $NUMDIRS
4958         for i in $(seq $NUMDIRS); do
4959                 $LFS setstripe -S 4M $dir/dir$i/$tfile
4960         done
4961
4962         local expected=$NUMDIRS
4963         local cmd="$LFS find -S 4M $dir"
4964         local nums=$($cmd | wc -l)
4965
4966         [ $nums -eq $expected ] || {
4967                 $LFS getstripe -R $dir
4968                 error "'$cmd' wrong: found $nums, expected $expected"
4969         }
4970         rm -rf $dir
4971
4972         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
4973
4974         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
4975
4976         expected=$(((NUMDIRS + 1) * NUMFILES))
4977         cmd="$LFS find -stripe-size 512k -type f $dir"
4978         nums=$($cmd | wc -l)
4979         [ $nums -eq $expected ] ||
4980                 error "'$cmd' wrong: found $nums, expected $expected"
4981
4982         cmd="$LFS find -stripe-size +320k -type f $dir"
4983         nums=$($cmd | wc -l)
4984         [ $nums -eq $expected ] ||
4985                 error "'$cmd' wrong: found $nums, expected $expected"
4986
4987         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
4988         cmd="$LFS find -stripe-size +200k -type f $dir"
4989         nums=$($cmd | wc -l)
4990         [ $nums -eq $expected ] ||
4991                 error "'$cmd' wrong: found $nums, expected $expected"
4992
4993         cmd="$LFS find -stripe-size -640k -type f $dir"
4994         nums=$($cmd | wc -l)
4995         [ $nums -eq $expected ] ||
4996                 error "'$cmd' wrong: found $nums, expected $expected"
4997
4998         expected=4
4999         cmd="$LFS find -stripe-size 256k -type f $dir"
5000         nums=$($cmd | wc -l)
5001         [ $nums -eq $expected ] ||
5002                 error "'$cmd' wrong: found $nums, expected $expected"
5003
5004         cmd="$LFS find -stripe-size -320k -type f $dir"
5005         nums=$($cmd | wc -l)
5006         [ $nums -eq $expected ] ||
5007                 error "'$cmd' wrong: found $nums, expected $expected"
5008
5009         expected=0
5010         cmd="$LFS find -stripe-size 1024k -type f $dir"
5011         nums=$($cmd | wc -l)
5012         [ $nums -eq $expected ] ||
5013                 error "'$cmd' wrong: found $nums, expected $expected"
5014 }
5015 run_test 56t "check lfs find -stripe-size works"
5016
5017 test_56u() { # LU-611
5018         local dir=$DIR/$tdir
5019
5020         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
5021
5022         if [[ $OSTCOUNT -gt 1 ]]; then
5023                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
5024                 onestripe=4
5025         else
5026                 onestripe=0
5027         fi
5028
5029         local expected=$(((NUMDIRS + 1) * NUMFILES))
5030         local cmd="$LFS find -stripe-index 0 -type f $dir"
5031         local nums=$($cmd | wc -l)
5032
5033         [ $nums -eq $expected ] ||
5034                 error "'$cmd' wrong: found $nums, expected $expected"
5035
5036         expected=$onestripe
5037         cmd="$LFS find -stripe-index 1 -type f $dir"
5038         nums=$($cmd | wc -l)
5039         [ $nums -eq $expected ] ||
5040                 error "'$cmd' wrong: found $nums, expected $expected"
5041
5042         cmd="$LFS find ! -stripe-index 0 -type f $dir"
5043         nums=$($cmd | wc -l)
5044         [ $nums -eq $expected ] ||
5045                 error "'$cmd' wrong: found $nums, expected $expected"
5046
5047         expected=0
5048         # This should produce an error and not return any files
5049         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
5050         nums=$($cmd 2>/dev/null | wc -l)
5051         [ $nums -eq $expected ] ||
5052                 error "'$cmd' wrong: found $nums, expected $expected"
5053
5054         if [[ $OSTCOUNT -gt 1 ]]; then
5055                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
5056                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
5057                 nums=$($cmd | wc -l)
5058                 [ $nums -eq $expected ] ||
5059                         error "'$cmd' wrong: found $nums, expected $expected"
5060         fi
5061 }
5062 run_test 56u "check lfs find -stripe-index works"
5063
5064 test_56v() {
5065         local MDT_IDX=0
5066         local dir=$DIR/$tdir
5067
5068         setup_56 $dir $NUMFILES $NUMDIRS
5069
5070         UUID=$(mdtuuid_from_index $MDT_IDX $dir)
5071         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $MDT_IDX"
5072
5073         for file in $($LFS find -mdt $UUID $dir); do
5074                 file_mdt_idx=$($LFS getstripe -M $file)
5075                 [ $file_mdt_idx -eq $MDT_IDX ] ||
5076                         error "lfind -mdt $UUID != getstripe -M $file_mdt_idx"
5077         done
5078 }
5079 run_test 56v "check 'lfs find -mdt match with lfs getstripe -M' ======="
5080
5081 test_56w() {
5082         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5083         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5084         local dir=$DIR/$tdir
5085
5086         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
5087
5088         local stripe_size=$($LFS getstripe -S -d $dir) ||
5089                 error "$LFS getstripe -S -d $dir failed"
5090         stripe_size=${stripe_size%% *}
5091
5092         local file_size=$((stripe_size * OSTCOUNT))
5093         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
5094         local required_space=$((file_num * file_size))
5095         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
5096                            head -n1)
5097         [[ $free_space -le $((required_space / 1024)) ]] &&
5098                 skip_env "need $required_space, have $free_space kbytes" &&
5099                 return
5100
5101         local dd_bs=65536
5102         local dd_count=$((file_size / dd_bs))
5103
5104         # write data into the files
5105         local i
5106         local j
5107         local file
5108
5109         for i in $(seq $NUMFILES); do
5110                 file=$dir/file$i
5111                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5112                         error "write data into $file failed"
5113         done
5114         for i in $(seq $NUMDIRS); do
5115                 for j in $(seq $NUMFILES); do
5116                         file=$dir/dir$i/file$j
5117                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
5118                                 error "write data into $file failed"
5119                 done
5120         done
5121
5122         # $LFS_MIGRATE will fail if hard link migration is unsupported
5123         if [[ $(lustre_version_code mds1) -gt $(version_code 2.5.55) ]]; then
5124                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
5125                         error "creating links to $dir/dir1/file1 failed"
5126         fi
5127
5128         local expected=-1
5129
5130         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
5131
5132         # lfs_migrate file
5133         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
5134
5135         echo "$cmd"
5136         eval $cmd || error "$cmd failed"
5137
5138         check_stripe_count $dir/file1 $expected
5139
5140         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.90) ];
5141         then
5142                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
5143                 # OST 1 if it is on OST 0. This file is small enough to
5144                 # be on only one stripe.
5145                 file=$dir/migr_1_ost
5146                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
5147                         error "write data into $file failed"
5148                 local obdidx=$($LFS getstripe -i $file)
5149                 local oldmd5=$(md5sum $file)
5150                 local newobdidx=0
5151
5152                 [[ $obdidx -eq 0 ]] && newobdidx=1
5153                 cmd="$LFS migrate -i $newobdidx $file"
5154                 echo $cmd
5155                 eval $cmd || error "$cmd failed"
5156
5157                 local realobdix=$($LFS getstripe -i $file)
5158                 local newmd5=$(md5sum $file)
5159
5160                 [[ $newobdidx -ne $realobdix ]] &&
5161                         error "new OST is different (was=$obdidx, "\
5162                               "wanted=$newobdidx, got=$realobdix)"
5163                 [[ "$oldmd5" != "$newmd5" ]] &&
5164                         error "md5sum differ: $oldmd5, $newmd5"
5165         fi
5166
5167         # lfs_migrate dir
5168         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
5169         echo "$cmd"
5170         eval $cmd || error "$cmd failed"
5171
5172         for j in $(seq $NUMFILES); do
5173                 check_stripe_count $dir/dir1/file$j $expected
5174         done
5175
5176         # lfs_migrate works with lfs find
5177         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
5178              $LFS_MIGRATE -y -c $expected"
5179         echo "$cmd"
5180         eval $cmd || error "$cmd failed"
5181
5182         for i in $(seq 2 $NUMFILES); do
5183                 check_stripe_count $dir/file$i $expected
5184         done
5185         for i in $(seq 2 $NUMDIRS); do
5186                 for j in $(seq $NUMFILES); do
5187                 check_stripe_count $dir/dir$i/file$j $expected
5188                 done
5189         done
5190 }
5191 run_test 56w "check lfs_migrate -c stripe_count works"
5192
5193 test_56wb() {
5194         local file1=$DIR/$tdir/file1
5195         local create_pool=false
5196         local initial_pool=$($LFS getstripe -p $DIR)
5197         local pool_list=()
5198         local pool=""
5199
5200         echo -n "Creating test dir..."
5201         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5202         echo "done."
5203
5204         echo -n "Creating test file..."
5205         touch $file1 || error "cannot create file"
5206         echo "done."
5207
5208         echo -n "Detecting existing pools..."
5209         while IFS='' read thispool; do
5210                 pool_list+=("$thispool")
5211         done < <($LFS pool_list $MOUNT | awk -F '.' 'NR>=2 { print $2 }')
5212
5213         if [ ${#pool_list[@]} -gt 0 ]; then
5214                 echo "${pool_list[@]}"
5215                 for thispool in "${pool_list[@]}"; do
5216                         if [[ -z "$initial_pool" ||
5217                               "$initial_pool" != "$thispool" ]]; then
5218                                 pool="$thispool"
5219                                 echo "Using existing pool '$pool'"
5220                                 break
5221                         fi
5222                 done
5223         else
5224                 echo "none detected."
5225         fi
5226         if [ -z "$pool" ]; then
5227                 pool=${POOL:-testpool}
5228                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
5229                 echo -n "Creating pool '$pool'..."
5230                 create_pool=true
5231                 pool_add $pool &> /dev/null ||
5232                         error "pool_add failed"
5233                 echo "done."
5234
5235                 echo -n "Adding target to pool..."
5236                 pool_add_targets $pool 0 0 1 &> /dev/null ||
5237                         error "pool_add_targets failed"
5238                 echo "done."
5239         fi
5240
5241         echo -n "Setting pool using -p option..."
5242         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
5243                 error "migrate failed rc = $?"
5244         echo "done."
5245
5246         echo -n "Verifying test file is in pool after migrating..."
5247         [ "$($LFS getstripe -p $file1)" = $pool ] ||
5248                 error "file was not migrated to pool $pool"
5249         echo "done."
5250
5251         echo -n "Removing test file from pool '$pool'..."
5252         $LFS migrate $file1 &> /dev/null ||
5253                 error "cannot remove from pool"
5254         [ "$($LFS getstripe -p $file1)" ] &&
5255                 error "pool still set"
5256         echo "done."
5257
5258         echo -n "Setting pool using --pool option..."
5259         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
5260                 error "migrate failed rc = $?"
5261         echo "done."
5262
5263         # Clean up
5264         rm -f $file1
5265         if $create_pool; then
5266                 destroy_test_pools 2> /dev/null ||
5267                         error "destroy test pools failed"
5268         fi
5269 }
5270 run_test 56wb "check lfs_migrate pool support"
5271
5272 test_56wc() {
5273         local file1="$DIR/$tdir/file 1"
5274
5275         echo -n "Creating test dir..."
5276         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
5277         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5278                 error "cannot set stripe"
5279         echo "done"
5280
5281         echo -n "Setting initial stripe for test file..."
5282         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
5283                 error "cannot set stripe"
5284         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5285                 error "stripe size not set"
5286         echo "done."
5287
5288         # File currently set to -S 512K -c 1
5289
5290         # Ensure -c and -S options are rejected when -R is set
5291         echo -n "Verifying incompatible options are detected..."
5292         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
5293                 error "incompatible -c and -R options not detected"
5294         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
5295                 error "incompatible -S and -R options not detected"
5296         echo "done."
5297
5298         # Ensure unrecognized options are passed through to 'lfs migrate'
5299         echo -n "Verifying -S option is passed through to lfs migrate..."
5300         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
5301                 error "migration failed"
5302         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5303                 error "file was not restriped"
5304         echo "done."
5305
5306         # File currently set to -S 1M -c 1
5307
5308         # Ensure long options are supported
5309         echo -n "Verifying long options supported..."
5310         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
5311                 error "long option without argument not supported"
5312         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
5313                 error "long option with argument not supported"
5314         [ $($LFS getstripe -S "$file1") -eq 524288 ] ||
5315                 error "file not restriped with --stripe-size option"
5316         echo "done."
5317
5318         # File currently set to -S 512K -c 1
5319
5320         if [ "$OSTCOUNT" -gt 1 ]; then
5321                 echo -n "Verifying explicit stripe count can be set..."
5322                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
5323                         error "migrate failed"
5324                 [ $($LFS getstripe -c "$file1") -eq 2 ] ||
5325                         error "file not restriped to explicit count"
5326                 echo "done."
5327         fi
5328
5329         # File currently set to -S 512K -c 1 or -S 512K -c 2
5330
5331         # Ensure parent striping is used if -R is set, and no stripe
5332         # count or size is specified
5333         echo -n "Setting stripe for parent directory..."
5334         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
5335                 error "cannot set stripe"
5336         echo "done."
5337
5338         echo -n "Verifying restripe option uses parent stripe settings..."
5339         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
5340                 error "migrate failed"
5341         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5342                 error "file not restriped to parent settings"
5343         [ $($LFS getstripe -c "$file1") -eq 1 ] ||
5344                 error "file not restriped to parent settings"
5345         echo "done."
5346
5347         # File currently set to -S 1M -c 1
5348
5349         # Ensure striping is preserved if -R is not set, and no stripe
5350         # count or size is specified
5351         echo -n "Verifying striping size preserved when not specified..."
5352         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
5353                 error "cannot set stripe on parent directory"
5354         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5355                 error "migrate failed"
5356         [ $($LFS getstripe -S "$file1") -eq 1048576 ] ||
5357                 error "file was restriped"
5358         echo "done."
5359
5360         # Ensure file name properly detected when final option has no argument
5361         echo -n "Verifying file name properly detected..."
5362         $LFS_MIGRATE -y "$file1" &> /dev/null ||
5363                 error "file name interpreted as option argument"
5364         echo "done."
5365
5366         # Clean up
5367         rm -f "$file1"
5368 }
5369 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
5370
5371 test_56wd() {
5372         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5373         local file1=$DIR/$tdir/file1
5374
5375         echo -n "Creating test dir..."
5376         test_mkdir $DIR/$tdir || error "cannot create dir"
5377         echo "done."
5378
5379         echo -n "Creating test file..."
5380         touch $file1
5381         echo "done."
5382
5383         # Ensure 'lfs migrate' will fail by using a non-existent option,
5384         # and make sure rsync is not called to recover
5385         echo -n "Make sure --no-rsync option works..."
5386         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
5387                 grep -q 'refusing to fall back to rsync' ||
5388                 error "rsync was called with --no-rsync set"
5389         echo "done."
5390
5391         # Ensure rsync is called without trying 'lfs migrate' first
5392         echo -n "Make sure --rsync option works..."
5393         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
5394                 grep -q 'falling back to rsync' &&
5395                 error "lfs migrate was called with --rsync set"
5396         echo "done."
5397
5398         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
5399         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
5400                 grep -q 'at the same time' ||
5401                 error "--rsync and --no-rsync accepted concurrently"
5402         echo "done."
5403
5404         # Clean up
5405         rm -f $file1
5406 }
5407 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
5408
5409 test_56x() {
5410         check_swap_layouts_support && return 0
5411         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5412
5413         local dir=$DIR/$tdir
5414         local ref1=/etc/passwd
5415         local file1=$dir/file1
5416
5417         test_mkdir $dir || error "creating dir $dir"
5418         $LFS setstripe -c 2 $file1
5419         cp $ref1 $file1
5420         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
5421         stripe=$($LFS getstripe -c $file1)
5422         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5423         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5424
5425         # clean up
5426         rm -f $file1
5427 }
5428 run_test 56x "lfs migration support"
5429
5430 test_56xa() {
5431         check_swap_layouts_support && return 0
5432         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
5433
5434         local dir=$DIR/$tdir/$testnum
5435
5436         test_mkdir -p $dir
5437
5438         local ref1=/etc/passwd
5439         local file1=$dir/file1
5440
5441         $LFS setstripe -c 2 $file1
5442         cp $ref1 $file1
5443         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
5444
5445         local stripe=$($LFS getstripe -c $file1)
5446
5447         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
5448         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
5449
5450         # clean up
5451         rm -f $file1
5452 }
5453 run_test 56xa "lfs migration --block support"
5454
5455 check_migrate_links() {
5456         local dir="$1"
5457         local file1="$dir/file1"
5458         local begin="$2"
5459         local count="$3"
5460         local total_count=$(($begin + $count - 1))
5461         local symlink_count=10
5462         local uniq_count=10
5463
5464         if [ ! -f "$file1" ]; then
5465                 echo -n "creating initial file..."
5466                 $LFS setstripe -c 1 -S "512k" "$file1" ||
5467                         error "cannot setstripe initial file"
5468                 echo "done"
5469
5470                 echo -n "creating symlinks..."
5471                 for s in $(seq 1 $symlink_count); do
5472                         ln -s "$file1" "$dir/slink$s" ||
5473                                 error "cannot create symlinks"
5474                 done
5475                 echo "done"
5476
5477                 echo -n "creating nonlinked files..."
5478                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
5479                         error "cannot create nonlinked files"
5480                 echo "done"
5481         fi
5482
5483         # create hard links
5484         if [ ! -f "$dir/file$total_count" ]; then
5485                 echo -n "creating hard links $begin:$total_count..."
5486                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
5487                         /dev/null || error "cannot create hard links"
5488                 echo "done"
5489         fi
5490
5491         echo -n "checking number of hard links listed in xattrs..."
5492         local fid=$($LFS getstripe -F "$file1")
5493         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
5494
5495         echo "${#paths[*]}"
5496         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
5497                         echo "hard link list has unexpected size, skipping test"
5498                         return 0
5499         fi
5500         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
5501                         error "link names should exceed xattrs size"
5502         fi
5503
5504         echo -n "migrating files..."
5505         local migrate_out=$($LFS_MIGRATE -y -S '1m' $dir)
5506         local rc=$?
5507         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
5508         echo "done"
5509
5510         # make sure all links have been properly migrated
5511         echo -n "verifying files..."
5512         fid=$($LFS getstripe -F "$file1") ||
5513                 error "cannot get fid for file $file1"
5514         for i in $(seq 2 $total_count); do
5515                 local fid2=$($LFS getstripe -F $dir/file$i)
5516
5517                 [ "$fid2" == "$fid" ] ||
5518                         error "migrated hard link has mismatched FID"
5519         done
5520
5521         # make sure hard links were properly detected, and migration was
5522         # performed only once for the entire link set; nonlinked files should
5523         # also be migrated
5524         local actual=$(grep -c 'done migrate' <<< "$migrate_out")
5525         local expected=$(($uniq_count + 1))
5526
5527         [ "$actual" -eq  "$expected" ] ||
5528                 error "hard links individually migrated ($actual != $expected)"
5529
5530         # make sure the correct number of hard links are present
5531         local hardlinks=$(stat -c '%h' "$file1")
5532
5533         [ $hardlinks -eq $total_count ] ||
5534                 error "num hard links $hardlinks != $total_count"
5535         echo "done"
5536
5537         return 0
5538 }
5539
5540 test_56xb() {
5541         local dir="$DIR/$tdir"
5542
5543         test_mkdir "$dir" || error "cannot create dir $dir"
5544
5545         echo "testing lfs migrate mode when all links fit within xattrs"
5546         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 2 99
5547
5548         echo "testing rsync mode when all links fit within xattrs"
5549         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 2 99
5550
5551         echo "testing lfs migrate mode when all links do not fit within xattrs"
5552         LFS_MIGRATE_RSYNC=false check_migrate_links "$dir" 101 100
5553
5554         echo "testing rsync mode when all links do not fit within xattrs"
5555         LFS_MIGRATE_RSYNC=true check_migrate_links "$dir" 101 100
5556
5557         # clean up
5558         rm -rf $dir
5559 }
5560 run_test 56xb "lfs migration hard link support"
5561
5562 test_56y() {
5563         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
5564                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
5565                 return
5566
5567         local res=""
5568         local dir=$DIR/$tdir
5569         local f1=$dir/file1
5570         local f2=$dir/file2
5571
5572         test_mkdir -p $dir || error "creating dir $dir"
5573         touch $f1 || error "creating std file $f1"
5574         $MULTIOP $f2 H2c || error "creating released file $f2"
5575
5576         # a directory can be raid0, so ask only for files
5577         res=$($LFS find $dir -L raid0 -type f | wc -l)
5578         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
5579
5580         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
5581         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
5582
5583         # only files can be released, so no need to force file search
5584         res=$($LFS find $dir -L released)
5585         [[ $res == $f2 ]] || error "search released: found $res != $f2"
5586
5587         res=$($LFS find $dir -type f \! -L released)
5588         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
5589 }
5590 run_test 56y "lfs find -L raid0|released"
5591
5592 test_56z() { # LU-4824
5593         # This checks to make sure 'lfs find' continues after errors
5594         # There are two classes of errors that should be caught:
5595         # - If multiple paths are provided, all should be searched even if one
5596         #   errors out
5597         # - If errors are encountered during the search, it should not terminate
5598         #   early
5599         local dir=$DIR/$tdir
5600         local i
5601
5602         test_mkdir $dir
5603         for i in d{0..9}; do
5604                 test_mkdir $dir/$i
5605         done
5606         touch $dir/d{0..9}/$tfile
5607         $LFS find $DIR/non_existent_dir $dir &&
5608                 error "$LFS find did not return an error"
5609         # Make a directory unsearchable. This should NOT be the last entry in
5610         # directory order.  Arbitrarily pick the 6th entry
5611         chmod 700 $($LFS find $dir -type d | sed '6!d')
5612
5613         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
5614
5615         # The user should be able to see 10 directories and 9 files
5616         [ $count == 19 ] || error "$LFS find did not continue after error"
5617 }
5618 run_test 56z "lfs find should continue after an error"
5619
5620 test_56aa() { # LU-5937
5621         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
5622
5623         local dir=$DIR/$tdir
5624
5625         mkdir $dir
5626         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
5627
5628         createmany -o $dir/striped_dir/${tfile}- 1024
5629         local dirs=$(lfs find --size +8k $dir/)
5630
5631         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
5632 }
5633 run_test 56aa "lfs find --size under striped dir"
5634
5635 test_56ba() {
5636         # Create composite files with one component
5637         local dir=$DIR/$tdir
5638
5639         setup_56 $dir/1Mfiles 5 1 "--component-end 1M"
5640         # Create composite files with three components
5641         setup_56 $dir/2Mfiles 5 2 "-E 2M -E 4M -E 6M"
5642         # Create non-composite files
5643         createmany -o $dir/${tfile}- 10
5644
5645         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
5646
5647         [[ $nfiles == 10 ]] ||
5648                 error "lfs find -E 1M found $nfiles != 10 files"
5649
5650         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
5651         [[ $nfiles == 25 ]] ||
5652                 error "lfs find ! -E 1M found $nfiles != 25 files"
5653
5654         # All files have a component that starts at 0
5655         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
5656         [[ $nfiles == 35 ]] ||
5657                 error "lfs find --component-start 0 - $nfiles != 35 files"
5658
5659         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
5660         [[ $nfiles == 15 ]] ||
5661                 error "lfs find --component-start 2M - $nfiles != 15 files"
5662
5663         # All files created here have a componenet that does not starts at 2M
5664         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
5665         [[ $nfiles == 35 ]] ||
5666                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
5667
5668         # Find files with a specified number of components
5669         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
5670         [[ $nfiles == 15 ]] ||
5671                 error "lfs find --component-count 3 - $nfiles != 15 files"
5672
5673         # Remember non-composite files have a component count of zero
5674         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
5675         [[ $nfiles == 10 ]] ||
5676                 error "lfs find --component-count 0 - $nfiles != 10 files"
5677
5678         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
5679         [[ $nfiles == 20 ]] ||
5680                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
5681
5682         # All files have a flag called "init"
5683         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
5684         [[ $nfiles == 35 ]] ||
5685                 error "lfs find --component-flags init - $nfiles != 35 files"
5686
5687         # Multi-component files will have a component not initialized
5688         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
5689         [[ $nfiles == 15 ]] ||
5690                 error "lfs find !--component-flags init - $nfiles != 15 files"
5691
5692         rm -rf $dir
5693
5694 }
5695 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
5696
5697 test_57a() {
5698         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5699         # note test will not do anything if MDS is not local
5700         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5701                 skip "ldiskfs only test"
5702                 return
5703         fi
5704
5705         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5706         local MNTDEV="osd*.*MDT*.mntdev"
5707         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
5708         [ -z "$DEV" ] && error "can't access $MNTDEV"
5709         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
5710                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
5711                         error "can't access $DEV"
5712                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
5713                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
5714                 rm $TMP/t57a.dump
5715         done
5716 }
5717 run_test 57a "verify MDS filesystem created with large inodes =="
5718
5719 test_57b() {
5720         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5721         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
5722                 skip "ldiskfs only test"
5723                 return
5724         fi
5725
5726         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5727         local dir=$DIR/$tdir
5728
5729         local FILECOUNT=100
5730         local FILE1=$dir/f1
5731         local FILEN=$dir/f$FILECOUNT
5732
5733         rm -rf $dir || error "removing $dir"
5734         test_mkdir -c1 $dir
5735         local mdtidx=$($LFS getstripe -M $dir)
5736         local mdtname=MDT$(printf %04x $mdtidx)
5737         local facet=mds$((mdtidx + 1))
5738
5739         echo "mcreating $FILECOUNT files"
5740         createmany -m $dir/f 1 $FILECOUNT || \
5741                 error "creating files in $dir"
5742
5743         # verify that files do not have EAs yet
5744         $GETSTRIPE $FILE1 2>&1 | grep -q "no stripe" || error "$FILE1 has an EA"
5745         $GETSTRIPE $FILEN 2>&1 | grep -q "no stripe" || error "$FILEN has an EA"
5746
5747         sync
5748         sleep 1
5749         df $dir  #make sure we get new statfs data
5750         local MDSFREE=$(do_facet $facet \
5751                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5752         local MDCFREE=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5753         echo "opening files to create objects/EAs"
5754         local FILE
5755         for FILE in `seq -f $dir/f%g 1 $FILECOUNT`; do
5756                 $OPENFILE -f O_RDWR $FILE > /dev/null 2>&1 || error "opening $FILE"
5757         done
5758
5759         # verify that files have EAs now
5760         $GETSTRIPE $FILE1 | grep -q "obdidx" || error "$FILE1 missing EA"
5761         $GETSTRIPE $FILEN | grep -q "obdidx" || error "$FILEN missing EA"
5762
5763         sleep 1  #make sure we get new statfs data
5764         df $dir
5765         local MDSFREE2=$(do_facet $facet \
5766                 lctl get_param -n osd*.*$mdtname.kbytesfree)
5767         local MDCFREE2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
5768         if [[ $MDCFREE2 -lt $((MDCFREE - 16)) ]]; then
5769                 if [ "$MDSFREE" != "$MDSFREE2" ]; then
5770                         error "MDC before $MDCFREE != after $MDCFREE2"
5771                 else
5772                         echo "MDC before $MDCFREE != after $MDCFREE2"
5773                         echo "unable to confirm if MDS has large inodes"
5774                 fi
5775         fi
5776         rm -rf $dir
5777 }
5778 run_test 57b "default LOV EAs are stored inside large inodes ==="
5779
5780 test_58() {
5781         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5782         [ -z "$(which wiretest 2>/dev/null)" ] &&
5783                         skip_env "could not find wiretest" && return
5784         wiretest
5785 }
5786 run_test 58 "verify cross-platform wire constants =============="
5787
5788 test_59() {
5789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5790         echo "touch 130 files"
5791         createmany -o $DIR/f59- 130
5792         echo "rm 130 files"
5793         unlinkmany $DIR/f59- 130
5794         sync
5795         # wait for commitment of removal
5796         wait_delete_completed
5797 }
5798 run_test 59 "verify cancellation of llog records async ========="
5799
5800 TEST60_HEAD="test_60 run $RANDOM"
5801 test_60a() {
5802         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5803         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
5804         do_facet mgs "! which run-llog.sh &> /dev/null" &&
5805                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
5806                         skip_env "missing subtest run-llog.sh" && return
5807
5808         log "$TEST60_HEAD - from kernel mode"
5809         do_facet mgs "$LCTL set_param debug=warning; $LCTL dk > /dev/null"
5810         do_facet mgs "sh run-llog.sh" || error "run-llog.sh failed"
5811         do_facet mgs $LCTL dk > $TMP/$tfile
5812
5813         # LU-6388: test llog_reader
5814         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
5815         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
5816         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
5817                         skip_env "missing llog_reader" && return
5818         local fstype=$(facet_fstype mgs)
5819         [ $fstype != ldiskfs -a $fstype != zfs ] &&
5820                 skip_env "Only for ldiskfs or zfs type mgs" && return
5821
5822         local mntpt=$(facet_mntpt mgs)
5823         local mgsdev=$(mgsdevname 1)
5824         local fid_list
5825         local fid
5826         local rec_list
5827         local rec
5828         local rec_type
5829         local obj_file
5830         local path
5831         local seq
5832         local oid
5833         local pass=true
5834
5835         #get fid and record list
5836         fid_list=($(awk '/9_sub.*record/ { print $NF }' /$TMP/$tfile |
5837                 tail -n 4))
5838         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' /$TMP/$tfile |
5839                 tail -n 4))
5840         #remount mgs as ldiskfs or zfs type
5841         stop mgs || error "stop mgs failed"
5842         mount_fstype mgs || error "remount mgs failed"
5843         for ((i = 0; i < ${#fid_list[@]}; i++)); do
5844                 fid=${fid_list[i]}
5845                 rec=${rec_list[i]}
5846                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
5847                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
5848                 oid=$((16#$oid))
5849
5850                 case $fstype in
5851                         ldiskfs )
5852                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
5853                         zfs )
5854                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
5855                 esac
5856                 echo "obj_file is $obj_file"
5857                 do_facet mgs $llog_reader $obj_file
5858
5859                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
5860                         awk '{ print $3 }' | sed -e "s/^type=//g")
5861                 if [ $rec_type != $rec ]; then
5862                         echo "FAILED test_60a wrong record type $rec_type," \
5863                               "should be $rec"
5864                         pass=false
5865                         break
5866                 fi
5867
5868                 #check obj path if record type is LLOG_LOGID_MAGIC
5869                 if [ "$rec" == "1064553b" ]; then
5870                         path=$(do_facet mgs $llog_reader $obj_file |
5871                                 grep "path=" | awk '{ print $NF }' |
5872                                 sed -e "s/^path=//g")
5873                         if [ $obj_file != $mntpt/$path ]; then
5874                                 echo "FAILED test_60a wrong obj path" \
5875                                       "$montpt/$path, should be $obj_file"
5876                                 pass=false
5877                                 break
5878                         fi
5879                 fi
5880         done
5881         rm -f $TMP/$tfile
5882         #restart mgs before "error", otherwise it will block the next test
5883         stop mgs || error "stop mgs failed"
5884         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
5885         $pass || error "test failed, see FAILED test_60a messages for specifics"
5886 }
5887 run_test 60a "llog_test run from kernel module and test llog_reader"
5888
5889 test_60aa() {
5890         # test old logid format
5891         if [ $(lustre_version_code mgs) -le $(version_code 3.1.53) ]; then
5892                 do_facet mgs $LCTL dl | grep MGS
5893                 do_facet mgs "$LCTL --device %MGS llog_print \\\\\\\$$FSNAME-client" ||
5894                         error "old llog_print failed"
5895         fi
5896
5897         # test new logid format
5898         if [ $(lustre_version_code mgs) -ge $(version_code 2.9.53) ]; then
5899                 do_facet mgs "$LCTL --device MGS llog_print $FSNAME-client" ||
5900                         error "new llog_print failed"
5901         fi
5902 }
5903 run_test 60aa "llog_print works with FIDs and simple names"
5904
5905 test_60b() { # bug 6411
5906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5907         dmesg > $DIR/$tfile
5908         LLOG_COUNT=$(dmesg | awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
5909                                 /llog.test/ {
5910                                         if (marker)
5911                                                 from_marker++
5912                                         from_begin++
5913                                 }
5914                                 END {
5915                                         if (marker)
5916                                                 print from_marker
5917                                         else
5918                                                 print from_begin
5919                                 }")
5920         [[ $LLOG_COUNT -gt 100 ]] &&
5921                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
5922 }
5923 run_test 60b "limit repeated messages from CERROR/CWARN ========"
5924
5925 test_60c() {
5926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5927         echo "create 5000 files"
5928         createmany -o $DIR/f60c- 5000
5929 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
5930         lctl set_param fail_loc=0x80000137
5931         unlinkmany $DIR/f60c- 5000
5932         lctl set_param fail_loc=0
5933 }
5934 run_test 60c "unlink file when mds full"
5935
5936 test_60d() {
5937         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5938         SAVEPRINTK=$(lctl get_param -n printk)
5939
5940         # verify "lctl mark" is even working"
5941         MESSAGE="test message ID $RANDOM $$"
5942         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5943         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
5944
5945         lctl set_param printk=0 || error "set lnet.printk failed"
5946         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
5947         MESSAGE="new test message ID $RANDOM $$"
5948         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
5949         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
5950         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
5951
5952         lctl set_param -n printk="$SAVEPRINTK"
5953 }
5954 run_test 60d "test printk console message masking"
5955
5956 test_60e() {
5957         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5958         remote_mds_nodsh && skip "remote MDS with nodsh" && return
5959         touch $DIR/$tfile
5960 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
5961         do_facet mds1 lctl set_param fail_loc=0x15b
5962         rm $DIR/$tfile
5963 }
5964 run_test 60e "no space while new llog is being created"
5965
5966 test_61() {
5967         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5968         f="$DIR/f61"
5969         dd if=/dev/zero of=$f bs=$(page_size) count=1 || error "dd $f failed"
5970         cancel_lru_locks osc
5971         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
5972         sync
5973 }
5974 run_test 61 "mmap() writes don't make sync hang ================"
5975
5976 # bug 2330 - insufficient obd_match error checking causes LBUG
5977 test_62() {
5978         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5979         f="$DIR/f62"
5980         echo foo > $f
5981         cancel_lru_locks osc
5982         lctl set_param fail_loc=0x405
5983         cat $f && error "cat succeeded, expect -EIO"
5984         lctl set_param fail_loc=0
5985 }
5986 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
5987 # match every page all of the time.
5988 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
5989
5990 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
5991 # Though this test is irrelevant anymore, it helped to reveal some
5992 # other grant bugs (LU-4482), let's keep it.
5993 test_63a() {   # was test_63
5994         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
5995         MAX_DIRTY_MB=`lctl get_param -n osc.*.max_dirty_mb | head -n 1`
5996         for i in `seq 10` ; do
5997                 dd if=/dev/zero of=$DIR/f63 bs=8k &
5998                 sleep 5
5999                 kill $!
6000                 sleep 1
6001         done
6002
6003         rm -f $DIR/f63 || true
6004 }
6005 run_test 63a "Verify oig_wait interruption does not crash ======="
6006
6007 # bug 2248 - async write errors didn't return to application on sync
6008 # bug 3677 - async write errors left page locked
6009 test_63b() {
6010         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6011         debugsave
6012         lctl set_param debug=-1
6013
6014         # ensure we have a grant to do async writes
6015         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
6016         rm $DIR/$tfile
6017
6018         sync    # sync lest earlier test intercept the fail_loc
6019
6020         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
6021         lctl set_param fail_loc=0x80000406
6022         $MULTIOP $DIR/$tfile Owy && \
6023                 error "sync didn't return ENOMEM"
6024         sync; sleep 2; sync     # do a real sync this time to flush page
6025         lctl get_param -n llite.*.dump_page_cache | grep locked && \
6026                 error "locked page left in cache after async error" || true
6027         debugrestore
6028 }
6029 run_test 63b "async write errors should be returned to fsync ==="
6030
6031 test_64a () {
6032         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6033         df $DIR
6034         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur* | grep "[0-9]"
6035 }
6036 run_test 64a "verify filter grant calculations (in kernel) ====="
6037
6038 test_64b () {
6039         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6040         sh oos.sh $MOUNT || error "oos.sh failed: $?"
6041 }
6042 run_test 64b "check out-of-space detection on client"
6043
6044 test_64c() {
6045         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
6046 }
6047 run_test 64c "verify grant shrink"
6048
6049 # this does exactly what osc_request.c:osc_announce_cached() does in
6050 # order to calculate max amount of grants to ask from server
6051 want_grant() {
6052         local tgt=$1
6053
6054         local page_size=$(get_page_size client)
6055
6056         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
6057         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
6058
6059         ((rpc_in_flight ++));
6060         nrpages=$((nrpages * rpc_in_flight))
6061
6062         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
6063
6064         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / page_size))
6065
6066         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
6067         local undirty=$((nrpages * page_size))
6068
6069         local max_extent_pages
6070         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
6071             grep grant_max_extent_size | awk '{print $2}')
6072         max_extent_pages=$((max_extent_pages / page_size))
6073         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
6074         local grant_extent_tax
6075         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6076             grep grant_extent_tax | awk '{print $2}')
6077
6078         undirty=$((undirty + nrextents * grant_extent_tax))
6079
6080         echo $undirty
6081 }
6082
6083 # this is size of unit for grant allocation. It should be equal to
6084 # what tgt_grant.c:tgt_grant_chunk() calculates
6085 grant_chunk() {
6086         local tgt=$1
6087         local max_brw_size
6088         local grant_extent_tax
6089
6090         max_brw_size=$($LCTL get_param osc.${tgt}.import |
6091             grep max_brw_size | awk '{print $2}')
6092
6093         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
6094             grep grant_extent_tax | awk '{print $2}')
6095
6096         echo $(((max_brw_size + grant_extent_tax) * 2))
6097 }
6098
6099 test_64d() {
6100         [ $(lustre_version_code ost1) -lt $(version_code 2.10.56) ] &&
6101                 skip "OST < 2.10.55 doesn't limit grants enough" && return 0
6102
6103         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
6104
6105         [[ $($LCTL get_param osc.${tgt}.import |
6106                     grep "connect_flags:.*grant_param") ]] || \
6107                         { skip "no grant_param connect flag"; return; }
6108
6109         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
6110
6111         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
6112
6113         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
6114
6115         $SETSTRIPE $DIR/$tfile -i 0 -c 1
6116         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
6117         ddpid=$!
6118
6119         while true
6120         do
6121                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
6122                 if [[ $cur_grant -gt $max_cur_granted ]]
6123                 then
6124                         kill $ddpid
6125                         error "cur_grant $cur_grant > $max_cur_granted"
6126                 fi
6127                 kill -0 $ddpid
6128                 [[ $? -ne 0 ]] && break;
6129                 sleep 2
6130         done
6131         $LCTL set_param debug="$olddebug" 2> /dev/null || true
6132 }
6133 run_test 64d "check grant limit exceed"
6134
6135 # bug 1414 - set/get directories' stripe info
6136 test_65a() {
6137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6138         test_mkdir $DIR/$tdir
6139         touch $DIR/$tdir/f1
6140         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
6141 }
6142 run_test 65a "directory with no stripe info"
6143
6144 test_65b() {
6145         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6146         test_mkdir $DIR/$tdir
6147         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6148
6149         $SETSTRIPE -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6150                                                 error "setstripe"
6151         touch $DIR/$tdir/f2
6152         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
6153 }
6154 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
6155
6156 test_65c() {
6157         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6158         [ $OSTCOUNT -lt 2 ] && skip "need at least 2 OSTs" && return
6159         test_mkdir $DIR/$tdir
6160         local stripesize=$($GETSTRIPE -S $DIR/$tdir)
6161
6162         $LFS setstripe -S $((stripesize * 4)) -i 1 \
6163                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
6164         touch $DIR/$tdir/f3
6165         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
6166 }
6167 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
6168
6169 test_65d() {
6170         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6171         test_mkdir $DIR/$tdir
6172         local STRIPECOUNT=$($GETSTRIPE -c $DIR/$tdir)
6173         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6174
6175         if [[ $STRIPECOUNT -le 0 ]]; then
6176                 sc=1
6177         elif [[ $STRIPECOUNT -gt 2000 ]]; then
6178 #LOV_MAX_STRIPE_COUNT is 2000
6179                 [[ $OSTCOUNT -gt 2000 ]] && sc=2000 || sc=$(($OSTCOUNT - 1))
6180         else
6181                 sc=$(($STRIPECOUNT - 1))
6182         fi
6183         $SETSTRIPE -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
6184         touch $DIR/$tdir/f4 $DIR/$tdir/f5
6185         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
6186                 error "lverify failed"
6187 }
6188 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
6189
6190 test_65e() {
6191         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6192         test_mkdir $DIR/$tdir
6193
6194         $SETSTRIPE $DIR/$tdir || error "setstripe"
6195         $GETSTRIPE -v $DIR/$tdir | grep "Default" ||
6196                                         error "no stripe info failed"
6197         touch $DIR/$tdir/f6
6198         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
6199 }
6200 run_test 65e "directory setstripe defaults"
6201
6202 test_65f() {
6203         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6204         test_mkdir $DIR/${tdir}f
6205         $RUNAS $SETSTRIPE $DIR/${tdir}f && error "setstripe succeeded" || true
6206 }
6207 run_test 65f "dir setstripe permission (should return error) ==="
6208
6209 test_65g() {
6210         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6211         test_mkdir $DIR/$tdir
6212         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6213
6214         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6215                 error "setstripe -S failed"
6216         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
6217         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
6218                 error "delete default stripe failed"
6219 }
6220 run_test 65g "directory setstripe -d"
6221
6222 test_65h() {
6223         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6224         test_mkdir $DIR/$tdir
6225         local STRIPESIZE=$($GETSTRIPE -S $DIR/$tdir)
6226
6227         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
6228                 error "setstripe -S failed"
6229         test_mkdir $DIR/$tdir/dd1
6230         [ $($LFS getstripe -c $DIR/$tdir) = $($GETSTRIPE -c $DIR/$tdir/dd1) ] ||
6231                 error "stripe info inherit failed"
6232 }
6233 run_test 65h "directory stripe info inherit ===================="
6234
6235 test_65i() { # bug6367
6236         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6237         $SETSTRIPE -S 65536 -c -1 $MOUNT
6238 }
6239 run_test 65i "set non-default striping on root directory (bug 6367)="
6240
6241 test_65ia() { # bug12836
6242         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6243         $GETSTRIPE $MOUNT || error "getstripe $MOUNT failed"
6244 }
6245 run_test 65ia "getstripe on -1 default directory striping"
6246
6247 test_65ib() { # bug12836
6248         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6249         $GETSTRIPE -v $MOUNT || error "getstripe -v $MOUNT failed"
6250 }
6251 run_test 65ib "getstripe -v on -1 default directory striping"
6252
6253 test_65ic() { # bug12836
6254         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6255         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
6256 }
6257 run_test 65ic "new find on -1 default directory striping"
6258
6259 test_65j() { # bug6367
6260         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6261         [ $($LFS getstripe --component-count $MOUNT) -gt 1 ] &&
6262                 skip "don't delete default PFL layout" && return
6263         sync; sleep 1
6264         # if we aren't already remounting for each test, do so for this test
6265         if [ "$CLEANUP" = ":" -a "$I_MOUNTED" = "yes" ]; then
6266                 cleanup || error "failed to unmount"
6267                 setup
6268         fi
6269         $SETSTRIPE -d $MOUNT || error "setstripe failed"
6270 }
6271 run_test 65j "set default striping on root directory (bug 6367)="
6272
6273 test_65k() { # bug11679
6274         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6275         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
6276         remote_mds_nodsh && skip "remote MDS with nodsh" && return
6277
6278         local disable_precreate=true
6279         [ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.8.54) ] &&
6280                 disable_precreate=false
6281
6282         echo "Check OST status: "
6283         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
6284                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
6285
6286         for OSC in $MDS_OSCS; do
6287                 echo $OSC "is active"
6288                 do_facet $SINGLEMDS lctl --device %$OSC activate
6289         done
6290
6291         for INACTIVE_OSC in $MDS_OSCS; do
6292                 local ost=$(osc_to_ost $INACTIVE_OSC)
6293                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
6294                                lov.*md*.target_obd |
6295                                awk -F: /$ost/'{ print $1 }' | head -n 1)
6296
6297                 mkdir -p $DIR/$tdir
6298                 $SETSTRIPE -i $ostnum -c 1 $DIR/$tdir
6299                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
6300
6301                 echo "Deactivate: " $INACTIVE_OSC
6302                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
6303
6304                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
6305                               osp.$ost*MDT0000.create_count")
6306                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
6307                                   osp.$ost*MDT0000.max_create_count")
6308                 $disable_precreate &&
6309                         do_facet $SINGLEMDS "lctl set_param -n \
6310                                 osp.$ost*MDT0000.max_create_count=0"
6311
6312                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
6313                         [ -f $DIR/$tdir/$idx ] && continue
6314                         echo "$SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx"
6315                         $SETSTRIPE -i $idx -c 1 $DIR/$tdir/$idx ||
6316                                 error "setstripe $idx should succeed"
6317                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
6318                 done
6319                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
6320                 rmdir $DIR/$tdir
6321
6322                 do_facet $SINGLEMDS "lctl set_param -n \
6323                         osp.$ost*MDT0000.max_create_count=$max_count"
6324                 do_facet $SINGLEMDS "lctl set_param -n \
6325                         osp.$ost*MDT0000.create_count=$count"
6326                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
6327                 echo $INACTIVE_OSC "is Activate"
6328
6329                 wait_osc_import_state mds ost$ostnum FULL
6330         done
6331 }
6332 run_test 65k "validate manual striping works properly with deactivated OSCs"
6333
6334 test_65l() { # bug 12836
6335         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6336         test_mkdir -p $DIR/$tdir/test_dir
6337         $SETSTRIPE -c -1 $DIR/$tdir/test_dir
6338         $LFS find -mtime -1 $DIR/$tdir >/dev/null
6339 }
6340 run_test 65l "lfs find on -1 stripe dir ========================"
6341
6342 test_65m() {
6343         $RUNAS $SETSTRIPE -c 2 $MOUNT && error "setstripe should fail"
6344         true
6345 }
6346 run_test 65m "normal user can't set filesystem default stripe"
6347
6348 # bug 2543 - update blocks count on client
6349 test_66() {
6350         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6351         COUNT=${COUNT:-8}
6352         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
6353         sync; sync_all_data; sync; sync_all_data
6354         cancel_lru_locks osc
6355         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
6356         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
6357 }
6358 run_test 66 "update inode blocks count on client ==============="
6359
6360 meminfo() {
6361         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
6362 }
6363
6364 swap_used() {
6365         swapon -s | awk '($1 == "'$1'") { print $4 }'
6366 }
6367
6368 # bug5265, obdfilter oa2dentry return -ENOENT
6369 # #define OBD_FAIL_SRV_ENOENT 0x217
6370 test_69() {
6371         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6372         remote_ost_nodsh && skip "remote OST with nodsh" && return
6373
6374         f="$DIR/$tfile"
6375         $SETSTRIPE -c 1 -i 0 $f
6376
6377         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
6378
6379         do_facet ost1 lctl set_param fail_loc=0x217
6380         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
6381         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
6382
6383         do_facet ost1 lctl set_param fail_loc=0
6384         $DIRECTIO write $f 0 2 || error "write error"
6385
6386         cancel_lru_locks osc
6387         $DIRECTIO read $f 0 1 || error "read error"
6388
6389         do_facet ost1 lctl set_param fail_loc=0x217
6390         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
6391
6392         do_facet ost1 lctl set_param fail_loc=0
6393         rm -f $f
6394 }
6395 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
6396
6397 test_71() {
6398         test_mkdir $DIR/$tdir
6399         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
6400         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
6401 }
6402 run_test 71 "Running dbench on lustre (don't segment fault) ===="
6403
6404 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
6405         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6406         [ "$RUNAS_ID" = "$UID" ] &&
6407                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6408
6409         # Check that testing environment is properly set up. Skip if not
6410         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS || {
6411                 skip_env "User $RUNAS_ID does not exist - skipping"
6412                 return 0
6413         }
6414         touch $DIR/$tfile
6415         chmod 777 $DIR/$tfile
6416         chmod ug+s $DIR/$tfile
6417         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
6418                 error "$RUNAS dd $DIR/$tfile failed"
6419         # See if we are still setuid/sgid
6420         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6421                 error "S/gid is not dropped on write"
6422         # Now test that MDS is updated too
6423         cancel_lru_locks mdc
6424         test -u $DIR/$tfile -o -g $DIR/$tfile &&
6425                 error "S/gid is not dropped on MDS"
6426         rm -f $DIR/$tfile
6427 }
6428 run_test 72a "Test that remove suid works properly (bug5695) ===="
6429
6430 test_72b() { # bug 24226 -- keep mode setting when size is not changing
6431         local perm
6432
6433         [ "$RUNAS_ID" = "$UID" ] && \
6434                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
6435         [ "$RUNAS_ID" -eq 0 ] && \
6436                 skip_env "RUNAS_ID = 0 -- skipping" && return
6437
6438         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6439         # Check that testing environment is properly set up. Skip if not
6440         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS || {
6441                 skip_env "User $RUNAS_ID does not exist - skipping"
6442                 return 0
6443         }
6444         touch $DIR/${tfile}-f{g,u}
6445         test_mkdir $DIR/${tfile}-dg
6446         test_mkdir $DIR/${tfile}-du
6447         chmod 770 $DIR/${tfile}-{f,d}{g,u}
6448         chmod g+s $DIR/${tfile}-{f,d}g
6449         chmod u+s $DIR/${tfile}-{f,d}u
6450         for perm in 777 2777 4777; do
6451                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
6452                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
6453                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
6454                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
6455         done
6456         true
6457 }
6458 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
6459
6460 # bug 3462 - multiple simultaneous MDC requests
6461 test_73() {
6462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6463         test_mkdir $DIR/d73-1
6464         test_mkdir $DIR/d73-2
6465         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
6466         pid1=$!
6467
6468         lctl set_param fail_loc=0x80000129
6469         $MULTIOP $DIR/d73-1/f73-2 Oc &
6470         sleep 1
6471         lctl set_param fail_loc=0
6472
6473         $MULTIOP $DIR/d73-2/f73-3 Oc &
6474         pid3=$!
6475
6476         kill -USR1 $pid1
6477         wait $pid1 || return 1
6478
6479         sleep 25
6480
6481         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
6482         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
6483         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
6484
6485         rm -rf $DIR/d73-*
6486 }
6487 run_test 73 "multiple MDC requests (should not deadlock)"
6488
6489 test_74a() { # bug 6149, 6184
6490         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6491         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6492         #
6493         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6494         # will spin in a tight reconnection loop
6495         touch $DIR/f74a
6496         $LCTL set_param fail_loc=0x8000030e
6497         # get any lock that won't be difficult - lookup works.
6498         ls $DIR/f74a
6499         $LCTL set_param fail_loc=0
6500         rm -f $DIR/f74a
6501         true
6502 }
6503 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
6504
6505 test_74b() { # bug 13310
6506         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6507         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
6508         #
6509         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
6510         # will spin in a tight reconnection loop
6511         $LCTL set_param fail_loc=0x8000030e
6512         # get a "difficult" lock
6513         touch $DIR/f74b
6514         $LCTL set_param fail_loc=0
6515         rm -f $DIR/f74b
6516         true
6517 }
6518 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
6519
6520 test_74c() {
6521         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6522         #define OBD_FAIL_LDLM_NEW_LOCK
6523         $LCTL set_param fail_loc=0x319
6524         touch $DIR/$tfile && error "touch successful"
6525         $LCTL set_param fail_loc=0
6526         true
6527 }
6528 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
6529
6530 num_inodes() {
6531         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
6532 }
6533
6534 test_76() { # Now for bug 20433, added originally in bug 1443
6535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6536         local CPUS=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
6537         cancel_lru_locks osc
6538         BEFORE_INODES=$(num_inodes)
6539         echo "before inodes: $BEFORE_INODES"
6540         local COUNT=1000
6541         [ "$SLOW" = "no" ] && COUNT=100
6542         for i in $(seq $COUNT); do
6543                 touch $DIR/$tfile
6544                 rm -f $DIR/$tfile
6545         done
6546         cancel_lru_locks osc
6547         AFTER_INODES=$(num_inodes)
6548         echo "after inodes: $AFTER_INODES"
6549         local wait=0
6550         while [[ $((AFTER_INODES-1*${CPUS:-1})) -gt $BEFORE_INODES ]]; do
6551                 sleep 2
6552                 AFTER_INODES=$(num_inodes)
6553                 wait=$((wait+2))
6554                 echo "wait $wait seconds inodes: $AFTER_INODES"
6555                 if [ $wait -gt 30 ]; then
6556                         error "inode slab grew from $BEFORE_INODES to $AFTER_INODES"
6557                 fi
6558         done
6559 }
6560 run_test 76 "confirm clients recycle inodes properly ===="
6561
6562
6563 export ORIG_CSUM=""
6564 set_checksums()
6565 {
6566         # Note: in sptlrpc modes which enable its own bulk checksum, the
6567         # original crc32_le bulk checksum will be automatically disabled,
6568         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
6569         # will be checked by sptlrpc code against sptlrpc bulk checksum.
6570         # In this case set_checksums() will not be no-op, because sptlrpc
6571         # bulk checksum will be enabled all through the test.
6572
6573         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
6574         lctl set_param -n osc.*.checksums $1
6575         return 0
6576 }
6577
6578 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
6579                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
6580 CKSUM_TYPES=${CKSUM_TYPES:-"crc32 adler"}
6581 [ "$ORIG_CSUM_TYPE" = "crc32c" ] && CKSUM_TYPES="$CKSUM_TYPES crc32c"
6582 set_checksum_type()
6583 {
6584         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
6585         log "set checksum type to $1"
6586         return 0
6587 }
6588 F77_TMP=$TMP/f77-temp
6589 F77SZ=8
6590 setup_f77() {
6591         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
6592                 error "error writing to $F77_TMP"
6593 }
6594
6595 test_77a() { # bug 10889
6596         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6597         $GSS && skip "could not run with gss" && return
6598         [ ! -f $F77_TMP ] && setup_f77
6599         set_checksums 1
6600         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
6601         set_checksums 0
6602         rm -f $DIR/$tfile
6603 }
6604 run_test 77a "normal checksum read/write operation"
6605
6606 test_77b() { # bug 10889
6607         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6608         $GSS && skip "could not run with gss" && return
6609         [ ! -f $F77_TMP ] && setup_f77
6610         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6611         $LCTL set_param fail_loc=0x80000409
6612         set_checksums 1
6613
6614         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6615                 error "dd error: $?"
6616         $LCTL set_param fail_loc=0
6617
6618         for algo in $CKSUM_TYPES; do
6619                 cancel_lru_locks osc
6620                 set_checksum_type $algo
6621                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6622                 $LCTL set_param fail_loc=0x80000408
6623                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6624                 $LCTL set_param fail_loc=0
6625         done
6626         set_checksums 0
6627         set_checksum_type $ORIG_CSUM_TYPE
6628         rm -f $DIR/$tfile
6629 }
6630 run_test 77b "checksum error on client write, read"
6631
6632 cleanup_77c() {
6633         trap 0
6634         set_checksums 0
6635         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
6636         $check_ost &&
6637                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
6638         [ -n $osc_file_prefix ] && rm -f ${osc_file_prefix}*
6639         $check_ost && [ -n $ost_file_prefix ] &&
6640                 do_facet ost1 rm -f ${ost_file_prefix}\*
6641 }
6642
6643 test_77c() {
6644         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6645         $GSS && skip "could not run with gss" && return
6646
6647         local bad1
6648         local osc_file_prefix
6649         local osc_file
6650         local check_ost=false
6651         local ost_file_prefix
6652         local ost_file
6653         local orig_cksum
6654         local dump_cksum
6655         local fid
6656
6657         # ensure corruption will occur on first OSS/OST
6658         $LFS setstripe -i 0 $DIR/$tfile
6659
6660         [ ! -f $F77_TMP ] && setup_f77
6661         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
6662                 error "dd write error: $?"
6663         fid=$($LFS path2fid $DIR/$tfile)
6664
6665         if [ $(lustre_version_code ost1) -ge $(version_code 2.9.57) ]
6666         then
6667                 check_ost=true
6668                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
6669                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
6670         else
6671                 echo "OSS do not support bulk pages dump upon error"
6672         fi
6673
6674         osc_file_prefix=$($LCTL get_param -n debug_path)
6675         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
6676
6677         trap cleanup_77c EXIT
6678
6679         set_checksums 1
6680         # enable bulk pages dump upon error on Client
6681         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
6682         # enable bulk pages dump upon error on OSS
6683         $check_ost &&
6684                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
6685
6686         # flush Client cache to allow next read to reach OSS
6687         cancel_lru_locks osc
6688
6689         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
6690         $LCTL set_param fail_loc=0x80000408
6691         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
6692         $LCTL set_param fail_loc=0
6693
6694         rm -f $DIR/$tfile
6695
6696         # check cksum dump on Client
6697         osc_file=$(ls ${osc_file_prefix}*)
6698         [ -n "$osc_file" ] || error "no checksum dump file on Client"
6699         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
6700         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
6701         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
6702         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
6703                      cksum)
6704         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
6705         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6706                 error "dump content does not match on Client"
6707
6708         $check_ost || { skip "No need to check cksum dump on OSS"; return 0; }
6709
6710         # check cksum dump on OSS
6711         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
6712         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
6713         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
6714         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
6715         [[ "$orig_cksum" == "$dump_cksum" ]] ||
6716                 error "dump content does not match on OSS"
6717
6718         cleanup_77c
6719 }
6720 run_test 77c "checksum error on client read with debug"
6721
6722 test_77d() { # bug 10889
6723         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6724         $GSS && skip "could not run with gss" && return
6725         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6726         $LCTL set_param fail_loc=0x80000409
6727         set_checksums 1
6728         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6729                 error "direct write: rc=$?"
6730         $LCTL set_param fail_loc=0
6731         set_checksums 0
6732
6733         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
6734         $LCTL set_param fail_loc=0x80000408
6735         set_checksums 1
6736         cancel_lru_locks osc
6737         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
6738                 error "direct read: rc=$?"
6739         $LCTL set_param fail_loc=0
6740         set_checksums 0
6741 }
6742 run_test 77d "checksum error on OST direct write, read"
6743
6744 test_77f() { # bug 10889
6745         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6746         $GSS && skip "could not run with gss" && return
6747         set_checksums 1
6748         for algo in $CKSUM_TYPES; do
6749                 cancel_lru_locks osc
6750                 set_checksum_type $algo
6751                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
6752                 $LCTL set_param fail_loc=0x409
6753                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
6754                         error "direct write succeeded"
6755                 $LCTL set_param fail_loc=0
6756         done
6757         set_checksum_type $ORIG_CSUM_TYPE
6758         set_checksums 0
6759 }
6760 run_test 77f "repeat checksum error on write (expect error)"
6761
6762 test_77g() { # bug 10889
6763         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6764         $GSS && skip "could not run with gss" && return
6765         remote_ost_nodsh && skip "remote OST with nodsh" && return
6766
6767         [ ! -f $F77_TMP ] && setup_f77
6768
6769         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
6770         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
6771         do_facet ost1 lctl set_param fail_loc=0x8000021a
6772         set_checksums 1
6773         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ ||
6774                 error "write error: rc=$?"
6775         do_facet ost1 lctl set_param fail_loc=0
6776         set_checksums 0
6777
6778         cancel_lru_locks osc
6779         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
6780         do_facet ost1 lctl set_param fail_loc=0x8000021b
6781         set_checksums 1
6782         cmp $F77_TMP $DIR/$tfile || error "file compare failed"
6783         do_facet ost1 lctl set_param fail_loc=0
6784         set_checksums 0
6785 }
6786 run_test 77g "checksum error on OST write, read"
6787
6788 test_77j() { # bug 13805
6789         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6790         $GSS && skip "could not run with gss" && return
6791         #define OBD_FAIL_OSC_CKSUM_ADLER_ONLY    0x40c
6792         lctl set_param fail_loc=0x40c
6793         remount_client $MOUNT
6794         lctl set_param fail_loc=0
6795         # wait async osc connect to finish and reflect updated state value
6796         local i
6797         for (( i=0; i < OSTCOUNT; i++ )) ; do
6798                 wait_osc_import_state client ost$((i+1)) FULL
6799         done
6800
6801         for VALUE in $(lctl get_param osc.*osc-[^mM]*.checksum_type); do
6802                 PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
6803                 algo=$(lctl get_param -n $PARAM | sed 's/.*\[\(.*\)\].*/\1/g')
6804                 [ "$algo" = "adler" ] || error "algo set to $algo instead of adler"
6805         done
6806         remount_client $MOUNT
6807 }
6808 run_test 77j "client only supporting ADLER32"
6809
6810 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
6811 rm -f $F77_TMP
6812 unset F77_TMP
6813
6814 cleanup_test_78() {
6815         trap 0
6816         rm -f $DIR/$tfile
6817 }
6818
6819 test_78() { # bug 10901
6820         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6821         remote_ost || { skip_env "local OST" && return; }
6822
6823         NSEQ=5
6824         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
6825         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
6826         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
6827         echo "MemTotal: $MEMTOTAL"
6828
6829         # reserve 256MB of memory for the kernel and other running processes,
6830         # and then take 1/2 of the remaining memory for the read/write buffers.
6831         if [ $MEMTOTAL -gt 512 ] ;then
6832                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
6833         else
6834                 # for those poor memory-starved high-end clusters...
6835                 MEMTOTAL=$((MEMTOTAL / 2))
6836         fi
6837         echo "Mem to use for directio: $MEMTOTAL"
6838
6839         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
6840         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
6841         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
6842         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
6843                 head -n1)
6844         echo "Smallest OST: $SMALLESTOST"
6845         [[ $SMALLESTOST -lt 10240 ]] &&
6846                 skip "too small OSTSIZE, useless to run large O_DIRECT test" && return 0
6847
6848         trap cleanup_test_78 EXIT
6849
6850         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
6851                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
6852
6853         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
6854         echo "File size: $F78SIZE"
6855         $SETSTRIPE -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
6856         for i in $(seq 1 $NSEQ); do
6857                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
6858                 echo directIO rdwr round $i of $NSEQ
6859                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
6860         done
6861
6862         cleanup_test_78
6863 }
6864 run_test 78 "handle large O_DIRECT writes correctly ============"
6865
6866 test_79() { # bug 12743
6867         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6868         wait_delete_completed
6869
6870         BKTOTAL=$(calc_osc_kbytes kbytestotal)
6871         BKFREE=$(calc_osc_kbytes kbytesfree)
6872         BKAVAIL=$(calc_osc_kbytes kbytesavail)
6873
6874         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
6875         DFTOTAL=`echo $STRING | cut -d, -f1`
6876         DFUSED=`echo $STRING  | cut -d, -f2`
6877         DFAVAIL=`echo $STRING | cut -d, -f3`
6878         DFFREE=$(($DFTOTAL - $DFUSED))
6879
6880         ALLOWANCE=$((64 * $OSTCOUNT))
6881
6882         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
6883            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
6884                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
6885         fi
6886         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
6887            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
6888                 error "df free($DFFREE) mismatch OST free($BKFREE)"
6889         fi
6890         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
6891            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
6892                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
6893         fi
6894 }
6895 run_test 79 "df report consistency check ======================="
6896
6897 test_80() { # bug 10718
6898         remote_ost_nodsh && skip "remote OST with nodsh" && return
6899         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6900         # relax strong synchronous semantics for slow backends like ZFS
6901         local soc="obdfilter.*.sync_on_lock_cancel"
6902         local soc_old=$(do_facet ost1 lctl get_param -n $soc | head -n1)
6903         local hosts=
6904         if [ "$soc_old" != "never" -a "$(facet_fstype ost1)" != "ldiskfs" ]; then
6905                 hosts=$(for host in $(seq -f "ost%g" 1 $OSTCOUNT); do
6906                           facet_active_host $host; done | sort -u)
6907                 do_nodes $hosts lctl set_param $soc=never
6908         fi
6909
6910         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
6911         sync; sleep 1; sync
6912         local BEFORE=`date +%s`
6913         cancel_lru_locks osc
6914         local AFTER=`date +%s`
6915         local DIFF=$((AFTER-BEFORE))
6916         if [ $DIFF -gt 1 ] ; then
6917                 error "elapsed for 1M@1T = $DIFF"
6918         fi
6919
6920         [ -n "$hosts" ] && do_nodes $hosts lctl set_param $soc=$soc_old
6921
6922         rm -f $DIR/$tfile
6923 }
6924 run_test 80 "Page eviction is equally fast at high offsets too  ===="
6925
6926 test_81a() { # LU-456
6927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6928         remote_ost_nodsh && skip "remote OST with nodsh" && return
6929         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6930         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
6931         do_facet ost1 lctl set_param fail_loc=0x80000228
6932
6933         # write should trigger a retry and success
6934         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6935         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6936         RC=$?
6937         if [ $RC -ne 0 ] ; then
6938                 error "write should success, but failed for $RC"
6939         fi
6940 }
6941 run_test 81a "OST should retry write when get -ENOSPC ==============="
6942
6943 test_81b() { # LU-456
6944         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
6945         remote_ost_nodsh && skip "remote OST with nodsh" && return
6946         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
6947         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
6948         do_facet ost1 lctl set_param fail_loc=0x228
6949
6950         # write should retry several times and return -ENOSPC finally
6951         $SETSTRIPE -i 0 -c 1 $DIR/$tfile
6952         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
6953         RC=$?
6954         ENOSPC=28
6955         if [ $RC -ne $ENOSPC ] ; then
6956                 error "dd should fail for -ENOSPC, but succeed."
6957         fi
6958 }
6959 run_test 81b "OST should return -ENOSPC when retry still fails ======="
6960
6961 test_82() { # LU-1031
6962         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
6963         local gid1=14091995
6964         local gid2=16022000
6965
6966         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
6967         local MULTIPID1=$!
6968         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
6969         local MULTIPID2=$!
6970         kill -USR1 $MULTIPID2
6971         sleep 2
6972         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
6973                 error "First grouplock does not block second one"
6974         else
6975                 echo "Second grouplock blocks first one"
6976         fi
6977         kill -USR1 $MULTIPID1
6978         wait $MULTIPID1
6979         wait $MULTIPID2
6980 }
6981 run_test 82 "Basic grouplock test"
6982
6983 test_83() {
6984         local sfile="/boot/System.map-$(uname -r)"
6985         # define OBD_FAIL_LLITE_PTASK_IO_FAIL 0x140d
6986         $LCTL set_param fail_loc=0x140d
6987         cp $sfile $DIR/$tfile || error "write failed"
6988         diff -c $sfile $DIR/$tfile || error "files are different"
6989         $LCTL set_param fail_loc=0
6990         rm -f $DIR/$tfile
6991 }
6992 run_test 83 "Short write in ptask ==============================="
6993
6994 test_99() {
6995         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs" &&
6996                 return
6997         test_mkdir $DIR/$tdir.cvsroot
6998         chown $RUNAS_ID $DIR/$tdir.cvsroot
6999
7000         cd $TMP
7001         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
7002
7003         cd /etc/init.d
7004         # some versions of cvs import exit(1) when asked to import links or
7005         # files they can't read.  ignore those files.
7006         local toignore=$(find . -type l -printf '-I %f\n' -o \
7007                          ! -perm /4 -printf '-I %f\n')
7008         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
7009                 $tdir.reposname vtag rtag
7010
7011         cd $DIR
7012         test_mkdir $DIR/$tdir.reposname
7013         chown $RUNAS_ID $DIR/$tdir.reposname
7014         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
7015
7016         cd $DIR/$tdir.reposname
7017         $RUNAS touch foo99
7018         $RUNAS cvs add -m 'addmsg' foo99
7019         $RUNAS cvs update
7020         $RUNAS cvs commit -m 'nomsg' foo99
7021         rm -fr $DIR/$tdir.cvsroot
7022 }
7023 run_test 99 "cvs strange file/directory operations"
7024
7025 test_100() {
7026         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7027         [[ "$NETTYPE" =~ tcp ]] ||
7028                 { skip "TCP secure port test, not useful for NETTYPE=$NETTYPE" &&
7029                         return ; }
7030
7031         remote_ost_nodsh && skip "remote OST with nodsh" && return
7032         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7033         remote_servers ||
7034                 { skip "useless for local single node setup" && return; }
7035
7036         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
7037                 [ "$PROT" != "tcp" ] && continue
7038                 RPORT=$(echo $REMOTE | cut -d: -f2)
7039                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
7040
7041                 rc=0
7042                 LPORT=`echo $LOCAL | cut -d: -f2`
7043                 if [ $LPORT -ge 1024 ]; then
7044                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
7045                         netstat -tna
7046                         error_exit "local: $LPORT > 1024, remote: $RPORT"
7047                 fi
7048         done
7049         [ "$rc" = 0 ] || error_exit "privileged port not found" )
7050 }
7051 run_test 100 "check local port using privileged port ==========="
7052
7053 function get_named_value()
7054 {
7055     local tag
7056
7057     tag=$1
7058     while read ;do
7059         line=$REPLY
7060         case $line in
7061         $tag*)
7062             echo $line | sed "s/^$tag[ ]*//"
7063             break
7064             ;;
7065         esac
7066     done
7067 }
7068
7069 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
7070                    awk '/^max_cached_mb/ { print $2 }')
7071
7072 cleanup_101a() {
7073         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
7074         trap 0
7075 }
7076
7077 test_101a() {
7078         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7079         [ $MDSCOUNT -ge 2 ] && skip "needs < 2 MDTs" && return #LU-4322
7080         local s
7081         local discard
7082         local nreads=10000
7083         local cache_limit=32
7084
7085         $LCTL set_param -n osc.*-osc*.rpc_stats 0
7086         trap cleanup_101a EXIT
7087         $LCTL set_param -n llite.*.read_ahead_stats 0
7088         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
7089
7090         #
7091         # randomly read 10000 of 64K chunks from file 3x 32MB in size
7092         #
7093         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
7094         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
7095
7096         discard=0
7097         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
7098                 get_named_value 'read but discarded' | cut -d" " -f1); do
7099                         discard=$(($discard + $s))
7100         done
7101         cleanup_101a
7102
7103         if [[ $(($discard * 10)) -gt $nreads ]]; then
7104                 $LCTL get_param osc.*-osc*.rpc_stats
7105                 $LCTL get_param llite.*.read_ahead_stats
7106                 error "too many ($discard) discarded pages"
7107         fi
7108         rm -f $DIR/$tfile || true
7109 }
7110 run_test 101a "check read-ahead for random reads"
7111
7112 setup_test101bc() {
7113         test_mkdir $DIR/$tdir
7114         local STRIPE_SIZE=$1
7115         local FILE_LENGTH=$2
7116         STRIPE_OFFSET=0
7117
7118         local FILE_SIZE_MB=$((FILE_LENGTH / STRIPE_SIZE))
7119
7120         local list=$(comma_list $(osts_nodes))
7121         set_osd_param $list '' read_cache_enable 0
7122         set_osd_param $list '' writethrough_cache_enable 0
7123
7124         trap cleanup_test101bc EXIT
7125         # prepare the read-ahead file
7126         $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
7127
7128         dd if=/dev/zero of=$DIR/$tfile bs=$STRIPE_SIZE \
7129                                 count=$FILE_SIZE_MB 2> /dev/null
7130
7131 }
7132
7133 cleanup_test101bc() {
7134         trap 0
7135         rm -rf $DIR/$tdir
7136         rm -f $DIR/$tfile
7137
7138         local list=$(comma_list $(osts_nodes))
7139         set_osd_param $list '' read_cache_enable 1
7140         set_osd_param $list '' writethrough_cache_enable 1
7141 }
7142
7143 calc_total() {
7144         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
7145 }
7146
7147 ra_check_101() {
7148         local READ_SIZE=$1
7149         local STRIPE_SIZE=$2
7150         local FILE_LENGTH=$3
7151         local RA_INC=1048576
7152         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
7153         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
7154                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
7155         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
7156                         get_named_value 'read but discarded' |
7157                         cut -d" " -f1 | calc_total)
7158         if [[ $DISCARD -gt $discard_limit ]]; then
7159                 $LCTL get_param llite.*.read_ahead_stats
7160                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
7161         else
7162                 echo "Read-ahead success for size ${READ_SIZE}"
7163         fi
7164 }
7165
7166 test_101b() {
7167         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7168         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7169         local STRIPE_SIZE=1048576
7170         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
7171         if [ $SLOW == "yes" ]; then
7172                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
7173         else
7174                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
7175         fi
7176
7177         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
7178
7179         # prepare the read-ahead file
7180         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7181         cancel_lru_locks osc
7182         for BIDX in 2 4 8 16 32 64 128 256
7183         do
7184                 local BSIZE=$((BIDX*4096))
7185                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
7186                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
7187                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
7188                 $LCTL set_param -n llite.*.read_ahead_stats 0
7189                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
7190                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
7191                 cancel_lru_locks osc
7192                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
7193         done
7194         cleanup_test101bc
7195         true
7196 }
7197 run_test 101b "check stride-io mode read-ahead ================="
7198
7199 test_101c() {
7200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7201         local STRIPE_SIZE=1048576
7202         local FILE_LENGTH=$((STRIPE_SIZE*100))
7203         local nreads=10000
7204         local osc_rpc_stats
7205
7206         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
7207
7208         cancel_lru_locks osc
7209         $LCTL set_param osc.*.rpc_stats 0
7210         $READS -f $DIR/$tfile -s$FILE_LENGTH -b65536 -n$nreads -t 180
7211         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
7212                 local stats=$($LCTL get_param -n $osc_rpc_stats)
7213                 local lines=$(echo "$stats" | awk 'END {print NR;}')
7214                 local size
7215
7216                 if [ $lines -le 20 ]; then
7217                         continue
7218                 fi
7219                 for size in 1 2 4 8; do
7220                         local rpc=$(echo "$stats" |
7221                                     awk '($1 == "'$size':") {print $2; exit; }')
7222                         [ $rpc != 0 ] &&
7223                                 error "Small $((size*4))k read IO $rpc !"
7224                 done
7225                 echo "$osc_rpc_stats check passed!"
7226         done
7227         cleanup_test101bc
7228         true
7229 }
7230 run_test 101c "check stripe_size aligned read-ahead ================="
7231
7232 set_read_ahead() {
7233         $LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1
7234         $LCTL set_param -n llite.*.max_read_ahead_mb $1 > /dev/null 2>&1
7235 }
7236
7237 test_101d() {
7238         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7239         local file=$DIR/$tfile
7240         local sz_MB=${FILESIZE_101d:-500}
7241         local ra_MB=${READAHEAD_MB:-40}
7242
7243         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
7244         [ $free_MB -lt $sz_MB ] &&
7245                 skip "Need free space ${sz_MB}M, have ${free_MB}M" && return
7246
7247         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
7248         $SETSTRIPE -c -1 $file || error "setstripe failed"
7249
7250         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
7251         echo Cancel LRU locks on lustre client to flush the client cache
7252         cancel_lru_locks osc
7253
7254         echo Disable read-ahead
7255         local old_READAHEAD=$(set_read_ahead 0)
7256
7257         echo Reading the test file $file with read-ahead disabled
7258         local raOFF=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7259
7260         echo Cancel LRU locks on lustre client to flush the client cache
7261         cancel_lru_locks osc
7262         echo Enable read-ahead with ${ra_MB}MB
7263         set_read_ahead $ra_MB
7264
7265         echo Reading the test file $file with read-ahead enabled
7266         local raON=$(do_and_time "dd if=$file of=/dev/null bs=1M count=$sz_MB")
7267
7268         echo "read-ahead disabled time read $raOFF"
7269         echo "read-ahead enabled  time read $raON"
7270
7271         set_read_ahead $old_READAHEAD
7272         rm -f $file
7273         wait_delete_completed
7274
7275         [ $raOFF -le 1 -o $raON -lt $raOFF ] ||
7276                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
7277 }
7278 run_test 101d "file read with and without read-ahead enabled"
7279
7280 test_101e() {
7281         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7282         local file=$DIR/$tfile
7283         local size_KB=500  #KB
7284         local count=100
7285         local bsize=1024
7286
7287         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
7288         local need_KB=$((count * size_KB))
7289         [[ $free_KB -le $need_KB ]] &&
7290                 skip_env "Need free space $need_KB, have $free_KB" && return
7291
7292         echo "Creating $count ${size_KB}K test files"
7293         for ((i = 0; i < $count; i++)); do
7294                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
7295         done
7296
7297         echo "Cancel LRU locks on lustre client to flush the client cache"
7298         cancel_lru_locks $OSC
7299
7300         echo "Reset readahead stats"
7301         $LCTL set_param -n llite.*.read_ahead_stats 0
7302
7303         for ((i = 0; i < $count; i++)); do
7304                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
7305         done
7306
7307         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7308                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
7309
7310         for ((i = 0; i < $count; i++)); do
7311                 rm -rf $file.$i 2>/dev/null
7312         done
7313
7314         #10000 means 20% reads are missing in readahead
7315         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
7316 }
7317 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
7318
7319 test_101f() {
7320         which iozone || { skip "no iozone installed" && return; }
7321
7322         local old_debug=$($LCTL get_param debug)
7323         old_debug=${old_debug#*=}
7324         $LCTL set_param debug="reada mmap"
7325
7326         # create a test file
7327         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
7328
7329         echo Cancel LRU locks on lustre client to flush the client cache
7330         cancel_lru_locks osc
7331
7332         echo Reset readahead stats
7333         $LCTL set_param -n llite.*.read_ahead_stats 0
7334
7335         echo mmap read the file with small block size
7336         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
7337                 > /dev/null 2>&1
7338
7339         echo checking missing pages
7340         $LCTL get_param llite.*.read_ahead_stats
7341         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
7342                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
7343
7344         $LCTL set_param debug="$old_debug"
7345         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
7346         rm -f $DIR/$tfile
7347 }
7348 run_test 101f "check mmap read performance"
7349
7350 test_101g_brw_size_test() {
7351         local mb=$1
7352         local pages=$((mb * 1048576 / $(page_size)))
7353
7354         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
7355                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
7356         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
7357                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
7358                         return 2
7359         done
7360
7361         $LCTL set_param -n osc.*.rpc_stats=0
7362
7363         # 10 RPCs should be enough for the test
7364         local count=10
7365         dd if=/dev/zero of=$DIR/$tfile bs=${mb}M count=$count ||
7366                 { error "dd write ${mb} MB blocks failed"; return 3; }
7367         cancel_lru_locks osc
7368         dd of=/dev/null if=$DIR/$tfile bs=${mb}M count=$count ||
7369                 { error "dd write ${mb} MB blocks failed"; return 4; }
7370
7371         # calculate number of full-sized read and write RPCs
7372         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
7373                 sed -n '/pages per rpc/,/^$/p' |
7374                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
7375                 END { print reads,writes }'))
7376         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
7377                 return 5
7378         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
7379                 return 6
7380
7381         return 0
7382 }
7383
7384 test_101g() {
7385         local rpcs
7386         local osts=$(get_facets OST)
7387         local list=$(comma_list $(osts_nodes))
7388         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
7389         local brw_size="obdfilter.*.brw_size"
7390         local ostver=$(lustre_version_code ost1)
7391         local cliver=$(lustre_version_code client)
7392
7393         $LFS setstripe -i 0 -c 1 $DIR/$tfile
7394
7395         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
7396         if [ $ostver -ge $(version_code 2.8.52) -o \
7397              \( $ostver -ge $(version_code 2.7.17) -a \
7398                 $ostver -lt $(version_code 2.7.50) \) ] &&
7399            [ $cliver -ge $(version_code 2.8.52) -o \
7400              \( $cliver -ge $(version_code 2.7.17) -a \
7401                 $cliver -lt $(version_code 2.7.50) \) ]; then
7402                 [ $ostver -ge $(version_code 2.9.52) ] && suffix="M"
7403                 if [[ $orig_mb -lt 16 ]]; then
7404                         save_lustre_params $osts "$brw_size" > $p
7405                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
7406                                 error "set 16MB RPC size failed"
7407
7408                         echo "remount client to enable new RPC size"
7409                         remount_client $MOUNT || error "remount_client failed"
7410                 fi
7411
7412                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
7413                 # should be able to set brw_size=12, but no rpc_stats for that
7414                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
7415         fi
7416
7417         test_101g_brw_size_test 4 || error "4MB RPC test failed"
7418
7419         if [[ $orig_mb -lt 16 ]]; then
7420                 restore_lustre_params < $p
7421                 remount_client $MOUNT || error "remount_client restore failed"
7422         fi
7423
7424         rm -f $p $DIR/$tfile
7425 }
7426 run_test 101g "Big bulk(4/16 MiB) readahead"
7427
7428 setup_test102() {
7429         test_mkdir $DIR/$tdir
7430         chown $RUNAS_ID $DIR/$tdir
7431         STRIPE_SIZE=65536
7432         STRIPE_OFFSET=1
7433         STRIPE_COUNT=$OSTCOUNT
7434         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
7435
7436         trap cleanup_test102 EXIT
7437         cd $DIR
7438         $1 $SETSTRIPE -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
7439         cd $DIR/$tdir
7440         for num in 1 2 3 4; do
7441                 for count in $(seq 1 $STRIPE_COUNT); do
7442                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
7443                                 local size=`expr $STRIPE_SIZE \* $num`
7444                                 local file=file"$num-$idx-$count"
7445                                 $1 $SETSTRIPE -S $size -i $idx -c $count $file
7446                         done
7447                 done
7448         done
7449
7450         cd $DIR
7451         $1 tar cf $TMP/f102.tar $tdir --xattrs
7452 }
7453
7454 cleanup_test102() {
7455         trap 0
7456         rm -f $TMP/f102.tar
7457         rm -rf $DIR/d0.sanity/d102
7458 }
7459
7460 test_102a() {
7461         local testfile=$DIR/$tfile
7462
7463         touch $testfile
7464
7465         [ "$UID" != 0 ] && skip_env "must run as root" && return
7466         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
7467                 skip_env "must have user_xattr" && return
7468
7469         [ -z "$(which setfattr 2>/dev/null)" ] &&
7470                 skip_env "could not find setfattr" && return
7471
7472         echo "set/get xattr..."
7473         setfattr -n trusted.name1 -v value1 $testfile ||
7474                 error "setfattr -n trusted.name1=value1 $testfile failed"
7475         getfattr -n trusted.name1 $testfile 2> /dev/null |
7476           grep "trusted.name1=.value1" ||
7477                 error "$testfile missing trusted.name1=value1"
7478
7479         setfattr -n user.author1 -v author1 $testfile ||
7480                 error "setfattr -n user.author1=author1 $testfile failed"
7481         getfattr -n user.author1 $testfile 2> /dev/null |
7482           grep "user.author1=.author1" ||
7483                 error "$testfile missing trusted.author1=author1"
7484
7485         echo "listxattr..."
7486         setfattr -n trusted.name2 -v value2 $testfile ||
7487                 error "$testfile unable to set trusted.name2"
7488         setfattr -n trusted.name3 -v value3 $testfile ||
7489                 error "$testfile unable to set trusted.name3"
7490         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
7491             grep "trusted.name" | wc -l) -eq 3 ] ||
7492                 error "$testfile missing 3 trusted.name xattrs"
7493
7494         setfattr -n user.author2 -v author2 $testfile ||
7495                 error "$testfile unable to set user.author2"
7496         setfattr -n user.author3 -v author3 $testfile ||
7497                 error "$testfile unable to set user.author3"
7498         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
7499             grep "user.author" | wc -l) -eq 3 ] ||
7500                 error "$testfile missing 3 user.author xattrs"
7501
7502         echo "remove xattr..."
7503         setfattr -x trusted.name1 $testfile ||
7504                 error "$testfile error deleting trusted.name1"
7505         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
7506                 error "$testfile did not delete trusted.name1 xattr"
7507
7508         setfattr -x user.author1 $testfile ||
7509                 error "$testfile error deleting user.author1"
7510         echo "set lustre special xattr ..."
7511         $LFS setstripe -c1 $testfile
7512         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
7513                 awk -F "=" '/trusted.lov/ { print $2 }' )
7514         setfattr -n "trusted.lov" -v $lovea $testfile ||
7515                 error "$testfile doesn't ignore setting trusted.lov again"
7516         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
7517                 error "$testfile allow setting invalid trusted.lov"
7518         rm -f $testfile
7519 }
7520 run_test 102a "user xattr test =================================="
7521
7522 test_102b() {
7523         [ -z "$(which setfattr 2>/dev/null)" ] &&
7524                 skip_env "could not find setfattr" && return
7525
7526         # b10930: get/set/list trusted.lov xattr
7527         echo "get/set/list trusted.lov xattr ..."
7528         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7529         local testfile=$DIR/$tfile
7530         $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7531                 error "setstripe failed"
7532         local STRIPECOUNT=$($GETSTRIPE -c $testfile) ||
7533                 error "getstripe failed"
7534         getfattr -d -m "^trusted" $testfile 2>/dev/null | grep "trusted.lov" ||
7535                 error "can't get trusted.lov from $testfile"
7536
7537         local testfile2=${testfile}2
7538         local value=$(getfattr -n trusted.lov $testfile 2>/dev/null |
7539                         grep "trusted.lov" | sed -e 's/[^=]\+=//')
7540
7541         $MCREATE $testfile2
7542         setfattr -n trusted.lov -v $value $testfile2
7543         local stripe_size=$($GETSTRIPE -S $testfile2)
7544         local stripe_count=$($GETSTRIPE -c $testfile2)
7545         [[ $stripe_size -eq 65536 ]] ||
7546                 error "stripe size $stripe_size != 65536"
7547         [[ $stripe_count -eq $STRIPECOUNT ]] ||
7548                 error "stripe count $stripe_count != $STRIPECOUNT"
7549         rm -f $DIR/$tfile
7550 }
7551 run_test 102b "getfattr/setfattr for trusted.lov EAs ============"
7552
7553 test_102c() {
7554         [ -z "$(which setfattr 2>/dev/null)" ] &&
7555                 skip_env "could not find setfattr" && return
7556
7557         # b10930: get/set/list lustre.lov xattr
7558         echo "get/set/list lustre.lov xattr ..."
7559         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7560         test_mkdir $DIR/$tdir
7561         chown $RUNAS_ID $DIR/$tdir
7562         local testfile=$DIR/$tdir/$tfile
7563         $RUNAS $SETSTRIPE -S 65536 -i 1 -c $OSTCOUNT $testfile ||
7564                 error "setstripe failed"
7565         local STRIPECOUNT=$($RUNAS $GETSTRIPE -c $testfile) ||
7566                 error "getstripe failed"
7567         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
7568         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
7569
7570         local testfile2=${testfile}2
7571         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
7572                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
7573
7574         $RUNAS $MCREATE $testfile2
7575         $RUNAS setfattr -n lustre.lov -v $value $testfile2
7576         local stripe_size=$($RUNAS $GETSTRIPE -S $testfile2)
7577         local stripe_count=$($RUNAS $GETSTRIPE -c $testfile2)
7578         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
7579         [ $stripe_count -eq $STRIPECOUNT ] ||
7580                 error "stripe count $stripe_count != $STRIPECOUNT"
7581 }
7582 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
7583
7584 compare_stripe_info1() {
7585         local stripe_index_all_zero=true
7586
7587         for num in 1 2 3 4; do
7588                 for count in $(seq 1 $STRIPE_COUNT); do
7589                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
7590                                 local size=$((STRIPE_SIZE * num))
7591                                 local file=file"$num-$offset-$count"
7592                                 stripe_size=$($LFS getstripe -S $PWD/$file)
7593                                 [[ $stripe_size -ne $size ]] &&
7594                                     error "$file: size $stripe_size != $size"
7595                                 stripe_count=$($LFS getstripe -c $PWD/$file)
7596                                 # allow fewer stripes to be created, ORI-601
7597                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
7598                                     error "$file: count $stripe_count != $count"
7599                                 stripe_index=$($LFS getstripe -i $PWD/$file)
7600                                 [[ $stripe_index -ne 0 ]] &&
7601                                         stripe_index_all_zero=false
7602                         done
7603                 done
7604         done
7605         $stripe_index_all_zero &&
7606                 error "all files are being extracted starting from OST index 0"
7607         return 0
7608 }
7609
7610 have_xattrs_include() {
7611         tar --help | grep -q xattrs-include &&
7612                 echo --xattrs-include="lustre.*"
7613 }
7614
7615 test_102d() {
7616         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7617         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7618         XINC=$(have_xattrs_include)
7619         setup_test102
7620         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7621         cd $DIR/$tdir/$tdir
7622         compare_stripe_info1
7623 }
7624 run_test 102d "tar restore stripe info from tarfile,not keep osts"
7625
7626 test_102f() {
7627         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7628         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7629         XINC=$(have_xattrs_include)
7630         setup_test102
7631         test_mkdir $DIR/$tdir.restore
7632         cd $DIR
7633         tar cf - --xattrs $tdir | tar xf - \
7634                 -C $DIR/$tdir.restore --xattrs $XINC
7635         cd $DIR/$tdir.restore/$tdir
7636         compare_stripe_info1
7637 }
7638 run_test 102f "tar copy files, not keep osts"
7639
7640 grow_xattr() {
7641         local xsize=${1:-1024}  # in bytes
7642         local file=$DIR/$tfile
7643
7644         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
7645                 skip "must have user_xattr" && return 0
7646         [ -z "$(which setfattr 2>/dev/null)" ] &&
7647                 skip_env "could not find setfattr" && return 0
7648         [ -z "$(which getfattr 2>/dev/null)" ] &&
7649                 skip_env "could not find getfattr" && return 0
7650
7651         touch $file
7652
7653         local value="$(generate_string $xsize)"
7654
7655         local xbig=trusted.big
7656         log "save $xbig on $file"
7657         setfattr -n $xbig -v $value $file ||
7658                 error "saving $xbig on $file failed"
7659
7660         local orig=$(get_xattr_value $xbig $file)
7661         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
7662
7663         local xsml=trusted.sml
7664         log "save $xsml on $file"
7665         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
7666
7667         local new=$(get_xattr_value $xbig $file)
7668         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
7669
7670         log "grow $xsml on $file"
7671         setfattr -n $xsml -v "$value" $file ||
7672                 error "growing $xsml on $file failed"
7673
7674         new=$(get_xattr_value $xbig $file)
7675         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
7676         log "$xbig still valid after growing $xsml"
7677
7678         rm -f $file
7679 }
7680
7681 test_102h() { # bug 15777
7682         grow_xattr 1024
7683 }
7684 run_test 102h "grow xattr from inside inode to external block"
7685
7686 test_102ha() {
7687         large_xattr_enabled || { skip "large_xattr disabled" && return; }
7688         grow_xattr $(max_xattr_size)
7689 }
7690 run_test 102ha "grow xattr from inside inode to external inode"
7691
7692 test_102i() { # bug 17038
7693         [ -z "$(which getfattr 2>/dev/null)" ] &&
7694                 skip "could not find getfattr" && return
7695         touch $DIR/$tfile
7696         ln -s $DIR/$tfile $DIR/${tfile}link
7697         getfattr -n trusted.lov $DIR/$tfile ||
7698                 error "lgetxattr on $DIR/$tfile failed"
7699         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
7700                 grep -i "no such attr" ||
7701                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
7702         rm -f $DIR/$tfile $DIR/${tfile}link
7703 }
7704 run_test 102i "lgetxattr test on symbolic link ============"
7705
7706 test_102j() {
7707         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
7708         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
7709         XINC=$(have_xattrs_include)
7710         setup_test102 "$RUNAS"
7711         chown $RUNAS_ID $DIR/$tdir
7712         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
7713         cd $DIR/$tdir/$tdir
7714         compare_stripe_info1 "$RUNAS"
7715 }
7716 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
7717
7718 test_102k() {
7719         [ -z "$(which setfattr 2>/dev/null)" ] &&
7720                 skip "could not find setfattr" && return
7721         touch $DIR/$tfile
7722         # b22187 just check that does not crash for regular file.
7723         setfattr -n trusted.lov $DIR/$tfile
7724         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
7725         local test_kdir=$DIR/$tdir
7726         test_mkdir $test_kdir
7727         local default_size=$($LFS getstripe -S $test_kdir)
7728         local default_count=$($LFS getstripe -c $test_kdir)
7729         local default_offset=$($LFS getstripe -i $test_kdir)
7730         $SETSTRIPE -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
7731                 error 'dir setstripe failed'
7732         setfattr -n trusted.lov $test_kdir
7733         local stripe_size=$($LFS getstripe -S $test_kdir)
7734         local stripe_count=$($LFS getstripe -c $test_kdir)
7735         local stripe_offset=$($LFS getstripe -i $test_kdir)
7736         [ $stripe_size -eq $default_size ] ||
7737                 error "stripe size $stripe_size != $default_size"
7738         [ $stripe_count -eq $default_count ] ||
7739                 error "stripe count $stripe_count != $default_count"
7740         [ $stripe_offset -eq $default_offset ] ||
7741                 error "stripe offset $stripe_offset != $default_offset"
7742         rm -rf $DIR/$tfile $test_kdir
7743 }
7744 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
7745
7746 test_102l() {
7747         [ -z "$(which getfattr 2>/dev/null)" ] &&
7748                 skip "could not find getfattr" && return
7749
7750         # LU-532 trusted. xattr is invisible to non-root
7751         local testfile=$DIR/$tfile
7752
7753         touch $testfile
7754
7755         echo "listxattr as user..."
7756         chown $RUNAS_ID $testfile
7757         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
7758             grep -q "trusted" &&
7759                 error "$testfile trusted xattrs are user visible"
7760
7761         return 0;
7762 }
7763 run_test 102l "listxattr size test =================================="
7764
7765 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
7766         local path=$DIR/$tfile
7767         touch $path
7768
7769         listxattr_size_check $path || error "listattr_size_check $path failed"
7770 }
7771 run_test 102m "Ensure listxattr fails on small bufffer ========"
7772
7773 cleanup_test102
7774
7775 getxattr() { # getxattr path name
7776         # Return the base64 encoding of the value of xattr name on path.
7777         local path=$1
7778         local name=$2
7779
7780         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
7781         # file: $path
7782         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7783         #
7784         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
7785
7786         getfattr --absolute-names --encoding=base64 --name=$name $path |
7787                 awk -F= -v name=$name '$1 == name {
7788                         print substr($0, index($0, "=") + 1);
7789         }'
7790 }
7791
7792 test_102n() { # LU-4101 mdt: protect internal xattrs
7793         [ -z "$(which setfattr 2>/dev/null)" ] &&
7794                 skip "could not find setfattr" && return
7795         if [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.50) ]
7796         then
7797                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
7798                 return
7799         fi
7800
7801         local file0=$DIR/$tfile.0
7802         local file1=$DIR/$tfile.1
7803         local xattr0=$TMP/$tfile.0
7804         local xattr1=$TMP/$tfile.1
7805         local namelist="lov lma lmv link fid version som hsm"
7806         local name
7807         local value
7808
7809         rm -rf $file0 $file1 $xattr0 $xattr1
7810         touch $file0 $file1
7811
7812         # Get 'before' xattrs of $file1.
7813         getfattr --absolute-names --dump --match=- $file1 > $xattr0
7814
7815         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
7816                 namelist+=" lfsck_namespace"
7817         for name in $namelist; do
7818                 # Try to copy xattr from $file0 to $file1.
7819                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7820
7821                 setfattr --name=trusted.$name --value="$value" $file1 ||
7822                         error "setxattr 'trusted.$name' failed"
7823
7824                 # Try to set a garbage xattr.
7825                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7826
7827                 if [[ x$name == "xlov" ]]; then
7828                         setfattr --name=trusted.lov --value="$value" $file1 &&
7829                         error "setxattr invalid 'trusted.lov' success"
7830                 else
7831                         setfattr --name=trusted.$name --value="$value" $file1 ||
7832                                 error "setxattr invalid 'trusted.$name' failed"
7833                 fi
7834
7835                 # Try to remove the xattr from $file1. We don't care if this
7836                 # appears to succeed or fail, we just don't want there to be
7837                 # any changes or crashes.
7838                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7839         done
7840
7841         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.6.50) ]
7842         then
7843                 name="lfsck_ns"
7844                 # Try to copy xattr from $file0 to $file1.
7845                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
7846
7847                 setfattr --name=trusted.$name --value="$value" $file1 ||
7848                         error "setxattr 'trusted.$name' failed"
7849
7850                 # Try to set a garbage xattr.
7851                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
7852
7853                 setfattr --name=trusted.$name --value="$value" $file1 ||
7854                         error "setxattr 'trusted.$name' failed"
7855
7856                 # Try to remove the xattr from $file1. We don't care if this
7857                 # appears to succeed or fail, we just don't want there to be
7858                 # any changes or crashes.
7859                 setfattr --remove=$trusted.$name $file1 2> /dev/null
7860         fi
7861
7862         # Get 'after' xattrs of file1.
7863         getfattr --absolute-names --dump --match=- $file1 > $xattr1
7864
7865         if ! diff $xattr0 $xattr1; then
7866                 error "before and after xattrs of '$file1' differ"
7867         fi
7868
7869         rm -rf $file0 $file1 $xattr0 $xattr1
7870
7871         return 0
7872 }
7873 run_test 102n "silently ignore setxattr on internal trusted xattrs"
7874
7875 test_102p() { # LU-4703 setxattr did not check ownership
7876         local testfile=$DIR/$tfile
7877
7878         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.56) ] &&
7879                 skip "MDS needs to be at least 2.5.56" && return
7880
7881         touch $testfile
7882
7883         echo "setfacl as user..."
7884         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
7885         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
7886
7887         echo "setfattr as user..."
7888         setfacl -m "u:$RUNAS_ID:---" $testfile
7889         $RUNAS setfattr -x system.posix_acl_access $testfile
7890         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
7891 }
7892 run_test 102p "check setxattr(2) correctly fails without permission"
7893
7894 test_102q() {
7895         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) ] &&
7896                 skip "MDS needs to be at least 2.6.92" && return
7897         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
7898 }
7899 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
7900
7901 test_102r() {
7902         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.93) ] &&
7903                 skip "MDS needs to be at least 2.6.93" && return
7904         touch $DIR/$tfile || error "touch"
7905         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
7906         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
7907         rm $DIR/$tfile || error "rm"
7908
7909         #normal directory
7910         mkdir -p $DIR/$tdir || error "mkdir"
7911         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7912         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7913         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7914                 error "$testfile error deleting user.author1"
7915         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7916                 grep "user.$(basename $tdir)" &&
7917                 error "$tdir did not delete user.$(basename $tdir)"
7918         rmdir $DIR/$tdir || error "rmdir"
7919
7920         #striped directory
7921         test_mkdir $DIR/$tdir
7922         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
7923         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
7924         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
7925                 error "$testfile error deleting user.author1"
7926         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
7927                 grep "user.$(basename $tdir)" &&
7928                 error "$tdir did not delete user.$(basename $tdir)"
7929         rmdir $DIR/$tdir || error "rm striped dir"
7930 }
7931 run_test 102r "set EAs with empty values"
7932
7933 run_acl_subtest()
7934 {
7935     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
7936     return $?
7937 }
7938
7939 test_103a() {
7940         [ "$UID" != 0 ] && skip_env "must run as root" && return
7941         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
7942                 skip "must have acl enabled" && return
7943         [ -z "$(which setfacl 2>/dev/null)" ] &&
7944                 skip_env "could not find setfacl" && return
7945         $GSS && skip "could not run under gss" && return
7946         remote_mds_nodsh && skip "remote MDS with nodsh" && return
7947
7948         gpasswd -a daemon bin                           # LU-5641
7949         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
7950
7951         declare -a identity_old
7952
7953         for num in $(seq $MDSCOUNT); do
7954                 switch_identity $num true || identity_old[$num]=$?
7955         done
7956
7957         SAVE_UMASK=$(umask)
7958         umask 0022
7959         mkdir -p $DIR/$tdir
7960         cd $DIR/$tdir
7961
7962         echo "performing cp ..."
7963         run_acl_subtest cp || error "run_acl_subtest cp failed"
7964         echo "performing getfacl-noacl..."
7965         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
7966         echo "performing misc..."
7967         run_acl_subtest misc || error  "misc test failed"
7968         echo "performing permissions..."
7969         run_acl_subtest permissions || error "permissions failed"
7970         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
7971         if [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.8.55) -o \
7972              \( $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6) -a \
7973              $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.29) \) ]
7974         then
7975                 echo "performing permissions xattr..."
7976                 run_acl_subtest permissions_xattr ||
7977                         error "permissions_xattr failed"
7978         fi
7979         echo "performing setfacl..."
7980         run_acl_subtest setfacl || error  "setfacl test failed"
7981
7982         # inheritance test got from HP
7983         echo "performing inheritance..."
7984         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
7985         chmod +x make-tree || error "chmod +x failed"
7986         run_acl_subtest inheritance || error "inheritance test failed"
7987         rm -f make-tree
7988
7989         echo "LU-974 ignore umask when acl is enabled..."
7990         run_acl_subtest 974 || error "LU-974 umask test failed"
7991         if [ $MDSCOUNT -ge 2 ]; then
7992                 run_acl_subtest 974_remote ||
7993                         error "LU-974 umask test failed under remote dir"
7994         fi
7995
7996         echo "LU-2561 newly created file is same size as directory..."
7997         if [ $(facet_fstype $SINGLEMDS) != "zfs" ]; then
7998                 run_acl_subtest 2561 || error "LU-2561 test failed"
7999         else
8000                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
8001         fi
8002
8003         run_acl_subtest 4924 || error "LU-4924 test failed"
8004
8005         cd $SAVE_PWD
8006         umask $SAVE_UMASK
8007
8008         for num in $(seq $MDSCOUNT); do
8009                 if [ "${identity_old[$num]}" = 1 ]; then
8010                         switch_identity $num false || identity_old[$num]=$?
8011                 fi
8012         done
8013 }
8014 run_test 103a "acl test ========================================="
8015
8016 test_103c() {
8017         mkdir -p $DIR/$tdir
8018         cp -rp $DIR/$tdir $DIR/$tdir.bak
8019
8020         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
8021                 error "$DIR/$tdir shouldn't contain default ACL"
8022         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
8023                 error "$DIR/$tdir.bak shouldn't contain default ACL"
8024         true
8025 }
8026 run_test 103c "'cp -rp' won't set empty acl"
8027
8028 test_104a() {
8029         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8030         touch $DIR/$tfile
8031         lfs df || error "lfs df failed"
8032         lfs df -ih || error "lfs df -ih failed"
8033         lfs df -h $DIR || error "lfs df -h $DIR failed"
8034         lfs df -i $DIR || error "lfs df -i $DIR failed"
8035         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
8036         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
8037
8038         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
8039         lctl --device %$OSC deactivate
8040         lfs df || error "lfs df with deactivated OSC failed"
8041         lctl --device %$OSC activate
8042         # wait the osc back to normal
8043         wait_osc_import_state client ost FULL
8044
8045         lfs df || error "lfs df with reactivated OSC failed"
8046         rm -f $DIR/$tfile
8047 }
8048 run_test 104a "lfs df [-ih] [path] test ========================="
8049
8050 test_104b() {
8051         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8052         [ $RUNAS_ID -eq $UID ] &&
8053                 skip_env "RUNAS_ID = UID = $UID -- skipping" && return
8054         chmod 666 /dev/obd
8055         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
8056                         grep "Permission denied" | wc -l)))
8057         if [ $denied_cnt -ne 0 ]; then
8058                 error "lfs check servers test failed"
8059         fi
8060 }
8061 run_test 104b "$RUNAS lfs check servers test ===================="
8062
8063 test_105a() {
8064         # doesn't work on 2.4 kernels
8065         touch $DIR/$tfile
8066         if $(flock_is_enabled); then
8067                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
8068         else
8069                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
8070         fi
8071         rm -f $DIR/$tfile
8072 }
8073 run_test 105a "flock when mounted without -o flock test ========"
8074
8075 test_105b() {
8076         touch $DIR/$tfile
8077         if $(flock_is_enabled); then
8078                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
8079         else
8080                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
8081         fi
8082         rm -f $DIR/$tfile
8083 }
8084 run_test 105b "fcntl when mounted without -o flock test ========"
8085
8086 test_105c() {
8087         touch $DIR/$tfile
8088         if $(flock_is_enabled); then
8089                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
8090         else
8091                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
8092         fi
8093         rm -f $DIR/$tfile
8094 }
8095 run_test 105c "lockf when mounted without -o flock test"
8096
8097 test_105d() { # bug 15924
8098         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8099         test_mkdir $DIR/$tdir
8100         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
8101         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
8102         $LCTL set_param fail_loc=0x80000315
8103         flocks_test 2 $DIR/$tdir
8104 }
8105 run_test 105d "flock race (should not freeze) ========"
8106
8107 test_105e() { # bug 22660 && 22040
8108         flock_is_enabled || { skip "mount w/o flock enabled" && return; }
8109         touch $DIR/$tfile
8110         flocks_test 3 $DIR/$tfile
8111 }
8112 run_test 105e "Two conflicting flocks from same process"
8113
8114 test_106() { #bug 10921
8115         test_mkdir $DIR/$tdir
8116         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
8117         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
8118 }
8119 run_test 106 "attempt exec of dir followed by chown of that dir"
8120
8121 test_107() {
8122         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8123         CDIR=`pwd`
8124         cd $DIR
8125
8126         local file=core
8127         rm -f $file
8128
8129         local save_pattern=$(sysctl -n kernel.core_pattern)
8130         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
8131         sysctl -w kernel.core_pattern=$file
8132         sysctl -w kernel.core_uses_pid=0
8133
8134         ulimit -c unlimited
8135         sleep 60 &
8136         SLEEPPID=$!
8137
8138         sleep 1
8139
8140         kill -s 11 $SLEEPPID
8141         wait $SLEEPPID
8142         if [ -e $file ]; then
8143                 size=`stat -c%s $file`
8144                 [ $size -eq 0 ] && error "Fail to create core file $file"
8145         else
8146                 error "Fail to create core file $file"
8147         fi
8148         rm -f $file
8149         sysctl -w kernel.core_pattern=$save_pattern
8150         sysctl -w kernel.core_uses_pid=$save_uses_pid
8151         cd $CDIR
8152 }
8153 run_test 107 "Coredump on SIG"
8154
8155 test_110() {
8156         test_mkdir $DIR/$tdir
8157         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
8158         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
8159                 error "mkdir with 256 char should fail, but did not"
8160         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
8161                 error "create with 255 char failed"
8162         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
8163                 error "create with 256 char should fail, but did not"
8164
8165         ls -l $DIR/$tdir
8166         rm -rf $DIR/$tdir
8167 }
8168 run_test 110 "filename length checking"
8169
8170 #
8171 # Purpose: To verify dynamic thread (OSS) creation.
8172 #
8173 test_115() {
8174         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8175         remote_ost_nodsh && skip "remote OST with nodsh" && return
8176
8177         # Lustre does not stop service threads once they are started.
8178         # Reset number of running threads to default.
8179         stopall
8180         setupall
8181
8182         local OSTIO_pre
8183         local save_params="$TMP/sanity-$TESTNAME.parameters"
8184
8185         # Get ll_ost_io count before I/O
8186         OSTIO_pre=$(do_facet ost1 \
8187                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
8188         # Exit if lustre is not running (ll_ost_io not running).
8189         [ -z "$OSTIO_pre" ] && error "no OSS threads"
8190
8191         echo "Starting with $OSTIO_pre threads"
8192         local thread_max=$((OSTIO_pre * 2))
8193         local rpc_in_flight=$((thread_max * 2))
8194         # Number of I/O Process proposed to be started.
8195         local nfiles
8196         local facets=$(get_facets OST)
8197
8198         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
8199         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
8200
8201         # Set in_flight to $rpc_in_flight
8202         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
8203                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
8204         nfiles=${rpc_in_flight}
8205         # Set ost thread_max to $thread_max
8206         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
8207
8208         # 5 Minutes should be sufficient for max number of OSS
8209         # threads(thread_max) to be created.
8210         local timeout=300
8211
8212         # Start I/O.
8213         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
8214         test_mkdir $DIR/$tdir
8215         for i in $(seq $nfiles); do
8216                 local file=$DIR/$tdir/${tfile}-$i
8217                 $LFS setstripe -c -1 -i 0 $file
8218                 ($WTL $file $timeout)&
8219         done
8220
8221         # I/O Started - Wait for thread_started to reach thread_max or report
8222         # error if thread_started is more than thread_max.
8223         echo "Waiting for thread_started to reach thread_max"
8224         local thread_started=0
8225         local end_time=$((SECONDS + timeout))
8226
8227         while [ $SECONDS -le $end_time ] ; do
8228                 echo -n "."
8229                 # Get ost i/o thread_started count.
8230                 thread_started=$(do_facet ost1 \
8231                         "$LCTL get_param \
8232                         ost.OSS.ost_io.threads_started | cut -d= -f2")
8233                 # Break out if thread_started is equal/greater than thread_max
8234                 if [[ $thread_started -ge $thread_max ]]; then
8235                         echo ll_ost_io thread_started $thread_started, \
8236                                 equal/greater than thread_max $thread_max
8237                         break
8238                 fi
8239                 sleep 1
8240         done
8241
8242         # Cleanup - We have the numbers, Kill i/o jobs if running.
8243         jobcount=($(jobs -p))
8244         for i in $(seq 0 $((${#jobcount[@]}-1)))
8245         do
8246                 kill -9 ${jobcount[$i]}
8247                 if [ $? -ne 0 ] ; then
8248                         echo Warning: \
8249                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
8250                 fi
8251         done
8252
8253         # Cleanup files left by WTL binary.
8254         for i in $(seq $nfiles); do
8255                 local file=$DIR/$tdir/${tfile}-$i
8256                 rm -rf $file
8257                 if [ $? -ne 0 ] ; then
8258                         echo "Warning: Failed to delete file $file"
8259                 fi
8260         done
8261
8262         restore_lustre_params <$save_params
8263         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
8264
8265         # Error out if no new thread has started or Thread started is greater
8266         # than thread max.
8267         if [[ $thread_started -le $OSTIO_pre ||
8268                         $thread_started -gt $thread_max ]]; then
8269                 error "ll_ost_io: thread_started $thread_started" \
8270                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
8271                       "No new thread started or thread started greater " \
8272                       "than thread_max."
8273         fi
8274 }
8275 run_test 115 "verify dynamic thread creation===================="
8276
8277 free_min_max () {
8278         wait_delete_completed
8279         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
8280         echo "OST kbytes available: ${AVAIL[@]}"
8281         MAXV=${AVAIL[0]}
8282         MAXI=0
8283         MINV=${AVAIL[0]}
8284         MINI=0
8285         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
8286                 #echo OST $i: ${AVAIL[i]}kb
8287                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
8288                         MAXV=${AVAIL[i]}
8289                         MAXI=$i
8290                 fi
8291                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
8292                         MINV=${AVAIL[i]}
8293                         MINI=$i
8294                 fi
8295         done
8296         echo "Min free space: OST $MINI: $MINV"
8297         echo "Max free space: OST $MAXI: $MAXV"
8298 }
8299
8300 test_116a() { # was previously test_116()
8301         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8302         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8303
8304         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8305
8306         echo -n "Free space priority "
8307         do_facet $SINGLEMDS lctl get_param -n lo*.*-mdtlov.qos_prio_free |
8308                 head -n1
8309         declare -a AVAIL
8310         free_min_max
8311
8312         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip" && return
8313         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip" \
8314                 && return
8315         trap simple_cleanup_common EXIT
8316
8317
8318         # Check if we need to generate uneven OSTs
8319         test_mkdir -p $DIR/$tdir/OST${MINI}
8320         local FILL=$((MINV / 4))
8321         local DIFF=$((MAXV - MINV))
8322         local DIFF2=$((DIFF * 100 / MINV))
8323
8324         local threshold=$(do_facet $SINGLEMDS \
8325                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
8326         threshold=${threshold%%%}
8327         echo -n "Check for uneven OSTs: "
8328         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
8329
8330         if [[ $DIFF2 -gt $threshold ]]; then
8331                 echo "ok"
8332                 echo "Don't need to fill OST$MINI"
8333         else
8334                 # generate uneven OSTs. Write 2% over the QOS threshold value
8335                 echo "no"
8336                 DIFF=$((threshold - DIFF2 + 2))
8337                 DIFF2=$((MINV * DIFF / 100))
8338                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
8339                 $SETSTRIPE -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
8340                         error "setstripe failed"
8341                 DIFF=$((DIFF2 / 2048))
8342                 i=0
8343                 while [ $i -lt $DIFF ]; do
8344                         i=$((i + 1))
8345                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
8346                                 bs=2M count=1 2>/dev/null
8347                         echo -n .
8348                 done
8349                 echo .
8350                 sync
8351                 sleep_maxage
8352                 free_min_max
8353         fi
8354
8355         DIFF=$((MAXV - MINV))
8356         DIFF2=$((DIFF * 100 / MINV))
8357         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
8358         if [ $DIFF2 -gt $threshold ]; then
8359                 echo "ok"
8360         else
8361                 echo "failed - QOS mode won't be used"
8362                 skip "QOS imbalance criteria not met"
8363                 simple_cleanup_common
8364                 return
8365         fi
8366
8367         MINI1=$MINI
8368         MINV1=$MINV
8369         MAXI1=$MAXI
8370         MAXV1=$MAXV
8371
8372         # now fill using QOS
8373         $SETSTRIPE -c 1 $DIR/$tdir
8374         FILL=$((FILL / 200))
8375         if [ $FILL -gt 600 ]; then
8376                 FILL=600
8377         fi
8378         echo "writing $FILL files to QOS-assigned OSTs"
8379         i=0
8380         while [ $i -lt $FILL ]; do
8381                 i=$((i + 1))
8382                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
8383                         count=1 2>/dev/null
8384                 echo -n .
8385         done
8386         echo "wrote $i 200k files"
8387         sync
8388         sleep_maxage
8389
8390         echo "Note: free space may not be updated, so measurements might be off"
8391         free_min_max
8392         DIFF2=$((MAXV - MINV))
8393         echo "free space delta: orig $DIFF final $DIFF2"
8394         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
8395         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
8396         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
8397         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
8398         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
8399         if [[ $DIFF -gt 0 ]]; then
8400                 FILL=$((DIFF2 * 100 / DIFF - 100))
8401                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
8402         fi
8403
8404         # Figure out which files were written where
8405         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8406                awk '/'$MINI1': / {print $2; exit}')
8407         echo $UUID
8408         MINC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8409         echo "$MINC files created on smaller OST $MINI1"
8410         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
8411                awk '/'$MAXI1': / {print $2; exit}')
8412         echo $UUID
8413         MAXC=$($GETSTRIPE --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
8414         echo "$MAXC files created on larger OST $MAXI1"
8415         if [[ $MINC -gt 0 ]]; then
8416                 FILL=$((MAXC * 100 / MINC - 100))
8417                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
8418         fi
8419         [[ $MAXC -gt $MINC ]] ||
8420                 error_ignore LU-9 "stripe QOS didn't balance free space"
8421         simple_cleanup_common
8422 }
8423 run_test 116a "stripe QOS: free space balance ==================="
8424
8425 test_116b() { # LU-2093
8426         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8427         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8428
8429 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
8430         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
8431                        lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
8432         [ -z "$old_rr" ] && skip "no QOS" && return 0
8433         do_facet $SINGLEMDS lctl set_param \
8434                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
8435         mkdir -p $DIR/$tdir
8436         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
8437         createmany -o $DIR/$tdir/f- 20 || error "can't create"
8438         do_facet $SINGLEMDS lctl set_param fail_loc=0
8439         rm -rf $DIR/$tdir
8440         do_facet $SINGLEMDS lctl set_param \
8441                 lo*.$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
8442 }
8443 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
8444
8445 test_117() # bug 10891
8446 {
8447         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8448         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
8449         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
8450         lctl set_param fail_loc=0x21e
8451         > $DIR/$tfile || error "truncate failed"
8452         lctl set_param fail_loc=0
8453         echo "Truncate succeeded."
8454         rm -f $DIR/$tfile
8455 }
8456 run_test 117 "verify osd extend =========="
8457
8458 NO_SLOW_RESENDCOUNT=4
8459 export OLD_RESENDCOUNT=""
8460 set_resend_count () {
8461         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
8462         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
8463         lctl set_param -n $PROC_RESENDCOUNT $1
8464         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
8465 }
8466
8467 # for reduce test_118* time (b=14842)
8468 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8469
8470 # Reset async IO behavior after error case
8471 reset_async() {
8472         FILE=$DIR/reset_async
8473
8474         # Ensure all OSCs are cleared
8475         $SETSTRIPE -c -1 $FILE
8476         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
8477         sync
8478         rm $FILE
8479 }
8480
8481 test_118a() #bug 11710
8482 {
8483         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8484         reset_async
8485
8486         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8487         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8488         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8489
8490         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8491                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8492                 return 1;
8493         fi
8494         rm -f $DIR/$tfile
8495 }
8496 run_test 118a "verify O_SYNC works =========="
8497
8498 test_118b()
8499 {
8500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8501         remote_ost_nodsh && skip "remote OST with nodsh" && return
8502
8503         reset_async
8504
8505         #define OBD_FAIL_SRV_ENOENT 0x217
8506         set_nodes_failloc "$(osts_nodes)" 0x217
8507         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8508         RC=$?
8509         set_nodes_failloc "$(osts_nodes)" 0
8510         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8511         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8512                     grep -c writeback)
8513
8514         if [[ $RC -eq 0 ]]; then
8515                 error "Must return error due to dropped pages, rc=$RC"
8516                 return 1;
8517         fi
8518
8519         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8520                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8521                 return 1;
8522         fi
8523
8524         echo "Dirty pages not leaked on ENOENT"
8525
8526         # Due to the above error the OSC will issue all RPCs syncronously
8527         # until a subsequent RPC completes successfully without error.
8528         $MULTIOP $DIR/$tfile Ow4096yc
8529         rm -f $DIR/$tfile
8530
8531         return 0
8532 }
8533 run_test 118b "Reclaim dirty pages on fatal error =========="
8534
8535 test_118c()
8536 {
8537         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8538
8539         # for 118c, restore the original resend count, LU-1940
8540         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
8541                                 set_resend_count $OLD_RESENDCOUNT
8542         remote_ost_nodsh && skip "remote OST with nodsh" && return
8543
8544         reset_async
8545
8546         #define OBD_FAIL_OST_EROFS               0x216
8547         set_nodes_failloc "$(osts_nodes)" 0x216
8548
8549         # multiop should block due to fsync until pages are written
8550         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8551         MULTIPID=$!
8552         sleep 1
8553
8554         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8555                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8556         fi
8557
8558         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8559                     grep -c writeback)
8560         if [[ $WRITEBACK -eq 0 ]]; then
8561                 error "No page in writeback, writeback=$WRITEBACK"
8562         fi
8563
8564         set_nodes_failloc "$(osts_nodes)" 0
8565         wait $MULTIPID
8566         RC=$?
8567         if [[ $RC -ne 0 ]]; then
8568                 error "Multiop fsync failed, rc=$RC"
8569         fi
8570
8571         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8572         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8573                     grep -c writeback)
8574         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8575                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8576         fi
8577
8578         rm -f $DIR/$tfile
8579         echo "Dirty pages flushed via fsync on EROFS"
8580         return 0
8581 }
8582 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
8583
8584 # continue to use small resend count to reduce test_118* time (b=14842)
8585 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
8586
8587 test_118d()
8588 {
8589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8590         remote_ost_nodsh && skip "remote OST with nodsh" && return
8591
8592         reset_async
8593
8594         #define OBD_FAIL_OST_BRW_PAUSE_BULK
8595         set_nodes_failloc "$(osts_nodes)" 0x214
8596         # multiop should block due to fsync until pages are written
8597         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8598         MULTIPID=$!
8599         sleep 1
8600
8601         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
8602                 error "Multiop failed to block on fsync, pid=$MULTIPID"
8603         fi
8604
8605         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8606                     grep -c writeback)
8607         if [[ $WRITEBACK -eq 0 ]]; then
8608                 error "No page in writeback, writeback=$WRITEBACK"
8609         fi
8610
8611         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
8612         set_nodes_failloc "$(osts_nodes)" 0
8613
8614         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8615         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8616                     grep -c writeback)
8617         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8618                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8619         fi
8620
8621         rm -f $DIR/$tfile
8622         echo "Dirty pages gaurenteed flushed via fsync"
8623         return 0
8624 }
8625 run_test 118d "Fsync validation inject a delay of the bulk =========="
8626
8627 test_118f() {
8628         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8629         reset_async
8630
8631         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
8632         lctl set_param fail_loc=0x8000040a
8633
8634         # Should simulate EINVAL error which is fatal
8635         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8636         RC=$?
8637         if [[ $RC -eq 0 ]]; then
8638                 error "Must return error due to dropped pages, rc=$RC"
8639         fi
8640
8641         lctl set_param fail_loc=0x0
8642
8643         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8644         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8645         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8646                     grep -c writeback)
8647         if [[ $LOCKED -ne 0 ]]; then
8648                 error "Locked pages remain in cache, locked=$LOCKED"
8649         fi
8650
8651         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8652                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8653         fi
8654
8655         rm -f $DIR/$tfile
8656         echo "No pages locked after fsync"
8657
8658         reset_async
8659         return 0
8660 }
8661 run_test 118f "Simulate unrecoverable OSC side error =========="
8662
8663 test_118g() {
8664         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8665         reset_async
8666
8667         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8668         lctl set_param fail_loc=0x406
8669
8670         # simulate local -ENOMEM
8671         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8672         RC=$?
8673
8674         lctl set_param fail_loc=0
8675         if [[ $RC -eq 0 ]]; then
8676                 error "Must return error due to dropped pages, rc=$RC"
8677         fi
8678
8679         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8680         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8681         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8682                         grep -c writeback)
8683         if [[ $LOCKED -ne 0 ]]; then
8684                 error "Locked pages remain in cache, locked=$LOCKED"
8685         fi
8686
8687         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8688                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8689         fi
8690
8691         rm -f $DIR/$tfile
8692         echo "No pages locked after fsync"
8693
8694         reset_async
8695         return 0
8696 }
8697 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
8698
8699 test_118h() {
8700         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8701         remote_ost_nodsh && skip "remote OST with nodsh" && return
8702
8703         reset_async
8704
8705         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8706         set_nodes_failloc "$(osts_nodes)" 0x20e
8707         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8708         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8709         RC=$?
8710
8711         set_nodes_failloc "$(osts_nodes)" 0
8712         if [[ $RC -eq 0 ]]; then
8713                 error "Must return error due to dropped pages, rc=$RC"
8714         fi
8715
8716         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8717         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8718         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
8719                     grep -c writeback)
8720         if [[ $LOCKED -ne 0 ]]; then
8721                 error "Locked pages remain in cache, locked=$LOCKED"
8722         fi
8723
8724         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8725                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8726         fi
8727
8728         rm -f $DIR/$tfile
8729         echo "No pages locked after fsync"
8730
8731         return 0
8732 }
8733 run_test 118h "Verify timeout in handling recoverables errors  =========="
8734
8735 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8736
8737 test_118i() {
8738         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8739         remote_ost_nodsh && skip "remote OST with nodsh" && return
8740
8741         reset_async
8742
8743         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8744         set_nodes_failloc "$(osts_nodes)" 0x20e
8745
8746         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
8747         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
8748         PID=$!
8749         sleep 5
8750         set_nodes_failloc "$(osts_nodes)" 0
8751
8752         wait $PID
8753         RC=$?
8754         if [[ $RC -ne 0 ]]; then
8755                 error "got error, but should be not, rc=$RC"
8756         fi
8757
8758         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8759         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8760         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8761         if [[ $LOCKED -ne 0 ]]; then
8762                 error "Locked pages remain in cache, locked=$LOCKED"
8763         fi
8764
8765         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8766                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8767         fi
8768
8769         rm -f $DIR/$tfile
8770         echo "No pages locked after fsync"
8771
8772         return 0
8773 }
8774 run_test 118i "Fix error before timeout in recoverable error  =========="
8775
8776 [ "$SLOW" = "no" ] && set_resend_count 4
8777
8778 test_118j() {
8779         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8780         remote_ost_nodsh && skip "remote OST with nodsh" && return
8781
8782         reset_async
8783
8784         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
8785         set_nodes_failloc "$(osts_nodes)" 0x220
8786
8787         # return -EIO from OST
8788         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8789         RC=$?
8790         set_nodes_failloc "$(osts_nodes)" 0x0
8791         if [[ $RC -eq 0 ]]; then
8792                 error "Must return error due to dropped pages, rc=$RC"
8793         fi
8794
8795         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
8796         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
8797         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
8798         if [[ $LOCKED -ne 0 ]]; then
8799                 error "Locked pages remain in cache, locked=$LOCKED"
8800         fi
8801
8802         # in recoverable error on OST we want resend and stay until it finished
8803         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
8804                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
8805         fi
8806
8807         rm -f $DIR/$tfile
8808         echo "No pages locked after fsync"
8809
8810         return 0
8811 }
8812 run_test 118j "Simulate unrecoverable OST side error =========="
8813
8814 test_118k()
8815 {
8816         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8817         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8818
8819         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
8820         set_nodes_failloc "$(osts_nodes)" 0x20e
8821         test_mkdir $DIR/$tdir
8822
8823         for ((i=0;i<10;i++)); do
8824                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
8825                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
8826                 SLEEPPID=$!
8827                 sleep 0.500s
8828                 kill $SLEEPPID
8829                 wait $SLEEPPID
8830         done
8831
8832         set_nodes_failloc "$(osts_nodes)" 0
8833         rm -rf $DIR/$tdir
8834 }
8835 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
8836
8837 test_118l()
8838 {
8839         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8840         # LU-646
8841         test_mkdir $DIR/$tdir
8842         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
8843         rm -rf $DIR/$tdir
8844 }
8845 run_test 118l "fsync dir"
8846
8847 test_118m() # LU-3066
8848 {
8849         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8850         test_mkdir $DIR/$tdir
8851         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
8852         rm -rf $DIR/$tdir
8853 }
8854 run_test 118m "fdatasync dir ========="
8855
8856 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
8857
8858 test_118n()
8859 {
8860         local begin
8861         local end
8862
8863         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8864         remote_ost_nodsh && skip "remote OSTs with nodsh" && return
8865
8866         # Sleep to avoid a cached response.
8867         #define OBD_STATFS_CACHE_SECONDS 1
8868         sleep 2
8869
8870         # Inject a 10 second delay in the OST_STATFS handler.
8871         #define OBD_FAIL_OST_STATFS_DELAY 0x242
8872         set_nodes_failloc "$(osts_nodes)" 0x242
8873
8874         begin=$SECONDS
8875         stat --file-system $MOUNT > /dev/null
8876         end=$SECONDS
8877
8878         set_nodes_failloc "$(osts_nodes)" 0
8879
8880         if ((end - begin > 20)); then
8881             error "statfs took $((end - begin)) seconds, expected 10"
8882         fi
8883 }
8884 run_test 118n "statfs() sends OST_STATFS requests in parallel"
8885
8886 test_119a() # bug 11737
8887 {
8888         BSIZE=$((512 * 1024))
8889         directio write $DIR/$tfile 0 1 $BSIZE
8890         # We ask to read two blocks, which is more than a file size.
8891         # directio will indicate an error when requested and actual
8892         # sizes aren't equeal (a normal situation in this case) and
8893         # print actual read amount.
8894         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
8895         if [ "$NOB" != "$BSIZE" ]; then
8896                 error "read $NOB bytes instead of $BSIZE"
8897         fi
8898         rm -f $DIR/$tfile
8899 }
8900 run_test 119a "Short directIO read must return actual read amount"
8901
8902 test_119b() # bug 11737
8903 {
8904         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
8905
8906         $SETSTRIPE -c 2 $DIR/$tfile || error "setstripe failed"
8907         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
8908         sync
8909         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) || \
8910                 error "direct read failed"
8911         rm -f $DIR/$tfile
8912 }
8913 run_test 119b "Sparse directIO read must return actual read amount"
8914
8915 test_119c() # bug 13099
8916 {
8917         BSIZE=1048576
8918         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
8919         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
8920         rm -f $DIR/$tfile
8921 }
8922 run_test 119c "Testing for direct read hitting hole"
8923
8924 test_119d() # bug 15950
8925 {
8926         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8927         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
8928         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
8929         BSIZE=1048576
8930         $SETSTRIPE $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
8931         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
8932         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
8933         lctl set_param fail_loc=0x40d
8934         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
8935         pid_dio=$!
8936         sleep 1
8937         cat $DIR/$tfile > /dev/null &
8938         lctl set_param fail_loc=0
8939         pid_reads=$!
8940         wait $pid_dio
8941         log "the DIO writes have completed, now wait for the reads (should not block very long)"
8942         sleep 2
8943         [ -n "`ps h -p $pid_reads -o comm`" ] && \
8944         error "the read rpcs have not completed in 2s"
8945         rm -f $DIR/$tfile
8946         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
8947 }
8948 run_test 119d "The DIO path should try to send a new rpc once one is completed"
8949
8950 test_120a() {
8951         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8952         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8953         test_mkdir $DIR/$tdir
8954         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8955                 { skip "no early lock cancel on server"; return 0; }
8956
8957         lru_resize_disable mdc
8958         lru_resize_disable osc
8959         cancel_lru_locks mdc
8960         # asynchronous object destroy at MDT could cause bl ast to client
8961         cancel_lru_locks osc
8962
8963         stat $DIR/$tdir > /dev/null
8964         can1=$(do_facet $SINGLEMDS \
8965                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8966                awk '/ldlm_cancel/ {print $2}')
8967         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8968                awk '/ldlm_bl_callback/ {print $2}')
8969         test_mkdir -c1 $DIR/$tdir/d1
8970         can2=$(do_facet $SINGLEMDS \
8971                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8972                awk '/ldlm_cancel/ {print $2}')
8973         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8974                awk '/ldlm_bl_callback/ {print $2}')
8975         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
8976         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
8977         lru_resize_enable mdc
8978         lru_resize_enable osc
8979 }
8980 run_test 120a "Early Lock Cancel: mkdir test"
8981
8982 test_120b() {
8983         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
8984         remote_mds_nodsh && skip "remote MDS with nodsh" && return
8985         test_mkdir $DIR/$tdir
8986         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
8987                 { skip "no early lock cancel on server"; return 0; }
8988         lru_resize_disable mdc
8989         lru_resize_disable osc
8990         cancel_lru_locks mdc
8991         stat $DIR/$tdir > /dev/null
8992         can1=$(do_facet $SINGLEMDS \
8993                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
8994                awk '/ldlm_cancel/ {print $2}')
8995         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
8996                awk '/ldlm_bl_callback/ {print $2}')
8997         touch $DIR/$tdir/f1
8998         can2=$(do_facet $SINGLEMDS \
8999                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9000                awk '/ldlm_cancel/ {print $2}')
9001         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9002                awk '/ldlm_bl_callback/ {print $2}')
9003         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9004         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9005         lru_resize_enable mdc
9006         lru_resize_enable osc
9007 }
9008 run_test 120b "Early Lock Cancel: create test"
9009
9010 test_120c() {
9011         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9012         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9013         test_mkdir -c1 $DIR/$tdir
9014         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9015                 { skip "no early lock cancel on server"; return 0; }
9016         lru_resize_disable mdc
9017         lru_resize_disable osc
9018         test_mkdir -c1 $DIR/$tdir/d1
9019         test_mkdir -c1 $DIR/$tdir/d2
9020         touch $DIR/$tdir/d1/f1
9021         cancel_lru_locks mdc
9022         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
9023         can1=$(do_facet $SINGLEMDS \
9024                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9025                awk '/ldlm_cancel/ {print $2}')
9026         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9027                awk '/ldlm_bl_callback/ {print $2}')
9028         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9029         can2=$(do_facet $SINGLEMDS \
9030                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9031                awk '/ldlm_cancel/ {print $2}')
9032         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9033                awk '/ldlm_bl_callback/ {print $2}')
9034         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9035         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9036         lru_resize_enable mdc
9037         lru_resize_enable osc
9038 }
9039 run_test 120c "Early Lock Cancel: link test"
9040
9041 test_120d() {
9042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9043         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9044         test_mkdir -c1 $DIR/$tdir
9045         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9046                 { skip "no early lock cancel on server"; return 0; }
9047         lru_resize_disable mdc
9048         lru_resize_disable osc
9049         touch $DIR/$tdir
9050         cancel_lru_locks mdc
9051         stat $DIR/$tdir > /dev/null
9052         can1=$(do_facet $SINGLEMDS \
9053                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9054                awk '/ldlm_cancel/ {print $2}')
9055         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9056                awk '/ldlm_bl_callback/ {print $2}')
9057         chmod a+x $DIR/$tdir
9058         can2=$(do_facet $SINGLEMDS \
9059                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9060                awk '/ldlm_cancel/ {print $2}')
9061         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9062                awk '/ldlm_bl_callback/ {print $2}')
9063         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9064         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9065         lru_resize_enable mdc
9066         lru_resize_enable osc
9067 }
9068 run_test 120d "Early Lock Cancel: setattr test"
9069
9070 test_120e() {
9071         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9072         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9073                 { skip "no early lock cancel on server"; return 0; }
9074         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9075         local dlmtrace_set=false
9076
9077         test_mkdir -c1 $DIR/$tdir
9078         lru_resize_disable mdc
9079         lru_resize_disable osc
9080         ! $LCTL get_param debug | grep -q dlmtrace &&
9081                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
9082         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
9083         cancel_lru_locks mdc
9084         cancel_lru_locks osc
9085         dd if=$DIR/$tdir/f1 of=/dev/null
9086         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
9087         # XXX client can not do early lock cancel of OST lock
9088         # during unlink (LU-4206), so cancel osc lock now.
9089         sleep 2
9090         cancel_lru_locks osc
9091         can1=$(do_facet $SINGLEMDS \
9092                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9093                awk '/ldlm_cancel/ {print $2}')
9094         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9095                awk '/ldlm_bl_callback/ {print $2}')
9096         unlink $DIR/$tdir/f1
9097         sleep 5
9098         can2=$(do_facet $SINGLEMDS \
9099                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9100                awk '/ldlm_cancel/ {print $2}')
9101         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9102                awk '/ldlm_bl_callback/ {print $2}')
9103         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
9104                 $LCTL dk $TMP/cancel.debug.txt
9105         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
9106                 $LCTL dk $TMP/blocking.debug.txt
9107         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
9108         lru_resize_enable mdc
9109         lru_resize_enable osc
9110 }
9111 run_test 120e "Early Lock Cancel: unlink test"
9112
9113 test_120f() {
9114         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9115         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9116                 { skip "no early lock cancel on server"; return 0; }
9117         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9118         test_mkdir -c1 $DIR/$tdir
9119         lru_resize_disable mdc
9120         lru_resize_disable osc
9121         test_mkdir -c1 $DIR/$tdir/d1
9122         test_mkdir -c1 $DIR/$tdir/d2
9123         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
9124         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
9125         cancel_lru_locks mdc
9126         cancel_lru_locks osc
9127         dd if=$DIR/$tdir/d1/f1 of=/dev/null
9128         dd if=$DIR/$tdir/d2/f2 of=/dev/null
9129         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
9130         # XXX client can not do early lock cancel of OST lock
9131         # during rename (LU-4206), so cancel osc lock now.
9132         sleep 2
9133         cancel_lru_locks osc
9134         can1=$(do_facet $SINGLEMDS \
9135                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9136                awk '/ldlm_cancel/ {print $2}')
9137         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9138                awk '/ldlm_bl_callback/ {print $2}')
9139         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
9140         sleep 5
9141         can2=$(do_facet $SINGLEMDS \
9142                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9143                awk '/ldlm_cancel/ {print $2}')
9144         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9145                awk '/ldlm_bl_callback/ {print $2}')
9146         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
9147         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
9148         lru_resize_enable mdc
9149         lru_resize_enable osc
9150 }
9151 run_test 120f "Early Lock Cancel: rename test"
9152
9153 test_120g() {
9154         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9155         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
9156                 { skip "no early lock cancel on server"; return 0; }
9157         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9158         lru_resize_disable mdc
9159         lru_resize_disable osc
9160         count=10000
9161         echo create $count files
9162         test_mkdir $DIR/$tdir
9163         cancel_lru_locks mdc
9164         cancel_lru_locks osc
9165         t0=$(date +%s)
9166
9167         can0=$(do_facet $SINGLEMDS \
9168                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9169                awk '/ldlm_cancel/ {print $2}')
9170         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9171                awk '/ldlm_bl_callback/ {print $2}')
9172         createmany -o $DIR/$tdir/f $count
9173         sync
9174         can1=$(do_facet $SINGLEMDS \
9175                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9176                awk '/ldlm_cancel/ {print $2}')
9177         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9178                awk '/ldlm_bl_callback/ {print $2}')
9179         t1=$(date +%s)
9180         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
9181         echo rm $count files
9182         rm -r $DIR/$tdir
9183         sync
9184         can2=$(do_facet $SINGLEMDS \
9185                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
9186                awk '/ldlm_cancel/ {print $2}')
9187         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
9188                awk '/ldlm_bl_callback/ {print $2}')
9189         t2=$(date +%s)
9190         echo total: $count removes in $((t2-t1))
9191         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
9192         sleep 2
9193         # wait for commitment of removal
9194         lru_resize_enable mdc
9195         lru_resize_enable osc
9196 }
9197 run_test 120g "Early Lock Cancel: performance test"
9198
9199 test_121() { #bug #10589
9200         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9201         rm -rf $DIR/$tfile
9202         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
9203 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
9204         lctl set_param fail_loc=0x310
9205         cancel_lru_locks osc > /dev/null
9206         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
9207         lctl set_param fail_loc=0
9208         [[ $reads -eq $writes ]] ||
9209                 error "read $reads blocks, must be $writes blocks"
9210 }
9211 run_test 121 "read cancel race ========="
9212
9213 test_123a() { # was test 123, statahead(bug 11401)
9214         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9215         SLOWOK=0
9216         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
9217                 log "testing UP system. Performance may be lower than expected."
9218                 SLOWOK=1
9219         fi
9220
9221         rm -rf $DIR/$tdir
9222         test_mkdir $DIR/$tdir
9223         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
9224         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
9225         MULT=10
9226         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
9227                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
9228
9229                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9230                 lctl set_param -n llite.*.statahead_max 0
9231                 lctl get_param llite.*.statahead_max
9232                 cancel_lru_locks mdc
9233                 cancel_lru_locks osc
9234                 stime=`date +%s`
9235                 time ls -l $DIR/$tdir | wc -l
9236                 etime=`date +%s`
9237                 delta=$((etime - stime))
9238                 log "ls $i files without statahead: $delta sec"
9239                 lctl set_param llite.*.statahead_max=$max
9240
9241                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9242                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
9243                 cancel_lru_locks mdc
9244                 cancel_lru_locks osc
9245                 stime=`date +%s`
9246                 time ls -l $DIR/$tdir | wc -l
9247                 etime=`date +%s`
9248                 delta_sa=$((etime - stime))
9249                 log "ls $i files with statahead: $delta_sa sec"
9250                 lctl get_param -n llite.*.statahead_stats
9251                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
9252
9253                 [[ $swrong -lt $ewrong ]] &&
9254                         log "statahead was stopped, maybe too many locks held!"
9255                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
9256
9257                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9258                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
9259                     lctl set_param -n llite.*.statahead_max 0
9260                     lctl get_param llite.*.statahead_max
9261                     cancel_lru_locks mdc
9262                     cancel_lru_locks osc
9263                     stime=`date +%s`
9264                     time ls -l $DIR/$tdir | wc -l
9265                     etime=`date +%s`
9266                     delta=$((etime - stime))
9267                     log "ls $i files again without statahead: $delta sec"
9268                     lctl set_param llite.*.statahead_max=$max
9269                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
9270                         if [  $SLOWOK -eq 0 ]; then
9271                                 error "ls $i files is slower with statahead!"
9272                         else
9273                                 log "ls $i files is slower with statahead!"
9274                         fi
9275                         break
9276                     fi
9277                 fi
9278
9279                 [ $delta -gt 20 ] && break
9280                 [ $delta -gt 8 ] && MULT=$((50 / delta))
9281                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
9282         done
9283         log "ls done"
9284
9285         stime=`date +%s`
9286         rm -r $DIR/$tdir
9287         sync
9288         etime=`date +%s`
9289         delta=$((etime - stime))
9290         log "rm -r $DIR/$tdir/: $delta seconds"
9291         log "rm done"
9292         lctl get_param -n llite.*.statahead_stats
9293 }
9294 run_test 123a "verify statahead work"
9295
9296 test_123b () { # statahead(bug 15027)
9297         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9298         test_mkdir $DIR/$tdir
9299         createmany -o $DIR/$tdir/$tfile-%d 1000
9300
9301         cancel_lru_locks mdc
9302         cancel_lru_locks osc
9303
9304 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
9305         lctl set_param fail_loc=0x80000803
9306         ls -lR $DIR/$tdir > /dev/null
9307         log "ls done"
9308         lctl set_param fail_loc=0x0
9309         lctl get_param -n llite.*.statahead_stats
9310         rm -r $DIR/$tdir
9311         sync
9312
9313 }
9314 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
9315
9316 test_124a() {
9317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9318         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9319                 { skip "no lru resize on server"; return 0; }
9320         local NR=2000
9321         test_mkdir $DIR/$tdir
9322
9323         log "create $NR files at $DIR/$tdir"
9324         createmany -o $DIR/$tdir/f $NR ||
9325                 error "failed to create $NR files in $DIR/$tdir"
9326
9327         cancel_lru_locks mdc
9328         ls -l $DIR/$tdir > /dev/null
9329
9330         local NSDIR=""
9331         local LRU_SIZE=0
9332         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
9333                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
9334                 LRU_SIZE=$($LCTL get_param -n $PARAM)
9335                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
9336                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
9337                         log "NSDIR=$NSDIR"
9338                         log "NS=$(basename $NSDIR)"
9339                         break
9340                 fi
9341         done
9342
9343         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
9344                 skip "Not enough cached locks created!"
9345                 return 0
9346         fi
9347         log "LRU=$LRU_SIZE"
9348
9349         local SLEEP=30
9350
9351         # We know that lru resize allows one client to hold $LIMIT locks
9352         # for 10h. After that locks begin to be killed by client.
9353         local MAX_HRS=10
9354         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
9355         log "LIMIT=$LIMIT"
9356         if [ $LIMIT -lt $LRU_SIZE ]; then
9357             skip "Limit is too small $LIMIT"
9358             return 0
9359         fi
9360
9361         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
9362         # killing locks. Some time was spent for creating locks. This means
9363         # that up to the moment of sleep finish we must have killed some of
9364         # them (10-100 locks). This depends on how fast ther were created.
9365         # Many of them were touched in almost the same moment and thus will
9366         # be killed in groups.
9367         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
9368
9369         # Use $LRU_SIZE_B here to take into account real number of locks
9370         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
9371         local LRU_SIZE_B=$LRU_SIZE
9372         log "LVF=$LVF"
9373         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
9374         log "OLD_LVF=$OLD_LVF"
9375         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
9376
9377         # Let's make sure that we really have some margin. Client checks
9378         # cached locks every 10 sec.
9379         SLEEP=$((SLEEP+20))
9380         log "Sleep ${SLEEP} sec"
9381         local SEC=0
9382         while ((SEC<$SLEEP)); do
9383                 echo -n "..."
9384                 sleep 5
9385                 SEC=$((SEC+5))
9386                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
9387                 echo -n "$LRU_SIZE"
9388         done
9389         echo ""
9390         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
9391         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
9392
9393         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
9394                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
9395                 unlinkmany $DIR/$tdir/f $NR
9396                 return
9397         }
9398
9399         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
9400         log "unlink $NR files at $DIR/$tdir"
9401         unlinkmany $DIR/$tdir/f $NR
9402 }
9403 run_test 124a "lru resize ======================================="
9404
9405 get_max_pool_limit()
9406 {
9407         local limit=$($LCTL get_param \
9408                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
9409         local max=0
9410         for l in $limit; do
9411                 if [[ $l -gt $max ]]; then
9412                         max=$l
9413                 fi
9414         done
9415         echo $max
9416 }
9417
9418 test_124b() {
9419         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9420         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9421                 { skip "no lru resize on server"; return 0; }
9422
9423         LIMIT=$(get_max_pool_limit)
9424
9425         NR=$(($(default_lru_size)*20))
9426         if [[ $NR -gt $LIMIT ]]; then
9427                 log "Limit lock number by $LIMIT locks"
9428                 NR=$LIMIT
9429         fi
9430
9431         IFree=$(mdsrate_inodes_available)
9432         if [ $IFree -lt $NR ]; then
9433                 log "Limit lock number by $IFree inodes"
9434                 NR=$IFree
9435         fi
9436
9437         lru_resize_disable mdc
9438         test_mkdir -p $DIR/$tdir/disable_lru_resize
9439
9440         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
9441         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
9442         cancel_lru_locks mdc
9443         stime=`date +%s`
9444         PID=""
9445         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9446         PID="$PID $!"
9447         sleep 2
9448         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9449         PID="$PID $!"
9450         sleep 2
9451         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
9452         PID="$PID $!"
9453         wait $PID
9454         etime=`date +%s`
9455         nolruresize_delta=$((etime-stime))
9456         log "ls -la time: $nolruresize_delta seconds"
9457         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9458         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
9459
9460         lru_resize_enable mdc
9461         test_mkdir -p $DIR/$tdir/enable_lru_resize
9462
9463         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
9464         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
9465         cancel_lru_locks mdc
9466         stime=`date +%s`
9467         PID=""
9468         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9469         PID="$PID $!"
9470         sleep 2
9471         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9472         PID="$PID $!"
9473         sleep 2
9474         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
9475         PID="$PID $!"
9476         wait $PID
9477         etime=`date +%s`
9478         lruresize_delta=$((etime-stime))
9479         log "ls -la time: $lruresize_delta seconds"
9480         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
9481
9482         if [ $lruresize_delta -gt $nolruresize_delta ]; then
9483                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
9484         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
9485                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
9486         else
9487                 log "lru resize performs the same with no lru resize"
9488         fi
9489         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
9490 }
9491 run_test 124b "lru resize (performance test) ======================="
9492
9493 test_124c() {
9494         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9495         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
9496                 { skip "no lru resize on server"; return 0; }
9497
9498         # cache ununsed locks on client
9499         local nr=100
9500         cancel_lru_locks mdc
9501         test_mkdir $DIR/$tdir
9502         createmany -o $DIR/$tdir/f $nr ||
9503                 error "failed to create $nr files in $DIR/$tdir"
9504         ls -l $DIR/$tdir > /dev/null
9505
9506         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
9507         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
9508         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
9509         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
9510         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
9511
9512         # set lru_max_age to 1 sec
9513         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
9514         echo "sleep $((recalc_p * 2)) seconds..."
9515         sleep $((recalc_p * 2))
9516
9517         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
9518         # restore lru_max_age
9519         $LCTL set_param -n $nsdir.lru_max_age $max_age
9520         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
9521         unlinkmany $DIR/$tdir/f $nr
9522 }
9523 run_test 124c "LRUR cancel very aged locks"
9524
9525 test_125() { # 13358
9526         $LCTL get_param -n llite.*.client_type | grep -q local ||
9527                 { skip "must run as local client"; return; }
9528         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
9529                 { skip "must have acl enabled"; return; }
9530         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
9531         test_mkdir $DIR/$tdir
9532         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
9533         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
9534         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
9535 }
9536 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
9537
9538 test_126() { # bug 12829/13455
9539         $LCTL get_param -n llite.*.client_type | grep -q local ||
9540                 { skip "must run as local client"; return; }
9541         [ "$UID" != 0 ] && skip_env "$TESTNAME (must run as root)" && return
9542         $GSS && skip "must run as gss disabled" && return
9543
9544         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
9545         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
9546         rm -f $DIR/$tfile
9547         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
9548 }
9549 run_test 126 "check that the fsgid provided by the client is taken into account"
9550
9551 test_127a() { # bug 15521
9552         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9553         $SETSTRIPE -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
9554         $LCTL set_param osc.*.stats=0
9555         FSIZE=$((2048 * 1024))
9556         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9557         cancel_lru_locks osc
9558         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE
9559
9560         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/${tfile}.tmp
9561         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9562                 echo "got $COUNT $NAME"
9563                 [ ! $MIN ] && error "Missing min value for $NAME proc entry"
9564                 eval $NAME=$COUNT || error "Wrong proc format"
9565
9566                 case $NAME in
9567                         read_bytes|write_bytes)
9568                         [ $MIN -lt 4096 ] && error "min is too small: $MIN"
9569                         [ $MIN -gt $FSIZE ] && error "min is too big: $MIN"
9570                         [ $MAX -lt 4096 ] && error "max is too small: $MAX"
9571                         [ $MAX -gt $FSIZE ] && error "max is too big: $MAX"
9572                         [ $SUM -ne $FSIZE ] && error "sum is wrong: $SUM"
9573                         [ $SUMSQ -lt $(((FSIZE /4096) * (4096 * 4096))) ] &&
9574                                 error "sumsquare is too small: $SUMSQ"
9575                         [ $SUMSQ -gt $((FSIZE * FSIZE)) ] &&
9576                                 error "sumsquare is too big: $SUMSQ"
9577                         ;;
9578                         *) ;;
9579                 esac
9580         done < $DIR/${tfile}.tmp
9581
9582         #check that we actually got some stats
9583         [ "$read_bytes" ] || error "Missing read_bytes stats"
9584         [ "$write_bytes" ] || error "Missing write_bytes stats"
9585         [ "$read_bytes" != 0 ] || error "no read done"
9586         [ "$write_bytes" != 0 ] || error "no write done"
9587 }
9588 run_test 127a "verify the client stats are sane"
9589
9590 test_127b() { # bug LU-333
9591         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9592         $LCTL set_param llite.*.stats=0
9593         FSIZE=65536 # sized fixed to match PAGE_SIZE for most clients
9594         # perform 2 reads and writes so MAX is different from SUM.
9595         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9596         dd if=/dev/zero of=$DIR/$tfile bs=$FSIZE count=1
9597         cancel_lru_locks osc
9598         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9599         dd if=$DIR/$tfile of=/dev/null bs=$FSIZE count=1
9600
9601         $LCTL get_param llite.*.stats | grep samples > $TMP/${tfile}.tmp
9602         while read NAME COUNT SAMP UNIT MIN MAX SUM SUMSQ; do
9603                 echo "got $COUNT $NAME"
9604                 eval $NAME=$COUNT || error "Wrong proc format"
9605
9606         case $NAME in
9607                 read_bytes)
9608                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9609                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9610                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9611                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9612                         ;;
9613                 write_bytes)
9614                         [ $COUNT -ne 2 ] && error "count is not 2: $COUNT"
9615                         [ $MIN -ne $FSIZE ] && error "min is not $FSIZE: $MIN"
9616                         [ $MAX -ne $FSIZE ] && error "max is incorrect: $MAX"
9617                         [ $SUM -ne $((FSIZE * 2)) ] && error "sum is wrong: $SUM"
9618                         ;;
9619                         *) ;;
9620                 esac
9621         done < $TMP/${tfile}.tmp
9622
9623         #check that we actually got some stats
9624         [ "$read_bytes" ] || error "Missing read_bytes stats"
9625         [ "$write_bytes" ] || error "Missing write_bytes stats"
9626         [ "$read_bytes" != 0 ] || error "no read done"
9627         [ "$write_bytes" != 0 ] || error "no write done"
9628
9629         rm -f $TMP/${tfile}.tmp
9630 }
9631 run_test 127b "verify the llite client stats are sane"
9632
9633 test_128() { # bug 15212
9634         touch $DIR/$tfile
9635         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
9636                 find $DIR/$tfile
9637                 find $DIR/$tfile
9638         EOF
9639
9640         result=$(grep error $TMP/$tfile.log)
9641         rm -f $DIR/$tfile $TMP/$tfile.log
9642         [ -z "$result" ] ||
9643                 error "consecutive find's under interactive lfs failed"
9644 }
9645 run_test 128 "interactive lfs for 2 consecutive find's"
9646
9647 set_dir_limits () {
9648         local mntdev
9649         local canondev
9650         local node
9651
9652         local ldproc=/proc/fs/ldiskfs
9653         local facets=$(get_facets MDS)
9654
9655         for facet in ${facets//,/ }; do
9656                 canondev=$(ldiskfs_canon \
9657                            *.$(convert_facet2label $facet).mntdev $facet)
9658                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
9659                         ldproc=/sys/fs/ldiskfs
9660                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
9661                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
9662         done
9663 }
9664
9665 check_mds_dmesg() {
9666         local facets=$(get_facets MDS)
9667         for facet in ${facets//,/ }; do
9668                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
9669         done
9670         return 1
9671 }
9672
9673 test_129() {
9674         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.56) ]] ||
9675                 { skip "Need MDS version with at least 2.5.56"; return 0; }
9676
9677         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
9678         if [ "$(facet_fstype $SINGLEMDS)" != ldiskfs ]; then
9679                 skip "ldiskfs only test"
9680                 return
9681         fi
9682         remote_mds_nodsh && skip "remote MDS with nodsh" && return
9683         local ENOSPC=28
9684         local EFBIG=27
9685         local has_warning=false
9686
9687         rm -rf $DIR/$tdir
9688         mkdir -p $DIR/$tdir
9689
9690         # block size of mds1
9691         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
9692         set_dir_limits $maxsize $maxsize
9693         local dirsize=$(stat -c%s "$DIR/$tdir")
9694         local nfiles=0
9695         while [[ $dirsize -le $maxsize ]]; do
9696                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
9697                 rc=$?
9698                 if ! $has_warning; then
9699                         check_mds_dmesg '"is approaching"' && has_warning=true
9700                 fi
9701                 # check two errors:
9702                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
9703                 # EFBIG for previous versions included in ldiskfs series
9704                 if [ $rc -eq $EFBIG -o $rc -eq $ENOSPC ]; then
9705                         set_dir_limits 0 0
9706                         echo "return code $rc received as expected"
9707
9708                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
9709                                 error_exit "create failed w/o dir size limit"
9710
9711                         check_mds_dmesg '"has reached"' ||
9712                                 error_exit "reached message should be output"
9713
9714                         [ $has_warning = "false" ] &&
9715                                 error_exit "warning message should be output"
9716
9717                         dirsize=$(stat -c%s "$DIR/$tdir")
9718
9719                         [[ $dirsize -ge $maxsize ]] && return 0
9720                         error_exit "current dir size $dirsize, " \
9721                                    "previous limit $maxsize"
9722                 elif [ $rc -ne 0 ]; then
9723                         set_dir_limits 0 0
9724                         error_exit "return $rc received instead of expected " \
9725                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
9726                 fi
9727                 nfiles=$((nfiles + 1))
9728                 dirsize=$(stat -c%s "$DIR/$tdir")
9729         done
9730
9731         set_dir_limits 0 0
9732         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
9733 }
9734 run_test 129 "test directory size limit ========================"
9735
9736 OLDIFS="$IFS"
9737 cleanup_130() {
9738         trap 0
9739         IFS="$OLDIFS"
9740 }
9741
9742 test_130a() {
9743         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9744         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9745                 return
9746
9747         trap cleanup_130 EXIT RETURN
9748
9749         local fm_file=$DIR/$tfile
9750         $SETSTRIPE -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
9751         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
9752                 error "dd failed for $fm_file"
9753
9754         # LU-1795: test filefrag/FIEMAP once, even if unsupported
9755         filefrag -ves $fm_file
9756         RC=$?
9757         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9758                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9759         [ $RC != 0 ] && error "filefrag $fm_file failed"
9760
9761         filefrag_op=$(filefrag -ve -k $fm_file |
9762                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9763         lun=$($GETSTRIPE -i $fm_file)
9764
9765         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
9766         IFS=$'\n'
9767         tot_len=0
9768         for line in $filefrag_op
9769         do
9770                 frag_lun=`echo $line | cut -d: -f5`
9771                 ext_len=`echo $line | cut -d: -f4`
9772                 if (( $frag_lun != $lun )); then
9773                         cleanup_130
9774                         error "FIEMAP on 1-stripe file($fm_file) failed"
9775                         return
9776                 fi
9777                 (( tot_len += ext_len ))
9778         done
9779
9780         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
9781                 cleanup_130
9782                 error "FIEMAP on 1-stripe file($fm_file) failed;"
9783                 return
9784         fi
9785
9786         cleanup_130
9787
9788         echo "FIEMAP on single striped file succeeded"
9789 }
9790 run_test 130a "FIEMAP (1-stripe file)"
9791
9792 test_130b() {
9793         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs" && return
9794
9795         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9796         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP" &&
9797                 return
9798
9799         trap cleanup_130 EXIT RETURN
9800
9801         local fm_file=$DIR/$tfile
9802         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9803                         error "setstripe on $fm_file"
9804         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9805                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9806
9807         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
9808                 error "dd failed on $fm_file"
9809
9810         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9811         filefrag_op=$(filefrag -ve -k $fm_file |
9812                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9813
9814         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9815                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9816
9817         IFS=$'\n'
9818         tot_len=0
9819         num_luns=1
9820         for line in $filefrag_op
9821         do
9822                 frag_lun=$(echo $line | cut -d: -f5 |
9823                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9824                 ext_len=$(echo $line | cut -d: -f4)
9825                 if (( $frag_lun != $last_lun )); then
9826                         if (( tot_len != 1024 )); then
9827                                 cleanup_130
9828                                 error "FIEMAP on $fm_file failed; returned " \
9829                                 "len $tot_len for OST $last_lun instead of 1024"
9830                                 return
9831                         else
9832                                 (( num_luns += 1 ))
9833                                 tot_len=0
9834                         fi
9835                 fi
9836                 (( tot_len += ext_len ))
9837                 last_lun=$frag_lun
9838         done
9839         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
9840                 cleanup_130
9841                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9842                         "luns or wrong len for OST $last_lun"
9843                 return
9844         fi
9845
9846         cleanup_130
9847
9848         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
9849 }
9850 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
9851
9852 test_130c() {
9853         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9854
9855         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9856         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9857                 return
9858
9859         trap cleanup_130 EXIT RETURN
9860
9861         local fm_file=$DIR/$tfile
9862         $SETSTRIPE -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
9863         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9864                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9865
9866         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
9867                         error "dd failed on $fm_file"
9868
9869         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9870         filefrag_op=$(filefrag -ve -k $fm_file |
9871                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9872
9873         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9874                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9875
9876         IFS=$'\n'
9877         tot_len=0
9878         num_luns=1
9879         for line in $filefrag_op
9880         do
9881                 frag_lun=$(echo $line | cut -d: -f5 |
9882                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9883                 ext_len=$(echo $line | cut -d: -f4)
9884                 if (( $frag_lun != $last_lun )); then
9885                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
9886                         if (( logical != 512 )); then
9887                                 cleanup_130
9888                                 error "FIEMAP on $fm_file failed; returned " \
9889                                 "logical start for lun $logical instead of 512"
9890                                 return
9891                         fi
9892                         if (( tot_len != 512 )); then
9893                                 cleanup_130
9894                                 error "FIEMAP on $fm_file failed; returned " \
9895                                 "len $tot_len for OST $last_lun instead of 1024"
9896                                 return
9897                         else
9898                                 (( num_luns += 1 ))
9899                                 tot_len=0
9900                         fi
9901                 fi
9902                 (( tot_len += ext_len ))
9903                 last_lun=$frag_lun
9904         done
9905         if (( num_luns != 2 || tot_len != 512 )); then
9906                 cleanup_130
9907                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9908                         "luns or wrong len for OST $last_lun"
9909                 return
9910         fi
9911
9912         cleanup_130
9913
9914         echo "FIEMAP on 2-stripe file with hole succeeded"
9915 }
9916 run_test 130c "FIEMAP (2-stripe file with hole)"
9917
9918 test_130d() {
9919         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs" && return
9920
9921         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9922         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
9923                 return
9924
9925         trap cleanup_130 EXIT RETURN
9926
9927         local fm_file=$DIR/$tfile
9928         $SETSTRIPE -S 65536 -c $OSTCOUNT $fm_file ||
9929                         error "setstripe on $fm_file"
9930         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9931                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9932
9933         local actual_stripe_count=$($GETSTRIPE -c $fm_file)
9934         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
9935                 error "dd failed on $fm_file"
9936
9937         filefrag -ves $fm_file || error "filefrag $fm_file failed"
9938         filefrag_op=$(filefrag -ve -k $fm_file |
9939                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
9940
9941         last_lun=$(echo $filefrag_op | cut -d: -f5 |
9942                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9943
9944         IFS=$'\n'
9945         tot_len=0
9946         num_luns=1
9947         for line in $filefrag_op
9948         do
9949                 frag_lun=$(echo $line | cut -d: -f5 |
9950                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
9951                 ext_len=$(echo $line | cut -d: -f4)
9952                 if (( $frag_lun != $last_lun )); then
9953                         if (( tot_len != 1024 )); then
9954                                 cleanup_130
9955                                 error "FIEMAP on $fm_file failed; returned " \
9956                                 "len $tot_len for OST $last_lun instead of 1024"
9957                                 return
9958                         else
9959                                 (( num_luns += 1 ))
9960                                 tot_len=0
9961                         fi
9962                 fi
9963                 (( tot_len += ext_len ))
9964                 last_lun=$frag_lun
9965         done
9966         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
9967                 cleanup_130
9968                 error "FIEMAP on $fm_file failed; returned wrong number of " \
9969                         "luns or wrong len for OST $last_lun"
9970                 return
9971         fi
9972
9973         cleanup_130
9974
9975         echo "FIEMAP on N-stripe file succeeded"
9976 }
9977 run_test 130d "FIEMAP (N-stripe file)"
9978
9979 test_130e() {
9980         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs" && return
9981
9982         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
9983         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" && return
9984
9985         trap cleanup_130 EXIT RETURN
9986
9987         local fm_file=$DIR/$tfile
9988         $SETSTRIPE -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
9989         [ "$(facet_fstype ost$(($($GETSTRIPE -i $fm_file) + 1)))" = "zfs" ] &&
9990                 skip "ORI-366/LU-1941: FIEMAP unimplemented on ZFS" && return
9991
9992         NUM_BLKS=512
9993         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
9994         for ((i = 0; i < $NUM_BLKS; i++))
9995         do
9996                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
9997         done
9998
9999         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10000         filefrag_op=$(filefrag -ve -k $fm_file |
10001                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
10002
10003         last_lun=$(echo $filefrag_op | cut -d: -f5 |
10004                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10005
10006         IFS=$'\n'
10007         tot_len=0
10008         num_luns=1
10009         for line in $filefrag_op
10010         do
10011                 frag_lun=$(echo $line | cut -d: -f5 |
10012                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
10013                 ext_len=$(echo $line | cut -d: -f4)
10014                 if (( $frag_lun != $last_lun )); then
10015                         if (( tot_len != $EXPECTED_LEN )); then
10016                                 cleanup_130
10017                                 error "FIEMAP on $fm_file failed; returned " \
10018                                 "len $tot_len for OST $last_lun instead " \
10019                                 "of $EXPECTED_LEN"
10020                                 return
10021                         else
10022                                 (( num_luns += 1 ))
10023                                 tot_len=0
10024                         fi
10025                 fi
10026                 (( tot_len += ext_len ))
10027                 last_lun=$frag_lun
10028         done
10029         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
10030                 cleanup_130
10031                 error "FIEMAP on $fm_file failed; returned wrong number " \
10032                         "of luns or wrong len for OST $last_lun"
10033                 return
10034         fi
10035
10036         cleanup_130
10037
10038         echo "FIEMAP with continuation calls succeeded"
10039 }
10040 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
10041
10042 # Test for writev/readv
10043 test_131a() {
10044         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
10045                 error "writev test failed"
10046         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
10047                 error "readv failed"
10048         rm -f $DIR/$tfile
10049 }
10050 run_test 131a "test iov's crossing stripe boundary for writev/readv"
10051
10052 test_131b() {
10053         local fsize=$((524288 + 1048576 + 1572864))
10054         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
10055                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10056                         error "append writev test failed"
10057
10058         ((fsize += 1572864 + 1048576))
10059         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
10060                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10061                         error "append writev test failed"
10062         rm -f $DIR/$tfile
10063 }
10064 run_test 131b "test append writev"
10065
10066 test_131c() {
10067         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
10068         error "NOT PASS"
10069 }
10070 run_test 131c "test read/write on file w/o objects"
10071
10072 test_131d() {
10073         rwv -f $DIR/$tfile -w -n 1 1572864
10074         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
10075         if [ "$NOB" != 1572864 ]; then
10076                 error "Short read filed: read $NOB bytes instead of 1572864"
10077         fi
10078         rm -f $DIR/$tfile
10079 }
10080 run_test 131d "test short read"
10081
10082 test_131e() {
10083         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
10084         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
10085         error "read hitting hole failed"
10086         rm -f $DIR/$tfile
10087 }
10088 run_test 131e "test read hitting hole"
10089
10090 check_stats() {
10091         local facet=$1
10092         local op=$2
10093         local want=${3:-0}
10094         local res
10095
10096         case $facet in
10097         mds*) res=$(do_facet $facet \
10098                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
10099                  ;;
10100         ost*) res=$(do_facet $facet \
10101                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
10102                  ;;
10103         *) error "Wrong facet '$facet'" ;;
10104         esac
10105         echo $res
10106         [ "$res" ] || error "The counter for $op on $facet was not incremented"
10107         # if the argument $3 is zero, it means any stat increment is ok.
10108         if [[ $want -gt 0 ]]; then
10109                 local count=$(echo $res | awk '{ print $2 }')
10110                 [[ $count -ne $want ]] &&
10111                         error "The $op counter on $facet is $count, not $want"
10112         fi
10113 }
10114
10115 test_133a() {
10116         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10117         remote_ost_nodsh && skip "remote OST with nodsh" && return
10118         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10119
10120         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10121                 { skip "MDS doesn't support rename stats"; return; }
10122         local testdir=$DIR/${tdir}/stats_testdir
10123         mkdir -p $DIR/${tdir}
10124
10125         # clear stats.
10126         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10127         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10128
10129         # verify mdt stats first.
10130         mkdir ${testdir} || error "mkdir failed"
10131         check_stats $SINGLEMDS "mkdir" 1
10132         touch ${testdir}/${tfile} || error "touch failed"
10133         check_stats $SINGLEMDS "open" 1
10134         check_stats $SINGLEMDS "close" 1
10135         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
10136                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
10137                 check_stats $SINGLEMDS "mknod" 2
10138         }
10139         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
10140         check_stats $SINGLEMDS "unlink" 1
10141         rm -f ${testdir}/${tfile} || error "file remove failed"
10142         check_stats $SINGLEMDS "unlink" 2
10143
10144         # remove working dir and check mdt stats again.
10145         rmdir ${testdir} || error "rmdir failed"
10146         check_stats $SINGLEMDS "rmdir" 1
10147
10148         local testdir1=$DIR/${tdir}/stats_testdir1
10149         mkdir -p ${testdir}
10150         mkdir -p ${testdir1}
10151         touch ${testdir1}/test1
10152         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
10153         check_stats $SINGLEMDS "crossdir_rename" 1
10154
10155         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
10156         check_stats $SINGLEMDS "samedir_rename" 1
10157
10158         rm -rf $DIR/${tdir}
10159 }
10160 run_test 133a "Verifying MDT stats ========================================"
10161
10162 test_133b() {
10163         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10164         remote_ost_nodsh && skip "remote OST with nodsh" && return
10165         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10166         local testdir=$DIR/${tdir}/stats_testdir
10167         mkdir -p ${testdir} || error "mkdir failed"
10168         touch ${testdir}/${tfile} || error "touch failed"
10169         cancel_lru_locks mdc
10170
10171         # clear stats.
10172         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10173         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10174
10175         # extra mdt stats verification.
10176         chmod 444 ${testdir}/${tfile} || error "chmod failed"
10177         check_stats $SINGLEMDS "setattr" 1
10178         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10179         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
10180         then            # LU-1740
10181                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
10182                 check_stats $SINGLEMDS "getattr" 1
10183         fi
10184         $LFS df || error "lfs failed"
10185         check_stats $SINGLEMDS "statfs" 1
10186
10187         rm -rf $DIR/${tdir}
10188 }
10189 run_test 133b "Verifying extra MDT stats =================================="
10190
10191 test_133c() {
10192         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10193         remote_ost_nodsh && skip "remote OST with nodsh" && return
10194         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10195         local testdir=$DIR/$tdir/stats_testdir
10196         test_mkdir -p $testdir
10197
10198         # verify obdfilter stats.
10199         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
10200         sync
10201         cancel_lru_locks osc
10202         wait_delete_completed
10203
10204         # clear stats.
10205         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10206         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10207
10208         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
10209                 error "dd failed"
10210         sync
10211         cancel_lru_locks osc
10212         check_stats ost1 "write" 1
10213
10214         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
10215         check_stats ost1 "read" 1
10216
10217         > $testdir/$tfile || error "truncate failed"
10218         check_stats ost1 "punch" 1
10219
10220         rm -f $testdir/$tfile || error "file remove failed"
10221         wait_delete_completed
10222         check_stats ost1 "destroy" 1
10223
10224         rm -rf $DIR/$tdir
10225 }
10226 run_test 133c "Verifying OST stats ========================================"
10227
10228 order_2() {
10229         local value=$1
10230         local orig=$value
10231         local order=1
10232
10233         while [ $value -ge 2 ]; do
10234                 order=$((order*2))
10235                 value=$((value/2))
10236         done
10237
10238         if [ $orig -gt $order ]; then
10239                 order=$((order*2))
10240         fi
10241         echo $order
10242 }
10243
10244 size_in_KMGT() {
10245     local value=$1
10246     local size=('K' 'M' 'G' 'T');
10247     local i=0
10248     local size_string=$value
10249
10250     while [ $value -ge 1024 ]; do
10251         if [ $i -gt 3 ]; then
10252             #T is the biggest unit we get here, if that is bigger,
10253             #just return XXXT
10254             size_string=${value}T
10255             break
10256         fi
10257         value=$((value >> 10))
10258         if [ $value -lt 1024 ]; then
10259             size_string=${value}${size[$i]}
10260             break
10261         fi
10262         i=$((i + 1))
10263     done
10264
10265     echo $size_string
10266 }
10267
10268 get_rename_size() {
10269     local size=$1
10270     local context=${2:-.}
10271     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
10272                 grep -A1 $context |
10273                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
10274     echo $sample
10275 }
10276
10277 test_133d() {
10278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10279         remote_ost_nodsh && skip "remote OST with nodsh" && return
10280         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10281         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10282         { skip "MDS doesn't support rename stats"; return; }
10283
10284         local testdir1=$DIR/${tdir}/stats_testdir1
10285         local testdir2=$DIR/${tdir}/stats_testdir2
10286
10287         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10288
10289         mkdir -p ${testdir1} || error "mkdir failed"
10290         mkdir -p ${testdir2} || error "mkdir failed"
10291
10292         createmany -o $testdir1/test 512 || error "createmany failed"
10293
10294         # check samedir rename size
10295         mv ${testdir1}/test0 ${testdir1}/test_0
10296
10297         local testdir1_size=$(ls -l $DIR/${tdir} |
10298                 awk '/stats_testdir1/ {print $5}')
10299         local testdir2_size=$(ls -l $DIR/${tdir} |
10300                 awk '/stats_testdir2/ {print $5}')
10301
10302         testdir1_size=$(order_2 $testdir1_size)
10303         testdir2_size=$(order_2 $testdir2_size)
10304
10305         testdir1_size=$(size_in_KMGT $testdir1_size)
10306         testdir2_size=$(size_in_KMGT $testdir2_size)
10307
10308         echo "source rename dir size: ${testdir1_size}"
10309         echo "target rename dir size: ${testdir2_size}"
10310
10311         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
10312         eval $cmd || error "$cmd failed"
10313         local samedir=$($cmd | grep 'same_dir')
10314         local same_sample=$(get_rename_size $testdir1_size)
10315         [ -z "$samedir" ] && error "samedir_rename_size count error"
10316         [[ $same_sample -eq 1 ]] ||
10317                 error "samedir_rename_size error $same_sample"
10318         echo "Check same dir rename stats success"
10319
10320         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10321
10322         # check crossdir rename size
10323         mv ${testdir1}/test_0 ${testdir2}/test_0
10324
10325         testdir1_size=$(ls -l $DIR/${tdir} |
10326                 awk '/stats_testdir1/ {print $5}')
10327         testdir2_size=$(ls -l $DIR/${tdir} |
10328                 awk '/stats_testdir2/ {print $5}')
10329
10330         testdir1_size=$(order_2 $testdir1_size)
10331         testdir2_size=$(order_2 $testdir2_size)
10332
10333         testdir1_size=$(size_in_KMGT $testdir1_size)
10334         testdir2_size=$(size_in_KMGT $testdir2_size)
10335
10336         echo "source rename dir size: ${testdir1_size}"
10337         echo "target rename dir size: ${testdir2_size}"
10338
10339         eval $cmd || error "$cmd failed"
10340         local crossdir=$($cmd | grep 'crossdir')
10341         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10342         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10343         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10344         [[ $src_sample -eq 1 ]] ||
10345                 error "crossdir_rename_size error $src_sample"
10346         [[ $tgt_sample -eq 1 ]] ||
10347                 error "crossdir_rename_size error $tgt_sample"
10348         echo "Check cross dir rename stats success"
10349         rm -rf $DIR/${tdir}
10350 }
10351 run_test 133d "Verifying rename_stats ========================================"
10352
10353 test_133e() {
10354         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10355         remote_ost_nodsh && skip "remote OST with nodsh" && return
10356         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10357         local testdir=$DIR/${tdir}/stats_testdir
10358         local ctr f0 f1 bs=32768 count=42 sum
10359
10360         mkdir -p ${testdir} || error "mkdir failed"
10361
10362         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10363
10364         for ctr in {write,read}_bytes; do
10365                 sync
10366                 cancel_lru_locks osc
10367
10368                 do_facet ost1 $LCTL set_param -n \
10369                         "obdfilter.*.exports.clear=clear"
10370
10371                 if [ $ctr = write_bytes ]; then
10372                         f0=/dev/zero
10373                         f1=${testdir}/${tfile}
10374                 else
10375                         f0=${testdir}/${tfile}
10376                         f1=/dev/null
10377                 fi
10378
10379                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10380                         error "dd failed"
10381                 sync
10382                 cancel_lru_locks osc
10383
10384                 sum=$(do_facet ost1 $LCTL get_param \
10385                         "obdfilter.*.exports.*.stats" |
10386                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10387                                 $1 == ctr { sum += $7 }
10388                                 END { printf("%0.0f", sum) }')
10389
10390                 if ((sum != bs * count)); then
10391                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10392                 fi
10393         done
10394
10395         rm -rf $DIR/${tdir}
10396 }
10397 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10398
10399 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10400
10401 test_133f() {
10402         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10403         remote_ost_nodsh && skip "remote OST with nodsh" && return
10404         # First without trusting modes.
10405         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10406         echo "proc_dirs='$proc_dirs'"
10407         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10408         find $proc_dirs -exec cat '{}' \; &> /dev/null
10409
10410         # Second verifying readability.
10411         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10412
10413         # eventually, this can also be replaced with "lctl get_param -R",
10414         # but not until that option is always available on the server
10415         local facet
10416         for facet in mds1 ost1; do
10417                 local facet_proc_dirs=$(do_facet $facet \
10418                                         \\\ls -d $proc_regexp 2>/dev/null)
10419                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10420                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10421                 do_facet $facet find $facet_proc_dirs \
10422                         ! -name req_history \
10423                         -exec cat '{}' \\\; &> /dev/null
10424
10425                 do_facet $facet find $facet_proc_dirs \
10426                         ! -name req_history \
10427                         -type f \
10428                         -exec cat '{}' \\\; &> /dev/null ||
10429                                 error "proc file read failed"
10430         done
10431 }
10432 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
10433
10434 test_133g() {
10435         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10436         remote_ost_nodsh && skip "remote OST with nodsh" && return
10437         # Second verifying writability.
10438         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10439         echo "proc_dirs='$proc_dirs'"
10440         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10441         find $proc_dirs \
10442                 -ignore_readdir_race \
10443                 -type f \
10444                 -not -name force_lbug \
10445                 -not -name changelog_mask \
10446                 -exec badarea_io '{}' \; ||
10447                 error "find $proc_dirs failed"
10448
10449         local facet
10450         for facet in mds1 ost1; do
10451                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10452                         skip "Too old lustre on $facet" && continue
10453                 local facet_proc_dirs=$(do_facet $facet \
10454                                         \\\ls -d $proc_regexp 2> /dev/null)
10455                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10456                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10457                 do_facet $facet find $facet_proc_dirs \
10458                         -ignore_readdir_race \
10459                         -type f \
10460                         -not -name force_lbug \
10461                         -not -name changelog_mask \
10462                         -exec badarea_io '{}' \\\; ||
10463                                 error "$facet find $facet_proc_dirs failed"
10464         done
10465
10466         # remount the FS in case writes/reads /proc break the FS
10467         cleanup || error "failed to unmount"
10468         setup || error "failed to setup"
10469         true
10470 }
10471 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
10472
10473 test_133h() {
10474         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10475         remote_ost_nodsh && skip "remote OST with nodsh" && return
10476         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10477                 skip "Need MDS version at least 2.9.54" && return
10478
10479         local facet
10480         for facet in client mds1 ost1; do
10481                 local facet_proc_dirs=$(do_facet $facet \
10482                                         \\\ls -d $proc_regexp 2> /dev/null)
10483                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10484                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10485                 # Get the list of files that are missing the terminating newline
10486                 local missing=($(do_facet $facet \
10487                         find ${facet_proc_dirs} -type f \|              \
10488                                 while read F\; do                       \
10489                                         awk -v FS='\v' -v RS='\v\v'     \
10490                                         "'END { if(NR>0 &&              \
10491                                         \\\$NF !~ /.*\\\n\$/)           \
10492                                                 print FILENAME}'"       \
10493                                         '\$F'\;                         \
10494                                 done 2>/dev/null))
10495                 [ ${#missing[*]} -eq 0 ] ||
10496                         error "files do not end with newline: ${missing[*]}"
10497         done
10498 }
10499 run_test 133h "Proc files should end with newlines"
10500
10501 test_134a() {
10502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10503         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10504                 skip "Need MDS version at least 2.7.54" && return
10505
10506         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10507         cancel_lru_locks mdc
10508
10509         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10510         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10511         [ $unused -eq 0 ] || error "$unused locks are not cleared"
10512
10513         local nr=1000
10514         createmany -o $DIR/$tdir/f $nr ||
10515                 error "failed to create $nr files in $DIR/$tdir"
10516         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10517
10518         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
10519         do_facet mds1 $LCTL set_param fail_loc=0x327
10520         do_facet mds1 $LCTL set_param fail_val=500
10521         touch $DIR/$tdir/m
10522
10523         echo "sleep 10 seconds ..."
10524         sleep 10
10525         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
10526
10527         do_facet mds1 $LCTL set_param fail_loc=0
10528         do_facet mds1 $LCTL set_param fail_val=0
10529         [ $lck_cnt -lt $unused ] ||
10530                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
10531
10532         rm $DIR/$tdir/m
10533         unlinkmany $DIR/$tdir/f $nr
10534 }
10535 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
10536
10537 test_134b() {
10538         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10539         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10540                 skip "Need MDS version at least 2.7.54" && return
10541
10542         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10543         cancel_lru_locks mdc
10544
10545         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10546                         ldlm.lock_reclaim_threshold_mb)
10547         # disable reclaim temporarily
10548         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10549
10550         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10551         do_facet mds1 $LCTL set_param fail_loc=0x328
10552         do_facet mds1 $LCTL set_param fail_val=500
10553
10554         $LCTL set_param debug=+trace
10555
10556         local nr=600
10557         createmany -o $DIR/$tdir/f $nr &
10558         local create_pid=$!
10559
10560         echo "Sleep $TIMEOUT seconds ..."
10561         sleep $TIMEOUT
10562         if ! ps -p $create_pid  > /dev/null 2>&1; then
10563                 do_facet mds1 $LCTL set_param fail_loc=0
10564                 do_facet mds1 $LCTL set_param fail_val=0
10565                 do_facet mds1 $LCTL set_param \
10566                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10567                 error "createmany finished incorrectly!"
10568         fi
10569         do_facet mds1 $LCTL set_param fail_loc=0
10570         do_facet mds1 $LCTL set_param fail_val=0
10571         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10572         wait $create_pid || return 1
10573
10574         unlinkmany $DIR/$tdir/f $nr
10575 }
10576 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10577
10578 test_140() { #bug-17379
10579         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10580         test_mkdir $DIR/$tdir
10581         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10582         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10583
10584         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10585         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10586         local i=0
10587         while i=$((i + 1)); do
10588                 test_mkdir $i
10589                 cd $i || error "Changing to $i"
10590                 ln -s ../stat stat || error "Creating stat symlink"
10591                 # Read the symlink until ELOOP present,
10592                 # not LBUGing the system is considered success,
10593                 # we didn't overrun the stack.
10594                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10595                 if [ $ret -ne 0 ]; then
10596                         if [ $ret -eq 40 ]; then
10597                                 break  # -ELOOP
10598                         else
10599                                 error "Open stat symlink"
10600                                         return
10601                         fi
10602                 fi
10603         done
10604         i=$((i - 1))
10605         echo "The symlink depth = $i"
10606         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10607                                         error "Invalid symlink depth"
10608
10609         # Test recursive symlink
10610         ln -s symlink_self symlink_self
10611         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10612         echo "open symlink_self returns $ret"
10613         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10614 }
10615 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10616
10617 test_150() {
10618         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10619         local TF="$TMP/$tfile"
10620
10621         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10622         cp $TF $DIR/$tfile
10623         cancel_lru_locks $OSC
10624         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10625         remount_client $MOUNT
10626         df -P $MOUNT
10627         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10628
10629         $TRUNCATE $TF 6000
10630         $TRUNCATE $DIR/$tfile 6000
10631         cancel_lru_locks $OSC
10632         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10633
10634         echo "12345" >>$TF
10635         echo "12345" >>$DIR/$tfile
10636         cancel_lru_locks $OSC
10637         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10638
10639         echo "12345" >>$TF
10640         echo "12345" >>$DIR/$tfile
10641         cancel_lru_locks $OSC
10642         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10643
10644         rm -f $TF
10645         true
10646 }
10647 run_test 150 "truncate/append tests"
10648
10649 #LU-2902 roc_hit was not able to read all values from lproc
10650 function roc_hit_init() {
10651         local list=$(comma_list $(osts_nodes))
10652         local dir=$DIR/$tdir-check
10653         local file=$dir/$tfile
10654         local BEFORE
10655         local AFTER
10656         local idx
10657
10658         test_mkdir $dir
10659         #use setstripe to do a write to every ost
10660         for i in $(seq 0 $((OSTCOUNT-1))); do
10661                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10662                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10663                 idx=$(printf %04x $i)
10664                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10665                         awk '$1 == "cache_access" {sum += $7}
10666                                 END { printf("%0.0f", sum) }')
10667
10668                 cancel_lru_locks osc
10669                 cat $file >/dev/null
10670
10671                 AFTER=$(get_osd_param $list *OST*$idx stats |
10672                         awk '$1 == "cache_access" {sum += $7}
10673                                 END { printf("%0.0f", sum) }')
10674
10675                 echo BEFORE:$BEFORE AFTER:$AFTER
10676                 if ! let "AFTER - BEFORE == 4"; then
10677                         rm -rf $dir
10678                         error "roc_hit is not safe to use"
10679                 fi
10680                 rm $file
10681         done
10682
10683         rm -rf $dir
10684 }
10685
10686 function roc_hit() {
10687         local list=$(comma_list $(osts_nodes))
10688         echo $(get_osd_param $list '' stats |
10689                 awk '$1 == "cache_hit" {sum += $7}
10690                         END { printf("%0.0f", sum) }')
10691 }
10692
10693 function set_cache() {
10694         local on=1
10695
10696         if [ "$2" == "off" ]; then
10697                 on=0;
10698         fi
10699         local list=$(comma_list $(osts_nodes))
10700         set_osd_param $list '' $1_cache_enable $on
10701
10702         cancel_lru_locks osc
10703 }
10704
10705 test_151() {
10706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10707         remote_ost_nodsh && skip "remote OST with nodsh" && return
10708
10709         local CPAGES=3
10710         local list=$(comma_list $(osts_nodes))
10711
10712         # check whether obdfilter is cache capable at all
10713         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10714                 echo "not cache-capable obdfilter"
10715                 return 0
10716         fi
10717
10718         # check cache is enabled on all obdfilters
10719         if get_osd_param $list '' read_cache_enable | grep 0; then
10720                 echo "oss cache is disabled"
10721                 return 0
10722         fi
10723
10724         set_osd_param $list '' writethrough_cache_enable 1
10725
10726         # check write cache is enabled on all obdfilters
10727         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10728                 echo "oss write cache is NOT enabled"
10729                 return 0
10730         fi
10731
10732         roc_hit_init
10733
10734         #define OBD_FAIL_OBD_NO_LRU  0x609
10735         do_nodes $list $LCTL set_param fail_loc=0x609
10736
10737         # pages should be in the case right after write
10738         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10739                 error "dd failed"
10740
10741         local BEFORE=$(roc_hit)
10742         cancel_lru_locks osc
10743         cat $DIR/$tfile >/dev/null
10744         local AFTER=$(roc_hit)
10745
10746         do_nodes $list $LCTL set_param fail_loc=0
10747
10748         if ! let "AFTER - BEFORE == CPAGES"; then
10749                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10750         fi
10751
10752         # the following read invalidates the cache
10753         cancel_lru_locks osc
10754         set_osd_param $list '' read_cache_enable 0
10755         cat $DIR/$tfile >/dev/null
10756
10757         # now data shouldn't be found in the cache
10758         BEFORE=$(roc_hit)
10759         cancel_lru_locks osc
10760         cat $DIR/$tfile >/dev/null
10761         AFTER=$(roc_hit)
10762         if let "AFTER - BEFORE != 0"; then
10763                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10764         fi
10765
10766         set_osd_param $list '' read_cache_enable 1
10767         rm -f $DIR/$tfile
10768 }
10769 run_test 151 "test cache on oss and controls ==============================="
10770
10771 test_152() {
10772         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10773         local TF="$TMP/$tfile"
10774
10775         # simulate ENOMEM during write
10776 #define OBD_FAIL_OST_NOMEM      0x226
10777         lctl set_param fail_loc=0x80000226
10778         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10779         cp $TF $DIR/$tfile
10780         sync || error "sync failed"
10781         lctl set_param fail_loc=0
10782
10783         # discard client's cache
10784         cancel_lru_locks osc
10785
10786         # simulate ENOMEM during read
10787         lctl set_param fail_loc=0x80000226
10788         cmp $TF $DIR/$tfile || error "cmp failed"
10789         lctl set_param fail_loc=0
10790
10791         rm -f $TF
10792 }
10793 run_test 152 "test read/write with enomem ============================"
10794
10795 test_153() {
10796         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10797 }
10798 run_test 153 "test if fdatasync does not crash ======================="
10799
10800 dot_lustre_fid_permission_check() {
10801         local fid=$1
10802         local ffid=$MOUNT/.lustre/fid/$fid
10803         local test_dir=$2
10804
10805         echo "stat fid $fid"
10806         stat $ffid > /dev/null || error "stat $ffid failed."
10807         echo "touch fid $fid"
10808         touch $ffid || error "touch $ffid failed."
10809         echo "write to fid $fid"
10810         cat /etc/hosts > $ffid || error "write $ffid failed."
10811         echo "read fid $fid"
10812         diff /etc/hosts $ffid || error "read $ffid failed."
10813         echo "append write to fid $fid"
10814         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10815         echo "rename fid $fid"
10816         mv $ffid $test_dir/$tfile.1 &&
10817                 error "rename $ffid to $tfile.1 should fail."
10818         touch $test_dir/$tfile.1
10819         mv $test_dir/$tfile.1 $ffid &&
10820                 error "rename $tfile.1 to $ffid should fail."
10821         rm -f $test_dir/$tfile.1
10822         echo "truncate fid $fid"
10823         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10824         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10825                 echo "link fid $fid"
10826                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10827         fi
10828         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10829                 echo "setfacl fid $fid"
10830                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10831                 echo "getfacl fid $fid"
10832                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10833         fi
10834         echo "unlink fid $fid"
10835         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10836         echo "mknod fid $fid"
10837         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10838
10839         fid=[0xf00000400:0x1:0x0]
10840         ffid=$MOUNT/.lustre/fid/$fid
10841
10842         echo "stat non-exist fid $fid"
10843         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10844         echo "write to non-exist fid $fid"
10845         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10846         echo "link new fid $fid"
10847         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10848
10849         mkdir -p $test_dir/$tdir
10850         touch $test_dir/$tdir/$tfile
10851         fid=$($LFS path2fid $test_dir/$tdir)
10852         rc=$?
10853         [ $rc -ne 0 ] &&
10854                 error "error: could not get fid for $test_dir/$dir/$tfile."
10855
10856         ffid=$MOUNT/.lustre/fid/$fid
10857
10858         echo "ls $fid"
10859         ls $ffid > /dev/null || error "ls $ffid failed."
10860         echo "touch $fid/$tfile.1"
10861         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10862
10863         echo "touch $MOUNT/.lustre/fid/$tfile"
10864         touch $MOUNT/.lustre/fid/$tfile && \
10865                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10866
10867         echo "setxattr to $MOUNT/.lustre/fid"
10868         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10869
10870         echo "listxattr for $MOUNT/.lustre/fid"
10871         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10872
10873         echo "delxattr from $MOUNT/.lustre/fid"
10874         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10875
10876         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10877         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10878                 error "touch invalid fid should fail."
10879
10880         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10881         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10882                 error "touch non-normal fid should fail."
10883
10884         echo "rename $tdir to $MOUNT/.lustre/fid"
10885         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10886                 error "rename to $MOUNT/.lustre/fid should fail."
10887
10888         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10889         then            # LU-3547
10890                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10891                 local new_obf_mode=777
10892
10893                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10894                 chmod $new_obf_mode $DIR/.lustre/fid ||
10895                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10896
10897                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10898                 [ $obf_mode -eq $new_obf_mode ] ||
10899                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10900
10901                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10902                 chmod $old_obf_mode $DIR/.lustre/fid ||
10903                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10904         fi
10905
10906         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10907         fid=$($LFS path2fid $test_dir/$tfile-2)
10908
10909         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10910         then # LU-5424
10911                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10912                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10913                         error "create lov data thru .lustre failed"
10914         fi
10915         echo "cp /etc/passwd $test_dir/$tfile-2"
10916         cp /etc/passwd $test_dir/$tfile-2 ||
10917                 error "copy to $test_dir/$tfile-2 failed."
10918         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10919         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10920                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10921
10922         rm -rf $test_dir/tfile.lnk
10923         rm -rf $test_dir/$tfile-2
10924 }
10925
10926 test_154A() {
10927         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10928                 skip "Need MDS version at least 2.4.1" && return
10929
10930         local tf=$DIR/$tfile
10931         touch $tf
10932
10933         local fid=$($LFS path2fid $tf)
10934         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10935
10936         # check that we get the same pathname back
10937         local found=$($LFS fid2path $MOUNT "$fid")
10938         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10939         [ "$found" == "$tf" ] ||
10940                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10941 }
10942 run_test 154A "lfs path2fid and fid2path basic checks"
10943
10944 test_154B() {
10945         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10946                 skip "Need MDS version at least 2.4.1" && return
10947
10948         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10949         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10950         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10951         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10952
10953         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10954         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10955
10956         # check that we get the same pathname
10957         echo "PFID: $PFID, name: $name"
10958         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10959         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10960         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10961                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10962
10963         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10964 }
10965 run_test 154B "verify the ll_decode_linkea tool"
10966
10967 test_154a() {
10968         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10969         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10970                 { skip "Need MDS version at least 2.2.51"; return 0; }
10971         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10972         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10973
10974         cp /etc/hosts $DIR/$tfile
10975
10976         fid=$($LFS path2fid $DIR/$tfile)
10977         rc=$?
10978         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
10979
10980         dot_lustre_fid_permission_check "$fid" $DIR ||
10981                 error "dot lustre permission check $fid failed"
10982
10983         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
10984
10985         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
10986
10987         touch $MOUNT/.lustre/file &&
10988                 error "creation is not allowed under .lustre"
10989
10990         mkdir $MOUNT/.lustre/dir &&
10991                 error "mkdir is not allowed under .lustre"
10992
10993         rm -rf $DIR/$tfile
10994 }
10995 run_test 154a "Open-by-FID"
10996
10997 test_154b() {
10998         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10999         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
11000                 { skip "Need MDS version at least 2.2.51"; return 0; }
11001         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11002
11003         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11004
11005         local remote_dir=$DIR/$tdir/remote_dir
11006         local MDTIDX=1
11007         local rc=0
11008
11009         mkdir -p $DIR/$tdir
11010         $LFS mkdir -i $MDTIDX $remote_dir ||
11011                 error "create remote directory failed"
11012
11013         cp /etc/hosts $remote_dir/$tfile
11014
11015         fid=$($LFS path2fid $remote_dir/$tfile)
11016         rc=$?
11017         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
11018
11019         dot_lustre_fid_permission_check "$fid" $remote_dir ||
11020                 error "dot lustre permission check $fid failed"
11021         rm -rf $DIR/$tdir
11022 }
11023 run_test 154b "Open-by-FID for remote directory"
11024
11025 test_154c() {
11026         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11027                 skip "Need MDS version at least 2.4.1" && return
11028
11029         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
11030         local FID1=$($LFS path2fid $DIR/$tfile.1)
11031         local FID2=$($LFS path2fid $DIR/$tfile.2)
11032         local FID3=$($LFS path2fid $DIR/$tfile.3)
11033
11034         local N=1
11035         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
11036                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
11037                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
11038                 local want=FID$N
11039                 [ "$FID" = "${!want}" ] ||
11040                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
11041                 N=$((N + 1))
11042         done
11043
11044         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
11045         do
11046                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
11047                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
11048                 N=$((N + 1))
11049         done
11050 }
11051 run_test 154c "lfs path2fid and fid2path multiple arguments"
11052
11053 test_154d() {
11054         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11055         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
11056                 skip "Need MDS version at least 2.5.53" && return
11057
11058         if remote_mds; then
11059                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
11060         else
11061                 nid="0@lo"
11062         fi
11063         local proc_ofile="mdt.*.exports.'$nid'.open_files"
11064         local fd
11065         local cmd
11066
11067         rm -f $DIR/$tfile
11068         touch $DIR/$tfile
11069
11070         local fid=$($LFS path2fid $DIR/$tfile)
11071         # Open the file
11072         fd=$(free_fd)
11073         cmd="exec $fd<$DIR/$tfile"
11074         eval $cmd
11075         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
11076         echo "$fid_list" | grep "$fid"
11077         rc=$?
11078
11079         cmd="exec $fd>/dev/null"
11080         eval $cmd
11081         if [ $rc -ne 0 ]; then
11082                 error "FID $fid not found in open files list $fid_list"
11083         fi
11084 }
11085 run_test 154d "Verify open file fid"
11086
11087 test_154e()
11088 {
11089         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
11090                 skip "Need MDS version at least 2.6.50" && return
11091
11092         if ls -a $MOUNT | grep -q '^\.lustre$'; then
11093                 error ".lustre returned by readdir"
11094         fi
11095 }
11096 run_test 154e ".lustre is not returned by readdir"
11097
11098 test_154f() {
11099         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11100         # create parent directory on a single MDT to avoid cross-MDT hardlinks
11101         test_mkdir -p -c1 $DIR/$tdir/d
11102         # test dirs inherit from its stripe
11103         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
11104         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
11105         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
11106         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
11107         touch $DIR/f
11108
11109         # get fid of parents
11110         local FID0=$($LFS path2fid $DIR/$tdir/d)
11111         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
11112         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
11113         local FID3=$($LFS path2fid $DIR)
11114
11115         # check that path2fid --parents returns expected <parent_fid>/name
11116         # 1) test for a directory (single parent)
11117         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
11118         [ "$parent" == "$FID0/foo1" ] ||
11119                 error "expected parent: $FID0/foo1, got: $parent"
11120
11121         # 2) test for a file with nlink > 1 (multiple parents)
11122         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
11123         echo "$parent" | grep -F "$FID1/$tfile" ||
11124                 error "$FID1/$tfile not returned in parent list"
11125         echo "$parent" | grep -F "$FID2/link" ||
11126                 error "$FID2/link not returned in parent list"
11127
11128         # 3) get parent by fid
11129         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
11130         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11131         echo "$parent" | grep -F "$FID1/$tfile" ||
11132                 error "$FID1/$tfile not returned in parent list (by fid)"
11133         echo "$parent" | grep -F "$FID2/link" ||
11134                 error "$FID2/link not returned in parent list (by fid)"
11135
11136         # 4) test for entry in root directory
11137         parent=$($LFS path2fid --parents $DIR/f)
11138         echo "$parent" | grep -F "$FID3/f" ||
11139                 error "$FID3/f not returned in parent list"
11140
11141         # 5) test it on root directory
11142         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
11143                 error "$MOUNT should not have parents"
11144
11145         # enable xattr caching and check that linkea is correctly updated
11146         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11147         save_lustre_params client "llite.*.xattr_cache" > $save
11148         lctl set_param llite.*.xattr_cache 1
11149
11150         # 6.1) linkea update on rename
11151         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
11152
11153         # get parents by fid
11154         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11155         # foo1 should no longer be returned in parent list
11156         echo "$parent" | grep -F "$FID1" &&
11157                 error "$FID1 should no longer be in parent list"
11158         # the new path should appear
11159         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
11160                 error "$FID2/$tfile.moved is not in parent list"
11161
11162         # 6.2) linkea update on unlink
11163         rm -f $DIR/$tdir/d/foo2/link
11164         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11165         # foo2/link should no longer be returned in parent list
11166         echo "$parent" | grep -F "$FID2/link" &&
11167                 error "$FID2/link should no longer be in parent list"
11168         true
11169
11170         rm -f $DIR/f
11171         restore_lustre_params < $save
11172         rm -f $save
11173 }
11174 run_test 154f "get parent fids by reading link ea"
11175
11176 test_154g()
11177 {
11178         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
11179            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
11180                 { skip "Need MDS version at least 2.6.92"; return 0; }
11181         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11182
11183         mkdir -p $DIR/$tdir
11184         llapi_fid_test -d $DIR/$tdir
11185 }
11186 run_test 154g "various llapi FID tests"
11187
11188 test_155_small_load() {
11189     local temp=$TMP/$tfile
11190     local file=$DIR/$tfile
11191
11192     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
11193         error "dd of=$temp bs=6096 count=1 failed"
11194     cp $temp $file
11195     cancel_lru_locks $OSC
11196     cmp $temp $file || error "$temp $file differ"
11197
11198     $TRUNCATE $temp 6000
11199     $TRUNCATE $file 6000
11200     cmp $temp $file || error "$temp $file differ (truncate1)"
11201
11202     echo "12345" >>$temp
11203     echo "12345" >>$file
11204     cmp $temp $file || error "$temp $file differ (append1)"
11205
11206     echo "12345" >>$temp
11207     echo "12345" >>$file
11208     cmp $temp $file || error "$temp $file differ (append2)"
11209
11210     rm -f $temp $file
11211     true
11212 }
11213
11214 test_155_big_load() {
11215     remote_ost_nodsh && skip "remote OST with nodsh" && return
11216     local temp=$TMP/$tfile
11217     local file=$DIR/$tfile
11218
11219     free_min_max
11220     local cache_size=$(do_facet ost$((MAXI+1)) \
11221         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
11222     local large_file_size=$((cache_size * 2))
11223
11224     echo "OSS cache size: $cache_size KB"
11225     echo "Large file size: $large_file_size KB"
11226
11227     [ $MAXV -le $large_file_size ] && \
11228         skip_env "max available OST size needs > $large_file_size KB" && \
11229         return 0
11230
11231     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
11232
11233     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
11234         error "dd of=$temp bs=$large_file_size count=1k failed"
11235     cp $temp $file
11236     ls -lh $temp $file
11237     cancel_lru_locks osc
11238     cmp $temp $file || error "$temp $file differ"
11239
11240     rm -f $temp $file
11241     true
11242 }
11243
11244 save_writethrough() {
11245         local facets=$(get_facets OST)
11246
11247         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
11248         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
11249 }
11250
11251 test_155a() {
11252         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11253         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11254         save_writethrough $p
11255
11256         set_cache read on
11257         set_cache writethrough on
11258         test_155_small_load
11259         restore_lustre_params < $p
11260         rm -f $p
11261 }
11262 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
11263
11264 test_155b() {
11265         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11266         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11267         save_writethrough $p
11268
11269         set_cache read on
11270         set_cache writethrough off
11271         test_155_small_load
11272         restore_lustre_params < $p
11273         rm -f $p
11274 }
11275 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
11276
11277 test_155c() {
11278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11279         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11280         save_writethrough $p
11281
11282         set_cache read off
11283         set_cache writethrough on
11284         test_155_small_load
11285         restore_lustre_params < $p
11286         rm -f $p
11287 }
11288 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
11289
11290 test_155d() {
11291         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11292         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11293         save_writethrough $p
11294
11295         set_cache read off
11296         set_cache writethrough off
11297         test_155_small_load
11298         restore_lustre_params < $p
11299         rm -f $p
11300 }
11301 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
11302
11303 test_155e() {
11304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11305         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11306         save_writethrough $p
11307
11308         set_cache read on
11309         set_cache writethrough on
11310         test_155_big_load
11311         restore_lustre_params < $p
11312         rm -f $p
11313 }
11314 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11315
11316 test_155f() {
11317         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11318         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11319         save_writethrough $p
11320
11321         set_cache read on
11322         set_cache writethrough off
11323         test_155_big_load
11324         restore_lustre_params < $p
11325         rm -f $p
11326 }
11327 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11328
11329 test_155g() {
11330         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11331         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11332         save_writethrough $p
11333
11334         set_cache read off
11335         set_cache writethrough on
11336         test_155_big_load
11337         restore_lustre_params < $p
11338         rm -f $p
11339 }
11340 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11341
11342 test_155h() {
11343         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11344         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11345         save_writethrough $p
11346
11347         set_cache read off
11348         set_cache writethrough off
11349         test_155_big_load
11350         restore_lustre_params < $p
11351         rm -f $p
11352 }
11353 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11354
11355 test_156() {
11356         remote_ost_nodsh && skip "remote OST with nodsh" && return
11357         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11358         local CPAGES=3
11359         local BEFORE
11360         local AFTER
11361         local file="$DIR/$tfile"
11362         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11363
11364         [ "$(facet_fstype ost1)" = "zfs" -a \
11365            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11366                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
11367                 return
11368
11369         save_writethrough $p
11370         roc_hit_init
11371
11372         log "Turn on read and write cache"
11373         set_cache read on
11374         set_cache writethrough on
11375
11376         log "Write data and read it back."
11377         log "Read should be satisfied from the cache."
11378         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11379         BEFORE=$(roc_hit)
11380         cancel_lru_locks osc
11381         cat $file >/dev/null
11382         AFTER=$(roc_hit)
11383         if ! let "AFTER - BEFORE == CPAGES"; then
11384                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11385         else
11386                 log "cache hits:: before: $BEFORE, after: $AFTER"
11387         fi
11388
11389         log "Read again; it should be satisfied from the cache."
11390         BEFORE=$AFTER
11391         cancel_lru_locks osc
11392         cat $file >/dev/null
11393         AFTER=$(roc_hit)
11394         if ! let "AFTER - BEFORE == CPAGES"; then
11395                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11396         else
11397                 log "cache hits:: before: $BEFORE, after: $AFTER"
11398         fi
11399
11400         log "Turn off the read cache and turn on the write cache"
11401         set_cache read off
11402         set_cache writethrough on
11403
11404         log "Read again; it should be satisfied from the cache."
11405         BEFORE=$(roc_hit)
11406         cancel_lru_locks osc
11407         cat $file >/dev/null
11408         AFTER=$(roc_hit)
11409         if ! let "AFTER - BEFORE == CPAGES"; then
11410                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11411         else
11412                 log "cache hits:: before: $BEFORE, after: $AFTER"
11413         fi
11414
11415         log "Read again; it should not be satisfied from the cache."
11416         BEFORE=$AFTER
11417         cancel_lru_locks osc
11418         cat $file >/dev/null
11419         AFTER=$(roc_hit)
11420         if ! let "AFTER - BEFORE == 0"; then
11421                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11422         else
11423                 log "cache hits:: before: $BEFORE, after: $AFTER"
11424         fi
11425
11426         log "Write data and read it back."
11427         log "Read should be satisfied from the cache."
11428         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11429         BEFORE=$(roc_hit)
11430         cancel_lru_locks osc
11431         cat $file >/dev/null
11432         AFTER=$(roc_hit)
11433         if ! let "AFTER - BEFORE == CPAGES"; then
11434                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11435         else
11436                 log "cache hits:: before: $BEFORE, after: $AFTER"
11437         fi
11438
11439         log "Read again; it should not be satisfied from the cache."
11440         BEFORE=$AFTER
11441         cancel_lru_locks osc
11442         cat $file >/dev/null
11443         AFTER=$(roc_hit)
11444         if ! let "AFTER - BEFORE == 0"; then
11445                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11446         else
11447                 log "cache hits:: before: $BEFORE, after: $AFTER"
11448         fi
11449
11450         log "Turn off read and write cache"
11451         set_cache read off
11452         set_cache writethrough off
11453
11454         log "Write data and read it back"
11455         log "It should not be satisfied from the cache."
11456         rm -f $file
11457         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11458         cancel_lru_locks osc
11459         BEFORE=$(roc_hit)
11460         cat $file >/dev/null
11461         AFTER=$(roc_hit)
11462         if ! let "AFTER - BEFORE == 0"; then
11463                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11464         else
11465                 log "cache hits:: before: $BEFORE, after: $AFTER"
11466         fi
11467
11468         log "Turn on the read cache and turn off the write cache"
11469         set_cache read on
11470         set_cache writethrough off
11471
11472         log "Write data and read it back"
11473         log "It should not be satisfied from the cache."
11474         rm -f $file
11475         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11476         BEFORE=$(roc_hit)
11477         cancel_lru_locks osc
11478         cat $file >/dev/null
11479         AFTER=$(roc_hit)
11480         if ! let "AFTER - BEFORE == 0"; then
11481                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11482         else
11483                 log "cache hits:: before: $BEFORE, after: $AFTER"
11484         fi
11485
11486         log "Read again; it should be satisfied from the cache."
11487         BEFORE=$(roc_hit)
11488         cancel_lru_locks osc
11489         cat $file >/dev/null
11490         AFTER=$(roc_hit)
11491         if ! let "AFTER - BEFORE == CPAGES"; then
11492                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11493         else
11494                 log "cache hits:: before: $BEFORE, after: $AFTER"
11495         fi
11496
11497         restore_lustre_params < $p
11498         rm -f $p $file
11499 }
11500 run_test 156 "Verification of tunables"
11501
11502 #Changelogs
11503 cleanup_changelog () {
11504         trap 0
11505         echo "Deregistering changelog client $CL_USER"
11506         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11507 }
11508
11509 err17935 () {
11510         if [[ $MDSCOUNT -gt 1 ]]; then
11511                 error_ignore bz17935 $*
11512         else
11513                 error $*
11514         fi
11515 }
11516
11517 changelog_chmask()
11518 {
11519         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11520
11521         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
11522
11523         if [ $MASK -eq 1 ]; then
11524                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
11525         else
11526                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
11527         fi
11528 }
11529
11530 changelog_extract_field() {
11531         local mdt=$1
11532         local cltype=$2
11533         local file=$3
11534         local identifier=$4
11535
11536         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
11537                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
11538                 tail -1
11539 }
11540
11541 test_160a() {
11542         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11543         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11544         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11545                 { skip "Need MDS version at least 2.2.0"; return; }
11546
11547         local CL_USERS="mdd.$MDT0.changelog_users"
11548         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11549         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11550                 changelog_register -n)
11551         echo "Registered as changelog user $CL_USER"
11552         trap cleanup_changelog EXIT
11553         $GET_CL_USERS | grep -q $CL_USER ||
11554                 error "User $CL_USER not found in changelog_users"
11555
11556         # change something
11557         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11558         touch $DIR/$tdir/pics/2008/zachy/timestamp
11559         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11560         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11561         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11562         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11563         rm $DIR/$tdir/pics/desktop.jpg
11564
11565         $LFS changelog $MDT0 | tail -5
11566
11567         echo "verifying changelog mask"
11568         changelog_chmask "MKDIR"
11569         changelog_chmask "CLOSE"
11570
11571         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11572         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11573
11574         changelog_chmask "MKDIR"
11575         changelog_chmask "CLOSE"
11576
11577         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11578         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11579
11580         $LFS changelog $MDT0
11581         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11582         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11583         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11584         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11585
11586         # verify contents
11587         echo "verifying target fid"
11588         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11589         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11590         [ "$fidc" == "$fidf" ] ||
11591                 err17935 "fid in changelog $fidc != file fid $fidf"
11592         echo "verifying parent fid"
11593         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11594         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11595         [ "$fidc" == "$fidf" ] ||
11596                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11597
11598         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11599         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11600         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11601         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11602         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11603                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11604
11605         MIN_REC=$($GET_CL_USERS |
11606                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11607         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11608         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11609         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11610                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11611
11612         # LU-3446 changelog index reset on MDT restart
11613         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11614         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11615         $LFS changelog_clear $MDT0 $CL_USER 0
11616         stop $SINGLEMDS || error "Fail to stop MDT."
11617         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11618         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11619         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11620         [ $CUR_REC1 == $CUR_REC2 ] ||
11621                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11622
11623         echo "verifying user deregister"
11624         cleanup_changelog
11625         $GET_CL_USERS | grep -q $CL_USER &&
11626                 error "User $CL_USER still in changelog_users"
11627
11628         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11629         if [ $CL_USER -eq 0 ]; then
11630                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11631                 touch $DIR/$tdir/chloe
11632                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11633                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11634                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11635         else
11636                 echo "$CL_USER other changelog users; can't verify off"
11637         fi
11638 }
11639 run_test 160a "changelog sanity"
11640
11641 test_160b() { # LU-3587
11642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11643         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11644         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11645                 { skip "Need MDS version at least 2.2.0"; return; }
11646
11647         local CL_USERS="mdd.$MDT0.changelog_users"
11648         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11649         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11650                 changelog_register -n)
11651         echo "Registered as changelog user $CL_USER"
11652         trap cleanup_changelog EXIT
11653         $GET_CL_USERS | grep -q $CL_USER ||
11654                 error "User $CL_USER not found in changelog_users"
11655
11656         local LONGNAME1=$(str_repeat a 255)
11657         local LONGNAME2=$(str_repeat b 255)
11658
11659         cd $DIR
11660         echo "creating very long named file"
11661         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11662         echo "moving very long named file"
11663         mv $LONGNAME1 $LONGNAME2
11664
11665         $LFS changelog $MDT0 | grep RENME
11666         rm -f $LONGNAME2
11667         cleanup_changelog
11668 }
11669 run_test 160b "Verify that very long rename doesn't crash in changelog"
11670
11671 test_160c() {
11672         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11673
11674         local rc=0
11675         local server_version=$(lustre_version_code $SINGLEMDS)
11676
11677         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11678                 [[ $server_version -gt $(version_code 2.5.1) &&
11679                    $server_version -lt $(version_code 2.5.50) ]] ||
11680                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11681         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11682
11683         # Registration step
11684         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11685                 changelog_register -n)
11686         trap cleanup_changelog EXIT
11687
11688         rm -rf $DIR/$tdir
11689         mkdir -p $DIR/$tdir
11690         $MCREATE $DIR/$tdir/foo_160c
11691         changelog_chmask "TRUNC"
11692         $TRUNCATE $DIR/$tdir/foo_160c 200
11693         changelog_chmask "TRUNC"
11694         $TRUNCATE $DIR/$tdir/foo_160c 199
11695         $LFS changelog $MDT0
11696         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11697         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11698         $LFS changelog_clear $MDT0 $CL_USER 0
11699
11700         # Deregistration step
11701         cleanup_changelog
11702 }
11703 run_test 160c "verify that changelog log catch the truncate event"
11704
11705 test_160d() {
11706         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11707         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11708
11709         local server_version=$(lustre_version_code mds1)
11710         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11711
11712         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11713                 { skip "Need MDS version at least 2.7.60+"; return; }
11714         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11715
11716         # Registration step
11717         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11718                 changelog_register -n)
11719
11720         trap cleanup_changelog EXIT
11721         mkdir -p $DIR/$tdir/migrate_dir
11722         $LFS changelog_clear $MDT0 $CL_USER 0
11723
11724         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11725         $LFS changelog $MDT0
11726         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11727         $LFS changelog_clear $MDT0 $CL_USER 0
11728         [ $MIGRATES -eq 1 ] ||
11729                 error "MIGRATE changelog mask count $MIGRATES != 1"
11730
11731         # Deregistration step
11732         cleanup_changelog
11733 }
11734 run_test 160d "verify that changelog log catch the migrate event"
11735
11736 test_160e() {
11737         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11738
11739         # Create a user
11740         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11741                 changelog_register -n)
11742         echo "Registered as changelog user $CL_USER"
11743         trap cleanup_changelog EXIT
11744
11745         # Delete a future user (expect fail)
11746         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11747         local rc=$?
11748
11749         if [ $rc -eq 0 ]; then
11750                 error "Deleted non-existant user cl77"
11751         elif [ $rc -ne 2 ]; then
11752                 error "changelog_deregister failed with $rc, " \
11753                         "expected 2 (ENOENT)"
11754         fi
11755
11756         # Clear to a bad index (1 billion should be safe)
11757         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11758         rc=$?
11759
11760         if [ $rc -eq 0 ]; then
11761                 error "Successfully cleared to invalid CL index"
11762         elif [ $rc -ne 22 ]; then
11763                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11764         fi
11765 }
11766 run_test 160e "changelog negative testing"
11767
11768 cleanup_160f() {
11769         trap 0
11770         do_facet $SINGLEMDS $LCTL set_param fail_loc=0 fail_val=0
11771         echo "Deregistering changelog client $CL_USER"
11772         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11773         echo "Deregistering changelog client $CL_USER2"
11774         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER2
11775         restore_lustre_params < $save_params
11776         rm -f $save_params
11777 }
11778
11779 test_160f() {
11780         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11781         # should be set by default
11782
11783         local CL_USERS="mdd.$MDT0.changelog_users"
11784         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11785         local save_params="$TMP/sanity-$TESTNAME.parameters"
11786
11787         save_lustre_params $SINGLEMDS \
11788                 "mdd.$MDT0.changelog_max_idle_time" > $save_params
11789         save_lustre_params $SINGLEMDS \
11790                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11791         save_lustre_params $SINGLEMDS \
11792                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11793
11794         trap cleanup_160f EXIT
11795
11796         # Create a user
11797         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11798                 changelog_register -n)
11799         echo "Registered as changelog user $CL_USER"
11800         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11801                 changelog_register -n)
11802         echo "Registered as changelog user $CL_USER2"
11803         $GET_CL_USERS | grep -q $CL_USER ||
11804                 error "User $CL_USER not found in changelog_users"
11805         $GET_CL_USERS | grep -q $CL_USER2 ||
11806                 error "User $CL_USER2 not found in changelog_users"
11807
11808         # generate some changelogs to accumulate
11809         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11810         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11811         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11812         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11813
11814         # check changelogs have been generated
11815         nbcl=$($LFS changelog $MDT0 | wc -l)
11816         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11817
11818         do_facet $SINGLEMDS $LCTL set_param \
11819                 mdd.$MDT0.changelog_max_idle_time=10
11820         do_facet $SINGLEMDS $LCTL set_param \
11821                 mdd.$MDT0.changelog_min_gc_interval=2
11822         do_facet $SINGLEMDS $LCTL set_param \
11823                 mdd.$MDT0.changelog_min_free_cat_entries=3
11824
11825         # simulate changelog catalog almost full
11826 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11827         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11828         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11829
11830         sleep 6
11831         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11832         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 2))
11833         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11834         echo "verifying user clear: $(( $USER_REC1 + 2 )) == $USER_REC2"
11835         [ $USER_REC2 == $(($USER_REC1 + 2)) ] ||
11836                 error "user index expected $(($USER_REC1 + 2)) is $USER_REC2"
11837         sleep 5
11838
11839         # generate one more changelog to trigger fail_loc
11840         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11841
11842         # ensure gc thread is done
11843         wait_update_facet $SINGLEMDS \
11844                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11845
11846         # check user still registered
11847         $GET_CL_USERS | grep -q $CL_USER ||
11848                 error "User $CL_USER not found in changelog_users"
11849         # check user2 unregistered
11850         $GET_CL_USERS | grep -q $CL_USER2 &&
11851                 error "User $CL_USER2 still found in changelog_users"
11852
11853         # check changelogs are present and starting at $USER_REC2 + 1
11854         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11855         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11856         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11857                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11858
11859         cleanup_160f
11860 }
11861 run_test 160f "changelog garbage collect (timestamped users)"
11862
11863 test_160g() {
11864         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11865         # should be set by default
11866
11867         local CL_USERS="mdd.$MDT0.changelog_users"
11868         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11869         local save_params="$TMP/sanity-$TESTNAME.parameters"
11870
11871         save_lustre_params $SINGLEMDS \
11872                 "mdd.$MDT0.changelog_max_idle_indexes" > $save_params
11873         save_lustre_params $SINGLEMDS \
11874                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11875         save_lustre_params $SINGLEMDS \
11876                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11877
11878         trap cleanup_160f EXIT
11879
11880 #define OBD_FAIL_TIME_IN_CHLOG_USER                 0x1314
11881         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1314
11882
11883         # Create a user
11884         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11885                 changelog_register -n)
11886         echo "Registered as changelog user $CL_USER"
11887         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11888                 changelog_register -n)
11889         echo "Registered as changelog user $CL_USER2"
11890         $GET_CL_USERS | grep -q $CL_USER ||
11891                 error "User $CL_USER not found in changelog_users"
11892         $GET_CL_USERS | grep -q $CL_USER2 ||
11893                 error "User $CL_USER2 not found in changelog_users"
11894
11895         # generate some changelogs to accumulate
11896         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11897         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11898         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11899         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11900
11901         # check changelogs have been generated
11902         nbcl=$($LFS changelog $MDT0 | wc -l)
11903         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11904
11905         do_facet $SINGLEMDS $LCTL set_param \
11906                 mdd.$MDT0.changelog_max_idle_indexes=$((nbcl - 1))
11907         do_facet $SINGLEMDS $LCTL set_param \
11908                 mdd.$MDT0.changelog_min_gc_interval=2
11909         do_facet $SINGLEMDS $LCTL set_param \
11910                 mdd.$MDT0.changelog_min_free_cat_entries=3
11911
11912         # simulate changelog catalog almost full
11913 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11914         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11915         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11916
11917         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11918         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 3))
11919         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11920         echo "verifying user clear: $(( $USER_REC1 + 3 )) == $USER_REC2"
11921         [ $USER_REC2 == $(($USER_REC1 + 3)) ] ||
11922                 error "user index expected $(($USER_REC1 + 3)) is $USER_REC2"
11923
11924         # generate one more changelog to trigger fail_loc
11925         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11926
11927         # ensure gc thread is done
11928         wait_update_facet $SINGLEMDS \
11929                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11930
11931         # check user still registered
11932         $GET_CL_USERS | grep -q $CL_USER ||
11933                 error "User $CL_USER not found in changelog_users"
11934         # check user2 unregistered
11935         $GET_CL_USERS | grep -q $CL_USER2 &&
11936                 error "User $CL_USER2 still found in changelog_users"
11937
11938         # check changelogs are present and starting at $USER_REC2 + 1
11939         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11940         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11941         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11942                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11943
11944         cleanup_160f
11945 }
11946 run_test 160g "changelog garbage collect (old users)"
11947
11948 test_161a() {
11949         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11950         test_mkdir -c1 $DIR/$tdir
11951         cp /etc/hosts $DIR/$tdir/$tfile
11952         test_mkdir -c1 $DIR/$tdir/foo1
11953         test_mkdir -c1 $DIR/$tdir/foo2
11954         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11955         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11956         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11957         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11958         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11959         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11960                 $LFS fid2path $DIR $FID
11961                 err17935 "bad link ea"
11962         fi
11963     # middle
11964     rm $DIR/$tdir/foo2/zachary
11965     # last
11966     rm $DIR/$tdir/foo2/thor
11967     # first
11968     rm $DIR/$tdir/$tfile
11969     # rename
11970     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11971     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11972         then
11973         $LFS fid2path $DIR $FID
11974         err17935 "bad link rename"
11975     fi
11976     rm $DIR/$tdir/foo2/maggie
11977
11978         # overflow the EA
11979         local longname=filename_avg_len_is_thirty_two_
11980         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
11981                 error "failed to hardlink many files"
11982         links=$($LFS fid2path $DIR $FID | wc -l)
11983         echo -n "${links}/1000 links in link EA"
11984         [[ $links -gt 60 ]] ||
11985                 err17935 "expected at least 60 links in link EA"
11986         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
11987                 error "failed to unlink many hardlinks"
11988 }
11989 run_test 161a "link ea sanity"
11990
11991 test_161b() {
11992         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11993         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
11994         local MDTIDX=1
11995         local remote_dir=$DIR/$tdir/remote_dir
11996
11997         mkdir -p $DIR/$tdir
11998         $LFS mkdir -i $MDTIDX $remote_dir ||
11999                 error "create remote directory failed"
12000
12001         cp /etc/hosts $remote_dir/$tfile
12002         mkdir -p $remote_dir/foo1
12003         mkdir -p $remote_dir/foo2
12004         ln $remote_dir/$tfile $remote_dir/foo1/sofia
12005         ln $remote_dir/$tfile $remote_dir/foo2/zachary
12006         ln $remote_dir/$tfile $remote_dir/foo1/luna
12007         ln $remote_dir/$tfile $remote_dir/foo2/thor
12008
12009         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
12010                      tr -d ']')
12011         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
12012                 $LFS fid2path $DIR $FID
12013                 err17935 "bad link ea"
12014         fi
12015         # middle
12016         rm $remote_dir/foo2/zachary
12017         # last
12018         rm $remote_dir/foo2/thor
12019         # first
12020         rm $remote_dir/$tfile
12021         # rename
12022         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
12023         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
12024         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
12025                 $LFS fid2path $DIR $FID
12026                 err17935 "bad link rename"
12027         fi
12028         rm $remote_dir/foo2/maggie
12029
12030         # overflow the EA
12031         local longname=filename_avg_len_is_thirty_two_
12032         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
12033                 error "failed to hardlink many files"
12034         links=$($LFS fid2path $DIR $FID | wc -l)
12035         echo -n "${links}/1000 links in link EA"
12036         [[ ${links} -gt 60 ]] ||
12037                 err17935 "expected at least 60 links in link EA"
12038         unlinkmany $remote_dir/foo2/$longname 1000 ||
12039         error "failed to unlink many hardlinks"
12040 }
12041 run_test 161b "link ea sanity under remote directory"
12042
12043 test_161c() {
12044         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12045         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12046         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
12047                 skip "Need MDS version at least 2.1.5" && return
12048
12049         # define CLF_RENAME_LAST 0x0001
12050         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
12051         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
12052                 changelog_register -n)
12053
12054         trap cleanup_changelog EXIT
12055         rm -rf $DIR/$tdir
12056         mkdir -p $DIR/$tdir
12057         touch $DIR/$tdir/foo_161c
12058         touch $DIR/$tdir/bar_161c
12059         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12060         $LFS changelog $MDT0 | grep RENME
12061         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
12062                 cut -f5 -d' ')
12063         $LFS changelog_clear $MDT0 $CL_USER 0
12064         if [ x$flags != "x0x1" ]; then
12065                 error "flag $flags is not 0x1"
12066         fi
12067         echo "rename overwrite a target having nlink = 1," \
12068                 "changelog record has flags of $flags"
12069
12070         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
12071         touch $DIR/$tdir/foo_161c
12072         touch $DIR/$tdir/bar_161c
12073         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12074         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12075         $LFS changelog $MDT0 | grep RENME
12076         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
12077         $LFS changelog_clear $MDT0 $CL_USER 0
12078         if [ x$flags != "x0x0" ]; then
12079                 error "flag $flags is not 0x0"
12080         fi
12081         echo "rename overwrite a target having nlink > 1," \
12082                 "changelog record has flags of $flags"
12083
12084         # rename doesn't overwrite a target (changelog flag 0x0)
12085         touch $DIR/$tdir/foo_161c
12086         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
12087         $LFS changelog $MDT0 | grep RENME
12088         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
12089         $LFS changelog_clear $MDT0 $CL_USER 0
12090         if [ x$flags != "x0x0" ]; then
12091                 error "flag $flags is not 0x0"
12092         fi
12093         echo "rename doesn't overwrite a target," \
12094                 "changelog record has flags of $flags"
12095
12096         # define CLF_UNLINK_LAST 0x0001
12097         # unlink a file having nlink = 1 (changelog flag 0x1)
12098         rm -f $DIR/$tdir/foo2_161c
12099         $LFS changelog $MDT0 | grep UNLNK
12100         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
12101         $LFS changelog_clear $MDT0 $CL_USER 0
12102         if [ x$flags != "x0x1" ]; then
12103                 error "flag $flags is not 0x1"
12104         fi
12105         echo "unlink a file having nlink = 1," \
12106                 "changelog record has flags of $flags"
12107
12108         # unlink a file having nlink > 1 (changelog flag 0x0)
12109         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12110         rm -f $DIR/$tdir/foobar_161c
12111         $LFS changelog $MDT0 | grep UNLNK
12112         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
12113         $LFS changelog_clear $MDT0 $CL_USER 0
12114         if [ x$flags != "x0x0" ]; then
12115                 error "flag $flags is not 0x0"
12116         fi
12117         echo "unlink a file having nlink > 1," \
12118                 "changelog record has flags of $flags"
12119         cleanup_changelog
12120 }
12121 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
12122
12123 test_161d() {
12124         local user
12125         local pid
12126         local fid
12127
12128         # cleanup previous run
12129         rm -rf $DIR/$tdir/$tfile
12130
12131         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
12132                 changelog_register -n)
12133         [[ $? -eq 0 ]] || error "changelog_register failed"
12134
12135         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
12136         # interfer with $MOUNT/.lustre/fid/ access
12137         mkdir $DIR/$tdir
12138         [[ $? -eq 0 ]] || error "mkdir failed"
12139
12140         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
12141         $LCTL set_param fail_loc=0x8000140c
12142         # 5s pause
12143         $LCTL set_param fail_val=5
12144
12145         # create file
12146         echo foofoo > $DIR/$tdir/$tfile &
12147         pid=$!
12148
12149         # wait for create to be delayed
12150         sleep 2
12151
12152         ps -p $pid
12153         [[ $? -eq 0 ]] || error "create should be blocked"
12154
12155         local tempfile=$(mktemp)
12156         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
12157         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
12158         # some delay may occur during ChangeLog publishing and file read just
12159         # above, that could allow file write to happen finally
12160         [[ -s $tempfile ]] && echo "file should be empty"
12161
12162         $LCTL set_param fail_loc=0
12163
12164         wait $pid
12165         [[ $? -eq 0 ]] || error "create failed"
12166
12167         $LFS changelog_clear $MDT0 $user 0
12168         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
12169 }
12170 run_test 161d "create with concurrent .lustre/fid access"
12171
12172 check_path() {
12173     local expected=$1
12174     shift
12175     local fid=$2
12176
12177     local path=$(${LFS} fid2path $*)
12178     # Remove the '//' indicating a remote directory
12179     path=$(echo $path | sed 's#//#/#g')
12180     RC=$?
12181
12182     if [ $RC -ne 0 ]; then
12183         err17935 "path looked up of $expected failed. Error $RC"
12184         return $RC
12185     elif [ "${path}" != "${expected}" ]; then
12186         err17935 "path looked up \"${path}\" instead of \"${expected}\""
12187         return 2
12188     fi
12189     echo "fid $fid resolves to path $path (expected $expected)"
12190 }
12191
12192 test_162a() { # was test_162
12193         # Make changes to filesystem
12194         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12195         test_mkdir -p -c1 $DIR/$tdir/d2
12196         touch $DIR/$tdir/d2/$tfile
12197         touch $DIR/$tdir/d2/x1
12198         touch $DIR/$tdir/d2/x2
12199         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
12200         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
12201         # regular file
12202         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
12203         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
12204                 error "check path $tdir/d2/$tfile failed"
12205
12206         # softlink
12207         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
12208         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
12209         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
12210                 error "check path $tdir/d2/p/q/r/slink failed"
12211
12212         # softlink to wrong file
12213         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
12214         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
12215         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
12216                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
12217
12218         # hardlink
12219         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
12220         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
12221         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
12222         # fid2path dir/fsname should both work
12223         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
12224                 error "check path $tdir/d2/a/b/c/new_file failed"
12225         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
12226                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
12227
12228         # hardlink count: check that there are 2 links
12229         # Doesnt work with CMD yet: 17935
12230         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
12231                 err17935 "expected 2 links"
12232
12233         # hardlink indexing: remove the first link
12234         rm $DIR/$tdir/d2/p/q/r/hlink
12235         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
12236                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
12237
12238         return 0
12239 }
12240 run_test 162a "path lookup sanity"
12241
12242 test_162b() {
12243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12244         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12245
12246         mkdir $DIR/$tdir
12247         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12248                                 error "create striped dir failed"
12249
12250         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
12251                                         tail -n 1 | awk '{print $2}')
12252         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
12253
12254         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
12255         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
12256
12257         # regular file
12258         for ((i=0;i<5;i++)); do
12259                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
12260                         error "get fid for f$i failed"
12261                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
12262                         error "check path $tdir/striped_dir/f$i failed"
12263
12264                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
12265                         error "get fid for d$i failed"
12266                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
12267                         error "check path $tdir/striped_dir/d$i failed"
12268         done
12269
12270         return 0
12271 }
12272 run_test 162b "striped directory path lookup sanity"
12273
12274 # LU-4239: Verify fid2path works with paths 100 or more directories deep
12275 test_162c() {
12276         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
12277                 skip "Need MDS version at least 2.7.51" && return
12278         test_mkdir $DIR/$tdir.local
12279         test_mkdir $DIR/$tdir.remote
12280         local lpath=$tdir.local
12281         local rpath=$tdir.remote
12282
12283         for ((i = 0; i <= 101; i++)); do
12284                 lpath="$lpath/$i"
12285                 mkdir $DIR/$lpath
12286                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
12287                         error "get fid for local directory $DIR/$lpath failed"
12288                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
12289                         error "check path for local directory $DIR/$lpath failed"
12290
12291                 rpath="$rpath/$i"
12292                 test_mkdir $DIR/$rpath
12293                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
12294                         error "get fid for remote directory $DIR/$rpath failed"
12295                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
12296                         error "check path for remote directory $DIR/$rpath failed"
12297         done
12298
12299         return 0
12300 }
12301 run_test 162c "fid2path works with paths 100 or more directories deep"
12302
12303 test_169() {
12304         # do directio so as not to populate the page cache
12305         log "creating a 10 Mb file"
12306         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12307         log "starting reads"
12308         dd if=$DIR/$tfile of=/dev/null bs=4096 &
12309         log "truncating the file"
12310         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12311         log "killing dd"
12312         kill %+ || true # reads might have finished
12313         echo "wait until dd is finished"
12314         wait
12315         log "removing the temporary file"
12316         rm -rf $DIR/$tfile || error "tmp file removal failed"
12317 }
12318 run_test 169 "parallel read and truncate should not deadlock"
12319
12320 test_170() {
12321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12322
12323         $LCTL clear     # bug 18514
12324         $LCTL debug_daemon start $TMP/${tfile}_log_good
12325         touch $DIR/$tfile
12326         $LCTL debug_daemon stop
12327         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
12328                 error "sed failed to read log_good"
12329
12330         $LCTL debug_daemon start $TMP/${tfile}_log_good
12331         rm -rf $DIR/$tfile
12332         $LCTL debug_daemon stop
12333
12334         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
12335                error "lctl df log_bad failed"
12336
12337         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12338         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12339
12340         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
12341         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
12342
12343         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
12344                 error "bad_line good_line1 good_line2 are empty"
12345
12346         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12347         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
12348         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12349
12350         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
12351         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12352         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12353
12354         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
12355                 error "bad_line_new good_line_new are empty"
12356
12357         local expected_good=$((good_line1 + good_line2*2))
12358
12359         rm -f $TMP/${tfile}*
12360         # LU-231, short malformed line may not be counted into bad lines
12361         if [ $bad_line -ne $bad_line_new ] &&
12362                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
12363                 error "expected $bad_line bad lines, but got $bad_line_new"
12364                 return 1
12365         fi
12366
12367         if [ $expected_good -ne $good_line_new ]; then
12368                 error "expected $expected_good good lines, but got $good_line_new"
12369                 return 2
12370         fi
12371         true
12372 }
12373 run_test 170 "test lctl df to handle corrupted log ====================="
12374
12375 test_171() { # bug20592
12376         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12377 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
12378         $LCTL set_param fail_loc=0x50e
12379         $LCTL set_param fail_val=3000
12380         multiop_bg_pause $DIR/$tfile O_s || true
12381         local MULTIPID=$!
12382         kill -USR1 $MULTIPID
12383         # cause log dump
12384         sleep 3
12385         wait $MULTIPID
12386         if dmesg | grep "recursive fault"; then
12387                 error "caught a recursive fault"
12388         fi
12389         $LCTL set_param fail_loc=0
12390         true
12391 }
12392 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
12393
12394 # it would be good to share it with obdfilter-survey/iokit-libecho code
12395 setup_obdecho_osc () {
12396         local rc=0
12397         local ost_nid=$1
12398         local obdfilter_name=$2
12399         echo "Creating new osc for $obdfilter_name on $ost_nid"
12400         # make sure we can find loopback nid
12401         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
12402
12403         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
12404                            ${obdfilter_name}_osc_UUID || rc=2; }
12405         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
12406                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
12407         return $rc
12408 }
12409
12410 cleanup_obdecho_osc () {
12411         local obdfilter_name=$1
12412         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
12413         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
12414         return 0
12415 }
12416
12417 obdecho_test() {
12418         local OBD=$1
12419         local node=$2
12420         local pages=${3:-64}
12421         local rc=0
12422         local id
12423
12424         local count=10
12425         local obd_size=$(get_obd_size $node $OBD)
12426         local page_size=$(get_page_size $node)
12427         if [[ -n "$obd_size" ]]; then
12428                 local new_count=$((obd_size / (pages * page_size / 1024)))
12429                 [[ $new_count -ge $count ]] || count=$new_count
12430         fi
12431
12432         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
12433         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
12434                            rc=2; }
12435         if [ $rc -eq 0 ]; then
12436             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
12437             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
12438         fi
12439         echo "New object id is $id"
12440         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
12441                            rc=4; }
12442         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
12443                            "test_brw $count w v $pages $id" || rc=4; }
12444         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
12445                            rc=4; }
12446         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
12447                                         "cleanup" || rc=5; }
12448         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
12449                                         "detach" || rc=6; }
12450         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
12451         return $rc
12452 }
12453
12454 test_180a() {
12455         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12456         remote_ost_nodsh && skip "remote OST with nodsh" && return
12457         local rc=0
12458         local rmmod_local=0
12459
12460         if ! module_loaded obdecho; then
12461             load_module obdecho/obdecho
12462             rmmod_local=1
12463         fi
12464
12465         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
12466         local host=$(lctl get_param -n osc.$osc.import |
12467                              awk '/current_connection:/ {print $2}' )
12468         local target=$(lctl get_param -n osc.$osc.import |
12469                              awk '/target:/ {print $2}' )
12470         target=${target%_UUID}
12471
12472         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
12473         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
12474         [[ -n $target ]] && cleanup_obdecho_osc $target
12475         [ $rmmod_local -eq 1 ] && rmmod obdecho
12476         return $rc
12477 }
12478 run_test 180a "test obdecho on osc"
12479
12480 test_180b() {
12481         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12482         remote_ost_nodsh && skip "remote OST with nodsh" && return
12483         local rc=0
12484         local rmmod_remote=0
12485
12486         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12487                 rmmod_remote=true || error "failed to load module obdecho"
12488         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
12489         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
12490         $rmmod_remote && do_facet ost1 "rmmod obdecho"
12491         return $rc
12492 }
12493 run_test 180b "test obdecho directly on obdfilter"
12494
12495 test_180c() { # LU-2598
12496         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12497         remote_ost_nodsh && skip "remote OST with nodsh" && return
12498         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
12499                 skip "Need MDS version at least 2.4.0" && return
12500
12501         local rc=0
12502         local rmmod_remote=false
12503         local pages=16384 # 64MB bulk I/O RPC size
12504         local target
12505
12506         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12507                 rmmod_remote=true || error "failed to load module obdecho"
12508
12509         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
12510                 head -n1)
12511         if [ -n "$target" ]; then
12512                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
12513         else
12514                 echo "there is no obdfilter target on ost1"
12515                 rc=2
12516         fi
12517         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
12518         return $rc
12519 }
12520 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
12521
12522 test_181() { # bug 22177
12523         test_mkdir $DIR/$tdir
12524         # create enough files to index the directory
12525         createmany -o $DIR/$tdir/foobar 4000
12526         # print attributes for debug purpose
12527         lsattr -d .
12528         # open dir
12529         multiop_bg_pause $DIR/$tdir D_Sc || return 1
12530         MULTIPID=$!
12531         # remove the files & current working dir
12532         unlinkmany $DIR/$tdir/foobar 4000
12533         rmdir $DIR/$tdir
12534         kill -USR1 $MULTIPID
12535         wait $MULTIPID
12536         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
12537         return 0
12538 }
12539 run_test 181 "Test open-unlinked dir ========================"
12540
12541 test_182() {
12542         local fcount=1000
12543         local tcount=10
12544
12545         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12546
12547         $LCTL set_param mdc.*.rpc_stats=clear
12548
12549         for (( i = 0; i < $tcount; i++ )) ; do
12550                 mkdir $DIR/$tdir/$i
12551         done
12552
12553         for (( i = 0; i < $tcount; i++ )) ; do
12554                 createmany -o $DIR/$tdir/$i/f- $fcount &
12555         done
12556         wait
12557
12558         for (( i = 0; i < $tcount; i++ )) ; do
12559                 unlinkmany $DIR/$tdir/$i/f- $fcount &
12560         done
12561         wait
12562
12563         $LCTL get_param mdc.*.rpc_stats
12564
12565         rm -rf $DIR/$tdir
12566 }
12567 run_test 182 "Test parallel modify metadata operations ================"
12568
12569 test_183() { # LU-2275
12570         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12571         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
12572                 skip "Need MDS version at least 2.3.56" && return
12573
12574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12575         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12576         echo aaa > $DIR/$tdir/$tfile
12577
12578 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
12579         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
12580
12581         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
12582         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
12583
12584         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
12585
12586         # Flush negative dentry cache
12587         touch $DIR/$tdir/$tfile
12588
12589         # We are not checking for any leaked references here, they'll
12590         # become evident next time we do cleanup with module unload.
12591         rm -rf $DIR/$tdir
12592 }
12593 run_test 183 "No crash or request leak in case of strange dispositions ========"
12594
12595 # test suite 184 is for LU-2016, LU-2017
12596 test_184a() {
12597         check_swap_layouts_support && return 0
12598
12599         dir0=$DIR/$tdir/$testnum
12600         test_mkdir -p -c1 $dir0
12601         ref1=/etc/passwd
12602         ref2=/etc/group
12603         file1=$dir0/f1
12604         file2=$dir0/f2
12605         $SETSTRIPE -c1 $file1
12606         cp $ref1 $file1
12607         $SETSTRIPE -c2 $file2
12608         cp $ref2 $file2
12609         gen1=$($GETSTRIPE -g $file1)
12610         gen2=$($GETSTRIPE -g $file2)
12611
12612         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
12613         gen=$($GETSTRIPE -g $file1)
12614         [[ $gen1 != $gen ]] ||
12615                 "Layout generation on $file1 does not change"
12616         gen=$($GETSTRIPE -g $file2)
12617         [[ $gen2 != $gen ]] ||
12618                 "Layout generation on $file2 does not change"
12619
12620         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
12621         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12622
12623         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
12624 }
12625 run_test 184a "Basic layout swap"
12626
12627 test_184b() {
12628         check_swap_layouts_support && return 0
12629
12630         dir0=$DIR/$tdir/$testnum
12631         mkdir -p $dir0 || error "creating dir $dir0"
12632         file1=$dir0/f1
12633         file2=$dir0/f2
12634         file3=$dir0/f3
12635         dir1=$dir0/d1
12636         dir2=$dir0/d2
12637         mkdir $dir1 $dir2
12638         $SETSTRIPE -c1 $file1
12639         $SETSTRIPE -c2 $file2
12640         $SETSTRIPE -c1 $file3
12641         chown $RUNAS_ID $file3
12642         gen1=$($GETSTRIPE -g $file1)
12643         gen2=$($GETSTRIPE -g $file2)
12644
12645         $LFS swap_layouts $dir1 $dir2 &&
12646                 error "swap of directories layouts should fail"
12647         $LFS swap_layouts $dir1 $file1 &&
12648                 error "swap of directory and file layouts should fail"
12649         $RUNAS $LFS swap_layouts $file1 $file2 &&
12650                 error "swap of file we cannot write should fail"
12651         $LFS swap_layouts $file1 $file3 &&
12652                 error "swap of file with different owner should fail"
12653         /bin/true # to clear error code
12654 }
12655 run_test 184b "Forbidden layout swap (will generate errors)"
12656
12657 test_184c() {
12658         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
12659         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
12660         check_swap_layouts_support && return 0
12661
12662         local dir0=$DIR/$tdir/$testnum
12663         mkdir -p $dir0 || error "creating dir $dir0"
12664
12665         local ref1=$dir0/ref1
12666         local ref2=$dir0/ref2
12667         local file1=$dir0/file1
12668         local file2=$dir0/file2
12669         # create a file large enough for the concurrent test
12670         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
12671         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
12672         echo "ref file size: ref1($(stat -c %s $ref1))," \
12673              "ref2($(stat -c %s $ref2))"
12674
12675         cp $ref2 $file2
12676         dd if=$ref1 of=$file1 bs=16k &
12677         local DD_PID=$!
12678
12679         # Make sure dd starts to copy file
12680         while [ ! -f $file1 ]; do sleep 0.1; done
12681
12682         $LFS swap_layouts $file1 $file2
12683         local rc=$?
12684         wait $DD_PID
12685         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
12686         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
12687
12688         # how many bytes copied before swapping layout
12689         local copied=$(stat -c %s $file2)
12690         local remaining=$(stat -c %s $ref1)
12691         remaining=$((remaining - copied))
12692         echo "Copied $copied bytes before swapping layout..."
12693
12694         cmp -n $copied $file1 $ref2 | grep differ &&
12695                 error "Content mismatch [0, $copied) of ref2 and file1"
12696         cmp -n $copied $file2 $ref1 ||
12697                 error "Content mismatch [0, $copied) of ref1 and file2"
12698         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
12699                 error "Content mismatch [$copied, EOF) of ref1 and file1"
12700
12701         # clean up
12702         rm -f $ref1 $ref2 $file1 $file2
12703 }
12704 run_test 184c "Concurrent write and layout swap"
12705
12706 test_184d() {
12707         check_swap_layouts_support && return 0
12708         [ -z "$(which getfattr 2>/dev/null)" ] &&
12709                 skip "no getfattr command" && return 0
12710
12711         local file1=$DIR/$tdir/$tfile-1
12712         local file2=$DIR/$tdir/$tfile-2
12713         local file3=$DIR/$tdir/$tfile-3
12714         local lovea1
12715         local lovea2
12716
12717         mkdir -p $DIR/$tdir
12718         touch $file1 || error "create $file1 failed"
12719         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12720                 error "create $file2 failed"
12721         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12722                 error "create $file3 failed"
12723         lovea1=$(get_layout_param $file1)
12724
12725         $LFS swap_layouts $file2 $file3 ||
12726                 error "swap $file2 $file3 layouts failed"
12727         $LFS swap_layouts $file1 $file2 ||
12728                 error "swap $file1 $file2 layouts failed"
12729
12730         lovea2=$(get_layout_param $file2)
12731         echo "$lovea1"
12732         echo "$lovea2"
12733         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12734
12735         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12736         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12737 }
12738 run_test 184d "allow stripeless layouts swap"
12739
12740 test_184e() {
12741         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12742                 { skip "Need MDS version at least 2.6.94"; return 0; }
12743         check_swap_layouts_support && return 0
12744         [ -z "$(which getfattr 2>/dev/null)" ] &&
12745                 skip "no getfattr command" && return 0
12746
12747         local file1=$DIR/$tdir/$tfile-1
12748         local file2=$DIR/$tdir/$tfile-2
12749         local file3=$DIR/$tdir/$tfile-3
12750         local lovea
12751
12752         mkdir -p $DIR/$tdir
12753         touch $file1 || error "create $file1 failed"
12754         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12755                 error "create $file2 failed"
12756         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12757                 error "create $file3 failed"
12758
12759         $LFS swap_layouts $file1 $file2 ||
12760                 error "swap $file1 $file2 layouts failed"
12761
12762         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12763         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12764
12765         echo 123 > $file1 || error "Should be able to write into $file1"
12766
12767         $LFS swap_layouts $file1 $file3 ||
12768                 error "swap $file1 $file3 layouts failed"
12769
12770         echo 123 > $file1 || error "Should be able to write into $file1"
12771
12772         rm -rf $file1 $file2 $file3
12773 }
12774 run_test 184e "Recreate layout after stripeless layout swaps"
12775
12776 test_185() { # LU-2441
12777         # LU-3553 - no volatile file support in old servers
12778         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12779                 { skip "Need MDS version at least 2.3.60"; return 0; }
12780
12781         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12782         touch $DIR/$tdir/spoo
12783         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12784         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12785                 error "cannot create/write a volatile file"
12786         [ "$FILESET" == "" ] &&
12787         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12788                 error "FID is still valid after close"
12789
12790         multiop_bg_pause $DIR/$tdir vVw4096_c
12791         local multi_pid=$!
12792
12793         local OLD_IFS=$IFS
12794         IFS=":"
12795         local fidv=($fid)
12796         IFS=$OLD_IFS
12797         # assume that the next FID for this client is sequential, since stdout
12798         # is unfortunately eaten by multiop_bg_pause
12799         local n=$((${fidv[1]} + 1))
12800         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12801         if [ "$FILESET" == "" ]; then
12802                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12803                         error "FID is missing before close"
12804         fi
12805         kill -USR1 $multi_pid
12806         # 1 second delay, so if mtime change we will see it
12807         sleep 1
12808         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12809         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12810 }
12811 run_test 185 "Volatile file support"
12812
12813 test_187a() {
12814         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12815         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12816                 skip "Need MDS version at least 2.3.0" && return
12817
12818         local dir0=$DIR/$tdir/$testnum
12819         mkdir -p $dir0 || error "creating dir $dir0"
12820
12821         local file=$dir0/file1
12822         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12823         local dv1=$($LFS data_version $file)
12824         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12825         local dv2=$($LFS data_version $file)
12826         [[ $dv1 != $dv2 ]] ||
12827                 error "data version did not change on write $dv1 == $dv2"
12828
12829         # clean up
12830         rm -f $file1
12831 }
12832 run_test 187a "Test data version change"
12833
12834 test_187b() {
12835         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12836         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12837                 skip "Need MDS version at least 2.3.0" && return
12838
12839         local dir0=$DIR/$tdir/$testnum
12840         mkdir -p $dir0 || error "creating dir $dir0"
12841
12842         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12843         [[ ${DV[0]} != ${DV[1]} ]] ||
12844                 error "data version did not change on write"\
12845                       " ${DV[0]} == ${DV[1]}"
12846
12847         # clean up
12848         rm -f $file1
12849 }
12850 run_test 187b "Test data version change on volatile file"
12851
12852 test_200() {
12853         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12854         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12855         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12856
12857         local POOL=${POOL:-cea1}
12858         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12859         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12860         # Pool OST targets
12861         local first_ost=0
12862         local last_ost=$(($OSTCOUNT - 1))
12863         local ost_step=2
12864         local ost_list=$(seq $first_ost $ost_step $last_ost)
12865         local ost_range="$first_ost $last_ost $ost_step"
12866         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12867         local file_dir=$POOL_ROOT/file_tst
12868         local subdir=$test_path/subdir
12869         local rc=0
12870
12871         if ! combined_mgs_mds ; then
12872                 mount_mgs_client
12873         fi
12874
12875         while : ; do
12876                 # former test_200a test_200b
12877                 pool_add $POOL                          || { rc=$? ; break; }
12878                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12879                 # former test_200c test_200d
12880                 mkdir -p $test_path
12881                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12882                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12883                 mkdir -p $subdir
12884                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12885                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12886                                                         || { rc=$? ; break; }
12887                 # former test_200e test_200f
12888                 local files=$((OSTCOUNT*3))
12889                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12890                                                         || { rc=$? ; break; }
12891                 pool_create_files $POOL $file_dir $files "$ost_list" \
12892                                                         || { rc=$? ; break; }
12893                 # former test_200g test_200h
12894                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12895                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12896
12897                 # former test_201a test_201b test_201c
12898                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12899
12900                 local f=$test_path/$tfile
12901                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12902                 pool_remove $POOL $f                    || { rc=$? ; break; }
12903                 break
12904         done
12905
12906         destroy_test_pools
12907
12908         if ! combined_mgs_mds ; then
12909                 umount_mgs_client
12910         fi
12911         return $rc
12912 }
12913 run_test 200 "OST pools"
12914
12915 # usage: default_attr <count | size | offset>
12916 default_attr() {
12917         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12918 }
12919
12920 # usage: check_default_stripe_attr
12921 check_default_stripe_attr() {
12922         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12923         case $1 in
12924         --stripe-count|-c)
12925                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12926         --stripe-size|-S)
12927                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12928         --stripe-index|-i)
12929                 EXPECTED=-1;;
12930         *)
12931                 error "unknown getstripe attr '$1'"
12932         esac
12933
12934         [ $ACTUAL == $EXPECTED ] ||
12935                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12936 }
12937
12938 test_204a() {
12939         test_mkdir $DIR/$tdir
12940         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12941
12942         check_default_stripe_attr --stripe-count
12943         check_default_stripe_attr --stripe-size
12944         check_default_stripe_attr --stripe-index
12945 }
12946 run_test 204a "Print default stripe attributes"
12947
12948 test_204b() {
12949         test_mkdir $DIR/$tdir
12950         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12951
12952         check_default_stripe_attr --stripe-size
12953         check_default_stripe_attr --stripe-index
12954 }
12955 run_test 204b "Print default stripe size and offset"
12956
12957 test_204c() {
12958         test_mkdir $DIR/$tdir
12959         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12960
12961         check_default_stripe_attr --stripe-count
12962         check_default_stripe_attr --stripe-index
12963 }
12964 run_test 204c "Print default stripe count and offset"
12965
12966 test_204d() {
12967         test_mkdir $DIR/$tdir
12968         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12969
12970         check_default_stripe_attr --stripe-count
12971         check_default_stripe_attr --stripe-size
12972 }
12973 run_test 204d "Print default stripe count and size"
12974
12975 test_204e() {
12976         test_mkdir $DIR/$tdir
12977         $SETSTRIPE -d $DIR/$tdir
12978
12979         check_default_stripe_attr --stripe-count --raw
12980         check_default_stripe_attr --stripe-size --raw
12981         check_default_stripe_attr --stripe-index --raw
12982 }
12983 run_test 204e "Print raw stripe attributes"
12984
12985 test_204f() {
12986         test_mkdir $DIR/$tdir
12987         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12988
12989         check_default_stripe_attr --stripe-size --raw
12990         check_default_stripe_attr --stripe-index --raw
12991 }
12992 run_test 204f "Print raw stripe size and offset"
12993
12994 test_204g() {
12995         test_mkdir $DIR/$tdir
12996         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12997
12998         check_default_stripe_attr --stripe-count --raw
12999         check_default_stripe_attr --stripe-index --raw
13000 }
13001 run_test 204g "Print raw stripe count and offset"
13002
13003 test_204h() {
13004         test_mkdir $DIR/$tdir
13005         $SETSTRIPE --stripe-index 0 $DIR/$tdir
13006
13007         check_default_stripe_attr --stripe-count --raw
13008         check_default_stripe_attr --stripe-size --raw
13009 }
13010 run_test 204h "Print raw stripe count and size"
13011
13012 # Figure out which job scheduler is being used, if any,
13013 # or use a fake one
13014 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
13015         JOBENV=SLURM_JOB_ID
13016 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
13017         JOBENV=LSB_JOBID
13018 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
13019         JOBENV=PBS_JOBID
13020 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
13021         JOBENV=LOADL_STEP_ID
13022 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
13023         JOBENV=JOB_ID
13024 else
13025         $LCTL list_param jobid_name > /dev/null 2>&1
13026         if [ $? -eq 0 ]; then
13027                 JOBENV=nodelocal
13028         else
13029                 JOBENV=FAKE_JOBID
13030         fi
13031 fi
13032
13033 verify_jobstats() {
13034         local cmd=($1)
13035         shift
13036         local facets="$@"
13037
13038 # we don't really need to clear the stats for this test to work, since each
13039 # command has a unique jobid, but it makes debugging easier if needed.
13040 #       for facet in $facets; do
13041 #               local dev=$(convert_facet2label $facet)
13042 #               # clear old jobstats
13043 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
13044 #       done
13045
13046         # use a new JobID for each test, or we might see an old one
13047         [ "$JOBENV" = "FAKE_JOBID" ] &&
13048                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13049
13050         JOBVAL=${!JOBENV}
13051
13052         [ "$JOBENV" = "nodelocal" ] && {
13053                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13054                 $LCTL set_param jobid_name=$FAKE_JOBID
13055                 JOBVAL=$FAKE_JOBID
13056         }
13057
13058         log "Test: ${cmd[*]}"
13059         log "Using JobID environment variable $JOBENV=$JOBVAL"
13060
13061         if [ $JOBENV = "FAKE_JOBID" ]; then
13062                 FAKE_JOBID=$JOBVAL ${cmd[*]}
13063         else
13064                 ${cmd[*]}
13065         fi
13066
13067         # all files are created on OST0000
13068         for facet in $facets; do
13069                 local stats="*.$(convert_facet2label $facet).job_stats"
13070                 if [ $(do_facet $facet lctl get_param $stats |
13071                        grep -c $JOBVAL) -ne 1 ]; then
13072                         do_facet $facet lctl get_param $stats
13073                         error "No jobstats for $JOBVAL found on $facet::$stats"
13074                 fi
13075         done
13076 }
13077
13078 jobstats_set() {
13079         trap 0
13080         NEW_JOBENV=${1:-$OLD_JOBENV}
13081         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
13082         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
13083 }
13084
13085 cleanup_205() {
13086         trap 0
13087         do_facet $SINGLEMDS \
13088                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
13089         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
13090         cleanup_changelog
13091 }
13092
13093 test_205() { # Job stats
13094         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
13095                 { skip "Need MDS version with at least 2.7.1"; return 0; }
13096
13097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13098         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13099         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13100         remote_ost_nodsh && skip "remote OST with nodsh" && return
13101
13102         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
13103                 skip "Server doesn't support jobstats" && return 0
13104         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
13105
13106         OLD_JOBENV=$($LCTL get_param -n jobid_var)
13107         if [ $OLD_JOBENV != $JOBENV ]; then
13108                 jobstats_set $JOBENV
13109                 trap cleanup_205 EXIT
13110         fi
13111
13112         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
13113         echo "Registered as changelog user $CL_USER"
13114
13115         OLD_INTERVAL=$(do_facet $SINGLEMDS \
13116                        lctl get_param -n mdt.*.job_cleanup_interval)
13117         local interval_new=5
13118         do_facet $SINGLEMDS \
13119                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
13120         local start=$SECONDS
13121
13122         local cmd
13123         # mkdir
13124         cmd="mkdir $DIR/$tdir"
13125         verify_jobstats "$cmd" "$SINGLEMDS"
13126         # rmdir
13127         cmd="rmdir $DIR/$tdir"
13128         verify_jobstats "$cmd" "$SINGLEMDS"
13129         # mkdir on secondary MDT
13130         if [ $MDSCOUNT -gt 1 ]; then
13131                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
13132                 verify_jobstats "$cmd" "mds2"
13133         fi
13134         # mknod
13135         cmd="mknod $DIR/$tfile c 1 3"
13136         verify_jobstats "$cmd" "$SINGLEMDS"
13137         # unlink
13138         cmd="rm -f $DIR/$tfile"
13139         verify_jobstats "$cmd" "$SINGLEMDS"
13140         # create all files on OST0000 so verify_jobstats can find OST stats
13141         # open & close
13142         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
13143         verify_jobstats "$cmd" "$SINGLEMDS"
13144         # setattr
13145         cmd="touch $DIR/$tfile"
13146         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13147         # write
13148         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
13149         verify_jobstats "$cmd" "ost1"
13150         # read
13151         cancel_lru_locks osc
13152         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
13153         verify_jobstats "$cmd" "ost1"
13154         # truncate
13155         cmd="$TRUNCATE $DIR/$tfile 0"
13156         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13157         # rename
13158         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
13159         verify_jobstats "$cmd" "$SINGLEMDS"
13160         # jobstats expiry - sleep until old stats should be expired
13161         local left=$((interval_new + 5 - (SECONDS - start)))
13162         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
13163                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
13164                         "0" $left
13165         cmd="mkdir $DIR/$tdir.expire"
13166         verify_jobstats "$cmd" "$SINGLEMDS"
13167         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
13168             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
13169
13170         # Ensure that jobid are present in changelog (if supported by MDS)
13171         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
13172         then
13173                 $LFS changelog $MDT0 | tail -9
13174                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
13175                 [ $jobids -eq 9 ] ||
13176                         error "Wrong changelog jobid count $jobids != 9"
13177
13178                 # LU-5862
13179                 JOBENV="disable"
13180                 jobstats_set $JOBENV
13181                 touch $DIR/$tfile
13182                 $LFS changelog $MDT0 | tail -1
13183                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
13184                 [ $jobids -eq 0 ] ||
13185                         error "Unexpected jobids when jobid_var=$JOBENV"
13186         fi
13187
13188         cleanup_205
13189 }
13190 run_test 205 "Verify job stats"
13191
13192 # LU-1480, LU-1773 and LU-1657
13193 test_206() {
13194         mkdir -p $DIR/$tdir
13195         $SETSTRIPE -c -1 $DIR/$tdir
13196 #define OBD_FAIL_LOV_INIT 0x1403
13197         $LCTL set_param fail_loc=0xa0001403
13198         $LCTL set_param fail_val=1
13199         touch $DIR/$tdir/$tfile || true
13200 }
13201 run_test 206 "fail lov_init_raid0() doesn't lbug"
13202
13203 test_207a() {
13204         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13205         local fsz=`stat -c %s $DIR/$tfile`
13206         cancel_lru_locks mdc
13207
13208         # do not return layout in getattr intent
13209 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
13210         $LCTL set_param fail_loc=0x170
13211         local sz=`stat -c %s $DIR/$tfile`
13212
13213         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
13214
13215         rm -rf $DIR/$tfile
13216 }
13217 run_test 207a "can refresh layout at glimpse"
13218
13219 test_207b() {
13220         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13221         local cksum=`md5sum $DIR/$tfile`
13222         local fsz=`stat -c %s $DIR/$tfile`
13223         cancel_lru_locks mdc
13224         cancel_lru_locks osc
13225
13226         # do not return layout in getattr intent
13227 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
13228         $LCTL set_param fail_loc=0x171
13229
13230         # it will refresh layout after the file is opened but before read issues
13231         echo checksum is "$cksum"
13232         echo "$cksum" |md5sum -c --quiet || error "file differs"
13233
13234         rm -rf $DIR/$tfile
13235 }
13236 run_test 207b "can refresh layout at open"
13237
13238 test_208() {
13239         # FIXME: in this test suite, only RD lease is used. This is okay
13240         # for now as only exclusive open is supported. After generic lease
13241         # is done, this test suite should be revised. - Jinshan
13242
13243         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13244         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
13245                 { skip "Need MDS version at least 2.4.52"; return 0; }
13246
13247         echo "==== test 1: verify get lease work"
13248         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
13249
13250         echo "==== test 2: verify lease can be broken by upcoming open"
13251         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13252         local PID=$!
13253         sleep 1
13254
13255         $MULTIOP $DIR/$tfile oO_RDONLY:c
13256         kill -USR1 $PID && wait $PID || error "break lease error"
13257
13258         echo "==== test 3: verify lease can't be granted if an open already exists"
13259         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
13260         local PID=$!
13261         sleep 1
13262
13263         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
13264         kill -USR1 $PID && wait $PID || error "open file error"
13265
13266         echo "==== test 4: lease can sustain over recovery"
13267         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
13268         PID=$!
13269         sleep 1
13270
13271         fail mds1
13272
13273         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
13274
13275         echo "==== test 5: lease broken can't be regained by replay"
13276         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13277         PID=$!
13278         sleep 1
13279
13280         # open file to break lease and then recovery
13281         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
13282         fail mds1
13283
13284         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
13285
13286         rm -f $DIR/$tfile
13287 }
13288 run_test 208 "Exclusive open"
13289
13290 test_209() {
13291         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
13292                 skip_env "must have disp_stripe" && return
13293
13294         touch $DIR/$tfile
13295         sync; sleep 5; sync;
13296
13297         echo 3 > /proc/sys/vm/drop_caches
13298         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13299
13300         # open/close 500 times
13301         for i in $(seq 500); do
13302                 cat $DIR/$tfile
13303         done
13304
13305         echo 3 > /proc/sys/vm/drop_caches
13306         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13307
13308         echo "before: $req_before, after: $req_after"
13309         [ $((req_after - req_before)) -ge 300 ] &&
13310                 error "open/close requests are not freed"
13311         return 0
13312 }
13313 run_test 209 "read-only open/close requests should be freed promptly"
13314
13315 test_212() {
13316         size=`date +%s`
13317         size=$((size % 8192 + 1))
13318         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
13319         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
13320         rm -f $DIR/f212 $DIR/f212.xyz
13321 }
13322 run_test 212 "Sendfile test ============================================"
13323
13324 test_213() {
13325         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
13326         cancel_lru_locks osc
13327         lctl set_param fail_loc=0x8000040f
13328         # generate a read lock
13329         cat $DIR/$tfile > /dev/null
13330         # write to the file, it will try to cancel the above read lock.
13331         cat /etc/hosts >> $DIR/$tfile
13332 }
13333 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
13334
13335 test_214() { # for bug 20133
13336         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
13337         for (( i=0; i < 340; i++ )) ; do
13338                 touch $DIR/$tdir/d214c/a$i
13339         done
13340
13341         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
13342         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
13343         ls $DIR/d214c || error "ls $DIR/d214c failed"
13344         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
13345         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
13346 }
13347 run_test 214 "hash-indexed directory test - bug 20133"
13348
13349 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
13350 create_lnet_proc_files() {
13351         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
13352 }
13353
13354 # counterpart of create_lnet_proc_files
13355 remove_lnet_proc_files() {
13356         rm -f $TMP/lnet_$1.sys
13357 }
13358
13359 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13360 # 3rd arg as regexp for body
13361 check_lnet_proc_stats() {
13362         local l=$(cat "$TMP/lnet_$1" |wc -l)
13363         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
13364
13365         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
13366 }
13367
13368 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13369 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
13370 # optional and can be regexp for 2nd line (lnet.routes case)
13371 check_lnet_proc_entry() {
13372         local blp=2          # blp stands for 'position of 1st line of body'
13373         [ -z "$5" ] || blp=3 # lnet.routes case
13374
13375         local l=$(cat "$TMP/lnet_$1" |wc -l)
13376         # subtracting one from $blp because the body can be empty
13377         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
13378
13379         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
13380                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
13381
13382         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
13383                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
13384
13385         # bail out if any unexpected line happened
13386         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
13387         [ "$?" != 0 ] || error "$2 misformatted"
13388 }
13389
13390 test_215() { # for bugs 18102, 21079, 21517
13391         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13392         local N='(0|[1-9][0-9]*)'       # non-negative numeric
13393         local P='[1-9][0-9]*'           # positive numeric
13394         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
13395         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
13396         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
13397         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
13398
13399         local L1 # regexp for 1st line
13400         local L2 # regexp for 2nd line (optional)
13401         local BR # regexp for the rest (body)
13402
13403         # lnet.stats should look as 11 space-separated non-negative numerics
13404         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
13405         create_lnet_proc_files "stats"
13406         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
13407         remove_lnet_proc_files "stats"
13408
13409         # lnet.routes should look like this:
13410         # Routing disabled/enabled
13411         # net hops priority state router
13412         # where net is a string like tcp0, hops > 0, priority >= 0,
13413         # state is up/down,
13414         # router is a string like 192.168.1.1@tcp2
13415         L1="^Routing (disabled|enabled)$"
13416         L2="^net +hops +priority +state +router$"
13417         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
13418         create_lnet_proc_files "routes"
13419         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
13420         remove_lnet_proc_files "routes"
13421
13422         # lnet.routers should look like this:
13423         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
13424         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
13425         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
13426         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
13427         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
13428         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
13429         create_lnet_proc_files "routers"
13430         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
13431         remove_lnet_proc_files "routers"
13432
13433         # lnet.peers should look like this:
13434         # nid refs state last max rtr min tx min queue
13435         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
13436         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
13437         # numeric (0 or >0 or <0), queue >= 0.
13438         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
13439         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
13440         create_lnet_proc_files "peers"
13441         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
13442         remove_lnet_proc_files "peers"
13443
13444         # lnet.buffers  should look like this:
13445         # pages count credits min
13446         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
13447         L1="^pages +count +credits +min$"
13448         BR="^ +$N +$N +$I +$I$"
13449         create_lnet_proc_files "buffers"
13450         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
13451         remove_lnet_proc_files "buffers"
13452
13453         # lnet.nis should look like this:
13454         # nid status alive refs peer rtr max tx min
13455         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
13456         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
13457         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
13458         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
13459         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
13460         create_lnet_proc_files "nis"
13461         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
13462         remove_lnet_proc_files "nis"
13463
13464         # can we successfully write to lnet.stats?
13465         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
13466 }
13467 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
13468
13469 test_216() { # bug 20317
13470         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13471         remote_ost_nodsh && skip "remote OST with nodsh" && return
13472
13473         local node
13474         local facets=$(get_facets OST)
13475         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13476
13477         save_lustre_params client "osc.*.contention_seconds" > $p
13478         save_lustre_params $facets \
13479                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
13480         save_lustre_params $facets \
13481                 "ldlm.namespaces.filter-*.contended_locks" >> $p
13482         save_lustre_params $facets \
13483                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
13484         clear_stats osc.*.osc_stats
13485
13486         # agressive lockless i/o settings
13487         do_nodes $(comma_list $(osts_nodes)) \
13488                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
13489                         ldlm.namespaces.filter-*.contended_locks=0 \
13490                         ldlm.namespaces.filter-*.contention_seconds=60"
13491         lctl set_param -n osc.*.contention_seconds=60
13492
13493         $DIRECTIO write $DIR/$tfile 0 10 4096
13494         $CHECKSTAT -s 40960 $DIR/$tfile
13495
13496         # disable lockless i/o
13497         do_nodes $(comma_list $(osts_nodes)) \
13498                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
13499                         ldlm.namespaces.filter-*.contended_locks=32 \
13500                         ldlm.namespaces.filter-*.contention_seconds=0"
13501         lctl set_param -n osc.*.contention_seconds=0
13502         clear_stats osc.*.osc_stats
13503
13504         dd if=/dev/zero of=$DIR/$tfile count=0
13505         $CHECKSTAT -s 0 $DIR/$tfile
13506
13507         restore_lustre_params <$p
13508         rm -f $p
13509         rm $DIR/$tfile
13510 }
13511 run_test 216 "check lockless direct write updates file size and kms correctly"
13512
13513 test_217() { # bug 22430
13514         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13515         local node
13516         local nid
13517
13518         for node in $(nodes_list); do
13519                 nid=$(host_nids_address $node $NETTYPE)
13520                 if [[ $nid = *-* ]] ; then
13521                         echo "lctl ping $(h2nettype $nid)"
13522                         lctl ping $(h2nettype $nid)
13523                 else
13524                         echo "skipping $node (no hyphen detected)"
13525                 fi
13526         done
13527 }
13528 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
13529
13530 test_218() {
13531        # do directio so as not to populate the page cache
13532        log "creating a 10 Mb file"
13533        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13534        log "starting reads"
13535        dd if=$DIR/$tfile of=/dev/null bs=4096 &
13536        log "truncating the file"
13537        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13538        log "killing dd"
13539        kill %+ || true # reads might have finished
13540        echo "wait until dd is finished"
13541        wait
13542        log "removing the temporary file"
13543        rm -rf $DIR/$tfile || error "tmp file removal failed"
13544 }
13545 run_test 218 "parallel read and truncate should not deadlock ======================="
13546
13547 test_219() {
13548         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13549         # write one partial page
13550         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
13551         # set no grant so vvp_io_commit_write will do sync write
13552         $LCTL set_param fail_loc=0x411
13553         # write a full page at the end of file
13554         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
13555
13556         $LCTL set_param fail_loc=0
13557         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
13558         $LCTL set_param fail_loc=0x411
13559         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
13560
13561         # LU-4201
13562         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
13563         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
13564 }
13565 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
13566
13567 test_220() { #LU-325
13568         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13569         remote_ost_nodsh && skip "remote OST with nodsh" && return
13570         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13571         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13572         local OSTIDX=0
13573
13574         # create on MDT0000 so the last_id and next_id are correct
13575         mkdir $DIR/$tdir
13576         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
13577         OST=${OST%_UUID}
13578
13579         # on the mdt's osc
13580         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
13581         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
13582                         osc.$mdtosc_proc1.prealloc_last_id)
13583         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
13584                         osc.$mdtosc_proc1.prealloc_next_id)
13585
13586         $LFS df -i
13587
13588         if ! combined_mgs_mds ; then
13589                 mount_mgs_client
13590         fi
13591
13592         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
13593         #define OBD_FAIL_OST_ENOINO              0x229
13594         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
13595         create_pool $FSNAME.$TESTNAME || return 1
13596         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
13597
13598         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
13599
13600         MDSOBJS=$((last_id - next_id))
13601         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
13602
13603         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
13604         echo "OST still has $count kbytes free"
13605
13606         echo "create $MDSOBJS files @next_id..."
13607         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
13608
13609         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13610                         osc.$mdtosc_proc1.prealloc_last_id)
13611         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13612                         osc.$mdtosc_proc1.prealloc_next_id)
13613
13614         echo "after creation, last_id=$last_id2, next_id=$next_id2"
13615         $LFS df -i
13616
13617         echo "cleanup..."
13618
13619         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
13620         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
13621
13622         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
13623                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
13624         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
13625                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
13626         echo "unlink $MDSOBJS files @$next_id..."
13627         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
13628
13629         if ! combined_mgs_mds ; then
13630                 umount_mgs_client
13631         fi
13632 }
13633 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
13634
13635 test_221() {
13636         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13637         dd if=`which date` of=$MOUNT/date oflag=sync
13638         chmod +x $MOUNT/date
13639
13640         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
13641         $LCTL set_param fail_loc=0x80001401
13642
13643         $MOUNT/date > /dev/null
13644         rm -f $MOUNT/date
13645 }
13646 run_test 221 "make sure fault and truncate race to not cause OOM"
13647
13648 test_222a () {
13649         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13650         rm -rf $DIR/$tdir
13651         test_mkdir $DIR/$tdir
13652         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13653         createmany -o $DIR/$tdir/$tfile 10
13654         cancel_lru_locks mdc
13655         cancel_lru_locks osc
13656         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13657         $LCTL set_param fail_loc=0x31a
13658         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
13659         $LCTL set_param fail_loc=0
13660         rm -r $DIR/$tdir
13661 }
13662 run_test 222a "AGL for ls should not trigger CLIO lock failure"
13663
13664 test_222b () {
13665         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13666         rm -rf $DIR/$tdir
13667         test_mkdir $DIR/$tdir
13668         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13669         createmany -o $DIR/$tdir/$tfile 10
13670         cancel_lru_locks mdc
13671         cancel_lru_locks osc
13672         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13673         $LCTL set_param fail_loc=0x31a
13674         rm -r $DIR/$tdir || error "AGL for rmdir failed"
13675         $LCTL set_param fail_loc=0
13676 }
13677 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
13678
13679 test_223 () {
13680         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13681         rm -rf $DIR/$tdir
13682         test_mkdir $DIR/$tdir
13683         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13684         createmany -o $DIR/$tdir/$tfile 10
13685         cancel_lru_locks mdc
13686         cancel_lru_locks osc
13687         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
13688         $LCTL set_param fail_loc=0x31b
13689         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
13690         $LCTL set_param fail_loc=0
13691         rm -r $DIR/$tdir
13692 }
13693 run_test 223 "osc reenqueue if without AGL lock granted ======================="
13694
13695 test_224a() { # LU-1039, MRP-303
13696         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13697         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
13698         $LCTL set_param fail_loc=0x508
13699         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
13700         $LCTL set_param fail_loc=0
13701         df $DIR
13702 }
13703 run_test 224a "Don't panic on bulk IO failure"
13704
13705 test_224b() { # LU-1039, MRP-303
13706         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13707         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
13708         cancel_lru_locks osc
13709         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
13710         $LCTL set_param fail_loc=0x515
13711         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
13712         $LCTL set_param fail_loc=0
13713         df $DIR
13714 }
13715 run_test 224b "Don't panic on bulk IO failure"
13716
13717 test_224c() { # LU-6441
13718         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13719         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13720
13721         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13722         save_writethrough $p
13723         set_cache writethrough on
13724
13725         local pages_per_rpc=$($LCTL get_param \
13726                                 osc.*.max_pages_per_rpc)
13727         local at_max=$($LCTL get_param -n at_max)
13728         local timeout=$($LCTL get_param -n timeout)
13729         local test_at="$LCTL get_param -n at_max"
13730         local param_at="$FSNAME.sys.at_max"
13731         local test_timeout="$LCTL get_param -n timeout"
13732         local param_timeout="$FSNAME.sys.timeout"
13733
13734         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
13735
13736         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13737                 error "conf_param at_max=0 failed"
13738         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13739                 error "conf_param timeout=5 failed"
13740
13741         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13742         do_facet ost1 $LCTL set_param fail_loc=0x520
13743         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13744         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13745         sync
13746         do_facet ost1 $LCTL set_param fail_loc=0
13747
13748         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13749                 error "conf_param at_max=$at_max failed"
13750         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13751                 $timeout || error "conf_param timeout=$timeout failed"
13752
13753         $LCTL set_param -n $pages_per_rpc
13754         restore_lustre_params < $p
13755         rm -f $p
13756 }
13757 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13758
13759 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13760 test_225a () {
13761         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13762         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13763         if [ -z ${MDSSURVEY} ]; then
13764               skip_env "mds-survey not found" && return
13765         fi
13766
13767         [ $MDSCOUNT -ge 2 ] &&
13768                 skip "skipping now for more than one MDT" && return
13769
13770        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13771             { skip "Need MDS version at least 2.2.51"; return; }
13772
13773        local mds=$(facet_host $SINGLEMDS)
13774        local target=$(do_nodes $mds 'lctl dl' | \
13775                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13776
13777        local cmd1="file_count=1000 thrhi=4"
13778        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13779        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13780        local cmd="$cmd1 $cmd2 $cmd3"
13781
13782        rm -f ${TMP}/mds_survey*
13783        echo + $cmd
13784        eval $cmd || error "mds-survey with zero-stripe failed"
13785        cat ${TMP}/mds_survey*
13786        rm -f ${TMP}/mds_survey*
13787 }
13788 run_test 225a "Metadata survey sanity with zero-stripe"
13789
13790 test_225b () {
13791         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13792         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13793         if [ -z ${MDSSURVEY} ]; then
13794               skip_env "mds-survey not found" && return
13795         fi
13796         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13797             { skip "Need MDS version at least 2.2.51"; return; }
13798
13799         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13800               skip_env "Need to mount OST to test" && return
13801         fi
13802
13803        local mds=$(facet_host $SINGLEMDS)
13804        local target=$(do_nodes $mds 'lctl dl' | \
13805                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13806
13807        local cmd1="file_count=1000 thrhi=4"
13808        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13809        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13810        local cmd="$cmd1 $cmd2 $cmd3"
13811
13812        rm -f ${TMP}/mds_survey*
13813        echo + $cmd
13814        eval $cmd || error "mds-survey with stripe_count failed"
13815        cat ${TMP}/mds_survey*
13816        rm -f ${TMP}/mds_survey*
13817 }
13818 run_test 225b "Metadata survey sanity with stripe_count = 1"
13819
13820 mcreate_path2fid () {
13821         local mode=$1
13822         local major=$2
13823         local minor=$3
13824         local name=$4
13825         local desc=$5
13826         local path=$DIR/$tdir/$name
13827         local fid
13828         local rc
13829         local fid_path
13830
13831         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13832                 error "cannot create $desc"
13833
13834         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13835         rc=$?
13836         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13837
13838         fid_path=$($LFS fid2path $MOUNT $fid)
13839         rc=$?
13840         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13841
13842         [ "$path" == "$fid_path" ] ||
13843                 error "fid2path returned $fid_path, expected $path"
13844
13845         echo "pass with $path and $fid"
13846 }
13847
13848 test_226a () {
13849         rm -rf $DIR/$tdir
13850         mkdir -p $DIR/$tdir
13851
13852         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13853         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13854         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13855         mcreate_path2fid 0040666 0 0 dir "directory"
13856         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13857         mcreate_path2fid 0100666 0 0 file "regular file"
13858         mcreate_path2fid 0120666 0 0 link "symbolic link"
13859         mcreate_path2fid 0140666 0 0 sock "socket"
13860 }
13861 run_test 226a "call path2fid and fid2path on files of all type"
13862
13863 test_226b () {
13864         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13865         rm -rf $DIR/$tdir
13866         local MDTIDX=1
13867
13868         mkdir -p $DIR/$tdir
13869         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13870                 error "create remote directory failed"
13871         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13872         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13873                                 "character special file (null)"
13874         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13875                                 "character special file (no device)"
13876         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13877         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13878                                 "block special file (loop)"
13879         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13880         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13881         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13882 }
13883 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13884
13885 # LU-1299 Executing or running ldd on a truncated executable does not
13886 # cause an out-of-memory condition.
13887 test_227() {
13888         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13889         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13890         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13891         chmod +x $MOUNT/date
13892
13893         $MOUNT/date > /dev/null
13894         ldd $MOUNT/date > /dev/null
13895         rm -f $MOUNT/date
13896 }
13897 run_test 227 "running truncated executable does not cause OOM"
13898
13899 # LU-1512 try to reuse idle OI blocks
13900 test_228a() {
13901         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13902         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13903         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13904                 skip "ldiskfs only test" && return
13905
13906         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13907         local myDIR=$DIR/$tdir
13908
13909         mkdir -p $myDIR
13910         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13911         $LCTL set_param fail_loc=0x80001002
13912         createmany -o $myDIR/t- 10000
13913         $LCTL set_param fail_loc=0
13914         # The guard is current the largest FID holder
13915         touch $myDIR/guard
13916         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13917                     tr -d '[')
13918         local IDX=$(($SEQ % 64))
13919
13920         do_facet $SINGLEMDS sync
13921         # Make sure journal flushed.
13922         sleep 6
13923         local blk1=$(do_facet $SINGLEMDS \
13924                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13925                      grep Blockcount | awk '{print $4}')
13926
13927         # Remove old files, some OI blocks will become idle.
13928         unlinkmany $myDIR/t- 10000
13929         # Create new files, idle OI blocks should be reused.
13930         createmany -o $myDIR/t- 2000
13931         do_facet $SINGLEMDS sync
13932         # Make sure journal flushed.
13933         sleep 6
13934         local blk2=$(do_facet $SINGLEMDS \
13935                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13936                      grep Blockcount | awk '{print $4}')
13937
13938         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13939 }
13940 run_test 228a "try to reuse idle OI blocks"
13941
13942 test_228b() {
13943         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13944         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13945         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13946                 skip "ldiskfs only test" && return
13947
13948         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13949         local myDIR=$DIR/$tdir
13950
13951         mkdir -p $myDIR
13952         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13953         $LCTL set_param fail_loc=0x80001002
13954         createmany -o $myDIR/t- 10000
13955         $LCTL set_param fail_loc=0
13956         # The guard is current the largest FID holder
13957         touch $myDIR/guard
13958         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13959                     tr -d '[')
13960         local IDX=$(($SEQ % 64))
13961
13962         do_facet $SINGLEMDS sync
13963         # Make sure journal flushed.
13964         sleep 6
13965         local blk1=$(do_facet $SINGLEMDS \
13966                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13967                      grep Blockcount | awk '{print $4}')
13968
13969         # Remove old files, some OI blocks will become idle.
13970         unlinkmany $myDIR/t- 10000
13971
13972         # stop the MDT
13973         stop $SINGLEMDS || error "Fail to stop MDT."
13974         # remount the MDT
13975         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13976
13977         df $MOUNT || error "Fail to df."
13978         # Create new files, idle OI blocks should be reused.
13979         createmany -o $myDIR/t- 2000
13980         do_facet $SINGLEMDS sync
13981         # Make sure journal flushed.
13982         sleep 6
13983         local blk2=$(do_facet $SINGLEMDS \
13984                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13985                      grep Blockcount | awk '{print $4}')
13986
13987         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13988 }
13989 run_test 228b "idle OI blocks can be reused after MDT restart"
13990
13991 #LU-1881
13992 test_228c() {
13993         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13994         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13995         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13996                 skip "ldiskfs only test" && return
13997
13998         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13999         local myDIR=$DIR/$tdir
14000
14001         mkdir -p $myDIR
14002         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14003         $LCTL set_param fail_loc=0x80001002
14004         # 20000 files can guarantee there are index nodes in the OI file
14005         createmany -o $myDIR/t- 20000
14006         $LCTL set_param fail_loc=0
14007         # The guard is current the largest FID holder
14008         touch $myDIR/guard
14009         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14010                     tr -d '[')
14011         local IDX=$(($SEQ % 64))
14012
14013         do_facet $SINGLEMDS sync
14014         # Make sure journal flushed.
14015         sleep 6
14016         local blk1=$(do_facet $SINGLEMDS \
14017                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14018                      grep Blockcount | awk '{print $4}')
14019
14020         # Remove old files, some OI blocks will become idle.
14021         unlinkmany $myDIR/t- 20000
14022         rm -f $myDIR/guard
14023         # The OI file should become empty now
14024
14025         # Create new files, idle OI blocks should be reused.
14026         createmany -o $myDIR/t- 2000
14027         do_facet $SINGLEMDS sync
14028         # Make sure journal flushed.
14029         sleep 6
14030         local blk2=$(do_facet $SINGLEMDS \
14031                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14032                      grep Blockcount | awk '{print $4}')
14033
14034         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14035 }
14036 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
14037
14038 test_229() { # LU-2482, LU-3448
14039         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
14040                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
14041                 return
14042         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14043         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
14044
14045         rm -f $DIR/$tfile
14046
14047         # Create a file with a released layout and stripe count 2.
14048         $MULTIOP $DIR/$tfile H2c ||
14049                 error "failed to create file with released layout"
14050
14051         $GETSTRIPE -v $DIR/$tfile
14052
14053         local pattern=$($GETSTRIPE -L $DIR/$tfile)
14054         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
14055
14056         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
14057         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
14058         stat $DIR/$tfile || error "failed to stat released file"
14059
14060         chown $RUNAS_ID $DIR/$tfile ||
14061                 error "chown $RUNAS_ID $DIR/$tfile failed"
14062
14063         chgrp $RUNAS_ID $DIR/$tfile ||
14064                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
14065
14066         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
14067         rm $DIR/$tfile || error "failed to remove released file"
14068 }
14069 run_test 229 "getstripe/stat/rm/attr changes work on released files"
14070
14071 test_230a() {
14072         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14073         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14074         local MDTIDX=1
14075
14076         test_mkdir $DIR/$tdir
14077         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
14078         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
14079         [ $mdt_idx -ne 0 ] &&
14080                 error "create local directory on wrong MDT $mdt_idx"
14081
14082         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
14083                         error "create remote directory failed"
14084         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
14085         [ $mdt_idx -ne $MDTIDX ] &&
14086                 error "create remote directory on wrong MDT $mdt_idx"
14087
14088         createmany -o $DIR/$tdir/test_230/t- 10 ||
14089                 error "create files on remote directory failed"
14090         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
14091         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
14092         rm -r $DIR/$tdir || error "unlink remote directory failed"
14093 }
14094 run_test 230a "Create remote directory and files under the remote directory"
14095
14096 test_230b() {
14097         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14098         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14099         local MDTIDX=1
14100         local mdt_index
14101         local i
14102         local file
14103         local pid
14104         local stripe_count
14105         local migrate_dir=$DIR/$tdir/migrate_dir
14106         local other_dir=$DIR/$tdir/other_dir
14107
14108         test_mkdir $DIR/$tdir
14109         test_mkdir -i0 -c1 $migrate_dir
14110         test_mkdir -i0 -c1 $other_dir
14111         for ((i=0; i<10; i++)); do
14112                 mkdir -p $migrate_dir/dir_${i}
14113                 createmany -o $migrate_dir/dir_${i}/f 10 ||
14114                         error "create files under remote dir failed $i"
14115         done
14116
14117         cp /etc/passwd $migrate_dir/$tfile
14118         cp /etc/passwd $other_dir/$tfile
14119         chattr +SAD $migrate_dir
14120         chattr +SAD $migrate_dir/$tfile
14121
14122         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14123         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14124         local old_dir_mode=$(stat -c%f $migrate_dir)
14125         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
14126
14127         mkdir -p $migrate_dir/dir_default_stripe2
14128         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
14129         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
14130
14131         mkdir -p $other_dir
14132         ln $migrate_dir/$tfile $other_dir/luna
14133         ln $migrate_dir/$tfile $migrate_dir/sofia
14134         ln $other_dir/$tfile $migrate_dir/david
14135         ln -s $migrate_dir/$tfile $other_dir/zachary
14136         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
14137         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
14138
14139         $LFS migrate -m $MDTIDX $migrate_dir ||
14140                 error "fails on migrating remote dir to MDT1"
14141
14142         echo "migratate to MDT1, then checking.."
14143         for ((i = 0; i < 10; i++)); do
14144                 for file in $(find $migrate_dir/dir_${i}); do
14145                         mdt_index=$($LFS getstripe -M $file)
14146                         [ $mdt_index == $MDTIDX ] ||
14147                                 error "$file is not on MDT${MDTIDX}"
14148                 done
14149         done
14150
14151         # the multiple link file should still in MDT0
14152         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
14153         [ $mdt_index == 0 ] ||
14154                 error "$file is not on MDT${MDTIDX}"
14155
14156         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14157         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14158                 error " expect $old_dir_flag get $new_dir_flag"
14159
14160         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14161         [ "$old_file_flag" = "$new_file_flag" ] ||
14162                 error " expect $old_file_flag get $new_file_flag"
14163
14164         local new_dir_mode=$(stat -c%f $migrate_dir)
14165         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14166                 error "expect mode $old_dir_mode get $new_dir_mode"
14167
14168         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14169         [ "$old_file_mode" = "$new_file_mode" ] ||
14170                 error "expect mode $old_file_mode get $new_file_mode"
14171
14172         diff /etc/passwd $migrate_dir/$tfile ||
14173                 error "$tfile different after migration"
14174
14175         diff /etc/passwd $other_dir/luna ||
14176                 error "luna different after migration"
14177
14178         diff /etc/passwd $migrate_dir/sofia ||
14179                 error "sofia different after migration"
14180
14181         diff /etc/passwd $migrate_dir/david ||
14182                 error "david different after migration"
14183
14184         diff /etc/passwd $other_dir/zachary ||
14185                 error "zachary different after migration"
14186
14187         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14188                 error "${tfile}_ln different after migration"
14189
14190         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14191                 error "${tfile}_ln_other different after migration"
14192
14193         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
14194         [ $stripe_count = 2 ] ||
14195                 error "dir strpe_count $d != 2 after migration."
14196
14197         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
14198         [ $stripe_count = 2 ] ||
14199                 error "file strpe_count $d != 2 after migration."
14200
14201         #migrate back to MDT0
14202         MDTIDX=0
14203
14204         $LFS migrate -m $MDTIDX $migrate_dir ||
14205                 error "fails on migrating remote dir to MDT0"
14206
14207         echo "migrate back to MDT0, checking.."
14208         for file in $(find $migrate_dir); do
14209                 mdt_index=$($LFS getstripe -M $file)
14210                 [ $mdt_index == $MDTIDX ] ||
14211                         error "$file is not on MDT${MDTIDX}"
14212         done
14213
14214         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14215         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14216                 error " expect $old_dir_flag get $new_dir_flag"
14217
14218         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14219         [ "$old_file_flag" = "$new_file_flag" ] ||
14220                 error " expect $old_file_flag get $new_file_flag"
14221
14222         local new_dir_mode=$(stat -c%f $migrate_dir)
14223         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14224                 error "expect mode $old_dir_mode get $new_dir_mode"
14225
14226         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14227         [ "$old_file_mode" = "$new_file_mode" ] ||
14228                 error "expect mode $old_file_mode get $new_file_mode"
14229
14230         diff /etc/passwd ${migrate_dir}/$tfile ||
14231                 error "$tfile different after migration"
14232
14233         diff /etc/passwd ${other_dir}/luna ||
14234                 error "luna different after migration"
14235
14236         diff /etc/passwd ${migrate_dir}/sofia ||
14237                 error "sofia different after migration"
14238
14239         diff /etc/passwd ${other_dir}/zachary ||
14240                 error "zachary different after migration"
14241
14242         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14243                 error "${tfile}_ln different after migration"
14244
14245         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14246                 error "${tfile}_ln_other different after migration"
14247
14248         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
14249         [ $stripe_count = 2 ] ||
14250                 error "dir strpe_count $d != 2 after migration."
14251
14252         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
14253         [ $stripe_count = 2 ] ||
14254                 error "file strpe_count $d != 2 after migration."
14255
14256         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14257 }
14258 run_test 230b "migrate directory"
14259
14260 test_230c() {
14261         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14262         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14263         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14264         local MDTIDX=1
14265         local mdt_index
14266         local file
14267         local migrate_dir=$DIR/$tdir/migrate_dir
14268
14269         #If migrating directory fails in the middle, all entries of
14270         #the directory is still accessiable.
14271         test_mkdir $DIR/$tdir
14272         test_mkdir -i0 -c1 $migrate_dir
14273         stat $migrate_dir
14274         createmany -o $migrate_dir/f 10 ||
14275                 error "create files under ${migrate_dir} failed"
14276
14277         #failed after migrating 5 entries
14278         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
14279         do_facet mds1 lctl set_param fail_loc=0x20001801
14280         do_facet mds1 lctl  set_param fail_val=5
14281         local t=$(ls $migrate_dir | wc -l)
14282         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
14283                 error "migrate should fail after 5 entries"
14284
14285         mkdir $migrate_dir/dir &&
14286                 error "mkdir succeeds under migrating directory"
14287         touch $migrate_dir/file &&
14288                 error "touch file succeeds under migrating directory"
14289
14290         local u=$(ls $migrate_dir | wc -l)
14291         [ "$u" == "$t" ] || error "$u != $t during migration"
14292
14293         for file in $(find $migrate_dir); do
14294                 stat $file || error "stat $file failed"
14295         done
14296
14297         do_facet mds1 lctl set_param fail_loc=0
14298         do_facet mds1 lctl set_param fail_val=0
14299
14300         $LFS migrate -m $MDTIDX $migrate_dir ||
14301                 error "migrate open files should failed with open files"
14302
14303         echo "Finish migration, then checking.."
14304         for file in $(find $migrate_dir); do
14305                 mdt_index=$($LFS getstripe -M $file)
14306                 [ $mdt_index == $MDTIDX ] ||
14307                         error "$file is not on MDT${MDTIDX}"
14308         done
14309
14310         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14311 }
14312 run_test 230c "check directory accessiblity if migration is failed"
14313
14314 test_230d() {
14315         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14316         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14317         local MDTIDX=1
14318         local mdt_index
14319         local migrate_dir=$DIR/$tdir/migrate_dir
14320         local i
14321         local j
14322
14323         test_mkdir $DIR/$tdir
14324         test_mkdir -i0 -c1 $migrate_dir
14325
14326         for ((i=0; i<100; i++)); do
14327                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
14328                 createmany -o $migrate_dir/dir_${i}/f 100 ||
14329                         error "create files under remote dir failed $i"
14330         done
14331
14332         $LFS migrate -m $MDTIDX $migrate_dir ||
14333                 error "migrate remote dir error"
14334
14335         echo "Finish migration, then checking.."
14336         for file in $(find $migrate_dir); do
14337                 mdt_index=$($LFS getstripe -M $file)
14338                 [ $mdt_index == $MDTIDX ] ||
14339                         error "$file is not on MDT${MDTIDX}"
14340         done
14341
14342         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14343 }
14344 run_test 230d "check migrate big directory"
14345
14346 test_230e() {
14347         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14348         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14349         local i
14350         local j
14351         local a_fid
14352         local b_fid
14353
14354         mkdir -p $DIR/$tdir
14355         mkdir $DIR/$tdir/migrate_dir
14356         mkdir $DIR/$tdir/other_dir
14357         touch $DIR/$tdir/migrate_dir/a
14358         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
14359         ls $DIR/$tdir/other_dir
14360
14361         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14362                 error "migrate dir fails"
14363
14364         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14365         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14366
14367         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14368         [ $mdt_index == 0 ] || error "a is not on MDT0"
14369
14370         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
14371                 error "migrate dir fails"
14372
14373         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
14374         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
14375
14376         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14377         [ $mdt_index == 1 ] || error "a is not on MDT1"
14378
14379         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
14380         [ $mdt_index == 1 ] || error "b is not on MDT1"
14381
14382         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14383         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
14384
14385         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
14386
14387         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14388 }
14389 run_test 230e "migrate mulitple local link files"
14390
14391 test_230f() {
14392         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14393         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14394         local a_fid
14395         local ln_fid
14396
14397         mkdir -p $DIR/$tdir
14398         mkdir $DIR/$tdir/migrate_dir
14399         $LFS mkdir -i1 $DIR/$tdir/other_dir
14400         touch $DIR/$tdir/migrate_dir/a
14401         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
14402         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
14403         ls $DIR/$tdir/other_dir
14404
14405         # a should be migrated to MDT1, since no other links on MDT0
14406         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14407                 error "#1 migrate dir fails"
14408         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14409         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14410         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14411         [ $mdt_index == 1 ] || error "a is not on MDT1"
14412
14413         # a should stay on MDT1, because it is a mulitple link file
14414         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14415                 error "#2 migrate dir fails"
14416         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14417         [ $mdt_index == 1 ] || error "a is not on MDT1"
14418
14419         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14420                 error "#3 migrate dir fails"
14421
14422         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14423         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
14424         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
14425
14426         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
14427         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
14428
14429         # a should be migrated to MDT0, since no other links on MDT1
14430         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14431                 error "#4 migrate dir fails"
14432         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14433         [ $mdt_index == 0 ] || error "a is not on MDT0"
14434
14435         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14436 }
14437 run_test 230f "migrate mulitple remote link files"
14438
14439 test_230g() {
14440         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14441         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14442
14443         mkdir -p $DIR/$tdir/migrate_dir
14444
14445         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
14446                 error "migrating dir to non-exist MDT succeeds"
14447         true
14448 }
14449 run_test 230g "migrate dir to non-exist MDT"
14450
14451 test_230h() {
14452         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14453         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14454         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
14455                 skip "Need MDS version at least 2.7.64" && return
14456         local mdt_index
14457
14458         mkdir -p $DIR/$tdir/migrate_dir
14459
14460         $LFS migrate -m1 $DIR &&
14461                 error "migrating mountpoint1 should fail"
14462
14463         $LFS migrate -m1 $DIR/$tdir/.. &&
14464                 error "migrating mountpoint2 should fail"
14465
14466         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
14467                 error "migrating $tdir fail"
14468
14469         mdt_index=$($LFS getstripe -M $DIR/$tdir)
14470         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14471
14472         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14473         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14474
14475 }
14476 run_test 230h "migrate .. and root"
14477
14478 test_230i() {
14479         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14480         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14481
14482         mkdir -p $DIR/$tdir/migrate_dir
14483
14484         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
14485                 error "migration fails with a tailing slash"
14486
14487         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
14488                 error "migration fails with two tailing slashes"
14489 }
14490 run_test 230i "lfs migrate -m tolerates trailing slashes"
14491
14492 test_231a()
14493 {
14494         # For simplicity this test assumes that max_pages_per_rpc
14495         # is the same across all OSCs
14496         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
14497         local bulk_size=$((max_pages * 4096))
14498         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
14499                                        head -n 1)
14500
14501         mkdir -p $DIR/$tdir
14502         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
14503                 error "failed to set stripe with -S ${brw_size}M option"
14504
14505         # clear the OSC stats
14506         $LCTL set_param osc.*.stats=0 &>/dev/null
14507         stop_writeback
14508
14509         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
14510         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
14511                 oflag=direct &>/dev/null || error "dd failed"
14512
14513         sync; sleep 1; sync # just to be safe
14514         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
14515         if [ x$nrpcs != "x1" ]; then
14516                 $LCTL get_param osc.*.stats
14517                 error "found $nrpcs ost_write RPCs, not 1 as expected"
14518         fi
14519
14520         start_writeback
14521         # Drop the OSC cache, otherwise we will read from it
14522         cancel_lru_locks osc
14523
14524         # clear the OSC stats
14525         $LCTL set_param osc.*.stats=0 &>/dev/null
14526
14527         # Client reads $bulk_size.
14528         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
14529                 iflag=direct &>/dev/null || error "dd failed"
14530
14531         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
14532         if [ x$nrpcs != "x1" ]; then
14533                 $LCTL get_param osc.*.stats
14534                 error "found $nrpcs ost_read RPCs, not 1 as expected"
14535         fi
14536 }
14537 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
14538
14539 test_231b() {
14540         mkdir -p $DIR/$tdir
14541         local i
14542         for i in {0..1023}; do
14543                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
14544                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
14545                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
14546         done
14547         sync
14548 }
14549 run_test 231b "must not assert on fully utilized OST request buffer"
14550
14551 test_232a() {
14552         mkdir -p $DIR/$tdir
14553         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14554
14555         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14556         do_facet ost1 $LCTL set_param fail_loc=0x31c
14557
14558         # ignore dd failure
14559         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
14560
14561         do_facet ost1 $LCTL set_param fail_loc=0
14562         umount_client $MOUNT || error "umount failed"
14563         mount_client $MOUNT || error "mount failed"
14564         stop ost1 || error "cannot stop ost1"
14565         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
14566 }
14567 run_test 232a "failed lock should not block umount"
14568
14569 test_232b() {
14570         mkdir -p $DIR/$tdir
14571         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14572         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
14573         sync
14574         cancel_lru_locks osc
14575
14576         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14577         do_facet ost1 $LCTL set_param fail_loc=0x31c
14578
14579         # ignore failure
14580         $LFS data_version $DIR/$tdir/$tfile || true
14581
14582         do_facet ost1 $LCTL set_param fail_loc=0
14583         umount_client $MOUNT || error "umount failed"
14584         mount_client $MOUNT || error "mount failed"
14585         stop ost1 || error "cannot stop ost1"
14586         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
14587 }
14588 run_test 232b "failed data version lock should not block umount"
14589
14590 test_233a() {
14591         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
14592         { skip "Need MDS version at least 2.3.64"; return; }
14593         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14594
14595         local fid=$($LFS path2fid $MOUNT)
14596         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14597                 error "cannot access $MOUNT using its FID '$fid'"
14598 }
14599 run_test 233a "checking that OBF of the FS root succeeds"
14600
14601 test_233b() {
14602         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
14603         { skip "Need MDS version at least 2.5.90"; return; }
14604         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14605
14606         local fid=$($LFS path2fid $MOUNT/.lustre)
14607         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14608                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
14609
14610         fid=$($LFS path2fid $MOUNT/.lustre/fid)
14611         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14612                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
14613 }
14614 run_test 233b "checking that OBF of the FS .lustre succeeds"
14615
14616 test_234() {
14617         local p="$TMP/sanityN-$TESTNAME.parameters"
14618         save_lustre_params client "llite.*.xattr_cache" > $p
14619         lctl set_param llite.*.xattr_cache 1 ||
14620                 { skip "xattr cache is not supported"; return 0; }
14621
14622         mkdir -p $DIR/$tdir || error "mkdir failed"
14623         touch $DIR/$tdir/$tfile || error "touch failed"
14624         # OBD_FAIL_LLITE_XATTR_ENOMEM
14625         $LCTL set_param fail_loc=0x1405
14626         getfattr -n user.attr $DIR/$tdir/$tfile &&
14627                 error "getfattr should have failed with ENOMEM"
14628         $LCTL set_param fail_loc=0x0
14629         rm -rf $DIR/$tdir
14630
14631         restore_lustre_params < $p
14632         rm -f $p
14633 }
14634 run_test 234 "xattr cache should not crash on ENOMEM"
14635
14636 test_235() {
14637         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
14638                 skip "Need MDS version at least 2.4.52" && return
14639         flock_deadlock $DIR/$tfile
14640         local RC=$?
14641         case $RC in
14642                 0)
14643                 ;;
14644                 124) error "process hangs on a deadlock"
14645                 ;;
14646                 *) error "error executing flock_deadlock $DIR/$tfile"
14647                 ;;
14648         esac
14649 }
14650 run_test 235 "LU-1715: flock deadlock detection does not work properly"
14651
14652 #LU-2935
14653 test_236() {
14654         check_swap_layouts_support && return 0
14655         test_mkdir -c1 $DIR/$tdir
14656
14657         local ref1=/etc/passwd
14658         local ref2=/etc/group
14659         local file1=$DIR/$tdir/f1
14660         local file2=$DIR/$tdir/f2
14661
14662         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
14663         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
14664         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
14665         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
14666         local fd=$(free_fd)
14667         local cmd="exec $fd<>$file2"
14668         eval $cmd
14669         rm $file2
14670         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
14671                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
14672         cmd="exec $fd>&-"
14673         eval $cmd
14674         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14675
14676         #cleanup
14677         rm -rf $DIR/$tdir
14678 }
14679 run_test 236 "Layout swap on open unlinked file"
14680
14681 # test to verify file handle related system calls
14682 # (name_to_handle_at/open_by_handle_at)
14683 # The new system calls are supported in glibc >= 2.14.
14684
14685 test_237() {
14686         echo "Test file_handle syscalls" > $DIR/$tfile ||
14687                 error "write failed"
14688         check_fhandle_syscalls $DIR/$tfile ||
14689                 error "check_fhandle_syscalls failed"
14690 }
14691 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
14692
14693 # LU-4659 linkea consistency
14694 test_238() {
14695         local server_version=$(lustre_version_code $SINGLEMDS)
14696
14697         [[ $server_version -gt $(version_code 2.5.57) ]] ||
14698                 [[ $server_version -gt $(version_code 2.5.1) &&
14699                    $server_version -lt $(version_code 2.5.50) ]] ||
14700                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
14701
14702         touch $DIR/$tfile
14703         ln $DIR/$tfile $DIR/$tfile.lnk
14704         touch $DIR/$tfile.new
14705         mv $DIR/$tfile.new $DIR/$tfile
14706         local fid1=$($LFS path2fid $DIR/$tfile)
14707         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
14708         local path1=$($LFS fid2path $FSNAME "$fid1")
14709         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
14710         local path2=$($LFS fid2path $FSNAME "$fid2")
14711         [ $tfile.lnk == $path2 ] ||
14712                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
14713         rm -f $DIR/$tfile*
14714 }
14715 run_test 238 "Verify linkea consistency"
14716
14717 test_239() {
14718         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
14719                 skip "Need MDS version at least 2.5.60" && return
14720         local list=$(comma_list $(mdts_nodes))
14721
14722         mkdir -p $DIR/$tdir
14723         createmany -o $DIR/$tdir/f- 5000
14724         unlinkmany $DIR/$tdir/f- 5000
14725         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.53) ] &&
14726                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
14727         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
14728                         osp.*MDT*.sync_in_flight" | calc_sum)
14729         [ "$changes" -eq 0 ] || error "$changes not synced"
14730 }
14731 run_test 239 "osp_sync test"
14732
14733 test_239a() { #LU-5297
14734         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14735         touch $DIR/$tfile
14736         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
14737         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
14738         chgrp $RUNAS_GID $DIR/$tfile
14739         wait_delete_completed
14740 }
14741 run_test 239a "process invalid osp sync record correctly"
14742
14743 test_239b() { #LU-5297
14744         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14745         touch $DIR/$tfile1
14746         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
14747         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
14748         chgrp $RUNAS_GID $DIR/$tfile1
14749         wait_delete_completed
14750         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14751         touch $DIR/$tfile2
14752         chgrp $RUNAS_GID $DIR/$tfile2
14753         wait_delete_completed
14754 }
14755 run_test 239b "process osp sync record with ENOMEM error correctly"
14756
14757 test_240() {
14758         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14759         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14760
14761         mkdir -p $DIR/$tdir
14762
14763         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14764                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14765         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14766                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14767
14768         umount_client $MOUNT || error "umount failed"
14769         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14770         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14771         mount_client $MOUNT || error "failed to mount client"
14772
14773         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14774         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14775 }
14776 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14777
14778 test_241_bio() {
14779         for LOOP in $(seq $1); do
14780                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14781                 cancel_lru_locks $OSC || true
14782         done
14783 }
14784
14785 test_241_dio() {
14786         for LOOP in $(seq $1); do
14787                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14788                                                 iflag=direct 2>/dev/null
14789         done
14790 }
14791
14792 test_241a() { # was test_241
14793         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14794         ls -la $DIR/$tfile
14795         cancel_lru_locks $OSC
14796         test_241_bio 1000 &
14797         PID=$!
14798         test_241_dio 1000
14799         wait $PID
14800 }
14801 run_test 241a "bio vs dio"
14802
14803 test_241b() {
14804         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14805         ls -la $DIR/$tfile
14806         test_241_dio 1000 &
14807         PID=$!
14808         test_241_dio 1000
14809         wait $PID
14810 }
14811 run_test 241b "dio vs dio"
14812
14813 test_242() {
14814         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14815         mkdir -p $DIR/$tdir
14816         touch $DIR/$tdir/$tfile
14817
14818         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14819         do_facet mds1 lctl set_param fail_loc=0x105
14820         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14821
14822         do_facet mds1 lctl set_param fail_loc=0
14823         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14824 }
14825 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14826
14827 test_243()
14828 {
14829         test_mkdir $DIR/$tdir
14830         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14831 }
14832 run_test 243 "various group lock tests"
14833
14834 test_244()
14835 {
14836         test_mkdir $DIR/$tdir
14837         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14838         sendfile_grouplock $DIR/$tdir/$tfile || \
14839                 error "sendfile+grouplock failed"
14840         rm -rf $DIR/$tdir
14841 }
14842 run_test 244 "sendfile with group lock tests"
14843
14844 test_245() {
14845         local flagname="multi_mod_rpcs"
14846         local connect_data_name="max_mod_rpcs"
14847         local out
14848
14849         # check if multiple modify RPCs flag is set
14850         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14851                 grep "connect_flags:")
14852         echo "$out"
14853
14854         echo "$out" | grep -qw $flagname
14855         if [ $? -ne 0 ]; then
14856                 echo "connect flag $flagname is not set"
14857                 return
14858         fi
14859
14860         # check if multiple modify RPCs data is set
14861         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14862         echo "$out"
14863
14864         echo "$out" | grep -qw $connect_data_name ||
14865                 error "import should have connect data $connect_data_name"
14866 }
14867 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14868
14869 test_246() { # LU-7371
14870         remote_ost_nodsh && skip "remote OST with nodsh" && return
14871         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14872                 skip "Need OST version >= 2.7.62" && return 0
14873         do_facet ost1 $LCTL set_param fail_val=4095
14874 #define OBD_FAIL_OST_READ_SIZE          0x234
14875         do_facet ost1 $LCTL set_param fail_loc=0x234
14876         $LFS setstripe $DIR/$tfile -i 0 -c 1
14877         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14878         cancel_lru_locks $FSNAME-OST0000
14879         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14880 }
14881 run_test 246 "Read file of size 4095 should return right length"
14882
14883 cleanup_247() {
14884         local submount=$1
14885
14886         trap 0
14887         umount_client $submount
14888         rmdir $submount
14889 }
14890
14891 test_247a() {
14892         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14893                 grep -q subtree ||
14894                 { skip "Fileset feature is not supported"; return; }
14895
14896         local submount=${MOUNT}_$tdir
14897
14898         mkdir $MOUNT/$tdir
14899         mkdir -p $submount || error "mkdir $submount failed"
14900         FILESET="$FILESET/$tdir" mount_client $submount ||
14901                 error "mount $submount failed"
14902         trap "cleanup_247 $submount" EXIT
14903         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14904         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14905                 error "read $MOUNT/$tdir/$tfile failed"
14906         cleanup_247 $submount
14907 }
14908 run_test 247a "mount subdir as fileset"
14909
14910 test_247b() {
14911         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14912                 { skip "Fileset feature is not supported"; return; }
14913
14914         local submount=${MOUNT}_$tdir
14915
14916         rm -rf $MOUNT/$tdir
14917         mkdir -p $submount || error "mkdir $submount failed"
14918         SKIP_FILESET=1
14919         FILESET="$FILESET/$tdir" mount_client $submount &&
14920                 error "mount $submount should fail"
14921         rmdir $submount
14922 }
14923 run_test 247b "mount subdir that dose not exist"
14924
14925 test_247c() {
14926         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14927                 { skip "Fileset feature is not supported"; return; }
14928
14929         local submount=${MOUNT}_$tdir
14930
14931         mkdir -p $MOUNT/$tdir/dir1
14932         mkdir -p $submount || error "mkdir $submount failed"
14933         trap "cleanup_247 $submount" EXIT
14934         FILESET="$FILESET/$tdir" mount_client $submount ||
14935                 error "mount $submount failed"
14936         local fid=$($LFS path2fid $MOUNT/)
14937         $LFS fid2path $submount $fid && error "fid2path should fail"
14938         cleanup_247 $submount
14939 }
14940 run_test 247c "running fid2path outside root"
14941
14942 test_247d() {
14943         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14944                 { skip "Fileset feature is not supported"; return; }
14945
14946         local submount=${MOUNT}_$tdir
14947
14948         mkdir -p $MOUNT/$tdir/dir1
14949         mkdir -p $submount || error "mkdir $submount failed"
14950         FILESET="$FILESET/$tdir" mount_client $submount ||
14951                 error "mount $submount failed"
14952         trap "cleanup_247 $submount" EXIT
14953         local fid=$($LFS path2fid $submount/dir1)
14954         $LFS fid2path $submount $fid || error "fid2path should succeed"
14955         cleanup_247 $submount
14956 }
14957 run_test 247d "running fid2path inside root"
14958
14959 # LU-8037
14960 test_247e() {
14961         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14962                 grep -q subtree ||
14963                 { skip "Fileset feature is not supported"; return; }
14964
14965         local submount=${MOUNT}_$tdir
14966
14967         mkdir $MOUNT/$tdir
14968         mkdir -p $submount || error "mkdir $submount failed"
14969         FILESET="$FILESET/.." mount_client $submount &&
14970                 error "mount $submount should fail"
14971         rmdir $submount
14972 }
14973 run_test 247e "mount .. as fileset"
14974
14975 test_248() {
14976         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14977         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14978
14979         # create a large file for fast read verification
14980         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
14981
14982         # make sure the file is created correctly
14983         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
14984                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
14985
14986         echo "Test 1: verify that fast read is 4 times faster on cache read"
14987
14988         # small read with fast read enabled
14989         $LCTL set_param -n llite.*.fast_read=1
14990         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14991                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14992                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14993         # small read with fast read disabled
14994         $LCTL set_param -n llite.*.fast_read=0
14995         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
14996                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
14997                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
14998
14999         # verify that fast read is 4 times faster for cache read
15000         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
15001                 error_not_in_vm "fast read was not 4 times faster: " \
15002                            "$t_fast vs $t_slow"
15003
15004         echo "Test 2: verify the performance between big and small read"
15005         $LCTL set_param -n llite.*.fast_read=1
15006
15007         # 1k non-cache read
15008         cancel_lru_locks osc
15009         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15010                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15011                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15012
15013         # 1M non-cache read
15014         cancel_lru_locks osc
15015         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15016                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15017                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15018
15019         # verify that big IO is not 4 times faster than small IO
15020         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
15021                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
15022
15023         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
15024         rm -f $DIR/$tfile
15025 }
15026 run_test 248 "fast read verification"
15027
15028 test_249() { # LU-7890
15029         rm -f $DIR/$tfile
15030         $SETSTRIPE -c 1 $DIR/$tfile
15031
15032         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
15033         skip "Need at least version 2.8.54"
15034
15035         # Offset 2T == 4k * 512M
15036         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
15037                 error "dd to 2T offset failed"
15038 }
15039 run_test 249 "Write above 2T file size"
15040
15041 test_250() {
15042         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
15043          && skip "no 16TB file size limit on ZFS" && return
15044
15045         $SETSTRIPE -c 1 $DIR/$tfile
15046         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
15047         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
15048         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
15049         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
15050                 conv=notrunc,fsync && error "append succeeded"
15051         return 0
15052 }
15053 run_test 250 "Write above 16T limit"
15054
15055 test_251() {
15056         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
15057
15058         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
15059         #Skip once - writing the first stripe will succeed
15060         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15061         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
15062                 error "short write happened"
15063
15064         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15065         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
15066                 error "short read happened"
15067
15068         rm -f $DIR/$tfile
15069 }
15070 run_test 251 "Handling short read and write correctly"
15071
15072 test_252() {
15073         local tgt
15074         local dev
15075         local out
15076         local uuid
15077         local num
15078         local gen
15079
15080         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15081         remote_ost_nodsh && skip "remote OST with nodsh" && return
15082         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
15083              "$(facet_fstype mds1)" != "ldiskfs" ]; then
15084                 skip "ldiskfs only test"
15085                 return
15086         fi
15087
15088         # check lr_reader on OST0000
15089         tgt=ost1
15090         dev=$(facet_device $tgt)
15091         out=$(do_facet $tgt $LR_READER $dev)
15092         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15093         echo "$out"
15094         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
15095         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
15096                 error "Invalid uuid returned by $LR_READER on target $tgt"
15097         echo -e "uuid returned by $LR_READER is '$uuid'\n"
15098
15099         # check lr_reader -c on MDT0000
15100         tgt=mds1
15101         dev=$(facet_device $tgt)
15102         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
15103                 echo "$LR_READER does not support additional options"
15104                 return 0
15105         fi
15106         out=$(do_facet $tgt $LR_READER -c $dev)
15107         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15108         echo "$out"
15109         num=$(echo "$out" | grep -c "mdtlov")
15110         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
15111                 error "Invalid number of mdtlov clients returned by $LR_READER"
15112         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
15113
15114         # check lr_reader -cr on MDT0000
15115         out=$(do_facet $tgt $LR_READER -cr $dev)
15116         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15117         echo "$out"
15118         echo "$out" | grep -q "^reply_data:$" ||
15119                 error "$LR_READER should have returned 'reply_data' section"
15120         num=$(echo "$out" | grep -c "client_generation")
15121         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
15122 }
15123 run_test 252 "check lr_reader tool"
15124
15125 test_253_fill_ost() {
15126         local size_mb #how many MB should we write to pass watermark
15127         local lwm=$3  #low watermark
15128         local free_10mb #10% of free space
15129
15130         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15131         size_mb=$((free_kb / 1024 - lwm))
15132         free_10mb=$((free_kb / 10240))
15133         #If 10% of free space cross low watermark use it
15134         if (( free_10mb > size_mb )); then
15135                 size_mb=$free_10mb
15136         else
15137                 #At least we need to store 1.1 of difference between
15138                 #free space and low watermark
15139                 size_mb=$((size_mb + size_mb / 10))
15140         fi
15141         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
15142                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
15143                          oflag=append conv=notrunc
15144         fi
15145
15146         sleep_maxage
15147
15148         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15149         echo "OST still has $((free_kb / 1024)) mbytes free"
15150 }
15151
15152 test_253() {
15153         local ostidx=0
15154         local rc=0
15155
15156         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15157         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15158         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
15159
15160         local ost_name=$($LFS osts |
15161                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
15162         # on the mdt's osc
15163         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
15164         do_facet $SINGLEMDS $LCTL get_param -n \
15165                 osp.$mdtosc_proc1.reserved_mb_high ||
15166                 { skip  "remote MDS does not support reserved_mb_high" &&
15167                   return; }
15168
15169         rm -rf $DIR/$tdir
15170         wait_mds_ost_sync
15171         wait_delete_completed
15172         mkdir $DIR/$tdir
15173
15174         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
15175                         osp.$mdtosc_proc1.reserved_mb_high)
15176         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
15177                         osp.$mdtosc_proc1.reserved_mb_low)
15178         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
15179
15180         if ! combined_mgs_mds ; then
15181                 mount_mgs_client
15182         fi
15183         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
15184         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
15185                 error "Adding $ost_name to pool failed"
15186
15187         # Wait for client to see a OST at pool
15188         wait_update $HOSTNAME "$LCTL get_param -n
15189                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
15190                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
15191                 error "Client can not see the pool"
15192         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
15193                 error "Setstripe failed"
15194
15195         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
15196         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
15197         echo "OST still has $((blocks/1024)) mbytes free"
15198
15199         local new_lwm=$((blocks/1024-10))
15200         do_facet $SINGLEMDS $LCTL set_param \
15201                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
15202         do_facet $SINGLEMDS $LCTL set_param \
15203                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
15204
15205         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15206
15207         #First enospc could execute orphan deletion so repeat.
15208         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15209
15210         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15211                         osp.$mdtosc_proc1.prealloc_status)
15212         echo "prealloc_status $oa_status"
15213
15214         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
15215                 error "File creation should fail"
15216         #object allocation was stopped, but we still able to append files
15217         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
15218                 error "Append failed"
15219         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
15220
15221         wait_delete_completed
15222
15223         sleep_maxage
15224
15225         for i in $(seq 10 12); do
15226                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
15227                         error "File creation failed after rm";
15228         done
15229
15230         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15231                         osp.$mdtosc_proc1.prealloc_status)
15232         echo "prealloc_status $oa_status"
15233
15234         if (( oa_status != 0 )); then
15235                 error "Object allocation still disable after rm"
15236         fi
15237         do_facet $SINGLEMDS $LCTL set_param \
15238                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
15239         do_facet $SINGLEMDS $LCTL set_param \
15240                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
15241
15242
15243         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
15244                 error "Remove $ost_name from pool failed"
15245         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15246                 error "Pool destroy fialed"
15247
15248         if ! combined_mgs_mds ; then
15249                 umount_mgs_client
15250         fi
15251 }
15252 run_test 253 "Check object allocation limit"
15253
15254 test_254() {
15255         local cl_user
15256
15257         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15258         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15259         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
15260                 { skip "MDS does not support changelog_size" && return; }
15261
15262         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15263         echo "Registered as changelog user $cl_user"
15264
15265         $LFS changelog_clear $MDT0 $cl_user 0
15266
15267         local size1=$(do_facet mds1 \
15268                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15269         echo "Changelog size $size1"
15270
15271         rm -rf $DIR/$tdir
15272         $LFS mkdir -i 0 $DIR/$tdir
15273         # change something
15274         mkdir -p $DIR/$tdir/pics/2008/zachy
15275         touch $DIR/$tdir/pics/2008/zachy/timestamp
15276         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
15277         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15278         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15279         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15280         rm $DIR/$tdir/pics/desktop.jpg
15281
15282         local size2=$(do_facet mds1 \
15283                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15284         echo "Changelog size after work $size2"
15285
15286         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15287
15288         if (( size2 <= size1 )); then
15289                 error "Changelog size after work should be greater than original"
15290         fi
15291         return 0
15292 }
15293 run_test 254 "Check changelog size"
15294
15295 ladvise_no_type()
15296 {
15297         local type=$1
15298         local file=$2
15299
15300         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
15301                 awk -F: '{print $2}' | grep $type > /dev/null
15302         if [ $? -ne 0 ]; then
15303                 return 0
15304         fi
15305         return 1
15306 }
15307
15308 ladvise_no_ioctl()
15309 {
15310         local file=$1
15311
15312         lfs ladvise -a willread $file > /dev/null 2>&1
15313         if [ $? -eq 0 ]; then
15314                 return 1
15315         fi
15316
15317         lfs ladvise -a willread $file 2>&1 |
15318                 grep "Inappropriate ioctl for device" > /dev/null
15319         if [ $? -eq 0 ]; then
15320                 return 0
15321         fi
15322         return 1
15323 }
15324
15325 percent() {
15326         bc <<<"scale=2; ($1 - $2) * 100 / $2"
15327 }
15328
15329 # run a random read IO workload
15330 # usage: random_read_iops <filename> <filesize> <iosize>
15331 random_read_iops() {
15332         local file=$1
15333         local fsize=$2
15334         local iosize=${3:-4096}
15335
15336         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
15337                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
15338 }
15339
15340 drop_file_oss_cache() {
15341         local file="$1"
15342         local nodes="$2"
15343
15344         $LFS ladvise -a dontneed $file 2>/dev/null ||
15345                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
15346 }
15347
15348 ladvise_willread_performance()
15349 {
15350         local repeat=10
15351         local average_origin=0
15352         local average_cache=0
15353         local average_ladvise=0
15354
15355         for ((i = 1; i <= $repeat; i++)); do
15356                 echo "Iter $i/$repeat: reading without willread hint"
15357                 cancel_lru_locks osc
15358                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15359                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
15360                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
15361                 average_origin=$(bc <<<"$average_origin + $speed_origin")
15362
15363                 cancel_lru_locks osc
15364                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
15365                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
15366                 average_cache=$(bc <<<"$average_cache + $speed_cache")
15367
15368                 cancel_lru_locks osc
15369                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15370                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
15371                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
15372                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
15373                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
15374         done
15375         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
15376         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
15377         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
15378
15379         speedup_cache=$(percent $average_cache $average_origin)
15380         speedup_ladvise=$(percent $average_ladvise $average_origin)
15381
15382         echo "Average uncached read: $average_origin"
15383         echo "Average speedup with OSS cached read: " \
15384                 "$average_cache = +$speedup_cache%"
15385         echo "Average speedup with ladvise willread: " \
15386                 "$average_ladvise = +$speedup_ladvise%"
15387
15388         local lowest_speedup=20
15389         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
15390                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
15391                         "got $average_cache%. Skipping ladvise willread check."
15392                 return 0
15393         fi
15394
15395         # the test won't work on ZFS until it supports 'ladvise dontneed', but
15396         # it is still good to run until then to exercise 'ladvise willread'
15397         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15398                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15399                 echo "osd-zfs does not support dontneed or drop_caches" &&
15400                 return 0
15401
15402         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
15403         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
15404                 error_not_in_vm "Speedup with willread is less than " \
15405                         "$lowest_speedup%, got $average_ladvise%"
15406 }
15407
15408 test_255a() {
15409         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15410                 skip "lustre < 2.8.54 does not support ladvise " && return
15411         remote_ost_nodsh && skip "remote OST with nodsh" && return
15412
15413         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
15414
15415         ladvise_no_type willread $DIR/$tfile &&
15416                 skip "willread ladvise is not supported" && return
15417
15418         ladvise_no_ioctl $DIR/$tfile &&
15419                 skip "ladvise ioctl is not supported" && return
15420
15421         local size_mb=100
15422         local size=$((size_mb * 1048576))
15423         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15424                 error "dd to $DIR/$tfile failed"
15425
15426         lfs ladvise -a willread $DIR/$tfile ||
15427                 error "Ladvise failed with no range argument"
15428
15429         lfs ladvise -a willread -s 0 $DIR/$tfile ||
15430                 error "Ladvise failed with no -l or -e argument"
15431
15432         lfs ladvise -a willread -e 1 $DIR/$tfile ||
15433                 error "Ladvise failed with only -e argument"
15434
15435         lfs ladvise -a willread -l 1 $DIR/$tfile ||
15436                 error "Ladvise failed with only -l argument"
15437
15438         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
15439                 error "End offset should not be smaller than start offset"
15440
15441         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
15442                 error "End offset should not be equal to start offset"
15443
15444         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
15445                 error "Ladvise failed with overflowing -s argument"
15446
15447         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
15448                 error "Ladvise failed with overflowing -e argument"
15449
15450         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
15451                 error "Ladvise failed with overflowing -l argument"
15452
15453         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
15454                 error "Ladvise succeeded with conflicting -l and -e arguments"
15455
15456         echo "Synchronous ladvise should wait"
15457         local delay=4
15458 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
15459         do_nodes $(comma_list $(osts_nodes)) \
15460                 $LCTL set_param fail_val=$delay fail_loc=0x237
15461
15462         local start_ts=$SECONDS
15463         lfs ladvise -a willread $DIR/$tfile ||
15464                 error "Ladvise failed with no range argument"
15465         local end_ts=$SECONDS
15466         local inteval_ts=$((end_ts - start_ts))
15467
15468         if [ $inteval_ts -lt $(($delay - 1)) ]; then
15469                 error "Synchronous advice didn't wait reply"
15470         fi
15471
15472         echo "Asynchronous ladvise shouldn't wait"
15473         local start_ts=$SECONDS
15474         lfs ladvise -a willread -b $DIR/$tfile ||
15475                 error "Ladvise failed with no range argument"
15476         local end_ts=$SECONDS
15477         local inteval_ts=$((end_ts - start_ts))
15478
15479         if [ $inteval_ts -gt $(($delay / 2)) ]; then
15480                 error "Asynchronous advice blocked"
15481         fi
15482
15483         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
15484         ladvise_willread_performance
15485 }
15486 run_test 255a "check 'lfs ladvise -a willread'"
15487
15488 facet_meminfo() {
15489         local facet=$1
15490         local info=$2
15491
15492         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
15493 }
15494
15495 test_255b() {
15496         remote_ost_nodsh && skip "remote OST with nodsh" && return
15497
15498         lfs setstripe -c 1 -i 0 $DIR/$tfile
15499
15500         ladvise_no_type dontneed $DIR/$tfile &&
15501                 skip "dontneed ladvise is not supported" && return
15502
15503         ladvise_no_ioctl $DIR/$tfile &&
15504                 skip "ladvise ioctl is not supported" && return
15505
15506         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15507                 skip "lustre < 2.8.54 does not support ladvise" && return
15508
15509         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15510                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15511                 skip "zfs-osd does not support 'ladvise dontneed'" && return
15512
15513         local size_mb=100
15514         local size=$((size_mb * 1048576))
15515         # In order to prevent disturbance of other processes, only check 3/4
15516         # of the memory usage
15517         local kibibytes=$((size_mb * 1024 * 3 / 4))
15518
15519         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15520                 error "dd to $DIR/$tfile failed"
15521
15522         local total=$(facet_meminfo ost1 MemTotal)
15523         echo "Total memory: $total KiB"
15524
15525         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
15526         local before_read=$(facet_meminfo ost1 Cached)
15527         echo "Cache used before read: $before_read KiB"
15528
15529         lfs ladvise -a willread $DIR/$tfile ||
15530                 error "Ladvise willread failed"
15531         local after_read=$(facet_meminfo ost1 Cached)
15532         echo "Cache used after read: $after_read KiB"
15533
15534         lfs ladvise -a dontneed $DIR/$tfile ||
15535                 error "Ladvise dontneed again failed"
15536         local no_read=$(facet_meminfo ost1 Cached)
15537         echo "Cache used after dontneed ladvise: $no_read KiB"
15538
15539         if [ $total -lt $((before_read + kibibytes)) ]; then
15540                 echo "Memory is too small, abort checking"
15541                 return 0
15542         fi
15543
15544         if [ $((before_read + kibibytes)) -gt $after_read ]; then
15545                 error "Ladvise willread should use more memory" \
15546                         "than $kibibytes KiB"
15547         fi
15548
15549         if [ $((no_read + kibibytes)) -gt $after_read ]; then
15550                 error "Ladvise dontneed should release more memory" \
15551                         "than $kibibytes KiB"
15552         fi
15553 }
15554 run_test 255b "check 'lfs ladvise -a dontneed'"
15555
15556 test_255c() {
15557         local count
15558         local new_count
15559         local difference
15560         local i
15561         local rc
15562
15563         [ $(lustre_version_code ost1) -lt $(version_code 2.10.50) ] &&
15564                 skip "lustre < 2.10.53 does not support lockahead" && return
15565
15566         test_mkdir -p $DIR/$tdir
15567         $SETSTRIPE -i 0 $DIR/$tdir
15568
15569         #test 10 returns only success/failure
15570         i=10
15571         lockahead_test -d $DIR/$tdir -t $i
15572         rc=$?
15573         if [ $rc -eq 255 ]; then
15574                 error "Ladvise test${i} failed, ${rc}"
15575         fi
15576
15577         #test 11 counts lock enqueue requests, all others count new locks
15578         i=11
15579         count=$(do_facet ost1 \
15580                 $LCTL get_param -n ost.OSS.ost.stats)
15581         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
15582
15583         lockahead_test -d $DIR/$tdir -t $i
15584         rc=$?
15585         if [ $rc -eq 255 ]; then
15586                 error "Ladvise test${i} failed, ${rc}"
15587         fi
15588
15589         new_count=$(do_facet ost1 \
15590                 $LCTL get_param -n ost.OSS.ost.stats)
15591         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
15592                    awk '{ print $2 }')
15593
15594         difference="$((new_count - count))"
15595         if [ $difference -ne $rc ]; then
15596                 error "Ladvise test${i}, bad enqueue count, returned " \
15597                       "${rc}, actual ${difference}"
15598         fi
15599
15600         for i in $(seq 12 21); do
15601                 # If we do not do this, we run the risk of having too many
15602                 # locks and starting lock cancellation while we are checking
15603                 # lock counts.
15604                 cancel_lru_locks osc
15605
15606                 count=$($LCTL get_param -n \
15607                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15608
15609                 lockahead_test -d $DIR/$tdir -t $i
15610                 rc=$?
15611                 if [ $rc -eq 255 ]; then
15612                         error "Ladvise test ${i} failed, ${rc}"
15613                 fi
15614
15615                 new_count=$($LCTL get_param -n \
15616                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15617                 difference="$((new_count - count))"
15618
15619                 # Test 15 output is divided by 100 to map down to valid return
15620                 if [ $i -eq 15 ]; then
15621                         rc="$((rc * 100))"
15622                 fi
15623
15624                 if [ $difference -ne $rc ]; then
15625                         error "Ladvise test ${i}, bad lock count, returned " \
15626                               "${rc}, actual ${difference}"
15627                 fi
15628         done
15629
15630         #test 22 returns only success/failure
15631         i=22
15632         lockahead_test -d $DIR/$tdir -t $i
15633         rc=$?
15634         if [ $rc -eq 255 ]; then
15635                 error "Ladvise test${i} failed, ${rc}"
15636         fi
15637
15638 }
15639 run_test 255c "suite of ladvise lockahead tests"
15640
15641 test_256() {
15642         local cl_user
15643         local cat_sl
15644         local mdt_dev
15645
15646         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15647         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15648         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
15649                 skip "ldiskfs only test" && return
15650
15651         mdt_dev=$(mdsdevname 1)
15652         echo $mdt_dev
15653         cl_user=$(do_facet mds1 \
15654         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
15655         if [[ -n $cl_user ]]; then
15656                 skip "active changelog user"
15657                 return
15658         fi
15659
15660         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15661         echo "Registered as changelog user $cl_user"
15662
15663         rm -rf $DIR/$tdir
15664         mkdir -p $DIR/$tdir
15665
15666         $LFS changelog_clear $MDT0 $cl_user 0
15667
15668         # change something
15669         touch $DIR/$tdir/{1..10}
15670
15671         # stop the MDT
15672         stop mds1 || error "Fail to stop MDT."
15673
15674         # remount the MDT
15675         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
15676
15677         #after mount new plainllog is used
15678         touch $DIR/$tdir/{11..19}
15679         do_facet mds1 sync
15680         local TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15681         cat_sl=$(do_facet mds1 \
15682         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15683          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15684         do_facet mds1 rm $TEMP256FILE
15685
15686         if (( cat_sl != 2 )); then
15687                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15688                 error "Changelog catalog has wrong number of slots $cat_sl"
15689         fi
15690
15691         $LFS changelog_clear $MDT0 $cl_user 0
15692
15693         do_facet mds1 sync
15694         TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15695         cat_sl=$(do_facet mds1 \
15696         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15697          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15698         do_facet mds1 rm $TEMP256FILE
15699
15700         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15701
15702         if (( cat_sl == 2 )); then
15703                 error "Empty plain llog was not deleted from changelog catalog"
15704         fi
15705         if (( cat_sl != 1 )); then
15706                 error "Active plain llog shouldn\`t be deleted from catalog"
15707         fi
15708 }
15709 run_test 256 "Check llog delete for empty and not full state"
15710
15711 test_257() {
15712         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15713         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15714                 skip "Need MDS version at least 2.8.55" && return
15715
15716         test_mkdir $DIR/$tdir
15717
15718         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
15719                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
15720         stat $DIR/$tdir
15721
15722 #define OBD_FAIL_MDS_XATTR_REP                  0x161
15723         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15724         local facet=mds$((mdtidx + 1))
15725         set_nodes_failloc $(facet_active_host $facet) 0x80000161
15726         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
15727
15728         stop $facet || error "stop MDS failed"
15729         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
15730                 error "start MDS fail"
15731 }
15732 run_test 257 "xattr locks are not lost"
15733
15734 # Verify we take the i_mutex when security requires it
15735 test_258a() {
15736 #define OBD_FAIL_IMUTEX_SEC 0x141c
15737         $LCTL set_param fail_loc=0x141c
15738         touch $DIR/$tfile
15739         chmod u+s $DIR/$tfile
15740         chmod a+rwx $DIR/$tfile
15741         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15742         RC=$?
15743         if [ $RC -ne 0 ]; then
15744                 error "error, failed to take i_mutex, rc=$?"
15745         fi
15746         rm -f $DIR/$tfile
15747 }
15748 run_test 258a
15749
15750 # Verify we do NOT take the i_mutex in the normal case
15751 test_258b() {
15752 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
15753         $LCTL set_param fail_loc=0x141d
15754         touch $DIR/$tfile
15755         chmod a+rwx $DIR
15756         chmod a+rw $DIR/$tfile
15757         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15758         RC=$?
15759         if [ $RC -ne 0 ]; then
15760                 error "error, took i_mutex unnecessarily, rc=$?"
15761         fi
15762         rm -f $DIR/$tfile
15763
15764 }
15765 run_test 258b "verify i_mutex security behavior"
15766
15767 test_260() {
15768 #define OBD_FAIL_MDC_CLOSE               0x806
15769         $LCTL set_param fail_loc=0x80000806
15770         touch $DIR/$tfile
15771
15772 }
15773 run_test 260 "Check mdc_close fail"
15774
15775 ### Data-on-MDT sanity tests ###
15776 test_270a() {
15777
15778         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15779                 skip "Need MDS version at least 2.10.55" && return
15780
15781         # create DoM file
15782         local dom=$DIR/$tdir/dom_file
15783         local tmp=$DIR/$tdir/tmp_file
15784
15785         mkdir -p $DIR/$tdir
15786
15787         # basic checks for DoM component creation
15788         $LFS setstripe -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
15789                 error "Can set MDT layout to non-first entry"
15790
15791         $LFS setstripe -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
15792                 error "Can define multiple entries as MDT layout"
15793
15794         $LFS setstripe -E 1M -L mdt $dom ||
15795                 error "Can't create DoM layout"
15796
15797         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
15798         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
15799         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
15800
15801         local mdtidx=$($GETSTRIPE -M $dom)
15802         local mdtname=MDT$(printf %04x $mdtidx)
15803         local facet=mds$((mdtidx + 1))
15804         local space_check=1
15805
15806         # Skip free space checks with ZFS
15807         if [ "$(facet_fstype $facet)" == "zfs" ]; then
15808                 space_check=0
15809         fi
15810
15811         # write
15812         sync
15813         local mdtfree1=$(do_facet $facet \
15814                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15815         dd if=/dev/urandom of=$tmp bs=1024 count=100
15816         # check also direct IO along write
15817         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
15818         sync
15819         cmp $tmp $dom || error "file data is different"
15820         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
15821         if [ $space_check == 1 ]; then
15822                 local mdtfree2=$(do_facet $facet \
15823                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15824                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15825                         error "MDT free space is wrong after write"
15826         fi
15827
15828         # truncate
15829         $TRUNCATE $dom 10000
15830         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
15831         if [ $space_check == 1 ]; then
15832                 mdtfree1=$(do_facet $facet \
15833                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15834                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
15835                         error "MDT free space is wrong after truncate"
15836         fi
15837
15838         # append
15839         cat $tmp >> $dom
15840         sync
15841         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
15842         if [ $space_check == 1 ]; then
15843                 mdtfree2=$(do_facet $facet \
15844                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15845                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15846                         error "MDT free space is wrong after append"
15847         fi
15848
15849         # delete
15850         rm $dom
15851         if [ $space_check == 1 ]; then
15852                 mdtfree1=$(do_facet $facet \
15853                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15854                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
15855                         error "MDT free space is wrong after removal"
15856         fi
15857
15858         # combined striping
15859         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
15860                 error "Can't create DoM + OST striping"
15861
15862         dd if=/dev/urandom of=$tmp bs=1024 count=2000
15863         # check also direct IO along write
15864         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
15865         sync
15866         cmp $tmp $dom || error "file data is different"
15867         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
15868         rm $dom
15869         rm $tmp
15870
15871         return 0
15872 }
15873 run_test 270a "DoM: basic functionality tests"
15874
15875 test_270b() {
15876         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15877                 skip "Need MDS version at least 2.10.55" && return
15878
15879         local dom=$DIR/$tdir/dom_file
15880         local max_size=1048576
15881
15882         mkdir -p $DIR/$tdir
15883         $LFS setstripe -E $max_size -L mdt $dom
15884
15885         # truncate over the limit
15886         $TRUNCATE $dom $(($max_size + 1)) &&
15887                 error "successful truncate over the maximum size"
15888         # write over the limit
15889         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
15890                 error "successful write over the maximum size"
15891         # append over the limit
15892         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
15893         echo "12345" >> $dom && error "successful append over the maximum size"
15894         rm $dom
15895
15896         return 0
15897 }
15898 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
15899
15900 test_270c() {
15901         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15902                 skip "Need MDS version at least 2.10.55" && return
15903
15904         mkdir -p $DIR/$tdir
15905         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15906
15907         # check files inherit DoM EA
15908         touch $DIR/$tdir/first
15909         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
15910                 error "bad pattern"
15911         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
15912                 error "bad stripe count"
15913         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
15914                 error "bad stripe size"
15915
15916         # check directory inherits DoM EA and uses it as default
15917         mkdir $DIR/$tdir/subdir
15918         touch $DIR/$tdir/subdir/second
15919         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
15920                 error "bad pattern in sub-directory"
15921         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
15922                 error "bad stripe count in sub-directory"
15923         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
15924                 error "bad stripe size in sub-directory"
15925         return 0
15926 }
15927 run_test 270c "DoM: DoM EA inheritance tests"
15928
15929 test_270d() {
15930         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15931                 skip "Need MDS version at least 2.10.55" && return
15932
15933         mkdir -p $DIR/$tdir
15934         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15935
15936         # inherit default DoM striping
15937         mkdir $DIR/$tdir/subdir
15938         touch $DIR/$tdir/subdir/f1
15939
15940         # change default directory striping
15941         $LFS setstripe -c 1 $DIR/$tdir/subdir
15942         touch $DIR/$tdir/subdir/f2
15943         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
15944                 error "wrong default striping in file 2"
15945         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
15946                 error "bad pattern in file 2"
15947         return 0
15948 }
15949 run_test 270d "DoM: change striping from DoM to RAID0"
15950
15951 test_270e() {
15952         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15953                 skip "Need MDS version at least 2.10.55" && return
15954
15955         mkdir -p $DIR/$tdir/dom
15956         mkdir -p $DIR/$tdir/norm
15957         DOMFILES=20
15958         NORMFILES=10
15959         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
15960         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
15961
15962         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
15963         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
15964
15965         # find DoM files by layout
15966         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
15967         [ $NUM -eq  $DOMFILES ] ||
15968                 error "lfs find -L: found $NUM, expected $DOMFILES"
15969         echo "Test 1: lfs find 20 DOM files by layout: OK"
15970
15971         # there should be 1 dir with default DOM striping
15972         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
15973         [ $NUM -eq  1 ] ||
15974                 error "lfs find -L: found $NUM, expected 1 dir"
15975         echo "Test 2: lfs find 1 DOM dir by layout: OK"
15976
15977         # find DoM files by stripe size
15978         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
15979         [ $NUM -eq  $DOMFILES ] ||
15980                 error "lfs find -S: found $NUM, expected $DOMFILES"
15981         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
15982
15983         # find files by stripe offset except DoM files
15984         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
15985         [ $NUM -eq  $NORMFILES ] ||
15986                 error "lfs find -i: found $NUM, expected $NORMFILES"
15987         echo "Test 5: lfs find no DOM files by stripe index: OK"
15988         return 0
15989 }
15990 run_test 270e "DoM: lfs find with DoM files test"
15991
15992 test_270f() {
15993         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15994                 skip "Need MDS version at least 2.10.55" && return
15995
15996         local mdtname=${FSNAME}-MDT0000-mdtlov
15997         local dom=$DIR/$tdir/dom_file
15998         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
15999                                                 lod.$mdtname.dom_stripesize)
16000         local dom_limit=131072
16001
16002         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
16003         local dom_current=$(do_facet mds1 $LCTL get_param -n \
16004                                                 lod.$mdtname.dom_stripesize)
16005         [ ${dom_limit} -eq ${dom_current} ] ||
16006                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
16007
16008         $LFS mkdir -i 0 -c 1 $DIR/$tdir
16009         $LFS setstripe -d $DIR/$tdir
16010         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
16011                 error "Can't set directory default striping"
16012
16013         # exceed maximum stripe size
16014         $LFS setstripe -E $(($dom_limit * 2)) -L mdt $dom &&
16015                 error "Able to create DoM component size more than LOD limit"
16016
16017         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
16018         dom_current=$(do_facet mds1 $LCTL get_param -n \
16019                                                 lod.$mdtname.dom_stripesize)
16020         [ 0 -eq ${dom_current} ] ||
16021                 error "Can't set zero DoM stripe limit"
16022
16023         # too low values to be aligned with smallest stripe size 64K
16024         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
16025         dom_current=$(do_facet mds1 $LCTL get_param -n \
16026                                                 lod.$mdtname.dom_stripesize)
16027         [ 30000 -eq ${dom_current} ] &&
16028                 error "Can set too small DoM stripe limit"
16029
16030         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
16031         dom_current=$(do_facet mds1 $LCTL get_param -n \
16032                                                 lod.$mdtname.dom_stripesize)
16033         echo $dom_current
16034         [ 2147483648 -eq ${dom_current} ] &&
16035                 error "Can set too large DoM stripe limit"
16036
16037         do_facet mds1 $LCTL set_param -n \
16038                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
16039         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
16040                 error "Can't create DoM component size after limit change"
16041         do_facet mds1 $LCTL set_param -n \
16042                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
16043         $LFS setstripe -E $dom_limit -L mdt ${dom}_big &&
16044                 error "Can create big DoM component after limit decrease"
16045         touch ${dom}_def ||
16046                 error "Can't create file with old default layout"
16047
16048         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
16049         return 0
16050 }
16051 run_test 270f "DoM: maximum DoM stripe size checks"
16052
16053 test_271a() {
16054         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16055                 skip "Need MDS version at least 2.10.55" && return
16056
16057         local dom=$DIR/$tdir/dom
16058
16059         mkdir -p $DIR/$tdir
16060
16061         $LFS setstripe -E 1024K -L mdt $dom
16062
16063         lctl set_param -n mdc.*.stats=clear
16064         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16065         cat $dom > /dev/null
16066         local reads=$(lctl get_param -n mdc.*.stats |
16067                         awk '/ost_read/ {print $2}')
16068         [ -z $reads ] || error "Unexpected $reads READ RPCs"
16069         ls $dom
16070         rm -f $dom
16071 }
16072 run_test 271a "DoM: data is cached for read after write"
16073
16074 test_271b() {
16075         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16076                 skip "Need MDS version at least 2.10.55" && return
16077
16078         local dom=$DIR/$tdir/dom
16079
16080         mkdir -p $DIR/$tdir
16081
16082         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16083
16084         lctl set_param -n mdc.*.stats=clear
16085         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16086         cancel_lru_locks mdc
16087         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
16088         # second stat to check size is cached on client
16089         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
16090         local gls=$(lctl get_param -n mdc.*.stats |
16091                         awk '/ldlm_glimpse/ {print $2}')
16092         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16093         rm -f $dom
16094 }
16095 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
16096
16097 test_271ba() {
16098         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16099                 skip "Need MDS version at least 2.10.55" && return
16100
16101         local dom=$DIR/$tdir/dom
16102
16103         mkdir -p $DIR/$tdir
16104
16105         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16106
16107         lctl set_param -n mdc.*.stats=clear
16108         lctl set_param -n osc.*.stats=clear
16109         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
16110         cancel_lru_locks mdc
16111         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16112         # second stat to check size is cached on client
16113         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16114         local gls=$(lctl get_param -n mdc.*.stats |
16115                         awk '/ldlm_glimpse/ {print $2}')
16116         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16117         local gls=$(lctl get_param -n osc.*.stats |
16118                         awk '/ldlm_glimpse/ {print $2}')
16119         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
16120         rm -f $dom
16121 }
16122 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
16123
16124 test_271c() {
16125         # test to be enabled with lock_convert
16126         skip "skipped until lock convert will be implemented" && return
16127
16128         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16129                 skip "Need MDS version at least 2.10.55" && return
16130
16131         local dom=$DIR/$tdir/dom
16132
16133         mkdir -p $DIR/$tdir
16134
16135         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16136
16137         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
16138         local facet=mds$((mdtidx + 1))
16139
16140         cancel_lru_locks mdc
16141         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
16142         createmany -o $dom 1000
16143         lctl set_param -n mdc.*.stats=clear
16144         smalliomany -w $dom 1000 200
16145         lctl get_param -n mdc.*.stats
16146         local enq=$(lctl get_param -n mdc.*.stats |
16147                         awk '/ldlm_ibits_enqueue/ {print $2}')
16148         # Each file has 1 open, 1 IO enqueues, total 2000
16149         # but now we have also +1 getxattr for security.capability, total 3000
16150         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
16151         unlinkmany $dom 1000
16152
16153         cancel_lru_locks mdc
16154         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
16155         createmany -o $dom 1000
16156         lctl set_param -n mdc.*.stats=clear
16157         smalliomany -w $dom 1000 200
16158         lctl get_param -n mdc.*.stats
16159         local enq_2=$(lctl get_param -n mdc.*.stats |
16160                         awk '/ldlm_ibits_enqueue/ {print $2}')
16161         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
16162         # for OPEN and IO lock.
16163         [ $((enq - enq_2)) -ge 1000 ] ||
16164                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
16165         unlinkmany $dom 1000
16166         return 0
16167 }
16168 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
16169
16170 cleanup_test_300() {
16171         trap 0
16172         umask $SAVE_UMASK
16173 }
16174 test_striped_dir() {
16175         local mdt_index=$1
16176         local stripe_count
16177         local stripe_index
16178
16179         mkdir -p $DIR/$tdir
16180
16181         SAVE_UMASK=$(umask)
16182         trap cleanup_test_300 RETURN EXIT
16183
16184         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
16185                                                 $DIR/$tdir/striped_dir ||
16186                 error "set striped dir error"
16187
16188         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
16189         [ "$mode" = "755" ] || error "expect 755 got $mode"
16190
16191         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
16192                 error "getdirstripe failed"
16193         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
16194         if [ "$stripe_count" != "2" ]; then
16195                 error "1:stripe_count is $stripe_count, expect 2"
16196         fi
16197         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
16198         if [ "$stripe_count" != "2" ]; then
16199                 error "2:stripe_count is $stripe_count, expect 2"
16200         fi
16201
16202         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
16203         if [ "$stripe_index" != "$mdt_index" ]; then
16204                 error "stripe_index is $stripe_index, expect $mdt_index"
16205         fi
16206
16207         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
16208                 error "nlink error after create striped dir"
16209
16210         mkdir $DIR/$tdir/striped_dir/a
16211         mkdir $DIR/$tdir/striped_dir/b
16212
16213         stat $DIR/$tdir/striped_dir/a ||
16214                 error "create dir under striped dir failed"
16215         stat $DIR/$tdir/striped_dir/b ||
16216                 error "create dir under striped dir failed"
16217
16218         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
16219                 error "nlink error after mkdir"
16220
16221         rmdir $DIR/$tdir/striped_dir/a
16222         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
16223                 error "nlink error after rmdir"
16224
16225         rmdir $DIR/$tdir/striped_dir/b
16226         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
16227                 error "nlink error after rmdir"
16228
16229         chattr +i $DIR/$tdir/striped_dir
16230         createmany -o $DIR/$tdir/striped_dir/f 10 &&
16231                 error "immutable flags not working under striped dir!"
16232         chattr -i $DIR/$tdir/striped_dir
16233
16234         rmdir $DIR/$tdir/striped_dir ||
16235                 error "rmdir striped dir error"
16236
16237         cleanup_test_300
16238
16239         true
16240 }
16241
16242 test_300a() {
16243         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16244                 skip "skipped for lustre < 2.7.0" && return
16245         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16246         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16247
16248         test_striped_dir 0 || error "failed on striped dir on MDT0"
16249         test_striped_dir 1 || error "failed on striped dir on MDT0"
16250 }
16251 run_test 300a "basic striped dir sanity test"
16252
16253 test_300b() {
16254         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16255                 skip "skipped for lustre < 2.7.0" && return
16256         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16257         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16258         local i
16259         local mtime1
16260         local mtime2
16261         local mtime3
16262
16263         test_mkdir $DIR/$tdir
16264         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16265                 error "set striped dir error"
16266         for ((i=0; i<10; i++)); do
16267                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
16268                 sleep 1
16269                 touch $DIR/$tdir/striped_dir/file_$i ||
16270                                         error "touch error $i"
16271                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
16272                 [ $mtime1 -eq $mtime2 ] &&
16273                         error "mtime not change after create"
16274                 sleep 1
16275                 rm -f $DIR/$tdir/striped_dir/file_$i ||
16276                                         error "unlink error $i"
16277                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
16278                 [ $mtime2 -eq $mtime3 ] &&
16279                         error "mtime did not change after unlink"
16280         done
16281         true
16282 }
16283 run_test 300b "check ctime/mtime for striped dir"
16284
16285 test_300c() {
16286         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16287                 skip "skipped for lustre < 2.7.0" && return
16288         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16289         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16290         local file_count
16291
16292         mkdir -p $DIR/$tdir
16293         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
16294                 error "set striped dir error"
16295
16296         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
16297                 error "chown striped dir failed"
16298
16299         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
16300                 error "create 5k files failed"
16301
16302         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
16303
16304         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
16305
16306         rm -rf $DIR/$tdir
16307 }
16308 run_test 300c "chown && check ls under striped directory"
16309
16310 test_300d() {
16311         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16312                 skip "skipped for lustre < 2.7.0" && return
16313         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16314         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16315         local stripe_count
16316         local file
16317
16318         mkdir -p $DIR/$tdir
16319         $SETSTRIPE -c 2 $DIR/$tdir
16320
16321         #local striped directory
16322         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16323                 error "set striped dir error"
16324         createmany -o $DIR/$tdir/striped_dir/f 10 ||
16325                 error "create 10 files failed"
16326
16327         #remote striped directory
16328         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
16329                 error "set striped dir error"
16330         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
16331                 error "create 10 files failed"
16332
16333         for file in $(find $DIR/$tdir); do
16334                 stripe_count=$($GETSTRIPE -c $file)
16335                 [ $stripe_count -eq 2 ] ||
16336                         error "wrong stripe $stripe_count for $file"
16337         done
16338
16339         rm -rf $DIR/$tdir
16340 }
16341 run_test 300d "check default stripe under striped directory"
16342
16343 test_300e() {
16344         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16345                 skip "Need MDS version at least 2.7.55" && return
16346         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16347         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16348         local stripe_count
16349         local file
16350
16351         mkdir -p $DIR/$tdir
16352
16353         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16354                 error "set striped dir error"
16355
16356         touch $DIR/$tdir/striped_dir/a
16357         touch $DIR/$tdir/striped_dir/b
16358         touch $DIR/$tdir/striped_dir/c
16359
16360         mkdir $DIR/$tdir/striped_dir/dir_a
16361         mkdir $DIR/$tdir/striped_dir/dir_b
16362         mkdir $DIR/$tdir/striped_dir/dir_c
16363
16364         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
16365                 error "set striped adir under striped dir error"
16366
16367         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
16368                 error "set striped bdir under striped dir error"
16369
16370         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
16371                 error "set striped cdir under striped dir error"
16372
16373         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
16374                 error "rename dir under striped dir fails"
16375
16376         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
16377                 error "rename dir under different stripes fails"
16378
16379         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
16380                 error "rename file under striped dir should succeed"
16381
16382         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
16383                 error "rename dir under striped dir should succeed"
16384
16385         rm -rf $DIR/$tdir
16386 }
16387 run_test 300e "check rename under striped directory"
16388
16389 test_300f() {
16390         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16391                 skip "Need MDS version at least 2.7.55" && return
16392         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16393         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16394         local stripe_count
16395         local file
16396
16397         rm -rf $DIR/$tdir
16398         mkdir -p $DIR/$tdir
16399
16400         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16401                 error "set striped dir error"
16402
16403         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
16404                 error "set striped dir error"
16405
16406         touch $DIR/$tdir/striped_dir/a
16407         mkdir $DIR/$tdir/striped_dir/dir_a
16408         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
16409                 error "create striped dir under striped dir fails"
16410
16411         touch $DIR/$tdir/striped_dir1/b
16412         mkdir $DIR/$tdir/striped_dir1/dir_b
16413         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
16414                 error "create striped dir under striped dir fails"
16415
16416         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
16417                 error "rename dir under different striped dir should fail"
16418
16419         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
16420                 error "rename striped dir under diff striped dir should fail"
16421
16422         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
16423                 error "rename file under diff striped dirs fails"
16424
16425         rm -rf $DIR/$tdir
16426 }
16427 run_test 300f "check rename cross striped directory"
16428
16429 test_300_check_default_striped_dir()
16430 {
16431         local dirname=$1
16432         local default_count=$2
16433         local default_index=$3
16434         local stripe_count
16435         local stripe_index
16436         local dir_stripe_index
16437         local dir
16438
16439         echo "checking $dirname $default_count $default_index"
16440         $LFS setdirstripe -D -c $default_count -i $default_index \
16441                                 -t all_char $DIR/$tdir/$dirname ||
16442                 error "set default stripe on striped dir error"
16443         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
16444         [ $stripe_count -eq $default_count ] ||
16445                 error "expect $default_count get $stripe_count for $dirname"
16446
16447         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
16448         [ $stripe_index -eq $default_index ] ||
16449                 error "expect $default_index get $stripe_index for $dirname"
16450
16451         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
16452                                                 error "create dirs failed"
16453
16454         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
16455         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
16456         for dir in $(find $DIR/$tdir/$dirname/*); do
16457                 stripe_count=$($LFS getdirstripe -c $dir)
16458                 [ $stripe_count -eq $default_count ] ||
16459                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
16460                 error "stripe count $default_count != $stripe_count for $dir"
16461
16462                 stripe_index=$($LFS getdirstripe -i $dir)
16463                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
16464                         error "$stripe_index != $default_index for $dir"
16465
16466                 #check default stripe
16467                 stripe_count=$($LFS getdirstripe -D -c $dir)
16468                 [ $stripe_count -eq $default_count ] ||
16469                 error "default count $default_count != $stripe_count for $dir"
16470
16471                 stripe_index=$($LFS getdirstripe -D -i $dir)
16472                 [ $stripe_index -eq $default_index ] ||
16473                 error "default index $default_index != $stripe_index for $dir"
16474         done
16475         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
16476 }
16477
16478 test_300g() {
16479         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16480                 skip "Need MDS version at least 2.7.55" && return
16481         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16482         local dir
16483         local stripe_count
16484         local stripe_index
16485
16486         mkdir $DIR/$tdir
16487         mkdir $DIR/$tdir/normal_dir
16488
16489         #Checking when client cache stripe index
16490         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
16491         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
16492                 error "create striped_dir failed"
16493
16494         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
16495                 error "create dir0 fails"
16496         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
16497         [ $stripe_index -eq 0 ] ||
16498                 error "dir0 expect index 0 got $stripe_index"
16499
16500         mkdir $DIR/$tdir/striped_dir/dir1 ||
16501                 error "create dir1 fails"
16502         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
16503         [ $stripe_index -eq 1 ] ||
16504                 error "dir1 expect index 1 got $stripe_index"
16505
16506         #check default stripe count/stripe index
16507         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
16508         test_300_check_default_striped_dir normal_dir 1 0
16509         test_300_check_default_striped_dir normal_dir 2 1
16510         test_300_check_default_striped_dir normal_dir 2 -1
16511
16512         #delete default stripe information
16513         echo "delete default stripeEA"
16514         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
16515                 error "set default stripe on striped dir error"
16516
16517         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
16518         for dir in $(find $DIR/$tdir/normal_dir/*); do
16519                 stripe_count=$($LFS getdirstripe -c $dir)
16520                 [ $stripe_count -eq 0 ] ||
16521                         error "expect 1 get $stripe_count for $dir"
16522                 stripe_index=$($LFS getdirstripe -i $dir)
16523                 [ $stripe_index -eq 0 ] ||
16524                         error "expect 0 get $stripe_index for $dir"
16525         done
16526 }
16527 run_test 300g "check default striped directory for normal directory"
16528
16529 test_300h() {
16530         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16531                 skip "Need MDS version at least 2.7.55" && return
16532         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16533         local dir
16534         local stripe_count
16535
16536         mkdir $DIR/$tdir
16537         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
16538                                         $DIR/$tdir/striped_dir ||
16539                 error "set striped dir error"
16540
16541         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
16542         test_300_check_default_striped_dir striped_dir 1 0
16543         test_300_check_default_striped_dir striped_dir 2 1
16544         test_300_check_default_striped_dir striped_dir 2 -1
16545
16546         #delete default stripe information
16547         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
16548                 error "set default stripe on striped dir error"
16549
16550         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
16551         for dir in $(find $DIR/$tdir/striped_dir/*); do
16552                 stripe_count=$($LFS getdirstripe -c $dir)
16553                 [ $stripe_count -eq 0 ] ||
16554                         error "expect 1 get $stripe_count for $dir"
16555         done
16556 }
16557 run_test 300h "check default striped directory for striped directory"
16558
16559 test_300i() {
16560         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16561                 skip "Need MDS version at least 2.7.55" && return
16562         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16563         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16564         local stripe_count
16565         local file
16566
16567         mkdir $DIR/$tdir
16568
16569         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16570                 error "set striped dir error"
16571
16572         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16573                 error "create files under striped dir failed"
16574
16575         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
16576                 error "set striped hashdir error"
16577
16578         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
16579                 error "create dir0 under hash dir failed"
16580         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
16581                 error "create dir1 under hash dir failed"
16582
16583         # unfortunately, we need to umount to clear dir layout cache for now
16584         # once we fully implement dir layout, we can drop this
16585         umount_client $MOUNT || error "umount failed"
16586         mount_client $MOUNT || error "mount failed"
16587
16588         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
16589         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
16590         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
16591
16592         #set the stripe to be unknown hash type
16593         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
16594         $LCTL set_param fail_loc=0x1901
16595         for ((i = 0; i < 10; i++)); do
16596                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
16597                         error "stat f-$i failed"
16598                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
16599         done
16600
16601         touch $DIR/$tdir/striped_dir/f0 &&
16602                 error "create under striped dir with unknown hash should fail"
16603
16604         $LCTL set_param fail_loc=0
16605
16606         umount_client $MOUNT || error "umount failed"
16607         mount_client $MOUNT || error "mount failed"
16608
16609         return 0
16610 }
16611 run_test 300i "client handle unknown hash type striped directory"
16612
16613 test_300j() {
16614         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16615         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16616                 skip "Need MDS version at least 2.7.55" && return
16617         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16618         local stripe_count
16619         local file
16620
16621         mkdir $DIR/$tdir
16622
16623         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
16624         $LCTL set_param fail_loc=0x1702
16625         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16626                 error "set striped dir error"
16627
16628         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16629                 error "create files under striped dir failed"
16630
16631         $LCTL set_param fail_loc=0
16632
16633         rm -rf $DIR/$tdir || error "unlink striped dir fails"
16634
16635         return 0
16636 }
16637 run_test 300j "test large update record"
16638
16639 test_300k() {
16640         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16641                 skip "Need MDS version at least 2.7.55" && return
16642         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16643         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16644         local stripe_count
16645         local file
16646
16647         mkdir $DIR/$tdir
16648
16649         #define OBD_FAIL_LARGE_STRIPE   0x1703
16650         $LCTL set_param fail_loc=0x1703
16651         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
16652                 error "set striped dir error"
16653         $LCTL set_param fail_loc=0
16654
16655         $LFS getdirstripe $DIR/$tdir/striped_dir ||
16656                 error "getstripeddir fails"
16657         rm -rf $DIR/$tdir/striped_dir ||
16658                 error "unlink striped dir fails"
16659
16660         return 0
16661 }
16662 run_test 300k "test large striped directory"
16663
16664 test_300l() {
16665         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16666                 skip "Need MDS version at least 2.7.55" && return
16667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16668         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16669         local stripe_index
16670
16671         test_mkdir -p $DIR/$tdir/striped_dir
16672         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
16673                         error "chown $RUNAS_ID failed"
16674         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
16675                 error "set default striped dir failed"
16676
16677         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
16678         $LCTL set_param fail_loc=0x80000158
16679         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
16680
16681         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
16682         [ $stripe_index -eq 1 ] ||
16683                 error "expect 1 get $stripe_index for $dir"
16684 }
16685 run_test 300l "non-root user to create dir under striped dir with stale layout"
16686
16687 test_300m() {
16688         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16689                 skip "Need MDS version at least 2.7.55" && return
16690         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16691         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
16692
16693         mkdir -p $DIR/$tdir/striped_dir
16694         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
16695                 error "set default stripes dir error"
16696
16697         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
16698
16699         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
16700         [ $stripe_count -eq 0 ] ||
16701                         error "expect 0 get $stripe_count for a"
16702
16703         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
16704                 error "set default stripes dir error"
16705
16706         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
16707
16708         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
16709         [ $stripe_count -eq 0 ] ||
16710                         error "expect 0 get $stripe_count for b"
16711
16712         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
16713                 error "set default stripes dir error"
16714
16715         mkdir $DIR/$tdir/striped_dir/c &&
16716                 error "default stripe_index is invalid, mkdir c should fails"
16717
16718         rm -rf $DIR/$tdir || error "rmdir fails"
16719 }
16720 run_test 300m "setstriped directory on single MDT FS"
16721
16722 cleanup_300n() {
16723         local list=$(comma_list $(mdts_nodes))
16724
16725         trap 0
16726         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16727 }
16728
16729 test_300n() {
16730         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16731                 skip "Need MDS version at least 2.7.55" && return
16732         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16733         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16734         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16735
16736         local stripe_index
16737         local list=$(comma_list $(mdts_nodes))
16738
16739         trap cleanup_300n RETURN EXIT
16740         mkdir -p $DIR/$tdir
16741         chmod 777 $DIR/$tdir
16742         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
16743                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16744                 error "create striped dir succeeds with gid=0"
16745
16746         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16747         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
16748                 error "create striped dir fails with gid=-1"
16749
16750         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16751         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
16752                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16753                 error "set default striped dir succeeds with gid=0"
16754
16755
16756         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16757         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
16758                 error "set default striped dir fails with gid=-1"
16759
16760
16761         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16762         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
16763                                         error "create test_dir fails"
16764         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
16765                                         error "create test_dir1 fails"
16766         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
16767                                         error "create test_dir2 fails"
16768         cleanup_300n
16769 }
16770 run_test 300n "non-root user to create dir under striped dir with default EA"
16771
16772 test_300o() {
16773         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16774                 skip "Need MDS version at least 2.7.55" && return
16775         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16776         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16777         local numfree1
16778         local numfree2
16779
16780         mkdir -p $DIR/$tdir
16781
16782         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
16783         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
16784         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
16785                 skip "not enough free inodes $numfree1 $numfree2"
16786                 return
16787         fi
16788
16789         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
16790         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
16791         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
16792                 skip "not enough free space $numfree1 $numfree2"
16793                 return
16794         fi
16795
16796         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
16797                 error "setdirstripe fails"
16798
16799         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
16800                 error "create dirs fails"
16801
16802         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
16803         ls $DIR/$tdir/striped_dir > /dev/null ||
16804                 error "ls striped dir fails"
16805         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
16806                 error "unlink big striped dir fails"
16807 }
16808 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
16809
16810 test_300p() {
16811         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16812         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16813         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16814
16815         mkdir -p $DIR/$tdir
16816
16817         #define OBD_FAIL_OUT_ENOSPC     0x1704
16818         do_facet mds2 lctl set_param fail_loc=0x80001704
16819         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
16820                         error "create striped directory should fail"
16821
16822         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
16823
16824         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
16825         true
16826 }
16827 run_test 300p "create striped directory without space"
16828
16829 test_300q() {
16830         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16831         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16832
16833         local fd=$(free_fd)
16834         local cmd="exec $fd<$tdir"
16835         cd $DIR
16836         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
16837         eval $cmd
16838         cmd="exec $fd<&-"
16839         trap "eval $cmd" EXIT
16840         cd $tdir || error "cd $tdir fails"
16841         rmdir  ../$tdir || error "rmdir $tdir fails"
16842         mkdir local_dir && error "create dir succeeds"
16843         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
16844         eval $cmd
16845         return 0
16846 }
16847 run_test 300q "create remote directory under orphan directory"
16848
16849 prepare_remote_file() {
16850         mkdir $DIR/$tdir/src_dir ||
16851                 error "create remote source failed"
16852
16853         cp /etc/hosts $DIR/$tdir/src_dir/a || error
16854         touch $DIR/$tdir/src_dir/a
16855
16856         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
16857                 error "create remote target dir failed"
16858
16859         touch $DIR/$tdir/tgt_dir/b
16860
16861         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
16862                 error "rename dir cross MDT failed!"
16863
16864         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
16865                 error "src_child still exists after rename"
16866
16867         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
16868                 error "missing file(a) after rename"
16869
16870         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
16871                 error "diff after rename"
16872 }
16873
16874 test_310a() {
16875         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16876         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16877         local remote_file=$DIR/$tdir/tgt_dir/b
16878
16879         mkdir -p $DIR/$tdir
16880
16881         prepare_remote_file || error "prepare remote file failed"
16882
16883         #open-unlink file
16884         $OPENUNLINK $remote_file $remote_file || error
16885         $CHECKSTAT -a $remote_file || error
16886 }
16887 run_test 310a "open unlink remote file"
16888
16889 test_310b() {
16890         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16891         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16892         local remote_file=$DIR/$tdir/tgt_dir/b
16893
16894         mkdir -p $DIR/$tdir
16895
16896         prepare_remote_file || error "prepare remote file failed"
16897
16898         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16899         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
16900         $CHECKSTAT -t file $remote_file || error "check file failed"
16901 }
16902 run_test 310b "unlink remote file with multiple links while open"
16903
16904 test_310c() {
16905         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
16906         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16907         local remote_file=$DIR/$tdir/tgt_dir/b
16908
16909         mkdir -p $DIR/$tdir
16910
16911         prepare_remote_file || error "prepare remote file failed"
16912
16913         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16914         multiop_bg_pause $remote_file O_uc ||
16915                         error "mulitop failed for remote file"
16916         MULTIPID=$!
16917         $MULTIOP $DIR/$tfile Ouc
16918         kill -USR1 $MULTIPID
16919         wait $MULTIPID
16920 }
16921 run_test 310c "open-unlink remote file with multiple links"
16922
16923 #LU-4825
16924 test_311() {
16925         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16926                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16928         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16929
16930         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16931
16932         mkdir -p $DIR/$tdir
16933         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16934         createmany -o $DIR/$tdir/$tfile. 1000
16935
16936         # statfs data is not real time, let's just calculate it
16937         old_iused=$((old_iused + 1000))
16938
16939         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16940                         osp.*OST0000*MDT0000.create_count")
16941         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16942                                 osp.*OST0000*MDT0000.max_create_count")
16943         for idx in $(seq $MDSCOUNT); do
16944                 do_facet mds$idx "lctl set_param -n \
16945                         osp.*OST0000*MDT000?.max_create_count=0"
16946         done
16947
16948         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16949         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16950         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16951
16952         unlinkmany $DIR/$tdir/$tfile. 1000
16953
16954         for idx in $(seq $MDSCOUNT); do
16955                 do_facet mds$idx "lctl set_param -n \
16956                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16957                 do_facet mds$idx "lctl set_param -n \
16958                         osp.*OST0000*MDT000?.create_count=$count"
16959         done
16960
16961         local new_iused
16962         for i in $(seq 120); do
16963                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16964                 # system may be too busy to destroy all objs in time, use
16965                 # a somewhat small value to not fail autotest
16966                 [ $((old_iused - new_iused)) -gt 400 ] && break
16967                 sleep 1
16968         done
16969
16970         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16971         [ $((old_iused - new_iused)) -gt 400 ] ||
16972                 error "objs not destroyed after unlink"
16973 }
16974 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16975
16976 zfs_oid_to_objid()
16977 {
16978         local ost=$1
16979         local objid=$2
16980
16981         local vdevdir=$(dirname $(facet_vdevice $ost))
16982         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
16983         local zfs_zapid=$(do_facet $ost $cmd |
16984                           grep -w "/O/0/d$((objid%32))" -C 5 |
16985                           awk '/Object/{getline; print $1}')
16986         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
16987                           awk "/$objid = /"'{printf $3}')
16988
16989         echo $zfs_objid
16990 }
16991
16992 zfs_object_blksz() {
16993         local ost=$1
16994         local objid=$2
16995
16996         local vdevdir=$(dirname $(facet_vdevice $ost))
16997         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
16998         local blksz=$(do_facet $ost $cmd $objid |
16999                       awk '/dblk/{getline; printf $4}')
17000
17001         case "${blksz: -1}" in
17002                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
17003                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
17004                 *) ;;
17005         esac
17006
17007         echo $blksz
17008 }
17009
17010 test_312() { # LU-4856
17011         remote_ost_nodsh && skip "remote OST with nodsh" && return
17012
17013         [ $(facet_fstype ost1) = "zfs" ] ||
17014                 { skip "the test only applies to zfs" && return; }
17015
17016         local max_blksz=$(do_facet ost1 \
17017                           $ZFS get -p recordsize $(facet_device ost1) |
17018                           awk '!/VALUE/{print $3}')
17019         local min_blksz=$(getconf PAGE_SIZE)
17020
17021         # to make life a little bit easier
17022         $LFS mkdir -c 1 -i 0 $DIR/$tdir
17023         $LFS setstripe -c 1 -i 0 $DIR/$tdir
17024
17025         local tf=$DIR/$tdir/$tfile
17026         touch $tf
17027         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17028
17029         # Get ZFS object id
17030         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17031
17032         # block size change by sequential over write
17033         local blksz
17034         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
17035                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
17036
17037                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17038                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
17039         done
17040         rm -f $tf
17041
17042         # block size change by sequential append write
17043         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
17044         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17045         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17046
17047         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
17048                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
17049                         oflag=sync conv=notrunc
17050
17051                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17052                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
17053                         error "blksz error, actual $blksz, "    \
17054                                 "expected: 2 * $count * $min_blksz"
17055         done
17056         rm -f $tf
17057
17058         # random write
17059         touch $tf
17060         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17061         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17062
17063         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
17064         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17065         [ $blksz -eq $min_blksz ] ||
17066                 error "blksz error: $blksz, expected: $min_blksz"
17067
17068         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
17069         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17070         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
17071
17072         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
17073         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17074         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
17075 }
17076 run_test 312 "make sure ZFS adjusts its block size by write pattern"
17077
17078 test_313() {
17079         remote_ost_nodsh && skip "remote OST with nodsh" && return
17080
17081         local file=$DIR/$tfile
17082         rm -f $file
17083         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
17084
17085         # define OBD_FAIL_TGT_RCVD_EIO           0x720
17086         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17087         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
17088                 error "write should failed"
17089         do_facet ost1 "$LCTL set_param fail_loc=0"
17090         rm -f $file
17091 }
17092 run_test 313 "io should fail after last_rcvd update fail"
17093
17094 test_314() {
17095         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
17096         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17097         rm -f $DIR/$tfile
17098         wait_delete_completed
17099         do_facet ost1 "$LCTL set_param fail_loc=0"
17100 }
17101 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
17102
17103 test_315() { # LU-618
17104         local file=$DIR/$tfile
17105         rm -f $file
17106
17107         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
17108         $MULTIOP $file oO_RDONLY:r4096000_c &
17109         PID=$!
17110
17111         sleep 2
17112
17113         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
17114         kill -USR1 $PID
17115
17116         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
17117         rm -f $file
17118 }
17119 run_test 315 "read should be accounted"
17120
17121 test_fake_rw() {
17122         local read_write=$1
17123         if [ "$read_write" = "write" ]; then
17124                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
17125         elif [ "$read_write" = "read" ]; then
17126                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
17127         else
17128                 error "argument error"
17129         fi
17130
17131         # turn off debug for performance testing
17132         local saved_debug=$($LCTL get_param -n debug)
17133         $LCTL set_param debug=0
17134
17135         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
17136
17137         # get ost1 size - lustre-OST0000
17138         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
17139         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
17140         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
17141
17142         if [ "$read_write" = "read" ]; then
17143                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
17144         fi
17145
17146         local start_time=$(date +%s.%N)
17147         $dd_cmd bs=1M count=$blocks oflag=sync ||
17148                 error "real dd $read_write error"
17149         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
17150
17151         if [ "$read_write" = "write" ]; then
17152                 rm -f $DIR/$tfile
17153         fi
17154
17155         # define OBD_FAIL_OST_FAKE_RW           0x238
17156         do_facet ost1 $LCTL set_param fail_loc=0x238
17157
17158         local start_time=$(date +%s.%N)
17159         $dd_cmd bs=1M count=$blocks oflag=sync ||
17160                 error "fake dd $read_write error"
17161         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
17162
17163         if [ "$read_write" = "write" ]; then
17164                 # verify file size
17165                 cancel_lru_locks osc
17166                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
17167                         error "$tfile size not $blocks MB"
17168         fi
17169         do_facet ost1 $LCTL set_param fail_loc=0
17170
17171         echo "fake $read_write $duration_fake vs. normal $read_write" \
17172                 "$duration in seconds"
17173         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
17174                 error_not_in_vm "fake write is slower"
17175
17176         $LCTL set_param -n debug="$saved_debug"
17177         rm -f $DIR/$tfile
17178 }
17179 test_399a() { # LU-7655 for OST fake write
17180         remote_ost_nodsh && skip "remote OST with nodsh" && return
17181
17182         test_fake_rw write
17183 }
17184 run_test 399a "fake write should not be slower than normal write"
17185
17186 test_399b() { # LU-8726 for OST fake read
17187         remote_ost_nodsh && skip "remote OST with nodsh" && return
17188
17189         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
17190                 skip "ldiskfs only test" && return 0
17191         fi
17192         test_fake_rw read
17193 }
17194 run_test 399b "fake read should not be slower than normal read"
17195
17196 test_400a() { # LU-1606, was conf-sanity test_74
17197         local extra_flags=''
17198         local out=$TMP/$tfile
17199         local prefix=/usr/include/lustre
17200         local prog
17201
17202         if ! which $CC > /dev/null 2>&1; then
17203                 skip_env "$CC is not installed"
17204                 return 0
17205         fi
17206
17207         if ! [[ -d $prefix ]]; then
17208                 # Assume we're running in tree and fixup the include path.
17209                 extra_flags+=" -I$LUSTRE/include"
17210                 extra_flags+=" -L$LUSTRE/utils"
17211         fi
17212
17213         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
17214                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
17215                         error "client api broken"
17216         done
17217         rm -f $out
17218 }
17219 run_test 400a "Lustre client api program can compile and link"
17220
17221 test_400b() { # LU-1606, LU-5011
17222         local header
17223         local out=$TMP/$tfile
17224         local prefix=/usr/include/linux/lustre
17225
17226         # We use a hard coded prefix so that this test will not fail
17227         # when run in tree. There are headers in lustre/include/lustre/
17228         # that are not packaged (like lustre_idl.h) and have more
17229         # complicated include dependencies (like config.h and lnet/types.h).
17230         # Since this test about correct packaging we just skip them when
17231         # they don't exist (see below) rather than try to fixup cppflags.
17232
17233         if ! which $CC > /dev/null 2>&1; then
17234                 skip_env "$CC is not installed"
17235                 return 0
17236         fi
17237
17238         for header in $prefix/*.h; do
17239                 if ! [[ -f "$header" ]]; then
17240                         continue
17241                 fi
17242
17243                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
17244                         continue # lustre_ioctl.h is internal header
17245                 fi
17246
17247                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
17248                         error "cannot compile '$header'"
17249         done
17250         rm -f $out
17251 }
17252 run_test 400b "packaged headers can be compiled"
17253
17254 test_401a() { #LU-7437
17255         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
17256         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
17257                 return
17258         #count the number of parameters by "list_param -R"
17259         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
17260         #count the number of parameters by listing proc files
17261         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
17262         echo "proc_dirs='$proc_dirs'"
17263         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
17264         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
17265                       sort -u | wc -l)
17266
17267         [ $params -eq $procs ] ||
17268                 error "found $params parameters vs. $procs proc files"
17269
17270         # test the list_param -D option only returns directories
17271         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
17272         #count the number of parameters by listing proc directories
17273         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
17274                 sort -u | wc -l)
17275
17276         [ $params -eq $procs ] ||
17277                 error "found $params parameters vs. $procs proc files"
17278 }
17279 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
17280
17281 test_401b() {
17282         local save=$($LCTL get_param -n jobid_var)
17283         local tmp=testing
17284
17285         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
17286                 error "no error returned when setting bad parameters"
17287
17288         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
17289         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
17290
17291         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
17292         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
17293         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
17294 }
17295 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
17296
17297 test_401c() {
17298         local jobid_var_old=$($LCTL get_param -n jobid_var)
17299         local jobid_var_new
17300
17301         $LCTL set_param jobid_var= &&
17302                 error "no error returned for 'set_param a='"
17303
17304         jobid_var_new=$($LCTL get_param -n jobid_var)
17305         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17306                 error "jobid_var was changed by setting without value"
17307
17308         $LCTL set_param jobid_var &&
17309                 error "no error returned for 'set_param a'"
17310
17311         jobid_var_new=$($LCTL get_param -n jobid_var)
17312         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17313                 error "jobid_var was changed by setting without value"
17314 }
17315 run_test 401c "Verify 'lctl set_param' without value fails in either format."
17316
17317 test_401d() {
17318         local jobid_var_old=$($LCTL get_param -n jobid_var)
17319         local jobid_var_new
17320         local new_value="foo=bar"
17321
17322         $LCTL set_param jobid_var=$new_value ||
17323                 error "'set_param a=b' did not accept a value containing '='"
17324
17325         jobid_var_new=$($LCTL get_param -n jobid_var)
17326         [[ "$jobid_var_new" == "$new_value" ]] ||
17327                 error "'set_param a=b' failed on a value containing '='"
17328
17329         # Reset the jobid_var to test the other format
17330         $LCTL set_param jobid_var=$jobid_var_old
17331         jobid_var_new=$($LCTL get_param -n jobid_var)
17332         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17333                 error "failed to reset jobid_var"
17334
17335         $LCTL set_param jobid_var $new_value ||
17336                 error "'set_param a b' did not accept a value containing '='"
17337
17338         jobid_var_new=$($LCTL get_param -n jobid_var)
17339         [[ "$jobid_var_new" == "$new_value" ]] ||
17340                 error "'set_param a b' failed on a value containing '='"
17341
17342         $LCTL set_param jobid_var $jobid_var_old
17343         jobid_var_new=$($LCTL get_param -n jobid_var)
17344         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17345                 error "failed to reset jobid_var"
17346 }
17347 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
17348
17349 test_402() {
17350         local server_version=$(lustre_version_code $SINGLEMDS)
17351         [[ $server_version -ge $(version_code 2.7.66) ]] ||
17352         [[ $server_version -ge $(version_code 2.7.18.4) &&
17353                 $server_version -lt $(version_code 2.7.50) ]] ||
17354         [[ $server_version -ge $(version_code 2.7.2) &&
17355                 $server_version -lt $(version_code 2.7.11) ]] ||
17356                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
17357                         return; }
17358         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17359         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
17360 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
17361         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
17362         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
17363                 echo "Touch failed - OK"
17364 }
17365 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
17366
17367 test_403() {
17368         local file1=$DIR/$tfile.1
17369         local file2=$DIR/$tfile.2
17370         local tfile=$TMP/$tfile
17371
17372         rm -f $file1 $file2 $tfile
17373
17374         touch $file1
17375         ln $file1 $file2
17376
17377         # 30 sec OBD_TIMEOUT in ll_getattr()
17378         # right before populating st_nlink
17379         $LCTL set_param fail_loc=0x80001409
17380         stat -c %h $file1 > $tfile &
17381
17382         # create an alias, drop all locks and reclaim the dentry
17383         < $file2
17384         cancel_lru_locks mdc
17385         cancel_lru_locks osc
17386         sysctl -w vm.drop_caches=2
17387
17388         wait
17389
17390         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
17391
17392         rm -f $tfile $file1 $file2
17393 }
17394 run_test 403 "i_nlink should not drop to zero due to aliasing"
17395
17396 test_404() { # LU-6601
17397         local server_version=$(lustre_version_code $SINGLEMDS)
17398         [[ $server_version -ge $(version_code 2.8.53) ]] ||
17399                 { skip "Need server version newer than 2.8.52"; return 0; }
17400
17401         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17402         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
17403                 awk '/osp .*-osc-MDT/ { print $4}')
17404
17405         local osp
17406         for osp in $mosps; do
17407                 echo "Deactivate: " $osp
17408                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
17409                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17410                         awk -vp=$osp '$4 == p { print $2 }')
17411                 [ $stat = IN ] || {
17412                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17413                         error "deactivate error"
17414                 }
17415                 echo "Activate: " $osp
17416                 do_facet $SINGLEMDS $LCTL --device %$osp activate
17417                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17418                         awk -vp=$osp '$4 == p { print $2 }')
17419                 [ $stat = UP ] || {
17420                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17421                         error "activate error"
17422                 }
17423         done
17424 }
17425 run_test 404 "validate manual {de}activated works properly for OSPs"
17426
17427 test_405() {
17428         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
17429         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
17430                 skip "Layout swap lock is not supported" && return
17431
17432         check_swap_layouts_support && return 0
17433
17434         test_mkdir $DIR/$tdir
17435         swap_lock_test -d $DIR/$tdir ||
17436                 error "One layout swap locked test failed"
17437 }
17438 run_test 405 "Various layout swap lock tests"
17439
17440 test_406() {
17441         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17442         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
17443         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
17444         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17445         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
17446                 skip "Need MDS version at least 2.8.50" && return
17447
17448         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
17449         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
17450         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
17451         local def_pool=$($GETSTRIPE -p $MOUNT)
17452         local test_pool=$TESTNAME
17453
17454         if ! combined_mgs_mds ; then
17455                 mount_mgs_client
17456         fi
17457         pool_add $test_pool || error "pool_add failed"
17458         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
17459                 error "pool_add_targets failed"
17460
17461         # parent set default stripe count only, child will stripe from both
17462         # parent and fs default
17463         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
17464                 error "setstripe $MOUNT failed"
17465         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
17466         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
17467         for i in $(seq 10); do
17468                 local f=$DIR/$tdir/$tfile.$i
17469                 touch $f || error "touch failed"
17470                 local count=$($GETSTRIPE -c $f)
17471                 [ $count -eq $OSTCOUNT ] ||
17472                         error "$f stripe count $count != $OSTCOUNT"
17473                 local offset=$($GETSTRIPE -i $f)
17474                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
17475                 local size=$($GETSTRIPE -S $f)
17476                 [ $size -eq $((def_stripe_size * 2)) ] ||
17477                         error "$f stripe size $size != $((def_stripe_size * 2))"
17478                 local pool=$($GETSTRIPE -p $f)
17479                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
17480         done
17481
17482         # change fs default striping, delete parent default striping, now child
17483         # will stripe from new fs default striping only
17484         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
17485                 error "change $MOUNT default stripe failed"
17486         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
17487         for i in $(seq 11 20); do
17488                 local f=$DIR/$tdir/$tfile.$i
17489                 touch $f || error "touch $f failed"
17490                 local count=$($GETSTRIPE -c $f)
17491                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
17492                 local offset=$($GETSTRIPE -i $f)
17493                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
17494                 local size=$($GETSTRIPE -S $f)
17495                 [ $size -eq $def_stripe_size ] ||
17496                         error "$f stripe size $size != $def_stripe_size"
17497                 local pool=$($GETSTRIPE -p $f)
17498                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
17499
17500         done
17501
17502         unlinkmany $DIR/$tdir/$tfile. 1 20
17503
17504         # restore FS default striping
17505         if [ -z $def_pool ]; then
17506                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17507                         -i $def_stripe_offset $MOUNT ||
17508                         error "restore default striping failed"
17509         else
17510                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17511                         -i $def_stripe_offset -p $def_pool $MOUNT ||
17512                         error "restore default striping with $def_pool failed"
17513         fi
17514
17515         local f=$DIR/$tdir/$tfile
17516         pool_remove_all_targets $test_pool $f
17517         pool_remove $test_pool $f
17518
17519         if ! combined_mgs_mds ; then
17520                 umount_mgs_client
17521         fi
17522 }
17523 run_test 406 "DNE support fs default striping"
17524
17525 test_407() {
17526         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17527         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
17528                 skip "Need MDS version at least 2.8.55" && return
17529         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17530
17531         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
17532                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
17533         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
17534                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
17535         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
17536
17537         #define OBD_FAIL_DT_TXN_STOP    0x2019
17538         for idx in $(seq $MDSCOUNT); do
17539                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
17540         done
17541         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
17542         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
17543                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
17544         true
17545 }
17546 run_test 407 "transaction fail should cause operation fail"
17547
17548 test_408() {
17549         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
17550
17551         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
17552         lctl set_param fail_loc=0x8000040a
17553         # let ll_prepare_partial_page() fail
17554         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
17555
17556         rm -f $DIR/$tfile
17557
17558         # create at least 100 unused inodes so that
17559         # shrink_icache_memory(0) should not return 0
17560         touch $DIR/$tfile-{0..100}
17561         rm -f $DIR/$tfile-{0..100}
17562         sync
17563
17564         echo 2 > /proc/sys/vm/drop_caches
17565 }
17566 run_test 408 "drop_caches should not hang due to page leaks"
17567
17568 test_409()
17569 {
17570         [ $MDSCOUNT -lt 2 ] &&
17571                 skip "We need at least 2 MDTs for this test" && return
17572
17573         check_mount_and_prep
17574
17575         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
17576         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
17577         touch $DIR/$tdir/guard || error "(2) Fail to create"
17578
17579         local PREFIX=$(str_repeat 'A' 128)
17580         echo "Create 1K hard links start at $(date)"
17581         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17582                 error "(3) Fail to hard link"
17583
17584         echo "Links count should be right although linkEA overflow"
17585         stat $DIR/$tdir/guard || error "(4) Fail to stat"
17586         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
17587         [ $linkcount -eq 1001 ] ||
17588                 error "(5) Unexpected hard links count: $linkcount"
17589
17590         echo "List all links start at $(date)"
17591         ls -l $DIR/$tdir/foo > /dev/null ||
17592                 error "(6) Fail to list $DIR/$tdir/foo"
17593
17594         echo "Unlink hard links start at $(date)"
17595         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17596                 error "(7) Fail to unlink"
17597 }
17598 run_test 409 "Large amount of cross-MDTs hard links on the same file"
17599
17600 test_410()
17601 {
17602         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
17603                 skip "Need client version at least 2.9.59" && return
17604
17605         # Create a file, and stat it from the kernel
17606         local testfile=$DIR/$tfile
17607         touch $testfile
17608
17609         local run_id=$RANDOM
17610         local my_ino=$(stat --format "%i" $testfile)
17611
17612         # Try to insert the module. This will always fail as the
17613         # module is designed to not be inserted.
17614         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
17615             &> /dev/null
17616
17617         # Anything but success is a test failure
17618         dmesg | grep -q \
17619             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
17620             error "no inode match"
17621 }
17622 run_test 410 "Test inode number returned from kernel thread"
17623
17624 cleanup_test411_cgroup() {
17625         trap 0
17626         rmdir "$1"
17627 }
17628
17629 test_411() {
17630         local cg_basedir=/sys/fs/cgroup/memory
17631         # LU-9966
17632         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
17633                 { skip "no setup for cgroup"; return; }
17634
17635         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
17636                 error "test file creation failed"
17637         cancel_lru_locks osc
17638
17639         # Create a very small memory cgroup to force a slab allocation error
17640         local cgdir=$cg_basedir/osc_slab_alloc
17641         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
17642         trap "cleanup_test411_cgroup $cgdir" EXIT
17643         echo 2M > $cgdir/memory.kmem.limit_in_bytes
17644         echo 1M > $cgdir/memory.limit_in_bytes
17645
17646         # Should not LBUG, just be killed by oom-killer
17647         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
17648                 error "fail to trigger a memory allocation error"
17649
17650         cleanup_test411_cgroup $cgdir
17651
17652         return 0
17653 }
17654 run_test 411 "Slab allocation error with cgroup does not LBUG"
17655
17656 test_412() {
17657         [ $MDSCOUNT -lt 2 ] &&
17658                 skip "We need at least 2 MDTs for this test" && return
17659
17660         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
17661                 skip "Need server version at least 2.10.55" & exit 0
17662         fi
17663
17664         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
17665                 error "mkdir failed"
17666         $LFS getdirstripe $DIR/$tdir
17667         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
17668         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
17669                 error "expect $((MDSCOUT - 1)) get $stripe_index"
17670         stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
17671         [ $stripe_count -eq 2 ] ||
17672                 error "expect 2 get $stripe_count"
17673 }
17674 run_test 412 "mkdir on specific MDTs"
17675
17676 prep_801() {
17677         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17678         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17679                 skip "Need server version at least 2.9.55" & exit 0
17680         start_full_debug_logging
17681 }
17682
17683 post_801() {
17684         stop_full_debug_logging
17685 }
17686
17687 barrier_stat() {
17688         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17689                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17690                            awk '/The barrier for/ { print $7 }')
17691                 echo $st
17692         else
17693                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
17694                 echo \'$st\'
17695         fi
17696 }
17697
17698 barrier_expired() {
17699         local expired
17700
17701         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17702                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17703                           awk '/will be expired/ { print $7 }')
17704         else
17705                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
17706         fi
17707
17708         echo $expired
17709 }
17710
17711 test_801a() {
17712         prep_801
17713
17714         echo "Start barrier_freeze at: $(date)"
17715         #define OBD_FAIL_BARRIER_DELAY          0x2202
17716         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17717         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
17718
17719         sleep 2
17720         local b_status=$(barrier_stat)
17721         echo "Got barrier status at: $(date)"
17722         [ "$b_status" = "'freezing_p1'" ] ||
17723                 error "(1) unexpected barrier status $b_status"
17724
17725         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17726         wait
17727         b_status=$(barrier_stat)
17728         [ "$b_status" = "'frozen'" ] ||
17729                 error "(2) unexpected barrier status $b_status"
17730
17731         local expired=$(barrier_expired)
17732         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
17733         sleep $((expired + 3))
17734
17735         b_status=$(barrier_stat)
17736         [ "$b_status" = "'expired'" ] ||
17737                 error "(3) unexpected barrier status $b_status"
17738
17739         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
17740                 error "(4) fail to freeze barrier"
17741
17742         b_status=$(barrier_stat)
17743         [ "$b_status" = "'frozen'" ] ||
17744                 error "(5) unexpected barrier status $b_status"
17745
17746         echo "Start barrier_thaw at: $(date)"
17747         #define OBD_FAIL_BARRIER_DELAY          0x2202
17748         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17749         do_facet mgs $LCTL barrier_thaw $FSNAME &
17750
17751         sleep 2
17752         b_status=$(barrier_stat)
17753         echo "Got barrier status at: $(date)"
17754         [ "$b_status" = "'thawing'" ] ||
17755                 error "(6) unexpected barrier status $b_status"
17756
17757         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17758         wait
17759         b_status=$(barrier_stat)
17760         [ "$b_status" = "'thawed'" ] ||
17761                 error "(7) unexpected barrier status $b_status"
17762
17763         #define OBD_FAIL_BARRIER_FAILURE        0x2203
17764         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
17765         do_facet mgs $LCTL barrier_freeze $FSNAME
17766
17767         b_status=$(barrier_stat)
17768         [ "$b_status" = "'failed'" ] ||
17769                 error "(8) unexpected barrier status $b_status"
17770
17771         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17772         do_facet mgs $LCTL barrier_thaw $FSNAME
17773
17774         post_801
17775 }
17776 run_test 801a "write barrier user interfaces and stat machine"
17777
17778 test_801b() {
17779         prep_801
17780
17781         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17782         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
17783         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
17784         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
17785         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
17786
17787         cancel_lru_locks mdc
17788
17789         # 180 seconds should be long enough
17790         do_facet mgs $LCTL barrier_freeze $FSNAME 180
17791
17792         local b_status=$(barrier_stat)
17793         [ "$b_status" = "'frozen'" ] ||
17794                 error "(6) unexpected barrier status $b_status"
17795
17796         mkdir $DIR/$tdir/d0/d10 &
17797         mkdir_pid=$!
17798
17799         touch $DIR/$tdir/d1/f13 &
17800         touch_pid=$!
17801
17802         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
17803         ln_pid=$!
17804
17805         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
17806         mv_pid=$!
17807
17808         rm -f $DIR/$tdir/d4/f12 &
17809         rm_pid=$!
17810
17811         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
17812
17813         # To guarantee taht the 'stat' is not blocked
17814         b_status=$(barrier_stat)
17815         [ "$b_status" = "'frozen'" ] ||
17816                 error "(8) unexpected barrier status $b_status"
17817
17818         # let above commands to run at background
17819         sleep 5
17820
17821         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
17822         ps -p $touch_pid || error "(10) touch should be blocked"
17823         ps -p $ln_pid || error "(11) link should be blocked"
17824         ps -p $mv_pid || error "(12) rename should be blocked"
17825         ps -p $rm_pid || error "(13) unlink should be blocked"
17826
17827         b_status=$(barrier_stat)
17828         [ "$b_status" = "'frozen'" ] ||
17829                 error "(14) unexpected barrier status $b_status"
17830
17831         do_facet mgs $LCTL barrier_thaw $FSNAME
17832         b_status=$(barrier_stat)
17833         [ "$b_status" = "'thawed'" ] ||
17834                 error "(15) unexpected barrier status $b_status"
17835
17836         wait $mkdir_pid || error "(16) mkdir should succeed"
17837         wait $touch_pid || error "(17) touch should succeed"
17838         wait $ln_pid || error "(18) link should succeed"
17839         wait $mv_pid || error "(19) rename should succeed"
17840         wait $rm_pid || error "(20) unlink should succeed"
17841
17842         post_801
17843 }
17844 run_test 801b "modification will be blocked by write barrier"
17845
17846 test_801c() {
17847         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17848
17849         prep_801
17850
17851         stop mds2 || error "(1) Fail to stop mds2"
17852
17853         do_facet mgs $LCTL barrier_freeze $FSNAME 30
17854
17855         local b_status=$(barrier_stat)
17856         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
17857                 do_facet mgs $LCTL barrier_thaw $FSNAME
17858                 error "(2) unexpected barrier status $b_status"
17859         }
17860
17861         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17862                 error "(3) Fail to rescan barrier bitmap"
17863
17864         do_facet mgs $LCTL barrier_freeze $FSNAME 10
17865
17866         b_status=$(barrier_stat)
17867         [ "$b_status" = "'frozen'" ] ||
17868                 error "(4) unexpected barrier status $b_status"
17869
17870         do_facet mgs $LCTL barrier_thaw $FSNAME
17871         b_status=$(barrier_stat)
17872         [ "$b_status" = "'thawed'" ] ||
17873                 error "(5) unexpected barrier status $b_status"
17874
17875         local devname=$(mdsdevname 2)
17876
17877         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
17878
17879         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17880                 error "(7) Fail to rescan barrier bitmap"
17881
17882         post_801
17883 }
17884 run_test 801c "rescan barrier bitmap"
17885
17886 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
17887 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
17888 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
17889
17890 cleanup_802() {
17891         trap 0
17892
17893         stopall
17894         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
17895         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
17896         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
17897         setupall
17898 }
17899
17900 test_802() {
17901
17902         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17903         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17904                 skip "Need server version at least 2.9.55" & exit 0
17905
17906         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17907
17908         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
17909                 error "(2) Fail to copy"
17910
17911         trap cleanup_802 EXIT
17912
17913         # sync by force before remount as readonly
17914         sync; sync_all_data; sleep 3; sync_all_data
17915
17916         stopall
17917
17918         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
17919         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
17920         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
17921
17922         echo "Mount the server as read only"
17923         setupall server_only || error "(3) Fail to start servers"
17924
17925         echo "Mount client without ro should fail"
17926         mount_client $MOUNT &&
17927                 error "(4) Mount client without 'ro' should fail"
17928
17929         echo "Mount client with ro should succeed"
17930         mount_client $MOUNT ro ||
17931                 error "(5) Mount client with 'ro' should succeed"
17932
17933         echo "Modify should be refused"
17934         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
17935
17936         echo "Read should be allowed"
17937         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
17938                 error "(7) Read should succeed under ro mode"
17939
17940         cleanup_802
17941 }
17942 run_test 802 "simulate readonly device"
17943
17944 test_803() {
17945         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17946         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
17947                 skip "MDS needs to be newer than 2.10.54" && return
17948
17949         mkdir -p $DIR/$tdir
17950         # Create some objects on all MDTs to trigger related logs objects
17951         for idx in $(seq $MDSCOUNT); do
17952                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
17953                         $DIR/$tdir/dir${idx} ||
17954                         error "Fail to create $DIR/$tdir/dir${idx}"
17955         done
17956
17957         sync; sleep 5
17958         echo "before create:"
17959         $LFS df -i $MOUNT
17960         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17961
17962         for ((i=0; i<10; i++)); do
17963                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
17964                         error "Fail to create $DIR/$tdir/foo$i"
17965         done
17966
17967         sync; sleep 5
17968         echo "after create:"
17969         $LFS df -i $MOUNT
17970         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17971
17972         [ $after_used -ge $((before_used + 10)) ] ||
17973                 error "before ($before_used) + 10 > after ($after_used)"
17974
17975         for ((i=0; i<10; i++)); do
17976                 rm -rf $DIR/$tdir/foo$i ||
17977                         error "Fail to remove $DIR/$tdir/foo$i"
17978         done
17979
17980         wait_delete_completed
17981         echo "after unlink:"
17982         $LFS df -i $MOUNT
17983         before_used=$after_used
17984         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17985
17986         [ $after_used -le $((before_used - 8)) ] ||
17987                 error "before ($before_used) - 8 < after ($after_used)"
17988 }
17989 run_test 803 "verify agent object for remote object"
17990
17991 test_804() {
17992         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17993         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
17994                 skip "MDS needs to be newer than 2.10.54" && return
17995
17996         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
17997                 skip "ldiskfs only test" && return 0
17998
17999         mkdir -p $DIR/$tdir
18000         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
18001                 error "Fail to create $DIR/$tdir/dir0"
18002
18003         local fid=$($LFS path2fid $DIR/$tdir/dir0)
18004         local dev=$(mdsdevname 2)
18005
18006         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18007                 grep ${fid} || error "NOT found agent entry for dir0"
18008
18009         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
18010                 error "Fail to create $DIR/$tdir/dir1"
18011
18012         touch $DIR/$tdir/dir1/foo0 ||
18013                 error "Fail to create $DIR/$tdir/dir1/foo0"
18014         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
18015         local rc=0
18016
18017         for idx in $(seq $MDSCOUNT); do
18018                 dev=$(mdsdevname $idx)
18019                 do_facet mds${idx} \
18020                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18021                         grep ${fid} && rc=$idx
18022         done
18023
18024         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
18025                 error "Fail to rename foo0 to foo1"
18026         if [ $rc -eq 0 ]; then
18027                 for idx in $(seq $MDSCOUNT); do
18028                         dev=$(mdsdevname $idx)
18029                         do_facet mds${idx} \
18030                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18031                         grep ${fid} && rc=$idx
18032                 done
18033         fi
18034
18035         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
18036                 error "Fail to rename foo1 to foo2"
18037         if [ $rc -eq 0 ]; then
18038                 for idx in $(seq $MDSCOUNT); do
18039                         dev=$(mdsdevname $idx)
18040                         do_facet mds${idx} \
18041                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18042                         grep ${fid} && rc=$idx
18043                 done
18044         fi
18045
18046         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
18047
18048         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
18049                 error "Fail to link to $DIR/$tdir/dir1/foo2"
18050         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
18051                 error "Fail to rename foo2 to foo0"
18052         unlink $DIR/$tdir/dir1/foo0 ||
18053                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
18054         rm -rf $DIR/$tdir/dir0 ||
18055                 error "Fail to rm $DIR/$tdir/dir0"
18056
18057         for idx in $(seq $MDSCOUNT); do
18058                 dev=$(mdsdevname $idx)
18059                 rc=0
18060
18061                 stop mds${idx}
18062                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
18063                         rc=$?
18064                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
18065                         error "mount mds$idx failed"
18066                 df $MOUNT > /dev/null 2>&1
18067
18068                 # e2fsck should not return error
18069                 [ $rc -eq 0 ] ||
18070                         error "e2fsck detected error on MDT${idx}: rc=$rc"
18071         done
18072 }
18073 run_test 804 "verify agent entry for remote entry"
18074
18075 #
18076 # tests that do cleanup/setup should be run at the end
18077 #
18078
18079 test_900() {
18080         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18081         local ls
18082         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
18083         $LCTL set_param fail_loc=0x903
18084
18085         cancel_lru_locks MGC
18086
18087         FAIL_ON_ERROR=true cleanup
18088         FAIL_ON_ERROR=true setup
18089 }
18090 run_test 900 "umount should not race with any mgc requeue thread"
18091
18092 complete $SECONDS
18093 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
18094 check_and_cleanup_lustre
18095 if [ "$I_MOUNTED" != "yes" ]; then
18096         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
18097 fi
18098 exit_status