Whamcloud - gitweb
LU-9411 tests: remove spaces around '+='
[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_130f() {
10043         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
10044         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP" &&
10045                 return
10046
10047         local fm_file=$DIR/$tfile
10048         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
10049                 error "multiop create with lov_delay_create on $fm_file"
10050
10051         filefrag -ves $fm_file || error "filefrag $fm_file failed"
10052         filefrag_extents=$(filefrag -vek $fm_file |
10053                            awk '/extents? found/ { print $2 }')
10054         if [[ "$filefrag_extents" != "0" ]]; then
10055                 error "FIEMAP on $fm_file failed; " \
10056                       "returned $filefrag_extents expected 0"
10057         fi
10058
10059         rm -f $fm_file
10060 }
10061 run_test 130f "FIEMAP (unstriped file)"
10062
10063 # Test for writev/readv
10064 test_131a() {
10065         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
10066                 error "writev test failed"
10067         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
10068                 error "readv failed"
10069         rm -f $DIR/$tfile
10070 }
10071 run_test 131a "test iov's crossing stripe boundary for writev/readv"
10072
10073 test_131b() {
10074         local fsize=$((524288 + 1048576 + 1572864))
10075         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
10076                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10077                         error "append writev test failed"
10078
10079         ((fsize += 1572864 + 1048576))
10080         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
10081                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
10082                         error "append writev test failed"
10083         rm -f $DIR/$tfile
10084 }
10085 run_test 131b "test append writev"
10086
10087 test_131c() {
10088         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
10089         error "NOT PASS"
10090 }
10091 run_test 131c "test read/write on file w/o objects"
10092
10093 test_131d() {
10094         rwv -f $DIR/$tfile -w -n 1 1572864
10095         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
10096         if [ "$NOB" != 1572864 ]; then
10097                 error "Short read filed: read $NOB bytes instead of 1572864"
10098         fi
10099         rm -f $DIR/$tfile
10100 }
10101 run_test 131d "test short read"
10102
10103 test_131e() {
10104         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
10105         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
10106         error "read hitting hole failed"
10107         rm -f $DIR/$tfile
10108 }
10109 run_test 131e "test read hitting hole"
10110
10111 check_stats() {
10112         local facet=$1
10113         local op=$2
10114         local want=${3:-0}
10115         local res
10116
10117         case $facet in
10118         mds*) res=$(do_facet $facet \
10119                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
10120                  ;;
10121         ost*) res=$(do_facet $facet \
10122                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
10123                  ;;
10124         *) error "Wrong facet '$facet'" ;;
10125         esac
10126         echo $res
10127         [ "$res" ] || error "The counter for $op on $facet was not incremented"
10128         # if the argument $3 is zero, it means any stat increment is ok.
10129         if [[ $want -gt 0 ]]; then
10130                 local count=$(echo $res | awk '{ print $2 }')
10131                 [[ $count -ne $want ]] &&
10132                         error "The $op counter on $facet is $count, not $want"
10133         fi
10134 }
10135
10136 test_133a() {
10137         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10138         remote_ost_nodsh && skip "remote OST with nodsh" && return
10139         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10140
10141         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10142                 { skip "MDS doesn't support rename stats"; return; }
10143         local testdir=$DIR/${tdir}/stats_testdir
10144         mkdir -p $DIR/${tdir}
10145
10146         # clear stats.
10147         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10148         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10149
10150         # verify mdt stats first.
10151         mkdir ${testdir} || error "mkdir failed"
10152         check_stats $SINGLEMDS "mkdir" 1
10153         touch ${testdir}/${tfile} || error "touch failed"
10154         check_stats $SINGLEMDS "open" 1
10155         check_stats $SINGLEMDS "close" 1
10156         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.8.54) ] && {
10157                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
10158                 check_stats $SINGLEMDS "mknod" 2
10159         }
10160         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
10161         check_stats $SINGLEMDS "unlink" 1
10162         rm -f ${testdir}/${tfile} || error "file remove failed"
10163         check_stats $SINGLEMDS "unlink" 2
10164
10165         # remove working dir and check mdt stats again.
10166         rmdir ${testdir} || error "rmdir failed"
10167         check_stats $SINGLEMDS "rmdir" 1
10168
10169         local testdir1=$DIR/${tdir}/stats_testdir1
10170         mkdir -p ${testdir}
10171         mkdir -p ${testdir1}
10172         touch ${testdir1}/test1
10173         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
10174         check_stats $SINGLEMDS "crossdir_rename" 1
10175
10176         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
10177         check_stats $SINGLEMDS "samedir_rename" 1
10178
10179         rm -rf $DIR/${tdir}
10180 }
10181 run_test 133a "Verifying MDT stats ========================================"
10182
10183 test_133b() {
10184         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10185         remote_ost_nodsh && skip "remote OST with nodsh" && return
10186         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10187         local testdir=$DIR/${tdir}/stats_testdir
10188         mkdir -p ${testdir} || error "mkdir failed"
10189         touch ${testdir}/${tfile} || error "touch failed"
10190         cancel_lru_locks mdc
10191
10192         # clear stats.
10193         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10194         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10195
10196         # extra mdt stats verification.
10197         chmod 444 ${testdir}/${tfile} || error "chmod failed"
10198         check_stats $SINGLEMDS "setattr" 1
10199         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10200         if [ $(lustre_version_code $SINGLEMDS) -ne $(version_code 2.2.0) ]
10201         then            # LU-1740
10202                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
10203                 check_stats $SINGLEMDS "getattr" 1
10204         fi
10205         $LFS df || error "lfs failed"
10206         check_stats $SINGLEMDS "statfs" 1
10207
10208         rm -rf $DIR/${tdir}
10209 }
10210 run_test 133b "Verifying extra MDT stats =================================="
10211
10212 test_133c() {
10213         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10214         remote_ost_nodsh && skip "remote OST with nodsh" && return
10215         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10216         local testdir=$DIR/$tdir/stats_testdir
10217         test_mkdir -p $testdir
10218
10219         # verify obdfilter stats.
10220         $SETSTRIPE -c 1 -i 0 $testdir/$tfile
10221         sync
10222         cancel_lru_locks osc
10223         wait_delete_completed
10224
10225         # clear stats.
10226         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
10227         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
10228
10229         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
10230                 error "dd failed"
10231         sync
10232         cancel_lru_locks osc
10233         check_stats ost1 "write" 1
10234
10235         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
10236         check_stats ost1 "read" 1
10237
10238         > $testdir/$tfile || error "truncate failed"
10239         check_stats ost1 "punch" 1
10240
10241         rm -f $testdir/$tfile || error "file remove failed"
10242         wait_delete_completed
10243         check_stats ost1 "destroy" 1
10244
10245         rm -rf $DIR/$tdir
10246 }
10247 run_test 133c "Verifying OST stats ========================================"
10248
10249 order_2() {
10250         local value=$1
10251         local orig=$value
10252         local order=1
10253
10254         while [ $value -ge 2 ]; do
10255                 order=$((order*2))
10256                 value=$((value/2))
10257         done
10258
10259         if [ $orig -gt $order ]; then
10260                 order=$((order*2))
10261         fi
10262         echo $order
10263 }
10264
10265 size_in_KMGT() {
10266     local value=$1
10267     local size=('K' 'M' 'G' 'T');
10268     local i=0
10269     local size_string=$value
10270
10271     while [ $value -ge 1024 ]; do
10272         if [ $i -gt 3 ]; then
10273             #T is the biggest unit we get here, if that is bigger,
10274             #just return XXXT
10275             size_string=${value}T
10276             break
10277         fi
10278         value=$((value >> 10))
10279         if [ $value -lt 1024 ]; then
10280             size_string=${value}${size[$i]}
10281             break
10282         fi
10283         i=$((i + 1))
10284     done
10285
10286     echo $size_string
10287 }
10288
10289 get_rename_size() {
10290     local size=$1
10291     local context=${2:-.}
10292     local sample=$(do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats |
10293                 grep -A1 $context |
10294                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
10295     echo $sample
10296 }
10297
10298 test_133d() {
10299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10300         remote_ost_nodsh && skip "remote OST with nodsh" && return
10301         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10302         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
10303         { skip "MDS doesn't support rename stats"; return; }
10304
10305         local testdir1=$DIR/${tdir}/stats_testdir1
10306         local testdir2=$DIR/${tdir}/stats_testdir2
10307
10308         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10309
10310         mkdir -p ${testdir1} || error "mkdir failed"
10311         mkdir -p ${testdir2} || error "mkdir failed"
10312
10313         createmany -o $testdir1/test 512 || error "createmany failed"
10314
10315         # check samedir rename size
10316         mv ${testdir1}/test0 ${testdir1}/test_0
10317
10318         local testdir1_size=$(ls -l $DIR/${tdir} |
10319                 awk '/stats_testdir1/ {print $5}')
10320         local testdir2_size=$(ls -l $DIR/${tdir} |
10321                 awk '/stats_testdir2/ {print $5}')
10322
10323         testdir1_size=$(order_2 $testdir1_size)
10324         testdir2_size=$(order_2 $testdir2_size)
10325
10326         testdir1_size=$(size_in_KMGT $testdir1_size)
10327         testdir2_size=$(size_in_KMGT $testdir2_size)
10328
10329         echo "source rename dir size: ${testdir1_size}"
10330         echo "target rename dir size: ${testdir2_size}"
10331
10332         local cmd="do_facet $SINGLEMDS $LCTL get_param mdt.*.rename_stats"
10333         eval $cmd || error "$cmd failed"
10334         local samedir=$($cmd | grep 'same_dir')
10335         local same_sample=$(get_rename_size $testdir1_size)
10336         [ -z "$samedir" ] && error "samedir_rename_size count error"
10337         [[ $same_sample -eq 1 ]] ||
10338                 error "samedir_rename_size error $same_sample"
10339         echo "Check same dir rename stats success"
10340
10341         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
10342
10343         # check crossdir rename size
10344         mv ${testdir1}/test_0 ${testdir2}/test_0
10345
10346         testdir1_size=$(ls -l $DIR/${tdir} |
10347                 awk '/stats_testdir1/ {print $5}')
10348         testdir2_size=$(ls -l $DIR/${tdir} |
10349                 awk '/stats_testdir2/ {print $5}')
10350
10351         testdir1_size=$(order_2 $testdir1_size)
10352         testdir2_size=$(order_2 $testdir2_size)
10353
10354         testdir1_size=$(size_in_KMGT $testdir1_size)
10355         testdir2_size=$(size_in_KMGT $testdir2_size)
10356
10357         echo "source rename dir size: ${testdir1_size}"
10358         echo "target rename dir size: ${testdir2_size}"
10359
10360         eval $cmd || error "$cmd failed"
10361         local crossdir=$($cmd | grep 'crossdir')
10362         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
10363         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
10364         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
10365         [[ $src_sample -eq 1 ]] ||
10366                 error "crossdir_rename_size error $src_sample"
10367         [[ $tgt_sample -eq 1 ]] ||
10368                 error "crossdir_rename_size error $tgt_sample"
10369         echo "Check cross dir rename stats success"
10370         rm -rf $DIR/${tdir}
10371 }
10372 run_test 133d "Verifying rename_stats ========================================"
10373
10374 test_133e() {
10375         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10376         remote_ost_nodsh && skip "remote OST with nodsh" && return
10377         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10378         local testdir=$DIR/${tdir}/stats_testdir
10379         local ctr f0 f1 bs=32768 count=42 sum
10380
10381         mkdir -p ${testdir} || error "mkdir failed"
10382
10383         $SETSTRIPE -c 1 -i 0 ${testdir}/${tfile}
10384
10385         for ctr in {write,read}_bytes; do
10386                 sync
10387                 cancel_lru_locks osc
10388
10389                 do_facet ost1 $LCTL set_param -n \
10390                         "obdfilter.*.exports.clear=clear"
10391
10392                 if [ $ctr = write_bytes ]; then
10393                         f0=/dev/zero
10394                         f1=${testdir}/${tfile}
10395                 else
10396                         f0=${testdir}/${tfile}
10397                         f1=/dev/null
10398                 fi
10399
10400                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
10401                         error "dd failed"
10402                 sync
10403                 cancel_lru_locks osc
10404
10405                 sum=$(do_facet ost1 $LCTL get_param \
10406                         "obdfilter.*.exports.*.stats" |
10407                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
10408                                 $1 == ctr { sum += $7 }
10409                                 END { printf("%0.0f", sum) }')
10410
10411                 if ((sum != bs * count)); then
10412                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
10413                 fi
10414         done
10415
10416         rm -rf $DIR/${tdir}
10417 }
10418 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
10419
10420 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
10421
10422 test_133f() {
10423         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10424         remote_ost_nodsh && skip "remote OST with nodsh" && return
10425         # First without trusting modes.
10426         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10427         echo "proc_dirs='$proc_dirs'"
10428         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10429         find $proc_dirs -exec cat '{}' \; &> /dev/null
10430
10431         # Second verifying readability.
10432         $LCTL get_param -R '*' &> /dev/null || error "proc file read failed"
10433
10434         # eventually, this can also be replaced with "lctl get_param -R",
10435         # but not until that option is always available on the server
10436         local facet
10437         for facet in mds1 ost1; do
10438                 local facet_proc_dirs=$(do_facet $facet \
10439                                         \\\ls -d $proc_regexp 2>/dev/null)
10440                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10441                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10442                 do_facet $facet find $facet_proc_dirs \
10443                         ! -name req_history \
10444                         -exec cat '{}' \\\; &> /dev/null
10445
10446                 do_facet $facet find $facet_proc_dirs \
10447                         ! -name req_history \
10448                         -type f \
10449                         -exec cat '{}' \\\; &> /dev/null ||
10450                                 error "proc file read failed"
10451         done
10452 }
10453 run_test 133f "Check for LBUGs/Oopses/unreadable files in /proc"
10454
10455 test_133g() {
10456         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10457         remote_ost_nodsh && skip "remote OST with nodsh" && return
10458         # Second verifying writability.
10459         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
10460         echo "proc_dirs='$proc_dirs'"
10461         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
10462         find $proc_dirs \
10463                 -ignore_readdir_race \
10464                 -type f \
10465                 -not -name force_lbug \
10466                 -not -name changelog_mask \
10467                 -exec badarea_io '{}' \; ||
10468                 error "find $proc_dirs failed"
10469
10470         local facet
10471         for facet in mds1 ost1; do
10472                 [ $(lustre_version_code $facet) -le $(version_code 2.5.54) ] &&
10473                         skip "Too old lustre on $facet" && continue
10474                 local facet_proc_dirs=$(do_facet $facet \
10475                                         \\\ls -d $proc_regexp 2> /dev/null)
10476                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10477                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10478                 do_facet $facet find $facet_proc_dirs \
10479                         -ignore_readdir_race \
10480                         -type f \
10481                         -not -name force_lbug \
10482                         -not -name changelog_mask \
10483                         -exec badarea_io '{}' \\\; ||
10484                                 error "$facet find $facet_proc_dirs failed"
10485         done
10486
10487         # remount the FS in case writes/reads /proc break the FS
10488         cleanup || error "failed to unmount"
10489         setup || error "failed to setup"
10490         true
10491 }
10492 run_test 133g "Check for Oopses on bad io area writes/reads in /proc"
10493
10494 test_133h() {
10495         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10496         remote_ost_nodsh && skip "remote OST with nodsh" && return
10497         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.9.54) ]] &&
10498                 skip "Need MDS version at least 2.9.54" && return
10499
10500         local facet
10501         for facet in client mds1 ost1; do
10502                 local facet_proc_dirs=$(do_facet $facet \
10503                                         \\\ls -d $proc_regexp 2> /dev/null)
10504                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
10505                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
10506                 # Get the list of files that are missing the terminating newline
10507                 local missing=($(do_facet $facet \
10508                         find ${facet_proc_dirs} -type f \|              \
10509                                 while read F\; do                       \
10510                                         awk -v FS='\v' -v RS='\v\v'     \
10511                                         "'END { if(NR>0 &&              \
10512                                         \\\$NF !~ /.*\\\n\$/)           \
10513                                                 print FILENAME}'"       \
10514                                         '\$F'\;                         \
10515                                 done 2>/dev/null))
10516                 [ ${#missing[*]} -eq 0 ] ||
10517                         error "files do not end with newline: ${missing[*]}"
10518         done
10519 }
10520 run_test 133h "Proc files should end with newlines"
10521
10522 test_134a() {
10523         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10524         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10525                 skip "Need MDS version at least 2.7.54" && return
10526
10527         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10528         cancel_lru_locks mdc
10529
10530         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
10531         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10532         [ $unused -eq 0 ] || error "$unused locks are not cleared"
10533
10534         local nr=1000
10535         createmany -o $DIR/$tdir/f $nr ||
10536                 error "failed to create $nr files in $DIR/$tdir"
10537         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
10538
10539         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
10540         do_facet mds1 $LCTL set_param fail_loc=0x327
10541         do_facet mds1 $LCTL set_param fail_val=500
10542         touch $DIR/$tdir/m
10543
10544         echo "sleep 10 seconds ..."
10545         sleep 10
10546         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
10547
10548         do_facet mds1 $LCTL set_param fail_loc=0
10549         do_facet mds1 $LCTL set_param fail_val=0
10550         [ $lck_cnt -lt $unused ] ||
10551                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
10552
10553         rm $DIR/$tdir/m
10554         unlinkmany $DIR/$tdir/f $nr
10555 }
10556 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
10557
10558 test_134b() {
10559         remote_mds_nodsh && skip "remote MDS with nodsh" && return
10560         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.54) ]] &&
10561                 skip "Need MDS version at least 2.7.54" && return
10562
10563         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
10564         cancel_lru_locks mdc
10565
10566         local low_wm=$(do_facet mds1 $LCTL get_param -n \
10567                         ldlm.lock_reclaim_threshold_mb)
10568         # disable reclaim temporarily
10569         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
10570
10571         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
10572         do_facet mds1 $LCTL set_param fail_loc=0x328
10573         do_facet mds1 $LCTL set_param fail_val=500
10574
10575         $LCTL set_param debug=+trace
10576
10577         local nr=600
10578         createmany -o $DIR/$tdir/f $nr &
10579         local create_pid=$!
10580
10581         echo "Sleep $TIMEOUT seconds ..."
10582         sleep $TIMEOUT
10583         if ! ps -p $create_pid  > /dev/null 2>&1; then
10584                 do_facet mds1 $LCTL set_param fail_loc=0
10585                 do_facet mds1 $LCTL set_param fail_val=0
10586                 do_facet mds1 $LCTL set_param \
10587                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
10588                 error "createmany finished incorrectly!"
10589         fi
10590         do_facet mds1 $LCTL set_param fail_loc=0
10591         do_facet mds1 $LCTL set_param fail_val=0
10592         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
10593         wait $create_pid || return 1
10594
10595         unlinkmany $DIR/$tdir/f $nr
10596 }
10597 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
10598
10599 test_140() { #bug-17379
10600         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10601         test_mkdir $DIR/$tdir
10602         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
10603         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
10604
10605         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
10606         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
10607         local i=0
10608         while i=$((i + 1)); do
10609                 test_mkdir $i
10610                 cd $i || error "Changing to $i"
10611                 ln -s ../stat stat || error "Creating stat symlink"
10612                 # Read the symlink until ELOOP present,
10613                 # not LBUGing the system is considered success,
10614                 # we didn't overrun the stack.
10615                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
10616                 if [ $ret -ne 0 ]; then
10617                         if [ $ret -eq 40 ]; then
10618                                 break  # -ELOOP
10619                         else
10620                                 error "Open stat symlink"
10621                                         return
10622                         fi
10623                 fi
10624         done
10625         i=$((i - 1))
10626         echo "The symlink depth = $i"
10627         [ $i -eq 5 -o $i -eq 7 -o $i -eq 8 -o $i -eq 40 ] ||
10628                                         error "Invalid symlink depth"
10629
10630         # Test recursive symlink
10631         ln -s symlink_self symlink_self
10632         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
10633         echo "open symlink_self returns $ret"
10634         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
10635 }
10636 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
10637
10638 test_150() {
10639         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10640         local TF="$TMP/$tfile"
10641
10642         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10643         cp $TF $DIR/$tfile
10644         cancel_lru_locks $OSC
10645         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
10646         remount_client $MOUNT
10647         df -P $MOUNT
10648         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
10649
10650         $TRUNCATE $TF 6000
10651         $TRUNCATE $DIR/$tfile 6000
10652         cancel_lru_locks $OSC
10653         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
10654
10655         echo "12345" >>$TF
10656         echo "12345" >>$DIR/$tfile
10657         cancel_lru_locks $OSC
10658         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
10659
10660         echo "12345" >>$TF
10661         echo "12345" >>$DIR/$tfile
10662         cancel_lru_locks $OSC
10663         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
10664
10665         rm -f $TF
10666         true
10667 }
10668 run_test 150 "truncate/append tests"
10669
10670 #LU-2902 roc_hit was not able to read all values from lproc
10671 function roc_hit_init() {
10672         local list=$(comma_list $(osts_nodes))
10673         local dir=$DIR/$tdir-check
10674         local file=$dir/$tfile
10675         local BEFORE
10676         local AFTER
10677         local idx
10678
10679         test_mkdir $dir
10680         #use setstripe to do a write to every ost
10681         for i in $(seq 0 $((OSTCOUNT-1))); do
10682                 $SETSTRIPE -c 1 -i $i $dir || error "$SETSTRIPE $file failed"
10683                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
10684                 idx=$(printf %04x $i)
10685                 BEFORE=$(get_osd_param $list *OST*$idx stats |
10686                         awk '$1 == "cache_access" {sum += $7}
10687                                 END { printf("%0.0f", sum) }')
10688
10689                 cancel_lru_locks osc
10690                 cat $file >/dev/null
10691
10692                 AFTER=$(get_osd_param $list *OST*$idx stats |
10693                         awk '$1 == "cache_access" {sum += $7}
10694                                 END { printf("%0.0f", sum) }')
10695
10696                 echo BEFORE:$BEFORE AFTER:$AFTER
10697                 if ! let "AFTER - BEFORE == 4"; then
10698                         rm -rf $dir
10699                         error "roc_hit is not safe to use"
10700                 fi
10701                 rm $file
10702         done
10703
10704         rm -rf $dir
10705 }
10706
10707 function roc_hit() {
10708         local list=$(comma_list $(osts_nodes))
10709         echo $(get_osd_param $list '' stats |
10710                 awk '$1 == "cache_hit" {sum += $7}
10711                         END { printf("%0.0f", sum) }')
10712 }
10713
10714 function set_cache() {
10715         local on=1
10716
10717         if [ "$2" == "off" ]; then
10718                 on=0;
10719         fi
10720         local list=$(comma_list $(osts_nodes))
10721         set_osd_param $list '' $1_cache_enable $on
10722
10723         cancel_lru_locks osc
10724 }
10725
10726 test_151() {
10727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10728         remote_ost_nodsh && skip "remote OST with nodsh" && return
10729
10730         local CPAGES=3
10731         local list=$(comma_list $(osts_nodes))
10732
10733         # check whether obdfilter is cache capable at all
10734         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
10735                 echo "not cache-capable obdfilter"
10736                 return 0
10737         fi
10738
10739         # check cache is enabled on all obdfilters
10740         if get_osd_param $list '' read_cache_enable | grep 0; then
10741                 echo "oss cache is disabled"
10742                 return 0
10743         fi
10744
10745         set_osd_param $list '' writethrough_cache_enable 1
10746
10747         # check write cache is enabled on all obdfilters
10748         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
10749                 echo "oss write cache is NOT enabled"
10750                 return 0
10751         fi
10752
10753         roc_hit_init
10754
10755         #define OBD_FAIL_OBD_NO_LRU  0x609
10756         do_nodes $list $LCTL set_param fail_loc=0x609
10757
10758         # pages should be in the case right after write
10759         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
10760                 error "dd failed"
10761
10762         local BEFORE=$(roc_hit)
10763         cancel_lru_locks osc
10764         cat $DIR/$tfile >/dev/null
10765         local AFTER=$(roc_hit)
10766
10767         do_nodes $list $LCTL set_param fail_loc=0
10768
10769         if ! let "AFTER - BEFORE == CPAGES"; then
10770                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
10771         fi
10772
10773         # the following read invalidates the cache
10774         cancel_lru_locks osc
10775         set_osd_param $list '' read_cache_enable 0
10776         cat $DIR/$tfile >/dev/null
10777
10778         # now data shouldn't be found in the cache
10779         BEFORE=$(roc_hit)
10780         cancel_lru_locks osc
10781         cat $DIR/$tfile >/dev/null
10782         AFTER=$(roc_hit)
10783         if let "AFTER - BEFORE != 0"; then
10784                 error "IN CACHE: before: $BEFORE, after: $AFTER"
10785         fi
10786
10787         set_osd_param $list '' read_cache_enable 1
10788         rm -f $DIR/$tfile
10789 }
10790 run_test 151 "test cache on oss and controls ==============================="
10791
10792 test_152() {
10793         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10794         local TF="$TMP/$tfile"
10795
10796         # simulate ENOMEM during write
10797 #define OBD_FAIL_OST_NOMEM      0x226
10798         lctl set_param fail_loc=0x80000226
10799         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
10800         cp $TF $DIR/$tfile
10801         sync || error "sync failed"
10802         lctl set_param fail_loc=0
10803
10804         # discard client's cache
10805         cancel_lru_locks osc
10806
10807         # simulate ENOMEM during read
10808         lctl set_param fail_loc=0x80000226
10809         cmp $TF $DIR/$tfile || error "cmp failed"
10810         lctl set_param fail_loc=0
10811
10812         rm -f $TF
10813 }
10814 run_test 152 "test read/write with enomem ============================"
10815
10816 test_153() {
10817         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
10818 }
10819 run_test 153 "test if fdatasync does not crash ======================="
10820
10821 dot_lustre_fid_permission_check() {
10822         local fid=$1
10823         local ffid=$MOUNT/.lustre/fid/$fid
10824         local test_dir=$2
10825
10826         echo "stat fid $fid"
10827         stat $ffid > /dev/null || error "stat $ffid failed."
10828         echo "touch fid $fid"
10829         touch $ffid || error "touch $ffid failed."
10830         echo "write to fid $fid"
10831         cat /etc/hosts > $ffid || error "write $ffid failed."
10832         echo "read fid $fid"
10833         diff /etc/hosts $ffid || error "read $ffid failed."
10834         echo "append write to fid $fid"
10835         cat /etc/hosts >> $ffid || error "append write $ffid failed."
10836         echo "rename fid $fid"
10837         mv $ffid $test_dir/$tfile.1 &&
10838                 error "rename $ffid to $tfile.1 should fail."
10839         touch $test_dir/$tfile.1
10840         mv $test_dir/$tfile.1 $ffid &&
10841                 error "rename $tfile.1 to $ffid should fail."
10842         rm -f $test_dir/$tfile.1
10843         echo "truncate fid $fid"
10844         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
10845         if [ $MDSCOUNT -lt 2 ]; then #FIXME when cross-MDT hard link is working
10846                 echo "link fid $fid"
10847                 ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
10848         fi
10849         if [ -n $(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl) ]; then
10850                 echo "setfacl fid $fid"
10851                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
10852                 echo "getfacl fid $fid"
10853                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
10854         fi
10855         echo "unlink fid $fid"
10856         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
10857         echo "mknod fid $fid"
10858         mknod $ffid c 1 3 && error "mknod $ffid should fail."
10859
10860         fid=[0xf00000400:0x1:0x0]
10861         ffid=$MOUNT/.lustre/fid/$fid
10862
10863         echo "stat non-exist fid $fid"
10864         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
10865         echo "write to non-exist fid $fid"
10866         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
10867         echo "link new fid $fid"
10868         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
10869
10870         mkdir -p $test_dir/$tdir
10871         touch $test_dir/$tdir/$tfile
10872         fid=$($LFS path2fid $test_dir/$tdir)
10873         rc=$?
10874         [ $rc -ne 0 ] &&
10875                 error "error: could not get fid for $test_dir/$dir/$tfile."
10876
10877         ffid=$MOUNT/.lustre/fid/$fid
10878
10879         echo "ls $fid"
10880         ls $ffid > /dev/null || error "ls $ffid failed."
10881         echo "touch $fid/$tfile.1"
10882         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
10883
10884         echo "touch $MOUNT/.lustre/fid/$tfile"
10885         touch $MOUNT/.lustre/fid/$tfile && \
10886                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
10887
10888         echo "setxattr to $MOUNT/.lustre/fid"
10889         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
10890
10891         echo "listxattr for $MOUNT/.lustre/fid"
10892         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
10893
10894         echo "delxattr from $MOUNT/.lustre/fid"
10895         setfattr -x trusted.name1 $MOUNT/.lustre/fid
10896
10897         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
10898         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
10899                 error "touch invalid fid should fail."
10900
10901         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
10902         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
10903                 error "touch non-normal fid should fail."
10904
10905         echo "rename $tdir to $MOUNT/.lustre/fid"
10906         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
10907                 error "rename to $MOUNT/.lustre/fid should fail."
10908
10909         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.51) ]
10910         then            # LU-3547
10911                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
10912                 local new_obf_mode=777
10913
10914                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
10915                 chmod $new_obf_mode $DIR/.lustre/fid ||
10916                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
10917
10918                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
10919                 [ $obf_mode -eq $new_obf_mode ] ||
10920                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
10921
10922                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
10923                 chmod $old_obf_mode $DIR/.lustre/fid ||
10924                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
10925         fi
10926
10927         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
10928         fid=$($LFS path2fid $test_dir/$tfile-2)
10929
10930         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.50) ]
10931         then # LU-5424
10932                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
10933                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
10934                         error "create lov data thru .lustre failed"
10935         fi
10936         echo "cp /etc/passwd $test_dir/$tfile-2"
10937         cp /etc/passwd $test_dir/$tfile-2 ||
10938                 error "copy to $test_dir/$tfile-2 failed."
10939         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
10940         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
10941                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
10942
10943         rm -rf $test_dir/tfile.lnk
10944         rm -rf $test_dir/$tfile-2
10945 }
10946
10947 test_154A() {
10948         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10949                 skip "Need MDS version at least 2.4.1" && return
10950
10951         local tf=$DIR/$tfile
10952         touch $tf
10953
10954         local fid=$($LFS path2fid $tf)
10955         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
10956
10957         # check that we get the same pathname back
10958         local found=$($LFS fid2path $MOUNT "$fid")
10959         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
10960         [ "$found" == "$tf" ] ||
10961                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
10962 }
10963 run_test 154A "lfs path2fid and fid2path basic checks"
10964
10965 test_154B() {
10966         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
10967                 skip "Need MDS version at least 2.4.1" && return
10968
10969         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
10970         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
10971         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
10972         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
10973
10974         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
10975         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
10976
10977         # check that we get the same pathname
10978         echo "PFID: $PFID, name: $name"
10979         local FOUND=$($LFS fid2path $MOUNT "$PFID")
10980         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
10981         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
10982                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
10983
10984         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
10985 }
10986 run_test 154B "verify the ll_decode_linkea tool"
10987
10988 test_154a() {
10989         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
10990         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
10991                 { skip "Need MDS version at least 2.2.51"; return 0; }
10992         [ -z "$(which setfacl)" ] && skip "must have setfacl tool" && return
10993         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
10994
10995         cp /etc/hosts $DIR/$tfile
10996
10997         fid=$($LFS path2fid $DIR/$tfile)
10998         rc=$?
10999         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
11000
11001         dot_lustre_fid_permission_check "$fid" $DIR ||
11002                 error "dot lustre permission check $fid failed"
11003
11004         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
11005
11006         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
11007
11008         touch $MOUNT/.lustre/file &&
11009                 error "creation is not allowed under .lustre"
11010
11011         mkdir $MOUNT/.lustre/dir &&
11012                 error "mkdir is not allowed under .lustre"
11013
11014         rm -rf $DIR/$tfile
11015 }
11016 run_test 154a "Open-by-FID"
11017
11018 test_154b() {
11019         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11020         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ]] ||
11021                 { skip "Need MDS version at least 2.2.51"; return 0; }
11022         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11023
11024         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11025
11026         local remote_dir=$DIR/$tdir/remote_dir
11027         local MDTIDX=1
11028         local rc=0
11029
11030         mkdir -p $DIR/$tdir
11031         $LFS mkdir -i $MDTIDX $remote_dir ||
11032                 error "create remote directory failed"
11033
11034         cp /etc/hosts $remote_dir/$tfile
11035
11036         fid=$($LFS path2fid $remote_dir/$tfile)
11037         rc=$?
11038         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
11039
11040         dot_lustre_fid_permission_check "$fid" $remote_dir ||
11041                 error "dot lustre permission check $fid failed"
11042         rm -rf $DIR/$tdir
11043 }
11044 run_test 154b "Open-by-FID for remote directory"
11045
11046 test_154c() {
11047         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.1) ]] &&
11048                 skip "Need MDS version at least 2.4.1" && return
11049
11050         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
11051         local FID1=$($LFS path2fid $DIR/$tfile.1)
11052         local FID2=$($LFS path2fid $DIR/$tfile.2)
11053         local FID3=$($LFS path2fid $DIR/$tfile.3)
11054
11055         local N=1
11056         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
11057                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
11058                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
11059                 local want=FID$N
11060                 [ "$FID" = "${!want}" ] ||
11061                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
11062                 N=$((N + 1))
11063         done
11064
11065         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
11066         do
11067                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
11068                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
11069                 N=$((N + 1))
11070         done
11071 }
11072 run_test 154c "lfs path2fid and fid2path multiple arguments"
11073
11074 test_154d() {
11075         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11076         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.53) ]] &&
11077                 skip "Need MDS version at least 2.5.53" && return
11078
11079         if remote_mds; then
11080                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
11081         else
11082                 nid="0@lo"
11083         fi
11084         local proc_ofile="mdt.*.exports.'$nid'.open_files"
11085         local fd
11086         local cmd
11087
11088         rm -f $DIR/$tfile
11089         touch $DIR/$tfile
11090
11091         local fid=$($LFS path2fid $DIR/$tfile)
11092         # Open the file
11093         fd=$(free_fd)
11094         cmd="exec $fd<$DIR/$tfile"
11095         eval $cmd
11096         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
11097         echo "$fid_list" | grep "$fid"
11098         rc=$?
11099
11100         cmd="exec $fd>/dev/null"
11101         eval $cmd
11102         if [ $rc -ne 0 ]; then
11103                 error "FID $fid not found in open files list $fid_list"
11104         fi
11105 }
11106 run_test 154d "Verify open file fid"
11107
11108 test_154e()
11109 {
11110         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.50) ]] &&
11111                 skip "Need MDS version at least 2.6.50" && return
11112
11113         if ls -a $MOUNT | grep -q '^\.lustre$'; then
11114                 error ".lustre returned by readdir"
11115         fi
11116 }
11117 run_test 154e ".lustre is not returned by readdir"
11118
11119 test_154f() {
11120         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11121         # create parent directory on a single MDT to avoid cross-MDT hardlinks
11122         test_mkdir -p -c1 $DIR/$tdir/d
11123         # test dirs inherit from its stripe
11124         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
11125         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
11126         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
11127         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
11128         touch $DIR/f
11129
11130         # get fid of parents
11131         local FID0=$($LFS path2fid $DIR/$tdir/d)
11132         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
11133         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
11134         local FID3=$($LFS path2fid $DIR)
11135
11136         # check that path2fid --parents returns expected <parent_fid>/name
11137         # 1) test for a directory (single parent)
11138         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
11139         [ "$parent" == "$FID0/foo1" ] ||
11140                 error "expected parent: $FID0/foo1, got: $parent"
11141
11142         # 2) test for a file with nlink > 1 (multiple parents)
11143         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
11144         echo "$parent" | grep -F "$FID1/$tfile" ||
11145                 error "$FID1/$tfile not returned in parent list"
11146         echo "$parent" | grep -F "$FID2/link" ||
11147                 error "$FID2/link not returned in parent list"
11148
11149         # 3) get parent by fid
11150         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
11151         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11152         echo "$parent" | grep -F "$FID1/$tfile" ||
11153                 error "$FID1/$tfile not returned in parent list (by fid)"
11154         echo "$parent" | grep -F "$FID2/link" ||
11155                 error "$FID2/link not returned in parent list (by fid)"
11156
11157         # 4) test for entry in root directory
11158         parent=$($LFS path2fid --parents $DIR/f)
11159         echo "$parent" | grep -F "$FID3/f" ||
11160                 error "$FID3/f not returned in parent list"
11161
11162         # 5) test it on root directory
11163         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
11164                 error "$MOUNT should not have parents"
11165
11166         # enable xattr caching and check that linkea is correctly updated
11167         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11168         save_lustre_params client "llite.*.xattr_cache" > $save
11169         lctl set_param llite.*.xattr_cache 1
11170
11171         # 6.1) linkea update on rename
11172         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
11173
11174         # get parents by fid
11175         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11176         # foo1 should no longer be returned in parent list
11177         echo "$parent" | grep -F "$FID1" &&
11178                 error "$FID1 should no longer be in parent list"
11179         # the new path should appear
11180         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
11181                 error "$FID2/$tfile.moved is not in parent list"
11182
11183         # 6.2) linkea update on unlink
11184         rm -f $DIR/$tdir/d/foo2/link
11185         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
11186         # foo2/link should no longer be returned in parent list
11187         echo "$parent" | grep -F "$FID2/link" &&
11188                 error "$FID2/link should no longer be in parent list"
11189         true
11190
11191         rm -f $DIR/f
11192         restore_lustre_params < $save
11193         rm -f $save
11194 }
11195 run_test 154f "get parent fids by reading link ea"
11196
11197 test_154g()
11198 {
11199         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.92) && \
11200            $(lustre_version_code client) -gt $(version_code 2.6.99) ]] ||
11201                 { skip "Need MDS version at least 2.6.92"; return 0; }
11202         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
11203
11204         mkdir -p $DIR/$tdir
11205         llapi_fid_test -d $DIR/$tdir
11206 }
11207 run_test 154g "various llapi FID tests"
11208
11209 test_155_small_load() {
11210     local temp=$TMP/$tfile
11211     local file=$DIR/$tfile
11212
11213     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
11214         error "dd of=$temp bs=6096 count=1 failed"
11215     cp $temp $file
11216     cancel_lru_locks $OSC
11217     cmp $temp $file || error "$temp $file differ"
11218
11219     $TRUNCATE $temp 6000
11220     $TRUNCATE $file 6000
11221     cmp $temp $file || error "$temp $file differ (truncate1)"
11222
11223     echo "12345" >>$temp
11224     echo "12345" >>$file
11225     cmp $temp $file || error "$temp $file differ (append1)"
11226
11227     echo "12345" >>$temp
11228     echo "12345" >>$file
11229     cmp $temp $file || error "$temp $file differ (append2)"
11230
11231     rm -f $temp $file
11232     true
11233 }
11234
11235 test_155_big_load() {
11236     remote_ost_nodsh && skip "remote OST with nodsh" && return
11237     local temp=$TMP/$tfile
11238     local file=$DIR/$tfile
11239
11240     free_min_max
11241     local cache_size=$(do_facet ost$((MAXI+1)) \
11242         "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
11243     local large_file_size=$((cache_size * 2))
11244
11245     echo "OSS cache size: $cache_size KB"
11246     echo "Large file size: $large_file_size KB"
11247
11248     [ $MAXV -le $large_file_size ] && \
11249         skip_env "max available OST size needs > $large_file_size KB" && \
11250         return 0
11251
11252     $SETSTRIPE $file -c 1 -i $MAXI || error "$SETSTRIPE $file failed"
11253
11254     dd if=/dev/urandom of=$temp bs=$large_file_size count=1k || \
11255         error "dd of=$temp bs=$large_file_size count=1k failed"
11256     cp $temp $file
11257     ls -lh $temp $file
11258     cancel_lru_locks osc
11259     cmp $temp $file || error "$temp $file differ"
11260
11261     rm -f $temp $file
11262     true
11263 }
11264
11265 save_writethrough() {
11266         local facets=$(get_facets OST)
11267
11268         save_lustre_params $facets "obdfilter.*.writethrough_cache_enable" > $1
11269         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" >> $1
11270 }
11271
11272 test_155a() {
11273         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11274         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11275         save_writethrough $p
11276
11277         set_cache read on
11278         set_cache writethrough on
11279         test_155_small_load
11280         restore_lustre_params < $p
11281         rm -f $p
11282 }
11283 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
11284
11285 test_155b() {
11286         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11287         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11288         save_writethrough $p
11289
11290         set_cache read on
11291         set_cache writethrough off
11292         test_155_small_load
11293         restore_lustre_params < $p
11294         rm -f $p
11295 }
11296 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
11297
11298 test_155c() {
11299         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11300         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11301         save_writethrough $p
11302
11303         set_cache read off
11304         set_cache writethrough on
11305         test_155_small_load
11306         restore_lustre_params < $p
11307         rm -f $p
11308 }
11309 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
11310
11311 test_155d() {
11312         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11313         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11314         save_writethrough $p
11315
11316         set_cache read off
11317         set_cache writethrough off
11318         test_155_small_load
11319         restore_lustre_params < $p
11320         rm -f $p
11321 }
11322 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
11323
11324 test_155e() {
11325         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11326         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11327         save_writethrough $p
11328
11329         set_cache read on
11330         set_cache writethrough on
11331         test_155_big_load
11332         restore_lustre_params < $p
11333         rm -f $p
11334 }
11335 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
11336
11337 test_155f() {
11338         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11339         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11340         save_writethrough $p
11341
11342         set_cache read on
11343         set_cache writethrough off
11344         test_155_big_load
11345         restore_lustre_params < $p
11346         rm -f $p
11347 }
11348 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
11349
11350 test_155g() {
11351         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11352         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11353         save_writethrough $p
11354
11355         set_cache read off
11356         set_cache writethrough on
11357         test_155_big_load
11358         restore_lustre_params < $p
11359         rm -f $p
11360 }
11361 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
11362
11363 test_155h() {
11364         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11365         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11366         save_writethrough $p
11367
11368         set_cache read off
11369         set_cache writethrough off
11370         test_155_big_load
11371         restore_lustre_params < $p
11372         rm -f $p
11373 }
11374 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
11375
11376 test_156() {
11377         remote_ost_nodsh && skip "remote OST with nodsh" && return
11378         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11379         local CPAGES=3
11380         local BEFORE
11381         local AFTER
11382         local file="$DIR/$tfile"
11383         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
11384
11385         [ "$(facet_fstype ost1)" = "zfs" -a \
11386            $(lustre_version_code ost1 -lt $(version_code 2.6.93)) ] &&
11387                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS" &&
11388                 return
11389
11390         save_writethrough $p
11391         roc_hit_init
11392
11393         log "Turn on read and write cache"
11394         set_cache read on
11395         set_cache writethrough on
11396
11397         log "Write data and read it back."
11398         log "Read should be satisfied from the cache."
11399         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11400         BEFORE=$(roc_hit)
11401         cancel_lru_locks osc
11402         cat $file >/dev/null
11403         AFTER=$(roc_hit)
11404         if ! let "AFTER - BEFORE == CPAGES"; then
11405                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11406         else
11407                 log "cache hits:: before: $BEFORE, after: $AFTER"
11408         fi
11409
11410         log "Read again; it should be satisfied from the cache."
11411         BEFORE=$AFTER
11412         cancel_lru_locks osc
11413         cat $file >/dev/null
11414         AFTER=$(roc_hit)
11415         if ! let "AFTER - BEFORE == CPAGES"; then
11416                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11417         else
11418                 log "cache hits:: before: $BEFORE, after: $AFTER"
11419         fi
11420
11421         log "Turn off the read cache and turn on the write cache"
11422         set_cache read off
11423         set_cache writethrough on
11424
11425         log "Read again; it should be satisfied from the cache."
11426         BEFORE=$(roc_hit)
11427         cancel_lru_locks osc
11428         cat $file >/dev/null
11429         AFTER=$(roc_hit)
11430         if ! let "AFTER - BEFORE == CPAGES"; then
11431                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11432         else
11433                 log "cache hits:: before: $BEFORE, after: $AFTER"
11434         fi
11435
11436         log "Read again; it should not be satisfied from the cache."
11437         BEFORE=$AFTER
11438         cancel_lru_locks osc
11439         cat $file >/dev/null
11440         AFTER=$(roc_hit)
11441         if ! let "AFTER - BEFORE == 0"; then
11442                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11443         else
11444                 log "cache hits:: before: $BEFORE, after: $AFTER"
11445         fi
11446
11447         log "Write data and read it back."
11448         log "Read should be satisfied from the cache."
11449         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11450         BEFORE=$(roc_hit)
11451         cancel_lru_locks osc
11452         cat $file >/dev/null
11453         AFTER=$(roc_hit)
11454         if ! let "AFTER - BEFORE == CPAGES"; then
11455                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11456         else
11457                 log "cache hits:: before: $BEFORE, after: $AFTER"
11458         fi
11459
11460         log "Read again; it should not be satisfied from the cache."
11461         BEFORE=$AFTER
11462         cancel_lru_locks osc
11463         cat $file >/dev/null
11464         AFTER=$(roc_hit)
11465         if ! let "AFTER - BEFORE == 0"; then
11466                 error "IN CACHE: before: $BEFORE, after: $AFTER"
11467         else
11468                 log "cache hits:: before: $BEFORE, after: $AFTER"
11469         fi
11470
11471         log "Turn off read and write cache"
11472         set_cache read off
11473         set_cache writethrough off
11474
11475         log "Write data and read it back"
11476         log "It should not be satisfied from the cache."
11477         rm -f $file
11478         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11479         cancel_lru_locks osc
11480         BEFORE=$(roc_hit)
11481         cat $file >/dev/null
11482         AFTER=$(roc_hit)
11483         if ! let "AFTER - BEFORE == 0"; then
11484                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11485         else
11486                 log "cache hits:: before: $BEFORE, after: $AFTER"
11487         fi
11488
11489         log "Turn on the read cache and turn off the write cache"
11490         set_cache read on
11491         set_cache writethrough off
11492
11493         log "Write data and read it back"
11494         log "It should not be satisfied from the cache."
11495         rm -f $file
11496         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
11497         BEFORE=$(roc_hit)
11498         cancel_lru_locks osc
11499         cat $file >/dev/null
11500         AFTER=$(roc_hit)
11501         if ! let "AFTER - BEFORE == 0"; then
11502                 error_ignore bz20762 "IN CACHE: before: $BEFORE, after: $AFTER"
11503         else
11504                 log "cache hits:: before: $BEFORE, after: $AFTER"
11505         fi
11506
11507         log "Read again; it should be satisfied from the cache."
11508         BEFORE=$(roc_hit)
11509         cancel_lru_locks osc
11510         cat $file >/dev/null
11511         AFTER=$(roc_hit)
11512         if ! let "AFTER - BEFORE == CPAGES"; then
11513                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
11514         else
11515                 log "cache hits:: before: $BEFORE, after: $AFTER"
11516         fi
11517
11518         restore_lustre_params < $p
11519         rm -f $p $file
11520 }
11521 run_test 156 "Verification of tunables"
11522
11523 #Changelogs
11524 cleanup_changelog () {
11525         trap 0
11526         echo "Deregistering changelog client $CL_USER"
11527         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11528 }
11529
11530 err17935 () {
11531         if [[ $MDSCOUNT -gt 1 ]]; then
11532                 error_ignore bz17935 $*
11533         else
11534                 error $*
11535         fi
11536 }
11537
11538 changelog_chmask()
11539 {
11540         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11541
11542         MASK=$(do_facet $SINGLEMDS $LCTL get_param $CL_MASK_PARAM| grep -c "$1")
11543
11544         if [ $MASK -eq 1 ]; then
11545                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="-$1"
11546         else
11547                 do_facet $SINGLEMDS $LCTL set_param $CL_MASK_PARAM="+$1"
11548         fi
11549 }
11550
11551 changelog_extract_field() {
11552         local mdt=$1
11553         local cltype=$2
11554         local file=$3
11555         local identifier=$4
11556
11557         $LFS changelog $mdt | gawk "/$cltype.*$file$/ {
11558                 print gensub(/^.* "$identifier'(\[[^\]]*\]).*$/,"\\1",1)}' |
11559                 tail -1
11560 }
11561
11562 test_160a() {
11563         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11564         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11565         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11566                 { skip "Need MDS version at least 2.2.0"; return; }
11567
11568         local CL_USERS="mdd.$MDT0.changelog_users"
11569         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11570         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11571                 changelog_register -n)
11572         echo "Registered as changelog user $CL_USER"
11573         trap cleanup_changelog EXIT
11574         $GET_CL_USERS | grep -q $CL_USER ||
11575                 error "User $CL_USER not found in changelog_users"
11576
11577         # change something
11578         test_mkdir -p $DIR/$tdir/pics/2008/zachy
11579         touch $DIR/$tdir/pics/2008/zachy/timestamp
11580         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
11581         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
11582         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
11583         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
11584         rm $DIR/$tdir/pics/desktop.jpg
11585
11586         $LFS changelog $MDT0 | tail -5
11587
11588         echo "verifying changelog mask"
11589         changelog_chmask "MKDIR"
11590         changelog_chmask "CLOSE"
11591
11592         test_mkdir -p $DIR/$tdir/pics/zach/sofia
11593         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11594
11595         changelog_chmask "MKDIR"
11596         changelog_chmask "CLOSE"
11597
11598         test_mkdir -p $DIR/$tdir/pics/2008/sofia
11599         echo "zzzzzz" > $DIR/$tdir/pics/zach/file
11600
11601         $LFS changelog $MDT0
11602         MKDIRS=$($LFS changelog $MDT0 | tail -5 | grep -c "MKDIR")
11603         CLOSES=$($LFS changelog $MDT0 | tail -5 | grep -c "CLOSE")
11604         [ $MKDIRS -eq 1 ] || err17935 "MKDIR changelog mask count $DIRS != 1"
11605         [ $CLOSES -eq 1 ] || err17935 "CLOSE changelog mask count $DIRS != 1"
11606
11607         # verify contents
11608         echo "verifying target fid"
11609         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "t=")
11610         fidf=$($LFS path2fid $DIR/$tdir/pics/zach/timestamp)
11611         [ "$fidc" == "$fidf" ] ||
11612                 err17935 "fid in changelog $fidc != file fid $fidf"
11613         echo "verifying parent fid"
11614         fidc=$(changelog_extract_field $MDT0 "CREAT" "timestamp" "p=")
11615         fidf=$($LFS path2fid $DIR/$tdir/pics/zach)
11616         [ "$fidc" == "$fidf" ] ||
11617                 err17935 "pfid in changelog $fidc != dir fid $fidf"
11618
11619         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11620         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 5))
11621         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11622         echo "verifying user clear: $(( $USER_REC1 + 5 )) == $USER_REC2"
11623         [ $USER_REC2 == $(($USER_REC1 + 5)) ] ||
11624                 err17935 "user index expected $(($USER_REC1 + 5)) is $USER_REC2"
11625
11626         MIN_REC=$($GET_CL_USERS |
11627                 awk 'min == "" || $2 < min {min = $2}; END {print min}')
11628         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11629         echo "verifying min purge: $(( $MIN_REC + 1 )) == $FIRST_REC"
11630         [ $FIRST_REC == $(($MIN_REC + 1)) ] ||
11631                 err17935 "first index should be $(($MIN_REC + 1)) is $FIRST_REC"
11632
11633         # LU-3446 changelog index reset on MDT restart
11634         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
11635         CUR_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11636         $LFS changelog_clear $MDT0 $CL_USER 0
11637         stop $SINGLEMDS || error "Fail to stop MDT."
11638         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
11639         CUR_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11640         echo "verifying index survives MDT restart: $CUR_REC1 == $CUR_REC2"
11641         [ $CUR_REC1 == $CUR_REC2 ] ||
11642                 err17935 "current index should be $CUR_REC1 is $CUR_REC2"
11643
11644         echo "verifying user deregister"
11645         cleanup_changelog
11646         $GET_CL_USERS | grep -q $CL_USER &&
11647                 error "User $CL_USER still in changelog_users"
11648
11649         USERS=$(( $($GET_CL_USERS | wc -l) - 2 ))
11650         if [ $CL_USER -eq 0 ]; then
11651                 LAST_REC1=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11652                 touch $DIR/$tdir/chloe
11653                 LAST_REC2=$($GET_CL_USERS | head -n1 | cut -f3 -d' ')
11654                 echo "verify changelogs are off: $LAST_REC1 == $LAST_REC2"
11655                 [ $LAST_REC1 == $LAST_REC2 ] || error "changelogs not off"
11656         else
11657                 echo "$CL_USER other changelog users; can't verify off"
11658         fi
11659 }
11660 run_test 160a "changelog sanity"
11661
11662 test_160b() { # LU-3587
11663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11664         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11665         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.0) ] ||
11666                 { skip "Need MDS version at least 2.2.0"; return; }
11667
11668         local CL_USERS="mdd.$MDT0.changelog_users"
11669         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11670         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11671                 changelog_register -n)
11672         echo "Registered as changelog user $CL_USER"
11673         trap cleanup_changelog EXIT
11674         $GET_CL_USERS | grep -q $CL_USER ||
11675                 error "User $CL_USER not found in changelog_users"
11676
11677         local LONGNAME1=$(str_repeat a 255)
11678         local LONGNAME2=$(str_repeat b 255)
11679
11680         cd $DIR
11681         echo "creating very long named file"
11682         touch $LONGNAME1 || error "create of $LONGNAME1 failed"
11683         echo "moving very long named file"
11684         mv $LONGNAME1 $LONGNAME2
11685
11686         $LFS changelog $MDT0 | grep RENME
11687         rm -f $LONGNAME2
11688         cleanup_changelog
11689 }
11690 run_test 160b "Verify that very long rename doesn't crash in changelog"
11691
11692 test_160c() {
11693         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11694
11695         local rc=0
11696         local server_version=$(lustre_version_code $SINGLEMDS)
11697
11698         [[ $server_version -gt $(version_code 2.5.57) ]] ||
11699                 [[ $server_version -gt $(version_code 2.5.1) &&
11700                    $server_version -lt $(version_code 2.5.50) ]] ||
11701                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
11702         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11703
11704         # Registration step
11705         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11706                 changelog_register -n)
11707         trap cleanup_changelog EXIT
11708
11709         rm -rf $DIR/$tdir
11710         mkdir -p $DIR/$tdir
11711         $MCREATE $DIR/$tdir/foo_160c
11712         changelog_chmask "TRUNC"
11713         $TRUNCATE $DIR/$tdir/foo_160c 200
11714         changelog_chmask "TRUNC"
11715         $TRUNCATE $DIR/$tdir/foo_160c 199
11716         $LFS changelog $MDT0
11717         TRUNCS=$($LFS changelog $MDT0 | tail -5 | grep -c "TRUNC")
11718         [ $TRUNCS -eq 1 ] || err17935 "TRUNC changelog mask count $TRUNCS != 1"
11719         $LFS changelog_clear $MDT0 $CL_USER 0
11720
11721         # Deregistration step
11722         cleanup_changelog
11723 }
11724 run_test 160c "verify that changelog log catch the truncate event"
11725
11726 test_160d() {
11727         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11728         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
11729
11730         local server_version=$(lustre_version_code mds1)
11731         local CL_MASK_PARAM="mdd.$MDT0.changelog_mask"
11732
11733         [[ $server_version -ge $(version_code 2.7.60) ]] ||
11734                 { skip "Need MDS version at least 2.7.60+"; return; }
11735         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11736
11737         # Registration step
11738         CL_USER=$(do_facet mds1 $LCTL --device $MDT0 \
11739                 changelog_register -n)
11740
11741         trap cleanup_changelog EXIT
11742         mkdir -p $DIR/$tdir/migrate_dir
11743         $LFS changelog_clear $MDT0 $CL_USER 0
11744
11745         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
11746         $LFS changelog $MDT0
11747         MIGRATES=$($LFS changelog $MDT0 | tail -5 | grep -c "MIGRT")
11748         $LFS changelog_clear $MDT0 $CL_USER 0
11749         [ $MIGRATES -eq 1 ] ||
11750                 error "MIGRATE changelog mask count $MIGRATES != 1"
11751
11752         # Deregistration step
11753         cleanup_changelog
11754 }
11755 run_test 160d "verify that changelog log catch the migrate event"
11756
11757 test_160e() {
11758         remote_mds_nodsh && skip "remote MDS with nodsh" && return
11759
11760         # Create a user
11761         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11762                 changelog_register -n)
11763         echo "Registered as changelog user $CL_USER"
11764         trap cleanup_changelog EXIT
11765
11766         # Delete a future user (expect fail)
11767         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister cl77
11768         local rc=$?
11769
11770         if [ $rc -eq 0 ]; then
11771                 error "Deleted non-existant user cl77"
11772         elif [ $rc -ne 2 ]; then
11773                 error "changelog_deregister failed with $rc, " \
11774                         "expected 2 (ENOENT)"
11775         fi
11776
11777         # Clear to a bad index (1 billion should be safe)
11778         $LFS changelog_clear $MDT0 $CL_USER 1000000000
11779         rc=$?
11780
11781         if [ $rc -eq 0 ]; then
11782                 error "Successfully cleared to invalid CL index"
11783         elif [ $rc -ne 22 ]; then
11784                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
11785         fi
11786 }
11787 run_test 160e "changelog negative testing"
11788
11789 cleanup_160f() {
11790         trap 0
11791         do_facet $SINGLEMDS $LCTL set_param fail_loc=0 fail_val=0
11792         echo "Deregistering changelog client $CL_USER"
11793         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER
11794         echo "Deregistering changelog client $CL_USER2"
11795         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $CL_USER2
11796         restore_lustre_params < $save_params
11797         rm -f $save_params
11798 }
11799
11800 test_160f() {
11801         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11802         # should be set by default
11803
11804         local CL_USERS="mdd.$MDT0.changelog_users"
11805         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11806         local save_params="$TMP/sanity-$TESTNAME.parameters"
11807
11808         save_lustre_params $SINGLEMDS \
11809                 "mdd.$MDT0.changelog_max_idle_time" > $save_params
11810         save_lustre_params $SINGLEMDS \
11811                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11812         save_lustre_params $SINGLEMDS \
11813                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11814
11815         trap cleanup_160f EXIT
11816
11817         # Create a user
11818         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11819                 changelog_register -n)
11820         echo "Registered as changelog user $CL_USER"
11821         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11822                 changelog_register -n)
11823         echo "Registered as changelog user $CL_USER2"
11824         $GET_CL_USERS | grep -q $CL_USER ||
11825                 error "User $CL_USER not found in changelog_users"
11826         $GET_CL_USERS | grep -q $CL_USER2 ||
11827                 error "User $CL_USER2 not found in changelog_users"
11828
11829         # generate some changelogs to accumulate
11830         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11831         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11832         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11833         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11834
11835         # check changelogs have been generated
11836         nbcl=$($LFS changelog $MDT0 | wc -l)
11837         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11838
11839         do_facet $SINGLEMDS $LCTL set_param \
11840                 mdd.$MDT0.changelog_max_idle_time=10
11841         do_facet $SINGLEMDS $LCTL set_param \
11842                 mdd.$MDT0.changelog_min_gc_interval=2
11843         do_facet $SINGLEMDS $LCTL set_param \
11844                 mdd.$MDT0.changelog_min_free_cat_entries=3
11845
11846         # simulate changelog catalog almost full
11847 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11848         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11849         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11850
11851         sleep 6
11852         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11853         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 2))
11854         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11855         echo "verifying user clear: $(( $USER_REC1 + 2 )) == $USER_REC2"
11856         [ $USER_REC2 == $(($USER_REC1 + 2)) ] ||
11857                 error "user index expected $(($USER_REC1 + 2)) is $USER_REC2"
11858         sleep 5
11859
11860         # generate one more changelog to trigger fail_loc
11861         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11862
11863         # ensure gc thread is done
11864         wait_update_facet $SINGLEMDS \
11865                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11866
11867         # check user still registered
11868         $GET_CL_USERS | grep -q $CL_USER ||
11869                 error "User $CL_USER not found in changelog_users"
11870         # check user2 unregistered
11871         $GET_CL_USERS | grep -q $CL_USER2 &&
11872                 error "User $CL_USER2 still found in changelog_users"
11873
11874         # check changelogs are present and starting at $USER_REC2 + 1
11875         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11876         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11877         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11878                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11879
11880         cleanup_160f
11881 }
11882 run_test 160f "changelog garbage collect (timestamped users)"
11883
11884 test_160g() {
11885         # do_facet $SINGLEMDS $LCTL set_param mdd.$MDT0.changelog_gc=1
11886         # should be set by default
11887
11888         local CL_USERS="mdd.$MDT0.changelog_users"
11889         local GET_CL_USERS="do_facet $SINGLEMDS $LCTL get_param -n $CL_USERS"
11890         local save_params="$TMP/sanity-$TESTNAME.parameters"
11891
11892         save_lustre_params $SINGLEMDS \
11893                 "mdd.$MDT0.changelog_max_idle_indexes" > $save_params
11894         save_lustre_params $SINGLEMDS \
11895                 "mdd.$MDT0.changelog_min_gc_interval" >> $save_params
11896         save_lustre_params $SINGLEMDS \
11897                 "mdd.$MDT0.changelog_min_free_cat_entries" >> $save_params
11898
11899         trap cleanup_160f EXIT
11900
11901 #define OBD_FAIL_TIME_IN_CHLOG_USER                 0x1314
11902         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1314
11903
11904         # Create a user
11905         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11906                 changelog_register -n)
11907         echo "Registered as changelog user $CL_USER"
11908         CL_USER2=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
11909                 changelog_register -n)
11910         echo "Registered as changelog user $CL_USER2"
11911         $GET_CL_USERS | grep -q $CL_USER ||
11912                 error "User $CL_USER not found in changelog_users"
11913         $GET_CL_USERS | grep -q $CL_USER2 ||
11914                 error "User $CL_USER2 not found in changelog_users"
11915
11916         # generate some changelogs to accumulate
11917         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
11918         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
11919         touch $DIR/$tdir/${tfile}2 || error "touch $DIR/$tdir/${tfile}2 failed"
11920         rm -f $DIR/$tdir/$tfile || error "rm -f $tfile failed"
11921
11922         # check changelogs have been generated
11923         nbcl=$($LFS changelog $MDT0 | wc -l)
11924         [[ $nbcl -eq 0 ]] && error "no changelogs found"
11925
11926         do_facet $SINGLEMDS $LCTL set_param \
11927                 mdd.$MDT0.changelog_max_idle_indexes=$((nbcl - 1))
11928         do_facet $SINGLEMDS $LCTL set_param \
11929                 mdd.$MDT0.changelog_min_gc_interval=2
11930         do_facet $SINGLEMDS $LCTL set_param \
11931                 mdd.$MDT0.changelog_min_free_cat_entries=3
11932
11933         # simulate changelog catalog almost full
11934 #define OBD_FAIL_CAT_FREE_RECORDS                  0x1313
11935         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1313
11936         do_facet $SINGLEMDS $LCTL set_param fail_val=3
11937
11938         USER_REC1=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11939         $LFS changelog_clear $MDT0 $CL_USER $(($USER_REC1 + 3))
11940         USER_REC2=$($GET_CL_USERS | awk "\$1 == \"$CL_USER\" {print \$2}")
11941         echo "verifying user clear: $(( $USER_REC1 + 3 )) == $USER_REC2"
11942         [ $USER_REC2 == $(($USER_REC1 + 3)) ] ||
11943                 error "user index expected $(($USER_REC1 + 3)) is $USER_REC2"
11944
11945         # generate one more changelog to trigger fail_loc
11946         rm -rf $DIR/$tdir || error "rm -rf $tdir failed"
11947
11948         # ensure gc thread is done
11949         wait_update_facet $SINGLEMDS \
11950                           "ps -e -o comm= | grep chlg_gc_thread" "" 20
11951
11952         # check user still registered
11953         $GET_CL_USERS | grep -q $CL_USER ||
11954                 error "User $CL_USER not found in changelog_users"
11955         # check user2 unregistered
11956         $GET_CL_USERS | grep -q $CL_USER2 &&
11957                 error "User $CL_USER2 still found in changelog_users"
11958
11959         # check changelogs are present and starting at $USER_REC2 + 1
11960         FIRST_REC=$($LFS changelog $MDT0 | head -n1 | awk '{print $1}')
11961         echo "verifying min purge: $(( $USER_REC2 + 1 )) == $FIRST_REC"
11962         [ $FIRST_REC == $(($USER_REC2 + 1)) ] ||
11963                 error "first index should be $(($USER_REC2 + 1)) is $FIRST_REC"
11964
11965         cleanup_160f
11966 }
11967 run_test 160g "changelog garbage collect (old users)"
11968
11969 test_161a() {
11970         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
11971         test_mkdir -c1 $DIR/$tdir
11972         cp /etc/hosts $DIR/$tdir/$tfile
11973         test_mkdir -c1 $DIR/$tdir/foo1
11974         test_mkdir -c1 $DIR/$tdir/foo2
11975         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
11976         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
11977         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
11978         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
11979         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
11980         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
11981                 $LFS fid2path $DIR $FID
11982                 err17935 "bad link ea"
11983         fi
11984     # middle
11985     rm $DIR/$tdir/foo2/zachary
11986     # last
11987     rm $DIR/$tdir/foo2/thor
11988     # first
11989     rm $DIR/$tdir/$tfile
11990     # rename
11991     mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
11992     if [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ]
11993         then
11994         $LFS fid2path $DIR $FID
11995         err17935 "bad link rename"
11996     fi
11997     rm $DIR/$tdir/foo2/maggie
11998
11999         # overflow the EA
12000         local longname=filename_avg_len_is_thirty_two_
12001         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
12002                 error "failed to hardlink many files"
12003         links=$($LFS fid2path $DIR $FID | wc -l)
12004         echo -n "${links}/1000 links in link EA"
12005         [[ $links -gt 60 ]] ||
12006                 err17935 "expected at least 60 links in link EA"
12007         unlinkmany $DIR/$tdir/foo2/$longname 1000 ||
12008                 error "failed to unlink many hardlinks"
12009 }
12010 run_test 161a "link ea sanity"
12011
12012 test_161b() {
12013         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12014         [ $MDSCOUNT -lt 2 ] && skip "skipping remote directory test" && return
12015         local MDTIDX=1
12016         local remote_dir=$DIR/$tdir/remote_dir
12017
12018         mkdir -p $DIR/$tdir
12019         $LFS mkdir -i $MDTIDX $remote_dir ||
12020                 error "create remote directory failed"
12021
12022         cp /etc/hosts $remote_dir/$tfile
12023         mkdir -p $remote_dir/foo1
12024         mkdir -p $remote_dir/foo2
12025         ln $remote_dir/$tfile $remote_dir/foo1/sofia
12026         ln $remote_dir/$tfile $remote_dir/foo2/zachary
12027         ln $remote_dir/$tfile $remote_dir/foo1/luna
12028         ln $remote_dir/$tfile $remote_dir/foo2/thor
12029
12030         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
12031                      tr -d ']')
12032         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
12033                 $LFS fid2path $DIR $FID
12034                 err17935 "bad link ea"
12035         fi
12036         # middle
12037         rm $remote_dir/foo2/zachary
12038         # last
12039         rm $remote_dir/foo2/thor
12040         # first
12041         rm $remote_dir/$tfile
12042         # rename
12043         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
12044         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
12045         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
12046                 $LFS fid2path $DIR $FID
12047                 err17935 "bad link rename"
12048         fi
12049         rm $remote_dir/foo2/maggie
12050
12051         # overflow the EA
12052         local longname=filename_avg_len_is_thirty_two_
12053         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
12054                 error "failed to hardlink many files"
12055         links=$($LFS fid2path $DIR $FID | wc -l)
12056         echo -n "${links}/1000 links in link EA"
12057         [[ ${links} -gt 60 ]] ||
12058                 err17935 "expected at least 60 links in link EA"
12059         unlinkmany $remote_dir/foo2/$longname 1000 ||
12060         error "failed to unlink many hardlinks"
12061 }
12062 run_test 161b "link ea sanity under remote directory"
12063
12064 test_161c() {
12065         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12066         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12067         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.1.5) ]] &&
12068                 skip "Need MDS version at least 2.1.5" && return
12069
12070         # define CLF_RENAME_LAST 0x0001
12071         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
12072         CL_USER=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
12073                 changelog_register -n)
12074
12075         trap cleanup_changelog EXIT
12076         rm -rf $DIR/$tdir
12077         mkdir -p $DIR/$tdir
12078         touch $DIR/$tdir/foo_161c
12079         touch $DIR/$tdir/bar_161c
12080         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12081         $LFS changelog $MDT0 | grep RENME
12082         local flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | \
12083                 cut -f5 -d' ')
12084         $LFS changelog_clear $MDT0 $CL_USER 0
12085         if [ x$flags != "x0x1" ]; then
12086                 error "flag $flags is not 0x1"
12087         fi
12088         echo "rename overwrite a target having nlink = 1," \
12089                 "changelog record has flags of $flags"
12090
12091         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
12092         touch $DIR/$tdir/foo_161c
12093         touch $DIR/$tdir/bar_161c
12094         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12095         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
12096         $LFS changelog $MDT0 | grep RENME
12097         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
12098         $LFS changelog_clear $MDT0 $CL_USER 0
12099         if [ x$flags != "x0x0" ]; then
12100                 error "flag $flags is not 0x0"
12101         fi
12102         echo "rename overwrite a target having nlink > 1," \
12103                 "changelog record has flags of $flags"
12104
12105         # rename doesn't overwrite a target (changelog flag 0x0)
12106         touch $DIR/$tdir/foo_161c
12107         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
12108         $LFS changelog $MDT0 | grep RENME
12109         flags=$($LFS changelog $MDT0 | grep RENME | tail -1 | cut -f5 -d' ')
12110         $LFS changelog_clear $MDT0 $CL_USER 0
12111         if [ x$flags != "x0x0" ]; then
12112                 error "flag $flags is not 0x0"
12113         fi
12114         echo "rename doesn't overwrite a target," \
12115                 "changelog record has flags of $flags"
12116
12117         # define CLF_UNLINK_LAST 0x0001
12118         # unlink a file having nlink = 1 (changelog flag 0x1)
12119         rm -f $DIR/$tdir/foo2_161c
12120         $LFS changelog $MDT0 | grep UNLNK
12121         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
12122         $LFS changelog_clear $MDT0 $CL_USER 0
12123         if [ x$flags != "x0x1" ]; then
12124                 error "flag $flags is not 0x1"
12125         fi
12126         echo "unlink a file having nlink = 1," \
12127                 "changelog record has flags of $flags"
12128
12129         # unlink a file having nlink > 1 (changelog flag 0x0)
12130         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
12131         rm -f $DIR/$tdir/foobar_161c
12132         $LFS changelog $MDT0 | grep UNLNK
12133         flags=$($LFS changelog $MDT0 | grep UNLNK | tail -1 | cut -f5 -d' ')
12134         $LFS changelog_clear $MDT0 $CL_USER 0
12135         if [ x$flags != "x0x0" ]; then
12136                 error "flag $flags is not 0x0"
12137         fi
12138         echo "unlink a file having nlink > 1," \
12139                 "changelog record has flags of $flags"
12140         cleanup_changelog
12141 }
12142 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
12143
12144 test_161d() {
12145         local user
12146         local pid
12147         local fid
12148
12149         # cleanup previous run
12150         rm -rf $DIR/$tdir/$tfile
12151
12152         user=$(do_facet $SINGLEMDS $LCTL --device $MDT0 \
12153                 changelog_register -n)
12154         [[ $? -eq 0 ]] || error "changelog_register failed"
12155
12156         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
12157         # interfer with $MOUNT/.lustre/fid/ access
12158         mkdir $DIR/$tdir
12159         [[ $? -eq 0 ]] || error "mkdir failed"
12160
12161         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
12162         $LCTL set_param fail_loc=0x8000140c
12163         # 5s pause
12164         $LCTL set_param fail_val=5
12165
12166         # create file
12167         echo foofoo > $DIR/$tdir/$tfile &
12168         pid=$!
12169
12170         # wait for create to be delayed
12171         sleep 2
12172
12173         ps -p $pid
12174         [[ $? -eq 0 ]] || error "create should be blocked"
12175
12176         local tempfile=$(mktemp)
12177         fid=$(changelog_extract_field $MDT0 "CREAT" "$tfile" "t=")
12178         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
12179         # some delay may occur during ChangeLog publishing and file read just
12180         # above, that could allow file write to happen finally
12181         [[ -s $tempfile ]] && echo "file should be empty"
12182
12183         $LCTL set_param fail_loc=0
12184
12185         wait $pid
12186         [[ $? -eq 0 ]] || error "create failed"
12187
12188         $LFS changelog_clear $MDT0 $user 0
12189         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister $user
12190 }
12191 run_test 161d "create with concurrent .lustre/fid access"
12192
12193 check_path() {
12194     local expected=$1
12195     shift
12196     local fid=$2
12197
12198     local path=$(${LFS} fid2path $*)
12199     # Remove the '//' indicating a remote directory
12200     path=$(echo $path | sed 's#//#/#g')
12201     RC=$?
12202
12203     if [ $RC -ne 0 ]; then
12204         err17935 "path looked up of $expected failed. Error $RC"
12205         return $RC
12206     elif [ "${path}" != "${expected}" ]; then
12207         err17935 "path looked up \"${path}\" instead of \"${expected}\""
12208         return 2
12209     fi
12210     echo "fid $fid resolves to path $path (expected $expected)"
12211 }
12212
12213 test_162a() { # was test_162
12214         # Make changes to filesystem
12215         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12216         test_mkdir -p -c1 $DIR/$tdir/d2
12217         touch $DIR/$tdir/d2/$tfile
12218         touch $DIR/$tdir/d2/x1
12219         touch $DIR/$tdir/d2/x2
12220         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
12221         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
12222         # regular file
12223         FID=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
12224         check_path "$tdir/d2/$tfile" $FSNAME $FID --link 0 ||
12225                 error "check path $tdir/d2/$tfile failed"
12226
12227         # softlink
12228         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
12229         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
12230         check_path "$tdir/d2/p/q/r/slink" $FSNAME $FID --link 0 ||
12231                 error "check path $tdir/d2/p/q/r/slink failed"
12232
12233         # softlink to wrong file
12234         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
12235         FID=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
12236         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME $FID --link 0 ||
12237                 error "check path $tdir/d2/p/q/r/slink.wrong failed"
12238
12239         # hardlink
12240         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
12241         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
12242         FID=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
12243         # fid2path dir/fsname should both work
12244         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 1 ||
12245                 error "check path $tdir/d2/a/b/c/new_file failed"
12246         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR $FID --link 0 ||
12247                 error "check path $DIR/$tdir/d2/p/q/r/hlink failed"
12248
12249         # hardlink count: check that there are 2 links
12250         # Doesnt work with CMD yet: 17935
12251         ${LFS} fid2path $DIR $FID | wc -l | grep -q 2 || \
12252                 err17935 "expected 2 links"
12253
12254         # hardlink indexing: remove the first link
12255         rm $DIR/$tdir/d2/p/q/r/hlink
12256         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $FID --link 0 ||
12257                 error "check path $DIR/$tdir/d2/a/b/c/new_file failed"
12258
12259         return 0
12260 }
12261 run_test 162a "path lookup sanity"
12262
12263 test_162b() {
12264         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12265         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
12266
12267         mkdir $DIR/$tdir
12268         $LFS setdirstripe -i0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
12269                                 error "create striped dir failed"
12270
12271         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
12272                                         tail -n 1 | awk '{print $2}')
12273         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
12274
12275         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
12276         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
12277
12278         # regular file
12279         for ((i=0;i<5;i++)); do
12280                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
12281                         error "get fid for f$i failed"
12282                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0 ||
12283                         error "check path $tdir/striped_dir/f$i failed"
12284
12285                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
12286                         error "get fid for d$i failed"
12287                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0 ||
12288                         error "check path $tdir/striped_dir/d$i failed"
12289         done
12290
12291         return 0
12292 }
12293 run_test 162b "striped directory path lookup sanity"
12294
12295 # LU-4239: Verify fid2path works with paths 100 or more directories deep
12296 test_162c() {
12297         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.51) ]] &&
12298                 skip "Need MDS version at least 2.7.51" && return
12299         test_mkdir $DIR/$tdir.local
12300         test_mkdir $DIR/$tdir.remote
12301         local lpath=$tdir.local
12302         local rpath=$tdir.remote
12303
12304         for ((i = 0; i <= 101; i++)); do
12305                 lpath="$lpath/$i"
12306                 mkdir $DIR/$lpath
12307                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
12308                         error "get fid for local directory $DIR/$lpath failed"
12309                 check_path "$DIR/$lpath" $MOUNT $FID --link 0 ||
12310                         error "check path for local directory $DIR/$lpath failed"
12311
12312                 rpath="$rpath/$i"
12313                 test_mkdir $DIR/$rpath
12314                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
12315                         error "get fid for remote directory $DIR/$rpath failed"
12316                 check_path "$DIR/$rpath" $MOUNT $FID --link 0 ||
12317                         error "check path for remote directory $DIR/$rpath failed"
12318         done
12319
12320         return 0
12321 }
12322 run_test 162c "fid2path works with paths 100 or more directories deep"
12323
12324 test_169() {
12325         # do directio so as not to populate the page cache
12326         log "creating a 10 Mb file"
12327         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
12328         log "starting reads"
12329         dd if=$DIR/$tfile of=/dev/null bs=4096 &
12330         log "truncating the file"
12331         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
12332         log "killing dd"
12333         kill %+ || true # reads might have finished
12334         echo "wait until dd is finished"
12335         wait
12336         log "removing the temporary file"
12337         rm -rf $DIR/$tfile || error "tmp file removal failed"
12338 }
12339 run_test 169 "parallel read and truncate should not deadlock"
12340
12341 test_170() {
12342         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12343
12344         $LCTL clear     # bug 18514
12345         $LCTL debug_daemon start $TMP/${tfile}_log_good
12346         touch $DIR/$tfile
12347         $LCTL debug_daemon stop
12348         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
12349                 error "sed failed to read log_good"
12350
12351         $LCTL debug_daemon start $TMP/${tfile}_log_good
12352         rm -rf $DIR/$tfile
12353         $LCTL debug_daemon stop
12354
12355         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
12356                error "lctl df log_bad failed"
12357
12358         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12359         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12360
12361         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
12362         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
12363
12364         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
12365                 error "bad_line good_line1 good_line2 are empty"
12366
12367         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12368         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
12369         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
12370
12371         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
12372         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
12373         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
12374
12375         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
12376                 error "bad_line_new good_line_new are empty"
12377
12378         local expected_good=$((good_line1 + good_line2*2))
12379
12380         rm -f $TMP/${tfile}*
12381         # LU-231, short malformed line may not be counted into bad lines
12382         if [ $bad_line -ne $bad_line_new ] &&
12383                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
12384                 error "expected $bad_line bad lines, but got $bad_line_new"
12385                 return 1
12386         fi
12387
12388         if [ $expected_good -ne $good_line_new ]; then
12389                 error "expected $expected_good good lines, but got $good_line_new"
12390                 return 2
12391         fi
12392         true
12393 }
12394 run_test 170 "test lctl df to handle corrupted log ====================="
12395
12396 test_171() { # bug20592
12397         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12398 #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
12399         $LCTL set_param fail_loc=0x50e
12400         $LCTL set_param fail_val=3000
12401         multiop_bg_pause $DIR/$tfile O_s || true
12402         local MULTIPID=$!
12403         kill -USR1 $MULTIPID
12404         # cause log dump
12405         sleep 3
12406         wait $MULTIPID
12407         if dmesg | grep "recursive fault"; then
12408                 error "caught a recursive fault"
12409         fi
12410         $LCTL set_param fail_loc=0
12411         true
12412 }
12413 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
12414
12415 # it would be good to share it with obdfilter-survey/iokit-libecho code
12416 setup_obdecho_osc () {
12417         local rc=0
12418         local ost_nid=$1
12419         local obdfilter_name=$2
12420         echo "Creating new osc for $obdfilter_name on $ost_nid"
12421         # make sure we can find loopback nid
12422         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
12423
12424         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
12425                            ${obdfilter_name}_osc_UUID || rc=2; }
12426         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
12427                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
12428         return $rc
12429 }
12430
12431 cleanup_obdecho_osc () {
12432         local obdfilter_name=$1
12433         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
12434         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
12435         return 0
12436 }
12437
12438 obdecho_test() {
12439         local OBD=$1
12440         local node=$2
12441         local pages=${3:-64}
12442         local rc=0
12443         local id
12444
12445         local count=10
12446         local obd_size=$(get_obd_size $node $OBD)
12447         local page_size=$(get_page_size $node)
12448         if [[ -n "$obd_size" ]]; then
12449                 local new_count=$((obd_size / (pages * page_size / 1024)))
12450                 [[ $new_count -ge $count ]] || count=$new_count
12451         fi
12452
12453         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
12454         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
12455                            rc=2; }
12456         if [ $rc -eq 0 ]; then
12457             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
12458             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
12459         fi
12460         echo "New object id is $id"
12461         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
12462                            rc=4; }
12463         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
12464                            "test_brw $count w v $pages $id" || rc=4; }
12465         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
12466                            rc=4; }
12467         [ $rc -eq 0 -o $rc -gt 2 ] && { do_facet $node "$LCTL --device ec "    \
12468                                         "cleanup" || rc=5; }
12469         [ $rc -eq 0 -o $rc -gt 1 ] && { do_facet $node "$LCTL --device ec "    \
12470                                         "detach" || rc=6; }
12471         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
12472         return $rc
12473 }
12474
12475 test_180a() {
12476         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12477         remote_ost_nodsh && skip "remote OST with nodsh" && return
12478         local rc=0
12479         local rmmod_local=0
12480
12481         if ! module_loaded obdecho; then
12482             load_module obdecho/obdecho
12483             rmmod_local=1
12484         fi
12485
12486         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
12487         local host=$(lctl get_param -n osc.$osc.import |
12488                              awk '/current_connection:/ {print $2}' )
12489         local target=$(lctl get_param -n osc.$osc.import |
12490                              awk '/target:/ {print $2}' )
12491         target=${target%_UUID}
12492
12493         [[ -n $target ]]  && { setup_obdecho_osc $host $target || rc=1; } || rc=1
12494         [ $rc -eq 0 ] && { obdecho_test ${target}_osc client || rc=2; }
12495         [[ -n $target ]] && cleanup_obdecho_osc $target
12496         [ $rmmod_local -eq 1 ] && rmmod obdecho
12497         return $rc
12498 }
12499 run_test 180a "test obdecho on osc"
12500
12501 test_180b() {
12502         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12503         remote_ost_nodsh && skip "remote OST with nodsh" && return
12504         local rc=0
12505         local rmmod_remote=0
12506
12507         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12508                 rmmod_remote=true || error "failed to load module obdecho"
12509         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ {print $4;exit}')
12510         [[ -n $target ]] && { obdecho_test $target ost1 || rc=1; }
12511         $rmmod_remote && do_facet ost1 "rmmod obdecho"
12512         return $rc
12513 }
12514 run_test 180b "test obdecho directly on obdfilter"
12515
12516 test_180c() { # LU-2598
12517         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12518         remote_ost_nodsh && skip "remote OST with nodsh" && return
12519         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.0) ]] &&
12520                 skip "Need MDS version at least 2.4.0" && return
12521
12522         local rc=0
12523         local rmmod_remote=false
12524         local pages=16384 # 64MB bulk I/O RPC size
12525         local target
12526
12527         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
12528                 rmmod_remote=true || error "failed to load module obdecho"
12529
12530         target=$(do_facet ost1 $LCTL dl | awk '/obdfilter/ { print $4 }' |
12531                 head -n1)
12532         if [ -n "$target" ]; then
12533                 obdecho_test "$target" ost1 "$pages" || rc=${PIPESTATUS[0]}
12534         else
12535                 echo "there is no obdfilter target on ost1"
12536                 rc=2
12537         fi
12538         $rmmod_remote && do_facet ost1 "rmmod obdecho" || true
12539         return $rc
12540 }
12541 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
12542
12543 test_181() { # bug 22177
12544         test_mkdir $DIR/$tdir
12545         # create enough files to index the directory
12546         createmany -o $DIR/$tdir/foobar 4000
12547         # print attributes for debug purpose
12548         lsattr -d .
12549         # open dir
12550         multiop_bg_pause $DIR/$tdir D_Sc || return 1
12551         MULTIPID=$!
12552         # remove the files & current working dir
12553         unlinkmany $DIR/$tdir/foobar 4000
12554         rmdir $DIR/$tdir
12555         kill -USR1 $MULTIPID
12556         wait $MULTIPID
12557         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
12558         return 0
12559 }
12560 run_test 181 "Test open-unlinked dir ========================"
12561
12562 test_182() {
12563         local fcount=1000
12564         local tcount=10
12565
12566         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12567
12568         $LCTL set_param mdc.*.rpc_stats=clear
12569
12570         for (( i = 0; i < $tcount; i++ )) ; do
12571                 mkdir $DIR/$tdir/$i
12572         done
12573
12574         for (( i = 0; i < $tcount; i++ )) ; do
12575                 createmany -o $DIR/$tdir/$i/f- $fcount &
12576         done
12577         wait
12578
12579         for (( i = 0; i < $tcount; i++ )) ; do
12580                 unlinkmany $DIR/$tdir/$i/f- $fcount &
12581         done
12582         wait
12583
12584         $LCTL get_param mdc.*.rpc_stats
12585
12586         rm -rf $DIR/$tdir
12587 }
12588 run_test 182 "Test parallel modify metadata operations ================"
12589
12590 test_183() { # LU-2275
12591         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12592         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.56) ]] &&
12593                 skip "Need MDS version at least 2.3.56" && return
12594
12595         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12596         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12597         echo aaa > $DIR/$tdir/$tfile
12598
12599 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
12600         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
12601
12602         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
12603         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
12604
12605         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
12606
12607         # Flush negative dentry cache
12608         touch $DIR/$tdir/$tfile
12609
12610         # We are not checking for any leaked references here, they'll
12611         # become evident next time we do cleanup with module unload.
12612         rm -rf $DIR/$tdir
12613 }
12614 run_test 183 "No crash or request leak in case of strange dispositions ========"
12615
12616 # test suite 184 is for LU-2016, LU-2017
12617 test_184a() {
12618         check_swap_layouts_support && return 0
12619
12620         dir0=$DIR/$tdir/$testnum
12621         test_mkdir -p -c1 $dir0
12622         ref1=/etc/passwd
12623         ref2=/etc/group
12624         file1=$dir0/f1
12625         file2=$dir0/f2
12626         $SETSTRIPE -c1 $file1
12627         cp $ref1 $file1
12628         $SETSTRIPE -c2 $file2
12629         cp $ref2 $file2
12630         gen1=$($GETSTRIPE -g $file1)
12631         gen2=$($GETSTRIPE -g $file2)
12632
12633         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
12634         gen=$($GETSTRIPE -g $file1)
12635         [[ $gen1 != $gen ]] ||
12636                 "Layout generation on $file1 does not change"
12637         gen=$($GETSTRIPE -g $file2)
12638         [[ $gen2 != $gen ]] ||
12639                 "Layout generation on $file2 does not change"
12640
12641         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
12642         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
12643
12644         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
12645 }
12646 run_test 184a "Basic layout swap"
12647
12648 test_184b() {
12649         check_swap_layouts_support && return 0
12650
12651         dir0=$DIR/$tdir/$testnum
12652         mkdir -p $dir0 || error "creating dir $dir0"
12653         file1=$dir0/f1
12654         file2=$dir0/f2
12655         file3=$dir0/f3
12656         dir1=$dir0/d1
12657         dir2=$dir0/d2
12658         mkdir $dir1 $dir2
12659         $SETSTRIPE -c1 $file1
12660         $SETSTRIPE -c2 $file2
12661         $SETSTRIPE -c1 $file3
12662         chown $RUNAS_ID $file3
12663         gen1=$($GETSTRIPE -g $file1)
12664         gen2=$($GETSTRIPE -g $file2)
12665
12666         $LFS swap_layouts $dir1 $dir2 &&
12667                 error "swap of directories layouts should fail"
12668         $LFS swap_layouts $dir1 $file1 &&
12669                 error "swap of directory and file layouts should fail"
12670         $RUNAS $LFS swap_layouts $file1 $file2 &&
12671                 error "swap of file we cannot write should fail"
12672         $LFS swap_layouts $file1 $file3 &&
12673                 error "swap of file with different owner should fail"
12674         /bin/true # to clear error code
12675 }
12676 run_test 184b "Forbidden layout swap (will generate errors)"
12677
12678 test_184c() {
12679         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
12680         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n" && return
12681         check_swap_layouts_support && return 0
12682
12683         local dir0=$DIR/$tdir/$testnum
12684         mkdir -p $dir0 || error "creating dir $dir0"
12685
12686         local ref1=$dir0/ref1
12687         local ref2=$dir0/ref2
12688         local file1=$dir0/file1
12689         local file2=$dir0/file2
12690         # create a file large enough for the concurrent test
12691         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
12692         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
12693         echo "ref file size: ref1($(stat -c %s $ref1))," \
12694              "ref2($(stat -c %s $ref2))"
12695
12696         cp $ref2 $file2
12697         dd if=$ref1 of=$file1 bs=16k &
12698         local DD_PID=$!
12699
12700         # Make sure dd starts to copy file
12701         while [ ! -f $file1 ]; do sleep 0.1; done
12702
12703         $LFS swap_layouts $file1 $file2
12704         local rc=$?
12705         wait $DD_PID
12706         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
12707         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
12708
12709         # how many bytes copied before swapping layout
12710         local copied=$(stat -c %s $file2)
12711         local remaining=$(stat -c %s $ref1)
12712         remaining=$((remaining - copied))
12713         echo "Copied $copied bytes before swapping layout..."
12714
12715         cmp -n $copied $file1 $ref2 | grep differ &&
12716                 error "Content mismatch [0, $copied) of ref2 and file1"
12717         cmp -n $copied $file2 $ref1 ||
12718                 error "Content mismatch [0, $copied) of ref1 and file2"
12719         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
12720                 error "Content mismatch [$copied, EOF) of ref1 and file1"
12721
12722         # clean up
12723         rm -f $ref1 $ref2 $file1 $file2
12724 }
12725 run_test 184c "Concurrent write and layout swap"
12726
12727 test_184d() {
12728         check_swap_layouts_support && return 0
12729         [ -z "$(which getfattr 2>/dev/null)" ] &&
12730                 skip "no getfattr command" && return 0
12731
12732         local file1=$DIR/$tdir/$tfile-1
12733         local file2=$DIR/$tdir/$tfile-2
12734         local file3=$DIR/$tdir/$tfile-3
12735         local lovea1
12736         local lovea2
12737
12738         mkdir -p $DIR/$tdir
12739         touch $file1 || error "create $file1 failed"
12740         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12741                 error "create $file2 failed"
12742         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12743                 error "create $file3 failed"
12744         lovea1=$(get_layout_param $file1)
12745
12746         $LFS swap_layouts $file2 $file3 ||
12747                 error "swap $file2 $file3 layouts failed"
12748         $LFS swap_layouts $file1 $file2 ||
12749                 error "swap $file1 $file2 layouts failed"
12750
12751         lovea2=$(get_layout_param $file2)
12752         echo "$lovea1"
12753         echo "$lovea2"
12754         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
12755
12756         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12757         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
12758 }
12759 run_test 184d "allow stripeless layouts swap"
12760
12761 test_184e() {
12762         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.94) ]] ||
12763                 { skip "Need MDS version at least 2.6.94"; return 0; }
12764         check_swap_layouts_support && return 0
12765         [ -z "$(which getfattr 2>/dev/null)" ] &&
12766                 skip "no getfattr command" && return 0
12767
12768         local file1=$DIR/$tdir/$tfile-1
12769         local file2=$DIR/$tdir/$tfile-2
12770         local file3=$DIR/$tdir/$tfile-3
12771         local lovea
12772
12773         mkdir -p $DIR/$tdir
12774         touch $file1 || error "create $file1 failed"
12775         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
12776                 error "create $file2 failed"
12777         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
12778                 error "create $file3 failed"
12779
12780         $LFS swap_layouts $file1 $file2 ||
12781                 error "swap $file1 $file2 layouts failed"
12782
12783         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
12784         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
12785
12786         echo 123 > $file1 || error "Should be able to write into $file1"
12787
12788         $LFS swap_layouts $file1 $file3 ||
12789                 error "swap $file1 $file3 layouts failed"
12790
12791         echo 123 > $file1 || error "Should be able to write into $file1"
12792
12793         rm -rf $file1 $file2 $file3
12794 }
12795 run_test 184e "Recreate layout after stripeless layout swaps"
12796
12797 test_185() { # LU-2441
12798         # LU-3553 - no volatile file support in old servers
12799         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.60) ]] ||
12800                 { skip "Need MDS version at least 2.3.60"; return 0; }
12801
12802         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
12803         touch $DIR/$tdir/spoo
12804         local mtime1=$(stat -c "%Y" $DIR/$tdir)
12805         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
12806                 error "cannot create/write a volatile file"
12807         [ "$FILESET" == "" ] &&
12808         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
12809                 error "FID is still valid after close"
12810
12811         multiop_bg_pause $DIR/$tdir vVw4096_c
12812         local multi_pid=$!
12813
12814         local OLD_IFS=$IFS
12815         IFS=":"
12816         local fidv=($fid)
12817         IFS=$OLD_IFS
12818         # assume that the next FID for this client is sequential, since stdout
12819         # is unfortunately eaten by multiop_bg_pause
12820         local n=$((${fidv[1]} + 1))
12821         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
12822         if [ "$FILESET" == "" ]; then
12823                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
12824                         error "FID is missing before close"
12825         fi
12826         kill -USR1 $multi_pid
12827         # 1 second delay, so if mtime change we will see it
12828         sleep 1
12829         local mtime2=$(stat -c "%Y" $DIR/$tdir)
12830         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
12831 }
12832 run_test 185 "Volatile file support"
12833
12834 test_187a() {
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         local file=$dir0/file1
12843         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
12844         local dv1=$($LFS data_version $file)
12845         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
12846         local dv2=$($LFS data_version $file)
12847         [[ $dv1 != $dv2 ]] ||
12848                 error "data version did not change on write $dv1 == $dv2"
12849
12850         # clean up
12851         rm -f $file1
12852 }
12853 run_test 187a "Test data version change"
12854
12855 test_187b() {
12856         remote_mds_nodsh && skip "remote MDS with nodsh" && return
12857         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.3.0) ] &&
12858                 skip "Need MDS version at least 2.3.0" && return
12859
12860         local dir0=$DIR/$tdir/$testnum
12861         mkdir -p $dir0 || error "creating dir $dir0"
12862
12863         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
12864         [[ ${DV[0]} != ${DV[1]} ]] ||
12865                 error "data version did not change on write"\
12866                       " ${DV[0]} == ${DV[1]}"
12867
12868         # clean up
12869         rm -f $file1
12870 }
12871 run_test 187b "Test data version change on volatile file"
12872
12873 test_200() {
12874         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
12875         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
12876         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
12877
12878         local POOL=${POOL:-cea1}
12879         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
12880         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
12881         # Pool OST targets
12882         local first_ost=0
12883         local last_ost=$(($OSTCOUNT - 1))
12884         local ost_step=2
12885         local ost_list=$(seq $first_ost $ost_step $last_ost)
12886         local ost_range="$first_ost $last_ost $ost_step"
12887         local test_path=$POOL_ROOT/$POOL_DIR_NAME
12888         local file_dir=$POOL_ROOT/file_tst
12889         local subdir=$test_path/subdir
12890         local rc=0
12891
12892         if ! combined_mgs_mds ; then
12893                 mount_mgs_client
12894         fi
12895
12896         while : ; do
12897                 # former test_200a test_200b
12898                 pool_add $POOL                          || { rc=$? ; break; }
12899                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
12900                 # former test_200c test_200d
12901                 mkdir -p $test_path
12902                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
12903                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
12904                 mkdir -p $subdir
12905                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
12906                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
12907                                                         || { rc=$? ; break; }
12908                 # former test_200e test_200f
12909                 local files=$((OSTCOUNT*3))
12910                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
12911                                                         || { rc=$? ; break; }
12912                 pool_create_files $POOL $file_dir $files "$ost_list" \
12913                                                         || { rc=$? ; break; }
12914                 # former test_200g test_200h
12915                 pool_lfs_df $POOL                       || { rc=$? ; break; }
12916                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
12917
12918                 # former test_201a test_201b test_201c
12919                 pool_remove_first_target $POOL          || { rc=$? ; break; }
12920
12921                 local f=$test_path/$tfile
12922                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
12923                 pool_remove $POOL $f                    || { rc=$? ; break; }
12924                 break
12925         done
12926
12927         destroy_test_pools
12928
12929         if ! combined_mgs_mds ; then
12930                 umount_mgs_client
12931         fi
12932         return $rc
12933 }
12934 run_test 200 "OST pools"
12935
12936 # usage: default_attr <count | size | offset>
12937 default_attr() {
12938         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
12939 }
12940
12941 # usage: check_default_stripe_attr
12942 check_default_stripe_attr() {
12943         ACTUAL=$($GETSTRIPE $* $DIR/$tdir)
12944         case $1 in
12945         --stripe-count|-c)
12946                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
12947         --stripe-size|-S)
12948                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
12949         --stripe-index|-i)
12950                 EXPECTED=-1;;
12951         *)
12952                 error "unknown getstripe attr '$1'"
12953         esac
12954
12955         [ $ACTUAL == $EXPECTED ] ||
12956                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
12957 }
12958
12959 test_204a() {
12960         test_mkdir $DIR/$tdir
12961         $SETSTRIPE --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
12962
12963         check_default_stripe_attr --stripe-count
12964         check_default_stripe_attr --stripe-size
12965         check_default_stripe_attr --stripe-index
12966 }
12967 run_test 204a "Print default stripe attributes"
12968
12969 test_204b() {
12970         test_mkdir $DIR/$tdir
12971         $SETSTRIPE --stripe-count 1 $DIR/$tdir
12972
12973         check_default_stripe_attr --stripe-size
12974         check_default_stripe_attr --stripe-index
12975 }
12976 run_test 204b "Print default stripe size and offset"
12977
12978 test_204c() {
12979         test_mkdir $DIR/$tdir
12980         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
12981
12982         check_default_stripe_attr --stripe-count
12983         check_default_stripe_attr --stripe-index
12984 }
12985 run_test 204c "Print default stripe count and offset"
12986
12987 test_204d() {
12988         test_mkdir $DIR/$tdir
12989         $SETSTRIPE --stripe-index 0 $DIR/$tdir
12990
12991         check_default_stripe_attr --stripe-count
12992         check_default_stripe_attr --stripe-size
12993 }
12994 run_test 204d "Print default stripe count and size"
12995
12996 test_204e() {
12997         test_mkdir $DIR/$tdir
12998         $SETSTRIPE -d $DIR/$tdir
12999
13000         check_default_stripe_attr --stripe-count --raw
13001         check_default_stripe_attr --stripe-size --raw
13002         check_default_stripe_attr --stripe-index --raw
13003 }
13004 run_test 204e "Print raw stripe attributes"
13005
13006 test_204f() {
13007         test_mkdir $DIR/$tdir
13008         $SETSTRIPE --stripe-count 1 $DIR/$tdir
13009
13010         check_default_stripe_attr --stripe-size --raw
13011         check_default_stripe_attr --stripe-index --raw
13012 }
13013 run_test 204f "Print raw stripe size and offset"
13014
13015 test_204g() {
13016         test_mkdir $DIR/$tdir
13017         $SETSTRIPE --stripe-size 65536 $DIR/$tdir
13018
13019         check_default_stripe_attr --stripe-count --raw
13020         check_default_stripe_attr --stripe-index --raw
13021 }
13022 run_test 204g "Print raw stripe count and offset"
13023
13024 test_204h() {
13025         test_mkdir $DIR/$tdir
13026         $SETSTRIPE --stripe-index 0 $DIR/$tdir
13027
13028         check_default_stripe_attr --stripe-count --raw
13029         check_default_stripe_attr --stripe-size --raw
13030 }
13031 run_test 204h "Print raw stripe count and size"
13032
13033 # Figure out which job scheduler is being used, if any,
13034 # or use a fake one
13035 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
13036         JOBENV=SLURM_JOB_ID
13037 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
13038         JOBENV=LSB_JOBID
13039 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
13040         JOBENV=PBS_JOBID
13041 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
13042         JOBENV=LOADL_STEP_ID
13043 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
13044         JOBENV=JOB_ID
13045 else
13046         $LCTL list_param jobid_name > /dev/null 2>&1
13047         if [ $? -eq 0 ]; then
13048                 JOBENV=nodelocal
13049         else
13050                 JOBENV=FAKE_JOBID
13051         fi
13052 fi
13053
13054 verify_jobstats() {
13055         local cmd=($1)
13056         shift
13057         local facets="$@"
13058
13059 # we don't really need to clear the stats for this test to work, since each
13060 # command has a unique jobid, but it makes debugging easier if needed.
13061 #       for facet in $facets; do
13062 #               local dev=$(convert_facet2label $facet)
13063 #               # clear old jobstats
13064 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
13065 #       done
13066
13067         # use a new JobID for each test, or we might see an old one
13068         [ "$JOBENV" = "FAKE_JOBID" ] &&
13069                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13070
13071         JOBVAL=${!JOBENV}
13072
13073         [ "$JOBENV" = "nodelocal" ] && {
13074                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
13075                 $LCTL set_param jobid_name=$FAKE_JOBID
13076                 JOBVAL=$FAKE_JOBID
13077         }
13078
13079         log "Test: ${cmd[*]}"
13080         log "Using JobID environment variable $JOBENV=$JOBVAL"
13081
13082         if [ $JOBENV = "FAKE_JOBID" ]; then
13083                 FAKE_JOBID=$JOBVAL ${cmd[*]}
13084         else
13085                 ${cmd[*]}
13086         fi
13087
13088         # all files are created on OST0000
13089         for facet in $facets; do
13090                 local stats="*.$(convert_facet2label $facet).job_stats"
13091                 if [ $(do_facet $facet lctl get_param $stats |
13092                        grep -c $JOBVAL) -ne 1 ]; then
13093                         do_facet $facet lctl get_param $stats
13094                         error "No jobstats for $JOBVAL found on $facet::$stats"
13095                 fi
13096         done
13097 }
13098
13099 jobstats_set() {
13100         trap 0
13101         NEW_JOBENV=${1:-$OLD_JOBENV}
13102         do_facet mgs $LCTL conf_param $FSNAME.sys.jobid_var=$NEW_JOBENV
13103         wait_update $HOSTNAME "$LCTL get_param -n jobid_var" $NEW_JOBENV
13104 }
13105
13106 cleanup_205() {
13107         trap 0
13108         do_facet $SINGLEMDS \
13109                 $LCTL set_param mdt.*.job_cleanup_interval=$OLD_INTERVAL
13110         [ $OLD_JOBENV != $JOBENV ] && jobstats_set $OLD_JOBENV
13111         cleanup_changelog
13112 }
13113
13114 test_205() { # Job stats
13115         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.7.1) ]] ||
13116                 { skip "Need MDS version with at least 2.7.1"; return 0; }
13117
13118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13119         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13120         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13121         remote_ost_nodsh && skip "remote OST with nodsh" && return
13122
13123         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
13124                 skip "Server doesn't support jobstats" && return 0
13125         [[ $JOBID_VAR = disable ]] && skip "jobstats is disabled" && return
13126
13127         OLD_JOBENV=$($LCTL get_param -n jobid_var)
13128         if [ $OLD_JOBENV != $JOBENV ]; then
13129                 jobstats_set $JOBENV
13130                 trap cleanup_205 EXIT
13131         fi
13132
13133         CL_USER=$(do_facet $SINGLEMDS lctl --device $MDT0 changelog_register -n)
13134         echo "Registered as changelog user $CL_USER"
13135
13136         OLD_INTERVAL=$(do_facet $SINGLEMDS \
13137                        lctl get_param -n mdt.*.job_cleanup_interval)
13138         local interval_new=5
13139         do_facet $SINGLEMDS \
13140                 $LCTL set_param mdt.*.job_cleanup_interval=$interval_new
13141         local start=$SECONDS
13142
13143         local cmd
13144         # mkdir
13145         cmd="mkdir $DIR/$tdir"
13146         verify_jobstats "$cmd" "$SINGLEMDS"
13147         # rmdir
13148         cmd="rmdir $DIR/$tdir"
13149         verify_jobstats "$cmd" "$SINGLEMDS"
13150         # mkdir on secondary MDT
13151         if [ $MDSCOUNT -gt 1 ]; then
13152                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
13153                 verify_jobstats "$cmd" "mds2"
13154         fi
13155         # mknod
13156         cmd="mknod $DIR/$tfile c 1 3"
13157         verify_jobstats "$cmd" "$SINGLEMDS"
13158         # unlink
13159         cmd="rm -f $DIR/$tfile"
13160         verify_jobstats "$cmd" "$SINGLEMDS"
13161         # create all files on OST0000 so verify_jobstats can find OST stats
13162         # open & close
13163         cmd="$SETSTRIPE -i 0 -c 1 $DIR/$tfile"
13164         verify_jobstats "$cmd" "$SINGLEMDS"
13165         # setattr
13166         cmd="touch $DIR/$tfile"
13167         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13168         # write
13169         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
13170         verify_jobstats "$cmd" "ost1"
13171         # read
13172         cancel_lru_locks osc
13173         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
13174         verify_jobstats "$cmd" "ost1"
13175         # truncate
13176         cmd="$TRUNCATE $DIR/$tfile 0"
13177         verify_jobstats "$cmd" "$SINGLEMDS ost1"
13178         # rename
13179         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
13180         verify_jobstats "$cmd" "$SINGLEMDS"
13181         # jobstats expiry - sleep until old stats should be expired
13182         local left=$((interval_new + 5 - (SECONDS - start)))
13183         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
13184                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
13185                         "0" $left
13186         cmd="mkdir $DIR/$tdir.expire"
13187         verify_jobstats "$cmd" "$SINGLEMDS"
13188         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
13189             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
13190
13191         # Ensure that jobid are present in changelog (if supported by MDS)
13192         if [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.6.52) ]
13193         then
13194                 $LFS changelog $MDT0 | tail -9
13195                 jobids=$($LFS changelog $MDT0 | tail -9 | grep -c "j=")
13196                 [ $jobids -eq 9 ] ||
13197                         error "Wrong changelog jobid count $jobids != 9"
13198
13199                 # LU-5862
13200                 JOBENV="disable"
13201                 jobstats_set $JOBENV
13202                 touch $DIR/$tfile
13203                 $LFS changelog $MDT0 | tail -1
13204                 jobids=$($LFS changelog $MDT0 | tail -1 | grep -c "j=")
13205                 [ $jobids -eq 0 ] ||
13206                         error "Unexpected jobids when jobid_var=$JOBENV"
13207         fi
13208
13209         cleanup_205
13210 }
13211 run_test 205 "Verify job stats"
13212
13213 # LU-1480, LU-1773 and LU-1657
13214 test_206() {
13215         mkdir -p $DIR/$tdir
13216         $SETSTRIPE -c -1 $DIR/$tdir
13217 #define OBD_FAIL_LOV_INIT 0x1403
13218         $LCTL set_param fail_loc=0xa0001403
13219         $LCTL set_param fail_val=1
13220         touch $DIR/$tdir/$tfile || true
13221 }
13222 run_test 206 "fail lov_init_raid0() doesn't lbug"
13223
13224 test_207a() {
13225         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13226         local fsz=`stat -c %s $DIR/$tfile`
13227         cancel_lru_locks mdc
13228
13229         # do not return layout in getattr intent
13230 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
13231         $LCTL set_param fail_loc=0x170
13232         local sz=`stat -c %s $DIR/$tfile`
13233
13234         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
13235
13236         rm -rf $DIR/$tfile
13237 }
13238 run_test 207a "can refresh layout at glimpse"
13239
13240 test_207b() {
13241         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
13242         local cksum=`md5sum $DIR/$tfile`
13243         local fsz=`stat -c %s $DIR/$tfile`
13244         cancel_lru_locks mdc
13245         cancel_lru_locks osc
13246
13247         # do not return layout in getattr intent
13248 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
13249         $LCTL set_param fail_loc=0x171
13250
13251         # it will refresh layout after the file is opened but before read issues
13252         echo checksum is "$cksum"
13253         echo "$cksum" |md5sum -c --quiet || error "file differs"
13254
13255         rm -rf $DIR/$tfile
13256 }
13257 run_test 207b "can refresh layout at open"
13258
13259 test_208() {
13260         # FIXME: in this test suite, only RD lease is used. This is okay
13261         # for now as only exclusive open is supported. After generic lease
13262         # is done, this test suite should be revised. - Jinshan
13263
13264         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13265         [[ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.4.52) ]] ||
13266                 { skip "Need MDS version at least 2.4.52"; return 0; }
13267
13268         echo "==== test 1: verify get lease work"
13269         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
13270
13271         echo "==== test 2: verify lease can be broken by upcoming open"
13272         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13273         local PID=$!
13274         sleep 1
13275
13276         $MULTIOP $DIR/$tfile oO_RDONLY:c
13277         kill -USR1 $PID && wait $PID || error "break lease error"
13278
13279         echo "==== test 3: verify lease can't be granted if an open already exists"
13280         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
13281         local PID=$!
13282         sleep 1
13283
13284         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
13285         kill -USR1 $PID && wait $PID || error "open file error"
13286
13287         echo "==== test 4: lease can sustain over recovery"
13288         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
13289         PID=$!
13290         sleep 1
13291
13292         fail mds1
13293
13294         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
13295
13296         echo "==== test 5: lease broken can't be regained by replay"
13297         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
13298         PID=$!
13299         sleep 1
13300
13301         # open file to break lease and then recovery
13302         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
13303         fail mds1
13304
13305         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
13306
13307         rm -f $DIR/$tfile
13308 }
13309 run_test 208 "Exclusive open"
13310
13311 test_209() {
13312         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
13313                 skip_env "must have disp_stripe" && return
13314
13315         touch $DIR/$tfile
13316         sync; sleep 5; sync;
13317
13318         echo 3 > /proc/sys/vm/drop_caches
13319         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13320
13321         # open/close 500 times
13322         for i in $(seq 500); do
13323                 cat $DIR/$tfile
13324         done
13325
13326         echo 3 > /proc/sys/vm/drop_caches
13327         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
13328
13329         echo "before: $req_before, after: $req_after"
13330         [ $((req_after - req_before)) -ge 300 ] &&
13331                 error "open/close requests are not freed"
13332         return 0
13333 }
13334 run_test 209 "read-only open/close requests should be freed promptly"
13335
13336 test_212() {
13337         size=`date +%s`
13338         size=$((size % 8192 + 1))
13339         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
13340         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
13341         rm -f $DIR/f212 $DIR/f212.xyz
13342 }
13343 run_test 212 "Sendfile test ============================================"
13344
13345 test_213() {
13346         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
13347         cancel_lru_locks osc
13348         lctl set_param fail_loc=0x8000040f
13349         # generate a read lock
13350         cat $DIR/$tfile > /dev/null
13351         # write to the file, it will try to cancel the above read lock.
13352         cat /etc/hosts >> $DIR/$tfile
13353 }
13354 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
13355
13356 test_214() { # for bug 20133
13357         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
13358         for (( i=0; i < 340; i++ )) ; do
13359                 touch $DIR/$tdir/d214c/a$i
13360         done
13361
13362         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
13363         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
13364         ls $DIR/d214c || error "ls $DIR/d214c failed"
13365         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
13366         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
13367 }
13368 run_test 214 "hash-indexed directory test - bug 20133"
13369
13370 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
13371 create_lnet_proc_files() {
13372         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
13373 }
13374
13375 # counterpart of create_lnet_proc_files
13376 remove_lnet_proc_files() {
13377         rm -f $TMP/lnet_$1.sys
13378 }
13379
13380 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13381 # 3rd arg as regexp for body
13382 check_lnet_proc_stats() {
13383         local l=$(cat "$TMP/lnet_$1" |wc -l)
13384         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
13385
13386         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
13387 }
13388
13389 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
13390 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
13391 # optional and can be regexp for 2nd line (lnet.routes case)
13392 check_lnet_proc_entry() {
13393         local blp=2          # blp stands for 'position of 1st line of body'
13394         [ -z "$5" ] || blp=3 # lnet.routes case
13395
13396         local l=$(cat "$TMP/lnet_$1" |wc -l)
13397         # subtracting one from $blp because the body can be empty
13398         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
13399
13400         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
13401                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
13402
13403         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
13404                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
13405
13406         # bail out if any unexpected line happened
13407         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
13408         [ "$?" != 0 ] || error "$2 misformatted"
13409 }
13410
13411 test_215() { # for bugs 18102, 21079, 21517
13412         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13413         local N='(0|[1-9][0-9]*)'       # non-negative numeric
13414         local P='[1-9][0-9]*'           # positive numeric
13415         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
13416         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
13417         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
13418         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
13419
13420         local L1 # regexp for 1st line
13421         local L2 # regexp for 2nd line (optional)
13422         local BR # regexp for the rest (body)
13423
13424         # lnet.stats should look as 11 space-separated non-negative numerics
13425         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
13426         create_lnet_proc_files "stats"
13427         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
13428         remove_lnet_proc_files "stats"
13429
13430         # lnet.routes should look like this:
13431         # Routing disabled/enabled
13432         # net hops priority state router
13433         # where net is a string like tcp0, hops > 0, priority >= 0,
13434         # state is up/down,
13435         # router is a string like 192.168.1.1@tcp2
13436         L1="^Routing (disabled|enabled)$"
13437         L2="^net +hops +priority +state +router$"
13438         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
13439         create_lnet_proc_files "routes"
13440         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
13441         remove_lnet_proc_files "routes"
13442
13443         # lnet.routers should look like this:
13444         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
13445         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
13446         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
13447         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
13448         L1="^ref +rtr_ref +alive_cnt +state +last_ping +ping_sent +deadline +down_ni +router$"
13449         BR="^$P +$P +$N +(up|down) +$N +(0|1) +$I +$I +$NID$"
13450         create_lnet_proc_files "routers"
13451         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
13452         remove_lnet_proc_files "routers"
13453
13454         # lnet.peers should look like this:
13455         # nid refs state last max rtr min tx min queue
13456         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
13457         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
13458         # numeric (0 or >0 or <0), queue >= 0.
13459         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
13460         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
13461         create_lnet_proc_files "peers"
13462         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
13463         remove_lnet_proc_files "peers"
13464
13465         # lnet.buffers  should look like this:
13466         # pages count credits min
13467         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
13468         L1="^pages +count +credits +min$"
13469         BR="^ +$N +$N +$I +$I$"
13470         create_lnet_proc_files "buffers"
13471         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
13472         remove_lnet_proc_files "buffers"
13473
13474         # lnet.nis should look like this:
13475         # nid status alive refs peer rtr max tx min
13476         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
13477         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
13478         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
13479         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
13480         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
13481         create_lnet_proc_files "nis"
13482         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
13483         remove_lnet_proc_files "nis"
13484
13485         # can we successfully write to lnet.stats?
13486         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
13487 }
13488 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
13489
13490 test_216() { # bug 20317
13491         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13492         remote_ost_nodsh && skip "remote OST with nodsh" && return
13493
13494         local node
13495         local facets=$(get_facets OST)
13496         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13497
13498         save_lustre_params client "osc.*.contention_seconds" > $p
13499         save_lustre_params $facets \
13500                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
13501         save_lustre_params $facets \
13502                 "ldlm.namespaces.filter-*.contended_locks" >> $p
13503         save_lustre_params $facets \
13504                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
13505         clear_stats osc.*.osc_stats
13506
13507         # agressive lockless i/o settings
13508         do_nodes $(comma_list $(osts_nodes)) \
13509                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
13510                         ldlm.namespaces.filter-*.contended_locks=0 \
13511                         ldlm.namespaces.filter-*.contention_seconds=60"
13512         lctl set_param -n osc.*.contention_seconds=60
13513
13514         $DIRECTIO write $DIR/$tfile 0 10 4096
13515         $CHECKSTAT -s 40960 $DIR/$tfile
13516
13517         # disable lockless i/o
13518         do_nodes $(comma_list $(osts_nodes)) \
13519                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
13520                         ldlm.namespaces.filter-*.contended_locks=32 \
13521                         ldlm.namespaces.filter-*.contention_seconds=0"
13522         lctl set_param -n osc.*.contention_seconds=0
13523         clear_stats osc.*.osc_stats
13524
13525         dd if=/dev/zero of=$DIR/$tfile count=0
13526         $CHECKSTAT -s 0 $DIR/$tfile
13527
13528         restore_lustre_params <$p
13529         rm -f $p
13530         rm $DIR/$tfile
13531 }
13532 run_test 216 "check lockless direct write updates file size and kms correctly"
13533
13534 test_217() { # bug 22430
13535         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13536         local node
13537         local nid
13538
13539         for node in $(nodes_list); do
13540                 nid=$(host_nids_address $node $NETTYPE)
13541                 if [[ $nid = *-* ]] ; then
13542                         echo "lctl ping $(h2nettype $nid)"
13543                         lctl ping $(h2nettype $nid)
13544                 else
13545                         echo "skipping $node (no hyphen detected)"
13546                 fi
13547         done
13548 }
13549 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
13550
13551 test_218() {
13552        # do directio so as not to populate the page cache
13553        log "creating a 10 Mb file"
13554        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
13555        log "starting reads"
13556        dd if=$DIR/$tfile of=/dev/null bs=4096 &
13557        log "truncating the file"
13558        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
13559        log "killing dd"
13560        kill %+ || true # reads might have finished
13561        echo "wait until dd is finished"
13562        wait
13563        log "removing the temporary file"
13564        rm -rf $DIR/$tfile || error "tmp file removal failed"
13565 }
13566 run_test 218 "parallel read and truncate should not deadlock ======================="
13567
13568 test_219() {
13569         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13570         # write one partial page
13571         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
13572         # set no grant so vvp_io_commit_write will do sync write
13573         $LCTL set_param fail_loc=0x411
13574         # write a full page at the end of file
13575         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
13576
13577         $LCTL set_param fail_loc=0
13578         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
13579         $LCTL set_param fail_loc=0x411
13580         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
13581
13582         # LU-4201
13583         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
13584         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
13585 }
13586 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
13587
13588 test_220() { #LU-325
13589         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13590         remote_ost_nodsh && skip "remote OST with nodsh" && return
13591         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13592         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
13593         local OSTIDX=0
13594
13595         # create on MDT0000 so the last_id and next_id are correct
13596         mkdir $DIR/$tdir
13597         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
13598         OST=${OST%_UUID}
13599
13600         # on the mdt's osc
13601         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
13602         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
13603                         osc.$mdtosc_proc1.prealloc_last_id)
13604         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
13605                         osc.$mdtosc_proc1.prealloc_next_id)
13606
13607         $LFS df -i
13608
13609         if ! combined_mgs_mds ; then
13610                 mount_mgs_client
13611         fi
13612
13613         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
13614         #define OBD_FAIL_OST_ENOINO              0x229
13615         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
13616         create_pool $FSNAME.$TESTNAME || return 1
13617         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
13618
13619         $SETSTRIPE $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
13620
13621         MDSOBJS=$((last_id - next_id))
13622         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
13623
13624         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
13625         echo "OST still has $count kbytes free"
13626
13627         echo "create $MDSOBJS files @next_id..."
13628         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
13629
13630         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13631                         osc.$mdtosc_proc1.prealloc_last_id)
13632         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
13633                         osc.$mdtosc_proc1.prealloc_next_id)
13634
13635         echo "after creation, last_id=$last_id2, next_id=$next_id2"
13636         $LFS df -i
13637
13638         echo "cleanup..."
13639
13640         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
13641         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
13642
13643         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
13644                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
13645         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
13646                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
13647         echo "unlink $MDSOBJS files @$next_id..."
13648         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
13649
13650         if ! combined_mgs_mds ; then
13651                 umount_mgs_client
13652         fi
13653 }
13654 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
13655
13656 test_221() {
13657         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13658         dd if=`which date` of=$MOUNT/date oflag=sync
13659         chmod +x $MOUNT/date
13660
13661         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
13662         $LCTL set_param fail_loc=0x80001401
13663
13664         $MOUNT/date > /dev/null
13665         rm -f $MOUNT/date
13666 }
13667 run_test 221 "make sure fault and truncate race to not cause OOM"
13668
13669 test_222a () {
13670         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13671         rm -rf $DIR/$tdir
13672         test_mkdir $DIR/$tdir
13673         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13674         createmany -o $DIR/$tdir/$tfile 10
13675         cancel_lru_locks mdc
13676         cancel_lru_locks osc
13677         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13678         $LCTL set_param fail_loc=0x31a
13679         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
13680         $LCTL set_param fail_loc=0
13681         rm -r $DIR/$tdir
13682 }
13683 run_test 222a "AGL for ls should not trigger CLIO lock failure"
13684
13685 test_222b () {
13686         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13687         rm -rf $DIR/$tdir
13688         test_mkdir $DIR/$tdir
13689         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13690         createmany -o $DIR/$tdir/$tfile 10
13691         cancel_lru_locks mdc
13692         cancel_lru_locks osc
13693         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
13694         $LCTL set_param fail_loc=0x31a
13695         rm -r $DIR/$tdir || error "AGL for rmdir failed"
13696         $LCTL set_param fail_loc=0
13697 }
13698 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
13699
13700 test_223 () {
13701         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13702         rm -rf $DIR/$tdir
13703         test_mkdir $DIR/$tdir
13704         $LFS setstripe -c 1 -i 0 $DIR/$tdir
13705         createmany -o $DIR/$tdir/$tfile 10
13706         cancel_lru_locks mdc
13707         cancel_lru_locks osc
13708         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
13709         $LCTL set_param fail_loc=0x31b
13710         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
13711         $LCTL set_param fail_loc=0
13712         rm -r $DIR/$tdir
13713 }
13714 run_test 223 "osc reenqueue if without AGL lock granted ======================="
13715
13716 test_224a() { # LU-1039, MRP-303
13717         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13718         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
13719         $LCTL set_param fail_loc=0x508
13720         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
13721         $LCTL set_param fail_loc=0
13722         df $DIR
13723 }
13724 run_test 224a "Don't panic on bulk IO failure"
13725
13726 test_224b() { # LU-1039, MRP-303
13727         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13728         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
13729         cancel_lru_locks osc
13730         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
13731         $LCTL set_param fail_loc=0x515
13732         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
13733         $LCTL set_param fail_loc=0
13734         df $DIR
13735 }
13736 run_test 224b "Don't panic on bulk IO failure"
13737
13738 test_224c() { # LU-6441
13739         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13740         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13741
13742         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13743         save_writethrough $p
13744         set_cache writethrough on
13745
13746         local pages_per_rpc=$($LCTL get_param \
13747                                 osc.*.max_pages_per_rpc)
13748         local at_max=$($LCTL get_param -n at_max)
13749         local timeout=$($LCTL get_param -n timeout)
13750         local test_at="$LCTL get_param -n at_max"
13751         local param_at="$FSNAME.sys.at_max"
13752         local test_timeout="$LCTL get_param -n timeout"
13753         local param_timeout="$FSNAME.sys.timeout"
13754
13755         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
13756
13757         set_conf_param_and_check client "$test_at" "$param_at" 0 ||
13758                 error "conf_param at_max=0 failed"
13759         set_conf_param_and_check client "$test_timeout" "$param_timeout" 5 ||
13760                 error "conf_param timeout=5 failed"
13761
13762         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3   0x520
13763         do_facet ost1 $LCTL set_param fail_loc=0x520
13764         $LFS setstripe -c 1 -i 0 $DIR/$tfile
13765         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
13766         sync
13767         do_facet ost1 $LCTL set_param fail_loc=0
13768
13769         set_conf_param_and_check client "$test_at" "$param_at" $at_max ||
13770                 error "conf_param at_max=$at_max failed"
13771         set_conf_param_and_check client "$test_timeout" "$param_timeout" \
13772                 $timeout || error "conf_param timeout=$timeout failed"
13773
13774         $LCTL set_param -n $pages_per_rpc
13775         restore_lustre_params < $p
13776         rm -f $p
13777 }
13778 run_test 224c "Don't hang if one of md lost during large bulk RPC"
13779
13780 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
13781 test_225a () {
13782         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13783         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13784         if [ -z ${MDSSURVEY} ]; then
13785               skip_env "mds-survey not found" && return
13786         fi
13787
13788         [ $MDSCOUNT -ge 2 ] &&
13789                 skip "skipping now for more than one MDT" && return
13790
13791        [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13792             { skip "Need MDS version at least 2.2.51"; return; }
13793
13794        local mds=$(facet_host $SINGLEMDS)
13795        local target=$(do_nodes $mds 'lctl dl' | \
13796                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13797
13798        local cmd1="file_count=1000 thrhi=4"
13799        local cmd2="dir_count=2 layer=mdd stripe_count=0"
13800        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13801        local cmd="$cmd1 $cmd2 $cmd3"
13802
13803        rm -f ${TMP}/mds_survey*
13804        echo + $cmd
13805        eval $cmd || error "mds-survey with zero-stripe failed"
13806        cat ${TMP}/mds_survey*
13807        rm -f ${TMP}/mds_survey*
13808 }
13809 run_test 225a "Metadata survey sanity with zero-stripe"
13810
13811 test_225b () {
13812         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13813         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13814         if [ -z ${MDSSURVEY} ]; then
13815               skip_env "mds-survey not found" && return
13816         fi
13817         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.2.51) ] ||
13818             { skip "Need MDS version at least 2.2.51"; return; }
13819
13820         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
13821               skip_env "Need to mount OST to test" && return
13822         fi
13823
13824        local mds=$(facet_host $SINGLEMDS)
13825        local target=$(do_nodes $mds 'lctl dl' | \
13826                       awk "{if (\$2 == \"UP\" && \$3 == \"mdt\") {print \$4}}")
13827
13828        local cmd1="file_count=1000 thrhi=4"
13829        local cmd2="dir_count=2 layer=mdd stripe_count=1"
13830        local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
13831        local cmd="$cmd1 $cmd2 $cmd3"
13832
13833        rm -f ${TMP}/mds_survey*
13834        echo + $cmd
13835        eval $cmd || error "mds-survey with stripe_count failed"
13836        cat ${TMP}/mds_survey*
13837        rm -f ${TMP}/mds_survey*
13838 }
13839 run_test 225b "Metadata survey sanity with stripe_count = 1"
13840
13841 mcreate_path2fid () {
13842         local mode=$1
13843         local major=$2
13844         local minor=$3
13845         local name=$4
13846         local desc=$5
13847         local path=$DIR/$tdir/$name
13848         local fid
13849         local rc
13850         local fid_path
13851
13852         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
13853                 error "cannot create $desc"
13854
13855         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
13856         rc=$?
13857         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
13858
13859         fid_path=$($LFS fid2path $MOUNT $fid)
13860         rc=$?
13861         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
13862
13863         [ "$path" == "$fid_path" ] ||
13864                 error "fid2path returned $fid_path, expected $path"
13865
13866         echo "pass with $path and $fid"
13867 }
13868
13869 test_226a () {
13870         rm -rf $DIR/$tdir
13871         mkdir -p $DIR/$tdir
13872
13873         mcreate_path2fid 0010666 0 0 fifo "FIFO"
13874         mcreate_path2fid 0020666 1 3 null "character special file (null)"
13875         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
13876         mcreate_path2fid 0040666 0 0 dir "directory"
13877         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
13878         mcreate_path2fid 0100666 0 0 file "regular file"
13879         mcreate_path2fid 0120666 0 0 link "symbolic link"
13880         mcreate_path2fid 0140666 0 0 sock "socket"
13881 }
13882 run_test 226a "call path2fid and fid2path on files of all type"
13883
13884 test_226b () {
13885         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
13886         rm -rf $DIR/$tdir
13887         local MDTIDX=1
13888
13889         mkdir -p $DIR/$tdir
13890         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
13891                 error "create remote directory failed"
13892         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
13893         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
13894                                 "character special file (null)"
13895         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
13896                                 "character special file (no device)"
13897         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
13898         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
13899                                 "block special file (loop)"
13900         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
13901         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
13902         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
13903 }
13904 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
13905
13906 # LU-1299 Executing or running ldd on a truncated executable does not
13907 # cause an out-of-memory condition.
13908 test_227() {
13909         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13910         [ -z "$(which ldd)" ] && skip "should have ldd tool" && return
13911         dd if=$(which date) of=$MOUNT/date bs=1k count=1
13912         chmod +x $MOUNT/date
13913
13914         $MOUNT/date > /dev/null
13915         ldd $MOUNT/date > /dev/null
13916         rm -f $MOUNT/date
13917 }
13918 run_test 227 "running truncated executable does not cause OOM"
13919
13920 # LU-1512 try to reuse idle OI blocks
13921 test_228a() {
13922         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13923         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13924         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13925                 skip "ldiskfs only test" && return
13926
13927         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13928         local myDIR=$DIR/$tdir
13929
13930         mkdir -p $myDIR
13931         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13932         $LCTL set_param fail_loc=0x80001002
13933         createmany -o $myDIR/t- 10000
13934         $LCTL set_param fail_loc=0
13935         # The guard is current the largest FID holder
13936         touch $myDIR/guard
13937         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13938                     tr -d '[')
13939         local IDX=$(($SEQ % 64))
13940
13941         do_facet $SINGLEMDS sync
13942         # Make sure journal flushed.
13943         sleep 6
13944         local blk1=$(do_facet $SINGLEMDS \
13945                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13946                      grep Blockcount | awk '{print $4}')
13947
13948         # Remove old files, some OI blocks will become idle.
13949         unlinkmany $myDIR/t- 10000
13950         # Create new files, idle OI blocks should be reused.
13951         createmany -o $myDIR/t- 2000
13952         do_facet $SINGLEMDS sync
13953         # Make sure journal flushed.
13954         sleep 6
13955         local blk2=$(do_facet $SINGLEMDS \
13956                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13957                      grep Blockcount | awk '{print $4}')
13958
13959         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
13960 }
13961 run_test 228a "try to reuse idle OI blocks"
13962
13963 test_228b() {
13964         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
13965         remote_mds_nodsh && skip "remote MDS with nodsh" && return
13966         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
13967                 skip "ldiskfs only test" && return
13968
13969         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
13970         local myDIR=$DIR/$tdir
13971
13972         mkdir -p $myDIR
13973         #define OBD_FAIL_SEQ_EXHAUST             0x1002
13974         $LCTL set_param fail_loc=0x80001002
13975         createmany -o $myDIR/t- 10000
13976         $LCTL set_param fail_loc=0
13977         # The guard is current the largest FID holder
13978         touch $myDIR/guard
13979         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
13980                     tr -d '[')
13981         local IDX=$(($SEQ % 64))
13982
13983         do_facet $SINGLEMDS sync
13984         # Make sure journal flushed.
13985         sleep 6
13986         local blk1=$(do_facet $SINGLEMDS \
13987                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
13988                      grep Blockcount | awk '{print $4}')
13989
13990         # Remove old files, some OI blocks will become idle.
13991         unlinkmany $myDIR/t- 10000
13992
13993         # stop the MDT
13994         stop $SINGLEMDS || error "Fail to stop MDT."
13995         # remount the MDT
13996         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
13997
13998         df $MOUNT || error "Fail to df."
13999         # Create new files, idle OI blocks should be reused.
14000         createmany -o $myDIR/t- 2000
14001         do_facet $SINGLEMDS sync
14002         # Make sure journal flushed.
14003         sleep 6
14004         local blk2=$(do_facet $SINGLEMDS \
14005                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14006                      grep Blockcount | awk '{print $4}')
14007
14008         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14009 }
14010 run_test 228b "idle OI blocks can be reused after MDT restart"
14011
14012 #LU-1881
14013 test_228c() {
14014         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14015         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14016         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
14017                 skip "ldiskfs only test" && return
14018
14019         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
14020         local myDIR=$DIR/$tdir
14021
14022         mkdir -p $myDIR
14023         #define OBD_FAIL_SEQ_EXHAUST             0x1002
14024         $LCTL set_param fail_loc=0x80001002
14025         # 20000 files can guarantee there are index nodes in the OI file
14026         createmany -o $myDIR/t- 20000
14027         $LCTL set_param fail_loc=0
14028         # The guard is current the largest FID holder
14029         touch $myDIR/guard
14030         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
14031                     tr -d '[')
14032         local IDX=$(($SEQ % 64))
14033
14034         do_facet $SINGLEMDS sync
14035         # Make sure journal flushed.
14036         sleep 6
14037         local blk1=$(do_facet $SINGLEMDS \
14038                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14039                      grep Blockcount | awk '{print $4}')
14040
14041         # Remove old files, some OI blocks will become idle.
14042         unlinkmany $myDIR/t- 20000
14043         rm -f $myDIR/guard
14044         # The OI file should become empty now
14045
14046         # Create new files, idle OI blocks should be reused.
14047         createmany -o $myDIR/t- 2000
14048         do_facet $SINGLEMDS sync
14049         # Make sure journal flushed.
14050         sleep 6
14051         local blk2=$(do_facet $SINGLEMDS \
14052                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
14053                      grep Blockcount | awk '{print $4}')
14054
14055         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
14056 }
14057 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
14058
14059 test_229() { # LU-2482, LU-3448
14060         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.53) ] &&
14061                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53" &&
14062                 return
14063         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14064         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
14065
14066         rm -f $DIR/$tfile
14067
14068         # Create a file with a released layout and stripe count 2.
14069         $MULTIOP $DIR/$tfile H2c ||
14070                 error "failed to create file with released layout"
14071
14072         $GETSTRIPE -v $DIR/$tfile
14073
14074         local pattern=$($GETSTRIPE -L $DIR/$tfile)
14075         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
14076
14077         local stripe_count=$($GETSTRIPE -c $DIR/$tfile) || error "getstripe"
14078         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
14079         stat $DIR/$tfile || error "failed to stat released file"
14080
14081         chown $RUNAS_ID $DIR/$tfile ||
14082                 error "chown $RUNAS_ID $DIR/$tfile failed"
14083
14084         chgrp $RUNAS_ID $DIR/$tfile ||
14085                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
14086
14087         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
14088         rm $DIR/$tfile || error "failed to remove released file"
14089 }
14090 run_test 229 "getstripe/stat/rm/attr changes work on released files"
14091
14092 test_230a() {
14093         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14094         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14095         local MDTIDX=1
14096
14097         test_mkdir $DIR/$tdir
14098         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
14099         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230_local)
14100         [ $mdt_idx -ne 0 ] &&
14101                 error "create local directory on wrong MDT $mdt_idx"
14102
14103         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
14104                         error "create remote directory failed"
14105         local mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230)
14106         [ $mdt_idx -ne $MDTIDX ] &&
14107                 error "create remote directory on wrong MDT $mdt_idx"
14108
14109         createmany -o $DIR/$tdir/test_230/t- 10 ||
14110                 error "create files on remote directory failed"
14111         mdt_idx=$($GETSTRIPE -M $DIR/$tdir/test_230/t-0)
14112         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
14113         rm -r $DIR/$tdir || error "unlink remote directory failed"
14114 }
14115 run_test 230a "Create remote directory and files under the remote directory"
14116
14117 test_230b() {
14118         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14119         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14120         local MDTIDX=1
14121         local mdt_index
14122         local i
14123         local file
14124         local pid
14125         local stripe_count
14126         local migrate_dir=$DIR/$tdir/migrate_dir
14127         local other_dir=$DIR/$tdir/other_dir
14128
14129         test_mkdir $DIR/$tdir
14130         test_mkdir -i0 -c1 $migrate_dir
14131         test_mkdir -i0 -c1 $other_dir
14132         for ((i=0; i<10; i++)); do
14133                 mkdir -p $migrate_dir/dir_${i}
14134                 createmany -o $migrate_dir/dir_${i}/f 10 ||
14135                         error "create files under remote dir failed $i"
14136         done
14137
14138         cp /etc/passwd $migrate_dir/$tfile
14139         cp /etc/passwd $other_dir/$tfile
14140         chattr +SAD $migrate_dir
14141         chattr +SAD $migrate_dir/$tfile
14142
14143         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14144         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14145         local old_dir_mode=$(stat -c%f $migrate_dir)
14146         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
14147
14148         mkdir -p $migrate_dir/dir_default_stripe2
14149         $SETSTRIPE -c 2 $migrate_dir/dir_default_stripe2
14150         $SETSTRIPE -c 2 $migrate_dir/${tfile}_stripe2
14151
14152         mkdir -p $other_dir
14153         ln $migrate_dir/$tfile $other_dir/luna
14154         ln $migrate_dir/$tfile $migrate_dir/sofia
14155         ln $other_dir/$tfile $migrate_dir/david
14156         ln -s $migrate_dir/$tfile $other_dir/zachary
14157         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
14158         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
14159
14160         $LFS migrate -m $MDTIDX $migrate_dir ||
14161                 error "fails on migrating remote dir to MDT1"
14162
14163         echo "migratate to MDT1, then checking.."
14164         for ((i = 0; i < 10; i++)); do
14165                 for file in $(find $migrate_dir/dir_${i}); do
14166                         mdt_index=$($LFS getstripe -M $file)
14167                         [ $mdt_index == $MDTIDX ] ||
14168                                 error "$file is not on MDT${MDTIDX}"
14169                 done
14170         done
14171
14172         # the multiple link file should still in MDT0
14173         mdt_index=$($LFS getstripe -M $migrate_dir/$tfile)
14174         [ $mdt_index == 0 ] ||
14175                 error "$file is not on MDT${MDTIDX}"
14176
14177         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14178         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14179                 error " expect $old_dir_flag get $new_dir_flag"
14180
14181         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14182         [ "$old_file_flag" = "$new_file_flag" ] ||
14183                 error " expect $old_file_flag get $new_file_flag"
14184
14185         local new_dir_mode=$(stat -c%f $migrate_dir)
14186         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14187                 error "expect mode $old_dir_mode get $new_dir_mode"
14188
14189         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14190         [ "$old_file_mode" = "$new_file_mode" ] ||
14191                 error "expect mode $old_file_mode get $new_file_mode"
14192
14193         diff /etc/passwd $migrate_dir/$tfile ||
14194                 error "$tfile different after migration"
14195
14196         diff /etc/passwd $other_dir/luna ||
14197                 error "luna different after migration"
14198
14199         diff /etc/passwd $migrate_dir/sofia ||
14200                 error "sofia different after migration"
14201
14202         diff /etc/passwd $migrate_dir/david ||
14203                 error "david different after migration"
14204
14205         diff /etc/passwd $other_dir/zachary ||
14206                 error "zachary different after migration"
14207
14208         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14209                 error "${tfile}_ln different after migration"
14210
14211         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14212                 error "${tfile}_ln_other different after migration"
14213
14214         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
14215         [ $stripe_count = 2 ] ||
14216                 error "dir strpe_count $d != 2 after migration."
14217
14218         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
14219         [ $stripe_count = 2 ] ||
14220                 error "file strpe_count $d != 2 after migration."
14221
14222         #migrate back to MDT0
14223         MDTIDX=0
14224
14225         $LFS migrate -m $MDTIDX $migrate_dir ||
14226                 error "fails on migrating remote dir to MDT0"
14227
14228         echo "migrate back to MDT0, checking.."
14229         for file in $(find $migrate_dir); do
14230                 mdt_index=$($LFS getstripe -M $file)
14231                 [ $mdt_index == $MDTIDX ] ||
14232                         error "$file is not on MDT${MDTIDX}"
14233         done
14234
14235         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
14236         [ "$old_dir_flag" = "$new_dir_flag" ] ||
14237                 error " expect $old_dir_flag get $new_dir_flag"
14238
14239         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
14240         [ "$old_file_flag" = "$new_file_flag" ] ||
14241                 error " expect $old_file_flag get $new_file_flag"
14242
14243         local new_dir_mode=$(stat -c%f $migrate_dir)
14244         [ "$old_dir_mode" = "$new_dir_mode" ] ||
14245                 error "expect mode $old_dir_mode get $new_dir_mode"
14246
14247         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
14248         [ "$old_file_mode" = "$new_file_mode" ] ||
14249                 error "expect mode $old_file_mode get $new_file_mode"
14250
14251         diff /etc/passwd ${migrate_dir}/$tfile ||
14252                 error "$tfile different after migration"
14253
14254         diff /etc/passwd ${other_dir}/luna ||
14255                 error "luna different after migration"
14256
14257         diff /etc/passwd ${migrate_dir}/sofia ||
14258                 error "sofia different after migration"
14259
14260         diff /etc/passwd ${other_dir}/zachary ||
14261                 error "zachary different after migration"
14262
14263         diff /etc/passwd $migrate_dir/${tfile}_ln ||
14264                 error "${tfile}_ln different after migration"
14265
14266         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
14267                 error "${tfile}_ln_other different after migration"
14268
14269         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
14270         [ $stripe_count = 2 ] ||
14271                 error "dir strpe_count $d != 2 after migration."
14272
14273         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
14274         [ $stripe_count = 2 ] ||
14275                 error "file strpe_count $d != 2 after migration."
14276
14277         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14278 }
14279 run_test 230b "migrate directory"
14280
14281 test_230c() {
14282         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14283         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14284         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14285         local MDTIDX=1
14286         local mdt_index
14287         local file
14288         local migrate_dir=$DIR/$tdir/migrate_dir
14289
14290         #If migrating directory fails in the middle, all entries of
14291         #the directory is still accessiable.
14292         test_mkdir $DIR/$tdir
14293         test_mkdir -i0 -c1 $migrate_dir
14294         stat $migrate_dir
14295         createmany -o $migrate_dir/f 10 ||
14296                 error "create files under ${migrate_dir} failed"
14297
14298         #failed after migrating 5 entries
14299         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
14300         do_facet mds1 lctl set_param fail_loc=0x20001801
14301         do_facet mds1 lctl  set_param fail_val=5
14302         local t=$(ls $migrate_dir | wc -l)
14303         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
14304                 error "migrate should fail after 5 entries"
14305
14306         mkdir $migrate_dir/dir &&
14307                 error "mkdir succeeds under migrating directory"
14308         touch $migrate_dir/file &&
14309                 error "touch file succeeds under migrating directory"
14310
14311         local u=$(ls $migrate_dir | wc -l)
14312         [ "$u" == "$t" ] || error "$u != $t during migration"
14313
14314         for file in $(find $migrate_dir); do
14315                 stat $file || error "stat $file failed"
14316         done
14317
14318         do_facet mds1 lctl set_param fail_loc=0
14319         do_facet mds1 lctl set_param fail_val=0
14320
14321         $LFS migrate -m $MDTIDX $migrate_dir ||
14322                 error "migrate open files should failed with open files"
14323
14324         echo "Finish migration, then checking.."
14325         for file in $(find $migrate_dir); do
14326                 mdt_index=$($LFS getstripe -M $file)
14327                 [ $mdt_index == $MDTIDX ] ||
14328                         error "$file is not on MDT${MDTIDX}"
14329         done
14330
14331         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14332 }
14333 run_test 230c "check directory accessiblity if migration is failed"
14334
14335 test_230d() {
14336         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14337         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14338         local MDTIDX=1
14339         local mdt_index
14340         local migrate_dir=$DIR/$tdir/migrate_dir
14341         local i
14342         local j
14343
14344         test_mkdir $DIR/$tdir
14345         test_mkdir -i0 -c1 $migrate_dir
14346
14347         for ((i=0; i<100; i++)); do
14348                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
14349                 createmany -o $migrate_dir/dir_${i}/f 100 ||
14350                         error "create files under remote dir failed $i"
14351         done
14352
14353         $LFS migrate -m $MDTIDX $migrate_dir ||
14354                 error "migrate remote dir error"
14355
14356         echo "Finish migration, then checking.."
14357         for file in $(find $migrate_dir); do
14358                 mdt_index=$($LFS getstripe -M $file)
14359                 [ $mdt_index == $MDTIDX ] ||
14360                         error "$file is not on MDT${MDTIDX}"
14361         done
14362
14363         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14364 }
14365 run_test 230d "check migrate big directory"
14366
14367 test_230e() {
14368         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14369         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14370         local i
14371         local j
14372         local a_fid
14373         local b_fid
14374
14375         mkdir -p $DIR/$tdir
14376         mkdir $DIR/$tdir/migrate_dir
14377         mkdir $DIR/$tdir/other_dir
14378         touch $DIR/$tdir/migrate_dir/a
14379         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
14380         ls $DIR/$tdir/other_dir
14381
14382         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14383                 error "migrate dir fails"
14384
14385         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14386         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14387
14388         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14389         [ $mdt_index == 0 ] || error "a is not on MDT0"
14390
14391         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
14392                 error "migrate dir fails"
14393
14394         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir)
14395         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
14396
14397         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14398         [ $mdt_index == 1 ] || error "a is not on MDT1"
14399
14400         mdt_index=$($LFS getstripe -M $DIR/$tdir/other_dir/b)
14401         [ $mdt_index == 1 ] || error "b is not on MDT1"
14402
14403         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14404         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
14405
14406         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
14407
14408         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14409 }
14410 run_test 230e "migrate mulitple local link files"
14411
14412 test_230f() {
14413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14414         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14415         local a_fid
14416         local ln_fid
14417
14418         mkdir -p $DIR/$tdir
14419         mkdir $DIR/$tdir/migrate_dir
14420         $LFS mkdir -i1 $DIR/$tdir/other_dir
14421         touch $DIR/$tdir/migrate_dir/a
14422         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
14423         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
14424         ls $DIR/$tdir/other_dir
14425
14426         # a should be migrated to MDT1, since no other links on MDT0
14427         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14428                 error "#1 migrate dir fails"
14429         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14430         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
14431         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14432         [ $mdt_index == 1 ] || error "a is not on MDT1"
14433
14434         # a should stay on MDT1, because it is a mulitple link file
14435         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14436                 error "#2 migrate dir fails"
14437         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14438         [ $mdt_index == 1 ] || error "a is not on MDT1"
14439
14440         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
14441                 error "#3 migrate dir fails"
14442
14443         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
14444         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
14445         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
14446
14447         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
14448         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
14449
14450         # a should be migrated to MDT0, since no other links on MDT1
14451         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
14452                 error "#4 migrate dir fails"
14453         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir/a)
14454         [ $mdt_index == 0 ] || error "a is not on MDT0"
14455
14456         rm -rf $DIR/$tdir || error "rm dir failed after migration"
14457 }
14458 run_test 230f "migrate mulitple remote link files"
14459
14460 test_230g() {
14461         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14462         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14463
14464         mkdir -p $DIR/$tdir/migrate_dir
14465
14466         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
14467                 error "migrating dir to non-exist MDT succeeds"
14468         true
14469 }
14470 run_test 230g "migrate dir to non-exist MDT"
14471
14472 test_230h() {
14473         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14474         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14475         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.64) ] &&
14476                 skip "Need MDS version at least 2.7.64" && return
14477         local mdt_index
14478
14479         mkdir -p $DIR/$tdir/migrate_dir
14480
14481         $LFS migrate -m1 $DIR &&
14482                 error "migrating mountpoint1 should fail"
14483
14484         $LFS migrate -m1 $DIR/$tdir/.. &&
14485                 error "migrating mountpoint2 should fail"
14486
14487         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. ||
14488                 error "migrating $tdir fail"
14489
14490         mdt_index=$($LFS getstripe -M $DIR/$tdir)
14491         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14492
14493         mdt_index=$($LFS getstripe -M $DIR/$tdir/migrate_dir)
14494         [ $mdt_index == 1 ] || error "$mdt_index != 1 after migration"
14495
14496 }
14497 run_test 230h "migrate .. and root"
14498
14499 test_230i() {
14500         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
14501         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14502
14503         mkdir -p $DIR/$tdir/migrate_dir
14504
14505         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
14506                 error "migration fails with a tailing slash"
14507
14508         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
14509                 error "migration fails with two tailing slashes"
14510 }
14511 run_test 230i "lfs migrate -m tolerates trailing slashes"
14512
14513 test_231a()
14514 {
14515         # For simplicity this test assumes that max_pages_per_rpc
14516         # is the same across all OSCs
14517         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
14518         local bulk_size=$((max_pages * 4096))
14519         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
14520                                        head -n 1)
14521
14522         mkdir -p $DIR/$tdir
14523         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
14524                 error "failed to set stripe with -S ${brw_size}M option"
14525
14526         # clear the OSC stats
14527         $LCTL set_param osc.*.stats=0 &>/dev/null
14528         stop_writeback
14529
14530         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
14531         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
14532                 oflag=direct &>/dev/null || error "dd failed"
14533
14534         sync; sleep 1; sync # just to be safe
14535         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
14536         if [ x$nrpcs != "x1" ]; then
14537                 $LCTL get_param osc.*.stats
14538                 error "found $nrpcs ost_write RPCs, not 1 as expected"
14539         fi
14540
14541         start_writeback
14542         # Drop the OSC cache, otherwise we will read from it
14543         cancel_lru_locks osc
14544
14545         # clear the OSC stats
14546         $LCTL set_param osc.*.stats=0 &>/dev/null
14547
14548         # Client reads $bulk_size.
14549         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
14550                 iflag=direct &>/dev/null || error "dd failed"
14551
14552         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
14553         if [ x$nrpcs != "x1" ]; then
14554                 $LCTL get_param osc.*.stats
14555                 error "found $nrpcs ost_read RPCs, not 1 as expected"
14556         fi
14557 }
14558 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
14559
14560 test_231b() {
14561         mkdir -p $DIR/$tdir
14562         local i
14563         for i in {0..1023}; do
14564                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
14565                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
14566                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
14567         done
14568         sync
14569 }
14570 run_test 231b "must not assert on fully utilized OST request buffer"
14571
14572 test_232a() {
14573         mkdir -p $DIR/$tdir
14574         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14575
14576         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14577         do_facet ost1 $LCTL set_param fail_loc=0x31c
14578
14579         # ignore dd failure
14580         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || 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 232a "failed lock should not block umount"
14589
14590 test_232b() {
14591         mkdir -p $DIR/$tdir
14592         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
14593         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
14594         sync
14595         cancel_lru_locks osc
14596
14597         #define OBD_FAIL_LDLM_OST_LVB            0x31c
14598         do_facet ost1 $LCTL set_param fail_loc=0x31c
14599
14600         # ignore failure
14601         $LFS data_version $DIR/$tdir/$tfile || true
14602
14603         do_facet ost1 $LCTL set_param fail_loc=0
14604         umount_client $MOUNT || error "umount failed"
14605         mount_client $MOUNT || error "mount failed"
14606         stop ost1 || error "cannot stop ost1"
14607         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
14608 }
14609 run_test 232b "failed data version lock should not block umount"
14610
14611 test_233a() {
14612         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.3.64) ] ||
14613         { skip "Need MDS version at least 2.3.64"; return; }
14614         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14615
14616         local fid=$($LFS path2fid $MOUNT)
14617         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14618                 error "cannot access $MOUNT using its FID '$fid'"
14619 }
14620 run_test 233a "checking that OBF of the FS root succeeds"
14621
14622 test_233b() {
14623         [ $(lustre_version_code $SINGLEMDS) -ge $(version_code 2.5.90) ] ||
14624         { skip "Need MDS version at least 2.5.90"; return; }
14625         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
14626
14627         local fid=$($LFS path2fid $MOUNT/.lustre)
14628         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14629                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
14630
14631         fid=$($LFS path2fid $MOUNT/.lustre/fid)
14632         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
14633                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
14634 }
14635 run_test 233b "checking that OBF of the FS .lustre succeeds"
14636
14637 test_234() {
14638         local p="$TMP/sanityN-$TESTNAME.parameters"
14639         save_lustre_params client "llite.*.xattr_cache" > $p
14640         lctl set_param llite.*.xattr_cache 1 ||
14641                 { skip "xattr cache is not supported"; return 0; }
14642
14643         mkdir -p $DIR/$tdir || error "mkdir failed"
14644         touch $DIR/$tdir/$tfile || error "touch failed"
14645         # OBD_FAIL_LLITE_XATTR_ENOMEM
14646         $LCTL set_param fail_loc=0x1405
14647         getfattr -n user.attr $DIR/$tdir/$tfile &&
14648                 error "getfattr should have failed with ENOMEM"
14649         $LCTL set_param fail_loc=0x0
14650         rm -rf $DIR/$tdir
14651
14652         restore_lustre_params < $p
14653         rm -f $p
14654 }
14655 run_test 234 "xattr cache should not crash on ENOMEM"
14656
14657 test_235() {
14658         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.4.52) ] &&
14659                 skip "Need MDS version at least 2.4.52" && return
14660         flock_deadlock $DIR/$tfile
14661         local RC=$?
14662         case $RC in
14663                 0)
14664                 ;;
14665                 124) error "process hangs on a deadlock"
14666                 ;;
14667                 *) error "error executing flock_deadlock $DIR/$tfile"
14668                 ;;
14669         esac
14670 }
14671 run_test 235 "LU-1715: flock deadlock detection does not work properly"
14672
14673 #LU-2935
14674 test_236() {
14675         check_swap_layouts_support && return 0
14676         test_mkdir -c1 $DIR/$tdir
14677
14678         local ref1=/etc/passwd
14679         local ref2=/etc/group
14680         local file1=$DIR/$tdir/f1
14681         local file2=$DIR/$tdir/f2
14682
14683         $SETSTRIPE -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
14684         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
14685         $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
14686         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
14687         local fd=$(free_fd)
14688         local cmd="exec $fd<>$file2"
14689         eval $cmd
14690         rm $file2
14691         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
14692                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
14693         cmd="exec $fd>&-"
14694         eval $cmd
14695         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
14696
14697         #cleanup
14698         rm -rf $DIR/$tdir
14699 }
14700 run_test 236 "Layout swap on open unlinked file"
14701
14702 # test to verify file handle related system calls
14703 # (name_to_handle_at/open_by_handle_at)
14704 # The new system calls are supported in glibc >= 2.14.
14705
14706 test_237() {
14707         echo "Test file_handle syscalls" > $DIR/$tfile ||
14708                 error "write failed"
14709         check_fhandle_syscalls $DIR/$tfile ||
14710                 error "check_fhandle_syscalls failed"
14711 }
14712 run_test 237 "Verify name_to_handle_at/open_by_handle_at syscalls"
14713
14714 # LU-4659 linkea consistency
14715 test_238() {
14716         local server_version=$(lustre_version_code $SINGLEMDS)
14717
14718         [[ $server_version -gt $(version_code 2.5.57) ]] ||
14719                 [[ $server_version -gt $(version_code 2.5.1) &&
14720                    $server_version -lt $(version_code 2.5.50) ]] ||
14721                 { skip "Need MDS version at least 2.5.58 or 2.5.2+"; return; }
14722
14723         touch $DIR/$tfile
14724         ln $DIR/$tfile $DIR/$tfile.lnk
14725         touch $DIR/$tfile.new
14726         mv $DIR/$tfile.new $DIR/$tfile
14727         local fid1=$($LFS path2fid $DIR/$tfile)
14728         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
14729         local path1=$($LFS fid2path $FSNAME "$fid1")
14730         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
14731         local path2=$($LFS fid2path $FSNAME "$fid2")
14732         [ $tfile.lnk == $path2 ] ||
14733                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
14734         rm -f $DIR/$tfile*
14735 }
14736 run_test 238 "Verify linkea consistency"
14737
14738 test_239() {
14739         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.5.60) ] &&
14740                 skip "Need MDS version at least 2.5.60" && return
14741         local list=$(comma_list $(mdts_nodes))
14742
14743         mkdir -p $DIR/$tdir
14744         createmany -o $DIR/$tdir/f- 5000
14745         unlinkmany $DIR/$tdir/f- 5000
14746         [ $(lustre_version_code $SINGLEMDS) -gt $(version_code 2.10.53) ] &&
14747                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
14748         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
14749                         osp.*MDT*.sync_in_flight" | calc_sum)
14750         [ "$changes" -eq 0 ] || error "$changes not synced"
14751 }
14752 run_test 239 "osp_sync test"
14753
14754 test_239a() { #LU-5297
14755         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14756         touch $DIR/$tfile
14757         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
14758         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
14759         chgrp $RUNAS_GID $DIR/$tfile
14760         wait_delete_completed
14761 }
14762 run_test 239a "process invalid osp sync record correctly"
14763
14764 test_239b() { #LU-5297
14765         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14766         touch $DIR/$tfile1
14767         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
14768         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
14769         chgrp $RUNAS_GID $DIR/$tfile1
14770         wait_delete_completed
14771         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
14772         touch $DIR/$tfile2
14773         chgrp $RUNAS_GID $DIR/$tfile2
14774         wait_delete_completed
14775 }
14776 run_test 239b "process osp sync record with ENOMEM error correctly"
14777
14778 test_240() {
14779         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
14780         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14781
14782         mkdir -p $DIR/$tdir
14783
14784         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
14785                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
14786         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
14787                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
14788
14789         umount_client $MOUNT || error "umount failed"
14790         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
14791         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
14792         mount_client $MOUNT || error "failed to mount client"
14793
14794         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
14795         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
14796 }
14797 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
14798
14799 test_241_bio() {
14800         for LOOP in $(seq $1); do
14801                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 2>/dev/null
14802                 cancel_lru_locks $OSC || true
14803         done
14804 }
14805
14806 test_241_dio() {
14807         for LOOP in $(seq $1); do
14808                 dd if=$DIR/$tfile of=/dev/null bs=40960 count=1 \
14809                                                 iflag=direct 2>/dev/null
14810         done
14811 }
14812
14813 test_241a() { # was test_241
14814         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14815         ls -la $DIR/$tfile
14816         cancel_lru_locks $OSC
14817         test_241_bio 1000 &
14818         PID=$!
14819         test_241_dio 1000
14820         wait $PID
14821 }
14822 run_test 241a "bio vs dio"
14823
14824 test_241b() {
14825         dd if=/dev/zero of=$DIR/$tfile count=1 bs=40960
14826         ls -la $DIR/$tfile
14827         test_241_dio 1000 &
14828         PID=$!
14829         test_241_dio 1000
14830         wait $PID
14831 }
14832 run_test 241b "dio vs dio"
14833
14834 test_242() {
14835         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14836         mkdir -p $DIR/$tdir
14837         touch $DIR/$tdir/$tfile
14838
14839         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
14840         do_facet mds1 lctl set_param fail_loc=0x105
14841         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
14842
14843         do_facet mds1 lctl set_param fail_loc=0
14844         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
14845 }
14846 run_test 242 "mdt_readpage failure should not cause directory unreadable"
14847
14848 test_243()
14849 {
14850         test_mkdir $DIR/$tdir
14851         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
14852 }
14853 run_test 243 "various group lock tests"
14854
14855 test_244()
14856 {
14857         test_mkdir $DIR/$tdir
14858         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
14859         sendfile_grouplock $DIR/$tdir/$tfile || \
14860                 error "sendfile+grouplock failed"
14861         rm -rf $DIR/$tdir
14862 }
14863 run_test 244 "sendfile with group lock tests"
14864
14865 test_245() {
14866         local flagname="multi_mod_rpcs"
14867         local connect_data_name="max_mod_rpcs"
14868         local out
14869
14870         # check if multiple modify RPCs flag is set
14871         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
14872                 grep "connect_flags:")
14873         echo "$out"
14874
14875         echo "$out" | grep -qw $flagname
14876         if [ $? -ne 0 ]; then
14877                 echo "connect flag $flagname is not set"
14878                 return
14879         fi
14880
14881         # check if multiple modify RPCs data is set
14882         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
14883         echo "$out"
14884
14885         echo "$out" | grep -qw $connect_data_name ||
14886                 error "import should have connect data $connect_data_name"
14887 }
14888 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
14889
14890 test_246() { # LU-7371
14891         remote_ost_nodsh && skip "remote OST with nodsh" && return
14892         [ $(lustre_version_code ost1) -lt $(version_code 2.7.62) ] &&
14893                 skip "Need OST version >= 2.7.62" && return 0
14894         do_facet ost1 $LCTL set_param fail_val=4095
14895 #define OBD_FAIL_OST_READ_SIZE          0x234
14896         do_facet ost1 $LCTL set_param fail_loc=0x234
14897         $LFS setstripe $DIR/$tfile -i 0 -c 1
14898         dd if=/dev/zero of=$DIR/$tfile bs=4095 count=1 > /dev/null 2>&1
14899         cancel_lru_locks $FSNAME-OST0000
14900         dd if=$DIR/$tfile of=/dev/null bs=1048576 || error "Read failed"
14901 }
14902 run_test 246 "Read file of size 4095 should return right length"
14903
14904 cleanup_247() {
14905         local submount=$1
14906
14907         trap 0
14908         umount_client $submount
14909         rmdir $submount
14910 }
14911
14912 test_247a() {
14913         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14914                 grep -q subtree ||
14915                 { skip "Fileset feature is not supported"; return; }
14916
14917         local submount=${MOUNT}_$tdir
14918
14919         mkdir $MOUNT/$tdir
14920         mkdir -p $submount || error "mkdir $submount failed"
14921         FILESET="$FILESET/$tdir" mount_client $submount ||
14922                 error "mount $submount failed"
14923         trap "cleanup_247 $submount" EXIT
14924         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
14925         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
14926                 error "read $MOUNT/$tdir/$tfile failed"
14927         cleanup_247 $submount
14928 }
14929 run_test 247a "mount subdir as fileset"
14930
14931 test_247b() {
14932         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14933                 { skip "Fileset feature is not supported"; return; }
14934
14935         local submount=${MOUNT}_$tdir
14936
14937         rm -rf $MOUNT/$tdir
14938         mkdir -p $submount || error "mkdir $submount failed"
14939         SKIP_FILESET=1
14940         FILESET="$FILESET/$tdir" mount_client $submount &&
14941                 error "mount $submount should fail"
14942         rmdir $submount
14943 }
14944 run_test 247b "mount subdir that dose not exist"
14945
14946 test_247c() {
14947         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14948                 { skip "Fileset feature is not supported"; return; }
14949
14950         local submount=${MOUNT}_$tdir
14951
14952         mkdir -p $MOUNT/$tdir/dir1
14953         mkdir -p $submount || error "mkdir $submount failed"
14954         trap "cleanup_247 $submount" EXIT
14955         FILESET="$FILESET/$tdir" mount_client $submount ||
14956                 error "mount $submount failed"
14957         local fid=$($LFS path2fid $MOUNT/)
14958         $LFS fid2path $submount $fid && error "fid2path should fail"
14959         cleanup_247 $submount
14960 }
14961 run_test 247c "running fid2path outside root"
14962
14963 test_247d() {
14964         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
14965                 { skip "Fileset feature is not supported"; return; }
14966
14967         local submount=${MOUNT}_$tdir
14968
14969         mkdir -p $MOUNT/$tdir/dir1
14970         mkdir -p $submount || error "mkdir $submount failed"
14971         FILESET="$FILESET/$tdir" mount_client $submount ||
14972                 error "mount $submount failed"
14973         trap "cleanup_247 $submount" EXIT
14974         local fid=$($LFS path2fid $submount/dir1)
14975         $LFS fid2path $submount $fid || error "fid2path should succeed"
14976         cleanup_247 $submount
14977 }
14978 run_test 247d "running fid2path inside root"
14979
14980 # LU-8037
14981 test_247e() {
14982         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
14983                 grep -q subtree ||
14984                 { skip "Fileset feature is not supported"; return; }
14985
14986         local submount=${MOUNT}_$tdir
14987
14988         mkdir $MOUNT/$tdir
14989         mkdir -p $submount || error "mkdir $submount failed"
14990         FILESET="$FILESET/.." mount_client $submount &&
14991                 error "mount $submount should fail"
14992         rmdir $submount
14993 }
14994 run_test 247e "mount .. as fileset"
14995
14996 test_248() {
14997         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
14998         [ -z "$fast_read_sav" ] && skip "no fast read support" && return
14999
15000         # create a large file for fast read verification
15001         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
15002
15003         # make sure the file is created correctly
15004         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
15005                 { rm -f $DIR/$tfile; skip "file creation error" && return; }
15006
15007         echo "Test 1: verify that fast read is 4 times faster on cache read"
15008
15009         # small read with fast read enabled
15010         $LCTL set_param -n llite.*.fast_read=1
15011         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
15012                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15013                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15014         # small read with fast read disabled
15015         $LCTL set_param -n llite.*.fast_read=0
15016         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
15017                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15018                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15019
15020         # verify that fast read is 4 times faster for cache read
15021         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
15022                 error_not_in_vm "fast read was not 4 times faster: " \
15023                            "$t_fast vs $t_slow"
15024
15025         echo "Test 2: verify the performance between big and small read"
15026         $LCTL set_param -n llite.*.fast_read=1
15027
15028         # 1k non-cache read
15029         cancel_lru_locks osc
15030         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15031                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15032                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15033
15034         # 1M non-cache read
15035         cancel_lru_locks osc
15036         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
15037                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
15038                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
15039
15040         # verify that big IO is not 4 times faster than small IO
15041         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
15042                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
15043
15044         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
15045         rm -f $DIR/$tfile
15046 }
15047 run_test 248 "fast read verification"
15048
15049 test_249() { # LU-7890
15050         rm -f $DIR/$tfile
15051         $SETSTRIPE -c 1 $DIR/$tfile
15052
15053         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.53) ] &&
15054         skip "Need at least version 2.8.54"
15055
15056         # Offset 2T == 4k * 512M
15057         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
15058                 error "dd to 2T offset failed"
15059 }
15060 run_test 249 "Write above 2T file size"
15061
15062 test_250() {
15063         [ "$(facet_fstype ost$(($($GETSTRIPE -i $DIR/$tfile) + 1)))" = "zfs" ] \
15064          && skip "no 16TB file size limit on ZFS" && return
15065
15066         $SETSTRIPE -c 1 $DIR/$tfile
15067         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
15068         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
15069         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
15070         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
15071                 conv=notrunc,fsync && error "append succeeded"
15072         return 0
15073 }
15074 run_test 250 "Write above 16T limit"
15075
15076 test_251() {
15077         $SETSTRIPE -c -1 -S 1048576 $DIR/$tfile
15078
15079         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
15080         #Skip once - writing the first stripe will succeed
15081         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15082         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
15083                 error "short write happened"
15084
15085         $LCTL set_param fail_loc=0xa0001407 fail_val=1
15086         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
15087                 error "short read happened"
15088
15089         rm -f $DIR/$tfile
15090 }
15091 run_test 251 "Handling short read and write correctly"
15092
15093 test_252() {
15094         local tgt
15095         local dev
15096         local out
15097         local uuid
15098         local num
15099         local gen
15100
15101         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15102         remote_ost_nodsh && skip "remote OST with nodsh" && return
15103         if [ "$(facet_fstype ost1)" != "ldiskfs" -o \
15104              "$(facet_fstype mds1)" != "ldiskfs" ]; then
15105                 skip "ldiskfs only test"
15106                 return
15107         fi
15108
15109         # check lr_reader on OST0000
15110         tgt=ost1
15111         dev=$(facet_device $tgt)
15112         out=$(do_facet $tgt $LR_READER $dev)
15113         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15114         echo "$out"
15115         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
15116         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
15117                 error "Invalid uuid returned by $LR_READER on target $tgt"
15118         echo -e "uuid returned by $LR_READER is '$uuid'\n"
15119
15120         # check lr_reader -c on MDT0000
15121         tgt=mds1
15122         dev=$(facet_device $tgt)
15123         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
15124                 echo "$LR_READER does not support additional options"
15125                 return 0
15126         fi
15127         out=$(do_facet $tgt $LR_READER -c $dev)
15128         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15129         echo "$out"
15130         num=$(echo "$out" | grep -c "mdtlov")
15131         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
15132                 error "Invalid number of mdtlov clients returned by $LR_READER"
15133         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
15134
15135         # check lr_reader -cr on MDT0000
15136         out=$(do_facet $tgt $LR_READER -cr $dev)
15137         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
15138         echo "$out"
15139         echo "$out" | grep -q "^reply_data:$" ||
15140                 error "$LR_READER should have returned 'reply_data' section"
15141         num=$(echo "$out" | grep -c "client_generation")
15142         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
15143 }
15144 run_test 252 "check lr_reader tool"
15145
15146 test_253_fill_ost() {
15147         local size_mb #how many MB should we write to pass watermark
15148         local lwm=$3  #low watermark
15149         local free_10mb #10% of free space
15150
15151         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15152         size_mb=$((free_kb / 1024 - lwm))
15153         free_10mb=$((free_kb / 10240))
15154         #If 10% of free space cross low watermark use it
15155         if (( free_10mb > size_mb )); then
15156                 size_mb=$free_10mb
15157         else
15158                 #At least we need to store 1.1 of difference between
15159                 #free space and low watermark
15160                 size_mb=$((size_mb + size_mb / 10))
15161         fi
15162         if (( lwm <= $((free_kb / 1024)) )) || [ ! -f $DIR/$tdir/1 ]; then
15163                 dd if=/dev/zero of=$DIR/$tdir/1 bs=1M count=$size_mb \
15164                          oflag=append conv=notrunc
15165         fi
15166
15167         sleep_maxage
15168
15169         free_kb=$($LFS df $MOUNT | grep $1 | awk '{ print $4 }')
15170         echo "OST still has $((free_kb / 1024)) mbytes free"
15171 }
15172
15173 test_253() {
15174         local ostidx=0
15175         local rc=0
15176
15177         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15178         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15179         remote_mgs_nodsh && skip "remote MGS with nodsh" && return
15180
15181         local ost_name=$($LFS osts |
15182                 sed -n 's/^'$ostidx': \(.*\)_UUID .*/\1/p')
15183         # on the mdt's osc
15184         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
15185         do_facet $SINGLEMDS $LCTL get_param -n \
15186                 osp.$mdtosc_proc1.reserved_mb_high ||
15187                 { skip  "remote MDS does not support reserved_mb_high" &&
15188                   return; }
15189
15190         rm -rf $DIR/$tdir
15191         wait_mds_ost_sync
15192         wait_delete_completed
15193         mkdir $DIR/$tdir
15194
15195         local last_wm_h=$(do_facet $SINGLEMDS $LCTL get_param -n \
15196                         osp.$mdtosc_proc1.reserved_mb_high)
15197         local last_wm_l=$(do_facet $SINGLEMDS $LCTL get_param -n \
15198                         osp.$mdtosc_proc1.reserved_mb_low)
15199         echo "prev high watermark $last_wm_h, prev low watermark $last_wm_l"
15200
15201         if ! combined_mgs_mds ; then
15202                 mount_mgs_client
15203         fi
15204         create_pool $FSNAME.$TESTNAME || error "Pool creation failed"
15205         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $ost_name ||
15206                 error "Adding $ost_name to pool failed"
15207
15208         # Wait for client to see a OST at pool
15209         wait_update $HOSTNAME "$LCTL get_param -n
15210                 lov.$FSNAME-*.pools.$TESTNAME | sort -u |
15211                 grep $ost_name" "$ost_name""_UUID" $((TIMEOUT/2)) ||
15212                 error "Client can not see the pool"
15213         $SETSTRIPE $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
15214                 error "Setstripe failed"
15215
15216         dd if=/dev/zero of=$DIR/$tdir/0 bs=1M count=10
15217         local blocks=$($LFS df $MOUNT | grep $ost_name | awk '{ print $4 }')
15218         echo "OST still has $((blocks/1024)) mbytes free"
15219
15220         local new_lwm=$((blocks/1024-10))
15221         do_facet $SINGLEMDS $LCTL set_param \
15222                         osp.$mdtosc_proc1.reserved_mb_high=$((new_lwm+5))
15223         do_facet $SINGLEMDS $LCTL set_param \
15224                         osp.$mdtosc_proc1.reserved_mb_low=$new_lwm
15225
15226         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15227
15228         #First enospc could execute orphan deletion so repeat.
15229         test_253_fill_ost $ost_name $mdtosc_proc1 $new_lwm
15230
15231         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15232                         osp.$mdtosc_proc1.prealloc_status)
15233         echo "prealloc_status $oa_status"
15234
15235         dd if=/dev/zero of=$DIR/$tdir/2 bs=1M count=1 &&
15236                 error "File creation should fail"
15237         #object allocation was stopped, but we still able to append files
15238         dd if=/dev/zero of=$DIR/$tdir/1 bs=1M seek=6 count=5 oflag=append ||
15239                 error "Append failed"
15240         rm -f $DIR/$tdir/1 $DIR/$tdir/0 $DIR/$tdir/r*
15241
15242         wait_delete_completed
15243
15244         sleep_maxage
15245
15246         for i in $(seq 10 12); do
15247                 dd if=/dev/zero of=$DIR/$tdir/$i bs=1M count=1 2>/dev/null ||
15248                         error "File creation failed after rm";
15249         done
15250
15251         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
15252                         osp.$mdtosc_proc1.prealloc_status)
15253         echo "prealloc_status $oa_status"
15254
15255         if (( oa_status != 0 )); then
15256                 error "Object allocation still disable after rm"
15257         fi
15258         do_facet $SINGLEMDS $LCTL set_param \
15259                         osp.$mdtosc_proc1.reserved_mb_high=$last_wm_h
15260         do_facet $SINGLEMDS $LCTL set_param \
15261                         osp.$mdtosc_proc1.reserved_mb_low=$last_wm_l
15262
15263
15264         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $ost_name ||
15265                 error "Remove $ost_name from pool failed"
15266         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
15267                 error "Pool destroy fialed"
15268
15269         if ! combined_mgs_mds ; then
15270                 umount_mgs_client
15271         fi
15272 }
15273 run_test 253 "Check object allocation limit"
15274
15275 test_254() {
15276         local cl_user
15277
15278         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15279         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15280         do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size ||
15281                 { skip "MDS does not support changelog_size" && return; }
15282
15283         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15284         echo "Registered as changelog user $cl_user"
15285
15286         $LFS changelog_clear $MDT0 $cl_user 0
15287
15288         local size1=$(do_facet mds1 \
15289                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15290         echo "Changelog size $size1"
15291
15292         rm -rf $DIR/$tdir
15293         $LFS mkdir -i 0 $DIR/$tdir
15294         # change something
15295         mkdir -p $DIR/$tdir/pics/2008/zachy
15296         touch $DIR/$tdir/pics/2008/zachy/timestamp
15297         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
15298         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15299         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15300         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15301         rm $DIR/$tdir/pics/desktop.jpg
15302
15303         local size2=$(do_facet mds1 \
15304                       $LCTL get_param -n mdd.$MDT0.changelog_size)
15305         echo "Changelog size after work $size2"
15306
15307         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15308
15309         if (( size2 <= size1 )); then
15310                 error "Changelog size after work should be greater than original"
15311         fi
15312         return 0
15313 }
15314 run_test 254 "Check changelog size"
15315
15316 ladvise_no_type()
15317 {
15318         local type=$1
15319         local file=$2
15320
15321         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
15322                 awk -F: '{print $2}' | grep $type > /dev/null
15323         if [ $? -ne 0 ]; then
15324                 return 0
15325         fi
15326         return 1
15327 }
15328
15329 ladvise_no_ioctl()
15330 {
15331         local file=$1
15332
15333         lfs ladvise -a willread $file > /dev/null 2>&1
15334         if [ $? -eq 0 ]; then
15335                 return 1
15336         fi
15337
15338         lfs ladvise -a willread $file 2>&1 |
15339                 grep "Inappropriate ioctl for device" > /dev/null
15340         if [ $? -eq 0 ]; then
15341                 return 0
15342         fi
15343         return 1
15344 }
15345
15346 percent() {
15347         bc <<<"scale=2; ($1 - $2) * 100 / $2"
15348 }
15349
15350 # run a random read IO workload
15351 # usage: random_read_iops <filename> <filesize> <iosize>
15352 random_read_iops() {
15353         local file=$1
15354         local fsize=$2
15355         local iosize=${3:-4096}
15356
15357         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
15358                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
15359 }
15360
15361 drop_file_oss_cache() {
15362         local file="$1"
15363         local nodes="$2"
15364
15365         $LFS ladvise -a dontneed $file 2>/dev/null ||
15366                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
15367 }
15368
15369 ladvise_willread_performance()
15370 {
15371         local repeat=10
15372         local average_origin=0
15373         local average_cache=0
15374         local average_ladvise=0
15375
15376         for ((i = 1; i <= $repeat; i++)); do
15377                 echo "Iter $i/$repeat: reading without willread hint"
15378                 cancel_lru_locks osc
15379                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15380                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
15381                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
15382                 average_origin=$(bc <<<"$average_origin + $speed_origin")
15383
15384                 cancel_lru_locks osc
15385                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
15386                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
15387                 average_cache=$(bc <<<"$average_cache + $speed_cache")
15388
15389                 cancel_lru_locks osc
15390                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
15391                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
15392                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
15393                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
15394                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
15395         done
15396         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
15397         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
15398         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
15399
15400         speedup_cache=$(percent $average_cache $average_origin)
15401         speedup_ladvise=$(percent $average_ladvise $average_origin)
15402
15403         echo "Average uncached read: $average_origin"
15404         echo "Average speedup with OSS cached read: " \
15405                 "$average_cache = +$speedup_cache%"
15406         echo "Average speedup with ladvise willread: " \
15407                 "$average_ladvise = +$speedup_ladvise%"
15408
15409         local lowest_speedup=20
15410         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
15411                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
15412                         "got $average_cache%. Skipping ladvise willread check."
15413                 return 0
15414         fi
15415
15416         # the test won't work on ZFS until it supports 'ladvise dontneed', but
15417         # it is still good to run until then to exercise 'ladvise willread'
15418         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15419                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15420                 echo "osd-zfs does not support dontneed or drop_caches" &&
15421                 return 0
15422
15423         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
15424         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
15425                 error_not_in_vm "Speedup with willread is less than " \
15426                         "$lowest_speedup%, got $average_ladvise%"
15427 }
15428
15429 test_255a() {
15430         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15431                 skip "lustre < 2.8.54 does not support ladvise " && return
15432         remote_ost_nodsh && skip "remote OST with nodsh" && return
15433
15434         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
15435
15436         ladvise_no_type willread $DIR/$tfile &&
15437                 skip "willread ladvise is not supported" && return
15438
15439         ladvise_no_ioctl $DIR/$tfile &&
15440                 skip "ladvise ioctl is not supported" && return
15441
15442         local size_mb=100
15443         local size=$((size_mb * 1048576))
15444         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15445                 error "dd to $DIR/$tfile failed"
15446
15447         lfs ladvise -a willread $DIR/$tfile ||
15448                 error "Ladvise failed with no range argument"
15449
15450         lfs ladvise -a willread -s 0 $DIR/$tfile ||
15451                 error "Ladvise failed with no -l or -e argument"
15452
15453         lfs ladvise -a willread -e 1 $DIR/$tfile ||
15454                 error "Ladvise failed with only -e argument"
15455
15456         lfs ladvise -a willread -l 1 $DIR/$tfile ||
15457                 error "Ladvise failed with only -l argument"
15458
15459         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
15460                 error "End offset should not be smaller than start offset"
15461
15462         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
15463                 error "End offset should not be equal to start offset"
15464
15465         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
15466                 error "Ladvise failed with overflowing -s argument"
15467
15468         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
15469                 error "Ladvise failed with overflowing -e argument"
15470
15471         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
15472                 error "Ladvise failed with overflowing -l argument"
15473
15474         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
15475                 error "Ladvise succeeded with conflicting -l and -e arguments"
15476
15477         echo "Synchronous ladvise should wait"
15478         local delay=4
15479 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
15480         do_nodes $(comma_list $(osts_nodes)) \
15481                 $LCTL set_param fail_val=$delay fail_loc=0x237
15482
15483         local start_ts=$SECONDS
15484         lfs ladvise -a willread $DIR/$tfile ||
15485                 error "Ladvise failed with no range argument"
15486         local end_ts=$SECONDS
15487         local inteval_ts=$((end_ts - start_ts))
15488
15489         if [ $inteval_ts -lt $(($delay - 1)) ]; then
15490                 error "Synchronous advice didn't wait reply"
15491         fi
15492
15493         echo "Asynchronous ladvise shouldn't wait"
15494         local start_ts=$SECONDS
15495         lfs ladvise -a willread -b $DIR/$tfile ||
15496                 error "Ladvise failed with no range argument"
15497         local end_ts=$SECONDS
15498         local inteval_ts=$((end_ts - start_ts))
15499
15500         if [ $inteval_ts -gt $(($delay / 2)) ]; then
15501                 error "Asynchronous advice blocked"
15502         fi
15503
15504         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
15505         ladvise_willread_performance
15506 }
15507 run_test 255a "check 'lfs ladvise -a willread'"
15508
15509 facet_meminfo() {
15510         local facet=$1
15511         local info=$2
15512
15513         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
15514 }
15515
15516 test_255b() {
15517         remote_ost_nodsh && skip "remote OST with nodsh" && return
15518
15519         lfs setstripe -c 1 -i 0 $DIR/$tfile
15520
15521         ladvise_no_type dontneed $DIR/$tfile &&
15522                 skip "dontneed ladvise is not supported" && return
15523
15524         ladvise_no_ioctl $DIR/$tfile &&
15525                 skip "ladvise ioctl is not supported" && return
15526
15527         [ $(lustre_version_code ost1) -lt $(version_code 2.8.54) ] &&
15528                 skip "lustre < 2.8.54 does not support ladvise" && return
15529
15530         ! $LFS ladvise -a dontneed $DIR/$tfile &&
15531                 [ "$(facet_fstype ost1)" = "zfs" ] &&
15532                 skip "zfs-osd does not support 'ladvise dontneed'" && return
15533
15534         local size_mb=100
15535         local size=$((size_mb * 1048576))
15536         # In order to prevent disturbance of other processes, only check 3/4
15537         # of the memory usage
15538         local kibibytes=$((size_mb * 1024 * 3 / 4))
15539
15540         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
15541                 error "dd to $DIR/$tfile failed"
15542
15543         local total=$(facet_meminfo ost1 MemTotal)
15544         echo "Total memory: $total KiB"
15545
15546         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
15547         local before_read=$(facet_meminfo ost1 Cached)
15548         echo "Cache used before read: $before_read KiB"
15549
15550         lfs ladvise -a willread $DIR/$tfile ||
15551                 error "Ladvise willread failed"
15552         local after_read=$(facet_meminfo ost1 Cached)
15553         echo "Cache used after read: $after_read KiB"
15554
15555         lfs ladvise -a dontneed $DIR/$tfile ||
15556                 error "Ladvise dontneed again failed"
15557         local no_read=$(facet_meminfo ost1 Cached)
15558         echo "Cache used after dontneed ladvise: $no_read KiB"
15559
15560         if [ $total -lt $((before_read + kibibytes)) ]; then
15561                 echo "Memory is too small, abort checking"
15562                 return 0
15563         fi
15564
15565         if [ $((before_read + kibibytes)) -gt $after_read ]; then
15566                 error "Ladvise willread should use more memory" \
15567                         "than $kibibytes KiB"
15568         fi
15569
15570         if [ $((no_read + kibibytes)) -gt $after_read ]; then
15571                 error "Ladvise dontneed should release more memory" \
15572                         "than $kibibytes KiB"
15573         fi
15574 }
15575 run_test 255b "check 'lfs ladvise -a dontneed'"
15576
15577 test_255c() {
15578         local count
15579         local new_count
15580         local difference
15581         local i
15582         local rc
15583
15584         [ $(lustre_version_code ost1) -lt $(version_code 2.10.50) ] &&
15585                 skip "lustre < 2.10.53 does not support lockahead" && return
15586
15587         test_mkdir -p $DIR/$tdir
15588         $SETSTRIPE -i 0 $DIR/$tdir
15589
15590         #test 10 returns only success/failure
15591         i=10
15592         lockahead_test -d $DIR/$tdir -t $i
15593         rc=$?
15594         if [ $rc -eq 255 ]; then
15595                 error "Ladvise test${i} failed, ${rc}"
15596         fi
15597
15598         #test 11 counts lock enqueue requests, all others count new locks
15599         i=11
15600         count=$(do_facet ost1 \
15601                 $LCTL get_param -n ost.OSS.ost.stats)
15602         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
15603
15604         lockahead_test -d $DIR/$tdir -t $i
15605         rc=$?
15606         if [ $rc -eq 255 ]; then
15607                 error "Ladvise test${i} failed, ${rc}"
15608         fi
15609
15610         new_count=$(do_facet ost1 \
15611                 $LCTL get_param -n ost.OSS.ost.stats)
15612         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
15613                    awk '{ print $2 }')
15614
15615         difference="$((new_count - count))"
15616         if [ $difference -ne $rc ]; then
15617                 error "Ladvise test${i}, bad enqueue count, returned " \
15618                       "${rc}, actual ${difference}"
15619         fi
15620
15621         for i in $(seq 12 21); do
15622                 # If we do not do this, we run the risk of having too many
15623                 # locks and starting lock cancellation while we are checking
15624                 # lock counts.
15625                 cancel_lru_locks osc
15626
15627                 count=$($LCTL get_param -n \
15628                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15629
15630                 lockahead_test -d $DIR/$tdir -t $i
15631                 rc=$?
15632                 if [ $rc -eq 255 ]; then
15633                         error "Ladvise test ${i} failed, ${rc}"
15634                 fi
15635
15636                 new_count=$($LCTL get_param -n \
15637                        ldlm.namespaces.$FSNAME-OST0000*osc-f*.lock_unused_count)
15638                 difference="$((new_count - count))"
15639
15640                 # Test 15 output is divided by 100 to map down to valid return
15641                 if [ $i -eq 15 ]; then
15642                         rc="$((rc * 100))"
15643                 fi
15644
15645                 if [ $difference -ne $rc ]; then
15646                         error "Ladvise test ${i}, bad lock count, returned " \
15647                               "${rc}, actual ${difference}"
15648                 fi
15649         done
15650
15651         #test 22 returns only success/failure
15652         i=22
15653         lockahead_test -d $DIR/$tdir -t $i
15654         rc=$?
15655         if [ $rc -eq 255 ]; then
15656                 error "Ladvise test${i} failed, ${rc}"
15657         fi
15658
15659 }
15660 run_test 255c "suite of ladvise lockahead tests"
15661
15662 test_256() {
15663         local cl_user
15664         local cat_sl
15665         local mdt_dev
15666
15667         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
15668         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15669         [ "$(facet_fstype mds1)" != "ldiskfs" ] &&
15670                 skip "ldiskfs only test" && return
15671
15672         mdt_dev=$(mdsdevname 1)
15673         echo $mdt_dev
15674         cl_user=$(do_facet mds1 \
15675         "$LCTL get_param -n mdd.$MDT0.changelog_users | grep cl")
15676         if [[ -n $cl_user ]]; then
15677                 skip "active changelog user"
15678                 return
15679         fi
15680
15681         cl_user=$(do_facet mds1 $LCTL --device $MDT0 changelog_register -n)
15682         echo "Registered as changelog user $cl_user"
15683
15684         rm -rf $DIR/$tdir
15685         mkdir -p $DIR/$tdir
15686
15687         $LFS changelog_clear $MDT0 $cl_user 0
15688
15689         # change something
15690         touch $DIR/$tdir/{1..10}
15691
15692         # stop the MDT
15693         stop mds1 || error "Fail to stop MDT."
15694
15695         # remount the MDT
15696         start mds1 $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT."
15697
15698         #after mount new plainllog is used
15699         touch $DIR/$tdir/{11..19}
15700         do_facet mds1 sync
15701         local TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15702         cat_sl=$(do_facet mds1 \
15703         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15704          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15705         do_facet mds1 rm $TEMP256FILE
15706
15707         if (( cat_sl != 2 )); then
15708                 do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15709                 error "Changelog catalog has wrong number of slots $cat_sl"
15710         fi
15711
15712         $LFS changelog_clear $MDT0 $cl_user 0
15713
15714         do_facet mds1 sync
15715         TEMP256FILE=$(mktemp -u TEMP256XXXXXX)
15716         cat_sl=$(do_facet mds1 \
15717         "$DEBUGFS -R \\\"dump changelog_catalog $TEMP256FILE\\\" $mdt_dev; \
15718          llog_reader $TEMP256FILE | grep \\\"type=1064553b\\\" | wc -l")
15719         do_facet mds1 rm $TEMP256FILE
15720
15721         do_facet mds1 $LCTL --device $MDT0 changelog_deregister $cl_user
15722
15723         if (( cat_sl == 2 )); then
15724                 error "Empty plain llog was not deleted from changelog catalog"
15725         fi
15726         if (( cat_sl != 1 )); then
15727                 error "Active plain llog shouldn\`t be deleted from catalog"
15728         fi
15729 }
15730 run_test 256 "Check llog delete for empty and not full state"
15731
15732 test_257() {
15733         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15734         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
15735                 skip "Need MDS version at least 2.8.55" && return
15736
15737         test_mkdir $DIR/$tdir
15738
15739         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
15740                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
15741         stat $DIR/$tdir
15742
15743 #define OBD_FAIL_MDS_XATTR_REP                  0x161
15744         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
15745         local facet=mds$((mdtidx + 1))
15746         set_nodes_failloc $(facet_active_host $facet) 0x80000161
15747         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
15748
15749         stop $facet || error "stop MDS failed"
15750         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
15751                 error "start MDS fail"
15752 }
15753 run_test 257 "xattr locks are not lost"
15754
15755 # Verify we take the i_mutex when security requires it
15756 test_258a() {
15757 #define OBD_FAIL_IMUTEX_SEC 0x141c
15758         $LCTL set_param fail_loc=0x141c
15759         touch $DIR/$tfile
15760         chmod u+s $DIR/$tfile
15761         chmod a+rwx $DIR/$tfile
15762         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15763         RC=$?
15764         if [ $RC -ne 0 ]; then
15765                 error "error, failed to take i_mutex, rc=$?"
15766         fi
15767         rm -f $DIR/$tfile
15768 }
15769 run_test 258a
15770
15771 # Verify we do NOT take the i_mutex in the normal case
15772 test_258b() {
15773 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
15774         $LCTL set_param fail_loc=0x141d
15775         touch $DIR/$tfile
15776         chmod a+rwx $DIR
15777         chmod a+rw $DIR/$tfile
15778         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
15779         RC=$?
15780         if [ $RC -ne 0 ]; then
15781                 error "error, took i_mutex unnecessarily, rc=$?"
15782         fi
15783         rm -f $DIR/$tfile
15784
15785 }
15786 run_test 258b "verify i_mutex security behavior"
15787
15788 test_260() {
15789 #define OBD_FAIL_MDC_CLOSE               0x806
15790         $LCTL set_param fail_loc=0x80000806
15791         touch $DIR/$tfile
15792
15793 }
15794 run_test 260 "Check mdc_close fail"
15795
15796 ### Data-on-MDT sanity tests ###
15797 test_270a() {
15798
15799         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15800                 skip "Need MDS version at least 2.10.55" && return
15801
15802         # create DoM file
15803         local dom=$DIR/$tdir/dom_file
15804         local tmp=$DIR/$tdir/tmp_file
15805
15806         mkdir -p $DIR/$tdir
15807
15808         # basic checks for DoM component creation
15809         $LFS setstripe -E 1024K -E 1024K -L mdt $dom 2>/dev/null &&
15810                 error "Can set MDT layout to non-first entry"
15811
15812         $LFS setstripe -E 1024K -L mdt -E 1024K -L mdt $dom 2>/dev/null &&
15813                 error "Can define multiple entries as MDT layout"
15814
15815         $LFS setstripe -E 1M -L mdt $dom ||
15816                 error "Can't create DoM layout"
15817
15818         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
15819         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
15820         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
15821
15822         local mdtidx=$($GETSTRIPE -M $dom)
15823         local mdtname=MDT$(printf %04x $mdtidx)
15824         local facet=mds$((mdtidx + 1))
15825         local space_check=1
15826
15827         # Skip free space checks with ZFS
15828         if [ "$(facet_fstype $facet)" == "zfs" ]; then
15829                 space_check=0
15830         fi
15831
15832         # write
15833         sync
15834         local mdtfree1=$(do_facet $facet \
15835                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15836         dd if=/dev/urandom of=$tmp bs=1024 count=100
15837         # check also direct IO along write
15838         dd if=$tmp of=$dom bs=102400 count=1 oflag=direct
15839         sync
15840         cmp $tmp $dom || error "file data is different"
15841         [ $(stat -c%s $dom) == 102400 ] || error "bad size after write"
15842         if [ $space_check == 1 ]; then
15843                 local 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 write"
15847         fi
15848
15849         # truncate
15850         $TRUNCATE $dom 10000
15851         [ $(stat -c%s $dom) == 10000 ] || error "bad size after truncate"
15852         if [ $space_check == 1 ]; then
15853                 mdtfree1=$(do_facet $facet \
15854                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15855                 [ $(($mdtfree1 - $mdtfree2)) -ge 92 ] ||
15856                         error "MDT free space is wrong after truncate"
15857         fi
15858
15859         # append
15860         cat $tmp >> $dom
15861         sync
15862         [ $(stat -c%s $dom) == 112400 ] || error "bad size after append"
15863         if [ $space_check == 1 ]; then
15864                 mdtfree2=$(do_facet $facet \
15865                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15866                 [ $(($mdtfree1 - $mdtfree2)) -ge 102 ] ||
15867                         error "MDT free space is wrong after append"
15868         fi
15869
15870         # delete
15871         rm $dom
15872         if [ $space_check == 1 ]; then
15873                 mdtfree1=$(do_facet $facet \
15874                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
15875                 [ $(($mdtfree1 - $mdtfree2)) -ge 112 ] ||
15876                         error "MDT free space is wrong after removal"
15877         fi
15878
15879         # combined striping
15880         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
15881                 error "Can't create DoM + OST striping"
15882
15883         dd if=/dev/urandom of=$tmp bs=1024 count=2000
15884         # check also direct IO along write
15885         dd if=$tmp of=$dom bs=102400 count=20 oflag=direct
15886         sync
15887         cmp $tmp $dom || error "file data is different"
15888         [ $(stat -c%s $dom) == 2048000 ] || error "bad size after write"
15889         rm $dom
15890         rm $tmp
15891
15892         return 0
15893 }
15894 run_test 270a "DoM: basic functionality tests"
15895
15896 test_270b() {
15897         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15898                 skip "Need MDS version at least 2.10.55" && return
15899
15900         local dom=$DIR/$tdir/dom_file
15901         local max_size=1048576
15902
15903         mkdir -p $DIR/$tdir
15904         $LFS setstripe -E $max_size -L mdt $dom
15905
15906         # truncate over the limit
15907         $TRUNCATE $dom $(($max_size + 1)) &&
15908                 error "successful truncate over the maximum size"
15909         # write over the limit
15910         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
15911                 error "successful write over the maximum size"
15912         # append over the limit
15913         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
15914         echo "12345" >> $dom && error "successful append over the maximum size"
15915         rm $dom
15916
15917         return 0
15918 }
15919 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
15920
15921 test_270c() {
15922         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15923                 skip "Need MDS version at least 2.10.55" && return
15924
15925         mkdir -p $DIR/$tdir
15926         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15927
15928         # check files inherit DoM EA
15929         touch $DIR/$tdir/first
15930         [ $($GETSTRIPE -L $DIR/$tdir/first) == "mdt" ] ||
15931                 error "bad pattern"
15932         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
15933                 error "bad stripe count"
15934         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
15935                 error "bad stripe size"
15936
15937         # check directory inherits DoM EA and uses it as default
15938         mkdir $DIR/$tdir/subdir
15939         touch $DIR/$tdir/subdir/second
15940         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
15941                 error "bad pattern in sub-directory"
15942         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
15943                 error "bad stripe count in sub-directory"
15944         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
15945                 error "bad stripe size in sub-directory"
15946         return 0
15947 }
15948 run_test 270c "DoM: DoM EA inheritance tests"
15949
15950 test_270d() {
15951         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15952                 skip "Need MDS version at least 2.10.55" && return
15953
15954         mkdir -p $DIR/$tdir
15955         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
15956
15957         # inherit default DoM striping
15958         mkdir $DIR/$tdir/subdir
15959         touch $DIR/$tdir/subdir/f1
15960
15961         # change default directory striping
15962         $LFS setstripe -c 1 $DIR/$tdir/subdir
15963         touch $DIR/$tdir/subdir/f2
15964         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
15965                 error "wrong default striping in file 2"
15966         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
15967                 error "bad pattern in file 2"
15968         return 0
15969 }
15970 run_test 270d "DoM: change striping from DoM to RAID0"
15971
15972 test_270e() {
15973         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
15974                 skip "Need MDS version at least 2.10.55" && return
15975
15976         mkdir -p $DIR/$tdir/dom
15977         mkdir -p $DIR/$tdir/norm
15978         DOMFILES=20
15979         NORMFILES=10
15980         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
15981         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
15982
15983         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
15984         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
15985
15986         # find DoM files by layout
15987         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
15988         [ $NUM -eq  $DOMFILES ] ||
15989                 error "lfs find -L: found $NUM, expected $DOMFILES"
15990         echo "Test 1: lfs find 20 DOM files by layout: OK"
15991
15992         # there should be 1 dir with default DOM striping
15993         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
15994         [ $NUM -eq  1 ] ||
15995                 error "lfs find -L: found $NUM, expected 1 dir"
15996         echo "Test 2: lfs find 1 DOM dir by layout: OK"
15997
15998         # find DoM files by stripe size
15999         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
16000         [ $NUM -eq  $DOMFILES ] ||
16001                 error "lfs find -S: found $NUM, expected $DOMFILES"
16002         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
16003
16004         # find files by stripe offset except DoM files
16005         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
16006         [ $NUM -eq  $NORMFILES ] ||
16007                 error "lfs find -i: found $NUM, expected $NORMFILES"
16008         echo "Test 5: lfs find no DOM files by stripe index: OK"
16009         return 0
16010 }
16011 run_test 270e "DoM: lfs find with DoM files test"
16012
16013 test_270f() {
16014         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16015                 skip "Need MDS version at least 2.10.55" && return
16016
16017         local mdtname=${FSNAME}-MDT0000-mdtlov
16018         local dom=$DIR/$tdir/dom_file
16019         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
16020                                                 lod.$mdtname.dom_stripesize)
16021         local dom_limit=131072
16022
16023         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
16024         local dom_current=$(do_facet mds1 $LCTL get_param -n \
16025                                                 lod.$mdtname.dom_stripesize)
16026         [ ${dom_limit} -eq ${dom_current} ] ||
16027                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
16028
16029         $LFS mkdir -i 0 -c 1 $DIR/$tdir
16030         $LFS setstripe -d $DIR/$tdir
16031         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
16032                 error "Can't set directory default striping"
16033
16034         # exceed maximum stripe size
16035         $LFS setstripe -E $(($dom_limit * 2)) -L mdt $dom &&
16036                 error "Able to create DoM component size more than LOD limit"
16037
16038         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
16039         dom_current=$(do_facet mds1 $LCTL get_param -n \
16040                                                 lod.$mdtname.dom_stripesize)
16041         [ 0 -eq ${dom_current} ] ||
16042                 error "Can't set zero DoM stripe limit"
16043
16044         # too low values to be aligned with smallest stripe size 64K
16045         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
16046         dom_current=$(do_facet mds1 $LCTL get_param -n \
16047                                                 lod.$mdtname.dom_stripesize)
16048         [ 30000 -eq ${dom_current} ] &&
16049                 error "Can set too small DoM stripe limit"
16050
16051         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
16052         dom_current=$(do_facet mds1 $LCTL get_param -n \
16053                                                 lod.$mdtname.dom_stripesize)
16054         echo $dom_current
16055         [ 2147483648 -eq ${dom_current} ] &&
16056                 error "Can set too large DoM stripe limit"
16057
16058         do_facet mds1 $LCTL set_param -n \
16059                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
16060         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
16061                 error "Can't create DoM component size after limit change"
16062         do_facet mds1 $LCTL set_param -n \
16063                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
16064         $LFS setstripe -E $dom_limit -L mdt ${dom}_big &&
16065                 error "Can create big DoM component after limit decrease"
16066         touch ${dom}_def ||
16067                 error "Can't create file with old default layout"
16068
16069         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
16070         return 0
16071 }
16072 run_test 270f "DoM: maximum DoM stripe size checks"
16073
16074 test_271a() {
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 $dom
16083
16084         lctl set_param -n mdc.*.stats=clear
16085         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16086         cat $dom > /dev/null
16087         local reads=$(lctl get_param -n mdc.*.stats |
16088                         awk '/ost_read/ {print $2}')
16089         [ -z $reads ] || error "Unexpected $reads READ RPCs"
16090         ls $dom
16091         rm -f $dom
16092 }
16093 run_test 271a "DoM: data is cached for read after write"
16094
16095 test_271b() {
16096         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16097                 skip "Need MDS version at least 2.10.55" && return
16098
16099         local dom=$DIR/$tdir/dom
16100
16101         mkdir -p $DIR/$tdir
16102
16103         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16104
16105         lctl set_param -n mdc.*.stats=clear
16106         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
16107         cancel_lru_locks mdc
16108         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
16109         # second stat to check size is cached on client
16110         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
16111         local gls=$(lctl get_param -n mdc.*.stats |
16112                         awk '/ldlm_glimpse/ {print $2}')
16113         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16114         rm -f $dom
16115 }
16116 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
16117
16118 test_271ba() {
16119         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16120                 skip "Need MDS version at least 2.10.55" && return
16121
16122         local dom=$DIR/$tdir/dom
16123
16124         mkdir -p $DIR/$tdir
16125
16126         $LFS setstripe -E 1024K -L mdt -E EOF $dom
16127
16128         lctl set_param -n mdc.*.stats=clear
16129         lctl set_param -n osc.*.stats=clear
16130         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
16131         cancel_lru_locks mdc
16132         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16133         # second stat to check size is cached on client
16134         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
16135         local gls=$(lctl get_param -n mdc.*.stats |
16136                         awk '/ldlm_glimpse/ {print $2}')
16137         [ -z $gls ] || error "Unexpected $gls glimpse RPCs"
16138         local gls=$(lctl get_param -n osc.*.stats |
16139                         awk '/ldlm_glimpse/ {print $2}')
16140         [ -z $gls ] || error "Unexpected $gls OSC glimpse RPCs"
16141         rm -f $dom
16142 }
16143 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
16144
16145 test_271c() {
16146         # test to be enabled with lock_convert
16147         skip "skipped until lock convert will be implemented" && return
16148
16149         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.55) ] &&
16150                 skip "Need MDS version at least 2.10.55" && return
16151
16152         local dom=$DIR/$tdir/dom
16153
16154         mkdir -p $DIR/$tdir
16155
16156         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
16157
16158         local mdtidx=$($LFS getstripe -M $DIR/$tdir)
16159         local facet=mds$((mdtidx + 1))
16160
16161         cancel_lru_locks mdc
16162         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
16163         createmany -o $dom 1000
16164         lctl set_param -n mdc.*.stats=clear
16165         smalliomany -w $dom 1000 200
16166         lctl get_param -n mdc.*.stats
16167         local enq=$(lctl get_param -n mdc.*.stats |
16168                         awk '/ldlm_ibits_enqueue/ {print $2}')
16169         # Each file has 1 open, 1 IO enqueues, total 2000
16170         # but now we have also +1 getxattr for security.capability, total 3000
16171         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
16172         unlinkmany $dom 1000
16173
16174         cancel_lru_locks mdc
16175         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
16176         createmany -o $dom 1000
16177         lctl set_param -n mdc.*.stats=clear
16178         smalliomany -w $dom 1000 200
16179         lctl get_param -n mdc.*.stats
16180         local enq_2=$(lctl get_param -n mdc.*.stats |
16181                         awk '/ldlm_ibits_enqueue/ {print $2}')
16182         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
16183         # for OPEN and IO lock.
16184         [ $((enq - enq_2)) -ge 1000 ] ||
16185                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
16186         unlinkmany $dom 1000
16187         return 0
16188 }
16189 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
16190
16191 cleanup_test_300() {
16192         trap 0
16193         umask $SAVE_UMASK
16194 }
16195 test_striped_dir() {
16196         local mdt_index=$1
16197         local stripe_count
16198         local stripe_index
16199
16200         mkdir -p $DIR/$tdir
16201
16202         SAVE_UMASK=$(umask)
16203         trap cleanup_test_300 RETURN EXIT
16204
16205         $LFS setdirstripe -i $mdt_index -c 2 -t all_char -m 755 \
16206                                                 $DIR/$tdir/striped_dir ||
16207                 error "set striped dir error"
16208
16209         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
16210         [ "$mode" = "755" ] || error "expect 755 got $mode"
16211
16212         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
16213                 error "getdirstripe failed"
16214         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
16215         if [ "$stripe_count" != "2" ]; then
16216                 error "1:stripe_count is $stripe_count, expect 2"
16217         fi
16218         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
16219         if [ "$stripe_count" != "2" ]; then
16220                 error "2:stripe_count is $stripe_count, expect 2"
16221         fi
16222
16223         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
16224         if [ "$stripe_index" != "$mdt_index" ]; then
16225                 error "stripe_index is $stripe_index, expect $mdt_index"
16226         fi
16227
16228         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
16229                 error "nlink error after create striped dir"
16230
16231         mkdir $DIR/$tdir/striped_dir/a
16232         mkdir $DIR/$tdir/striped_dir/b
16233
16234         stat $DIR/$tdir/striped_dir/a ||
16235                 error "create dir under striped dir failed"
16236         stat $DIR/$tdir/striped_dir/b ||
16237                 error "create dir under striped dir failed"
16238
16239         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
16240                 error "nlink error after mkdir"
16241
16242         rmdir $DIR/$tdir/striped_dir/a
16243         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
16244                 error "nlink error after rmdir"
16245
16246         rmdir $DIR/$tdir/striped_dir/b
16247         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
16248                 error "nlink error after rmdir"
16249
16250         chattr +i $DIR/$tdir/striped_dir
16251         createmany -o $DIR/$tdir/striped_dir/f 10 &&
16252                 error "immutable flags not working under striped dir!"
16253         chattr -i $DIR/$tdir/striped_dir
16254
16255         rmdir $DIR/$tdir/striped_dir ||
16256                 error "rmdir striped dir error"
16257
16258         cleanup_test_300
16259
16260         true
16261 }
16262
16263 test_300a() {
16264         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16265                 skip "skipped for lustre < 2.7.0" && return
16266         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16267         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16268
16269         test_striped_dir 0 || error "failed on striped dir on MDT0"
16270         test_striped_dir 1 || error "failed on striped dir on MDT0"
16271 }
16272 run_test 300a "basic striped dir sanity test"
16273
16274 test_300b() {
16275         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16276                 skip "skipped for lustre < 2.7.0" && return
16277         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16278         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16279         local i
16280         local mtime1
16281         local mtime2
16282         local mtime3
16283
16284         test_mkdir $DIR/$tdir
16285         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16286                 error "set striped dir error"
16287         for ((i=0; i<10; i++)); do
16288                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
16289                 sleep 1
16290                 touch $DIR/$tdir/striped_dir/file_$i ||
16291                                         error "touch error $i"
16292                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
16293                 [ $mtime1 -eq $mtime2 ] &&
16294                         error "mtime not change after create"
16295                 sleep 1
16296                 rm -f $DIR/$tdir/striped_dir/file_$i ||
16297                                         error "unlink error $i"
16298                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
16299                 [ $mtime2 -eq $mtime3 ] &&
16300                         error "mtime did not change after unlink"
16301         done
16302         true
16303 }
16304 run_test 300b "check ctime/mtime for striped dir"
16305
16306 test_300c() {
16307         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16308                 skip "skipped for lustre < 2.7.0" && return
16309         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16310         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16311         local file_count
16312
16313         mkdir -p $DIR/$tdir
16314         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
16315                 error "set striped dir error"
16316
16317         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
16318                 error "chown striped dir failed"
16319
16320         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
16321                 error "create 5k files failed"
16322
16323         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
16324
16325         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
16326
16327         rm -rf $DIR/$tdir
16328 }
16329 run_test 300c "chown && check ls under striped directory"
16330
16331 test_300d() {
16332         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.0) ] &&
16333                 skip "skipped for lustre < 2.7.0" && return
16334         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16335         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16336         local stripe_count
16337         local file
16338
16339         mkdir -p $DIR/$tdir
16340         $SETSTRIPE -c 2 $DIR/$tdir
16341
16342         #local striped directory
16343         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16344                 error "set striped dir error"
16345         createmany -o $DIR/$tdir/striped_dir/f 10 ||
16346                 error "create 10 files failed"
16347
16348         #remote striped directory
16349         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
16350                 error "set striped dir error"
16351         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
16352                 error "create 10 files failed"
16353
16354         for file in $(find $DIR/$tdir); do
16355                 stripe_count=$($GETSTRIPE -c $file)
16356                 [ $stripe_count -eq 2 ] ||
16357                         error "wrong stripe $stripe_count for $file"
16358         done
16359
16360         rm -rf $DIR/$tdir
16361 }
16362 run_test 300d "check default stripe under striped directory"
16363
16364 test_300e() {
16365         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16366                 skip "Need MDS version at least 2.7.55" && return
16367         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16368         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16369         local stripe_count
16370         local file
16371
16372         mkdir -p $DIR/$tdir
16373
16374         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16375                 error "set striped dir error"
16376
16377         touch $DIR/$tdir/striped_dir/a
16378         touch $DIR/$tdir/striped_dir/b
16379         touch $DIR/$tdir/striped_dir/c
16380
16381         mkdir $DIR/$tdir/striped_dir/dir_a
16382         mkdir $DIR/$tdir/striped_dir/dir_b
16383         mkdir $DIR/$tdir/striped_dir/dir_c
16384
16385         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_a ||
16386                 error "set striped adir under striped dir error"
16387
16388         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
16389                 error "set striped bdir under striped dir error"
16390
16391         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir/stp_c ||
16392                 error "set striped cdir under striped dir error"
16393
16394         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
16395                 error "rename dir under striped dir fails"
16396
16397         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
16398                 error "rename dir under different stripes fails"
16399
16400         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
16401                 error "rename file under striped dir should succeed"
16402
16403         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
16404                 error "rename dir under striped dir should succeed"
16405
16406         rm -rf $DIR/$tdir
16407 }
16408 run_test 300e "check rename under striped directory"
16409
16410 test_300f() {
16411         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16412                 skip "Need MDS version at least 2.7.55" && return
16413         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16414         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16415         local stripe_count
16416         local file
16417
16418         rm -rf $DIR/$tdir
16419         mkdir -p $DIR/$tdir
16420
16421         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir ||
16422                 error "set striped dir error"
16423
16424         $LFS setdirstripe -i 0 -c 2 -t all_char $DIR/$tdir/striped_dir1 ||
16425                 error "set striped dir error"
16426
16427         touch $DIR/$tdir/striped_dir/a
16428         mkdir $DIR/$tdir/striped_dir/dir_a
16429         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
16430                 error "create striped dir under striped dir fails"
16431
16432         touch $DIR/$tdir/striped_dir1/b
16433         mkdir $DIR/$tdir/striped_dir1/dir_b
16434         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
16435                 error "create striped dir under striped dir fails"
16436
16437         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
16438                 error "rename dir under different striped dir should fail"
16439
16440         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
16441                 error "rename striped dir under diff striped dir should fail"
16442
16443         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
16444                 error "rename file under diff striped dirs fails"
16445
16446         rm -rf $DIR/$tdir
16447 }
16448 run_test 300f "check rename cross striped directory"
16449
16450 test_300_check_default_striped_dir()
16451 {
16452         local dirname=$1
16453         local default_count=$2
16454         local default_index=$3
16455         local stripe_count
16456         local stripe_index
16457         local dir_stripe_index
16458         local dir
16459
16460         echo "checking $dirname $default_count $default_index"
16461         $LFS setdirstripe -D -c $default_count -i $default_index \
16462                                 -t all_char $DIR/$tdir/$dirname ||
16463                 error "set default stripe on striped dir error"
16464         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
16465         [ $stripe_count -eq $default_count ] ||
16466                 error "expect $default_count get $stripe_count for $dirname"
16467
16468         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
16469         [ $stripe_index -eq $default_index ] ||
16470                 error "expect $default_index get $stripe_index for $dirname"
16471
16472         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
16473                                                 error "create dirs failed"
16474
16475         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
16476         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
16477         for dir in $(find $DIR/$tdir/$dirname/*); do
16478                 stripe_count=$($LFS getdirstripe -c $dir)
16479                 [ $stripe_count -eq $default_count ] ||
16480                 [ $stripe_count -eq 0 -o $default_count -eq 1 ] ||
16481                 error "stripe count $default_count != $stripe_count for $dir"
16482
16483                 stripe_index=$($LFS getdirstripe -i $dir)
16484                 [ $default_index -eq -1 -o $stripe_index -eq $default_index ] ||
16485                         error "$stripe_index != $default_index for $dir"
16486
16487                 #check default stripe
16488                 stripe_count=$($LFS getdirstripe -D -c $dir)
16489                 [ $stripe_count -eq $default_count ] ||
16490                 error "default count $default_count != $stripe_count for $dir"
16491
16492                 stripe_index=$($LFS getdirstripe -D -i $dir)
16493                 [ $stripe_index -eq $default_index ] ||
16494                 error "default index $default_index != $stripe_index for $dir"
16495         done
16496         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
16497 }
16498
16499 test_300g() {
16500         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16501                 skip "Need MDS version at least 2.7.55" && return
16502         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16503         local dir
16504         local stripe_count
16505         local stripe_index
16506
16507         mkdir $DIR/$tdir
16508         mkdir $DIR/$tdir/normal_dir
16509
16510         #Checking when client cache stripe index
16511         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
16512         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
16513                 error "create striped_dir failed"
16514
16515         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
16516                 error "create dir0 fails"
16517         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
16518         [ $stripe_index -eq 0 ] ||
16519                 error "dir0 expect index 0 got $stripe_index"
16520
16521         mkdir $DIR/$tdir/striped_dir/dir1 ||
16522                 error "create dir1 fails"
16523         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
16524         [ $stripe_index -eq 1 ] ||
16525                 error "dir1 expect index 1 got $stripe_index"
16526
16527         #check default stripe count/stripe index
16528         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
16529         test_300_check_default_striped_dir normal_dir 1 0
16530         test_300_check_default_striped_dir normal_dir 2 1
16531         test_300_check_default_striped_dir normal_dir 2 -1
16532
16533         #delete default stripe information
16534         echo "delete default stripeEA"
16535         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
16536                 error "set default stripe on striped dir error"
16537
16538         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
16539         for dir in $(find $DIR/$tdir/normal_dir/*); do
16540                 stripe_count=$($LFS getdirstripe -c $dir)
16541                 [ $stripe_count -eq 0 ] ||
16542                         error "expect 1 get $stripe_count for $dir"
16543                 stripe_index=$($LFS getdirstripe -i $dir)
16544                 [ $stripe_index -eq 0 ] ||
16545                         error "expect 0 get $stripe_index for $dir"
16546         done
16547 }
16548 run_test 300g "check default striped directory for normal directory"
16549
16550 test_300h() {
16551         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16552                 skip "Need MDS version at least 2.7.55" && return
16553         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16554         local dir
16555         local stripe_count
16556
16557         mkdir $DIR/$tdir
16558         $LFS setdirstripe -i 0 -c $MDSCOUNT -t all_char \
16559                                         $DIR/$tdir/striped_dir ||
16560                 error "set striped dir error"
16561
16562         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
16563         test_300_check_default_striped_dir striped_dir 1 0
16564         test_300_check_default_striped_dir striped_dir 2 1
16565         test_300_check_default_striped_dir striped_dir 2 -1
16566
16567         #delete default stripe information
16568         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
16569                 error "set default stripe on striped dir error"
16570
16571         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
16572         for dir in $(find $DIR/$tdir/striped_dir/*); do
16573                 stripe_count=$($LFS getdirstripe -c $dir)
16574                 [ $stripe_count -eq 0 ] ||
16575                         error "expect 1 get $stripe_count for $dir"
16576         done
16577 }
16578 run_test 300h "check default striped directory for striped directory"
16579
16580 test_300i() {
16581         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16582                 skip "Need MDS version at least 2.7.55" && return
16583         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16584         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16585         local stripe_count
16586         local file
16587
16588         mkdir $DIR/$tdir
16589
16590         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16591                 error "set striped dir error"
16592
16593         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16594                 error "create files under striped dir failed"
16595
16596         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
16597                 error "set striped hashdir error"
16598
16599         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
16600                 error "create dir0 under hash dir failed"
16601         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
16602                 error "create dir1 under hash dir failed"
16603
16604         # unfortunately, we need to umount to clear dir layout cache for now
16605         # once we fully implement dir layout, we can drop this
16606         umount_client $MOUNT || error "umount failed"
16607         mount_client $MOUNT || error "mount failed"
16608
16609         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
16610         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
16611         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
16612
16613         #set the stripe to be unknown hash type
16614         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
16615         $LCTL set_param fail_loc=0x1901
16616         for ((i = 0; i < 10; i++)); do
16617                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
16618                         error "stat f-$i failed"
16619                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
16620         done
16621
16622         touch $DIR/$tdir/striped_dir/f0 &&
16623                 error "create under striped dir with unknown hash should fail"
16624
16625         $LCTL set_param fail_loc=0
16626
16627         umount_client $MOUNT || error "umount failed"
16628         mount_client $MOUNT || error "mount failed"
16629
16630         return 0
16631 }
16632 run_test 300i "client handle unknown hash type striped directory"
16633
16634 test_300j() {
16635         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16636         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16637                 skip "Need MDS version at least 2.7.55" && return
16638         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16639         local stripe_count
16640         local file
16641
16642         mkdir $DIR/$tdir
16643
16644         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
16645         $LCTL set_param fail_loc=0x1702
16646         $LFS setdirstripe -i 0 -c$MDSCOUNT -t all_char $DIR/$tdir/striped_dir ||
16647                 error "set striped dir error"
16648
16649         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
16650                 error "create files under striped dir failed"
16651
16652         $LCTL set_param fail_loc=0
16653
16654         rm -rf $DIR/$tdir || error "unlink striped dir fails"
16655
16656         return 0
16657 }
16658 run_test 300j "test large update record"
16659
16660 test_300k() {
16661         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16662                 skip "Need MDS version at least 2.7.55" && return
16663         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16664         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16665         local stripe_count
16666         local file
16667
16668         mkdir $DIR/$tdir
16669
16670         #define OBD_FAIL_LARGE_STRIPE   0x1703
16671         $LCTL set_param fail_loc=0x1703
16672         $LFS setdirstripe -i 0 -c512 $DIR/$tdir/striped_dir ||
16673                 error "set striped dir error"
16674         $LCTL set_param fail_loc=0
16675
16676         $LFS getdirstripe $DIR/$tdir/striped_dir ||
16677                 error "getstripeddir fails"
16678         rm -rf $DIR/$tdir/striped_dir ||
16679                 error "unlink striped dir fails"
16680
16681         return 0
16682 }
16683 run_test 300k "test large striped directory"
16684
16685 test_300l() {
16686         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16687                 skip "Need MDS version at least 2.7.55" && return
16688         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16689         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16690         local stripe_index
16691
16692         test_mkdir -p $DIR/$tdir/striped_dir
16693         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
16694                         error "chown $RUNAS_ID failed"
16695         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
16696                 error "set default striped dir failed"
16697
16698         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
16699         $LCTL set_param fail_loc=0x80000158
16700         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
16701
16702         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
16703         [ $stripe_index -eq 1 ] ||
16704                 error "expect 1 get $stripe_index for $dir"
16705 }
16706 run_test 300l "non-root user to create dir under striped dir with stale layout"
16707
16708 test_300m() {
16709         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16710                 skip "Need MDS version at least 2.7.55" && return
16711         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16712         [ $MDSCOUNT -ge 2 ] && skip "Only for single MDT" && return
16713
16714         mkdir -p $DIR/$tdir/striped_dir
16715         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
16716                 error "set default stripes dir error"
16717
16718         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
16719
16720         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
16721         [ $stripe_count -eq 0 ] ||
16722                         error "expect 0 get $stripe_count for a"
16723
16724         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
16725                 error "set default stripes dir error"
16726
16727         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
16728
16729         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
16730         [ $stripe_count -eq 0 ] ||
16731                         error "expect 0 get $stripe_count for b"
16732
16733         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
16734                 error "set default stripes dir error"
16735
16736         mkdir $DIR/$tdir/striped_dir/c &&
16737                 error "default stripe_index is invalid, mkdir c should fails"
16738
16739         rm -rf $DIR/$tdir || error "rmdir fails"
16740 }
16741 run_test 300m "setstriped directory on single MDT FS"
16742
16743 cleanup_300n() {
16744         local list=$(comma_list $(mdts_nodes))
16745
16746         trap 0
16747         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16748 }
16749
16750 test_300n() {
16751         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16752                 skip "Need MDS version at least 2.7.55" && return
16753         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16754         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16755         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16756
16757         local stripe_index
16758         local list=$(comma_list $(mdts_nodes))
16759
16760         trap cleanup_300n RETURN EXIT
16761         mkdir -p $DIR/$tdir
16762         chmod 777 $DIR/$tdir
16763         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
16764                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16765                 error "create striped dir succeeds with gid=0"
16766
16767         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16768         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
16769                 error "create striped dir fails with gid=-1"
16770
16771         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16772         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
16773                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
16774                 error "set default striped dir succeeds with gid=0"
16775
16776
16777         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
16778         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
16779                 error "set default striped dir fails with gid=-1"
16780
16781
16782         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
16783         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
16784                                         error "create test_dir fails"
16785         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
16786                                         error "create test_dir1 fails"
16787         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
16788                                         error "create test_dir2 fails"
16789         cleanup_300n
16790 }
16791 run_test 300n "non-root user to create dir under striped dir with default EA"
16792
16793 test_300o() {
16794         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.7.55) ] &&
16795                 skip "Need MDS version at least 2.7.55" && return
16796         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16797         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16798         local numfree1
16799         local numfree2
16800
16801         mkdir -p $DIR/$tdir
16802
16803         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
16804         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
16805         if [ $numfree1 -lt 66000 -o $numfree2 -lt 66000 ]; then
16806                 skip "not enough free inodes $numfree1 $numfree2"
16807                 return
16808         fi
16809
16810         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
16811         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
16812         if [ $numfree1 -lt 300000 -o $numfree2 -lt 300000 ]; then
16813                 skip "not enough free space $numfree1 $numfree2"
16814                 return
16815         fi
16816
16817         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
16818                 error "setdirstripe fails"
16819
16820         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
16821                 error "create dirs fails"
16822
16823         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
16824         ls $DIR/$tdir/striped_dir > /dev/null ||
16825                 error "ls striped dir fails"
16826         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
16827                 error "unlink big striped dir fails"
16828 }
16829 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
16830
16831 test_300p() {
16832         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16833         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16834         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16835
16836         mkdir -p $DIR/$tdir
16837
16838         #define OBD_FAIL_OUT_ENOSPC     0x1704
16839         do_facet mds2 lctl set_param fail_loc=0x80001704
16840         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 &&
16841                         error "create striped directory should fail"
16842
16843         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
16844
16845         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
16846         true
16847 }
16848 run_test 300p "create striped directory without space"
16849
16850 test_300q() {
16851         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16852         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
16853
16854         local fd=$(free_fd)
16855         local cmd="exec $fd<$tdir"
16856         cd $DIR
16857         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
16858         eval $cmd
16859         cmd="exec $fd<&-"
16860         trap "eval $cmd" EXIT
16861         cd $tdir || error "cd $tdir fails"
16862         rmdir  ../$tdir || error "rmdir $tdir fails"
16863         mkdir local_dir && error "create dir succeeds"
16864         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
16865         eval $cmd
16866         return 0
16867 }
16868 run_test 300q "create remote directory under orphan directory"
16869
16870 prepare_remote_file() {
16871         mkdir $DIR/$tdir/src_dir ||
16872                 error "create remote source failed"
16873
16874         cp /etc/hosts $DIR/$tdir/src_dir/a || error
16875         touch $DIR/$tdir/src_dir/a
16876
16877         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
16878                 error "create remote target dir failed"
16879
16880         touch $DIR/$tdir/tgt_dir/b
16881
16882         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
16883                 error "rename dir cross MDT failed!"
16884
16885         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
16886                 error "src_child still exists after rename"
16887
16888         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
16889                 error "missing file(a) after rename"
16890
16891         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
16892                 error "diff after rename"
16893 }
16894
16895 test_310a() {
16896         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16897         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16898         local remote_file=$DIR/$tdir/tgt_dir/b
16899
16900         mkdir -p $DIR/$tdir
16901
16902         prepare_remote_file || error "prepare remote file failed"
16903
16904         #open-unlink file
16905         $OPENUNLINK $remote_file $remote_file || error
16906         $CHECKSTAT -a $remote_file || error
16907 }
16908 run_test 310a "open unlink remote file"
16909
16910 test_310b() {
16911         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 4 MDTs" && return
16912         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16913         local remote_file=$DIR/$tdir/tgt_dir/b
16914
16915         mkdir -p $DIR/$tdir
16916
16917         prepare_remote_file || error "prepare remote file failed"
16918
16919         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16920         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
16921         $CHECKSTAT -t file $remote_file || error "check file failed"
16922 }
16923 run_test 310b "unlink remote file with multiple links while open"
16924
16925 test_310c() {
16926         [[ $MDSCOUNT -lt 4 ]] && skip "needs >= 4 MDTs" && return
16927         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16928         local remote_file=$DIR/$tdir/tgt_dir/b
16929
16930         mkdir -p $DIR/$tdir
16931
16932         prepare_remote_file || error "prepare remote file failed"
16933
16934         ln $remote_file $DIR/$tfile || error "link failed for remote file"
16935         multiop_bg_pause $remote_file O_uc ||
16936                         error "mulitop failed for remote file"
16937         MULTIPID=$!
16938         $MULTIOP $DIR/$tfile Ouc
16939         kill -USR1 $MULTIPID
16940         wait $MULTIPID
16941 }
16942 run_test 310c "open-unlink remote file with multiple links"
16943
16944 #LU-4825
16945 test_311() {
16946         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.54) ] &&
16947                 skip "lustre < 2.8.54 does not contain LU-4825 fix" && return
16948         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
16949         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16950
16951         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16952
16953         mkdir -p $DIR/$tdir
16954         $SETSTRIPE -i 0 -c 1 $DIR/$tdir
16955         createmany -o $DIR/$tdir/$tfile. 1000
16956
16957         # statfs data is not real time, let's just calculate it
16958         old_iused=$((old_iused + 1000))
16959
16960         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
16961                         osp.*OST0000*MDT0000.create_count")
16962         local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
16963                                 osp.*OST0000*MDT0000.max_create_count")
16964         for idx in $(seq $MDSCOUNT); do
16965                 do_facet mds$idx "lctl set_param -n \
16966                         osp.*OST0000*MDT000?.max_create_count=0"
16967         done
16968
16969         $SETSTRIPE -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
16970         local index=$($GETSTRIPE -i $DIR/$tdir/$tfile)
16971         [ $index -ne 0 ] || error "$tfile stripe index is 0"
16972
16973         unlinkmany $DIR/$tdir/$tfile. 1000
16974
16975         for idx in $(seq $MDSCOUNT); do
16976                 do_facet mds$idx "lctl set_param -n \
16977                         osp.*OST0000*MDT000?.max_create_count=$max_count"
16978                 do_facet mds$idx "lctl set_param -n \
16979                         osp.*OST0000*MDT000?.create_count=$count"
16980         done
16981
16982         local new_iused
16983         for i in $(seq 120); do
16984                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
16985                 # system may be too busy to destroy all objs in time, use
16986                 # a somewhat small value to not fail autotest
16987                 [ $((old_iused - new_iused)) -gt 400 ] && break
16988                 sleep 1
16989         done
16990
16991         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
16992         [ $((old_iused - new_iused)) -gt 400 ] ||
16993                 error "objs not destroyed after unlink"
16994 }
16995 run_test 311 "disable OSP precreate, and unlink should destroy objs"
16996
16997 zfs_oid_to_objid()
16998 {
16999         local ost=$1
17000         local objid=$2
17001
17002         local vdevdir=$(dirname $(facet_vdevice $ost))
17003         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
17004         local zfs_zapid=$(do_facet $ost $cmd |
17005                           grep -w "/O/0/d$((objid%32))" -C 5 |
17006                           awk '/Object/{getline; print $1}')
17007         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
17008                           awk "/$objid = /"'{printf $3}')
17009
17010         echo $zfs_objid
17011 }
17012
17013 zfs_object_blksz() {
17014         local ost=$1
17015         local objid=$2
17016
17017         local vdevdir=$(dirname $(facet_vdevice $ost))
17018         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
17019         local blksz=$(do_facet $ost $cmd $objid |
17020                       awk '/dblk/{getline; printf $4}')
17021
17022         case "${blksz: -1}" in
17023                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
17024                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
17025                 *) ;;
17026         esac
17027
17028         echo $blksz
17029 }
17030
17031 test_312() { # LU-4856
17032         remote_ost_nodsh && skip "remote OST with nodsh" && return
17033
17034         [ $(facet_fstype ost1) = "zfs" ] ||
17035                 { skip "the test only applies to zfs" && return; }
17036
17037         local max_blksz=$(do_facet ost1 \
17038                           $ZFS get -p recordsize $(facet_device ost1) |
17039                           awk '!/VALUE/{print $3}')
17040         local min_blksz=$(getconf PAGE_SIZE)
17041
17042         # to make life a little bit easier
17043         $LFS mkdir -c 1 -i 0 $DIR/$tdir
17044         $LFS setstripe -c 1 -i 0 $DIR/$tdir
17045
17046         local tf=$DIR/$tdir/$tfile
17047         touch $tf
17048         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17049
17050         # Get ZFS object id
17051         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17052
17053         # block size change by sequential over write
17054         local blksz
17055         for ((bs=$min_blksz; bs <= max_blksz; bs <<= 2)); do
17056                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
17057
17058                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17059                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
17060         done
17061         rm -f $tf
17062
17063         # block size change by sequential append write
17064         dd if=/dev/zero of=$tf bs=$min_blksz count=1 oflag=sync conv=notrunc
17065         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17066         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17067
17068         for ((count = 1; count < $((max_blksz / min_blksz)); count *= 2)); do
17069                 dd if=/dev/zero of=$tf bs=$min_blksz count=$count seek=$count \
17070                         oflag=sync conv=notrunc
17071
17072                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
17073                 [ $blksz -eq $((2 * count * min_blksz)) ] ||
17074                         error "blksz error, actual $blksz, "    \
17075                                 "expected: 2 * $count * $min_blksz"
17076         done
17077         rm -f $tf
17078
17079         # random write
17080         touch $tf
17081         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
17082         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
17083
17084         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
17085         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17086         [ $blksz -eq $min_blksz ] ||
17087                 error "blksz error: $blksz, expected: $min_blksz"
17088
17089         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
17090         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17091         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
17092
17093         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
17094         blksz=$(zfs_object_blksz ost1 $zfs_objid)
17095         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
17096 }
17097 run_test 312 "make sure ZFS adjusts its block size by write pattern"
17098
17099 test_313() {
17100         remote_ost_nodsh && skip "remote OST with nodsh" && return
17101
17102         local file=$DIR/$tfile
17103         rm -f $file
17104         $SETSTRIPE -c 1 -i 0 $file || error "setstripe failed"
17105
17106         # define OBD_FAIL_TGT_RCVD_EIO           0x720
17107         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17108         dd if=/dev/zero of=$file bs=4096 oflag=direct count=1 &&
17109                 error "write should failed"
17110         do_facet ost1 "$LCTL set_param fail_loc=0"
17111         rm -f $file
17112 }
17113 run_test 313 "io should fail after last_rcvd update fail"
17114
17115 test_314() {
17116         $SETSTRIPE -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
17117         do_facet ost1 "$LCTL set_param fail_loc=0x720"
17118         rm -f $DIR/$tfile
17119         wait_delete_completed
17120         do_facet ost1 "$LCTL set_param fail_loc=0"
17121 }
17122 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
17123
17124 test_315() { # LU-618
17125         local file=$DIR/$tfile
17126         rm -f $file
17127
17128         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4096000c
17129         $MULTIOP $file oO_RDONLY:r4096000_c &
17130         PID=$!
17131
17132         sleep 2
17133
17134         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
17135         kill -USR1 $PID
17136
17137         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
17138         rm -f $file
17139 }
17140 run_test 315 "read should be accounted"
17141
17142 test_fake_rw() {
17143         local read_write=$1
17144         if [ "$read_write" = "write" ]; then
17145                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
17146         elif [ "$read_write" = "read" ]; then
17147                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
17148         else
17149                 error "argument error"
17150         fi
17151
17152         # turn off debug for performance testing
17153         local saved_debug=$($LCTL get_param -n debug)
17154         $LCTL set_param debug=0
17155
17156         $SETSTRIPE -c 1 -i 0 $DIR/$tfile
17157
17158         # get ost1 size - lustre-OST0000
17159         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
17160         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
17161         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
17162
17163         if [ "$read_write" = "read" ]; then
17164                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
17165         fi
17166
17167         local start_time=$(date +%s.%N)
17168         $dd_cmd bs=1M count=$blocks oflag=sync ||
17169                 error "real dd $read_write error"
17170         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
17171
17172         if [ "$read_write" = "write" ]; then
17173                 rm -f $DIR/$tfile
17174         fi
17175
17176         # define OBD_FAIL_OST_FAKE_RW           0x238
17177         do_facet ost1 $LCTL set_param fail_loc=0x238
17178
17179         local start_time=$(date +%s.%N)
17180         $dd_cmd bs=1M count=$blocks oflag=sync ||
17181                 error "fake dd $read_write error"
17182         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
17183
17184         if [ "$read_write" = "write" ]; then
17185                 # verify file size
17186                 cancel_lru_locks osc
17187                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
17188                         error "$tfile size not $blocks MB"
17189         fi
17190         do_facet ost1 $LCTL set_param fail_loc=0
17191
17192         echo "fake $read_write $duration_fake vs. normal $read_write" \
17193                 "$duration in seconds"
17194         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
17195                 error_not_in_vm "fake write is slower"
17196
17197         $LCTL set_param -n debug="$saved_debug"
17198         rm -f $DIR/$tfile
17199 }
17200 test_399a() { # LU-7655 for OST fake write
17201         remote_ost_nodsh && skip "remote OST with nodsh" && return
17202
17203         test_fake_rw write
17204 }
17205 run_test 399a "fake write should not be slower than normal write"
17206
17207 test_399b() { # LU-8726 for OST fake read
17208         remote_ost_nodsh && skip "remote OST with nodsh" && return
17209
17210         if [ "$(facet_fstype ost1)" != "ldiskfs" ]; then
17211                 skip "ldiskfs only test" && return 0
17212         fi
17213         test_fake_rw read
17214 }
17215 run_test 399b "fake read should not be slower than normal read"
17216
17217 test_400a() { # LU-1606, was conf-sanity test_74
17218         local extra_flags=''
17219         local out=$TMP/$tfile
17220         local prefix=/usr/include/lustre
17221         local prog
17222
17223         if ! which $CC > /dev/null 2>&1; then
17224                 skip_env "$CC is not installed"
17225                 return 0
17226         fi
17227
17228         if ! [[ -d $prefix ]]; then
17229                 # Assume we're running in tree and fixup the include path.
17230                 extra_flags+=" -I$LUSTRE/include"
17231                 extra_flags+=" -L$LUSTRE/utils"
17232         fi
17233
17234         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
17235                 $CC -Wall -Werror $extra_flags -llustreapi -o $out $prog ||
17236                         error "client api broken"
17237         done
17238         rm -f $out
17239 }
17240 run_test 400a "Lustre client api program can compile and link"
17241
17242 test_400b() { # LU-1606, LU-5011
17243         local header
17244         local out=$TMP/$tfile
17245         local prefix=/usr/include/linux/lustre
17246
17247         # We use a hard coded prefix so that this test will not fail
17248         # when run in tree. There are headers in lustre/include/lustre/
17249         # that are not packaged (like lustre_idl.h) and have more
17250         # complicated include dependencies (like config.h and lnet/types.h).
17251         # Since this test about correct packaging we just skip them when
17252         # they don't exist (see below) rather than try to fixup cppflags.
17253
17254         if ! which $CC > /dev/null 2>&1; then
17255                 skip_env "$CC is not installed"
17256                 return 0
17257         fi
17258
17259         for header in $prefix/*.h; do
17260                 if ! [[ -f "$header" ]]; then
17261                         continue
17262                 fi
17263
17264                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
17265                         continue # lustre_ioctl.h is internal header
17266                 fi
17267
17268                 $CC -Wall -Werror -include $header -c -x c /dev/null -o $out ||
17269                         error "cannot compile '$header'"
17270         done
17271         rm -f $out
17272 }
17273 run_test 400b "packaged headers can be compiled"
17274
17275 test_401a() { #LU-7437
17276         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
17277         [ -n "$printf_arg" ] && skip_env "find does not support -printf" &&
17278                 return
17279         #count the number of parameters by "list_param -R"
17280         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
17281         #count the number of parameters by listing proc files
17282         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
17283         echo "proc_dirs='$proc_dirs'"
17284         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
17285         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
17286                       sort -u | wc -l)
17287
17288         [ $params -eq $procs ] ||
17289                 error "found $params parameters vs. $procs proc files"
17290
17291         # test the list_param -D option only returns directories
17292         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
17293         #count the number of parameters by listing proc directories
17294         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
17295                 sort -u | wc -l)
17296
17297         [ $params -eq $procs ] ||
17298                 error "found $params parameters vs. $procs proc files"
17299 }
17300 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
17301
17302 test_401b() {
17303         local save=$($LCTL get_param -n jobid_var)
17304         local tmp=testing
17305
17306         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
17307                 error "no error returned when setting bad parameters"
17308
17309         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
17310         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
17311
17312         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
17313         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
17314         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
17315 }
17316 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
17317
17318 test_401c() {
17319         local jobid_var_old=$($LCTL get_param -n jobid_var)
17320         local jobid_var_new
17321
17322         $LCTL set_param jobid_var= &&
17323                 error "no error returned for 'set_param a='"
17324
17325         jobid_var_new=$($LCTL get_param -n jobid_var)
17326         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17327                 error "jobid_var was changed by setting without value"
17328
17329         $LCTL set_param jobid_var &&
17330                 error "no error returned for 'set_param a'"
17331
17332         jobid_var_new=$($LCTL get_param -n jobid_var)
17333         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
17334                 error "jobid_var was changed by setting without value"
17335 }
17336 run_test 401c "Verify 'lctl set_param' without value fails in either format."
17337
17338 test_401d() {
17339         local jobid_var_old=$($LCTL get_param -n jobid_var)
17340         local jobid_var_new
17341         local new_value="foo=bar"
17342
17343         $LCTL set_param jobid_var=$new_value ||
17344                 error "'set_param a=b' did not accept a value containing '='"
17345
17346         jobid_var_new=$($LCTL get_param -n jobid_var)
17347         [[ "$jobid_var_new" == "$new_value" ]] ||
17348                 error "'set_param a=b' failed on a value containing '='"
17349
17350         # Reset the jobid_var to test the other format
17351         $LCTL set_param jobid_var=$jobid_var_old
17352         jobid_var_new=$($LCTL get_param -n jobid_var)
17353         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17354                 error "failed to reset jobid_var"
17355
17356         $LCTL set_param jobid_var $new_value ||
17357                 error "'set_param a b' did not accept a value containing '='"
17358
17359         jobid_var_new=$($LCTL get_param -n jobid_var)
17360         [[ "$jobid_var_new" == "$new_value" ]] ||
17361                 error "'set_param a b' failed on a value containing '='"
17362
17363         $LCTL set_param jobid_var $jobid_var_old
17364         jobid_var_new=$($LCTL get_param -n jobid_var)
17365         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
17366                 error "failed to reset jobid_var"
17367 }
17368 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
17369
17370 test_402() {
17371         local server_version=$(lustre_version_code $SINGLEMDS)
17372         [[ $server_version -ge $(version_code 2.7.66) ]] ||
17373         [[ $server_version -ge $(version_code 2.7.18.4) &&
17374                 $server_version -lt $(version_code 2.7.50) ]] ||
17375         [[ $server_version -ge $(version_code 2.7.2) &&
17376                 $server_version -lt $(version_code 2.7.11) ]] ||
17377                 { skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+";
17378                         return; }
17379         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17380         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
17381 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
17382         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
17383         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
17384                 echo "Touch failed - OK"
17385 }
17386 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
17387
17388 test_403() {
17389         local file1=$DIR/$tfile.1
17390         local file2=$DIR/$tfile.2
17391         local tfile=$TMP/$tfile
17392
17393         rm -f $file1 $file2 $tfile
17394
17395         touch $file1
17396         ln $file1 $file2
17397
17398         # 30 sec OBD_TIMEOUT in ll_getattr()
17399         # right before populating st_nlink
17400         $LCTL set_param fail_loc=0x80001409
17401         stat -c %h $file1 > $tfile &
17402
17403         # create an alias, drop all locks and reclaim the dentry
17404         < $file2
17405         cancel_lru_locks mdc
17406         cancel_lru_locks osc
17407         sysctl -w vm.drop_caches=2
17408
17409         wait
17410
17411         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
17412
17413         rm -f $tfile $file1 $file2
17414 }
17415 run_test 403 "i_nlink should not drop to zero due to aliasing"
17416
17417 test_404() { # LU-6601
17418         local server_version=$(lustre_version_code $SINGLEMDS)
17419         [[ $server_version -ge $(version_code 2.8.53) ]] ||
17420                 { skip "Need server version newer than 2.8.52"; return 0; }
17421
17422         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17423         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
17424                 awk '/osp .*-osc-MDT/ { print $4}')
17425
17426         local osp
17427         for osp in $mosps; do
17428                 echo "Deactivate: " $osp
17429                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
17430                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17431                         awk -vp=$osp '$4 == p { print $2 }')
17432                 [ $stat = IN ] || {
17433                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17434                         error "deactivate error"
17435                 }
17436                 echo "Activate: " $osp
17437                 do_facet $SINGLEMDS $LCTL --device %$osp activate
17438                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
17439                         awk -vp=$osp '$4 == p { print $2 }')
17440                 [ $stat = UP ] || {
17441                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
17442                         error "activate error"
17443                 }
17444         done
17445 }
17446 run_test 404 "validate manual {de}activated works properly for OSPs"
17447
17448 test_405() {
17449         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.6.92) -o \
17450         [ $(lustre_version_code client) -lt $(version_code 2.6.99) ] &&
17451                 skip "Layout swap lock is not supported" && return
17452
17453         check_swap_layouts_support && return 0
17454
17455         test_mkdir $DIR/$tdir
17456         swap_lock_test -d $DIR/$tdir ||
17457                 error "One layout swap locked test failed"
17458 }
17459 run_test 405 "Various layout swap lock tests"
17460
17461 test_406() {
17462         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17463         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs" && return
17464         [ -n "$FILESET" ] && skip "SKIP due to FILESET set" && return
17465         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
17466         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.50) ] &&
17467                 skip "Need MDS version at least 2.8.50" && return
17468
17469         local def_stripe_count=$($GETSTRIPE -c $MOUNT)
17470         local def_stripe_size=$($GETSTRIPE -S $MOUNT)
17471         local def_stripe_offset=$($GETSTRIPE -i $MOUNT)
17472         local def_pool=$($GETSTRIPE -p $MOUNT)
17473         local test_pool=$TESTNAME
17474
17475         if ! combined_mgs_mds ; then
17476                 mount_mgs_client
17477         fi
17478         pool_add $test_pool || error "pool_add failed"
17479         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
17480                 error "pool_add_targets failed"
17481
17482         # parent set default stripe count only, child will stripe from both
17483         # parent and fs default
17484         $SETSTRIPE -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
17485                 error "setstripe $MOUNT failed"
17486         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
17487         $SETSTRIPE -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
17488         for i in $(seq 10); do
17489                 local f=$DIR/$tdir/$tfile.$i
17490                 touch $f || error "touch failed"
17491                 local count=$($GETSTRIPE -c $f)
17492                 [ $count -eq $OSTCOUNT ] ||
17493                         error "$f stripe count $count != $OSTCOUNT"
17494                 local offset=$($GETSTRIPE -i $f)
17495                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
17496                 local size=$($GETSTRIPE -S $f)
17497                 [ $size -eq $((def_stripe_size * 2)) ] ||
17498                         error "$f stripe size $size != $((def_stripe_size * 2))"
17499                 local pool=$($GETSTRIPE -p $f)
17500                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
17501         done
17502
17503         # change fs default striping, delete parent default striping, now child
17504         # will stripe from new fs default striping only
17505         $SETSTRIPE -c 1 -S $def_stripe_size -i 0 $MOUNT ||
17506                 error "change $MOUNT default stripe failed"
17507         $SETSTRIPE -c 0 $DIR/$tdir || error "delete $tdir default stripe failed"
17508         for i in $(seq 11 20); do
17509                 local f=$DIR/$tdir/$tfile.$i
17510                 touch $f || error "touch $f failed"
17511                 local count=$($GETSTRIPE -c $f)
17512                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
17513                 local offset=$($GETSTRIPE -i $f)
17514                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
17515                 local size=$($GETSTRIPE -S $f)
17516                 [ $size -eq $def_stripe_size ] ||
17517                         error "$f stripe size $size != $def_stripe_size"
17518                 local pool=$($GETSTRIPE -p $f)
17519                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
17520
17521         done
17522
17523         unlinkmany $DIR/$tdir/$tfile. 1 20
17524
17525         # restore FS default striping
17526         if [ -z $def_pool ]; then
17527                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17528                         -i $def_stripe_offset $MOUNT ||
17529                         error "restore default striping failed"
17530         else
17531                 $SETSTRIPE -c $def_stripe_count -S $def_stripe_size \
17532                         -i $def_stripe_offset -p $def_pool $MOUNT ||
17533                         error "restore default striping with $def_pool failed"
17534         fi
17535
17536         local f=$DIR/$tdir/$tfile
17537         pool_remove_all_targets $test_pool $f
17538         pool_remove $test_pool $f
17539
17540         if ! combined_mgs_mds ; then
17541                 umount_mgs_client
17542         fi
17543 }
17544 run_test 406 "DNE support fs default striping"
17545
17546 test_407() {
17547         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
17548         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.8.55) ]] &&
17549                 skip "Need MDS version at least 2.8.55" && return
17550         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17551
17552         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
17553                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
17554         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
17555                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
17556         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
17557
17558         #define OBD_FAIL_DT_TXN_STOP    0x2019
17559         for idx in $(seq $MDSCOUNT); do
17560                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
17561         done
17562         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
17563         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
17564                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
17565         true
17566 }
17567 run_test 407 "transaction fail should cause operation fail"
17568
17569 test_408() {
17570         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 oflag=direct
17571
17572         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
17573         lctl set_param fail_loc=0x8000040a
17574         # let ll_prepare_partial_page() fail
17575         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
17576
17577         rm -f $DIR/$tfile
17578
17579         # create at least 100 unused inodes so that
17580         # shrink_icache_memory(0) should not return 0
17581         touch $DIR/$tfile-{0..100}
17582         rm -f $DIR/$tfile-{0..100}
17583         sync
17584
17585         echo 2 > /proc/sys/vm/drop_caches
17586 }
17587 run_test 408 "drop_caches should not hang due to page leaks"
17588
17589 test_409()
17590 {
17591         [ $MDSCOUNT -lt 2 ] &&
17592                 skip "We need at least 2 MDTs for this test" && return
17593
17594         check_mount_and_prep
17595
17596         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
17597         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
17598         touch $DIR/$tdir/guard || error "(2) Fail to create"
17599
17600         local PREFIX=$(str_repeat 'A' 128)
17601         echo "Create 1K hard links start at $(date)"
17602         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17603                 error "(3) Fail to hard link"
17604
17605         echo "Links count should be right although linkEA overflow"
17606         stat $DIR/$tdir/guard || error "(4) Fail to stat"
17607         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
17608         [ $linkcount -eq 1001 ] ||
17609                 error "(5) Unexpected hard links count: $linkcount"
17610
17611         echo "List all links start at $(date)"
17612         ls -l $DIR/$tdir/foo > /dev/null ||
17613                 error "(6) Fail to list $DIR/$tdir/foo"
17614
17615         echo "Unlink hard links start at $(date)"
17616         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
17617                 error "(7) Fail to unlink"
17618 }
17619 run_test 409 "Large amount of cross-MDTs hard links on the same file"
17620
17621 test_410()
17622 {
17623         [[ $(lustre_version_code client) -lt $(version_code 2.9.59) ]] &&
17624                 skip "Need client version at least 2.9.59" && return
17625
17626         # Create a file, and stat it from the kernel
17627         local testfile=$DIR/$tfile
17628         touch $testfile
17629
17630         local run_id=$RANDOM
17631         local my_ino=$(stat --format "%i" $testfile)
17632
17633         # Try to insert the module. This will always fail as the
17634         # module is designed to not be inserted.
17635         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
17636             &> /dev/null
17637
17638         # Anything but success is a test failure
17639         dmesg | grep -q \
17640             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
17641             error "no inode match"
17642 }
17643 run_test 410 "Test inode number returned from kernel thread"
17644
17645 cleanup_test411_cgroup() {
17646         trap 0
17647         rmdir "$1"
17648 }
17649
17650 test_411() {
17651         local cg_basedir=/sys/fs/cgroup/memory
17652         # LU-9966
17653         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
17654                 { skip "no setup for cgroup"; return; }
17655
17656         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
17657                 error "test file creation failed"
17658         cancel_lru_locks osc
17659
17660         # Create a very small memory cgroup to force a slab allocation error
17661         local cgdir=$cg_basedir/osc_slab_alloc
17662         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
17663         trap "cleanup_test411_cgroup $cgdir" EXIT
17664         echo 2M > $cgdir/memory.kmem.limit_in_bytes
17665         echo 1M > $cgdir/memory.limit_in_bytes
17666
17667         # Should not LBUG, just be killed by oom-killer
17668         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null" &&
17669                 error "fail to trigger a memory allocation error"
17670
17671         cleanup_test411_cgroup $cgdir
17672
17673         return 0
17674 }
17675 run_test 411 "Slab allocation error with cgroup does not LBUG"
17676
17677 test_412() {
17678         [ $MDSCOUNT -lt 2 ] &&
17679                 skip "We need at least 2 MDTs for this test" && return
17680
17681         if [ $(lustre_version_code mds1) -lt $(version_code 2.10.55) ]; then
17682                 skip "Need server version at least 2.10.55" & exit 0
17683         fi
17684
17685         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
17686                 error "mkdir failed"
17687         $LFS getdirstripe $DIR/$tdir
17688         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
17689         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
17690                 error "expect $((MDSCOUT - 1)) get $stripe_index"
17691         stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
17692         [ $stripe_count -eq 2 ] ||
17693                 error "expect 2 get $stripe_count"
17694 }
17695 run_test 412 "mkdir on specific MDTs"
17696
17697 prep_801() {
17698         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17699         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17700                 skip "Need server version at least 2.9.55" & exit 0
17701         start_full_debug_logging
17702 }
17703
17704 post_801() {
17705         stop_full_debug_logging
17706 }
17707
17708 barrier_stat() {
17709         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17710                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17711                            awk '/The barrier for/ { print $7 }')
17712                 echo $st
17713         else
17714                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
17715                 echo \'$st\'
17716         fi
17717 }
17718
17719 barrier_expired() {
17720         local expired
17721
17722         if [ $(lustre_version_code mgs) -le $(version_code 2.10.0) ]; then
17723                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
17724                           awk '/will be expired/ { print $7 }')
17725         else
17726                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
17727         fi
17728
17729         echo $expired
17730 }
17731
17732 test_801a() {
17733         prep_801
17734
17735         echo "Start barrier_freeze at: $(date)"
17736         #define OBD_FAIL_BARRIER_DELAY          0x2202
17737         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17738         do_facet mgs $LCTL barrier_freeze $FSNAME 10 &
17739
17740         sleep 2
17741         local b_status=$(barrier_stat)
17742         echo "Got barrier status at: $(date)"
17743         [ "$b_status" = "'freezing_p1'" ] ||
17744                 error "(1) unexpected barrier status $b_status"
17745
17746         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17747         wait
17748         b_status=$(barrier_stat)
17749         [ "$b_status" = "'frozen'" ] ||
17750                 error "(2) unexpected barrier status $b_status"
17751
17752         local expired=$(barrier_expired)
17753         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
17754         sleep $((expired + 3))
17755
17756         b_status=$(barrier_stat)
17757         [ "$b_status" = "'expired'" ] ||
17758                 error "(3) unexpected barrier status $b_status"
17759
17760         do_facet mgs $LCTL barrier_freeze $FSNAME 10 ||
17761                 error "(4) fail to freeze barrier"
17762
17763         b_status=$(barrier_stat)
17764         [ "$b_status" = "'frozen'" ] ||
17765                 error "(5) unexpected barrier status $b_status"
17766
17767         echo "Start barrier_thaw at: $(date)"
17768         #define OBD_FAIL_BARRIER_DELAY          0x2202
17769         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
17770         do_facet mgs $LCTL barrier_thaw $FSNAME &
17771
17772         sleep 2
17773         b_status=$(barrier_stat)
17774         echo "Got barrier status at: $(date)"
17775         [ "$b_status" = "'thawing'" ] ||
17776                 error "(6) unexpected barrier status $b_status"
17777
17778         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
17779         wait
17780         b_status=$(barrier_stat)
17781         [ "$b_status" = "'thawed'" ] ||
17782                 error "(7) unexpected barrier status $b_status"
17783
17784         #define OBD_FAIL_BARRIER_FAILURE        0x2203
17785         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
17786         do_facet mgs $LCTL barrier_freeze $FSNAME
17787
17788         b_status=$(barrier_stat)
17789         [ "$b_status" = "'failed'" ] ||
17790                 error "(8) unexpected barrier status $b_status"
17791
17792         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17793         do_facet mgs $LCTL barrier_thaw $FSNAME
17794
17795         post_801
17796 }
17797 run_test 801a "write barrier user interfaces and stat machine"
17798
17799 test_801b() {
17800         prep_801
17801
17802         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17803         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
17804         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
17805         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
17806         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
17807
17808         cancel_lru_locks mdc
17809
17810         # 180 seconds should be long enough
17811         do_facet mgs $LCTL barrier_freeze $FSNAME 180
17812
17813         local b_status=$(barrier_stat)
17814         [ "$b_status" = "'frozen'" ] ||
17815                 error "(6) unexpected barrier status $b_status"
17816
17817         mkdir $DIR/$tdir/d0/d10 &
17818         mkdir_pid=$!
17819
17820         touch $DIR/$tdir/d1/f13 &
17821         touch_pid=$!
17822
17823         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
17824         ln_pid=$!
17825
17826         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
17827         mv_pid=$!
17828
17829         rm -f $DIR/$tdir/d4/f12 &
17830         rm_pid=$!
17831
17832         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
17833
17834         # To guarantee taht the 'stat' is not blocked
17835         b_status=$(barrier_stat)
17836         [ "$b_status" = "'frozen'" ] ||
17837                 error "(8) unexpected barrier status $b_status"
17838
17839         # let above commands to run at background
17840         sleep 5
17841
17842         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
17843         ps -p $touch_pid || error "(10) touch should be blocked"
17844         ps -p $ln_pid || error "(11) link should be blocked"
17845         ps -p $mv_pid || error "(12) rename should be blocked"
17846         ps -p $rm_pid || error "(13) unlink should be blocked"
17847
17848         b_status=$(barrier_stat)
17849         [ "$b_status" = "'frozen'" ] ||
17850                 error "(14) unexpected barrier status $b_status"
17851
17852         do_facet mgs $LCTL barrier_thaw $FSNAME
17853         b_status=$(barrier_stat)
17854         [ "$b_status" = "'thawed'" ] ||
17855                 error "(15) unexpected barrier status $b_status"
17856
17857         wait $mkdir_pid || error "(16) mkdir should succeed"
17858         wait $touch_pid || error "(17) touch should succeed"
17859         wait $ln_pid || error "(18) link should succeed"
17860         wait $mv_pid || error "(19) rename should succeed"
17861         wait $rm_pid || error "(20) unlink should succeed"
17862
17863         post_801
17864 }
17865 run_test 801b "modification will be blocked by write barrier"
17866
17867 test_801c() {
17868         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17869
17870         prep_801
17871
17872         stop mds2 || error "(1) Fail to stop mds2"
17873
17874         do_facet mgs $LCTL barrier_freeze $FSNAME 30
17875
17876         local b_status=$(barrier_stat)
17877         [ "$b_status" = "'expired'" -o "$b_status" = "'failed'" ] || {
17878                 do_facet mgs $LCTL barrier_thaw $FSNAME
17879                 error "(2) unexpected barrier status $b_status"
17880         }
17881
17882         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17883                 error "(3) Fail to rescan barrier bitmap"
17884
17885         do_facet mgs $LCTL barrier_freeze $FSNAME 10
17886
17887         b_status=$(barrier_stat)
17888         [ "$b_status" = "'frozen'" ] ||
17889                 error "(4) unexpected barrier status $b_status"
17890
17891         do_facet mgs $LCTL barrier_thaw $FSNAME
17892         b_status=$(barrier_stat)
17893         [ "$b_status" = "'thawed'" ] ||
17894                 error "(5) unexpected barrier status $b_status"
17895
17896         local devname=$(mdsdevname 2)
17897
17898         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
17899
17900         do_facet mgs $LCTL barrier_rescan $FSNAME ||
17901                 error "(7) Fail to rescan barrier bitmap"
17902
17903         post_801
17904 }
17905 run_test 801c "rescan barrier bitmap"
17906
17907 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
17908 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
17909 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
17910
17911 cleanup_802() {
17912         trap 0
17913
17914         stopall
17915         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
17916         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
17917         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
17918         setupall
17919 }
17920
17921 test_802() {
17922
17923         [[ $(lustre_version_code mds1) -lt $(version_code 2.9.55) ]] ||
17924         [[ $(lustre_version_code ost1) -lt $(version_code 2.9.55) ]] &&
17925                 skip "Need server version at least 2.9.55" & exit 0
17926
17927         mkdir $DIR/$tdir || error "(1) fail to mkdir"
17928
17929         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
17930                 error "(2) Fail to copy"
17931
17932         trap cleanup_802 EXIT
17933
17934         # sync by force before remount as readonly
17935         sync; sync_all_data; sleep 3; sync_all_data
17936
17937         stopall
17938
17939         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
17940         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
17941         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
17942
17943         echo "Mount the server as read only"
17944         setupall server_only || error "(3) Fail to start servers"
17945
17946         echo "Mount client without ro should fail"
17947         mount_client $MOUNT &&
17948                 error "(4) Mount client without 'ro' should fail"
17949
17950         echo "Mount client with ro should succeed"
17951         mount_client $MOUNT ro ||
17952                 error "(5) Mount client with 'ro' should succeed"
17953
17954         echo "Modify should be refused"
17955         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
17956
17957         echo "Read should be allowed"
17958         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
17959                 error "(7) Read should succeed under ro mode"
17960
17961         cleanup_802
17962 }
17963 run_test 802 "simulate readonly device"
17964
17965 test_803() {
17966         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
17967         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
17968                 skip "MDS needs to be newer than 2.10.54" && return
17969
17970         mkdir -p $DIR/$tdir
17971         # Create some objects on all MDTs to trigger related logs objects
17972         for idx in $(seq $MDSCOUNT); do
17973                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
17974                         $DIR/$tdir/dir${idx} ||
17975                         error "Fail to create $DIR/$tdir/dir${idx}"
17976         done
17977
17978         sync; sleep 5
17979         echo "before create:"
17980         $LFS df -i $MOUNT
17981         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17982
17983         for ((i=0; i<10; i++)); do
17984                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
17985                         error "Fail to create $DIR/$tdir/foo$i"
17986         done
17987
17988         sync; sleep 5
17989         echo "after create:"
17990         $LFS df -i $MOUNT
17991         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
17992
17993         [ $after_used -ge $((before_used + 10)) ] ||
17994                 error "before ($before_used) + 10 > after ($after_used)"
17995
17996         for ((i=0; i<10; i++)); do
17997                 rm -rf $DIR/$tdir/foo$i ||
17998                         error "Fail to remove $DIR/$tdir/foo$i"
17999         done
18000
18001         wait_delete_completed
18002         echo "after unlink:"
18003         $LFS df -i $MOUNT
18004         before_used=$after_used
18005         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
18006
18007         [ $after_used -le $((before_used - 8)) ] ||
18008                 error "before ($before_used) - 8 < after ($after_used)"
18009 }
18010 run_test 803 "verify agent object for remote object"
18011
18012 test_804() {
18013         [[ $MDSCOUNT -lt 2 ]] && skip "needs >= 2 MDTs" && return
18014         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.10.54) ] &&
18015                 skip "MDS needs to be newer than 2.10.54" && return
18016
18017         [ "$(facet_fstype $SINGLEMDS)" != "ldiskfs" ] &&
18018                 skip "ldiskfs only test" && return 0
18019
18020         mkdir -p $DIR/$tdir
18021         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
18022                 error "Fail to create $DIR/$tdir/dir0"
18023
18024         local fid=$($LFS path2fid $DIR/$tdir/dir0)
18025         local dev=$(mdsdevname 2)
18026
18027         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18028                 grep ${fid} || error "NOT found agent entry for dir0"
18029
18030         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
18031                 error "Fail to create $DIR/$tdir/dir1"
18032
18033         touch $DIR/$tdir/dir1/foo0 ||
18034                 error "Fail to create $DIR/$tdir/dir1/foo0"
18035         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
18036         local rc=0
18037
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
18045         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
18046                 error "Fail to rename foo0 to foo1"
18047         if [ $rc -eq 0 ]; then
18048                 for idx in $(seq $MDSCOUNT); do
18049                         dev=$(mdsdevname $idx)
18050                         do_facet mds${idx} \
18051                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18052                         grep ${fid} && rc=$idx
18053                 done
18054         fi
18055
18056         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
18057                 error "Fail to rename foo1 to foo2"
18058         if [ $rc -eq 0 ]; then
18059                 for idx in $(seq $MDSCOUNT); do
18060                         dev=$(mdsdevname $idx)
18061                         do_facet mds${idx} \
18062                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
18063                         grep ${fid} && rc=$idx
18064                 done
18065         fi
18066
18067         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
18068
18069         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
18070                 error "Fail to link to $DIR/$tdir/dir1/foo2"
18071         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
18072                 error "Fail to rename foo2 to foo0"
18073         unlink $DIR/$tdir/dir1/foo0 ||
18074                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
18075         rm -rf $DIR/$tdir/dir0 ||
18076                 error "Fail to rm $DIR/$tdir/dir0"
18077
18078         for idx in $(seq $MDSCOUNT); do
18079                 dev=$(mdsdevname $idx)
18080                 rc=0
18081
18082                 stop mds${idx}
18083                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
18084                         rc=$?
18085                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
18086                         error "mount mds$idx failed"
18087                 df $MOUNT > /dev/null 2>&1
18088
18089                 # e2fsck should not return error
18090                 [ $rc -eq 0 ] ||
18091                         error "e2fsck detected error on MDT${idx}: rc=$rc"
18092         done
18093 }
18094 run_test 804 "verify agent entry for remote entry"
18095
18096 #
18097 # tests that do cleanup/setup should be run at the end
18098 #
18099
18100 test_900() {
18101         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18102         local ls
18103         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
18104         $LCTL set_param fail_loc=0x903
18105
18106         cancel_lru_locks MGC
18107
18108         FAIL_ON_ERROR=true cleanup
18109         FAIL_ON_ERROR=true setup
18110 }
18111 run_test 900 "umount should not race with any mgc requeue thread"
18112
18113 complete $SECONDS
18114 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
18115 check_and_cleanup_lustre
18116 if [ "$I_MOUNTED" != "yes" ]; then
18117         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
18118 fi
18119 exit_status